X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=simple-launcher.cc;h=cd05b6d5d07cc3a7a2dbb1ccabf364ebc288d2d0;hb=2f7c7455ee37f7ccd919dbba6f2ef7f0564eb1f0;hp=b533a6d4548b7e000bc7a094d113d2327e22d7b1;hpb=32b8a7a4026cce021263f2e0af953455ec3a79f6;p=simple-launcher diff --git a/simple-launcher.cc b/simple-launcher.cc index b533a6d..cd05b6d 100644 --- a/simple-launcher.cc +++ b/simple-launcher.cc @@ -1,6 +1,6 @@ // This file is a part of Simple Launcher // -// Copyright (C) 2006, Mikhail Sobolev +// Copyright (C) 2006, 2007, Mikhail Sobolev // // Simple Launcher is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License version 2 as published by @@ -17,6 +17,7 @@ #include #include +#include #include @@ -25,6 +26,7 @@ #include "launcher-item.h" #include "sla-list.h" +#include "launchable-item.h" #define SL_APPLET_DBUS_NAME "simple-launcher" #define SL_APPLET_VERSION "0.0" @@ -32,29 +34,6 @@ #define SL_APPLET_BORDER_SIZE 14 #define SL_APPLET_CANVAS_SIZE (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE) -class LaunchableItem { -public: - LaunchableItem(LauncherItem *, bool); - ~LaunchableItem(); - - GdkPixbuf *getIcon(int icon_size) const { return myItem->getIcon(icon_size); } - - const std::string& getName() const { return myItem->getName(); } - const std::string& getComment() const { return myItem->getComment(); } - const std::string& getService() const { return myItem->getService(); } - - bool isEnabled(void) const { return myEnabled; } - - void enable() { myEnabled = true; } - void disable() { myEnabled = false; } - - bool activate(osso_context_t *); - -private: - LauncherItem *myItem; - bool myEnabled; -}; - class SimpleLauncherApplet { public: SimpleLauncherApplet(); @@ -70,7 +49,13 @@ public: GtkWidget *getWidget() { return myWidget; } private: + void addItem(const std::string&, bool); + + void loadConfig(); + void saveConfig(); + bool initWidget(); + void updateWidget(); void buttonClicked(GtkToolButton *); void runDialog(); @@ -83,10 +68,9 @@ private: GtkWidget *myWidget; GtkWindow *myParent; - typedef std::vector > ItemList; - ItemList myItems; + LauncherItems myItems; - static char *ourFiles[]; + static char *ourDirs[]; }; // Hildon home applet interface functions @@ -94,12 +78,12 @@ private: void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) { SimpleLauncherApplet *applet = new SimpleLauncherApplet(); - if (applet != 0) { + if (applet != NULL) { if (applet->doInit(state_data, state_size)) { *widget = applet->getWidget(); } else { delete applet; - applet = 0; + applet = NULL; } } @@ -130,34 +114,21 @@ int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int // SimpleLauncherApplet implementation -char *SimpleLauncherApplet::ourFiles[] = { - "/usr/share/applications/hildon/FBReader.desktop", - "/usr/share/applications/hildon/mp_ui.desktop", - "/usr/share/applications/hildon/osso-xterm.desktop", - "/usr/share/applications/hildon/filemanager.desktop", - "/usr/share/applications/hildon/osso-application-installer.desktop", - "/usr/share/applications/hildon/hildon-control-panel.desktop", - 0 +char *SimpleLauncherApplet::ourDirs[] = { + "/usr/share/applications/hildon", + NULL }; -SimpleLauncherApplet::SimpleLauncherApplet(): myContext(0), myWidget(0), myParent(0) { +SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) { } bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) { - if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, 0)) == 0) { + if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) { g_debug("sla-applet: failed to initialize the osso layer"); return false; } - for (int i = 0 ; ourFiles[i] != 0 ; ++i) { - LauncherItem *item = new LauncherItem(); - - if (item->load(ourFiles[i])) { - myItems.push_back(std::pair(ourFiles[i], new LaunchableItem(item, true))); - } else { - delete item; - } - } + loadConfig(); if (!initWidget()) { return false; @@ -169,52 +140,124 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) { } SimpleLauncherApplet::~SimpleLauncherApplet() { - for (ItemList::iterator it = myItems.begin(); it != myItems.end(); ++it) { - if (it->second != 0) { - delete it->second; - it->second = 0; + myItems.clear(); + + if (myWidget != NULL) { + gtk_widget_destroy(myWidget); + myWidget = NULL; + } + + if (myContext != NULL) { + osso_deinitialize(myContext); + myContext = NULL; + } +} + +void SimpleLauncherApplet::addItem(const std::string& name, bool enabled) { + if (!myItems.exists(name)) { + LaunchableItem *item = new LaunchableItem(); + + item->load(name); + + if (enabled) { + item->enable(); + } else { + item->disable(); } + + myItems.add(name, item); } +} - myItems.resize(0); +static char *configFileName="/home/user/.slarc"; - if (myWidget != 0) { - gtk_widget_destroy(myWidget); - myWidget = 0; +void SimpleLauncherApplet::loadConfig() { + std::ifstream config(configFileName); + + if (config) { + char *buffer = new char [1024]; + + while (config.getline(buffer, 1024)) { + char *p = strchr(buffer, ','); + + if (p != NULL) { + *p++ = '\0'; + } + + addItem(buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y'))); + + } + + delete buffer; } +#if 0 + for (int i = 0 ; ourFiles[i] != NULL ; ++i) { + LaunchableItem *item = new LaunchableItem(); - if (myContext != 0) { - osso_deinitialize(myContext); - myContext = 0; + if (item->load(ourFiles[i])) { + myItems.push_back(std::pair(ourFiles[i], item)); + } else { + delete item; + } + } +#endif +} + +void SimpleLauncherApplet::saveConfig() { + // TODO: make saving config an atomic operation + std::ofstream config(configFileName); + + if (config) { + for (size_t i = 0 ; i < myItems.size() ; ++i) { + config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl; + } } } bool SimpleLauncherApplet::initWidget() { - int button_no = 0; + myWidget = gtk_frame_new(NULL); + + if (myWidget != NULL) { + gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN); + + updateWidget(); + } + + return myWidget != NULL; +} + +void SimpleLauncherApplet::updateWidget() { + GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget)); + if (child != NULL) { + gtk_container_remove(GTK_CONTAINER(myWidget), child); + gtk_widget_destroy(child); + } + + int button_no = 0; GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new()); - for (ItemList::const_iterator it = myItems.begin(); it != myItems.end(); ++it) { - GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(it->second->getIcon(SL_APPLET_ICON_SIZE)), 0); + for (size_t i = 0 ; i < myItems.size() ; ++i) { + LauncherItem *item = myItems[i]; - gtk_object_set_user_data(GTK_OBJECT(button), it->second); - g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this); + if (item != NULL && item->isEnabled()) { + GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL); - gtk_toolbar_insert(toolbar, button, -1); + gtk_object_set_user_data(GTK_OBJECT(button), item); + g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this); - ++button_no; + gtk_toolbar_insert(toolbar, button, -1); + + ++button_no; + } } if (button_no) { - myWidget = gtk_frame_new(0); - gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN); - gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE); gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar)); + gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE); } else { gtk_widget_destroy(GTK_WIDGET(toolbar)); } - - return myWidget != 0; } void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { @@ -222,21 +265,21 @@ void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { } void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) { - if (button != 0) { + if (button != NULL) { LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button)); - if (item != 0) { + if (item != NULL) { item->activate(myContext); } } } int SimpleLauncherApplet::saveState(void **state_data, int *state_size) { - if (state_data != 0) { - *state_data = 0; + if (state_data != NULL) { + *state_data = NULL; } - if (state_size != 0) { + if (state_size != NULL) { *state_size = 0; } @@ -262,13 +305,9 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) { } void SimpleLauncherApplet::runDialog() { - SLAList list(SL_APPLET_ICON_SIZE); + SLAList list(SL_APPLET_ICON_SIZE, myItems); - for (ItemList::const_iterator item = myItems.begin(); item != myItems.end(); ++item) { - list.addItem(item->first.c_str(), item->second->getIcon(SL_APPLET_ICON_SIZE), item->second->getComment().c_str(), item->second->isEnabled()); - } - - GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new_with_buttons("Launcher Settings", myParent, (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), "OK", GTK_RESPONSE_OK, "Cancel", GTK_RESPONSE_CANCEL, 0)); + GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new_with_buttons("Launcher Settings", myParent, (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), "OK", GTK_RESPONSE_OK, "Cancel", GTK_RESPONSE_CANCEL, NULL)); gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget()); @@ -290,17 +329,4 @@ void SimpleLauncherApplet::runDialog() { } } -LaunchableItem::LaunchableItem(LauncherItem *item, bool enabled): myItem(item), myEnabled(enabled) { -} - -LaunchableItem::~LaunchableItem() { - if (myItem != 0) { - delete myItem; - } -} - -bool LaunchableItem::activate(osso_context_t *context) { - return osso_application_top(context, myItem->getService().c_str(), 0) == OSSO_OK; -} - // vim:ts=2:sw=2:et