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