added new todo: starting CLI apps
[simple-launcher] / simple-launcher.cc
index 52ace44..47c61cd 100644 (file)
@@ -19,6 +19,8 @@
 #include <vector>
 #include <fstream>
 
+#include <dirent.h>
+
 #include <gtk/gtk.h>
 
 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
@@ -49,9 +51,14 @@ 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();
 
@@ -132,8 +139,6 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
     return false;
   }
 
-  gtk_widget_show_all(myWidget);
-
   return true;
 }
 
@@ -151,6 +156,23 @@ SimpleLauncherApplet::~SimpleLauncherApplet() {
   }
 }
 
+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);
+  }
+}
+
+// FIXME: this probably should be done somehow differently
 static char *configFileName="/home/user/.slarc";
 
 void SimpleLauncherApplet::loadConfig() {
@@ -166,33 +188,11 @@ void SimpleLauncherApplet::loadConfig() {
         *p++ = '\0';
       }
 
-      LaunchableItem *item = new LaunchableItem();
-
-      item->load(buffer);
-
-      if (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')) {
-        item->enable();
-      } else {
-        item->disable();
-      }
-
-      myItems.add(buffer, item);
+      addItem(myItems, 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 (item->load(ourFiles[i])) {
-      myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
-    } else {
-      delete item;
-    }
-  }
-#endif
 }
 
 void SimpleLauncherApplet::saveConfig() {
@@ -206,6 +206,36 @@ void SimpleLauncherApplet::saveConfig() {
   }
 }
 
+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() {
   myWidget = gtk_frame_new(NULL);
 
@@ -244,12 +274,15 @@ 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);
+  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);
   }
+
+  gtk_widget_show_all(myWidget);
 }
 
 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
@@ -279,10 +312,6 @@ 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...");
@@ -297,7 +326,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));
 
@@ -311,6 +344,9 @@ void SimpleLauncherApplet::runDialog() {
 
   switch (response) {
     case GTK_RESPONSE_OK:
+      myItems = newItems;
+      saveConfig();   // save it immediately!
+      updateWidget();
       break;
 
     case GTK_RESPONSE_CANCEL: