updated text for stubs in the dialog
[simple-launcher] / simple-launcher.cc
index 41895a6..b30f5e8 100644 (file)
@@ -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,9 @@
 
 #include <string>
 #include <vector>
+#include <fstream>
+
+#include <dirent.h>
 
 #include <gtk/gtk.h>
 
@@ -26,6 +29,7 @@
 #include "launcher-item.h"
 #include "sla-list.h"
 #include "launchable-item.h"
+#include "settings-dialog.h"
 
 #define SL_APPLET_DBUS_NAME  "simple-launcher"
 #define SL_APPLET_VERSION    "0.0"
@@ -48,7 +52,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();
@@ -63,7 +76,7 @@ private:
 
   LauncherItems myItems;
 
-  static char *ourFiles[];
+  static char *ourDirs[];
 };
 
 // Hildon home applet interface functions
@@ -107,13 +120,8 @@ 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",
+char *SimpleLauncherApplet::ourDirs[] = {
+  "/usr/share/applications/hildon",
   NULL
 };
 
@@ -126,34 +134,17 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
     return false;
   }
 
-  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;
-    }
-  }
+  loadConfig();
 
   if (!initWidget()) {
     return false;
   }
 
-  gtk_widget_show_all(myWidget);
-
   return true;
 }
 
 SimpleLauncherApplet::~SimpleLauncherApplet() {
-  for (LauncherItems::iterator it = myItems.begin(); it != myItems.end(); ++it) {
-    if (it->second != NULL) {
-      delete it->second;
-      it->second = NULL;
-    }
-  }
-
-  myItems.resize(0);
+  myItems.clear();
 
   if (myWidget != NULL) {
     gtk_widget_destroy(myWidget);
@@ -166,32 +157,135 @@ 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() {
+  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;
+  }
+}
+
+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() {
+  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));
 
-  GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
+  for (size_t i = 0 ; i < myItems.size() ; ++i) {
+    LauncherItem *item = myItems[i];
 
-  for (LauncherItems::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)), NULL);
+    if (item != NULL && item->isEnabled()) {
+      GtkWidget *button = gtk_button_new();
 
-    gtk_object_set_user_data(GTK_OBJECT(button), it->second);
-    g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
+      gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)));
 
-    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_box_pack_start(box, GTK_WIDGET(button), false, false, 0);
+
+      ++button_no;
+    }
   }
 
-  if (button_no) {
-    myWidget = gtk_frame_new(NULL);
-    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_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_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 != NULL;
+  gtk_widget_show_all(myWidget);
 }
 
 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
@@ -221,10 +315,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...");
@@ -239,20 +329,19 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
 }
 
 void SimpleLauncherApplet::runDialog() {
-  SLAList list(SL_APPLET_ICON_SIZE, myItems);
-
-  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);
+  // 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 :)
 
-  int response = gtk_dialog_run(dialog);
+  LauncherItems newItems = myItems;
 
-  gtk_widget_destroy(GTK_WIDGET(dialog));
+  SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems);
 
-  switch (response) {
+  switch (dialog.run()) {
     case GTK_RESPONSE_OK:
+      myItems = newItems;
+      saveConfig();   // save it immediately!
+      updateWidget();
       break;
 
     case GTK_RESPONSE_CANCEL: