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"
37 #define SL_APPLET_GCONF_PATH "/apps/simple-launcher"
39 class SimpleLauncherApplet {
41 SimpleLauncherApplet();
42 ~SimpleLauncherApplet();
44 bool doInit(void *state_data, int *state_size);
48 int saveState(void **state_data, int *state_size);
49 GtkWidget *settings(GtkWindow *parent);
51 GtkWidget *getWidget() { return myWidget; }
54 static void addItem(LauncherItems&, const std::string&, bool);
59 static void updateItems(LauncherItems&);
60 static void processDirectory(LauncherItems&, const std::string&);
65 void buttonPressed(GtkWidget *button, GdkEventButton *event);
68 static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
69 static void _run_dialog(GtkMenuItem *, void *);
72 // GConfClientWrapper myClient;
73 // GConfKey myMainSettings;
75 osso_context_t *myContext;
80 LauncherItems myItems;
83 // bool myShowInfobanner; // FIXME: to implement
87 static char *ourDirs[];
90 // Hildon home applet interface functions
92 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
93 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
96 if (applet->doInit(state_data, state_size)) {
97 *widget = applet->getWidget();
104 return (void*)applet;
107 void hildon_home_applet_lib_deinitialize(void *applet_data) {
108 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
113 void hildon_home_applet_lib_background(void *applet_data) {
114 ((SimpleLauncherApplet *)applet_data)->background();
117 void hildon_home_applet_lib_foreground (void *applet_data) {
118 ((SimpleLauncherApplet *)applet_data)->foreground();
121 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
122 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
125 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
126 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
129 // SimpleLauncherApplet implementation
131 char *SimpleLauncherApplet::ourDirs[] = {
132 "/usr/share/applications/hildon",
136 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
137 SimpleLauncherApplet::SimpleLauncherApplet() : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(true), myIconSize(48), myCanvasSize(1) {
140 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
141 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
142 g_debug("sla-applet: failed to initialize the osso layer");
155 SimpleLauncherApplet::~SimpleLauncherApplet() {
158 if (myWidget != NULL) {
159 gtk_widget_destroy(myWidget);
163 if (myContext != NULL) {
164 osso_deinitialize(myContext);
169 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
170 if (!items.exists(name)) {
171 LaunchableItem *item = new LaunchableItem();
181 items.add(name, item);
185 // FIXME: this probably should be done somehow differently
186 static char *configFileName="/home/user/.slarc";
188 void SimpleLauncherApplet::loadConfig() {
189 std::ifstream config(configFileName);
192 char *buffer = new char [1024];
194 while (config.getline(buffer, 1024)) {
195 char *p = strchr(buffer, ',');
201 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
208 void SimpleLauncherApplet::saveConfig() {
209 // TODO: make saving config an atomic operation
210 std::ofstream config(configFileName);
213 for (size_t i = 0 ; i < myItems.size() ; ++i) {
214 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
219 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
220 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
221 processDirectory(items, ourDirs[i]);
225 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
226 DIR *dir = opendir(dirname.c_str());
229 const std::string namePrefix = dirname + "/";
230 std::string shortName;
231 std::string desktopExtension = ".desktop";
234 while ((file = readdir(dir)) != 0) {
235 shortName = file->d_name;
236 if ((shortName == ".") || (shortName == "..")) {
240 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
241 addItem(items, namePrefix+shortName, false);
249 bool SimpleLauncherApplet::initWidget() {
250 myWidget = gtk_hbox_new(false, 0);
252 if (myWidget != NULL) {
256 return myWidget != NULL;
259 void SimpleLauncherApplet::updateWidget() {
260 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
262 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
266 for (size_t i = 0 ; i < myItems.size() ; ++i) {
267 LauncherItem *item = myItems[i];
269 if (item != NULL && item->isEnabled()) {
270 GtkWidget *button = gtk_event_box_new();
272 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
273 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
275 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent);
276 gtk_container_set_border_width(GTK_CONTAINER(button), myCanvasSize);
279 GdkPixbuf *pixbuf = item->getIcon(myIconSize);
280 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
281 g_object_unref(G_OBJECT(pixbuf));
284 gtk_object_set_user_data(GTK_OBJECT(button), item);
286 gtk_size_group_add_widget(group, button);
288 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
294 g_object_unref(G_OBJECT(group));
296 if (button_no == 0) {
297 gtk_widget_set_size_request(myWidget, myIconSize+myCanvasSize+myCanvasSize, myIconSize+myCanvasSize+myCanvasSize);
299 gtk_widget_set_size_request(myWidget, button_no*(myIconSize+myCanvasSize+myCanvasSize), myIconSize+myCanvasSize+myCanvasSize);
302 gtk_widget_show_all(myWidget);
305 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
306 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
309 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
310 if (button != NULL && event->button == 1) {
311 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
314 item->activate(myContext);
319 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
320 if (state_data != NULL) {
324 if (state_size != NULL) {
331 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
332 myParent = parent; // FIXME: Ugly piece of code :(
334 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
336 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
341 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
342 ((SimpleLauncherApplet *)self)->runDialog();
345 void SimpleLauncherApplet::runDialog() {
346 // We update the items before using them to avoid a small memory leak
347 // FIXME: deal with the situation in a better way (figure it out first :))
348 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
350 LauncherItems newItems = myItems;
352 SettingsDialog dialog(myParent, newItems);
354 switch (dialog.run()) {
355 case GTK_RESPONSE_OK:
357 saveConfig(); // save it immediately!
361 case GTK_RESPONSE_CANCEL:
365 ; // FIXME: do I want to do anything in here?