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