setting padding to 0 (instead of 2)
[simple-launcher] / simple-launcher.cc
index 672897d..3ebd1da 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
 #include <vector>
 #include <fstream>
 
+#include <dirent.h>
+
 #include <gtk/gtk.h>
 
-#include <hildon-home-plugin/hildon-home-plugin-interface.h>
 #include <libosso.h>
 
 #include "launcher-item.h"
-#include "sla-list.h"
 #include "launchable-item.h"
+#include "settings-dialog.h"
+#include "gconf-wrapper.h"
 
 #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)
+
+#define SL_APPLET_GCONF_PATH  "/apps/simple-launcher"
+
+// A copy of interface functions from hildon-home-plugin-interface (new hildon desktop does not have it) {{{
+
+extern "C" {
+
+  void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget);
+  int hildon_home_applet_lib_save_state(void *applet_data, void **state_data, int *state_size);
+  void hildon_home_applet_lib_background(void *applet_data);
+  void hildon_home_applet_lib_foreground(void *applet_data);
+  void hildon_home_applet_lib_deinitialize(void *applet_data);
+  GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent);
+
+};
+
+// }}}
 
 class SimpleLauncherApplet {
 public:
-  SimpleLauncherApplet();
+  SimpleLauncherApplet(const GConfKey&);
  ~SimpleLauncherApplet();
 
   bool doInit(void *state_data, int *state_size);
@@ -49,31 +65,47 @@ 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 buttonPressed(GtkWidget *button, GdkEventButton *event);
   void runDialog();
 
-  static void _button_clicked(GtkToolButton *, void *);
+  static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
   static void _run_dialog(GtkMenuItem *, void *);
 
 private:
+  // GConfClientWrapper myClient;
+  // GConfKey myMainSettings;
+
   osso_context_t *myContext;
+
   GtkWidget *myWidget;
   GtkWindow *myParent;
 
   LauncherItems myItems;
 
+  GConfBooleanOption myTransparent;
+  // bool myShowInfobanner; // FIXME: to implement
+  GConfIntegerOption myIconSize;
+
   static char *ourDirs[];
 };
 
 // Hildon home applet interface functions
 
 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
-  SimpleLauncherApplet *applet = new SimpleLauncherApplet();
+  GConfKey baseKey(SL_APPLET_GCONF_PATH);
+
+  SimpleLauncherApplet *applet = new SimpleLauncherApplet(baseKey);
 
   if (applet != NULL) {
     if (applet->doInit(state_data, state_size)) {
@@ -116,7 +148,8 @@ char *SimpleLauncherApplet::ourDirs[] = {
   NULL
 };
 
-SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
+// SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
+SimpleLauncherApplet::SimpleLauncherApplet(const GConfKey& base) : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(base, "transparent", false), myIconSize(base, "icon_size", 48) {
 }
 
 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
@@ -131,93 +164,177 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
     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 0
+  // This does not seem to be necessary
   if (myWidget != NULL) {
     gtk_widget_destroy(myWidget);
     myWidget = NULL;
   }
-
+#endif
   if (myContext != NULL) {
     osso_deinitialize(myContext);
     myContext = NULL;
   }
 }
 
-static char *configFileName="/home/user/.slarc";
-
-void SimpleLauncherApplet::loadConfig() {
-#if 0
-  for (int i = 0 ; ourFiles[i] != NULL ; ++i) {
+void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
+  if (!items.exists(name)) {
     LaunchableItem *item = new LaunchableItem();
 
-    if (item->load(ourFiles[i])) {
-      myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
+    item->load(name);
+
+    if (enabled) {
+      item->enable();
     } else {
-      delete item;
+      item->disable();
     }
+
+    items.add(name, item);
+  }
+}
+
+// {{{ Configuration file managment
+static const gchar *getConfigFileName() {
+  static gchar *configFileName = NULL;
+
+  if (configFileName == NULL) {
+    configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
+  }
+
+  return configFileName;
+}
+
+void SimpleLauncherApplet::loadConfig() {
+  std::ifstream config(getConfigFileName());
+
+  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;
   }
-#endif
 }
 
 void SimpleLauncherApplet::saveConfig() {
   // TODO: make saving config an atomic operation
-  std::ofstream config(configFileName);
+  std::ofstream config(getConfigFileName());
 
   if (config) {
-    for (LauncherItems::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
-      config << it->first << ',' << it->second->isEnabled() << std::endl;
+    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_hbox_new(false, 0);
+
+  if (myWidget != NULL) {
+    updateWidget();
+  }
+
+  return myWidget != NULL;
+}
+
+void SimpleLauncherApplet::updateWidget() {
+  gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
+
+  GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
+
   int button_no = 0;
 
-  GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
+  for (size_t i = 0 ; i < myItems.size() ; ++i) {
+    LauncherItem *item = myItems[i];
+
+    if (item != NULL && item->isEnabled()) {
+      GtkWidget *button = gtk_event_box_new();
+
+      gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
+      g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
+
+      gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
+
+      {
+        GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
+        gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
+        g_object_unref(G_OBJECT(pixbuf));
+      }
 
-  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);
+      gtk_object_set_user_data(GTK_OBJECT(button), item);
 
-    gtk_object_set_user_data(GTK_OBJECT(button), it->second);
-    g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
+      gtk_size_group_add_widget(group, button);
 
-    gtk_toolbar_insert(toolbar, button, -1);
+      gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
 
-    ++button_no;
+      ++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));
+  g_object_unref(G_OBJECT(group));
+
+  int totalSize = myIconSize.value();
+
+  if (button_no == 0) {
+    gtk_widget_set_size_request(myWidget, totalSize, totalSize);
   } else {
-    gtk_widget_destroy(GTK_WIDGET(toolbar));
+    gtk_widget_set_size_request(myWidget, button_no*totalSize, totalSize);
   }
 
-  return myWidget != NULL;
+  gtk_widget_show_all(myWidget);
 }
 
-void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
-  ((SimpleLauncherApplet *)self)->buttonClicked(button);
+void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
+  ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
 }
 
-void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
-  if (button != NULL) {
+void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
+  if (button != NULL && event->button == 1) {
     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
 
     if (item != NULL) {
@@ -239,10 +356,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...");
@@ -257,20 +370,22 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
 }
 
 void SimpleLauncherApplet::runDialog() {
-  SLAList list(SL_APPLET_ICON_SIZE, myItems);
+  // 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 :)
 
-  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));
+  LauncherItems newItems = myItems;
 
-  gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
+  // TODO: make it nicer... this code is ugly :(
+  SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
 
-  gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
-
-  int response = gtk_dialog_run(dialog);
-
-  gtk_widget_destroy(GTK_WIDGET(dialog));
-
-  switch (response) {
+  switch (dialog.run()) {
     case GTK_RESPONSE_OK:
+      myItems = newItems;
+      dialog.updateValues();  // FIXME: hackish :( make it better
+
+      saveConfig();   // save it immediately!
+      updateWidget();
       break;
 
     case GTK_RESPONSE_CANCEL:
@@ -279,6 +394,8 @@ void SimpleLauncherApplet::runDialog() {
     default:
       ;     // FIXME: do I want to do anything in here?
   }
+
+  // newItems.clear(); // TODO: do I really need it?
 }
 
 // vim:ts=2:sw=2:et