Refactoring:
authormishas <mikhail.sobolev@gmail.com>
Thu, 21 Dec 2006 22:59:58 +0000 (22:59 +0000)
committermishas <mikhail.sobolev@gmail.com>
Thu, 21 Dec 2006 22:59:58 +0000 (22:59 +0000)
 * introduced new class -- LaunchableItem -- a wrapper over LauncherItem with new properties :)
    * enable/disable
    * activatable
 * introduced new type -- LaunchableItems -- a prototype for list of available launchable items
 * re-wrote SLAList to work with LaunchableItems instead of some unknown internal structure

git-svn-id: file:///svnroot/simple-launcher/trunk@58 3ba93dab-e023-0410-b42a-de7732cf370a

Makefile
launchable-item.cc [new file with mode: 0644]
launchable-item.h [new file with mode: 0644]
simple-launcher.cc
sla-list.cc
sla-list.h

index 9678d96..fb27a98 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ all: $(TARGET)
 
 tests: test test1
 
-$(TARGET): simple-launcher.o launcher-item.o sla-list.o
+$(TARGET): simple-launcher.o launchable-item.o launcher-item.o sla-list.o
        $(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
 
 test: test.o launcher-item.o
diff --git a/launchable-item.cc b/launchable-item.cc
new file mode 100644 (file)
index 0000000..71aab95
--- /dev/null
@@ -0,0 +1,33 @@
+// This file is a part of Simple Launcher
+//
+// Copyright (C) 2006, 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
+// 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
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+#include "launchable-item.h"
+
+LaunchableItem::LaunchableItem(LauncherItem *item, bool enabled): myItem(item), myEnabled(enabled) {
+}
+
+LaunchableItem::~LaunchableItem() {
+  if (myItem != 0) {
+    delete myItem;
+  }
+}
+
+bool LaunchableItem::activate(osso_context_t *context) {
+  return osso_application_top(context, myItem->getService().c_str(), 0) == OSSO_OK;
+}
+
+// vim:ts=2:sw=2:et
diff --git a/launchable-item.h b/launchable-item.h
new file mode 100644 (file)
index 0000000..13afb72
--- /dev/null
@@ -0,0 +1,57 @@
+// This file is a part of Simple Launcher
+//
+// Copyright (C) 2006, 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
+// 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
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+#ifndef __LAUNCHABLE_ITEM_H__
+#define __LAUNCHABLE_ITEM_H__
+
+#include <vector>
+#include <string>
+
+#include <libosso.h>
+
+#include "launcher-item.h"
+
+// TODO: or better use inheritance?
+class LaunchableItem {
+public:
+  LaunchableItem(LauncherItem *, bool);
+ ~LaunchableItem();
+
+  GdkPixbuf *getIcon(int icon_size) const { return myItem->getIcon(icon_size); }
+
+  const std::string& getName() const { return myItem->getName(); }
+  const std::string& getComment() const { return myItem->getComment(); }
+  const std::string& getService() const { return myItem->getService(); }
+
+  bool isEnabled(void) const { return myEnabled; }
+
+  void enable() { myEnabled = true; }
+  void disable() { myEnabled = false; }
+  void toggle() { myEnabled = !myEnabled; }
+
+  bool activate(osso_context_t *);
+
+private:
+  LauncherItem *myItem;
+  bool myEnabled;
+};
+
+typedef std::vector<std::pair<std::string, LaunchableItem *> > LaunchableItems;
+
+#endif
+
+// vim:ts=2:sw=2:et
index b533a6d..7b72077 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "launcher-item.h"
 #include "sla-list.h"
+#include "launchable-item.h"
 
 #define SL_APPLET_DBUS_NAME  "simple-launcher"
 #define SL_APPLET_VERSION    "0.0"
 #define SL_APPLET_BORDER_SIZE  14
 #define SL_APPLET_CANVAS_SIZE  (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
 
-class LaunchableItem {
-public:
-  LaunchableItem(LauncherItem *, bool);
- ~LaunchableItem();
-
-  GdkPixbuf *getIcon(int icon_size) const { return myItem->getIcon(icon_size); }
-
-  const std::string& getName() const { return myItem->getName(); }
-  const std::string& getComment() const { return myItem->getComment(); }
-  const std::string& getService() const { return myItem->getService(); }
-
-  bool isEnabled(void) const { return myEnabled; }
-
-  void enable() { myEnabled = true; }
-  void disable() { myEnabled = false; }
-
-  bool activate(osso_context_t *);
-
-private:
-  LauncherItem *myItem;
-  bool myEnabled;
-};
-
 class SimpleLauncherApplet {
 public:
   SimpleLauncherApplet();
@@ -83,8 +61,7 @@ private:
   GtkWidget *myWidget;
   GtkWindow *myParent;
 
-  typedef std::vector<std::pair<std::string, LaunchableItem *> > ItemList;
-  ItemList myItems;
+  LaunchableItems myItems;
 
   static char *ourFiles[];
 };
@@ -169,7 +146,7 @@ bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
 }
 
 SimpleLauncherApplet::~SimpleLauncherApplet() {
-  for (ItemList::iterator it = myItems.begin(); it != myItems.end(); ++it) {
+  for (LaunchableItems::iterator it = myItems.begin(); it != myItems.end(); ++it) {
     if (it->second != 0) {
       delete it->second;
       it->second = 0;
@@ -194,7 +171,7 @@ bool SimpleLauncherApplet::initWidget() {
 
   GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
 
-  for (ItemList::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
+  for (LaunchableItems::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)), 0);
 
     gtk_object_set_user_data(GTK_OBJECT(button), it->second);
@@ -262,11 +239,7 @@ void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
 }
 
 void SimpleLauncherApplet::runDialog() {
-  SLAList list(SL_APPLET_ICON_SIZE);
-
-  for (ItemList::const_iterator item = myItems.begin(); item != myItems.end(); ++item) {
-    list.addItem(item->first.c_str(), item->second->getIcon(SL_APPLET_ICON_SIZE), item->second->getComment().c_str(), item->second->isEnabled());
-  }
+  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, 0));
 
@@ -290,17 +263,4 @@ void SimpleLauncherApplet::runDialog() {
   }
 }
 
-LaunchableItem::LaunchableItem(LauncherItem *item, bool enabled): myItem(item), myEnabled(enabled) {
-}
-
-LaunchableItem::~LaunchableItem() {
-  if (myItem != 0) {
-    delete myItem;
-  }
-}
-
-bool LaunchableItem::activate(osso_context_t *context) {
-  return osso_application_top(context, myItem->getService().c_str(), 0) == OSSO_OK;
-}
-
 // vim:ts=2:sw=2:et
index 7f2d842..70dea06 100644 (file)
 
 #include "sla-list.h"
 
-SLAList::SLAList(int icon_size): myWidget(0), myStore(0), myView(0), mySelection(0) {
+SLAList::SLAList(int icon_size, LaunchableItems& items): myWidget(0), myStore(0), myView(0), mySelection(0), myItems(items) {
   GtkTreeViewColumn *column;
   GtkCellRenderer *renderer;
 
-  myStore = gtk_list_store_new(4, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
+  myStore = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_INT);
 
   myView = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(myStore)));
   gtk_tree_view_set_headers_visible(myView, FALSE);
@@ -48,7 +48,8 @@ SLAList::SLAList(int icon_size): myWidget(0), myStore(0), myView(0), mySelection
 
   renderer = gtk_cell_renderer_text_new();
   g_object_set(renderer, "yalign", 0.0, 0);
-  column = GTK_TREE_VIEW_COLUMN(gtk_tree_view_column_new_with_attributes("", renderer, "text", 1, 0));
+  column = gtk_tree_view_column_new();
+  gtk_tree_view_column_set_cell_data_func(column, renderer, _renderText, this, 0);
   gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
   gtk_tree_view_column_set_expand(column, TRUE);
 
@@ -57,7 +58,8 @@ SLAList::SLAList(int icon_size): myWidget(0), myStore(0), myView(0), mySelection
   renderer = gtk_cell_renderer_toggle_new();
   g_object_set(renderer, "activatable", TRUE, 0);
   g_signal_connect(renderer, "toggled", G_CALLBACK(_toggleBool), this);
-  column = GTK_TREE_VIEW_COLUMN(gtk_tree_view_column_new_with_attributes("", renderer, "active", 2, 0));
+  column = gtk_tree_view_column_new();
+  gtk_tree_view_column_set_cell_data_func(column, renderer, _renderBool, this, 0);
 
   gtk_tree_view_insert_column(myView, column, -1);
 
@@ -82,36 +84,25 @@ SLAList::SLAList(int icon_size): myWidget(0), myStore(0), myView(0), mySelection
   gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(table), FALSE, FALSE, 0);
 
   gtk_widget_show_all(myWidget);
