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 // SimpleLauncherApplet implementation
33 char *SimpleLauncherApplet::ourDirs[] = {
34 "/usr/share/applications/hildon",
38 SimpleLauncherApplet::SimpleLauncherApplet(const std::string& base) : myContext(NULL), myWidget(NULL), myParent(NULL), myValidItems(0), myTransparent(GConfKey(base), "transparent", false), myIconSize(GConfKey(base), "icon_size", 48) {
41 bool SimpleLauncherApplet::doInit() {
42 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, NULL)) == NULL) {
43 g_debug("sla-applet: failed to initialize the osso layer");
56 SimpleLauncherApplet::~SimpleLauncherApplet() {
59 // This does not seem to be necessary
60 if (myWidget != NULL) {
61 gtk_widget_destroy(myWidget);
65 if (myContext != NULL) {
66 osso_deinitialize(myContext);
71 void SimpleLauncherApplet::addItem(LauncherItems& items, const std::string& name, bool enabled) {
72 if (!items.exists(name)) {
73 LaunchableItem *item = new LaunchableItem();
83 items.add(name, item);
87 // {{{ Configuration file managment
88 static const gchar *getConfigFileName() {
89 static gchar *configFileName = NULL;
91 if (configFileName == NULL) {
92 configFileName = g_build_filename(g_get_home_dir(), ".slarc", NULL);
95 return configFileName;
98 void SimpleLauncherApplet::loadConfig() {
99 std::ifstream config(getConfigFileName());
102 char *buffer = new char [1024];
104 while (config.getline(buffer, 1024)) {
105 char *p = strchr(buffer, ',');
111 addItem(myItems, buffer, (p != NULL && (*p == '1' || *p == 'y' || *p == 'Y')));
118 void SimpleLauncherApplet::saveConfig() {
119 // TODO: make saving config an atomic operation
120 std::ofstream config(getConfigFileName());
123 for (size_t i = 0 ; i < myItems.size() ; ++i) {
124 config << myItems.name(i) << ',' << myItems[i]->isEnabled() << std::endl;
131 void SimpleLauncherApplet::updateItems(LauncherItems& items) {
132 for (int i = 0 ; ourDirs[i] != NULL ; ++i) {
133 processDirectory(items, ourDirs[i]);
137 void SimpleLauncherApplet::processDirectory(LauncherItems& items, const std::string& dirname) {
138 DIR *dir = opendir(dirname.c_str());
141 const std::string namePrefix = dirname + "/";
142 std::string shortName;
143 std::string desktopExtension = ".desktop";
146 while ((file = readdir(dir)) != 0) {
147 shortName = file->d_name;
148 if ((shortName == ".") || (shortName == "..")) {
152 if ((shortName.length() >= desktopExtension.length()) && (shortName.compare(shortName.length() - desktopExtension.length(), desktopExtension.length(), desktopExtension) == 0)) {
153 addItem(items, namePrefix+shortName, false);
161 bool SimpleLauncherApplet::initWidget() {
162 myWidget = gtk_hbox_new(false, 0);
164 if (myWidget != NULL) {
168 return myWidget != NULL;
171 void SimpleLauncherApplet::updateWidget() {
172 gtk_container_foreach(GTK_CONTAINER(myWidget), (GtkCallback)gtk_widget_destroy, NULL);
174 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
178 for (size_t i = 0 ; i < myItems.size() ; ++i) {
179 LauncherItem *item = myItems[i];
181 if (item != NULL && item->isEnabled()) {
182 GtkWidget *button = gtk_event_box_new();
184 gtk_widget_set_events(button, GDK_BUTTON_PRESS_MASK);
185 g_signal_connect(button, "button-press-event", G_CALLBACK(_button_pressed), this);
187 gtk_event_box_set_visible_window(GTK_EVENT_BOX(button), !myTransparent.value());
190 GdkPixbuf *pixbuf = item->getIcon(myIconSize.value());
191 gtk_container_add(GTK_CONTAINER(button), gtk_image_new_from_pixbuf(pixbuf));
192 g_object_unref(G_OBJECT(pixbuf));
195 gtk_object_set_user_data(GTK_OBJECT(button), item);
197 gtk_size_group_add_widget(group, button);
199 gtk_box_pack_start(GTK_BOX(myWidget), GTK_WIDGET(button), false, false, 0);
205 g_object_unref(G_OBJECT(group));
207 gtk_widget_set_size_request(myWidget, getWidth(), getHeight());
209 gtk_widget_show_all(myWidget);
212 void SimpleLauncherApplet::_button_pressed(GtkWidget *button, GdkEventButton *event, void *self) {
213 ((SimpleLauncherApplet *)self)->buttonPressed(button, event);
216 void SimpleLauncherApplet::buttonPressed(GtkWidget *button, GdkEventButton *event) {
217 if (button != NULL && event->button == 1) {
218 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
221 item->activate(myContext);
226 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
227 myParent = parent; // FIXME: Ugly piece of code :(
229 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
231 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
236 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
237 ((SimpleLauncherApplet *)self)->runDialog();
240 void SimpleLauncherApplet::runDialog() {
241 // We update the items before using them to avoid a small memory leak
242 // FIXME: deal with the situation in a better way (figure it out first :))
243 updateItems(myItems); // User requested 'settings', let's give her the latest stuff :)
245 LauncherItems newItems = myItems;
247 // TODO: make it nicer... this code is ugly :(
248 SettingsDialog dialog(myParent, newItems, myTransparent, myIconSize);
250 switch (dialog.run()) {
251 case GTK_RESPONSE_OK:
253 dialog.updateValues(); // FIXME: hackish :( make it better
255 saveConfig(); // save it immediately!
259 case GTK_RESPONSE_CANCEL:
263 ; // FIXME: do I want to do anything in here?
266 // newItems.clear(); // TODO: do I really need it?
269 int SimpleLauncherApplet::getWidth() const {
271 return myIconSize.value() * myValidItems;
273 return myIconSize.value();
277 int SimpleLauncherApplet::getHeight() const {
278 return myIconSize.value();
281 void SimpleLauncherApplet::getBackgroundColour(double& red, double& green, double& blue, double alpha) const {
283 red = 1.0; green = 1.0; blue = 1.0;
285 alpha = myTransparent.value() ? 0.0 : 1.0;
288 // vim:ts=2:sw=2:et:foldmethod=marker