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
28 #include "launcher-item.h"
29 #include "launchable-item.h"
30 #include "settings-dialog.h"
31 #include "gconf-wrapper.h"
33 #define SL_APPLET_DBUS_NAME "simple-launcher"
34 #define SL_APPLET_VERSION "0.0"
36 #define SL_APPLET_GCONF_PATH "/apps/simple-launcher"
38 // A copy of interface functions from hildon-home-plugin-interface (new hildon desktop does not have it) {{{
42 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget);
43 int hildon_home_applet_lib_save_state(void *applet_data, void **state_data, int *state_size);
44 void hildon_home_applet_lib_background(void *applet_data);
45 void hildon_home_applet_lib_foreground(void *applet_data);
46 void hildon_home_applet_lib_deinitialize(void *applet_data);
47 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent);
53 class SimpleLauncherApplet {
55 SimpleLauncherApplet(const GConfKey&);
56 ~SimpleLauncherApplet();
58 bool doInit(void *state_data, int *state_size);
62 int saveState(void **state_data, int *state_size);
63 GtkWidget *settings(GtkWindow *parent);
65 GtkWidget *getWidget() { return myWidget; }
68 static void addItem(LauncherItems&, const std::string&, bool);
73 static void updateItems(LauncherItems&);
74 static void processDirectory(LauncherItems&, const std::string&);
79 void buttonPressed(GtkWidget *button, GdkEventButton *event);
82 static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
83 static void _run_dialog(GtkMenuItem *, void *);
86 // GConfClientWrapper myClient;
87 // GConfKey myMainSettings;
89 osso_context_t *myContext;
94 LauncherItems myItems;
96 GConfBooleanOption myTransparent;
97 // bool myShowInfobanner; // FIXME: to implement
98 GConfIntegerOption myIconSize;
100 static char *ourDirs[];
103 // Hildon home applet interface functions
105 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
106 GConfKey baseKey(SL_APPLET_GCONF_PATH);
108 SimpleLauncherApplet *applet = new SimpleLauncherApplet(baseKey);
110 if (applet != NULL) {
111 if (applet->doInit(state_data, state_size)) {
112 *widget = applet->getWidget();
119 return (void*)applet;
122 void hildon_home_applet_lib_deinitialize(void *applet_data) {
123 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
128 void hildon_home_applet_lib_background(void *applet_data) {
129 ((SimpleLauncherApplet *)applet_data)->background();
132 void hildon_home_applet_lib_foreground (void *applet_data) {
133 ((SimpleLauncherApplet *)applet_data)->foreground();
136 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
137 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
140 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
141 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
144 // SimpleLauncherApplet implementation
146 char *SimpleLauncherApplet::ourDirs[] = {
147 "/usr/share/applications/hildon",
151 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
152 SimpleLauncherApplet::SimpleLauncherApplet(const GConfKey& base) : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(base, "transparent", false), myIconSize(base, "icon_size", 48) {
155 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
156 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
157 g_debug("sla-applet: failed to initialize the osso layer");
170 SimpleLauncherApplet::~SimpleLauncherApplet() {
173 // This does not seem to be necessary
174 if (myWidget != NULL) {
175 gtk_widget_destroy(myWidget);
179 if (myContext != NULL) {
180 osso_deinitialize(myContext);
185 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
186 if (!items.exists(name)) {
187 LaunchableItem *item = new LaunchableItem();
197 items.add(name, item);
201 // {{{ Configuration file managment
202 static const gchar *getConfigFileName() {
203 static gchar *configFileName = NULL;
205 if (configFileName == NULL) {
206 configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
209 return configFileName;
212 void SimpleLauncherApplet::loadConfig() {
213 std::ifstream config(getConfigFileName());
216 char *buffer = new char [1024];
218 while (config.getline(buffer, 1024)) {
219 char *p = strchr(buffer, ',');
225 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
232 void SimpleLauncherApplet::saveConfig() {
233 // TODO: make saving config an atomic operation
234 std::ofstream config(getConfigFileName());
237 for (size_t i = 0 ; i < myItems.size() ; ++i) {
238 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
245 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
246 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
247 processDirectory(items, ourDirs[i]);
251 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
252 DIR *dir = opendir(dirname.c_str());
255 const std::string namePrefix = dirname + "/";
256 std::string shortName;
257 std::string desktopExtension = ".desktop";
260 while ((file = readdir(dir)) != 0) {
261 shortName = file->d_name;
262 if ((shortName == ".") || (shortName == "..")) {
266 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
267 addItem(items, namePrefix+shortName, false);
275 bool SimpleLauncherApplet::initWidget() {
276 myWidget = gtk_hbox_new(false, 0);
278 if (myWidget != NULL) {
282 return myWidget != NULL;
285 void SimpleLauncherApplet::updateWidget() {
286 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
288 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
292 for (size_t i = 0 ; i < myItems.size() ; ++i) {
293 LauncherItem *item = myItems[i];
295 if (item != NULL && item->isEnabled()) {
296 GtkWidget *button = gtk_event_box_new();
298 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
299 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
301 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
304 GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
305 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
306 g_object_unref(G_OBJECT(pixbuf));
309 gtk_object_set_user_data(GTK_OBJECT(button), item);
311 gtk_size_group_add_widget(group, button);
313 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
319 g_object_unref(G_OBJECT(group));
321 int totalSize = myIconSize.value();
323 if (button_no == 0) {
324 gtk_widget_set_size_request(myWidget, totalSize, totalSize);
326 gtk_widget_set_size_request(myWidget, button_no*totalSize, totalSize);
329 gtk_widget_show_all(myWidget);
332 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
333 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
336 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
337 if (button != NULL && event->button == 1) {
338 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
341 item->activate(myContext);
346 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
347 if (state_data != NULL) {
351 if (state_size != NULL) {
358 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
359 myParent = parent; // FIXME: Ugly piece of code :(
361 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
363 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
368 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
369 ((SimpleLauncherApplet *)self)->runDialog();
372 void SimpleLauncherApplet::runDialog() {
373 // We update the items before using them to avoid a small memory leak
374 // FIXME: deal with the situation in a better way (figure it out first :))
375 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
377 LauncherItems newItems = myItems;
379 // TODO: make it nicer... this code is ugly :(
380 SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
382 switch (dialog.run()) {
383 case GTK_RESPONSE_OK:
385 dialog.updateValues(); // FIXME: hackish :( make it better
387 saveConfig(); // save it immediately!
391 case GTK_RESPONSE_CANCEL:
395 ; // FIXME: do I want to do anything in here?
398 // newItems.clear(); // TODO: do I really need it?