delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / workspace / plasma / scriptengines / google_gadgets / plasma_host.cpp
blobc7ea0eee2f8552246aafb1462eded6a0c6412d22
1 /*
2 Copyright 2008 Google Inc.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <string>
18 #include <QtGui/QGraphicsWidget>
19 #include <QtGui/QFontDatabase>
20 #include <ggadget/common.h>
21 #include <ggadget/logger.h>
22 #include <ggadget/qt/qt_view_host.h>
23 #include <ggadget/script_runtime_manager.h>
24 #include <ggadget/gadget_consts.h>
25 #include <ggadget/decorated_view_host.h>
26 #include <ggadget/docked_main_view_decorator.h>
27 #include <ggadget/popout_main_view_decorator.h>
28 #include <ggadget/details_view_decorator.h>
29 #include <ggadget/permissions.h>
30 #include <ggadget/qt/utilities.h>
31 #include <ggadget/qt/qt_view_host.h>
32 #include <ggadget/gadget.h>
34 #include <Plasma/Applet>
35 #include "plasma_view_host.h"
36 #include "plasma_host.h"
37 #include "panel_decorator.h"
38 #include "floating_decorator.h"
40 namespace ggadget {
42 class PlasmaHost::Private {
43 public:
44 Private(GadgetInfo *i)
45 : info(i),
46 gadget_w_(0),
47 gadget_h_(0) {
48 global_permissions_.SetGranted(Permissions::ALL_ACCESS, true);
51 void onCloseMainViewHandler() {
52 if (info->expanded_main_view_host)
53 onPopInHandler();
54 info->gadget->RemoveMe(true);
57 void onCloseDetailsViewHandler() {
58 info->gadget->CloseDetailsView();
61 void onClosePopOutViewHandler() {
62 onPopInHandler();
65 void onPopOutHandler() {
66 if (info->expanded_main_view_host != NULL) {
67 onPopInHandler();
68 return;
70 ViewInterface *child = info->main_view_host->GetView();
71 ASSERT(child);
72 if (child) {
73 PlasmaViewHost *vh = new PlasmaViewHost(
74 info, ViewHostInterface::VIEW_HOST_MAIN, true);
75 PopOutMainViewDecorator *view_decorator =
76 new PopOutMainViewDecorator(vh);
77 DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
78 view_decorator->ConnectOnClose(
79 NewSlot(this, &Private::onClosePopOutViewHandler));
81 // Send popout event to decorator first.
82 SimpleEvent event(Event::EVENT_POPOUT);
83 info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
85 child->SwitchViewHost(dvh);
86 dvh->ShowView(false, 0, NULL);
88 info->expanded_main_view_host = dvh;
92 void onPopInHandler() {
93 if (!info->expanded_main_view_host) return;
94 ViewInterface *child = info->expanded_main_view_host->GetView();
95 ASSERT(child);
96 if (child) {
97 // Close details view
98 child->GetGadget()->CloseDetailsView();
100 child->SwitchViewHost(info->main_view_host);
101 SimpleEvent event(Event::EVENT_POPIN);
102 info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
103 info->expanded_main_view_host->Destroy();
104 info->expanded_main_view_host = NULL;
108 DecoratedViewHost *newFloatingViewHost() {
109 PlasmaViewHost* vh = new PlasmaViewHost(
110 info, ViewHostInterface::VIEW_HOST_MAIN);
112 FloatingDecorator *decorator = new FloatingDecorator(vh);
113 decorator->ConnectOnClose(NewSlot(this, &Private::onCloseMainViewHandler));
114 decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
115 decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
116 DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
118 DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
119 dvh, vh, decorator);
120 return dvh;
123 DecoratedViewHost *newPanelViewHost() {
124 PlasmaViewHost* vh = new PlasmaViewHost(
125 info, ViewHostInterface::VIEW_HOST_MAIN);
127 PanelDecorator *decorator = new PanelDecorator(vh);
128 if (isHorizontal(info->applet->location()))
129 decorator->setHorizontal();
130 else
131 decorator->setVertical();
132 decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
133 decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
134 DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
135 DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
136 dvh, vh, decorator);
137 return dvh;
140 GadgetInfo *info;
141 Permissions global_permissions_;
142 double gadget_w_, gadget_h_;
145 PlasmaHost::PlasmaHost(GadgetInfo *info)
146 : d(new Private(info)) {
149 PlasmaHost::~PlasmaHost() {
150 delete d;
153 ViewHostInterface *PlasmaHost::NewViewHost(Gadget *,
154 ViewHostInterface::Type type) {
155 if (type == ViewHostInterface::VIEW_HOST_MAIN) {
156 if (d->info->applet->location() == Plasma::Floating) {
157 d->info->main_view_host = d->newFloatingViewHost();
158 } else {
159 d->info->main_view_host = d->newPanelViewHost();
161 return d->info->main_view_host;
162 } else if (type == ViewHostInterface::VIEW_HOST_OPTIONS) {
163 ViewHostInterface* vh = new QtViewHost(type, 1.0, 0, 0, NULL);
164 d->info->options_view_host = vh;
165 return vh;
166 } else {
167 ViewHostInterface* vh = new PlasmaViewHost(d->info, type);
168 DetailsViewDecorator *view_decorator = new DetailsViewDecorator(vh);
169 DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
170 view_decorator->ConnectOnClose(
171 NewSlot(d, &Private::onCloseDetailsViewHandler));
172 d->info->details_view_host = dvh;
173 return dvh;
177 void PlasmaHost::RemoveGadget(Gadget *gadget, bool save_data) {
178 // Please close me through plasma's button
181 bool PlasmaHost::LoadFont(const char *filename) {
182 if (QFontDatabase::addApplicationFont(filename) != -1)
183 return true;
184 else
185 return false;
188 int PlasmaHost::GetDefaultFontSize() {
189 return kDefaultFontSize;
192 bool PlasmaHost::OpenURL(const ggadget::Gadget *gadget, const char *url) {
193 return ggadget::qt::OpenURL(gadget, url);
196 Gadget* PlasmaHost::LoadGadget(const char *path, const char *options_name,
197 int instance_id, bool show_debug_console) {
198 Q_UNUSED(instance_id);
199 Q_UNUSED(show_debug_console);
201 Gadget *gadget = new Gadget(this, path, options_name, 0,
202 d->global_permissions_,
203 Gadget::DEBUG_CONSOLE_DISABLED);
205 if (!gadget->IsValid()) {
206 LOG("Failed to load gadget %s", path);
207 delete gadget;
208 return NULL;
211 if (!gadget->ShowMainView()) {
212 LOG("Failed to show main view of gadget %s", path);
213 delete gadget;
214 d->info->main_view_host = NULL;
215 return NULL;
218 if (gadget->HasOptionsDialog()) {
219 d->info->script->setHasConfigurationInterface(true);
222 return gadget;
225 void PlasmaHost::onConstraintsEvent(Plasma::Constraints constraints) {
226 if (!d->info->main_view_host) return;
228 if (constraints & Plasma::FormFactorConstraint) {
229 // TODO: Do something to handle it right
230 kDebug() << "FormFactorConstraint changed:" << d->info->applet->formFactor();
233 if ((constraints & Plasma::LocationConstraint) &&
234 d->info->applet->location() != d->info->location) {
235 d->onPopInHandler();
236 d->onCloseDetailsViewHandler();
237 Plasma::Location loc = d->info->applet->location();
239 kDebug() << "LocationConstraint changed from " << d->info->location
240 << " to " << loc;
242 if ((d->info->location == Plasma::Floating && loc != Plasma::Floating) ||
243 (d->info->location != Plasma::Floating && loc == Plasma::Floating)) {
244 DecoratedViewHost *vh;
245 if (loc == Plasma::Floating)
246 vh = d->newFloatingViewHost();
247 else
248 vh = d->newPanelViewHost();
250 // Send popout event here so elements like browser_element will know
251 // about it and they will hide themselves.
252 SimpleEvent event(Event::EVENT_POPOUT);
253 d->info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
255 ViewInterface *child = d->info->main_view_host->GetView();
256 ViewHostInterface *old = child->SwitchViewHost(vh);
257 old->Destroy();
259 d->info->main_view_host = vh;
260 SimpleEvent event1(Event::EVENT_POPIN);
261 vh->GetViewDecorator()->OnOtherEvent(event1);
263 // Must call it to get the aspectRatioMode of applet right.
264 // Maybe we can do it nicely in GGL.
265 vh->GetViewDecorator()->GetViewHost()->SetResizable(
266 vh->GetViewDecorator()->GetResizable());
268 vh->ShowView(false, 0, NULL);
269 } else if (isVertical(d->info->location) != isVertical(loc)) {
270 PanelDecorator *decorator = static_cast<PanelDecorator*>(
271 d->info->main_view_host->GetViewDecorator());
272 if (isVertical(loc))
273 decorator->setVertical();
274 else
275 decorator->setHorizontal();
277 d->info->location = loc;
278 return;
281 if (constraints & Plasma::SizeConstraint) {
282 ViewInterface *view = d->info->main_view_host->GetViewDecorator();
283 if (!view) return;
284 QSizeF s = d->info->applet->size();
285 kDebug() << "size requested:" << s;
286 double w = s.width();
287 double h = s.height();
289 if (view->OnSizing(&w, &h)) {
290 kDebug() << "Original view size:" << view->GetWidth()
291 << " " << view->GetHeight();
292 view->SetSize(w, h);
293 kDebug() << "view size change to:" << w << " " << h;
298 } // namespace ggadget