+
+  for (LaunchableItems::const_iterator item = myItems.begin(); item != myItems.end(); ++item) {
+    GtkTreeIter iter;
+    
+    gtk_list_store_append(myStore, &iter);
+    gtk_list_store_set(myStore, &iter, 0, item->second->getIcon(icon_size), 1, item-myItems.begin(), -1);
+  }
 }
 
 SLAList::~SLAList() {
   // FIXME: do something! :)
 }
 
-void SLAList::collectItems(std::vector<std::pair<std::string, bool> >& result) {
-  GtkTreeIter iter;
-
-  if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(myStore), &iter)) {
-    char *name;
-    gboolean active;
-
-    gtk_tree_model_get(GTK_TREE_MODEL(myStore), &iter, 3, &name, 2, &active, -1);
-
-    result.push_back(std::pair<std::string, bool>(name, active));
-
-    while (gtk_tree_model_iter_next(GTK_TREE_MODEL(myStore), &iter)) {
-      gtk_tree_model_get(GTK_TREE_MODEL(myStore), &iter, 3, &name, 2, &active, -1);
-
-      result.push_back(std::pair<std::string, bool>(name, active));
-    }
-  }
+void SLAList::_renderText(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer self) {
+  ((SLAList *)self)->renderText(column, cell, model, iter);
 }
 
