added a fixme note for the config file name
[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
33 #define SL_APPLET_DBUS_NAME  "simple-launcher"
34 #define SL_APPLET_VERSION    "0.0"
35 #define SL_APPLET_ICON_SIZE  26
36 #define SL_APPLET_BORDER_SIZE  14
37 #define SL_APPLET_CANVAS_SIZE  (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
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 buttonClicked(GtkToolButton *);
66   void runDialog();
67
68   static void _button_clicked(GtkToolButton *, void *);
69   static void _run_dialog(GtkMenuItem *, void *);
70
71 private:
72   osso_context_t *myContext;
73   GtkWidget *myWidget;
74   GtkWindow *myParent;
75
76   LauncherItems myItems;
77
78   static char *ourDirs[];
79 };
80
81 // Hildon home applet interface functions
82
83 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
84   SimpleLauncherApplet *applet = new SimpleLauncherApplet();
85
86   if (applet != NULL) {
87     if (applet->doInit(state_data, state_size)) {
88       *widget = applet->getWidget();
89     } else {
90       delete applet;
91       applet = NULL;
92     }
93   }
94
95   return (void*)applet;
96 }
97
98 void hildon_home_applet_lib_deinitialize(void *applet_data) {
99   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
100
101   delete applet;
102 }
103
104 void hildon_home_applet_lib_background(void *applet_data) {
105   ((SimpleLauncherApplet *)applet_data)->background();
106 }
107
108 void hildon_home_applet_lib_foreground (void *applet_data) {
109   ((SimpleLauncherApplet *)applet_data)->foreground();
110 }
111
112 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
113   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
114 }
115
116 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
117   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
118 }
119
120 // SimpleLauncherApplet implementation
121
122 char *SimpleLauncherApplet::ourDirs[] = {
123   "/usr/share/applications/hildon",
124   NULL
125 };
126
127 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
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
148   if (myWidget != NULL) {
149     gtk_widget_destroy(myWidget);
150     myWidget = NULL;
151   }
152
153   if (myContext != NULL) {
154     osso_deinitialize(myContext);
155     myContext = NULL;
156   }
157 }
158
159 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
160   if (!items.exists(name)) {
161     LaunchableItem *item = new LaunchableItem();
162
163     item->load(name);
164
165     if (enabled) {
166       item->enable();
167     } else {
168       item->disable();
169     }
170
171     items.add(name, item);
172   }
173 }
174
175 // FIXME: this probably should be done somehow differently
176 static char *configFileName="/home/user/.slarc";
177
178 void SimpleLauncherApplet::loadConfig() {
179   std::ifstream config(configFileName);
180
181   if (config) {
182     char *buffer = new char [1024];
183
184     while (config.getline(buffer, 1024)) {
185       char *p = strchr(buffer, ',');
186
187       if (p != NULL) {
188         *p++ = '\0';
189       }
190
191       addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
192
193     }
194
195     delete buffer;
196   }
197 }
198
199 void SimpleLauncherApplet::saveConfig() {
200   // TODO: make saving config an atomic operation
201   std::ofstream config(configFileName);
202
203   if (config) {
204     for (size_t i = 0 ; i < myItems.size() ; ++i) {
205       config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
206     }
207   }
208 }
209
210 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
211   for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
212     processDirectory(items, ourDirs[i]);
213   }
214 }
215
216 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
217   DIR *dir = opendir(dirname.c_str());
218
219   if (dir != NULL) {
220     const std::string namePrefix = dirname + "/";
221     std::string shortName;
222     std::string desktopExtension = ".desktop";
223     const dirent *file;
224
225     while ((file = readdir(dir)) != 0) {
226       shortName = file->d_name;
227       if ((shortName == ".") || (shortName == "..")) {
228         continue;
229       }
230
231       if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
232         addItem(items, namePrefix+shortName, false);
233       }
234     }
235
236     closedir(dir);
237   }
238 }
239
240 bool SimpleLauncherApplet::initWidget() {
241   myWidget = gtk_frame_new(NULL);
242
243   if (myWidget != NULL) {
244     gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
245
246     updateWidget();
247   }
248
249   return myWidget != NULL;
250 }
251
252 void SimpleLauncherApplet::updateWidget() {
253   GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
254
255   if (child != NULL) {
256     gtk_container_remove(GTK_CONTAINER(myWidget), child);
257     gtk_widget_destroy(child);
258   }
259
260   int button_no = 0;
261   GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
262
263   for (size_t i = 0 ; i < myItems.size() ; ++i) {
264     LauncherItem *item = myItems[i];
265
266     if (item != NULL && item->isEnabled()) {
267       GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
268
269       gtk_object_set_user_data(GTK_OBJECT(button), item);
270       g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
271
272       gtk_toolbar_insert(toolbar, button, -1);
273
274       ++button_no;
275     }
276   }
277
278   if (button_no) {
279     gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
280     if (button_no == 0) {
281       gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
282     } else {
283       gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
284     }
285   } else {
286     gtk_widget_destroy(GTK_WIDGET(toolbar));
287   }
288
289   gtk_widget_show_all(myWidget);
290 }
291
292 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
293   ((SimpleLauncherApplet *)self)->buttonClicked(button);
294 }
295
296 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
297   if (button != NULL) {
298     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
299
300     if (item != NULL) {
301       item->activate(myContext);
302     }
303   }
304 }
305
306 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
307   if (state_data != NULL) {
308     *state_data = NULL;
309   }
310
311   if (state_size != NULL) {
312     *state_size = 0;
313   }
314
315   return 1;
316 }
317
318 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
319   // TODO: in case we want SimpleLauncherApplet to be configurable, this method
320   // should return a gtk_menu_item that would be included in home settings
321   // menu.  Method should make sure that when we activate that item, a
322   // corresponding dialog appears.
323   myParent = parent;  // FIXME: Ugly piece of code :(
324
325   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
326
327   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
328
329   return menuItem;
330 }
331
332 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
333   ((SimpleLauncherApplet *)self)->runDialog();
334 }
335
336 void SimpleLauncherApplet::runDialog() {
337   LauncherItems newItems = myItems;
338
339   updateItems(newItems);  // User requested 'settings', let's give her the latest stuff :)
340
341   SLAList list(SL_APPLET_ICON_SIZE, newItems);
342
343   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, NULL));
344
345   gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
346
347   gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
348
349   int response = gtk_dialog_run(dialog);
350
351   gtk_widget_destroy(GTK_WIDGET(dialog));
352
353   switch (response) {
354     case GTK_RESPONSE_OK:
355       myItems = newItems;
356       saveConfig();   // save it immediately!
357       updateWidget();
358       break;
359
360     case GTK_RESPONSE_CANCEL:
361       break;
362
363     default:
364       ;     // FIXME: do I want to do anything in here?
365   }
366 }
367
368 // vim:ts=2:sw=2:et