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