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