1 // This file is a part of Simple Launcher
3 // Copyright (C) 2006, 2007, 2008 Mikhail Sobolev <mss@mawhrin.net>
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 "simple-launcher.h"
28 #define SL_APPLET_DBUS_NAME "simple-launcher"
29 #define SL_APPLET_VERSION "0.0"
31 // Hildon home applet interface functions
33 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
34 SimpleLauncherApplet *applet = new SimpleLauncherApplet(SL_APPLET_GCONF_PATH);
37 if (applet->doInit(state_data, state_size)) {
38 *widget = applet->getWidget();
48 void hildon_home_applet_lib_deinitialize(void *applet_data) {
49 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
54 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
55 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
58 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
59 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
62 // SimpleLauncherApplet implementation
64 char *SimpleLauncherApplet::ourDirs[] = {
65 "/usr/share/applications/hildon",
69 // SimpleLauncherApplet::SimpleLauncherApplet() : myMainSettings(myClient.getKey(SL_APPLET_GCONF_PATH)), myContext(NULL), myWidget(NULL), myParent(NULL) {
70 SimpleLauncherApplet::SimpleLauncherApplet(const std::string& base) : myContext(NULL), myWidget(NULL), myParent(NULL), myTransparent(GConfKey(base), "transparent", false), myIconSize(GConfKey(base), "icon_size", 48) {
73 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
74 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
75 g_debug("sla-applet: failed to initialize the osso layer");
88 SimpleLauncherApplet::~SimpleLauncherApplet() {
91 // This does not seem to be necessary
92 if (myWidget != NULL) {
93 gtk_widget_destroy(myWidget);
97 if (myContext != NULL) {
98 osso_deinitialize(myContext);
103 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
104 if (!items.exists(name)) {
105 LaunchableItem *item = new LaunchableItem();
115 items.add(name, item);
119 // {{{ Configuration file managment
120 static const gchar *getConfigFileName() {
121 static gchar *configFileName = NULL;
123 if (configFileName == NULL) {
124 configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
127 return configFileName;
130 void SimpleLauncherApplet::loadConfig() {
131 std::ifstream config(getConfigFileName());
134 char *buffer = new char [1024];
136 while (config.getline(buffer, 1024)) {
137 char *p = strchr(buffer, ',');
143 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
150 void SimpleLauncherApplet::saveConfig() {
151 // TODO: make saving config an atomic operation
152 std::ofstream config(getConfigFileName());
155 for (size_t i = 0 ; i < myItems.size() ; ++i) {
156 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
163 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
164 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
165 processDirectory(items, ourDirs[i]);
169 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
170 DIR *dir = opendir(dirname.c_str());
173 const std::string namePrefix = dirname + "/";
174 std::string shortName;
175 std::string desktopExtension = ".desktop";
178 while ((file = readdir(dir)) != 0) {
179 shortName = file->d_name;
180 if ((shortName == ".") || (shortName == "..")) {
184 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
185 addItem(items, namePrefix+shortName, false);
193 bool SimpleLauncherApplet::initWidget() {
194 myWidget = gtk_hbox_new(false, 0);
196 if (myWidget != NULL) {
200 return myWidget != NULL;
203 void SimpleLauncherApplet::updateWidget() {
204 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
206 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
210 for (size_t i = 0 ; i < myItems.size() ; ++i) {
211 LauncherItem *item = myItems[i];
213 if (item != NULL && item->isEnabled()) {
214 GtkWidget *button = gtk_event_box_new();
216 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
217 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
219 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
222 GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
223 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
224 g_object_unref(G_OBJECT(pixbuf));
227 gtk_object_set_user_data(GTK_OBJECT(button), item);
229 gtk_size_group_add_widget(group, button);
231 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
237 g_object_unref(G_OBJECT(group));
239 int totalSize = myIconSize.value();
241 if (button_no == 0) {
242 gtk_widget_set_size_request(myWidget, totalSize, totalSize);
244 gtk_widget_set_size_request(myWidget, button_no*totalSize, totalSize);
247 gtk_widget_show_all(myWidget);
250 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
251 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
254 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
255 if (button != NULL && event->button == 1) {
256 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
259 item->activate(myContext);
264 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
265 if (state_data != NULL) {
269 if (state_size != NULL) {
276 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
277 myParent = parent; // FIXME: Ugly piece of code :(
279 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
281 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
286 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
287 ((SimpleLauncherApplet *)self)->runDialog();
290 void SimpleLauncherApplet::runDialog() {
291 // We update the items before using them to avoid a small memory leak
292 // FIXME: deal with the situation in a better way (figure it out first :))
293 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
295 LauncherItems newItems = myItems;
297 // TODO: make it nicer... this code is ugly :(
298 SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
300 switch (dialog.run()) {
301 case GTK_RESPONSE_OK:
303 dialog.updateValues(); // FIXME: hackish :( make it better
305 saveConfig(); // save it immediately!
309 case GTK_RESPONSE_CANCEL:
313 ; // FIXME: do I want to do anything in here?
316 // newItems.clear(); // TODO: do I really need it?