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