New method modest_maemo_utils_create_caption_with_size_type
[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
56 /*
57  * For getting and tracking the Bluetooth name
58  */
59 #define BTNAME_SERVICE                  "org.bluez"
60 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
61 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
62 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
63 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
64
65 #define BTNAME_REQ_GET                  "GetName"
66 #define BTNAME_SIG_CHANGED              "NameChanged"
67
68 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
69                           "',member='" BTNAME_SIG_CHANGED "'"
70
71
72 static osso_context_t *__osso_context = NULL; /* urgh global */
73
74 osso_context_t *
75 modest_maemo_utils_get_osso_context (void)
76 {
77         if (!__osso_context) 
78                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
79         
80         return __osso_context;
81 }
82
83 void
84 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
85 {
86         g_return_if_fail (osso_context);
87         __osso_context = osso_context;
88 }
89
90 static void
91 update_device_name_from_msg (DBusMessage *message)
92 {
93         DBusError error;
94         DBusMessageIter iter;
95
96         dbus_error_init (&error);
97
98         if (dbus_set_error_from_message (&error, message)) {
99                 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
100                 dbus_error_free (&error);
101         } else {
102                 const gchar *device_name;
103                 if (!dbus_message_iter_init (message, &iter)) {
104                         g_printerr ("modest: message did not have argument\n");
105                         return;
106                 }
107                 dbus_message_iter_get_basic (&iter, &device_name);
108                 modest_conf_set_string (modest_runtime_get_conf(),
109                                         MODEST_CONF_DEVICE_NAME, device_name,
110                                         NULL);
111         }
112 }
113
114
115 static void
116 on_device_name_received (DBusPendingCall *call, void *user_data)
117 {
118         DBusMessage *message;
119         
120         g_return_if_fail (dbus_pending_call_get_completed (call));
121         
122         message = dbus_pending_call_steal_reply (call);
123         if (!message) {
124                 g_printerr ("modest: no reply on device name query\n");
125                 return;
126         }
127
128         update_device_name_from_msg (message);
129         dbus_message_unref (message);
130 }
131
132
133 static DBusHandlerResult
134 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
135 {
136         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
137                 update_device_name_from_msg (msg);
138
139         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
140 }
141
142
143 static void
144 get_device_name_from_dbus ()
145 {
146         static DBusConnection *conn = NULL;
147         DBusMessage *request;
148         DBusError error;
149         DBusPendingCall *call = NULL;
150         
151         dbus_error_init (&error);
152         if (!conn) {
153                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
154                 if (!conn) {
155                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
156                                     error.name, error.message);
157                         dbus_error_free (&error);
158                         return;
159                 }
160         }
161         
162         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
163                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
164         if (!request) {
165                 /* should we free the connection? */
166                 g_printerr ("modest: dbus_message_new_method_call failed\n");
167                 return;
168         }
169         dbus_message_set_auto_start (request, TRUE);
170         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
171                 dbus_pending_call_set_notify (call, on_device_name_received,
172                                               NULL, NULL);
173                 dbus_pending_call_unref (call);
174         }
175         dbus_message_unref (request);
176         
177         dbus_connection_setup_with_g_main (conn, NULL);
178         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
179         if (dbus_error_is_set(&error)) {
180                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
181                 dbus_error_free (&error);
182         }
183
184         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
185                 g_printerr ("modest: dbus_connection_add_filter failed\n");
186 }
187
188
189 void
190 modest_maemo_utils_get_device_name (void)
191 {
192         get_device_name_from_dbus ();
193 }
194
195 void
196 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
197 {
198         gchar *images_folder;
199         GtkFileFilter *file_filter;
200         GList *image_mimetypes_list;
201         GList *node;
202
203         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
204
205         /* Set the default folder to images folder */
206         images_folder = g_build_filename (g_get_home_dir (), 
207                                           MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
208                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
209         gtk_file_chooser_set_current_folder (chooser, images_folder);
210         g_free (images_folder);
211
212         /* Set the images mime filter */
213         file_filter = gtk_file_filter_new ();
214 #ifdef MODEST_HAVE_HILDON0_WIDGETS
215         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
216 #else
217         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
218 #endif
219         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
220                 gtk_file_filter_add_mime_type (file_filter, node->data);
221         }
222         gtk_file_chooser_set_filter (chooser, file_filter);
223 #ifdef MODEST_HAVE_HILDON0_WIDGETS
224         osso_mime_types_list_free (image_mimetypes_list);
225 #else
226         hildon_mime_types_list_free (image_mimetypes_list);
227 #endif
228
229 }
230
231 void
232 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
233                                       gboolean thumbable)
234 {
235         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
236 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
237         hildon_helper_set_thumb_scrollbar (win, thumbable);
238 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
239 }
240
241 FILE*
242 modest_maemo_open_mcc_mapping_file (void)
243 {
244         FILE* result;
245         
246         const gchar* path;
247         const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
248         const gchar* path2 = MODEST_MCC_MAPPING;
249         
250         if (access(path1, R_OK) == 0) 
251                 path = path1;
252         else if (access(path2, R_OK) == 0)
253                 path = path2;
254         else {
255                 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
256                            __FUNCTION__, path1, path2);
257                 return NULL;
258         }
259         
260         result = fopen (path, "r");
261         if (!result) {
262                 g_warning ("%s: error opening mapping file '%s': %s",
263                            __FUNCTION__, path, strerror(errno));
264                 return NULL;
265         }
266         return result;
267 }
268
269 GtkWidget *
270 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
271                                                 const gchar *item_name)
272 {
273         GtkWidget *new_menu;
274         GtkWidget *menubar;
275         GList *children, *iter;
276
277         menubar = gtk_ui_manager_get_widget (manager, item_name);
278         new_menu = gtk_menu_new ();
279
280         children = gtk_container_get_children (GTK_CONTAINER (menubar));
281         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
282                 GtkWidget *menu;
283
284                 menu = GTK_WIDGET (iter->data);
285                 gtk_widget_reparent (menu, new_menu);
286         }
287         
288         g_list_free (children);
289
290         return new_menu;
291 }
292
293 /**
294  * modest_maemo_utils_create_captioned:
295  * @title_size_group: a #GtkSizeGroup
296  * @value_size_group: a #GtkSizeGroup
297  * @title: a string
298  * @control: a #GtkWidget
299  *
300  * this creates a widget (a #GtkHBox) with a control, and a label
301  * (@string) captioning it. It also uses the proper size groups for title
302  * and control.
303  *
304  * Returns: a widget containing the control and a proper label.
305  */
306 GtkWidget *
307 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
308                                         GtkSizeGroup *value_size_group,
309                                         const gchar *title,
310                                         GtkWidget *control)
311 {
312         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
313                                                                    value_size_group,
314                                                                    title,
315                                                                    control,
316                                                                    HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
317 }
318
319 /**
320  * modest_maemo_utils_create_captioned_with_size_type:
321  * @title_size_group: a #GtkSizeGroup
322  * @value_size_group: a #GtkSizeGroup
323  * @title: a string
324  * @control: a #GtkWidget
325  * @size_type: a #HildonSizeType
326  *
327  * this creates a widget (a #GtkHBox) with a control, and a label
328  * (@string) captioning it. It also uses the proper size groups for title
329  * and control.
330  *
331  * Returns: a widget containing the control and a proper label.
332  */
333 GtkWidget *
334 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
335                                                        GtkSizeGroup *value_size_group,
336                                                        const gchar *title,
337                                                        GtkWidget *control,
338                                                        HildonSizeType size_type)
339 {
340         GtkWidget *label;
341         GtkWidget *box;
342   
343         label = gtk_label_new (title);
344         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
345         gtk_widget_show (label);
346         box = gtk_hbox_new (TRUE, MODEST_MARGIN_HALF);
347         gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, MODEST_MARGIN_HALF);
348         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
349         if (title_size_group)
350                 gtk_size_group_add_widget (title_size_group, label);
351         if (value_size_group)
352                 gtk_size_group_add_widget (value_size_group, control);
353
354         hildon_gtk_widget_set_theme_size (control, size_type);
355
356         return box;
357 }
358
359 /**
360  * modest_maemo_utils_set_hbutton_layout:
361  * @title_sizegroup: a #GtkSizeGroup, or %NULL
362  * @value_sizegroup: a #GtkSizeGroup, or %NULL
363  * @title: a string
364  * @button: a #HildonButton
365  *
366  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
367  * the title will be aligned to the left using it. If @value_sizegroup is provided,
368  * the value will be aligned to the left using it. It also sets the title
369  * of the button.
370  *
371  * The alignment is left for the title and for the value.
372  */
373 void
374 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
375                                        GtkSizeGroup *value_sizegroup,
376                                        const gchar *title, 
377                                        GtkWidget *button)
378 {
379         hildon_button_set_title (HILDON_BUTTON (button), title);
380         if (title_sizegroup)
381                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
382         if (value_sizegroup)
383                 hildon_button_add_title_size_group (HILDON_BUTTON (button), value_sizegroup);
384         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
385         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
386         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
387 }
388
389 void
390 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
391                                        const gchar *title, 
392                                        GtkWidget *button)
393 {
394         hildon_button_set_title (HILDON_BUTTON (button), title);
395         if (sizegroup) {
396                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
397                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
398         }
399         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
400         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
401         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
402 }
403
404 GtkWidget *
405 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
406 {
407         GtkWidget *label;
408         GtkWidget *box;
409
410         label = gtk_label_new (label_text);
411         gtk_widget_show (label);
412
413         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
414         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
415         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
416         gtk_widget_show (box);
417
418         return box;
419 }