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