X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=simple-launcher.cc;h=8c2e319e8991138925b02ef1bc2b4ff57b7d87d3;hb=0f3cf1676744186a87899064c19d4cb21d74b892;hp=ceddd7c5f46322d308d3553cc2ff810efee75787;hpb=0b8bbdf0f11007a567c2b5d3837df2dad20ed286;p=simple-launcher diff --git a/simple-launcher.cc b/simple-launcher.cc index ceddd7c..8c2e319 100644 --- a/simple-launcher.cc +++ b/simple-launcher.cc @@ -19,6 +19,8 @@ #include #include +#include + #include #include @@ -49,12 +51,13 @@ public: GtkWidget *getWidget() { return myWidget; } private: - void addItem(const std::string&, bool); + static void addItem(LauncherItems&, const std::string&, bool); void loadConfig(); void saveConfig(); - void processDirectory(const std::string&); + static void updateItems(LauncherItems&); + static void processDirectory(LauncherItems&, const std::string&); bool initWidget(); void updateWidget(); @@ -132,16 +135,10 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) { loadConfig(); - for (int i = 0 ; ourDirs[i] != NULL ; ++i) { - processDirectory(ourDirs[i]); - } - if (!initWidget()) { return false; } - gtk_widget_show_all(myWidget); - return true; } @@ -159,8 +156,8 @@ SimpleLauncherApplet::~SimpleLauncherApplet() { } } -void SimpleLauncherApplet::addItem(const std::string& name, bool enabled) { - if (!myItems.exists(name)) { +void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) { + if (!items.exists(name)) { LaunchableItem *item = new LaunchableItem(); item->load(name); @@ -171,7 +168,7 @@ void SimpleLauncherApplet::addItem(const std::string& name, bool enabled) { item->disable(); } - myItems.add(name, item); + items.add(name, item); } } @@ -190,7 +187,7 @@ void SimpleLauncherApplet::loadConfig() { *p++ = '\0'; } - addItem(buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y'))); + addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y'))); } @@ -209,7 +206,34 @@ void SimpleLauncherApplet::saveConfig() { } } -void SimpleLauncherApplet::processDirectory(const std::string& dirname) { +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() { @@ -252,10 +276,16 @@ void SimpleLauncherApplet::updateWidget() { if (button_no) { 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); + 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); + } } else { gtk_widget_destroy(GTK_WIDGET(toolbar)); } + + gtk_widget_show_all(myWidget); } void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) { @@ -303,7 +333,11 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) { } void SimpleLauncherApplet::runDialog() { - SLAList list(SL_APPLET_ICON_SIZE, myItems); + 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)); @@ -317,6 +351,9 @@ void SimpleLauncherApplet::runDialog() { switch (response) { case GTK_RESPONSE_OK: + myItems = newItems; + saveConfig(); // save it immediately! + updateWidget(); break; case GTK_RESPONSE_CANCEL: