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