~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/google_gadgets/plasma_host.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright 2008 Google Inc.
 
3
 
 
4
  Licensed under the Apache License, Version 2.0 (the "License");
 
5
  you may not use this file except in compliance with the License.
 
6
  You may obtain a copy of the License at
 
7
 
 
8
       http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
10
  Unless required by applicable law or agreed to in writing, software
 
11
  distributed under the License is distributed on an "AS IS" BASIS,
 
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
  See the License for the specific language governing permissions and
 
14
  limitations under the License.
 
15
*/
 
16
#include "plasma_host.h"
 
17
 
 
18
#include <string>
 
19
#include <QGraphicsWidget>
 
20
#include <QGraphicsProxyWidget>
 
21
#include <QtGui/QFontDatabase>
 
22
#include <ggadget/common.h>
 
23
#include <ggadget/logger.h>
 
24
#include <ggadget/qt/qt_view_host.h>
 
25
#include <ggadget/script_runtime_manager.h>
 
26
#include <ggadget/gadget_consts.h>
 
27
#include <ggadget/decorated_view_host.h>
 
28
#include <ggadget/docked_main_view_decorator.h>
 
29
#include <ggadget/details_view_decorator.h>
 
30
#include <ggadget/permissions.h>
 
31
#include <ggadget/qt/utilities.h>
 
32
#include <ggadget/qt/qt_view_host.h>
 
33
#include <ggadget/gadget.h>
 
34
 
 
35
#include <KDebug>
 
36
 
 
37
#include <Plasma/Applet>
 
38
#include <Plasma/ToolTipManager>
 
39
#include "plasma_view_host.h"
 
40
#include "panel_decorator.h"
 
41
#include "popout_decorator.h"
 
42
#include "floating_decorator.h"
 
43
 
 
44
namespace ggadget {
 
45
 
 
46
class PlasmaHost::Private {
 
47
 public:
 
48
  Private(GadgetInfo *i)
 
49
      : info(i),
 
50
        gadget_w_(0),
 
51
        gadget_h_(0) {
 
52
    global_permissions_.SetGranted(Permissions::ALL_ACCESS, true);
 
53
  }
 
54
 
 
55
  void onCloseMainViewHandler() {
 
56
    if (info->expanded_main_view_host)
 
57
      onPopInHandler();
 
58
    info->gadget->RemoveMe(true);
 
59
  }
 
60
 
 
61
  void onCloseDetailsViewHandler() {
 
62
    info->gadget->CloseDetailsView();
 
63
  }
 
64
 
 
65
  void onClosePopOutViewHandler() {
 
66
    onPopInHandler();
 
67
  }
 
68
 
 
69
  void onPopOutHandler() {
 
70
    if (info->expanded_main_view_host != NULL) {
 
71
      onPopInHandler();
 
72
      return;
 
73
    }
 
74
    ViewInterface *child = info->main_view_host->GetView();
 
75
    ASSERT(child);
 
76
    if (child) {
 
77
      PlasmaViewHost *vh = new PlasmaViewHost(
 
78
              info, ViewHostInterface::VIEW_HOST_MAIN, true);
 
79
      PopOutDecorator *view_decorator = new PopOutDecorator(vh);
 
80
      DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
 
81
      view_decorator->ConnectOnClose(
 
82
          NewSlot(this, &Private::onClosePopOutViewHandler));
 
83
 
 
84
      // Send popout event to decorator first.
 
85
      SimpleEvent event(Event::EVENT_POPOUT);
 
86
      info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
 
87
 
 
88
      child->SwitchViewHost(dvh);
 
89
      dvh->ShowView(false, 0, NULL);
 
90
 
 
91
      info->expanded_main_view_host = dvh;
 
92
    }
 
93
  }
 
94
 
 
95
  void onPopInHandler() {
 
96
    if (!info->expanded_main_view_host) return;
 
97
    ViewInterface *child = info->expanded_main_view_host->GetView();
 
98
    ASSERT(child);
 
99
    if (child) {
 
100
      // Close details view
 
101
      child->GetGadget()->CloseDetailsView();
 
102
 
 
103
      child->SwitchViewHost(info->main_view_host);
 
104
      SimpleEvent event(Event::EVENT_POPIN);
 
105
      info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
 
106
      info->expanded_main_view_host->Destroy();
 
107
      info->expanded_main_view_host = NULL;
 
108
    }
 
109
  }
 
110
 
 
111
  DecoratedViewHost *newFloatingViewHost() {
 
112
    PlasmaViewHost* vh =  new PlasmaViewHost(
 
113
        info, ViewHostInterface::VIEW_HOST_MAIN);
 
114
 
 
115
    FloatingDecorator *decorator = new FloatingDecorator(vh);
 
116
    decorator->ConnectOnClose(NewSlot(this, &Private::onCloseMainViewHandler));
 
117
    decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
 
118
    decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
 
119
    DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
 
120
 
 
121
    DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
 
122
         dvh, vh, decorator);
 
123
    return dvh;
 
124
  }
 
125
 
 
126
  DecoratedViewHost *newPanelViewHost(bool vertical) {
 
127
    PlasmaViewHost* vh =  new PlasmaViewHost(
 
128
        info, ViewHostInterface::VIEW_HOST_MAIN);
 
129
 
 
130
    PanelDecorator *decorator = new PanelDecorator(vh, vertical);
 
131
    decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
 
132
    decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
 
133
    DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
 
134
 
 
135
    DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
 
136
         dvh, vh, decorator);
 
137
    return dvh;
 
138
  }
 
