X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=simple-launcher.cc;h=b30f5e8f31fd454c3b008833d552bcb7bb1fbc28;hb=a35af69c462e54d249586a4a74dd9d374f2c9411;hp=5e31a96294247078ff580e95ab30b8b986ecf885;hpb=a6117e286571199af04f6b83d8a4af3d08c6a57b;p=simple-launcher diff --git a/simple-launcher.cc b/simple-launcher.cc index 5e31a96..b30f5e8 100644 --- a/simple-launcher.cc +++ b/simple-launcher.cc @@ -1,6 +1,25 @@ +// This file is a part of Simple Launcher +// +// 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 +// the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include + +#include #include @@ -8,18 +27,15 @@ #include #include "launcher-item.h" +#include "sla-list.h" +#include "launchable-item.h" +#include "settings-dialog.h" -extern "C" { - void *hildon_home_applet_lib_initialize (void *state_data, int *state_size, GtkWidget **widget); - void hildon_home_applet_lib_deinitialize (void *applet_data); - void hildon_home_applet_lib_background (void *applet_data); - void hildon_home_applet_lib_foreground(void *applet_data); - int hildon_home_applet_lib_save_state(void *applet_data, void **state_data, int *state_size); - GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent); -}; - -#define SLA_APPLET_DBUS_NAME "simple-launcher" -#define SLA_APPLET_VERSION "0.0" +#define SL_APPLET_DBUS_NAME "simple-launcher" +#define SL_APPLET_VERSION "0.0" +#define SL_APPLET_ICON_SIZE 26 +#define SL_APPLET_BORDER_SIZE 14 +#define SL_APPLET_CANVAS_SIZE (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE) class SimpleLauncherApplet { public: @@ -36,17 +52,31 @@ public: GtkWidget *getWidget() { return myWidget; } private: + static void addItem(LauncherItems&, const std::string&, bool); + + void loadConfig(); + void saveConfig(); + + static void updateItems(LauncherItems&); + static void processDirectory(LauncherItems&, const std::string&); + bool initWidget(); + void updateWidget(); - bool startApplication(const std::string& application); + void buttonClicked(GtkToolButton *); + void runDialog(); + + static void _button_clicked(GtkToolButton *, void *); + static void _run_dialog(GtkMenuItem *, void *); private: osso_context_t *myContext; GtkWidget *myWidget; + GtkWindow *myParent; - std::vector myItems; + LauncherItems myItems; - static char *ourFiles[]; + static char *ourDirs[]; }; // Hildon home applet interface functions @@ -54,12 +84,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; } } @@ -90,71 +120,194 @@ int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int // SimpleLauncherApplet implementation -char *SimpleLauncherApplet::ourFiles[] = { - 0 +char *SimpleLauncherApplet::ourDirs[] = { + "/usr/share/applications/hildon", + NULL }; -SimpleLauncherApplet::SimpleLauncherApplet(): myContext(0), myWidget(0) { +SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) { } bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) { - if ((myContext = osso_initialize(SLA_APPLET_DBUS_NAME, SLA_APPLET_VERSION, FALSE, NULL)) == 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; } + loadConfig(); + if (!initWidget()) { return false; } - for (int i = 0 ; ourFiles[i] != 0 ; ++i) { - LauncherItem *item = new LauncherItem(); + return true; +} + +SimpleLauncherApplet::~SimpleLauncherApplet() { + myItems.clear(); + + if (myWidget != NULL) { + gtk_widget_destroy(myWidget); + myWidget = NULL; + } + + if (myContext != NULL) { + osso_deinitialize(myContext); + myContext = NULL; + } +} + +void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) { + if (!items.exists(name)) { + LaunchableItem *item = new LaunchableItem(); + + item->load(name); - if (item->load(ourFiles[i])) { - myItems.push_back(item); + if (enabled) { + item->enable(); } else { - delete item; + item->disable(); } + + items.add(name, item); } +} - // g_signal_connect (applet->myWidget, "do_search", G_CALLBACK (mis_applet_do_search), (gpointer)applet); +// FIXME: this probably should be done somehow differently +static char *configFileName="/home/user/.slarc"; - gtk_widget_show_all(myWidget); +void SimpleLauncherApplet::loadConfig() { + std::ifstream config(configFileName); - return true; -} + if (config) { + char *buffer = new char [1024]; -SimpleLauncherApplet::~SimpleLauncherApplet() { - for (std::vector::iterator it = myItems.begin(); it != myItems.end(); ++it) { - if (*it != 0) { - delete *it; - *it = 0; + while (config.getline(buffer, 1024)) { + char *p = strchr(buffer, ','); + + if (p != NULL) { + *p++ = '\0'; + } + + addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y'))); } + + delete buffer; } +} - myItems.resize(0); +void SimpleLauncherApplet::saveConfig() { + // TODO: make saving config an atomic operation + std::ofstream config(configFileName); - if (myWidget != 0) { - gtk_widget_destroy(myWidget); - myWidget = 0; + if (config) { + for (size_t i = 0 ; i < myItems.size() ; ++i) { + config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl; + } } +} - if (myContext != 0) { - osso_deinitialize(myContext); - myContext = 0; +void SimpleLauncherApplet::updateItems(LauncherItems& items) { + for (int i = 0 ; ourDirs[i] != NULL ; ++i) { + processDirectory(items, ourDirs[i]); + } +} + +void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) { + DIR *dir = opendir(dirname.c_str()); + + if (dir != NULL) { + const std::string namePrefix = dirname + "/"; + std::string shortName; + std::string desktopExtension = ".desktop"; + const dirent *file; + + while ((file = readdir(dir)) != 0) { + shortName = file->d_name; + if ((shortName == ".") || (shortName == "..")) { + continue; + } + + if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) { + addItem(items, namePrefix+shortName, false); + } + } + + closedir(dir); } } bool SimpleLauncherApplet::initWidget() { - return false; + 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; + GtkBox *box = GTK_BOX(gtk_hbox_new(true, 1)); + + for (size_t i = 0 ; i < myItems.size() ; ++i) { + LauncherItem *item = myItems[i]; + + if (item != NULL && item->isEnabled()) { + GtkWidget *button = gtk_button_new(); + + gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE))); + + gtk_object_set_user_data(GTK_OBJECT(button), item); + g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this); + + gtk_box_pack_start(box, GTK_WIDGET(button), false, false, 0); + + ++button_no; + } + } + + gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(box)); + + if (button_no == 0) { + gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE); + } else { + 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_widget_show_all(myWidget); +} + +void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { + ((SimpleLauncherApplet *)self)->buttonClicked(button); +} + +void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) { + if (button != NULL) { + LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button)); + + 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; } @@ -162,13 +315,41 @@ int SimpleLauncherApplet::saveState(void **state_data, int *state_size) { } GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) { - // TODO: in case we want SimpleLauncherApplet to be configurable, this method - // should return a gtk_menu_item that would be included in home settings - // menu. Method should make sure that when we activate that item, a - // corresponding dialog appears. - return 0; + myParent = parent; // FIXME: Ugly piece of code :( + + GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings..."); + + g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this); + + return menuItem; +} + +void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) { + ((SimpleLauncherApplet *)self)->runDialog(); } -bool SimpleLauncherApplet::startApplication(const std::string& application) { - return osso_application_top(myContext, application.c_str(), 0) == OSSO_OK; +void SimpleLauncherApplet::runDialog() { + // We update the items before using them to avoid a small memory leak + // FIXME: deal with the situation in a better way (figure it out first :)) + updateItems(myItems); // User requested 'settings', let's give her the latest stuff :) + + LauncherItems newItems = myItems; + + SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems); + + switch (dialog.run()) { + case GTK_RESPONSE_OK: + myItems = newItems; + saveConfig(); // save it immediately! + updateWidget(); + break; + + case GTK_RESPONSE_CANCEL: + break; + + default: + ; // FIXME: do I want to do anything in here? + } } + +// vim:ts=2:sw=2:et