* cleanup: moved init stuff to modest-init.[ch]
[modest] / src / maemo / modest-maemo-utils.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
33
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36 #include <glib.h>
37 #include <modest-runtime.h>
38
39 #include "modest-maemo-utils.h"
40
41 /*
42  * For getting and tracking the Bluetooth name
43  */
44 #define BTNAME_SERVICE                  "org.bluez"
45 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
46 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
47 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
48 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
49
50 #define BTNAME_REQ_GET                  "GetName"
51 #define BTNAME_SIG_CHANGED              "NameChanged"
52
53 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
54                           "',member='" BTNAME_SIG_CHANGED "'"
55
56
57 GtkWidget*
58 modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager)
59 {
60         GtkWidget *main_menu;
61         GtkWidget *menubar;
62         GList *iter;
63
64         g_return_val_if_fail (ui_manager, NULL);
65         
66         /* Create new main menu */
67         main_menu = gtk_menu_new();
68
69         /* Get the menubar from the UI manager */
70         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
71
72         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
73         while (iter) {
74                 GtkWidget *menu;
75
76                 menu = GTK_WIDGET (iter->data);
77                 gtk_widget_reparent(menu, main_menu);
78
79                 iter = g_list_next (iter);
80         }
81         return main_menu;
82 }
83
84
85
86
87
88
89 static void
90 update_device_name_from_msg (DBusMessage *message)
91 {
92         DBusError error;
93         DBusMessageIter iter;
94
95         dbus_error_init (&error);
96
97         if (dbus_set_error_from_message (&error, message)) {
98                 g_printerr ("modest: failed to get bt name: %s\n", error.message);
99                 dbus_error_free (&error);
100                 modest_conf_set_string (modest_runtime_get_conf(),
101                                         MODEST_CONF_DEVICE_NAME,
102                                         MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
103                                         NULL);                                  
104         } else {
105                 const gchar *device_name;
106                 if (!dbus_message_iter_init (message, &iter)) {
107                         g_printerr ("modest: message did not have argument\n");
108                         return;
109                 }
110
111                 dbus_message_iter_get_basic (&iter, &device_name);
112                 g_warning ("update device name: %s", device_name);
113                 modest_conf_set_string (modest_runtime_get_conf(),
114                                         MODEST_CONF_DEVICE_NAME, device_name,
115                                         NULL);
116         }
117 }
118
119
120 static void
121 on_device_name_received (DBusPendingCall *call, void *user_data)
122 {
123         DBusMessage *message;
124         
125         g_return_if_fail (dbus_pending_call_get_completed (call));
126         
127         message = dbus_pending_call_steal_reply (call);
128         if (!message) {
129                 g_printerr ("modest: no reply on device name query\n");
130                 return;
131         }
132
133         update_device_name_from_msg (message);
134         dbus_message_unref (message);
135 }
136
137
138 static DBusHandlerResult
139 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
140 {
141         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
142                 update_device_name_from_msg (msg);
143
144         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
145 }
146
147
148 static void
149 get_device_name_from_dbus ()
150 {
151         static DBusConnection *conn = NULL;
152         DBusMessage *request;
153         DBusError error;
154         DBusPendingCall *call = NULL;
155         
156         dbus_error_init (&error);
157         if (!conn) {
158                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
159                 if (!conn) {
160                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
161                                     error.name, error.message);
162                         dbus_error_free (&error);
163                         return;
164                 }
165         }
166         
167         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
168                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
169         if (!request) {
170                 /* should we free the connection? */
171                 g_printerr ("modest: dbus_message_new_method_call failed\n");
172                 return;
173         }
174         dbus_message_set_auto_start (request, TRUE);
175         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
176                 dbus_pending_call_set_notify (call, on_device_name_received,
177                                               NULL, NULL);
178                 dbus_pending_call_unref (call);
179         }
180         dbus_message_unref (request);
181         
182         dbus_connection_setup_with_g_main (conn, NULL);
183         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
184         if (dbus_error_is_set(&error)) {
185                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
186                 dbus_error_free (&error);
187         }
188
189         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
190                 g_printerr ("modest: dbus_connection_add_filter failed\n");
191 }
192
193
194 void
195 modest_maemo_utils_get_device_name (void)
196 {
197         get_device_name_from_dbus ();
198 }
199
200
201