Add dialog to select attachments in save/remove actions.
[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
57 /*
58  * For getting and tracking the Bluetooth name
59  */
60 #define BTNAME_SERVICE                  "org.bluez"
61 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
62 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
63 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
64 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
65
66 #define BTNAME_REQ_GET                  "GetName"
67 #define BTNAME_SIG_CHANGED              "NameChanged"
68
69 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
70                           "',member='" BTNAME_SIG_CHANGED "'"
71
72 /* Label child of a captioned */
73 #define CAPTIONED_LABEL_CHILD "captioned-label"
74
75
76 static osso_context_t *__osso_context = NULL; /* urgh global */
77
78 osso_context_t *
79 modest_maemo_utils_get_osso_context (void)
80 {
81         if (!__osso_context) 
82                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
83         
84         return __osso_context;
85 }
86
87 void
88 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
89 {
90         g_return_if_fail (osso_context);
91         __osso_context = osso_context;
92 }
93
94 static void
95 update_device_name_from_msg (DBusMessage *message)
96 {
97         DBusError error;
98         DBusMessageIter iter;
99
100         dbus_error_init (&error);
101
102         if (dbus_set_error_from_message (&error, message)) {
103                 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
104                 dbus_error_free (&error);
105         } else {
106                 const gchar *device_name;
107                 if (!dbus_message_iter_init (message, &iter)) {
108                         g_printerr ("modest: message did not have argument\n");
109                         return;
110                 }
111                 dbus_message_iter_get_basic (&iter, &device_name);
112                 modest_conf_set_string (modest_runtime_get_conf(),
113                                         MODEST_CONF_DEVICE_NAME, device_name,
114                                         NULL);
115         }
116 }
117
118
119 static void
120 on_device_name_received (DBusPendingCall *call, void *user_data)
121 {
122         DBusMessage *message;
123         
124         g_return_if_fail (dbus_pending_call_get_completed (call));
125         
126         message = dbus_pending_call_steal_reply (call);
127         if (!message) {
128                 g_printerr ("modest: no reply on device name query\n");
129                 return;
130         }
131
132         update_device_name_from_msg (message);
133         dbus_message_unref (message);
134 }
135
136
137 static DBusHandlerResult
138 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
139 {
140         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
141                 update_device_name_from_msg (msg);
142
143         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
144 }
145
146
147 static void
148 get_device_name_from_dbus ()
149 {
150         static DBusConnection *conn = NULL;
151         DBusMessage *request;
152         DBusError error;
153         DBusPendingCall *call = NULL;
154         
155         dbus_error_init (&error);
156         if (!conn) {
157                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
158                 if (!conn) {
159                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
160                                     error.name, error.message);
161                         dbus_error_free (&error);
162                         return;
163                 }
164         }
165         
166         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
167                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
168         if (!request) {
169                 /* should we free the connection? */
170                 g_printerr ("modest: dbus_message_new_method_call failed\n");
171                 return;
172         }
173         dbus_message_set_auto_start (request, TRUE);
174         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
175                 dbus_pending_call_set_notify (call, on_device_name_received,
176                                               NULL, NULL);
177                 dbus_pending_call_unref (call);
178         }
179         dbus_message_unref (request);
180         
181         dbus_connection_setup_with_g_main (conn, NULL);
182         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
183         if (dbus_error_is_set(&error)) {
184                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
185                 dbus_error_free (&error);
186         }
187
188         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
189                 g_printerr ("modest: dbus_connection_add_filter failed\n");
190 }
191
192
193 void
194 modest_maemo_utils_get_device_name (void)
195 {
196         get_device_name_from_dbus ();
197 }
198
199 void
200 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
201 {
202         gchar *images_folder;
203         GtkFileFilter *file_filter;
204         GList *image_mimetypes_list;
205         GList *node;
206
207         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
208
209         /* Set the default folder to images folder */
210         images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
211                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
212         gtk_file_chooser_set_current_folder (chooser, images_folder);
213         g_free (images_folder);
214
215         /* Set the images mime filter */
216         file_filter = gtk_file_filter_new ();
217 #ifdef MODEST_HAVE_HILDON0_WIDGETS
218         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
219 #else
220         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
221 #endif
222         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
223                 gtk_file_filter_add_mime_type (file_filter, node->data);
224         }
225         gtk_file_chooser_set_filter (chooser, file_filter);
226 #ifdef MODEST_HAVE_HILDON0_WIDGETS
227         osso_mime_types_list_free (image_mimetypes_list);
228 #else
229         hildon_mime_types_list_free (image_mimetypes_list);
230 #endif
231
232 }
233
234 void
235 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
236                                       gboolean thumbable)
237 {
238         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
239 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
240         hildon_helper_set_thumb_scrollbar (win, thumbable);
241 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
242 }
243
244 FILE*
245 modest_maemo_open_mcc_mapping_file (void)
246 {
247         FILE* result;
248         
249         const gchar* path;
250         const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
251         const gchar* path2 = MODEST_MCC_MAPPING;
252         
253         if (access(path1, R_OK) == 0) 
254                 path = path1;
255         else if (access(path2, R_OK) == 0)
256                 path = path2;
257         else {
258                 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
259                            __FUNCTION__, path1, path2);
260                 return NULL;
261         }
262         
263         result = fopen (path, "r");
264         if (!result) {
265                 g_warning ("%s: error opening mapping file '%s': %s",
266                            __FUNCTION__, path, strerror(errno));
267                 return NULL;
268         }
269         return result;
270 }
271
272 GtkWidget *
273 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
274                                                 const gchar *item_name)
275 {
276         GtkWidget *new_menu;
277         GtkWidget *menubar;
278         GList *children, *iter;
279
280         menubar = gtk_ui_manager_get_widget (manager, item_name);
281         new_menu = gtk_menu_new ();
282
283         children = gtk_container_get_children (GTK_CONTAINER (menubar));
284         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
285                 GtkWidget *menu;
286
287                 menu = GTK_WIDGET (iter->data);
288                 gtk_widget_reparent (menu, new_menu);
289         }
290         
291         g_list_free (children);
292
293         return new_menu;
294 }
295
296 /**
297  * modest_maemo_utils_create_captioned:
298  * @title_size_group: a #GtkSizeGroup
299  * @value_size_group: a #GtkSizeGroup
300  * @title: a string
301  * @control: a #GtkWidget
302  *
303  * this creates a widget (a #GtkHBox) with a control, and a label
304  * (@string) captioning it. It also uses the proper size groups for title
305  * and control.
306  *
307  * Returns: a widget containing the control and a proper label.
308  */
309 GtkWidget *
310 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
311                                         GtkSizeGroup *value_size_group,
312                                         const gchar *title,
313                                         gboolean use_markup,
314                                         GtkWidget *control)
315 {
316         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
317                                                                    value_size_group,
318                                                                    title,
319                                                                    use_markup,
320                                                                    control,
321                                                                    HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
322 }
323
324 /**
325  * modest_maemo_utils_create_captioned_with_size_type:
326  * @title_size_group: a #GtkSizeGroup
327  * @value_size_group: a #GtkSizeGroup
328  * @title: a string
329  * @control: a #GtkWidget
330  * @size_type: a #HildonSizeType
331  *
332  * this creates a widget (a #GtkHBox) with a control, and a label
333  * (@string) captioning it. It also uses the proper size groups for title
334  * and control.
335  *
336  * Returns: a widget containing the control and a proper label.
337  */
338 GtkWidget *
339 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
340                                                        GtkSizeGroup *value_size_group,
341                                                        const gchar *title,
342                                                        gboolean use_markup,
343                                                        GtkWidget *control,
344                                                        HildonSizeType size_type)
345 {
346         GtkWidget *label;
347         GtkWidget *box;
348   
349         if (use_markup) {
350                 label = gtk_label_new (NULL);
351                 gtk_label_set_markup (GTK_LABEL (label), title);
352         } else {
353                 label = gtk_label_new (title);
354         }
355
356         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
357         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
358         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
359         gtk_widget_show (label);
360         box = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
361         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
362         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
363         if (title_size_group)
364                 gtk_size_group_add_widget (title_size_group, label);
365         if (value_size_group)
366                 gtk_size_group_add_widget (value_size_group, control);
367
368         hildon_gtk_widget_set_theme_size (control, size_type);
369
370         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
371
372         return box;
373 }
374
375 /**
376  * modest_maemo_utils_captioned_set_label:
377  * @captioned: a #GtkWidget built as captioned
378  * @new_label: a string
379  * @use_markup: a #gboolean
380  *
381  * set a new label for the captioned
382  */
383 void
384 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
385                                         const gchar *new_label,
386                                         gboolean use_markup)
387 {
388         GtkWidget *label;
389
390         g_return_if_fail (GTK_IS_WIDGET (captioned));
391
392         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
393         g_return_if_fail (GTK_IS_LABEL (label));
394
395         if (use_markup) {
396                 gtk_label_set_markup (GTK_LABEL (label), new_label);
397         } else {
398                 gtk_label_set_text (GTK_LABEL (label), new_label);
399         }
400 }
401
402 /**
403  * modest_maemo_utils_set_hbutton_layout:
404  * @title_sizegroup: a #GtkSizeGroup, or %NULL
405  * @value_sizegroup: a #GtkSizeGroup, or %NULL
406  * @title: a string
407  * @button: a #HildonButton
408  *
409  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
410  * the title will be aligned to the left using it. If @value_sizegroup is provided,
411  * the value will be aligned to the left using it. It also sets the title
412  * of the button.
413  *
414  * The alignment is left for the title and for the value.
415  */
416 void
417 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
418                                        GtkSizeGroup *value_sizegroup,
419                                        const gchar *title, 
420                                        GtkWidget *button)
421 {
422         hildon_button_set_title (HILDON_BUTTON (button), title);
423         if (title_sizegroup)
424                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
425         if (value_sizegroup)
426                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
427         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
428         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
429         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
430 }
431
432 void
433 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
434                                        const gchar *title, 
435                                        GtkWidget *button)
436 {
437         hildon_button_set_title (HILDON_BUTTON (button), title);
438         if (sizegroup) {
439                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
440                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
441         }
442         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
443         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
444         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
445 }
446
447 GtkWidget *
448 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
449 {
450         GtkWidget *label;
451         GtkWidget *box;
452
453         label = gtk_label_new (label_text);
454         gtk_widget_show (label);
455
456         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
457         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
458         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
459         gtk_widget_show (box);
460
461         return box;
462 }
463
464 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
465 {
466         return TRUE;
467 }
468
469 gboolean
470 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list)
471 {
472         GtkTreeModel *model;
473         TnyIterator *iterator;
474         GtkWidget *selector;
475         GtkCellRenderer *renderer;
476         GtkWidget *dialog;
477         gint response;
478         gboolean result = TRUE;
479
480         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
481         for (iterator = tny_list_create_iterator (att_list);
482              !tny_iterator_is_done (iterator);
483              tny_iterator_next (iterator)) {
484                 GtkTreeIter iter;
485                 TnyMimePart *part;
486                 gchar *label;
487                 gchar *filename = NULL;
488
489                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
490
491                 if (TNY_IS_MSG (part)) {
492                         TnyHeader *header;
493                         
494                         header = tny_msg_get_header (TNY_MSG (part));
495                         if (TNY_IS_HEADER (header)) {
496                                 filename = g_strdup (tny_mime_part_get_filename (part));
497                                 if (!filename)
498                                         filename = tny_header_dup_subject (header);
499                                 if (filename == NULL || filename[0] == '\0')
500                                         filename = g_strdup (_("mail_va_no_subject"));
501                         }
502                 } else {
503                         filename = g_strdup (tny_mime_part_get_filename (part));
504                 }
505
506                 label = g_strconcat (filename, NULL);
507                 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
508                 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, 1, part, -1);
509                 g_free (label);
510                 g_object_unref (part);
511         }
512
513         selector = GTK_WIDGET (hildon_touch_selector_new ());
514         renderer = gtk_cell_renderer_text_new ();
515         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
516                                              "text", 0, NULL);
517         hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), 
518                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
519
520         dialog = hildon_picker_dialog_new (window);
521         gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title"));
522         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
523         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
524
525         response = gtk_dialog_run (GTK_DIALOG (dialog));
526
527         if (response == GTK_RESPONSE_OK) {
528                 GList *selected_rows, *node;
529
530                 tny_list_remove_matches (att_list, match_all, NULL);
531                 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
532                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
533                         GtkTreePath *path;
534                         GObject *selected;
535                         GtkTreeIter iter;
536
537                         path = (GtkTreePath *) node->data;
538                         gtk_tree_model_get_iter (model, &iter, path);
539                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
540                         tny_list_append (att_list, selected);
541                 }
542                 if (tny_list_get_length (att_list) == 0)
543                         result = FALSE;
544         } else {
545                 result = FALSE;
546         }
547
548         gtk_widget_destroy (dialog);
549
550         g_object_unref (model);
551
552         return result;
553 }