9aae77c5e856bc35a984049a91801781cb3f3143
[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 #include <libgnomevfs/gnome-vfs.h>
39 #include <tny-fs-stream.h>
40
41 #include "modest-maemo-utils.h"
42
43 /*
44  * For getting and tracking the Bluetooth name
45  */
46 #define BTNAME_SERVICE                  "org.bluez"
47 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
48 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
49 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
50 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
51
52 #define BTNAME_REQ_GET                  "GetName"
53 #define BTNAME_SIG_CHANGED              "NameChanged"
54
55 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
56                           "',member='" BTNAME_SIG_CHANGED "'"
57
58
59 GtkWidget*
60 modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager)
61 {
62         GtkWidget *main_menu;
63         GtkWidget *menubar;
64         GList *iter;
65
66         g_return_val_if_fail (ui_manager, NULL);
67         
68         /* Create new main menu */
69         main_menu = gtk_menu_new();
70
71         /* Get the menubar from the UI manager */
72         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
73
74         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
75         while (iter) {
76                 GtkWidget *menu;
77
78                 menu = GTK_WIDGET (iter->data);
79                 gtk_widget_reparent(menu, main_menu);
80
81                 iter = g_list_next (iter);
82         }
83         return main_menu;
84 }
85
86
87 static void
88 update_device_name_from_msg (DBusMessage *message)
89 {
90         DBusError error;
91         DBusMessageIter iter;
92
93         dbus_error_init (&error);
94
95         if (dbus_set_error_from_message (&error, message)) {
96                 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
97                 dbus_error_free (&error);
98         } else {
99                 const gchar *device_name;
100                 if (!dbus_message_iter_init (message, &iter)) {
101                         g_printerr ("modest: message did not have argument\n");
102                         return;
103                 }
104                 dbus_message_iter_get_basic (&iter, &device_name);
105                 g_warning ("update device name: %s", device_name);
106                 modest_conf_set_string (modest_runtime_get_conf(),
107                                         MODEST_CONF_DEVICE_NAME, device_name,
108                                         NULL);
109         }
110 }
111
112
113 static void
114 on_device_name_received (DBusPendingCall *call, void *user_data)
115 {
116         DBusMessage *message;
117         
118         g_return_if_fail (dbus_pending_call_get_completed (call));
119         
120         message = dbus_pending_call_steal_reply (call);
121         if (!message) {
122                 g_printerr ("modest: no reply on device name query\n");
123                 return;
124         }
125
126         update_device_name_from_msg (message);
127         dbus_message_unref (message);
128 }
129
130
131 static DBusHandlerResult
132 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
133 {
134         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
135                 update_device_name_from_msg (msg);
136
137         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
138 }
139
140
141 static void
142 get_device_name_from_dbus ()
143 {
144         static DBusConnection *conn = NULL;
145         DBusMessage *request;
146         DBusError error;
147         DBusPendingCall *call = NULL;
148         
149         dbus_error_init (&error);
150         if (!conn) {
151                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
152                 if (!conn) {
153                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
154                                     error.name, error.message);
155                         dbus_error_free (&error);
156                         return;
157                 }
158         }
159         
160         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
161                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
162         if (!request) {
163                 /* should we free the connection? */
164                 g_printerr ("modest: dbus_message_new_method_call failed\n");
165                 return;
166         }
167         dbus_message_set_auto_start (request, TRUE);
168         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
169                 dbus_pending_call_set_notify (call, on_device_name_received,
170                                               NULL, NULL);
171                 dbus_pending_call_unref (call);
172         }
173         dbus_message_unref (request);
174         
175         dbus_connection_setup_with_g_main (conn, NULL);
176         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
177         if (dbus_error_is_set(&error)) {
178                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
179                 dbus_error_free (&error);
180         }
181
182         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
183                 g_printerr ("modest: dbus_connection_add_filter failed\n");
184 }
185
186
187 void
188 modest_maemo_utils_get_device_name (void)
189 {
190         get_device_name_from_dbus ();
191 }
192
193 gboolean 
194 modest_maemo_utils_folder_writable (const gchar *filename)
195 {
196         if (g_strncasecmp (filename, "obex", 4) != 0) {
197                 GnomeVFSFileInfo folder_info;
198                 gchar *folder;
199                 folder = g_path_get_dirname (filename);
200                 gnome_vfs_get_file_info (folder, &folder_info,
201                                          GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
202                 g_free (folder);
203                 if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
204                       (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
205                         return FALSE;
206                 }
207         }
208         return TRUE;
209 }
210
211 gboolean 
212 modest_maemo_utils_file_exists (const gchar *filename)
213 {
214         GnomeVFSURI *uri = NULL;
215         gboolean result = FALSE;
216
217         uri = gnome_vfs_uri_new (filename);
218         if (uri) {
219                 result = gnome_vfs_uri_exists (uri);
220                 gnome_vfs_uri_unref (uri);
221         }
222         return result;
223 }
224
225 TnyFsStream *
226 modest_maemo_utils_create_temp_stream (gchar **path)
227 {
228         TnyStream *tmp_fs_stream;
229         gint fd;
230         gchar *filepath;
231
232         fd = g_file_open_tmp (NULL, &filepath, NULL);
233         if (path != NULL)
234                 *path = filepath;
235         if (fd == -1) {
236                 g_message ("TODO BANNER: Error saving stream");
237                 return NULL;
238         }
239         tmp_fs_stream = tny_fs_stream_new (fd);
240         
241         return TNY_FS_STREAM (tmp_fs_stream);
242 }