1 // This file is a part of Simple Launcher
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
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.
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
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
24 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
27 #include "launcher-item.h"
29 #include "launchable-item.h"
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)
37 class SimpleLauncherApplet {
39 SimpleLauncherApplet();
40 ~SimpleLauncherApplet();
42 bool doInit(void *state_data, int *state_size);
46 int saveState(void **state_data, int *state_size);
47 GtkWidget *settings(GtkWindow *parent);
49 GtkWidget *getWidget() { return myWidget; }
52 void addItem(const std::string&, bool);
60 void buttonClicked(GtkToolButton *);
63 static void _button_clicked(GtkToolButton *, void *);
64 static void _run_dialog(GtkMenuItem *, void *);
67 osso_context_t *myContext;
71 LauncherItems myItems;
73 static char *ourDirs[];
76 // Hildon home applet interface functions
78 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
79 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
82 if (applet->doInit(state_data, state_size)) {
83 *widget = applet->getWidget();
93 void hildon_home_applet_lib_deinitialize(void *applet_data) {
94 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
99 void hildon_home_applet_lib_background(void *applet_data) {
100 ((SimpleLauncherApplet *)applet_data)->background();
103 void hildon_home_applet_lib_foreground (void *applet_data) {
104 ((SimpleLauncherApplet *)applet_data)->foreground();
107 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
108 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
111 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
112 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
115 // SimpleLauncherApplet implementation
117 char *SimpleLauncherApplet::ourDirs[] = {
118 "/usr/share/applications/hildon",
122 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
125 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
126 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
127 g_debug("sla-applet: failed to initialize the osso layer");
137 gtk_widget_show_all(myWidget);
142 SimpleLauncherApplet::~SimpleLauncherApplet() {
145 if (myWidget != NULL) {
146 gtk_widget_destroy(myWidget);
150 if (myContext != NULL) {
151 osso_deinitialize(myContext);
156 void SimpleLauncherApplet::addItem(const std::string& name, bool enabled) {
157 if (!myItems.exists(name)) {
158 LaunchableItem *item = new LaunchableItem();
168 myItems.add(name, item);
172 static char *configFileName="/home/user/.slarc";
174 void SimpleLauncherApplet::loadConfig() {
175 std::ifstream config(configFileName);
178 char *buffer = new char [1024];
180 while (config.getline(buffer, 1024)) {
181 char *p = strchr(buffer, ',');
187 addItem(buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
194 for (int i = 0 ; ourFiles[i] != NULL ; ++i) {
195 LaunchableItem *item = new LaunchableItem();
197 if (item->load(ourFiles[i])) {
198 myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
206 void SimpleLauncherApplet::saveConfig() {
207 // TODO: make saving config an atomic operation
208 std::ofstream config(configFileName);
211 for (size_t i = 0 ; i < myItems.size() ; ++i) {
212 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
217 bool SimpleLauncherApplet::initWidget() {
218 myWidget = gtk_frame_new(NULL);
220 if (myWidget != NULL) {
221 gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
226 return myWidget != NULL;
229 void SimpleLauncherApplet::updateWidget() {
230 GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
233 gtk_container_remove(GTK_CONTAINER(myWidget), child);
234 gtk_widget_destroy(child);
238 GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
240 for (size_t i = 0 ; i < myItems.size() ; ++i) {
241 LauncherItem *item = myItems[i];
243 if (item != NULL && item->isEnabled()) {
244 GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
246 gtk_object_set_user_data(GTK_OBJECT(button), item);
247 g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
249 gtk_toolbar_insert(toolbar, button, -1);
256 gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
257 gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
259 gtk_widget_destroy(GTK_WIDGET(toolbar));
263 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
264 ((SimpleLauncherApplet *)self)->buttonClicked(button);
267 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
268 if (button != NULL) {
269 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
272 item->activate(myContext);
277 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
278 if (state_data != NULL) {
282 if (state_size != NULL) {
289 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
290 // TODO: in case we want SimpleLauncherApplet to be configurable, this method
291 // should return a gtk_menu_item that would be included in home settings
292 // menu. Method should make sure that when we activate that item, a
293 // corresponding dialog appears.
294 myParent = parent; // FIXME: Ugly piece of code :(
296 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
298 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
303 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
304 ((SimpleLauncherApplet *)self)->runDialog();
307 void SimpleLauncherApplet::runDialog() {
308 SLAList list(SL_APPLET_ICON_SIZE, myItems);
310 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));
312 gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
314 gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
316 int response = gtk_dialog_run(dialog);
318 gtk_widget_destroy(GTK_WIDGET(dialog));
321 case GTK_RESPONSE_OK:
324 case GTK_RESPONSE_CANCEL:
328 ; // FIXME: do I want to do anything in here?