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
38 class SimpleLauncherApplet {
40 SimpleLauncherApplet();
41 ~SimpleLauncherApplet();
43 bool doInit(void *state_data, int *state_size);
47 int saveState(void **state_data, int *state_size);
48 GtkWidget *settings(GtkWindow *parent);
50 GtkWidget *getWidget() { return myWidget; }
53 static void addItem(LauncherItems&, const std::string&, bool);
58 static void updateItems(LauncherItems&);
59 static void processDirectory(LauncherItems&, const std::string&);
64 void buttonPressed(GtkWidget *button, GdkEventButton *event);
67 static void _button_pressed(GtkWidget *button, GdkEventButton *event, void *self);
68 static void _run_dialog(GtkMenuItem *, void *);
71 osso_context_t *myContext;
75 LauncherItems myItems;
77 static char *ourDirs[];
80 // Hildon home applet interface functions
82 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
83 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
86 if (applet->doInit(state_data, state_size)) {
87 *widget = applet->getWidget();
97 void hildon_home_applet_lib_deinitialize(void *applet_data) {
98 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
103 void hildon_home_applet_lib_background(void *applet_data) {
104 ((SimpleLauncherApplet *)applet_data)->background();
107 void hildon_home_applet_lib_foreground (void *applet_data) {
108 ((SimpleLauncherApplet *)applet_data)->foreground();
111 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
112 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
115 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
116 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
119 // SimpleLauncherApplet implementation
121 char *SimpleLauncherApplet::ourDirs[] = {
122 "/usr/share/applications/hildon",
126 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(NULL), myWidget(NULL), myParent(NULL) {
129 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
130 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
131 g_debug("sla-applet: failed to initialize the osso layer");
144 SimpleLauncherApplet::~SimpleLauncherApplet() {
147 if (myWidget != NULL) {
148 gtk_widget_destroy(myWidget);
152 if (myContext != NULL) {
153 osso_deinitialize(myContext);
158 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
159 if (!items.exists(name)) {
160 LaunchableItem *item = new LaunchableItem();
170 items.add(name, item);
174 // FIXME: this probably should be done somehow differently
175 static char *configFileName="/home/user/.slarc";
177 void SimpleLauncherApplet::loadConfig() {
178 std::ifstream config(configFileName);
181 char *buffer = new char [1024];
183 while (config.getline(buffer, 1024)) {
184 char *p = strchr(buffer, ',');
190 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
197 void SimpleLauncherApplet::saveConfig() {
198 // TODO: make saving config an atomic operation
199 std::ofstream config(configFileName);
202 for (size_t i = 0 ; i < myItems.size() ; ++i) {
203 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
208 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
209 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
210 processDirectory(items, ourDirs[i]);
214 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
215 DIR *dir = opendir(dirname.c_str());
218 const std::string namePrefix = dirname + "/";
219 std::string shortName;
220 std::string desktopExtension = ".desktop";
223 while ((file = readdir(dir)) != 0) {
224 shortName = file->d_name;
225 if ((shortName == ".") || (shortName == "..")) {
229 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
230 addItem(items, namePrefix+shortName, false);
238 bool SimpleLauncherApplet::initWidget() {
239 myWidget = gtk_hbox_new(false, 0);
241 if (myWidget != NULL) {
245 return myWidget != NULL;
248 void SimpleLauncherApplet::updateWidget() {
249 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
251 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
255 for (size_t i = 0 ; i < myItems.size() ; ++i) {
256 LauncherItem *item = myItems[i];
258 if (item != NULL && item->isEnabled()) {
259 GtkWidget *button = gtk_event_box_new();
261 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
262 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
264 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), false);
265 // gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);
267 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)));
269 gtk_object_set_user_data(GTK_OBJECT(button), item);
271 gtk_size_group_add_widget(group, button);
273 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
279 g_object_unref(G_OBJECT(group));
281 if (button_no == 0) {
282 gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE, SL_APPLET_ICON_SIZE);
284 gtk_widget_set_size_request(myWidget, button_no*SL_APPLET_ICON_SIZE, SL_APPLET_ICON_SIZE);
287 gtk_widget_show_all(myWidget);
290 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
291 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
294 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
295 if (button != NULL && event->button == 1) {
296 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
299 item->activate(myContext);
304 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
305 if (state_data != NULL) {
309 if (state_size != NULL) {
316 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
317 myParent = parent; // FIXME: Ugly piece of code :(
319 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
321 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
326 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
327 ((SimpleLauncherApplet *)self)->runDialog();
330 void SimpleLauncherApplet::runDialog() {
331 // We update the items before using them to avoid a small memory leak
332 // FIXME: deal with the situation in a better way (figure it out first :))
333 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
335 LauncherItems newItems = myItems;
337 SettingsDialog dialog(myParent, SL_APPLET_ICON_SIZE, newItems);
339 switch (dialog.run()) {
340 case GTK_RESPONSE_OK:
342 saveConfig(); // save it immediately!
346 case GTK_RESPONSE_CANCEL:
350 ; // FIXME: do I want to do anything in here?