moved widget initialization into a separate method
[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 private:
39   bool initWidget();
40
41   bool startApplication(const std::string& application);
42
43 private:
44   osso_context_t *myContext;
45   GtkWidget *myWidget;
46
47   std::vector<LauncherItem *> myItems;
48
49   static char *ourFiles[];
50 };
51
52 // Hildon home applet interface functions
53
54 void *hildon_home_applet_lib_initialize (void *state_data, int *state_size, GtkWidget **widget) {
55   SimpleLauncherApplet *applet = new SimpleLauncherApplet();
56
57   if (applet != 0) {
58     if (applet->doInit(state_data, state_size)) {
59       *widget = applet->getWidget();
60     } else {
61       delete applet;
62       applet = 0;
63     }
64   }
65
66   return (void*)applet;
67 }
68
69 void hildon_home_applet_lib_deinitialize(void *applet_data) {
70   SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
71
72   delete applet;
73 }
74
75 void hildon_home_applet_lib_background(void *applet_data) {
76   ((SimpleLauncherApplet *)applet_data)->background();
77 }
78
79 void hildon_home_applet_lib_foreground (void *applet_data) {
80   ((SimpleLauncherApplet *)applet_data)->foreground();
81 }
82
83 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
84   return ((SimpleLauncherApplet *)applet_data)->settings(parent);
85 }
86
87 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
88   return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
89 }
90
91 // SimpleLauncherApplet implementation
92
93 char *SimpleLauncherApplet::ourFiles[] = {
94   0
95 };
96
97 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(0), myWidget(0) {
98 }
99
100 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
101   if ((myContext = osso_initialize(SLA_APPLET_DBUS_NAME, SLA_APPLET_VERSION, FALSE, NULL)) == 0) {
102     g_debug("sla-applet: failed to initialize the osso layer");
103     return false;
104   }
105
106   if (!initWidget()) {
107     return false;
108   }
109
110   for (int i = 0 ; ourFiles[i] != 0 ; ++i) {
111     LauncherItem *item = new LauncherItem();
112
113     if (item->load(ourFiles[i])) {
114       myItems.push_back(item);
115     } else {
116       delete item;
117     }
118   }
119
120   // g_signal_connect (applet->myWidget, "do_search", G_CALLBACK (mis_applet_do_search), (gpointer)applet);
121
122   gtk_widget_show_all(myWidget);
123
124   return true;
125 }
126
127 SimpleLauncherApplet::~SimpleLauncherApplet() {
128   for (std::vector<LauncherItem *>::iterator it = myItems.begin(); it != myItems.end(); ++it) {
129     if (*it != 0) {
130       delete *it;
131       *it = 0;
132     }
133   }
134
135   myItems.resize(0);
136
137   if (myWidget != 0) {
138     gtk_widget_destroy(myWidget);
139     myWidget = 0;
140   }
141
142   if (myContext != 0) {
143     osso_deinitialize(myContext);
144     myContext = 0;
145   }
146 }
147
148 bool SimpleLauncherApplet::initWidget() {
149   return false;
150 }
151
152 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
153   if (state_data != 0) {
154     *state_data = 0;
155   }
156
157   if (state_size != 0) {
158     *state_size = 0;
159   }
160
161   return 1;
162 }
163
164 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
165   // TODO: in case we want SimpleLauncherApplet to be configurable, this method
166   // should return a gtk_menu_item that would be included in home settings
167   // menu.  Method should make sure that when we activate that item, a
168   // corresponding dialog appears.
169   return 0;
170 }
171
172 bool SimpleLauncherApplet::startApplication(const std::string& application) {
173   return osso_application_top(myContext, application.c_str(), 0) == OSSO_OK;
174 }