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"
33 #define SL_APPLET_DBUS_NAME "simple-launcher"
34 #define SL_APPLET_VERSION "0.0"
35 #define SL_APPLET_ICON_SIZE 26
36 #define SL_APPLET_BORDER_SIZE 14
37 #define SL_APPLET_CANVAS_SIZE (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
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 buttonClicked(GtkToolButton *);
68 static void _button_clicked(GtkToolButton *, void *);
69 static void _run_dialog(GtkMenuItem *, void *);
72 osso_context_t *myContext;
76 LauncherItems myItems;
78 static char *ourDirs[];
81 // Hildon home applet interface functions
83 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
84 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
87 if (applet->doInit(state_data, state_size)) {
88 *widget = applet->getWidget();
98 void hildon_home_applet_lib_deinitialize(void *applet_data) {
99 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
104 void hildon_home_applet_lib_background(void *applet_data) {
105 ((SimpleLauncherApplet *)applet_data)->background();
108 void hildon_home_applet_lib_foreground (void *applet_data) {
109 ((SimpleLauncherApplet *)applet_data)->foreground();
112 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
113 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
116 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
117 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
120 // SimpleLauncherApplet implementation
122 char *SimpleLauncherApplet::ourDirs[] = {
123 "/usr/share/applications/hildon",
127 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
130 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
131 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
132 g_debug("sla-applet: failed to initialize the osso layer");
142 gtk_widget_show_all(myWidget);
147 SimpleLauncherApplet::~SimpleLauncherApplet() {
150 if (myWidget != NULL) {
151 gtk_widget_destroy(myWidget);
155 if (myContext != NULL) {
156 osso_deinitialize(myContext);
161 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
162 if (!items.exists(name)) {
163 LaunchableItem *item = new LaunchableItem();
173 items.add(name, item);
177 static char *configFileName="/home/user/.slarc";
179 void SimpleLauncherApplet::loadConfig() {
180 std::ifstream config(configFileName);
183 char *buffer = new char [1024];
185 while (config.getline(buffer, 1024)) {
186 char *p = strchr(buffer, ',');
192 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
200 void SimpleLauncherApplet::saveConfig() {
201 // TODO: make saving config an atomic operation
202 std::ofstream config(configFileName);
205 for (size_t i = 0 ; i < myItems.size() ; ++i) {
206 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
211 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
212 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
213 processDirectory(items, ourDirs[i]);
217 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
218 DIR *dir = opendir(dirname.c_str());
221 const std::string namePrefix = dirname + "/";
222 std::string shortName;
223 std::string desktopExtension = ".desktop";
226 while ((file = readdir(dir)) != 0) {
227 shortName = file->d_name;
228 if ((shortName == ".") || (shortName == "..")) {
232 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
233 addItem(items, namePrefix+shortName, false);
241 bool SimpleLauncherApplet::initWidget() {
242 myWidget = gtk_frame_new(NULL);
244 if (myWidget != NULL) {
245 gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
250 return myWidget != NULL;
253 void SimpleLauncherApplet::updateWidget() {
254 GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
257 gtk_container_remove(GTK_CONTAINER(myWidget), child);
258 gtk_widget_destroy(child);
262 GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
264 for (size_t i = 0 ; i < myItems.size() ; ++i) {
265 LauncherItem *item = myItems[i];
267 if (item != NULL && item->isEnabled()) {
268 GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
270 gtk_object_set_user_data(GTK_OBJECT(button), item);
271 g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
273 gtk_toolbar_insert(toolbar, button, -1);
280 gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
281 gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
283 gtk_widget_destroy(GTK_WIDGET(toolbar));
287 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
288 ((SimpleLauncherApplet *)self)->buttonClicked(button);
291 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
292 if (button != NULL) {
293 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
296 item->activate(myContext);
301 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
302 if (state_data != NULL) {
306 if (state_size != NULL) {
313 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
314 // TODO: in case we want SimpleLauncherApplet to be configurable, this method
315 // should return a gtk_menu_item that would be included in home settings
316 // menu. Method should make sure that when we activate that item, a
317 // corresponding dialog appears.
318 myParent = parent; // FIXME: Ugly piece of code :(
320 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
322 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
327 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
328 ((SimpleLauncherApplet *)self)->runDialog();
331 void SimpleLauncherApplet::runDialog() {
332 LauncherItems newItems = myItems;
334 updateItems(newItems); // User requested 'settings', let's give her the latest stuff :)
336 SLAList list(SL_APPLET_ICON_SIZE, newItems);
338 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));
340 gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
342 gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
344 int response = gtk_dialog_run(dialog);
346 gtk_widget_destroy(GTK_WIDGET(dialog));
349 case GTK_RESPONSE_OK:
351 saveConfig(); // save it immediately!
355 case GTK_RESPONSE_CANCEL:
359 ; // FIXME: do I want to do anything in here?