Refactor part of ModestWindow and ModestHildon2Window
[modest] / src / hildon2 / 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 <glib/gstdio.h>
38 #include <errno.h>
39 #include <string.h> /* for strlen */
40 #include <modest-runtime.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <tny-fs-stream.h>
43 #include <tny-camel-account.h>
44 #include <tny-status.h>
45 #include <tny-camel-transport-account.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
48 #include "modest-hildon-includes.h"
49
50 #include <modest-defs.h>
51 #include "modest-maemo-utils.h"
52 #include "modest-text-utils.h"
53 #include "modest-platform.h"
54 #include "modest-ui-constants.h"
55 #include <hildon/hildon-picker-dialog.h>
56 #ifdef MODEST_USE_IPHB
57 #include <iphbd/libiphb.h>
58 #endif
59
60 /* Label child of a captioned */
61 #define CAPTIONED_LABEL_CHILD "captioned-label"
62
63 #ifdef MODEST_PLATFORM_MAEMO
64 #define INTERNAL_MMC_USB_MODE  "/system/osso/af/internal-mmc-used-over-usb"
65 #endif
66
67 static osso_context_t *__osso_context = NULL; /* urgh global */
68
69 osso_context_t *
70 modest_maemo_utils_get_osso_context (void)
71 {
72         if (!__osso_context) 
73                 __osso_context = osso_initialize(PACKAGE,PACKAGE_VERSION,
74                                                  FALSE, NULL);
75
76         return __osso_context;
77 }
78
79 static void
80 get_properties_cb (DBusMessage *message)
81 {
82         DBusMessageIter iter;
83         DBusMessageIter dict_iter;
84         DBusMessageIter dict_entry_iter;
85         DBusError err;
86         gchar *bt_name = NULL;
87         int key_type, array_type, msg_type;
88
89         dbus_error_init(&err);
90         if (dbus_set_error_from_message (&err, message)) {
91                 g_warning ("%s: %s", __FUNCTION__, err.message);
92         }
93
94         /* Get msg type */
95         dbus_message_iter_init (message, &iter);
96         msg_type = dbus_message_iter_get_arg_type (&iter);
97         dbus_message_iter_recurse (&iter, &dict_iter);
98
99         while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY) {
100
101                 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
102
103                 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING) {
104                         DBusMessageIter dict_entry_content_iter;
105                         char *key;
106                         char *value;
107                         int dict_entry_type;
108                         int dict_entry_content_type;
109
110                         dbus_message_iter_get_basic (&dict_entry_iter, &key);
111                         dbus_message_iter_next (&dict_entry_iter);
112                         dict_entry_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
113                         dbus_message_iter_recurse (&dict_entry_iter, &dict_entry_content_iter);
114                         dict_entry_content_type = dbus_message_iter_get_arg_type (&dict_entry_content_iter);
115
116                         if (dict_entry_content_type == DBUS_TYPE_STRING) {
117                                 dbus_message_iter_get_basic ( &dict_entry_content_iter, &value );
118
119                                 if (strcmp (key, "Name") == 0 ) {
120                                         g_debug ("-------------Name %s", value);
121                                         bt_name = value;
122                                         break;
123                                 }
124                         }
125                         dbus_message_iter_next (&dict_entry_iter);
126                 }
127
128                 if (key_type != DBUS_TYPE_INVALID)
129                         break;
130
131                 dbus_message_iter_next (&dict_iter);
132         }
133
134         /* Save device name */
135         if (bt_name) {
136                 g_debug ("Setting the device name %s", bt_name);
137                 modest_conf_set_string (modest_runtime_get_conf(),
138                                         MODEST_CONF_DEVICE_NAME, bt_name,
139                                         NULL);
140         }
141 }
142
143 static void
144 get_default_adapter_cb (DBusConnection *conn,
145                         DBusMessage *message)
146 {
147         DBusMessageIter iter;
148         gchar* path = NULL;
149         DBusError error;
150         DBusMessage *msg, *adapterMsg;
151
152         dbus_message_iter_init (message, &iter);
153         dbus_message_iter_get_basic (&iter, &path);
154
155         if (!path)
156                 return;
157
158         adapterMsg = dbus_message_new_method_call ("org.bluez", path,
159                                                    "org.bluez.Adapter",
160                                                    "GetProperties");
161
162         dbus_error_init (&error);
163         msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error);
164         if (msg) {
165                 g_debug ("Getting the properties");
166                 get_properties_cb (msg);
167                 dbus_message_unref (msg);
168         }
169         dbus_message_unref (adapterMsg);
170 }
171
172 void
173 modest_maemo_utils_get_device_name (void)
174 {
175         static DBusConnection *conn = NULL;
176         DBusMessage *request;
177         DBusError error;
178         DBusMessage *msg;
179
180         dbus_error_init (&error);
181         if (!conn) {
182                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
183                 if (!conn) {
184                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
185                                     error.name, error.message);
186                         dbus_error_free (&error);
187                         return;
188                 }
189         }
190
191         /* Get the default adapter */
192         request = dbus_message_new_method_call ("org.bluez", "/" ,
193                                                 "org.bluez.Manager",
194                                                 "DefaultAdapter");
195
196         msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error);
197         if (msg) {
198                 g_debug ("Getting the default adapter");
199                 get_default_adapter_cb (conn, msg);
200                 dbus_message_unref (msg);
201         }
202
203         dbus_message_unref (request);
204         if (dbus_error_is_set (&error))
205                 dbus_error_free (&error);
206 }
207
208 void
209 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
210 {
211         GtkFileFilter *file_filter;
212         GList *image_mimetypes_list;
213         GList *node;
214         gchar *conf_folder;
215
216         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
217
218         conf_folder = modest_conf_get_string (modest_runtime_get_conf (),
219                                               MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
220         if (conf_folder && conf_folder[0] != '\0') {
221                 gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
222         } else {
223                 gchar *images_folder;
224                 /* Set the default folder to images folder */
225                 images_folder = (gchar *) g_strdup(g_get_user_special_dir (G_USER_DIRECTORY_PICTURES));
226                 if (!images_folder) {
227                         /* fallback */
228                         images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
229                                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
230                 }
231                 gtk_file_chooser_set_current_folder (chooser, images_folder);
232                 g_free (images_folder);
233         }
234         g_free (conf_folder);
235
236         /* Set the images mime filter */
237         file_filter = gtk_file_filter_new ();
238         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
239         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
240                 gtk_file_filter_add_mime_type (file_filter, node->data);
241         }
242         gtk_file_chooser_set_filter (chooser, file_filter);
243         hildon_mime_types_list_free (image_mimetypes_list);
244
245 }
246
247 #ifdef MODEST_PLATFORM_MAEMO
248 gboolean
249 modest_maemo_utils_in_usb_mode ()
250 {
251         return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL);
252 }
253 #endif
254
255 #ifdef MODEST_USE_IPHB
256
257 typedef struct _ModestHeartbeatSource {
258         GSource source;
259         iphb_t iphb;
260         GPollFD poll;
261         gint interval;
262 } ModestHeartbeatSource;
263
264 static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout)
265 {
266     *timeout = -1;
267     return FALSE;
268 }
269
270 static gboolean 
271 modest_heartbeat_check(GSource* source)
272 {
273         return ((ModestHeartbeatSource *) source)->poll.revents != 0;
274 }
275
276 static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata)
277 {
278     if (callback(userdata))
279     {
280             ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source;
281
282             g_source_remove_poll (source, &(hb_source->poll));
283
284             int min = MAX(hb_source->interval - 30, 5);
285             iphb_wait(hb_source->iphb, min, min + 60, 0);
286
287             hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
288             hb_source->poll.events = G_IO_IN;
289             hb_source->poll.revents = 0;
290
291             g_source_add_poll(source, &(hb_source->poll));
292
293             return TRUE;
294     } else {
295             return FALSE;
296     }
297 }
298
299 static void 
300 modest_heartbeat_finalize (GSource* source)
301 {
302         ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source;
303         hb_source->iphb = iphb_close(hb_source->iphb);
304 }
305
306 GSourceFuncs modest_heartbeat_funcs =
307 {
308   modest_heartbeat_prepare,
309   modest_heartbeat_check,
310   modest_heartbeat_dispatch,
311   modest_heartbeat_finalize,
312   NULL,
313   NULL
314 };
315
316 static GSource *
317 modest_heartbeat_source_new (void)
318 {
319         GSource *source;
320         ModestHeartbeatSource *hb_source;
321         iphb_t iphb;
322         int hb_interval;
323
324         source = NULL;
325         hb_interval = 0;
326
327         iphb = iphb_open (&hb_interval);
328
329         if (iphb != 0) {
330                 int min;
331                 source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource));
332                 hb_source = (ModestHeartbeatSource *) source;
333                 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
334                 hb_source->iphb = iphb;
335                 hb_source->interval = hb_interval;
336
337                 min = MAX(hb_interval - 30, 5);
338                 iphb_wait(hb_source->iphb, min, min + 60, 0);
339
340                 hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
341                 hb_source->poll.events = G_IO_IN;
342                 hb_source->poll.revents = 0;
343
344                 g_source_add_poll(source, &(hb_source->poll));
345         } else {
346                 source = g_idle_source_new ();
347         }
348
349         return source;
350 }
351
352 guint
353 modest_heartbeat_add (GSourceFunc function,
354                       gpointer userdata)
355 {
356         GSource *source;
357         guint id;
358
359         source = modest_heartbeat_source_new ();
360         g_source_set_callback (source, function, userdata, NULL);
361         id = g_source_attach (source, NULL);
362         g_source_unref (source);
363
364         return id;
365 }
366
367 #else
368 guint
369 modest_heartbeat_add (GSourceFunc function,
370                       gpointer userdata)
371 {
372         return g_idle_add (function, userdata);
373 }
374 #endif