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