put stubs for loading/saving configuration
[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 version 2 as published by
7 // the Free Software Foundation.
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 #include <fstream>
21
22 #include <gtk/gtk.h>
23
24 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
25 #include <libosso.h>
26
27 #include "launcher-item.h"
28 #include "sla-list.h"
29 #include "launchable-item.h"
30
31 #define SL_APPLET_DBUS_NAME  "simple-launcher"
32 #define SL_APPLET_VERSION    "0.0"
33 #define SL_APPLET_ICON_SIZE  26
34 #define SL_APPLET_BORDER_SIZE  14
35 #define SL_APPLET_CANVAS_SIZE  (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
36
37 class SimpleLauncherApplet {
38 public:
39   SimpleLauncherApplet();
40  ~SimpleLauncherApplet();
41
42   bool doInit(void *state_data, int *state_size);
43
44   void background() {}
45   void foreground() {}
46   int saveState(void **state_data, int *state_size);
47   GtkWidget *settings(GtkWindow *parent);
48
49   GtkWidget *getWidget() { return myWidget; }
50
51 private:
52   void loadConfig();
53   void saveConfig();
54
55   bool initWidget();
56
57   void buttonClicked(GtkToolButton *);
58   void runDialog();
59
60   static void _button_clicked(GtkToolButton *, void *);
61   static void _run_dialog(GtkMenuItem *, void *);
62
63 private:
64   osso_context_t *myContext;
65   GtkWidget *myWidget;
66   GtkWindow *myParent;
67
68   LauncherItems myItems;
69
70   static char *ourDirs[];
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 != NULL) {
79     if (applet->doInit(state_data, state_size)) {
80       *widget = applet->getWidget();
81     } else {
82       delete applet;
83       applet = NULL;
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::ourDirs[] = {
115   "/usr/share/applications/hildon",
116   NULL
117 };
118
119 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
120 }
121
122 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
123   if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
124     g_debug("sla-applet: failed to initialize the osso layer");
125     return false;
126   }
127
128   loadConfig();
129
130   if (!initWidget()) {
131     return false;
132   }
133
134   gtk_widget_show_all(myWidget);
135
136   return true;
137 }
138
139 SimpleLauncherApplet::~SimpleLauncherApplet() {
140   for (LauncherItems::iterator it = myItems.begin(); it != myItems.end(); ++it) {
141     if (it->second != NULL) {
142       delete it->second;
143       it->second = NULL;
144     }
145   }
146
147   myItems.resize(0);
148
149   if (myWidget != NULL) {
150     gtk_widget_destroy(myWidget);
151     myWidget = NULL;
152   }
153
154   if (myContext != NULL) {
155     osso_deinitialize(myContext);
156     myContext = NULL;
157   }
158 }
159
160 static char *configFileName="/home/user/.slarc";
161
162 void SimpleLauncherApplet::loadConfig() {
163 #if 0
164   for (int i = 0 ; ourFiles[i] != NULL ; ++i) {
165     LaunchableItem *item = new LaunchableItem();
166
167     if (item->load(ourFiles[i])) {
168       myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
169     } else {
170       delete item;
171     }
172   }
173 #endif
174 }
175
176 void SimpleLauncherApplet::saveConfig() {
177   // TODO: make saving config an atomic operation
178   std::ofstream config(configFileName);
179
180   if (config) {
181     for (LauncherItems::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
182       config << it->first << ',' << it->second->isEnabled() << std::endl;
183     }
184   }
185 }
186
187 bool SimpleLauncherApplet::initWidget() {
188   int button_no = 0;
189
190   GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
191
192   for (LauncherItems::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
193     GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(it->second->getIcon(SL_APPLET_ICON_SIZE)), NULL);
194
195     gtk_object_set_user_data(GTK_OBJECT(button), it->second);
196     g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
197
198     gtk_toolbar_insert(toolbar, button, -1);
199
200     ++button_no;
201   }
202
203   if (button_no) {
204     myWidget = gtk_frame_new(NULL);
205     gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
206     gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
207     gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
208   } else {
209     gtk_widget_destroy(GTK_WIDGET(toolbar));
210   }
211
212   return myWidget != NULL;
213 }
214
215 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
216   ((SimpleLauncherApplet *)self)->buttonClicked(button);
217 }
218
219 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
220   if (button != NULL) {
221     LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
222
223     if (item != NULL) {
224       item->activate(myContext);
225     }
226   }
227 }
228
229 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
230   if (state_data != NULL) {
231     *state_data = NULL;
232   }
233
234   if (state_size != NULL) {
235     *state_size = 0;
236   }
237
238   return 1;
239 }
240
241 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
242   // TODO: in case we want SimpleLauncherApplet to be configurable, this method
243   // should return a gtk_menu_item that would be included in home settings
244   // menu.  Method should make sure that when we activate that item, a
245   // corresponding dialog appears.
246   myParent = parent;  // FIXME: Ugly piece of code :(
247
248   GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
249
250   g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
251
252   return menuItem;
253 }
254
255 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
256   ((SimpleLauncherApplet *)self)->runDialog();
257 }
258
259 void SimpleLauncherApplet::runDialog() {
260   SLAList list(SL_APPLET_ICON_SIZE, myItems);
261
262   GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new_with_buttons("Launcher Settings", myParent, (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), "OK", GTK_RESPONSE_OK, "Cancel", GTK_RESPONSE_CANCEL, NULL));
263
264   gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
265
266   gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
267
268   int response = gtk_dialog_run(dialog);
269
270   gtk_widget_destroy(GTK_WIDGET(dialog));
271
272   switch (response) {
273     case GTK_RESPONSE_OK:
274       break;
275
276     case GTK_RESPONSE_CANCEL:
277       break;
278
279     default:
280       ;     // FIXME: do I want to do anything in here?
281   }
282 }
283
284 // vim:ts=2:sw=2:et