ApplicationItemFactory:
[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 "launchable-item.h"
31 #include "settings-dialog.h"
32 #include "gconf-wrapper.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(const GConfKey&);
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   GConfBooleanOption myTransparent;
83   // bool myShowInfobanner; // FIXME: to implement
84   GConfIntegerOption myIconSize;
85
86   static char *ourDirs[];
87 };
88
89 // Hildon home applet interface functions
90
91 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
92   GConfKey baseKey(SL_APPLET_GCONF_PATH);
93
94   SimpleLauncherApplet *applet = new SimpleLauncherApplet(baseKey);
95
96   if (applet != NULL) {
97     if (applet->doInit(state_data, state_size)) {
98       *widget = applet->getWidget();
99     } else {
100       delete applet;
101       applet = NULL;
102     }
103   }
104
105   return (void*)applet;
106 }
107
108 void hildon_home_applet_lib_deinitialize(void *applet_data) {
109   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
110
111   delete applet;
112 }
113
114 void hildon_home_applet_lib_background(void *applet_data) {
115   ((SimpleLauncherApplet *)applet_data)->background();
116 }
117
118 void hildon_home_applet_lib_foreground (void *applet_data) {
119   ((SimpleLauncherApplet *)applet_data)->foreground();
120 }
121
122 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
123   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
124 }
125
126 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
127   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
128 }
129
130 // SimpleLauncherApplet implementation
131
132 char *SimpleLauncherApplet::ourDirs[] = {
133   "/usr/share/applications/hildon",
134   NULL
135 };
136
137 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
138 SimpleLauncherApplet::SimpleLauncherApplet(const GConfKey& base) : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(base, "transparent", true), myIconSize(base, "icon_size", 26) {
139 }
140
141 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
142   if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
143     g_debug("sla-applet: failed to initialize the osso layer");
144     return false;
145   }
146
147   loadConfig();
148
149   if (!initWidget()) {
150     return false;
151   }
152
153   return true;
154 }
155
156 SimpleLauncherApplet::~SimpleLauncherApplet() {
157   myItems.clear();
158 #if 0
159   // This does not seem to be necessary
160   if (myWidget != NULL) {
161     gtk_widget_destroy(myWidget);
162     myWidget = NULL;
163   }
164 #endif
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 static const gchar *getConfigFileName() {
188   static gchar *configFileName = NULL;
189
190   if (configFileName == NULL) {
191     configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
192   }
193
194   return configFileName;
195 }
196
197 void SimpleLauncherApplet::loadConfig() {
198   std::ifstream config(getConfigFileName());
199
200   if (config) {
201     char *buffer = new char [1024];
202
203     while (config.getline(buffer, 1024)) {
204       char *p = strchr(buffer, ',');
205
206       if (p != NULL) {
207         *p++ = '\0';
208       }
209
210       addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
211     }
212
213     delete [] buffer;
214   }
215 }
216
217 void SimpleLauncherApplet::saveConfig() {
218   // TODO: make saving config an atomic operation
219   std::ofstream config(getConfigFileName());
220
221   if (config) {
222     for (size_t i = 0 ; i < myItems.size() ; ++i) {
223       config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
224     }
225   }
226 }
227
228 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
229   for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
230     processDirectory(items, ourDirs[i]);
231   }
232 }
233
234 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
235   DIR *dir = opendir(dirname.c_str());
236
237   if (dir != NULL) {
238     const std::string namePrefix = dirname + "/";
239     std::string shortName;
240     std::string desktopExtension = ".desktop";
241     const dirent *file;
242
243     while ((file = readdir(dir)) != 0) {
244       shortName = file->d_name;
245       if ((shortName == ".") || (shortName == "..")) {
246         continue;
247       }
248
249       if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
250         addItem(items, namePrefix+shortName, false);
251       }
252     }
253
254     closedir(dir);
255   }
256 }
257
258 bool SimpleLauncherApplet::initWidget() {
259   myWidget = gtk_hbox_new(false, 2);
260
261   if (myWidget != NULL) {
262     updateWidget();
263   }
264
265   return myWidget != NULL;
266 }
267
268 void SimpleLauncherApplet::updateWidget() {
269   gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
270
271   GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
272
273   int button_no = 0;
274
275   for (size_t i = 0 ; i < myItems.size() ; ++i) {
276     LauncherItem *item = myItems[i];
277
278     if (item != NULL && item->isEnabled()) {
279       GtkWidget *button = gtk_event_box_new();
280
281       gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
282       g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
283
284       gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
285
286       {
287         GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
288         gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
289         g_object_unref(G_OBJECT(pixbuf));
290       }
291
292       gtk_object_set_user_data(GTK_OBJECT(button), item);
293
294       gtk_size_group_add_widget(group, button);
295
296       gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
297
298       ++button_no;
299     }
300   }
301
302   g_object_unref(G_OBJECT(group));
303
304   int totalSize = myIconSize.value();
305
306   if (button_no == 0) {
307     gtk_widget_set_size_request(myWidget, totalSize, totalSize);
308   } else {
309     gtk_widget_set_size_request(myWidget, button_no*totalSize, totalSize);
310   }
311
312   gtk_widget_show_all(myWidget);
313 }
314
315 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
316   ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
317 }
318
319 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
320   if (button != NULL && event->button == 1) {
321     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
322
323     if (item != NULL) {
324       item->activate(myContext);
325     }
326   }
327 }
328
329 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
330   if (state_data != NULL) {
331     *state_data = NULL;
332   }
333
334   if (state_size != NULL) {
335     *state_size = 0;
336   }
337
338   return 1;
339 }
340
341 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
342   myParent = parent;  // FIXME: Ugly piece of code :(
343
344   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
345
346   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
347
348   return menuItem;
349 }
350
351 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
352   ((SimpleLauncherApplet *)self)->runDialog();
353 }
354
355 void SimpleLauncherApplet::runDialog() {
356   // We update the items before using them to avoid a small memory leak
357   // FIXME: deal with the situation in a better way (figure it out first :))
358   updateItems(myItems);       // User requested 'settings', let's give her the latest stuff :)
359
360   LauncherItems newItems = myItems;
361
362   // TODO: make it nicer... this code is ugly :(
363   SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
364
365   switch (dialog.run()) {
366     case GTK_RESPONSE_OK:
367       myItems = newItems;
368       dialog.updateValues();  // FIXME: hackish :( make it better
369
370       saveConfig();   // save it immediately!
371       updateWidget();
372       break;
373
374     case GTK_RESPONSE_CANCEL:
375       break;
376
377     default:
378       ;     // FIXME: do I want to do anything in here?
379   }
380
381   // newItems.clear(); // TODO: do I really need it?
382 }
383
384 // vim:ts=2:sw=2:et