added stub for processing directories with .desktop files
[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 <gtk/gtk.h>
23
24 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
25 #include <libosso.h>
26
27 #include "launcher-item.h"
28 #include "sla-list.h"
29 #include "launchable-item.h"
30
31 #define SL_APPLET_DBUS_NAME  "simple-launcher"
32 #define SL_APPLET_VERSION    "0.0"
33 #define SL_APPLET_ICON_SIZE  26
34 #define SL_APPLET_BORDER_SIZE  14
35 #define SL_APPLET_CANVAS_SIZE  (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
36
37 class SimpleLauncherApplet {
38 public:
39   SimpleLauncherApplet();
40  ~SimpleLauncherApplet();
41
42   bool doInit(void *state_data, int *state_size);
43
44   void background() {}
45   void foreground() {}
46   int saveState(void **state_data, int *state_size);
47   GtkWidget *settings(GtkWindow *parent);
48
49   GtkWidget *getWidget() { return myWidget; }
50
51 private:
52   void addItem(const std::string&, bool);
53
54   void loadConfig();
55   void saveConfig();
56
57   void processDirectory(const std::string&);
58
59   bool initWidget();
60   void updateWidget();
61
62   void buttonClicked(GtkToolButton *);
63   void runDialog();
64
65   static void _button_clicked(GtkToolButton *, void *);
66   static void _run_dialog(GtkMenuItem *, void *);
67
68 private:
69   osso_context_t *myContext;
70   GtkWidget *myWidget;
71   GtkWindow *myParent;
72
73   LauncherItems myItems;
74
75   static char *ourDirs[];
76 };
77
78 // Hildon home applet interface functions
79
80 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
81   SimpleLauncherApplet *applet = new SimpleLauncherApplet();
82
83   if (applet != NULL) {
84     if (applet->doInit(state_data, state_size)) {
85       *widget = applet->getWidget();
86     } else {
87       delete applet;
88       applet = NULL;
89     }
90   }
91
92   return (void*)applet;
93 }
94
95 void hildon_home_applet_lib_deinitialize(void *applet_data) {
96   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
97
98   delete applet;
99 }
100
101 void hildon_home_applet_lib_background(void *applet_data) {
102   ((SimpleLauncherApplet *)applet_data)->background();
103 }
104
105 void hildon_home_applet_lib_foreground (void *applet_data) {
106   ((SimpleLauncherApplet *)applet_data)->foreground();
107 }
108
109 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
110   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
111 }
112
113 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
114   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
115 }
116
117 // SimpleLauncherApplet implementation
118
119 char *SimpleLauncherApplet::ourDirs[] = {
120   "/usr/share/applications/hildon",
121   NULL
122 };
123
124 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
125 }
126
127 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
128   if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
129     g_debug("sla-applet: failed to initialize the osso layer");
130     return false;
131   }
132
133   loadConfig();
134
135   for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
136     processDirectory(ourDirs[i]);
137   }
138
139   if (!initWidget()) {
140     return false;
141   }
142
143   gtk_widget_show_all(myWidget);
144
145   return true;
146 }
147
148 SimpleLauncherApplet::~SimpleLauncherApplet() {
149   myItems.clear();
150
151   if (myWidget != NULL) {
152     gtk_widget_destroy(myWidget);
153     myWidget = NULL;
154   }
155
156   if (myContext != NULL) {
157     osso_deinitialize(myContext);
158     myContext = NULL;
159   }
160 }
161
162 void SimpleLauncherApplet::addItem(const std::string& name, bool enabled) {
163   if (!myItems.exists(name)) {
164     LaunchableItem *item = new LaunchableItem();
165
166     item->load(name);
167
168     if (enabled) {
169       item->enable();
170     } else {
171       item->disable();
172     }
173
174     myItems.add(name, item);
175   }
176 }
177
178 static char *configFileName="/home/user/.slarc";
179
180 void SimpleLauncherApplet::loadConfig() {
181   std::ifstream config(configFileName);
182
183   if (config) {
184     char *buffer = new char [1024];
185
186     while (config.getline(buffer, 1024)) {
187       char *p = strchr(buffer, ',');
188
189       if (p != NULL) {
190         *p++ = '\0';
191       }
192
193       addItem(buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
194
195     }
196
197     delete buffer;
198   }
199 }
200
201 void SimpleLauncherApplet::saveConfig() {
202   // TODO: make saving config an atomic operation
203   std::ofstream config(configFileName);
204
205   if (config) {
206     for (size_t i = 0 ; i < myItems.size() ; ++i) {
207       config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
208     }
209   }
210 }
211
212 void SimpleLauncherApplet::processDirectory(const std::string& dirname) {
213 }
214
215 bool SimpleLauncherApplet::initWidget() {
216   myWidget = gtk_frame_new(NULL);
217
218   if (myWidget != NULL) {
219     gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
220
221     updateWidget();
222   }
223
224   return myWidget != NULL;
225 }
226
227 void SimpleLauncherApplet::updateWidget() {
228   GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
229
230   if (child != NULL) {
231     gtk_container_remove(GTK_CONTAINER(myWidget), child);
232     gtk_widget_destroy(child);
233   }
234
235   int button_no = 0;
236   GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
237
238   for (size_t i = 0 ; i < myItems.size() ; ++i) {
239     LauncherItem *item = myItems[i];
240
241     if (item != NULL && item->isEnabled()) {
242       GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
243
244       gtk_object_set_user_data(GTK_OBJECT(button), item);
245       g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
246
247       gtk_toolbar_insert(toolbar, button, -1);
248
249       ++button_no;
250     }
251   }
252
253   if (button_no) {
254     gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
255     gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
256   } else {
257     gtk_widget_destroy(GTK_WIDGET(toolbar));
258   }
259 }
260
261 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
262   ((SimpleLauncherApplet *)self)->buttonClicked(button);
263 }
264
265 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
266   if (button != NULL) {
267     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
268
269     if (item != NULL) {
270       item->activate(myContext);
271     }
272   }
273 }
274
275 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
276   if (state_data != NULL) {
277     *state_data = NULL;
278   }
279
280   if (state_size != NULL) {
281     *state_size = 0;
282   }
283
284   return 1;
285 }
286
287 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
288   // TODO: in case we want SimpleLauncherApplet to be configurable, this method
289   // should return a gtk_menu_item that would be included in home settings
290   // menu.  Method should make sure that when we activate that item, a
291   // corresponding dialog appears.
292   myParent = parent;  // FIXME: Ugly piece of code :(
293
294   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
295
296   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
297
298   return menuItem;
299 }
300
301 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
302   ((SimpleLauncherApplet *)self)->runDialog();
303 }
304
305 void SimpleLauncherApplet::runDialog() {
306   SLAList list(SL_APPLET_ICON_SIZE, myItems);
307
308   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));
309
310   gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
311
312   gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
313
314   int response = gtk_dialog_run(dialog);
315
316   gtk_widget_destroy(GTK_WIDGET(dialog));
317
318   switch (response) {
319     case GTK_RESPONSE_OK:
320       break;
321
322     case GTK_RESPONSE_CANCEL:
323       break;
324
325     default:
326       ;     // FIXME: do I want to do anything in here?
327   }
328 }
329
330 // vim:ts=2:sw=2:et