-void SLAList::addItem(const char *filename, GdkPixbuf *pixbuf, const char *name, bool active) {
-  GtkTreeIter iter;
-  
-  gtk_list_store_append(myStore, &iter);
-  gtk_list_store_set(myStore, &iter, 0, pixbuf, 1, name, 2, active, 3, filename, -1);
+void SLAList::_renderBool(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer self) {
+  ((SLAList *)self)->renderBool(column, cell, model, iter);
 }
 
 void SLAList::_toggleBool(GtkCellRendererToggle *renderer, const gchar *path, void *self) {
@@ -126,6 +117,28 @@ void SLAList::_moveDown(GtkButton *button, void *self) {
   ((SLAList *)self)->moveDown(button);
 }
 
+void SLAList::renderText(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter) {
+  int index;
+
+  gtk_tree_model_get(GTK_TREE_MODEL(myStore), iter, 1, &index, -1);
+
+  if (gtk_tree_selection_iter_is_selected(mySelection, iter)) {
+    gchar *text = g_markup_printf_escaped("%s\n<small>%s</small>", myItems[index].second->getName().c_str(), myItems[index].second->getComment().c_str());
+    g_object_set(cell, "markup", text, 0);
+    g_free(text);
+  } else {
+    g_object_set(cell, "text", myItems[index].second->getName().c_str(), 0);
+  }
+}
+
+void SLAList::renderBool(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter) {
+  int index;
+
+  gtk_tree_model_get(GTK_TREE_MODEL(myStore), iter, 1, &index, -1);
+
+  g_object_set(cell, "active", myItems[index].second->isEnabled(), 0);
+}
+
 void SLAList::toggleBool(GtkCellRendererToggle *renderer, const gchar *spath) {
   GtkTreePath *path = gtk_tree_path_new_from_string(spath);
 
@@ -133,10 +146,10 @@ void SLAList::toggleBool(GtkCellRendererToggle *renderer, const gchar *spath) {
     GtkTreeIter iter;
 
     if (gtk_tree_model_get_iter(GTK_TREE_MODEL(myStore), &iter, path)) {
-      gboolean value;
+      int index;
 
-      gtk_tree_model_get(GTK_TREE_MODEL(myStore), &iter, 2, &value, -1);
-      gtk_list_store_set(myStore, &iter, 2, !value, -1);
+      gtk_tree_model_get(GTK_TREE_MODEL(myStore), &iter, 1, &index, -1);
+      myItems[index].second->toggle();
     }
   }
 }
@@ -153,6 +166,16 @@ void SLAList::moveUp(GtkButton *) {
       GtkTreeIter next;
 
       if (gtk_tree_model_get_iter(GTK_TREE_MODEL(myStore), &next, path)) {
+        int i1, i2;
+
+        gtk_tree_model_get(GTK_TREE_MODEL(myStore), &current, 1, &i1, -1);
+        gtk_tree_model_get(GTK_TREE_MODEL(myStore), &next, 1, &i2, -1);
+
+        std::swap(myItems[i1], myItems[i2]);
+
+        gtk_list_store_set(myStore, &current, 1, i2, -1);
+        gtk_list_store_set(myStore, &next, 1, i1, -1);
+
         gtk_list_store_swap(myStore, &current, &next);
       }
     }
@@ -173,6 +196,16 @@ void SLAList::moveDown(GtkButton *) {
     gtk_tree_path_next(path);
 
     if (gtk_tree_model_get_iter(GTK_TREE_MODEL(myStore), &next, path)) {
+      int i1, i2;
+
+      gtk_tree_model_get(GTK_TREE_MODEL(myStore), &current, 1, &i1, -1);
+      gtk_tree_model_get(GTK_TREE_MODEL(myStore), &next, 1, &i2, -1);
+
+      std::swap(myItems[i1], myItems[i2]);
+
+      gtk_list_store_set(myStore, &current, 1, i2, -1);
+      gtk_list_store_set(myStore, &next, 1, i1, -1);
+
       gtk_list_store_swap(myStore, &current, &next);
     }
 
index 0fbfc3a..5ca852d 100644 (file)
 #ifndef __SLA_LIST_H__
 #define __SLA_LIST_H__
 
-#include <vector>
-#include <string>
-
 #include <gtk/gtkliststore.h>
 #include <gtk/gtktreeview.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkcellrenderertoggle.h>
 
+#include "launchable-item.h"
+
 class SLAList {
 public:
-  SLAList(int);
+  SLAList(int, LaunchableItems&);
  ~SLAList();
 
-  void addItem(const char *filename, GdkPixbuf *pixbuf, const char *name, bool active);
-
   GtkWidget *getWidget() { return myWidget; }
 
-  void collectItems(std::vector<std::pair<std::string, bool> >&);
-
 private:
+  static void _renderText(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer self);
+  static void _renderBool(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer self);
   static void _moveUp(GtkButton *, void *);
   static void _moveDown(GtkButton *, void *);
   static void _toggleBool(GtkCellRendererToggle *, const gchar *, void *);
 
+  void renderText(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter);
+  void renderBool(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter);
   void moveUp(GtkButton *);
   void moveDown(GtkButton *);
   void toggleBool(GtkCellRendererToggle *, const gchar *);
@@ -51,6 +50,8 @@ private:
   GtkListStore *myStore;
   GtkTreeView *myView;
   GtkTreeSelection *mySelection;
+
+  LaunchableItems& myItems;
 };
 
 #endif