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
26 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
29 #include "launcher-item.h"
31 #include "launchable-item.h"
32 #include "settings-dialog.h"
34 #define SL_APPLET_DBUS_NAME "simple-launcher"
35 #define SL_APPLET_VERSION "0.0"
36 #define SL_APPLET_ICON_SIZE 48
38 #define SL_APPLET_GCONF_PATH "/apps/simple-launcher"
40 class SimpleLauncherApplet {
42 SimpleLauncherApplet();
43 ~SimpleLauncherApplet();
45 bool doInit(void *state_data, int *state_size);
49 int saveState(void **state_data, int *state_size);
50 GtkWidget *settings(GtkWindow *parent);
52 GtkWidget *getWidget() { return myWidget; }
55 static void addItem(LauncherItems&, const std::string&, bool);
60 static void updateItems(LauncherItems&);
61 static void processDirectory(LauncherItems&, const std::string&);
66 void buttonPressed(GtkWidget *button, GdkEventButton *event);
69 static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
70 static void _run_dialog(GtkMenuItem *, void *);
73 // GConfClientWrapper myClient;
74 // GConfKey myMainSettings;
76 osso_context_t *myContext;
81 LauncherItems myItems;
83 static char *ourDirs[];
86 // Hildon home applet interface functions
88 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
89 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
92 if (applet->doInit(state_data, state_size)) {
93 *widget = applet->getWidget();
100 return (void*)applet;
103 void hildon_home_applet_lib_deinitialize(void *applet_data) {
104 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
109 void hildon_home_applet_lib_background(void *applet_data) {
110 ((SimpleLauncherApplet *)applet_data)->background();
113 void hildon_home_applet_lib_foreground (void *applet_data) {
114 ((SimpleLauncherApplet *)applet_data)->foreground();
117 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
118 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
121 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
122 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
125 // SimpleLauncherApplet implementation
127 char *SimpleLauncherApplet::ourDirs[] = {
128 "/usr/share/applications/hildon",
132 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
133 SimpleLauncherApplet::SimpleLauncherApplet() : myContext(NULL), myWidget(NULL), myParent(NULL) {
136 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
137 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
138 g_debug("sla-applet: failed to initialize the osso layer");
151 SimpleLauncherApplet::~SimpleLauncherApplet() {
154 if (myWidget != NULL) {
155 gtk_widget_destroy(myWidget);
159 if (myContext != NULL) {
160 osso_deinitialize(myContext);
165 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
166 if (!items.exists(name)) {
167 LaunchableItem *item = new LaunchableItem();
177 items.add(name, item);
181 // FIXME: this probably should be done somehow differently
182 static char *configFileName="/home/user/.slarc";
184 void SimpleLauncherApplet::loadConfig() {
185 std::ifstream config(configFileName);
188 char *buffer = new char [1024];
190 while (config.getline(buffer, 1024)) {
191 char *p = strchr(buffer, ',');
197 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
204 void SimpleLauncherApplet::saveConfig() {
205 // TODO: make saving config an atomic operation
206 std::ofstream config(configFileName);
209 for (size_t i = 0 ; i < myItems.size() ; ++i) {
210 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
215 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
216 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
217 processDirectory(items, ourDirs[i]);
221 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
222 DIR *dir = opendir(dirname.c_str());
225 const std::string namePrefix = dirname + "/";
226 std::string shortName;
227 std::string desktopExtension = ".desktop";
230 while ((file = readdir(dir)) != 0) {
231 shortName = file->d_name;
232 if ((shortName == ".") || (shortName == "..")) {
236 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
237 addItem(items, namePrefix+shortName, false);
245 bool SimpleLauncherApplet::initWidget() {
246 myWidget = gtk_hbox_new(false, 0);
248 if (myWidget != NULL) {
252 return myWidget != NULL;
255 void SimpleLauncherApplet::updateWidget() {
256 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
258 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
262 for (size_t i = 0 ; i < myItems.size() ; ++i) {
263 LauncherItem *item = myItems[i];
265 if (item != NULL && item->isEnabled()) {
266 GtkWidget *button = gtk_event_box_new();
268 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
269 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
271 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), false);
272 // gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);
274 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)));
276 gtk_object_set_user_data(GTK_OBJECT(button), item);
278 gtk_size_group_add_widget(group, button);
280 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
286 g_object_unref(G_OBJECT(group));
288 if (button_no == 0) {
289 gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE, SL_APPLET_ICON_SIZE);
291 gtk_widget_set_size_request(myWidget, button_no*SL_APPLET_ICON_SIZE, SL_APPLET_ICON_SIZE);
294 gtk_widget_show_all(myWidget);
297 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
298 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
301 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
302 if (button != NULL && event->button == 1) {
303 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
306 item->activate(myContext);
311 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
312 if (state_data != NULL) {
316 if (state_size != NULL) {
323 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
324 myParent = parent; // FIXME: Ugly piece of code :(
326 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
328 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
333 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
334 ((SimpleLauncherApplet *)self)->runDialog();
337 void SimpleLauncherApplet::runDialog() {
338 // We update the items before using them to avoid a small memory leak
339 // FIXME: deal with the situation in a better way (figure it out first :))
340 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
342 LauncherItems newItems = myItems;
344 SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems);
346 switch (dialog.run()) {
347 case GTK_RESPONSE_OK:
349 saveConfig(); // save it immediately!
353 case GTK_RESPONSE_CANCEL:
357 ; // FIXME: do I want to do anything in here?