1 // This file is a part of Simple Launcher
3 // Copyright (C) 2006, 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
23 #include <hildon-home-plugin/hildon-home-plugin-interface.h>
26 #include "launcher-item.h"
29 #define SL_APPLET_DBUS_NAME "simple-launcher"
30 #define SL_APPLET_VERSION "0.0"
31 #define SL_APPLET_ICON_SIZE 26
32 #define SL_APPLET_BORDER_SIZE 14
33 #define SL_APPLET_CANVAS_SIZE (SL_APPLET_BORDER_SIZE+SL_APPLET_BORDER_SIZE)
35 class LaunchableItem {
37 LaunchableItem(LauncherItem *, bool);
40 GdkPixbuf *getIcon(int icon_size) const { return myItem->getIcon(icon_size); }
42 const std::string& getName() const { return myItem->getName(); }
43 const std::string& getComment() const { return myItem->getComment(); }
44 const std::string& getService() const { return myItem->getService(); }
46 bool isEnabled(void) const { return myEnabled; }
48 void enable() { myEnabled = true; }
49 void disable() { myEnabled = false; }
51 bool activate(osso_context_t *);
58 class SimpleLauncherApplet {
60 SimpleLauncherApplet();
61 ~SimpleLauncherApplet();
63 bool doInit(void *state_data, int *state_size);
67 int saveState(void **state_data, int *state_size);
68 GtkWidget *settings(GtkWindow *parent);
70 GtkWidget *getWidget() { return myWidget; }
75 void buttonClicked(GtkToolButton *);
78 static void _button_clicked(GtkToolButton *, void *);
79 static void _run_dialog(GtkMenuItem *, void *);
82 osso_context_t *myContext;
86 typedef std::vector<std::pair<std::string, LaunchableItem *> > ItemList;
89 static char *ourFiles[];
92 // Hildon home applet interface functions
94 void *hildon_home_applet_lib_initialize(void *state_data, int *state_size, GtkWidget **widget) {
95 SimpleLauncherApplet *applet = new SimpleLauncherApplet();
98 if (applet->doInit(state_data, state_size)) {
99 *widget = applet->getWidget();
106 return (void*)applet;
109 void hildon_home_applet_lib_deinitialize(void *applet_data) {
110 SimpleLauncherApplet *applet = (SimpleLauncherApplet *)applet_data;
115 void hildon_home_applet_lib_background(void *applet_data) {
116 ((SimpleLauncherApplet *)applet_data)->background();
119 void hildon_home_applet_lib_foreground (void *applet_data) {
120 ((SimpleLauncherApplet *)applet_data)->foreground();
123 GtkWidget *hildon_home_applet_lib_settings(void *applet_data, GtkWindow *parent) {
124 return ((SimpleLauncherApplet *)applet_data)->settings(parent);
127 int hildon_home_applet_lib_save_state (void *applet_data, void **state_data, int *state_size) {
128 return ((SimpleLauncherApplet *)applet_data)->saveState(state_data, state_size);
131 // SimpleLauncherApplet implementation
133 char *SimpleLauncherApplet::ourFiles[] = {
134 "/usr/share/applications/hildon/FBReader.desktop",
135 "/usr/share/applications/hildon/mp_ui.desktop",
136 "/usr/share/applications/hildon/osso-xterm.desktop",
137 "/usr/share/applications/hildon/filemanager.desktop",
138 "/usr/share/applications/hildon/osso-application-installer.desktop",
139 "/usr/share/applications/hildon/hildon-control-panel.desktop",
143 SimpleLauncherApplet::SimpleLauncherApplet(): myContext(0), myWidget(0), myParent(0) {
146 bool SimpleLauncherApplet::doInit(void *state_data, int *state_size) {
147 if ((myContext = osso_initialize(SL_APPLET_DBUS_NAME, SL_APPLET_VERSION, FALSE, 0)) == 0) {
148 g_debug("sla-applet: failed to initialize the osso layer");
152 for (int i = 0 ; ourFiles[i] != 0 ; ++i) {
153 LauncherItem *item = new LauncherItem();
155 if (item->load(ourFiles[i])) {
156 myItems.push_back(std::pair<std::string, LaunchableItem *>(ourFiles[i], new LaunchableItem(item, true)));
166 gtk_widget_show_all(myWidget);
171 SimpleLauncherApplet::~SimpleLauncherApplet() {
172 for (ItemList::iterator it = myItems.begin(); it != myItems.end(); ++it) {
173 if (it->second != 0) {
182 gtk_widget_destroy(myWidget);
186 if (myContext != 0) {
187 osso_deinitialize(myContext);
192 bool SimpleLauncherApplet::initWidget() {
195 GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
197 for (ItemList::const_iterator it = myItems.begin(); it != myItems.end(); ++it) {
198 GtkToolItem *button = gtk_tool_button_new(gtk_image_new_from_pixbuf(it->second->getIcon(SL_APPLET_ICON_SIZE)), 0);
200 gtk_object_set_user_data(GTK_OBJECT(button), it->second);
201 g_signal_connect(button, "clicked", G_CALLBACK(_button_clicked), this);
203 gtk_toolbar_insert(toolbar, button, -1);
209 myWidget = gtk_frame_new(0);
210 gtk_frame_set_shadow_type(GTK_FRAME(myWidget), GTK_SHADOW_ETCHED_IN);
211 gtk_widget_set_size_request(myWidget, button_no*(SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE), SL_APPLET_ICON_SIZE+SL_APPLET_CANVAS_SIZE);
212 gtk_container_add(GTK_CONTAINER(myWidget), GTK_WIDGET(toolbar));
214 gtk_widget_destroy(GTK_WIDGET(toolbar));
217 return myWidget != 0;
220 void SimpleLauncherApplet::_button_clicked(GtkToolButton *button, void *self) {
221 ((SimpleLauncherApplet *)self)->buttonClicked(button);
224 void SimpleLauncherApplet::buttonClicked(GtkToolButton *button) {
226 LaunchableItem *item = (LaunchableItem *)gtk_object_get_user_data(GTK_OBJECT(button));
229 item->activate(myContext);
234 int SimpleLauncherApplet::saveState(void **state_data, int *state_size) {
235 if (state_data != 0) {
239 if (state_size != 0) {
246 GtkWidget *SimpleLauncherApplet::settings(GtkWindow *parent) {
247 // TODO: in case we want SimpleLauncherApplet to be configurable, this method
248 // should return a gtk_menu_item that would be included in home settings
249 // menu. Method should make sure that when we activate that item, a
250 // corresponding dialog appears.
251 myParent = parent; // FIXME: Ugly piece of code :(
253 GtkWidget *menuItem = gtk_menu_item_new_with_label("Launcher settings...");
255 g_signal_connect(menuItem, "activate", G_CALLBACK(_run_dialog), this);
260 void SimpleLauncherApplet::_run_dialog(GtkMenuItem *, void *self) {
261 ((SimpleLauncherApplet *)self)->runDialog();
264 void SimpleLauncherApplet::runDialog() {
265 SLAList list(SL_APPLET_ICON_SIZE);
267 for (ItemList::const_iterator item = myItems.begin(); item != myItems.end(); ++item) {
268 list.addItem(item->first.c_str(), item->second->getIcon(SL_APPLET_ICON_SIZE), item->second->getComment().c_str(), item->second->isEnabled());
271 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, 0));
273 gtk_container_add(GTK_CONTAINER(dialog->vbox), list.getWidget());
275 gtk_widget_set_size_request(GTK_WIDGET(dialog), 540, 257);
277 int response = gtk_dialog_run(dialog);
279 gtk_widget_destroy(GTK_WIDGET(dialog));
282 case GTK_RESPONSE_OK:
285 case GTK_RESPONSE_CANCEL:
289 ; // FIXME: do I want to do anything in here?
293 LaunchableItem::LaunchableItem(LauncherItem *item, bool enabled): myItem(item), myEnabled(enabled) {
296 LaunchableItem::~LaunchableItem() {
302 bool LaunchableItem::activate(osso_context_t *context) {
303 return osso_application_top(context, myItem->getService().c_str(), 0) == OSSO_OK;