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