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 26
37 #define SL_APPLET_BORDER_SIZE 14
38 #define SL_APPLET_CANVAS_SIZE (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
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 buttonClicked(GtkToolButton *);
69 static void _button_clicked(GtkToolButton *, void *);
70 static void _run_dialog(GtkMenuItem *, void *);
73 osso_context_t *myContext;
77 LauncherItems myItems;
79 static char *ourDirs[];
82 // Hildon home applet interface functions
84 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
85 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
88 if (applet->doInit(state_data, state_size)) {
89 *widget = applet->getWidget();
99 void hildon_home_applet_lib_deinitialize(void *applet_data) {
100 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
105 void hildon_home_applet_lib_background(void *applet_data) {
106 ((SimpleLauncherApplet *)applet_data)->background();
109 void hildon_home_applet_lib_foreground (void *applet_data) {
110 ((SimpleLauncherApplet *)applet_data)->foreground();
113 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
114 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
117 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
118 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
121 // SimpleLauncherApplet implementation
123 char *SimpleLauncherApplet::ourDirs[] = {
124 "/usr/share/applications/hildon",
128 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
131 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
132 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
133 g_debug("sla-applet: failed to initialize the osso layer");
146 SimpleLauncherApplet::~SimpleLauncherApplet() {
149 if (myWidget != NULL) {
150 gtk_widget_destroy(myWidget);
154 if (myContext != NULL) {
155 osso_deinitialize(myContext);
160 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
161 if (!items.exists(name)) {
162 LaunchableItem *item = new LaunchableItem();
172 items.add(name, item);
176 // FIXME: this probably should be done somehow differently
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')));
199 void SimpleLauncherApplet::saveConfig() {
200 // TODO: make saving config an atomic operation
201 std::ofstream config(configFileName);
204 for (size_t i = 0 ; i < myItems.size() ; ++i) {
205 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
210 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
211 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
212 processDirectory(items, ourDirs[i]);
216 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
217 DIR *dir = opendir(dirname.c_str());
220 const std::string namePrefix = dirname + "/";
221 std::string shortName;
222 std::string desktopExtension = ".desktop";
225 while ((file = readdir(dir)) != 0) {
226 shortName = file->d_name;
227 if ((shortName == ".") || (shortName == "..")) {
231 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
232 addItem(items, namePrefix+shortName, false);
240 bool SimpleLauncherApplet::initWidget() {
241 myWidget = gtk_frame_new(NULL);
243 if (myWidget != NULL) {
244 gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
249 return myWidget != NULL;
252 void SimpleLauncherApplet::updateWidget() {
253 GtkWidget *child = gtk_bin_get_child(GTK_BIN(myWidget));
256 gtk_container_remove(GTK_CONTAINER(myWidget), child);
257 gtk_widget_destroy(child);
261 GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
263 for (size_t i = 0 ; i < myItems.size() ; ++i) {
264 LauncherItem *item = myItems[i];
266 if (item != NULL && item->isEnabled()) {
267 GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
269 gtk_object_set_user_data(GTK_OBJECT(button), item);
270 g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
272 gtk_toolbar_insert(toolbar, button, -1);
278 gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
280 if (button_no == 0) {
281 gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
283 gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
286 gtk_widget_show_all(myWidget);
289 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
290 ((SimpleLauncherApplet *)self)->buttonClicked(button);
293 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
294 if (button != NULL) {
295 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
298 item->activate(myContext);
303 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
304 if (state_data != NULL) {
308 if (state_size != NULL) {
315 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
316 myParent = parent; // FIXME: Ugly piece of code :(
318 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
320 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
325 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
326 ((SimpleLauncherApplet *)self)->runDialog();
329 void SimpleLauncherApplet::runDialog() {
330 // We update the items before using them to avoid a small memory leak
331 // FIXME: deal with the situation in a better way (figure it out first :))
332 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
334 LauncherItems newItems = myItems;
336 SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems);
338 switch (dialog.run()) {
339 case GTK_RESPONSE_OK:
341 saveConfig(); // save it immediately!
345 case GTK_RESPONSE_CANCEL:
349 ; // FIXME: do I want to do anything in here?