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