// This file is a part of Simple Launcher
//
-// Copyright (C) 2006, Mikhail Sobolev
+// Copyright (C) 2006, 2007, Mikhail Sobolev
//
// Simple Launcher is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 2 as published by
#include <vector>
#include <fstream>
+#include <dirent.h>
+
#include <gtk/gtk.h>
#include <hildon-home-plugin/hildon-home-plugin-interface.h>
GtkWidget *getWidget() { return myWidget; }
private:
+ static void addItem(LauncherItems&, const std::string&, bool);
+
void loadConfig();
void saveConfig();
+ static void updateItems(LauncherItems&);
+ static void processDirectory(LauncherItems&, const std::string&);
+
bool initWidget();
void updateWidget();
return false;
}
- gtk_widget_show_all(myWidget);
-
return true;
}
SimpleLauncherApplet::~SimpleLauncherApplet() {
- for (LauncherItems::iterator it = myItems.begin(); it != myItems.end(); ++it) {
- if (it->second != NULL) {
- delete it->second;
- it->second = NULL;
- }
- }
-
- myItems.resize(0);
+ myItems.clear();
if (myWidget != NULL) {
gtk_widget_destroy(myWidget);
}
}
+void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
+ if (!items.exists(name)) {
+ LaunchableItem *item = new LaunchableItem();
+
+ item->load(name);
+
+ if (enabled) {
+ item->enable();
+ } else {
+ item->disable();
+ }
+
+ items.add(name, item);
+ }
+}
+
static char *configFileName="/home/user/.slarc";
void SimpleLauncherApplet::loadConfig() {
-#if 0
- for (int i = 0 ; ourFiles[i] != NULL ; ++i) {
- LaunchableItem *item = new LaunchableItem();
+ std::ifstream config(configFileName);
+
+ if (config) {
+ char *buffer = new char [1024];
+
+ while (config.getline(buffer, 1024)) {
+ char *p = strchr(buffer, ',');
+
+ if (p != NULL) {
+ *p++ = '\0';
+ }
+
+ addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
- if (item->load(ourFiles[i])) {
- myItems.push_back(std::pair<std::string, LauncherItem *>(ourFiles[i], item));
- } else {
- delete item;
}
+
+ delete buffer;
}
-#endif
}
void SimpleLauncherApplet::saveConfig() {
std::ofstream config(configFileName);
if (config) {
- for (LauncherItems::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
- config << it->first << ',' << it->second->isEnabled() << std::endl;
+ for (size_t i = 0 ; i < myItems.size() ; ++i) {
+ config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
+ }
+ }
+}
+
+void SimpleLauncherApplet::updateItems(LauncherItems& items) {
+ for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
+ processDirectory(items, ourDirs[i]);
+ }
+}
+
+void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
+ DIR *dir = opendir(dirname.c_str());
+
+ if (dir != NULL) {
+ const std::string namePrefix = dirname + "/";
+ std::string shortName;
+ std::string desktopExtension = ".desktop";
+ const dirent *file;
+
+ while ((file = readdir(dir)) != 0) {
+ shortName = file->d_name;
+ if ((shortName == ".") || (shortName == "..")) {
+ continue;
+ }
+
+ if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
+ addItem(items, namePrefix+shortName, false);
+ }
}
+
+ closedir(dir);
}
}
if (myWidget != NULL) {
gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
- gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
updateWidget();
}
int button_no = 0;
GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
- for (LauncherItems::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
- GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(it->second->getIcon(SL_APPLET_ICON_SIZE)), NULL);
+ for (size_t i = 0 ; i < myItems.size() ; ++i) {
+ LauncherItem *item = myItems[i];
- gtk_object_set_user_data(GTK_OBJECT(button), it->second);
- g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
+ if (item != NULL && item->isEnabled()) {
+ GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(item->getIcon(SL_APPLET_ICON_SIZE)), NULL);
- gtk_toolbar_insert(toolbar, button, -1);
+ gtk_object_set_user_data(GTK_OBJECT(button), item);
+ g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
- ++button_no;
+ gtk_toolbar_insert(toolbar, button, -1);
+
+ ++button_no;
+ }
}
if (button_no) {
gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
+ if (button_no == 0) {
+ gtk_widget_set_size_request(myWidget, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE, SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
+ } else {
+ gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
+ }
} else {
gtk_widget_destroy(GTK_WIDGET(toolbar));
}
+
+ gtk_widget_show_all(myWidget);
}
void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
}
void SimpleLauncherApplet::runDialog() {
- SLAList list(SL_APPLET_ICON_SIZE, myItems);
+ LauncherItems newItems = myItems;
+
+ updateItems(newItems); // User requested 'settings', let's give her the latest stuff :)
+
+ SLAList list(SL_APPLET_ICON_SIZE, newItems);
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));
switch (response) {
case GTK_RESPONSE_OK:
+ myItems = newItems;
+ saveConfig(); // save it immediately!
+ updateWidget();
break;
case GTK_RESPONSE_CANCEL: