"Fossies" - the Fresh Open Source Software Archive

Member "google-gadgets-for-linux-0.11.2/hosts/qt/qt_host_internal.h" (18 Jan 2010, 14011 Bytes) of package /linux/misc/old/google-gadgets-for-linux-0.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 #ifndef HOSTS_QT_HOST_INTERNAL_H__
   17 #define HOSTS_QT_HOST_INTERNAL_H__
   18 
   19 #include <QtGui/QCursor>
   20 #include <ggadget/host_utils.h>
   21 
   22 using namespace ggadget;
   23 using namespace ggadget::qt;
   24 
   25 namespace hosts {
   26 namespace qt {
   27 
   28 class QtHost::Impl : public QObject {
   29   Q_OBJECT
   30  public:
   31   struct GadgetInfo {
   32     GadgetInfo() : gadget(NULL), menu_item(NULL), debug_console(NULL) {}
   33 
   34     ~GadgetInfo() {
   35       delete menu_item;
   36       delete debug_console;
   37       if (gadget) gadget->CloseMainView();
   38       delete gadget;
   39     }
   40     Gadget *gadget;
   41     QAction *menu_item;
   42     QWidget* debug_console;
   43   };
   44   Impl(QtHost *host, bool composite,
   45        int view_debug_mode,
   46        Gadget::DebugConsoleConfig debug_console_config)
   47     : gadget_manager_(GetGadgetManager()),
   48       gadget_browser_host_(host, view_debug_mode),
   49       host_(host),
   50       view_debug_mode_(view_debug_mode),
   51       debug_console_config_(debug_console_config),
   52       composite_(composite),
   53       gadgets_shown_(true),
   54       gadgets_menu_separator_(NULL),
   55       expanded_popout_(NULL),
   56       expanded_original_(NULL),
   57       show_(true) {
   58     // Initializes global permissions.
   59     // FIXME: Supports customizable global permissions.
   60     global_permissions_.SetGranted(Permissions::ALL_ACCESS, true);
   61     SetupUI();
   62   }
   63 
   64   void SetupUI() {
   65     qApp->setQuitOnLastWindowClosed(false);
   66     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_ADD_GADGETS")),
   67                     this, SLOT(OnAddGadget()));
   68     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_ADD_IGOOGLE_GADGET")),
   69                     this, SLOT(OnAddIGoogleGadget()));
   70     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_SHOW_ALL")),
   71                     this, SLOT(OnShowAll()));
   72     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_HIDE_ALL")),
   73                     this, SLOT(OnHideAll()));
   74     menu_.addSeparator();
   75     gadgets_menu_separator_ = menu_.addSeparator();
   76     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_ABOUT")),
   77                     this, SLOT(OnAbout()));
   78     menu_.addAction(QString::fromUtf8(GM_("MENU_ITEM_EXIT")),
   79                     qApp, SLOT(quit()));
   80     tray_.setContextMenu(&menu_);
   81     QObject::connect(&tray_,
   82                      SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
   83                      this,
   84                      SLOT(OnTrayActivated(QSystemTrayIcon::ActivationReason)));
   85     std::string icon_data;
   86     if (GetGlobalFileManager()->ReadFile(kGadgetsIcon, &icon_data)) {
   87       QPixmap pixmap;
   88       pixmap.loadFromData(reinterpret_cast<const uchar *>(icon_data.c_str()),
   89                           static_cast<int>(icon_data.length()));
   90       tray_.setIcon(pixmap);
   91     }
   92     tray_.show();
   93   }
   94 
   95   void InitGadgets() {
   96     gadget_manager_->ConnectOnNewGadgetInstance(
   97         NewSlot(this, &Impl::NewGadgetInstanceCallback));
   98     gadget_manager_->EnumerateGadgetInstances(
   99         NewSlot(this, &Impl::EnumerateGadgetInstancesCallback));
  100     gadget_manager_->ConnectOnRemoveGadgetInstance(
  101         NewSlot(this, &Impl::RemoveGadgetInstanceCallback));
  102   }
  103 
  104   bool EnumerateGadgetInstancesCallback(int id) {
  105     if (!LoadGadgetInstance(id))
  106       gadget_manager_->RemoveGadgetInstance(id);
  107     // Return true to continue the enumeration.
  108     return true;
  109   }
  110 
  111   bool NewGadgetInstanceCallback(int id) {
  112     return LoadGadgetInstance(id);
  113   }
  114 
  115   bool LoadGadgetInstance(int id) {
  116     bool result = false;
  117     if (ggadget::qt::ConfirmGadget(gadget_manager_, id)) {
  118       std::string options = gadget_manager_->GetGadgetInstanceOptionsName(id);
  119       std::string path = gadget_manager_->GetGadgetInstancePath(id);
  120       if (options.length() && path.length()) {
  121         result = (LoadGadget(path.c_str(), options.c_str(), id, false) != NULL);
  122         DLOG("QtHost: Load gadget %s, with option %s, %s",
  123              path.c_str(), options.c_str(), result ? "succeeded" : "failed");
  124       }
  125     } else {
  126       QMessageBox::information(
  127           NULL,
  128           QString::fromUtf8(GM_("GOOGLE_GADGETS")),
  129           QString::fromUtf8(
  130               StringPrintf(
  131                   GM_("GADGET_LOAD_FAILURE"),
  132                   gadget_manager_->GetGadgetInstancePath(id).c_str()).c_str()));
  133     }
  134     return result;
  135   }
  136 
  137   Gadget *LoadGadget(const char *path, const char *options_name,
  138                      int instance_id, bool show_debug_console) {
  139     if (gadgets_.find(instance_id) != gadgets_.end()) {
  140       // Gadget is already loaded.
  141       return gadgets_[instance_id].gadget;
  142     }
  143 
  144     // Create GadgetInfo here so when during Gadget creation, if DebugConsole
  145     // shall be created, this information will be ready.
  146     GadgetInfo *info = &gadgets_[instance_id];
  147 
  148     Gadget::DebugConsoleConfig dcc = show_debug_console ?
  149         Gadget::DEBUG_CONSOLE_INITIAL : debug_console_config_;
  150 
  151     Gadget *gadget = new Gadget(host_, path, options_name, instance_id,
  152                                 global_permissions_, dcc);
  153     info->gadget = gadget;
  154 
  155     if (!gadget->IsValid()) {
  156       LOG("Failed to load gadget %s", path);
  157       gadgets_.erase(instance_id);
  158       return NULL;
  159     }
  160 
  161     SetupGadgetGetFeedbackURLHandler(gadget);
  162 
  163     gadget->SetDisplayTarget(Gadget::TARGET_FLOATING_VIEW);
  164     gadget->GetMainView()->OnOtherEvent(SimpleEvent(Event::EVENT_UNDOCK));
  165 
  166     if (!gadget->ShowMainView()) {
  167       LOG("Failed to show main view of gadget %s", path);
  168       gadgets_.erase(instance_id);
  169       return NULL;
  170     }
  171     InsertGadgetToMenu(info);
  172     return gadget;
  173   }
  174 
  175   ViewHostInterface *NewViewHost(Gadget *gadget,
  176                                  ViewHostInterface::Type type) {
  177     QWidget *parent = NULL;
  178     QtViewHost::Flags flags;
  179 
  180     if (composite_)
  181       flags |= QtViewHost::FLAG_COMPOSITE;
  182 
  183     if (type == ViewHostInterface::VIEW_HOST_DETAILS)
  184       parent = static_cast<QWidget*>(gadget->GetMainView()->GetNativeWidget());
  185     else if (type == ViewHostInterface::VIEW_HOST_MAIN)
  186       flags |= QtViewHost::FLAG_RECORD_STATES;
  187 
  188     QtViewHost *qvh = new QtViewHost(
  189         type, 1.0, flags, view_debug_mode_, parent);
  190     QObject::connect(this, SIGNAL(show(bool, Gadget*)),
  191                      qvh->GetQObject(), SLOT(OnShow(bool, Gadget*)));
  192 
  193     if (type == ViewHostInterface::VIEW_HOST_OPTIONS)
  194       return qvh;
  195 
  196     DecoratedViewHost *dvh;
  197 
  198     if (type == ViewHostInterface::VIEW_HOST_MAIN) {
  199       FloatingMainViewDecorator *view_decorator =
  200           new FloatingMainViewDecorator(qvh, composite_);
  201       dvh = new DecoratedViewHost(view_decorator);
  202       view_decorator->ConnectOnClose(
  203           NewSlot(this, &Impl::OnCloseMainViewHandler, dvh));
  204       view_decorator->ConnectOnPopOut(
  205           NewSlot(this, &Impl::OnPopOutHandler, dvh));
  206       view_decorator->ConnectOnPopIn(
  207           NewSlot(this, &Impl::OnPopInHandler, dvh));
  208       view_decorator->SetButtonVisible(MainViewDecoratorBase::POP_IN_OUT_BUTTON,
  209                                        false);
  210     } else {
  211       DetailsViewDecorator *view_decorator = new DetailsViewDecorator(qvh);
  212       dvh = new DecoratedViewHost(view_decorator);
  213       view_decorator->ConnectOnClose(
  214           NewSlot(this, &Impl::OnCloseDetailsViewHandler, dvh));
  215     }
  216 
  217     return dvh;
  218   }
  219 
  220   void RemoveGadget(Gadget *gadget, bool save_data) {
  221     GGL_UNUSED(save_data);
  222     ViewInterface *main_view = gadget->GetMainView();
  223 
  224     // If this gadget is popped out, popin it first.
  225     if (main_view->GetViewHost() == expanded_popout_) {
  226       OnPopInHandler(expanded_original_);
  227     }
  228 
  229     int id = gadget->GetInstanceID();
  230     // If RemoveGadgetInstance() returns false, then means this instance is not
  231     // installed by gadget manager.
  232     if (!gadget_manager_->RemoveGadgetInstance(id))
  233       RemoveGadgetInstanceCallback(id);
  234   }
  235 
  236   void RemoveGadgetInstanceCallback(int instance_id) {
  237     GadgetsMap::iterator it = gadgets_.find(instance_id);
  238     if (it != gadgets_.end()) {
  239       DLOG("Close Gadget: %s",
  240            it->second.gadget->GetManifestInfo(kManifestName).c_str());
  241       RemoveGadgetFromMenu(&it->second);
  242       gadgets_.erase(it);
  243     } else {
  244       LOG("Can't find gadget instance %d", instance_id);
  245     }
  246   }
  247 
  248   void InsertGadgetToMenu(GadgetInfo *info) {
  249     std::string name = info->gadget->GetManifestInfo(kManifestName);
  250     info->menu_item = new QAction(QString::fromUtf8(name.c_str()), this);
  251     QObject::connect(info->menu_item, SIGNAL(triggered()),
  252                      this, SLOT(OnGadgetMenuItem()));
  253     menu_.insertAction(gadgets_menu_separator_, info->menu_item);
  254     gadget_menu_map_[info->menu_item] = info->gadget;
  255   }
  256 
  257   void RemoveGadgetFromMenu(GadgetInfo *info) {
  258     gadget_menu_map_.remove(info->menu_item);
  259     menu_.removeAction(info->menu_item);
  260   }
  261 
  262   void OnCloseMainViewHandler(DecoratedViewHost *decorated) {
  263     // Closing a main view which has popout view causes the popout view close
  264     // first
  265     if (expanded_original_ == decorated && expanded_popout_)
  266       OnPopInHandler(decorated);
  267 
  268     ViewInterface *child = decorated->GetView();
  269     Gadget *gadget = child ? child->GetGadget() : NULL;
  270 
  271     if (gadget) gadget->RemoveMe(true);
  272   }
  273 
  274   void OnClosePopOutViewHandler(DecoratedViewHost *decorated) {
  275     if (expanded_original_ && expanded_popout_ == decorated)
  276       OnPopInHandler(expanded_original_);
  277   }
  278 
  279   void OnCloseDetailsViewHandler(DecoratedViewHost *decorated) {
  280     ViewInterface *child = decorated->GetView();
  281     Gadget *gadget = child ? child->GetGadget() : NULL;
  282     if (gadget)
  283       gadget->CloseDetailsView();
  284   }
  285 
  286   void OnPopOutHandler(DecoratedViewHost *decorated) {
  287     if (expanded_original_) {
  288       bool just_hide = (decorated == expanded_original_);
  289       OnPopInHandler(expanded_original_);
  290       if (just_hide) return;
  291     }
  292 
  293     ViewInterface *child = decorated->GetView();
  294     ASSERT(child);
  295     if (child) {
  296       expanded_original_ = decorated;
  297       QtViewHost *qvh = new QtViewHost(
  298           ViewHostInterface::VIEW_HOST_MAIN, 1.0,
  299           composite_ ?
  300           QtViewHost::FLAG_COMPOSITE :
  301           QtViewHost::FLAG_NONE,
  302           view_debug_mode_,
  303           static_cast<QWidget*>(decorated->GetNativeWidget()));
  304       QObject::connect(this, SIGNAL(show(bool, Gadget*)),
  305                        qvh->GetQObject(), SLOT(OnShow(bool, Gadget*)));
  306       // qvh->ConnectOnBeginMoveDrag(NewSlot(this, &Impl::HandlePopoutViewMove));
  307       PopOutMainViewDecorator *view_decorator =
  308           new PopOutMainViewDecorator(qvh);
  309       expanded_popout_ = new DecoratedViewHost(view_decorator);
  310       view_decorator->ConnectOnClose(
  311           NewSlot(this, &Impl::OnClosePopOutViewHandler, expanded_popout_));
  312 
  313       // Send popout event to decorator first.
  314       SimpleEvent event(Event::EVENT_POPOUT);
  315       expanded_original_->GetViewDecorator()->OnOtherEvent(event);
  316 
  317       child->SwitchViewHost(expanded_popout_);
  318       expanded_popout_->ShowView(false, 0, NULL);
  319     }
  320   }
  321 
  322   void OnPopInHandler(DecoratedViewHost *decorated) {
  323     if (expanded_original_ == decorated && expanded_popout_) {
  324       ViewInterface *child = expanded_popout_->GetView();
  325       ASSERT(child);
  326       if (child) {
  327         // Close details view
  328         child->GetGadget()->CloseDetailsView();
  329 
  330         ViewHostInterface *old_host = child->SwitchViewHost(expanded_original_);
  331         SimpleEvent event(Event::EVENT_POPIN);
  332         expanded_original_->GetViewDecorator()->OnOtherEvent(event);
  333         // The old host must be destroyed after sending onpopin event.
  334         old_host->Destroy();
  335         expanded_original_ = NULL;
  336         expanded_popout_ = NULL;
  337       }
  338     }
  339   }
  340 
  341   void ShowGadgetDebugConsole(Gadget *gadget) {
  342     if (!gadget) return;
  343     GadgetsMap::iterator it = gadgets_.find(gadget->GetInstanceID());
  344     if (it == gadgets_.end()) return;
  345     GadgetInfo *info = &(it->second);
  346     if (info->debug_console) {
  347       DLOG("Gadget has already opened a debug console: %p",
  348            info->debug_console);
  349       return;
  350     }
  351     NewGadgetDebugConsole(gadget, &info->debug_console);
  352   }
  353 
  354   GadgetManagerInterface *gadget_manager_;
  355   GadgetBrowserHost gadget_browser_host_;
  356   QtHost *host_;
  357   int view_debug_mode_;
  358   Gadget::DebugConsoleConfig debug_console_config_;
  359   bool composite_;
  360   bool gadgets_shown_;
  361   QAction *gadgets_menu_separator_;
  362   QMap<QObject*, Gadget*> gadget_menu_map_; // Map from QAction to Gadget.
  363 
  364   DecoratedViewHost *expanded_popout_;
  365   DecoratedViewHost *expanded_original_;
  366 
  367   typedef LightMap<int, GadgetInfo> GadgetsMap;
  368   GadgetsMap gadgets_;
  369 
  370   QMenu menu_;
  371   QSystemTrayIcon tray_;
  372   bool show_;
  373 
  374   Permissions global_permissions_;
  375 
  376   // QObject stuffs
  377  signals:
  378   void show(bool, Gadget*);
  379 
  380  public slots:
  381   void OnAddGadget() {
  382     GetGadgetManager()->ShowGadgetBrowserDialog(&gadget_browser_host_);
  383   }
  384 
  385   void OnAddIGoogleGadget() {
  386     GetGadgetManager()->NewGadgetInstanceFromFile(kIGoogleGadgetName);
  387   }
  388 
  389   void OnGadgetMenuItem() {
  390     QAction *s = static_cast<QAction*>(sender());
  391     if (s && gadget_menu_map_.contains(s)) {
  392       Gadget *gadget = gadget_menu_map_[s];
  393       ASSERT(gadget);
  394       emit show(true, gadget);
  395     }
  396   }
  397 
  398   void OnShowAll() {
  399     emit show(true, NULL);
  400     show_ = true;
  401   }
  402   void OnHideAll() {
  403     emit show(false, NULL);
  404     show_ = false;
  405   }
  406 
  407   void OnTrayActivated(QSystemTrayIcon::ActivationReason reason) {
  408     if (reason == QSystemTrayIcon::DoubleClick) {
  409       if (show_)
  410         OnHideAll();
  411       else
  412         OnShowAll();
  413     }
  414   }
  415   void OnAbout() {
  416     ShowAboutDialog(host_);
  417   }
  418 };
  419 
  420 }
  421 }
  422 
  423 #endif // HOSTS_QT_HOST_INTERNAL_H__