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