made the main widget transparency-capable
[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 #define SL_APPLET_ICON_SIZE  26
37 #define SL_APPLET_BORDER_SIZE  14
38 #define SL_APPLET_CANVAS_SIZE  (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
39
40 class SimpleLauncherApplet {
41 public:
42   SimpleLauncherApplet();
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 buttonClicked(GtkToolButton *);
67   void runDialog();
68
69   static void _button_clicked(GtkToolButton *, void *);
70   static void _run_dialog(GtkMenuItem *, void *);
71
72 private:
73   osso_context_t *myContext;
74   GtkWidget *myWidget;
75   GtkWindow *myParent;
76
77   LauncherItems myItems;
78
79   static char *ourDirs[];
80 };
81
82 // Hildon home applet interface functions
83
84 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
85   SimpleLauncherApplet *applet = new SimpleLauncherApplet();
86
87   if (applet != NULL) {
88     if (applet->doInit(state_data, state_size)) {
89       *widget = applet->getWidget();
90     } else {
91       delete applet;
92       applet = NULL;
93     }
94   }
95
96   return (void*)applet;
97 }
98
99 void hildon_home_applet_lib_deinitialize(void *applet_data) {
100   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
101
102   delete applet;
103 }
104
105 void hildon_home_applet_lib_background(void *applet_data) {
106   ((SimpleLauncherApplet *)applet_data)->background();
107 }
108
109 void hildon_home_applet_lib_foreground (void *applet_data) {
110   ((SimpleLauncherApplet *)applet_data)->foreground();
111 }
112
113 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
114   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
115 }
116
117 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
118   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
119 }
120
121 // SimpleLauncherApplet implementation
122
123 char *SimpleLauncherApplet::ourDirs[] = {
124   "/usr/share/applications/hildon",
125   NULL
126 };
127
128 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
129 }
130
131 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
132   if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
133     g_debug("sla-applet: failed to initialize the osso layer");
134     return false;
135   }
136
137   loadConfig();
138
139   if (!initWidget()) {
140     return false;
141   }
142
143   return true;
144 }
145
146 SimpleLauncherApplet::~SimpleLauncherApplet() {
147   myItems.clear();
148
149   if (myWidget != NULL) {
150     gtk_widget_destroy(myWidget);
151     myWidget = NULL;
152   }
153
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 // FIXME: this probably should be done somehow differently
177 static char *configFileName="/home/user/.slarc";
178
179 void SimpleLauncherApplet::loadConfig() {
180   std::ifstream config(configFileName);
181
182   if (config) {
183     char *buffer = new char [1024];
184
185     while (config.getline(buffer, 1024)) {
186       char *p = strchr(buffer, ',');
187
188       if (p != NULL) {
189         *p++ = '\0';
190       }
191
192       addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
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_hbox_new(false, 0);
242
243   if (myWidget != NULL) {
244     updateWidget();
245   }
246
247   return myWidget != NULL;
248 }
249
250 void SimpleLauncherApplet::updateWidget() {
251   gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
252
253   int button_no = 0;
254
255   for (size_t i = 0 ; i < myItems.size() ; ++i) {
256     LauncherItem *item = myItems[i];
257
258     if (item != NULL && item->isEnabled()) {
259       GtkWidget *button = gtk_button_new();
260
261       // gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);
262       gtk_button_set_focus_on_click(GTK_BUTTON(button),FALSE);
263
264       gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)));
265
266       gtk_object_set_user_data(GTK_OBJECT(button), item);
267       g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
268
269       gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
270
271       ++button_no;
272     }
273   }
274
275   if (button_no == 0) {
276     gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
277   } else {
278     gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
279   }
280
281   gtk_widget_show_all(myWidget);
282 }
283
284 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
285   ((SimpleLauncherApplet *)self)->buttonClicked(button);
286 }
287
288 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
289   if (button != NULL) {
290     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
291
292     if (item != NULL) {
293       item->activate(myContext);
294     }
295   }
296 }
297
298 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
299   if (state_data != NULL) {
300     *state_data = NULL;
301   }
302
303   if (state_size != NULL) {
304     *state_size = 0;
305   }
306
307   return 1;
308 }
309
310 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
311   myParent = parent;  // FIXME: Ugly piece of code :(
312
313   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
314
315   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
316
317   return menuItem;
318 }
319
320 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
321   ((SimpleLauncherApplet *)self)->runDialog();
322 }
323
324 void SimpleLauncherApplet::runDialog() {
325   // We update the items before using them to avoid a small memory leak
326   // FIXME: deal with the situation in a better way (figure it out first :))
327   updateItems(myItems);       // User requested 'settings', let's give her the latest stuff :)
328
329   LauncherItems newItems = myItems;
330
331   SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems);
332
333   switch (dialog.run()) {
334     case GTK_RESPONSE_OK:
335       myItems = newItems;
336       saveConfig();   // save it immediately!
337       updateWidget();
338       break;
339
340     case GTK_RESPONSE_CANCEL:
341       break;
342
343     default:
344       ;     // FIXME: do I want to do anything in here?
345   }
346 }
347
348 // vim:ts=2:sw=2:et