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; }
58 void buttonClicked(GtkToolButton *);
61 static void _button_clicked(GtkToolButton *, void *);
62 static void _run_dialog(GtkMenuItem *, void *);
65 osso_context_t *myContext;
69 LauncherItems myItems;
71 static char *ourDirs[];
74 // Hildon home applet interface functions
76 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
77 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
80 if (applet->doInit(state_data, state_size)) {
81 *widget = applet->getWidget();
91 void hildon_home_applet_lib_deinitialize(void *applet_data) {
92 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
97 void hildon_home_applet_lib_background(void *applet_data) {
98 ((SimpleLauncherApplet *)applet_data)->background();
101 void hildon_home_applet_lib_foreground (void *applet_data) {
102 ((SimpleLauncherApplet *)applet_data)->foreground();
105 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
106 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
109 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
110 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
113 // SimpleLauncherApplet implementation
115 char *SimpleLauncherApplet::ourDirs[] = {
116 "/usr/share/applications/hildon",
120 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
123 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
124 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
125 g_debug("sla-applet: failed to initialize the osso layer");
135 gtk_widget_show_all(myWidget);
140 SimpleLauncherApplet::~SimpleLauncherApplet() {
143 if (myWidget != NULL) {
144 gtk_widget_destroy(myWidget);
148 if (myContext != NULL) {
149 osso_deinitialize(myContext);
154 static char *configFileName="/home/user/.slarc";
156 void SimpleLauncherApplet::loadConfig() {
157 std::ifstream config(configFileName);
160 char *buffer = new char [1024];
162 while (config.getline(buffer, 1024)) {
163 char *p = strchr(buffer, ',');
169 LaunchableItem *item = new LaunchableItem();
173 if (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')) {
179 myItems.add(buffer, item);
186 for (int i = 0 ; ourFiles[i] != NULL ; ++i) {
187 LaunchableItem *item = new LaunchableItem();
189 if (item->load(ourFiles[i])) {
190 myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
198 void SimpleLauncherApplet::saveConfig() {
199 // TODO: make saving config an atomic operation
200 std::ofstream config(configFileName);
203 for (size_t i = 0 ; i < myItems.size() ; ++i) {
204 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
209 bool SimpleLauncherApplet::initWidget() {
210 myWidget = gtk_frame_new(NULL);
212 if (myWidget != NULL) {
213 gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
218 return myWidget != NULL;
221 void SimpleLauncherApplet::updateWidget() {
222 GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
225 gtk_container_remove(GTK_CONTAINER(myWidget), child);
226 gtk_widget_destroy(child);
230 GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
232 for (size_t i = 0 ; i < myItems.size() ; ++i) {
233 LauncherItem *item = myItems[i];
235 if (item != NULL && item->isEnabled()) {
236 GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
238 gtk_object_set_user_data(GTK_OBJECT(button), item);
239 g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
241 gtk_toolbar_insert(toolbar, button, -1);
248 gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
249 gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
251 gtk_widget_destroy(GTK_WIDGET(toolbar));
255 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
256 ((SimpleLauncherApplet *)self)->buttonClicked(button);
259 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
260 if (button != NULL) {
261 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
264 item->activate(myContext);
269 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
270 if (state_data != NULL) {
274 if (state_size != NULL) {
281 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
282 // TODO: in case we want SimpleLauncherApplet to be configurable, this method
283 // should return a gtk_menu_item that would be included in home settings
284 // menu. Method should make sure that when we activate that item, a
285 // corresponding dialog appears.
286 myParent = parent; // FIXME: Ugly piece of code :(
288 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
290 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
295 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
296 ((SimpleLauncherApplet *)self)->runDialog();
299 void SimpleLauncherApplet::runDialog() {
300 SLAList list(SL_APPLET_ICON_SIZE, myItems);
302 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));
304 gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
306 gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
308 int response = gtk_dialog_run(dialog);
310 gtk_widget_destroy(GTK_WIDGET(dialog));
313 case GTK_RESPONSE_OK:
316 case GTK_RESPONSE_CANCEL:
320 ; // FIXME: do I want to do anything in here?