added a configurable canvas around button icons
[simple-launcher] / simple-launcher.cc
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
4 //
5 // Simple Launcher is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License version 2 as published by
7 // the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 #include <string>
19 #include <vector>
20 #include <fstream>
21
22 #include <dirent.h>
23
24 #include <gtk/gtk.h>
25
26 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
27 #include <libosso.h>
28
29 #include "launcher-item.h"
30 #include "sla-list.h"
31 #include "launchable-item.h"
32 #include "settings-dialog.h"
33
34 #define SL_APPLET_DBUS_NAME  "simple-launcher"
35 #define SL_APPLET_VERSION    "0.0"
36
37 #define SL_APPLET_GCONF_PATH  "/apps/simple-launcher"
38
39 class SimpleLauncherApplet {
40 public:
41   SimpleLauncherApplet();
42  ~SimpleLauncherApplet();
43
44   bool doInit(void *state_data, int *state_size);
45
46   void background() {}
47   void foreground() {}
48   int saveState(void **state_data, int *state_size);
49   GtkWidget *settings(GtkWindow *parent);
50
51   GtkWidget *getWidget() { return myWidget; }
52
53 private:
54   static void addItem(LauncherItems&, const std::string&, bool);
55
56   void loadConfig();
57   void saveConfig();
58
59   static void updateItems(LauncherItems&);
60   static void processDirectory(LauncherItems&, const std::string&);
61
62   bool initWidget();
63   void updateWidget();
64
65   void buttonPressed(GtkWidget *button, GdkEventButton *event);
66   void runDialog();
67
68   static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
69   static void _run_dialog(GtkMenuItem *, void *);
70
71 private:
72   // GConfClientWrapper myClient;
73   // GConfKey myMainSettings;
74
75   osso_context_t *myContext;
76
77   GtkWidget *myWidget;
78   GtkWindow *myParent;
79
80   LauncherItems myItems;
81
82   bool myTransparent;
83   // bool myShowInfobanner; // FIXME: to implement
84   int myIconSize;
85   int myCanvasSize;
86
87   static char *ourDirs[];
88 };
89
90 // Hildon home applet interface functions
91
92 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
93   SimpleLauncherApplet *applet = new SimpleLauncherApplet();
94
95   if (applet != NULL) {
96     if (applet->doInit(state_data, state_size)) {
97       *widget = applet->getWidget();
98     } else {
99       delete applet;
100       applet = NULL;
101     }
102   }
103
104   return (void*)applet;
105 }
106
107 void hildon_home_applet_lib_deinitialize(void *applet_data) {
108   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
109
110   delete applet;
111 }
112
113 void hildon_home_applet_lib_background(void *applet_data) {
114   ((SimpleLauncherApplet *)applet_data)->background();
115 }
116
117 void hildon_home_applet_lib_foreground (void *applet_data) {
118   ((SimpleLauncherApplet *)applet_data)->foreground();
119 }
120
121 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
122   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
123 }
124
125 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
126   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
127 }
128
129 // SimpleLauncherApplet implementation
130
131 char *SimpleLauncherApplet::ourDirs[] = {
132   "/usr/share/applications/hildon",
133   NULL
134 };
135
136 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
137 SimpleLauncherApplet::SimpleLauncherApplet() : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(true), myIconSize(48), myCanvasSize(1) {
138 }
139
140 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
141   if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
142     g_debug("sla-applet: failed to initialize the osso layer");
143     return false;
144   }
145
146   loadConfig();
147
148   if (!initWidget()) {
149     return false;
150   }
151
152   return true;
153 }
154
155 SimpleLauncherApplet::~SimpleLauncherApplet() {
156   myItems.clear();
157
158   if (myWidget != NULL) {
159     gtk_widget_destroy(myWidget);
160     myWidget = NULL;
161   }
162
163   if (myContext != NULL) {
164     osso_deinitialize(myContext);
165     myContext = NULL;
166   }
167 }
168
169 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
170   if (!items.exists(name)) {
171     LaunchableItem *item = new LaunchableItem();
172
173     item->load(name);
174
175     if (enabled) {
176       item->enable();
177     } else {
178       item->disable();
179     }
180
181     items.add(name, item);
182   }
183 }
184
185 // FIXME: this probably should be done somehow differently
186 static char *configFileName="/home/user/.slarc";
187
188 void SimpleLauncherApplet::loadConfig() {
189   std::ifstream config(configFileName);
190
191   if (config) {
192     char *buffer = new char [1024];
193
194     while (config.getline(buffer, 1024)) {
195       char *p = strchr(buffer, ',');
196
197       if (p != NULL) {
198         *p++ = '\0';
199       }
200
201       addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
202     }
203
204     delete buffer;
205   }
206 }
207
208 void SimpleLauncherApplet::saveConfig() {
209   // TODO: make saving config an atomic operation
210   std::ofstream config(configFileName);
211
212   if (config) {
213     for (size_t i = 0 ; i < myItems.size() ; ++i) {
214       config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
215     }
216   }
217 }
218
219 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
220   for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
221     processDirectory(items, ourDirs[i]);
222   }
223 }
224
225 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
226   DIR *dir = opendir(dirname.c_str());
227
228   if (dir != NULL) {
229     const std::string namePrefix = dirname + "/";
230     std::string shortName;
231     std::string desktopExtension = ".desktop";
232     const dirent *file;
233
234     while ((file = readdir(dir)) != 0) {
235       shortName = file->d_name;
236       if ((shortName == ".") || (shortName == "..")) {
237         continue;
238       }
239
240       if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
241         addItem(items, namePrefix+shortName, false);
242       }
243     }
244
245     closedir(dir);
246   }
247 }
248
249 bool SimpleLauncherApplet::initWidget() {
250   myWidget = gtk_hbox_new(false, 0);
251
252   if (myWidget != NULL) {
253     updateWidget();
254   }
255
256   return myWidget != NULL;
257 }
258
259 void SimpleLauncherApplet::updateWidget() {
260   gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
261
262   GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
263
264   int button_no = 0;
265
266   for (size_t i = 0 ; i < myItems.size() ; ++i) {
267     LauncherItem *item = myItems[i];
268
269     if (item != NULL && item->isEnabled()) {
270       GtkWidget *button = gtk_event_box_new();
271
272       gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
273       g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
274
275       gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent);
276       gtk_container_set_border_width(GTK_CONTAINER(button), myCanvasSize);
277
278       {
279         GdkPixbuf *pixbuf = item->getIcon(myIconSize);
280         gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
281         g_object_unref(G_OBJECT(pixbuf));
282       }
283
284       gtk_object_set_user_data(GTK_OBJECT(button), item);
285
286       gtk_size_group_add_widget(group, button);
287
288       gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
289
290       ++button_no;
291     }
292   }
293
294   g_object_unref(G_OBJECT(group));
295
296   if (button_no == 0) {
297     gtk_widget_set_size_request(myWidget, myIconSize+myCanvasSize+myCanvasSize, myIconSize+myCanvasSize+myCanvasSize);
298   } else {
299     gtk_widget_set_size_request(myWidget, button_no*(myIconSize+myCanvasSize+myCanvasSize), myIconSize+myCanvasSize+myCanvasSize);
300   }
301
302   gtk_widget_show_all(myWidget);
303 }
304
305 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
306   ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
307 }
308
309 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
310   if (button != NULL && event->button == 1) {
311     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
312
313     if (item != NULL) {
314       item->activate(myContext);
315     }
316   }
317 }
318
319 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
320   if (state_data != NULL) {
321     *state_data = NULL;
322   }
323
324   if (state_size != NULL) {
325     *state_size = 0;
326   }
327
328   return 1;
329 }
330
331 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
332   myParent = parent;  // FIXME: Ugly piece of code :(
333
334   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
335
336   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
337
338   return menuItem;
339 }
340
341 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
342   ((SimpleLauncherApplet *)self)->runDialog();
343 }
344
345 void SimpleLauncherApplet::runDialog() {
346   // We update the items before using them to avoid a small memory leak
347   // FIXME: deal with the situation in a better way (figure it out first :))
348   updateItems(myItems);       // User requested 'settings', let's give her the latest stuff :)
349
350   LauncherItems newItems = myItems;
351
352   SettingsDialog dialog(myParent, newItems);
353
354   switch (dialog.run()) {
355     case GTK_RESPONSE_OK:
356       myItems = newItems;
357       saveConfig();   // save it immediately!
358       updateWidget();
359       break;
360
361     case GTK_RESPONSE_CANCEL:
362       break;
363
364     default:
365       ;     // FIXME: do I want to do anything in here?
366   }
367 }
368
369 // vim:ts=2:sw=2:et