1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
37 #include <glib/gstdio.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"
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>
58 * For getting and tracking the Bluetooth name
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"
66 #define BTNAME_REQ_GET "GetName"
67 #define BTNAME_SIG_CHANGED "NameChanged"
69 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
70 "',member='" BTNAME_SIG_CHANGED "'"
72 /* Label child of a captioned */
73 #define CAPTIONED_LABEL_CHILD "captioned-label"
76 static osso_context_t *__osso_context = NULL; /* urgh global */
79 modest_maemo_utils_get_osso_context (void)
82 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
84 return __osso_context;
88 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
90 g_return_if_fail (osso_context);
91 __osso_context = osso_context;
95 update_device_name_from_msg (DBusMessage *message)
100 dbus_error_init (&error);
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);
106 const gchar *device_name;
107 if (!dbus_message_iter_init (message, &iter)) {
108 g_printerr ("modest: message did not have argument\n");
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,
120 on_device_name_received (DBusPendingCall *call, void *user_data)
122 DBusMessage *message;
124 g_return_if_fail (dbus_pending_call_get_completed (call));
126 message = dbus_pending_call_steal_reply (call);
128 g_printerr ("modest: no reply on device name query\n");
132 update_device_name_from_msg (message);
133 dbus_message_unref (message);
137 static DBusHandlerResult
138 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
140 if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
141 update_device_name_from_msg (msg);
143 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
148 get_device_name_from_dbus ()
150 static DBusConnection *conn = NULL;
151 DBusMessage *request;
153 DBusPendingCall *call = NULL;
155 dbus_error_init (&error);
157 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
159 g_printerr ("modest: cannot get on the dbus: %s: %s\n",
160 error.name, error.message);
161 dbus_error_free (&error);
166 request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
167 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
169 /* should we free the connection? */
170 g_printerr ("modest: dbus_message_new_method_call failed\n");
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,
177 dbus_pending_call_unref (call);
179 dbus_message_unref (request);
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);
188 if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
189 g_printerr ("modest: dbus_connection_add_filter failed\n");
193 modest_maemo_utils_get_device_name (void)
195 get_device_name_from_dbus ();
199 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
201 GtkFileFilter *file_filter;
202 GList *image_mimetypes_list;
206 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
208 conf_folder = modest_conf_get_string (modest_runtime_get_conf (), MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
209 if (conf_folder && conf_folder[0] != '\0') {
210 gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
212 gchar *images_folder;
213 /* Set the default folder to images folder */
214 images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
215 MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
216 gtk_file_chooser_set_current_folder (chooser, images_folder);
217 g_free (images_folder);
219 g_free (conf_folder);
221 /* Set the images mime filter */
222 file_filter = gtk_file_filter_new ();
223 #ifdef MODEST_HAVE_HILDON0_WIDGETS
224 image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
226 image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
228 for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
229 gtk_file_filter_add_mime_type (file_filter, node->data);
231 gtk_file_chooser_set_filter (chooser, file_filter);
232 #ifdef MODEST_HAVE_HILDON0_WIDGETS
233 osso_mime_types_list_free (image_mimetypes_list);
235 hildon_mime_types_list_free (image_mimetypes_list);
241 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win,
244 g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
245 #ifdef MODEST_HAVE_HILDON1_WIDGETS
246 hildon_helper_set_thumb_scrollbar (win, thumbable);
247 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
251 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
252 const gchar *item_name)
256 GList *children, *iter;
258 menubar = gtk_ui_manager_get_widget (manager, item_name);
259 new_menu = gtk_menu_new ();
261 children = gtk_container_get_children (GTK_CONTAINER (menubar));
262 for (iter = children; iter != NULL; iter = g_list_next (iter)) {
265 menu = GTK_WIDGET (iter->data);
266 gtk_widget_reparent (menu, new_menu);
269 g_list_free (children);
275 * modest_maemo_utils_create_captioned:
276 * @title_size_group: a #GtkSizeGroup
277 * @value_size_group: a #GtkSizeGroup
279 * @control: a #GtkWidget
281 * this creates a widget (a #GtkHBox) with a control, and a label
282 * (@string) captioning it. It also uses the proper size groups for title
285 * Returns: a widget containing the control and a proper label.
288 modest_maemo_utils_create_captioned (GtkSizeGroup *title_size_group,
289 GtkSizeGroup *value_size_group,
294 return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
303 * modest_maemo_utils_create_captioned_with_size_type:
304 * @title_size_group: a #GtkSizeGroup
305 * @value_size_group: a #GtkSizeGroup
307 * @control: a #GtkWidget
308 * @size_type: a #HildonSizeType
310 * this creates a widget (a #GtkHBox) with a control, and a label
311 * (@string) captioning it. It also uses the proper size groups for title
314 * Returns: a widget containing the control and a proper label.
317 modest_maemo_utils_create_captioned_with_size_type (GtkSizeGroup *title_size_group,
318 GtkSizeGroup *value_size_group,
322 HildonSizeType size_type)
329 label = gtk_label_new (NULL);
330 gtk_label_set_markup (GTK_LABEL (label), title);
332 label = gtk_label_new (title);
334 align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
335 gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_TRIPLE);
337 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
338 hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
339 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
340 gtk_widget_show (label);
341 gtk_widget_show (align);
342 box = gtk_hbox_new (FALSE, 0);
343 gtk_container_add (GTK_CONTAINER (align), label);
344 gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
345 gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
346 if (title_size_group)
347 gtk_size_group_add_widget (title_size_group, label);
348 if (value_size_group)
349 gtk_size_group_add_widget (value_size_group, control);
351 hildon_gtk_widget_set_theme_size (control, size_type);
353 g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
359 * modest_maemo_utils_captioned_set_label:
360 * @captioned: a #GtkWidget built as captioned
361 * @new_label: a string
362 * @use_markup: a #gboolean
364 * set a new label for the captioned
367 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
368 const gchar *new_label,
373 g_return_if_fail (GTK_IS_WIDGET (captioned));
375 label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
376 g_return_if_fail (GTK_IS_LABEL (label));
379 gtk_label_set_markup (GTK_LABEL (label), new_label);
381 gtk_label_set_text (GTK_LABEL (label), new_label);
386 * modest_maemo_utils_set_hbutton_layout:
387 * @title_sizegroup: a #GtkSizeGroup, or %NULL
388 * @value_sizegroup: a #GtkSizeGroup, or %NULL
390 * @button: a #HildonButton
392 * Configures the alignment and layout of @button. If @title_sizegroup is provided,
393 * the title will be aligned to the left using it. If @value_sizegroup is provided,
394 * the value will be aligned to the left using it. It also sets the title
397 * The alignment is left for the title and for the value.
400 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup,
401 GtkSizeGroup *value_sizegroup,
405 hildon_button_set_title (HILDON_BUTTON (button), title);
407 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
409 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
410 hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
411 hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
412 hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
416 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup,
420 hildon_button_set_title (HILDON_BUTTON (button), title);
422 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
423 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
425 hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
426 hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
427 hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
431 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
436 label = gtk_label_new (label_text);
437 gtk_widget_show (label);
439 box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
440 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
441 gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
442 gtk_widget_show (box);
447 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
453 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
456 TnyIterator *iterator;
458 GtkCellRenderer *renderer;
461 gboolean result = TRUE;
462 gint attachments_added = 0;
464 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
465 for (iterator = tny_list_create_iterator (att_list);
466 !tny_iterator_is_done (iterator);
467 tny_iterator_next (iterator)) {
470 gchar *filename = NULL;
472 part = (TnyMimePart *) tny_iterator_get_current (iterator);
474 /* Ignore purged attachments and messages if ignore is
476 if (!(tny_mime_part_is_purged (part) ||
477 (TNY_IS_MSG (part) && !include_msgs))) {
479 if (TNY_IS_MSG (part)) {
480 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
481 filename = tny_header_dup_subject (header);
482 if ((filename == NULL) || (filename[0] == '\0')) {
484 filename = g_strdup (_("mail_va_no_subject"));
486 g_object_unref (header);
488 filename = g_strdup (tny_mime_part_get_filename (part));
490 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
491 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
492 attachments_added ++;
494 g_object_unref (part);
498 selector = GTK_WIDGET (hildon_touch_selector_new ());
499 renderer = gtk_cell_renderer_text_new ();
500 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
502 hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
503 HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
505 dialog = hildon_picker_dialog_new (window);
506 gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
507 _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
508 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
509 hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
511 response = gtk_dialog_run (GTK_DIALOG (dialog));
513 if (response == GTK_RESPONSE_OK) {
514 GList *selected_rows, *node;
516 tny_list_remove_matches (att_list, match_all, NULL);
517 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
518 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
523 path = (GtkTreePath *) node->data;
524 gtk_tree_model_get_iter (model, &iter, path);
525 gtk_tree_model_get (model, &iter, 1, &selected, -1);
526 tny_list_append (att_list, selected);
528 if (tny_list_get_length (att_list) == 0)
534 gtk_widget_destroy (dialog);
536 g_object_unref (model);