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", true), myIconSize(base, "icon_size", 26) {
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 static const gchar *getConfigFileName() {
202 static gchar *configFileName = NULL;
204 if (configFileName == NULL) {
205 configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
208 return configFileName;
211 void SimpleLauncherApplet::loadConfig() {
212 std::ifstream config(getConfigFileName());
215 char *buffer = new char [1024];
217 while (config.getline(buffer, 1024)) {
218 char *p = strchr(buffer, ',');
224 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
231 void SimpleLauncherApplet::saveConfig() {
232 // TODO: make saving config an atomic operation
233 std::ofstream config(getConfigFileName());
236 for (size_t i = 0 ; i < myItems.size() ; ++i) {
237 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
242 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
243 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
244 processDirectory(items, ourDirs[i]);
248 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
249 DIR *dir = opendir(dirname.c_str());
252 const std::string namePrefix = dirname + "/";
253 std::string shortName;
254 std::string desktopExtension = ".desktop";
257 while ((file = readdir(dir)) != 0) {
258 shortName = file->d_name;
259 if ((shortName == ".") || (shortName == "..")) {
263 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
264 addItem(items, namePrefix+shortName, false);
272 bool SimpleLauncherApplet::initWidget() {
273 myWidget = gtk_hbox_new(false, 2);
275 if (myWidget != NULL) {
279 return myWidget != NULL;
282 void SimpleLauncherApplet::updateWidget() {
283 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
285 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
289 for (size_t i = 0 ; i < myItems.size() ; ++i) {
290 LauncherItem *item = myItems[i];
292 if (item != NULL && item->isEnabled()) {
293 GtkWidget *button = gtk_event_box_new();
295 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
296 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
298 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
301 GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
302 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
303 g_object_unref(G_OBJECT(pixbuf));
306 gtk_object_set_user_data(GTK_OBJECT(button), item);
308 gtk_size_group_add_widget(group, button);
310 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
316 g_object_unref(G_OBJECT(group));
318 int totalSize = myIconSize.value();
320 if (button_no == 0) {
321 gtk_widget_set_size_request(myWidget, totalSize, totalSize);
323 gtk_widget_set_size_request(myWidget, button_no*totalSize, totalSize);
326 gtk_widget_show_all(myWidget);
329 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
330 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
333 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
334 if (button != NULL && event->button == 1) {
335 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
338 item->activate(myContext);
343 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
344 if (state_data != NULL) {
348 if (state_size != NULL) {
355 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
356 myParent = parent; // FIXME: Ugly piece of code :(
358 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
360 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
365 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
366 ((SimpleLauncherApplet *)self)->runDialog();
369 void SimpleLauncherApplet::runDialog() {
370 // We update the items before using them to avoid a small memory leak
371 // FIXME: deal with the situation in a better way (figure it out first :))
372 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
374 LauncherItems newItems = myItems;
376 // TODO: make it nicer... this code is ugly :(
377 SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
379 switch (dialog.run()) {
380 case GTK_RESPONSE_OK:
382 dialog.updateValues(); // FIXME: hackish :( make it better
384 saveConfig(); // save it immediately!
388 case GTK_RESPONSE_CANCEL:
392 ; // FIXME: do I want to do anything in here?
395 // newItems.clear(); // TODO: do I really need it?