X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=simple-launcher.cc;h=47c61cd008404a70f4bc3b2e977516b521307af3;hb=cf73ee0b4ffea2066c1bdf781ac3d871da59932f;hp=c2a08ded86d70d9725c00c8107f7aec74cb4310f;hpb=06e56b1c6f1a45dd900df22486f8255d74002f79;p=simple-launcher diff --git a/simple-launcher.cc b/simple-launcher.cc index c2a08de..47c61cd 100644 --- a/simple-launcher.cc +++ b/simple-launcher.cc @@ -1,10 +1,10 @@ // 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 as published by the Free -// Software Foundation; either version 2 of the License. +// 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 @@ -17,6 +17,9 @@ #include #include +#include + +#include #include @@ -24,12 +27,14 @@ #include #include "launcher-item.h" +#include "sla-list.h" +#include "launchable-item.h" -#define SLA_APPLET_DBUS_NAME "simple-launcher" -#define SLA_APPLET_VERSION "0.0" -#define SLA_APPLET_ICON_SIZE 26 -#define SLA_APPLET_BORDER_SIZE 14 -#define SLA_APPLET_CANVAS_SIZE (SLA_APPLET_BORDER_SIZE+SLA_APPLET_BORDER_SIZE) +#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: @@ -46,7 +51,16 @@ 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(); void buttonClicked(GtkToolButton *); void runDialog(); @@ -59,9 +73,9 @@ private: GtkWidget *myWidget; GtkWindow *myParent; - std::vector myItems; + LauncherItems myItems; - static char *ourFiles[]; + static char *ourDirs[]; }; // Hildon home applet interface functions @@ -69,12 +83,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; } } @@ -105,91 +119,170 @@ 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(SLA_APPLET_DBUS_NAME, SLA_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(item); - } else { - delete item; - } - } + loadConfig(); if (!initWidget()) { return false; } - gtk_widget_show_all(myWidget); - return true; } SimpleLauncherApplet::~SimpleLauncherApplet() { - for (std::vector::iterator it = myItems.begin(); it != myItems.end(); ++it) { - if (*it != 0) { - delete *it; - *it = 0; + 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 (enabled) { + item->enable(); + } else { + item->disable(); } + + items.add(name, item); } +} - myItems.resize(0); +// FIXME: this probably should be done somehow differently +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(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y'))); + } + + delete buffer; } +} - if (myContext != 0) { - osso_deinitialize(myContext); - myContext = 0; +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; + } + } +} + +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() { - 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 (std::vector::const_iterator it = myItems.begin(); it != myItems.end(); ++it) { - GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf((*it)->getIcon(SLA_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); - 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*(SLA_APPLET_ICON_SIZE+SLA_APPLET_CANVAS_SIZE), SLA_APPLET_ICON_SIZE+SLA_APPLET_CANVAS_SIZE); - gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar)); + gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar)); + + 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_destroy(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); } - return myWidget != 0; + gtk_widget_show_all(myWidget); } void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { @@ -197,21 +290,21 @@ void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { } void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) { - if (button != 0) { - LauncherItem *item = (LauncherItem *)gtk_object_get_user_data(GTK_OBJECT(button)); + 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; } @@ -219,13 +312,9 @@ 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. myParent = parent; // FIXME: Ugly piece of code :( - GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher Settings..."); + GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings..."); g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this); @@ -237,7 +326,17 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) { } void SimpleLauncherApplet::runDialog() { - 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)); + LauncherItems newItems = myItems; + + updateItems(newItems); // User requested 'settings', let's give her the latest stuff :) + + SLAList list(SL_APPLET_ICON_SIZE, newItems); + + 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()); + + gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257); int response = gtk_dialog_run(dialog); @@ -245,6 +344,9 @@ void SimpleLauncherApplet::runDialog() { switch (response) { case GTK_RESPONSE_OK: + myItems = newItems; + saveConfig(); // save it immediately! + updateWidget(); break; case GTK_RESPONSE_CANCEL: