00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <string>
00018 #include <QtGui/QGraphicsWidget>
00019 #include <QtGui/QFontDatabase>
00020 #include <ggadget/common.h>
00021 #include <ggadget/logger.h>
00022 #include <ggadget/qt/qt_view_host.h>
00023 #include <ggadget/script_runtime_manager.h>
00024 #include <ggadget/gadget_consts.h>
00025 #include <ggadget/decorated_view_host.h>
00026 #include <ggadget/docked_main_view_decorator.h>
00027 #include <ggadget/popout_main_view_decorator.h>
00028 #include <ggadget/details_view_decorator.h>
00029 #include <ggadget/permissions.h>
00030 #include <ggadget/qt/utilities.h>
00031 #include <ggadget/qt/qt_view_host.h>
00032 #include <ggadget/gadget.h>
00033
00034 #include <Plasma/Applet>
00035 #include "plasma_view_host.h"
00036 #include "plasma_host.h"
00037 #include "panel_decorator.h"
00038 #include "floating_decorator.h"
00039
00040 namespace ggadget {
00041
00042 class PlasmaHost::Private {
00043 public:
00044 Private(GadgetInfo *i)
00045 : info(i),
00046 gadget_w_(0),
00047 gadget_h_(0) {
00048 global_permissions_.SetGranted(Permissions::ALL_ACCESS, true);
00049 }
00050
00051 void onCloseMainViewHandler() {
00052 if (info->expanded_main_view_host)
00053 onPopInHandler();
00054 info->gadget->RemoveMe(true);
00055 }
00056
00057 void onCloseDetailsViewHandler() {
00058 info->gadget->CloseDetailsView();
00059 }
00060
00061 void onClosePopOutViewHandler() {
00062 onPopInHandler();
00063 }
00064
00065 void onPopOutHandler() {
00066 if (info->expanded_main_view_host != NULL) {
00067 onPopInHandler();
00068 return;
00069 }
00070 ViewInterface *child = info->main_view_host->GetView();
00071 ASSERT(child);
00072 if (child) {
00073 PlasmaViewHost *vh = new PlasmaViewHost(
00074 info, ViewHostInterface::VIEW_HOST_MAIN, true);
00075 PopOutMainViewDecorator *view_decorator =
00076 new PopOutMainViewDecorator(vh);
00077 DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
00078 view_decorator->ConnectOnClose(
00079 NewSlot(this, &Private::onClosePopOutViewHandler));
00080
00081
00082 SimpleEvent event(Event::EVENT_POPOUT);
00083 info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
00084
00085 child->SwitchViewHost(dvh);
00086 dvh->ShowView(false, 0, NULL);
00087
00088 info->expanded_main_view_host = dvh;
00089 }
00090 }
00091
00092 void onPopInHandler() {
00093 if (!info->expanded_main_view_host) return;
00094 ViewInterface *child = info->expanded_main_view_host->GetView();
00095 ASSERT(child);
00096 if (child) {
00097
00098 child->GetGadget()->CloseDetailsView();
00099
00100 child->SwitchViewHost(info->main_view_host);
00101 SimpleEvent event(Event::EVENT_POPIN);
00102 info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
00103 info->expanded_main_view_host->Destroy();
00104 info->expanded_main_view_host = NULL;
00105 }
00106 }
00107
00108 DecoratedViewHost *newFloatingViewHost() {
00109 PlasmaViewHost* vh = new PlasmaViewHost(
00110 info, ViewHostInterface::VIEW_HOST_MAIN);
00111
00112 FloatingDecorator *decorator = new FloatingDecorator(vh);
00113 decorator->ConnectOnClose(NewSlot(this, &Private::onCloseMainViewHandler));
00114 decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
00115 decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
00116 DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
00117
00118 DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
00119 dvh, vh, decorator);
00120 return dvh;
00121 }
00122
00123 DecoratedViewHost *newPanelViewHost() {
00124 PlasmaViewHost* vh = new PlasmaViewHost(
00125 info, ViewHostInterface::VIEW_HOST_MAIN);
00126
00127 PanelDecorator *decorator = new PanelDecorator(vh);
00128 if (isHorizontal(info->applet->location()))
00129 decorator->setHorizontal();
00130 else
00131 decorator->setVertical();
00132 decorator->ConnectOnPopOut(NewSlot(this, &Private::onPopOutHandler));
00133 decorator->ConnectOnPopIn(NewSlot(this, &Private::onPopInHandler));
00134 DecoratedViewHost *dvh = new DecoratedViewHost(decorator);
00135 DLOG("NewViewHost: dvh(%p), pvh(%p), vd(%p)",
00136 dvh, vh, decorator);
00137 return dvh;
00138 }
00139
00140 GadgetInfo *info;
00141 Permissions global_permissions_;
00142 double gadget_w_, gadget_h_;
00143 };
00144
00145 PlasmaHost::PlasmaHost(GadgetInfo *info)
00146 : d(new Private(info)) {
00147 }
00148
00149 PlasmaHost::~PlasmaHost() {
00150 delete d;
00151 }
00152
00153 ViewHostInterface *PlasmaHost::NewViewHost(Gadget *,
00154 ViewHostInterface::Type type) {
00155 if (type == ViewHostInterface::VIEW_HOST_MAIN) {
00156 if (d->info->applet->location() == Plasma::Floating) {
00157 d->info->main_view_host = d->newFloatingViewHost();
00158 } else {
00159 d->info->main_view_host = d->newPanelViewHost();
00160 }
00161 return d->info->main_view_host;
00162 } else if (type == ViewHostInterface::VIEW_HOST_OPTIONS) {
00163 ViewHostInterface* vh = new QtViewHost(type, 1.0, 0, 0, NULL);
00164 d->info->options_view_host = vh;
00165 return vh;
00166 } else {
00167 ViewHostInterface* vh = new PlasmaViewHost(d->info, type);
00168 DetailsViewDecorator *view_decorator = new DetailsViewDecorator(vh);
00169 DecoratedViewHost *dvh = new DecoratedViewHost(view_decorator);
00170 view_decorator->ConnectOnClose(
00171 NewSlot(d, &Private::onCloseDetailsViewHandler));
00172 d->info->details_view_host = dvh;
00173 return dvh;
00174 }
00175 }
00176
00177 void PlasmaHost::RemoveGadget(Gadget *gadget, bool save_data) {
00178
00179 }
00180
00181 bool PlasmaHost::LoadFont(const char *filename) {
00182 if (QFontDatabase::addApplicationFont(filename) != -1)
00183 return true;
00184 else
00185 return false;
00186 }
00187
00188 int PlasmaHost::GetDefaultFontSize() {
00189 return kDefaultFontSize;
00190 }
00191
00192 bool PlasmaHost::OpenURL(const ggadget::Gadget *gadget, const char *url) {
00193 return ggadget::qt::OpenURL(gadget, url);
00194 }
00195
00196 Gadget* PlasmaHost::LoadGadget(const char *path, const char *options_name,
00197 int instance_id, bool show_debug_console) {
00198 Q_UNUSED(instance_id);
00199 Q_UNUSED(show_debug_console);
00200
00201 Gadget *gadget = new Gadget(this, path, options_name, 0,
00202 d->global_permissions_,
00203 Gadget::DEBUG_CONSOLE_DISABLED);
00204
00205 if (!gadget->IsValid()) {
00206 LOG("Failed to load gadget %s", path);
00207 delete gadget;
00208 return NULL;
00209 }
00210
00211 if (!gadget->ShowMainView()) {
00212 LOG("Failed to show main view of gadget %s", path);
00213 delete gadget;
00214 d->info->main_view_host = NULL;
00215 return NULL;
00216 }
00217
00218 if (gadget->HasOptionsDialog()) {
00219 d->info->script->setHasConfigurationInterface(true);
00220 }
00221
00222 return gadget;
00223 }
00224
00225 void PlasmaHost::onConstraintsEvent(Plasma::Constraints constraints) {
00226 if (!d->info->main_view_host) return;
00227
00228 if (constraints & Plasma::FormFactorConstraint) {
00229
00230 kDebug() << "FormFactorConstraint changed:" << d->info->applet->formFactor();
00231 }
00232
00233 if ((constraints & Plasma::LocationConstraint) &&
00234 d->info->applet->location() != d->info->location) {
00235 d->onPopInHandler();
00236 d->onCloseDetailsViewHandler();
00237 Plasma::Location loc = d->info->applet->location();
00238
00239 kDebug() << "LocationConstraint changed from " << d->info->location
00240 << " to " << loc;
00241
00242 if ((d->info->location == Plasma::Floating && loc != Plasma::Floating) ||
00243 (d->info->location != Plasma::Floating && loc == Plasma::Floating)) {
00244 DecoratedViewHost *vh;
00245 if (loc == Plasma::Floating)
00246 vh = d->newFloatingViewHost();
00247 else
00248 vh = d->newPanelViewHost();
00249
00250
00251
00252 SimpleEvent event(Event::EVENT_POPOUT);
00253 d->info->main_view_host->GetViewDecorator()->OnOtherEvent(event);
00254
00255 ViewInterface *child = d->info->main_view_host->GetView();
00256 ViewHostInterface *old = child->SwitchViewHost(vh);
00257 old->Destroy();
00258
00259 d->info->main_view_host = vh;
00260 SimpleEvent event1(Event::EVENT_POPIN);
00261 vh->GetViewDecorator()->OnOtherEvent(event1);
00262
00263
00264
00265 vh->GetViewDecorator()->GetViewHost()->SetResizable(
00266 vh->GetViewDecorator()->GetResizable());
00267
00268 vh->ShowView(false, 0, NULL);
00269 } else if (isVertical(d->info->location) != isVertical(loc)) {
00270 PanelDecorator *decorator = static_cast<PanelDecorator*>(
00271 d->info->main_view_host->GetViewDecorator());
00272 if (isVertical(loc))
00273 decorator->setVertical();
00274 else
00275 decorator->setHorizontal();
00276 }
00277 d->info->location = loc;
00278 return;
00279 }
00280
00281 if (constraints & Plasma::SizeConstraint) {
00282 ViewInterface *view = d->info->main_view_host->GetViewDecorator();
00283 if (!view) return;
00284 QSizeF s = d->info->applet->size();
00285 kDebug() << "size requested:" << s;
00286 double w = s.width();
00287 double h = s.height();
00288
00289 if (view->OnSizing(&w, &h)) {
00290 kDebug() << "Original view size:" << view->GetWidth()
00291 << " " << view->GetHeight();
00292 view->SetSize(w, h);
00293 kDebug() << "view size change to:" << w << " " << h;
00294 }
00295 }
00296 }
00297
00298 }