139
 
 
140
  GadgetInfo *info;
 
141
  Permissions global_permissions_;
 
142
  double gadget_w_, gadget_h_;
 
143
};
 
144
 
 
145
PlasmaHost::PlasmaHost(GadgetInfo *info)
 
146
  : d(new Private(info)) {
 
147
}
 
148
 
 
149
PlasmaHost::~PlasmaHost() {
 
150
  delete d;
 
151
}
 
152
 
 
153
ViewHostInterface *PlasmaHost::NewViewHost(Gadget *,
 
154
                                           ViewHostInterface::Type type) {
 
155
  if (type == ViewHostInterface::VIEW_HOST_MAIN) {
 
156
    Plasma::Location loc = d->info->applet->location();
 
157
    if (loc == Plasma::Floating) {
 
158
      d->info->main_view_host = d->newFloatingViewHost();
 
159
    } else {
 
160
      d->info->main_view_host = d->newPanelViewHost(isVertical(loc));
 
161
    }
 
162
    return d->info->main_view_host;
 
163
  } else if (type == ViewHostInterface::VIEW_HOST_OPTIONS) {
 
164
    ViewHostInterface* vh =  new QtViewHost(type, 1.0, 0, 0, NULL);
 
165
    d->info->options_view_host = vh;
 
166
    return vh;
 
167
  } else {
 
168
    ViewHostInterface* vh =  new PlasmaViewHost(d->info, type);
 
169
    DetailsViewDecorator *view_decorator = new DetailsViewDecorator(vh);
 
170
    DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
 
171
    view_decorator->ConnectOnClose(
 
172
        NewSlot(d, &Private::onCloseDetailsViewHandler));
 
173
    d->info->details_view_host = dvh;
 
174
    return dvh;
 
175
  }
 
176
}
 
177
 
 
178
void PlasmaHost::RemoveGadget(Gadget *gadget, bool save_data) {
 
179
  // Please close me through plasma's button
 
180
  Q_UNUSED(gadget);
 
181
  Q_UNUSED(save_data);
 
182
}
 
183
 
 
184
bool PlasmaHost::LoadFont(const char *filename) {
 
185
  if (QFontDatabase::addApplicationFont(filename) != -1)
 
186
    return true;
 
187
  else
 
188
    return false;
 
189
}
 
190
 
 
191
int PlasmaHost::GetDefaultFontSize() {
 
192
  return kDefaultFontSize;
 
193
}
 
194
 
 
195
bool PlasmaHost::OpenURL(const ggadget::Gadget *gadget, const char *url) {
 
196
    return ggadget::qt::OpenURL(gadget, url);
 
197
}
 
