Refactor mcc parsing code
[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 <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
55 /*
56  * For getting and tracking the Bluetooth name
57  */
58 #define BTNAME_SERVICE                  "org.bluez"
59 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
60 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
61 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
62 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
63
64 #define BTNAME_REQ_GET                  "GetName"
65 #define BTNAME_SIG_CHANGED              "NameChanged"
66
67 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
68                           "',member='" BTNAME_SIG_CHANGED "'"
69
70
71 static osso_context_t *__osso_context = NULL; /* urgh global */
72
73 osso_context_t *
74 modest_maemo_utils_get_osso_context (void)
75 {
76         if (!__osso_context) 
77                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
78         
79         return __osso_context;
80 }
81
82 void
83 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
84 {
85         g_return_if_fail (osso_context);
86         __osso_context = osso_context;
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 bluetooth name: %s\n", error.message);
99                 dbus_error_free (&error);
100         } else {
101                 const gchar *device_name;
102                 if (!dbus_message_iter_init (message, &iter)) {
103                         g_printerr ("modest: message did not have argument\n");
104                         return;
105                 }
106                 dbus_message_iter_get_basic (&iter, &device_name);
107                 modest_conf_set_string (modest_runtime_get_conf(),
108                                         MODEST_CONF_DEVICE_NAME, device_name,
109                                         NULL);
110         }
111 }
112
113
114 static void
115 on_device_name_received (DBusPendingCall *call, void *user_data)
116 {
117         DBusMessage *message;
118         
119         g_return_if_fail (dbus_pending_call_get_completed (call));
120         
121         message = dbus_pending_call_steal_reply (call);
122         if (!message) {
123                 g_printerr ("modest: no reply on device name query\n");
124                 return;
125         }
126
127         update_device_name_from_msg (message);
128         dbus_message_unref (message);
129 }
130
131
132 static DBusHandlerResult
133 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
134 {
135         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
136                 update_device_name_from_msg (msg);
137
138         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
139 }
140
141
142 static void
143 get_device_name_from_dbus ()
144 {
145         static DBusConnection *conn = NULL;
146         DBusMessage *request;
147         DBusError error;
148         DBusPendingCall *call = NULL;
149         
150         dbus_error_init (&error);
151         if (!conn) {
152                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
153                 if (!conn) {
154                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
155                                     error.name, error.message);
156                         dbus_error_free (&error);
157                         return;
158                 }
159         }
160         
161         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
162                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
163         if (!request) {
164                 /* should we free the connection? */
165                 g_printerr ("modest: dbus_message_new_method_call failed\n");
166                 return;
167         }
168         dbus_message_set_auto_start (request, TRUE);
169         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
170                 dbus_pending_call_set_notify (call, on_device_name_received,
171                                               NULL, NULL);
172                 dbus_pending_call_unref (call);
173         }
174         dbus_message_unref (request);
175         
176         dbus_connection_setup_with_g_main (conn, NULL);
177         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
178         if (dbus_error_is_set(&error)) {
179                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
180                 dbus_error_free (&error);
181         }
182
183         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
184                 g_printerr ("modest: dbus_connection_add_filter failed\n");
185 }
186
187
188 void
189 modest_maemo_utils_get_device_name (void)
190 {
191         get_device_name_from_dbus ();
192 }
193
194 void
195 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
196 {
197         gchar *images_folder;
198         GtkFileFilter *file_filter;
199         GList *image_mimetypes_list;
200         GList *node;
201
202         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
203
204         /* Set the default folder to images folder */
205         images_folder = g_build_filename (g_get_home_dir (), 
206                                           MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
207                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
208         gtk_file_chooser_set_current_folder (chooser, images_folder);
209         g_free (images_folder);
210
211         /* Set the images mime filter */
212         file_filter = gtk_file_filter_new ();
213 #ifdef MODEST_HAVE_HILDON0_WIDGETS
214         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
215 #else
216         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
217 #endif
218         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
219                 gtk_file_filter_add_mime_type (file_filter, node->data);
220         }
221         gtk_file_chooser_set_filter (chooser, file_filter);
222 #ifdef MODEST_HAVE_HILDON0_WIDGETS
223         osso_mime_types_list_free (image_mimetypes_list);
224 #else
225         hildon_mime_types_list_free (image_mimetypes_list);
226 #endif
227
228 }
229
230 void
231 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
232                                       gboolean thumbable)
233 {
234         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
235 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
236         hildon_helper_set_thumb_scrollbar (win, thumbable);
237 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
238 }
239
240 GtkWidget *
241 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
242                                                 const gchar *item_name)
243 {
244         GtkWidget *new_menu;
245         GtkWidget *menubar;
246         GList *children, *iter;
247
248         menubar = gtk_ui_manager_get_widget (manager, item_name);
249         new_menu = gtk_menu_new ();
250
251         children = gtk_container_get_children (GTK_CONTAINER (menubar));
252         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
253                 GtkWidget *menu;
254
255                 menu = GTK_WIDGET (iter->data);
256                 gtk_widget_reparent (menu, new_menu);
257         }
258         
259         g_list_free (children);
260
261         return new_menu;
262 }