198
 
 
199
Gadget* PlasmaHost::LoadGadget(const char *path, const char *options_name,
 
200
                               int instance_id, bool show_debug_console) {
 
201
  Q_UNUSED(instance_id);
 
202
  Q_UNUSED(show_debug_console);
 
203
 
 
204
  Gadget *gadget = new Gadget(this, path, options_name, 0,
 
205
                              d->global_permissions_,
 
206
                              Gadget::DEBUG_CONSOLE_DISABLED);
 
207
 
 
208
  if (!gadget->IsValid()) {
 
209
    LOG("Failed to load gadget %s", path);
 
210
    delete gadget;
 
211
    return NULL;
 
212
  }
 
213
 
 
214
  if (!gadget->ShowMainView()) {
 
215
    LOG("Failed to show main view of gadget %s", path);
 
216
    delete gadget;
 
217
    d->info->main_view_host = NULL;
 
218
    return NULL;
 
219
  }
 
220
 
 
221
  if (gadget->HasOptionsDialog()) {
 
222
    d->info->script->setHasConfigurationInterface(true);
 
223
  }
 
224
 
 
225
  return gadget;
 
226
}
 
227
 
 
228
void PlasmaHost::onConstraintsEvent(Plasma::Constraints constraints) {
 
229
  if (!d->info->main_view_host) return;
 
230
 
 
231
  if (constraints & Plasma::FormFactorConstraint) {
 
232
    // TODO: Do something to handle it right
 
233
    kDebug() << "FormFactorConstraint changed:" << d->info->applet->formFactor();
 
234
  }
 
235
 
 
236
  if ((constraints & Plasma::LocationConstraint) &&
 
237
      d->info->applet->location() != d->info->location) {
 
238
    // disable tip.
 
239
    // It will be enabled when PanelDecorator is on horizontal panel.
 
240
    Plasma::ToolTipManager::self()->unregisterWidget(d->info->applet);
 
241
 
 
242
    d->onPopInHandler();
 
243
    d->onCloseDetailsViewHandler();
 
244
    Plasma::Location loc = d->info->applet->location();
 
245
 
 
246
    kDebug() << "LocationConstraint changed from " << d->info->location
 
247
             << " to " << loc;
 
248
 
 
249
    DecoratedViewHost *vh;
 
250
    if (loc == Plasma::Floating) {
 
251
      vh = d->newFloatingViewHost();
 
252
    } else {
 
253
      vh = d->newPanelViewHost(isVertical(loc));
 
254
    }
 
255
 
 
256
    // Send popout event here so elements like browser_element will know
 
257
    // about it and they will hide themselves.
 
258
    SimpleEvent event(Event::EVENT_POPOUT);
 
259
    d->info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
 
260
 
 
261
    ViewInterface *child = d->info->main_view_host->GetView();
 
262
    ViewHostInterface *old = child->SwitchViewHost(vh);
 
263
    old->Destroy();
 
264
 
 
265
    d->info->main_view_host = vh;
 
266
    SimpleEvent event1(Event::EVENT_POPIN);
 
267
    vh->GetViewDecorator()->OnOtherEvent(event1);
 
268
 
 
269
    // Must call it to get the aspectRatioMode of applet right.
 
270
    // Maybe we can do it nicely in GGL.
 
271
    vh->GetViewDecorator()->GetViewHost()->SetResizable(
 
272
            vh->GetViewDecorator()->GetResizable());
 
273
 
 
274
    vh->ShowView(false, 0, NULL);
 
275
    d->info->location = loc;
 
276
    return;
 
277
  }
 
278
 
 
279
  if (constraints & Plasma::SizeConstraint) {
 
280
    ViewInterface *view = d->info->main_view_host->GetViewDecorator();
 
281
    if (!view || !d->info->main_view_widget || !d->info->proxy) return;
 
282
 
 
283
    QSizeF s = d->info->applet->size();
 
284
    kDebug() << "size requested:" << s;
 
285
    double w = s.width();
 
286
    double h = s.height();
 
287
    double old_w = view->GetWidth();
 
288
    double old_h = view->GetHeight();
 
289
    if (w == old_w && h == old_h) {
 
290
      d->info->main_view_widget->resize(w, h);
 
291
      d->info->proxy->resize(s);
 
292
      return;
 
293
    }
 
294
 
 
295
    if (view->OnSizing(&w, &h)) {
 
296
      view->SetSize(w, h);
 
297
    }
 
298
  }
 
299
}
 
300
 
 
301
} // namespace ggadget