* Fixes NB#92465, show 1Kb when folder size is less than 1Kb
[modest] / src / widgets / modest-details-dialog.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 #include "modest-details-dialog.h"
31
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkscrolledwindow.h>
35 #include <gtk/gtktable.h>
36 #include <gtk/gtkstock.h>
37 #include <gtk/gtklabel.h>
38 #include <tny-msg.h>
39 #include <tny-header.h>
40 #include <tny-header-view.h>
41 #include <tny-folder-store.h>
42 #include <modest-tny-folder.h>
43 #include <modest-tny-account.h>
44 #include <modest-text-utils.h>
45 #include <modest-datetime-formatter.h>
46 #include <string.h> /* for strlen */
47
48 static void    modest_details_dialog_set_header_default          (ModestDetailsDialog *self,
49                                                                   TnyHeader *header);
50
51 static void    modest_details_dialog_set_folder_default          (ModestDetailsDialog *self,
52                                                                   TnyFolder *foler);
53
54 static void    modest_details_dialog_create_container_default    (ModestDetailsDialog *self);
55
56 static void    modest_details_dialog_add_data_default            (ModestDetailsDialog *self,
57                                                                   const gchar *label,
58                                                                   const gchar *value);
59
60
61 G_DEFINE_TYPE (ModestDetailsDialog, 
62                modest_details_dialog, 
63                GTK_TYPE_DIALOG);
64
65 #define MODEST_DETAILS_DIALOG_GET_PRIVATE(o) \
66         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_DETAILS_DIALOG, ModestDetailsDialogPrivate))
67
68
69 typedef struct _ModestDetailsDialogPrivate ModestDetailsDialogPrivate;
70
71 struct _ModestDetailsDialogPrivate
72 {
73         GtkWidget *props_table;
74 };
75
76 static void
77 modest_details_dialog_finalize (GObject *object)
78 {
79         G_OBJECT_CLASS (modest_details_dialog_parent_class)->finalize (object);
80 }
81
82 static void
83 modest_details_dialog_class_init (ModestDetailsDialogClass *klass)
84 {
85         GObjectClass *object_class = G_OBJECT_CLASS (klass);
86
87         g_type_class_add_private (klass, sizeof (ModestDetailsDialogPrivate));
88         object_class->finalize = modest_details_dialog_finalize;
89
90         klass->create_container_func = modest_details_dialog_create_container_default;
91         klass->add_data_func = modest_details_dialog_add_data_default;
92         klass->set_header_func = modest_details_dialog_set_header_default;
93         klass->set_folder_func = modest_details_dialog_set_folder_default;
94 }
95
96 static void
97 modest_details_dialog_init (ModestDetailsDialog *self)
98 {
99 }
100
101 GtkWidget*
102 modest_details_dialog_new_with_header (GtkWindow *parent, 
103                                        TnyHeader *header)
104 {
105         ModestDetailsDialog *dialog;
106
107         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
108         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
109
110         dialog = (ModestDetailsDialog *) (g_object_new (MODEST_TYPE_DETAILS_DIALOG, 
111                                                         "transient-for", parent, 
112                                                         NULL));
113
114         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->create_container_func (dialog);
115         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->set_header_func (dialog, header);
116
117         /* Add close button */
118         gtk_dialog_add_button (GTK_DIALOG (dialog), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
119
120         return GTK_WIDGET (dialog);
121 }
122
123 GtkWidget* 
124 modest_details_dialog_new_with_folder  (GtkWindow *parent, 
125                                         TnyFolder *folder)
126 {
127         ModestDetailsDialog *dialog;
128
129         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
130         g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
131
132         dialog = (ModestDetailsDialog *) (g_object_new (MODEST_TYPE_DETAILS_DIALOG, 
133                                                         "transient-for", parent, 
134                                                         NULL));
135
136         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->create_container_func (dialog);
137         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->set_folder_func (dialog, folder);
138
139         /* Add close button */
140         gtk_dialog_add_button (GTK_DIALOG (dialog), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
141
142         return GTK_WIDGET (dialog);
143 }
144
145 void
146 modest_details_dialog_add_data (ModestDetailsDialog *self,
147                                 const gchar *label,
148                                 const gchar *value)
149 {
150         MODEST_DETAILS_DIALOG_GET_CLASS (self)->add_data_func (self, label, value);
151 }
152
153 static void
154 modest_details_dialog_add_data_default (ModestDetailsDialog *self,
155                                         const gchar *label,
156                                         const gchar *value)
157 {
158         ModestDetailsDialogPrivate *priv;
159         guint n_rows = 0;
160         GtkWidget *label_w, *value_w;
161
162         priv = MODEST_DETAILS_DIALOG_GET_PRIVATE (self);
163
164         g_object_get (G_OBJECT (priv->props_table), "n-rows", &n_rows,NULL);
165
166         /* Create label */
167         label_w = gtk_label_new (label);
168         gtk_misc_set_alignment (GTK_MISC (label_w), 1.0, 0.0);
169         gtk_label_set_justify (GTK_LABEL (label_w), GTK_JUSTIFY_RIGHT);
170
171         /* Create value */
172         value_w = gtk_label_new (value);
173         gtk_label_set_line_wrap (GTK_LABEL (value_w), TRUE);
174         gtk_label_set_line_wrap_mode (GTK_LABEL (value_w), PANGO_WRAP_WORD_CHAR);
175         gtk_misc_set_alignment (GTK_MISC (value_w), 0.0, 0.0);
176         gtk_label_set_justify (GTK_LABEL (value_w), GTK_JUSTIFY_LEFT);
177
178         /* Attach label and value */
179         gtk_table_attach (GTK_TABLE (priv->props_table), 
180                           label_w, 0, 1, 
181                           n_rows, n_rows + 1, 
182                           GTK_SHRINK|GTK_FILL, 
183                           GTK_SHRINK|GTK_FILL, 
184                           0, 0);
185         gtk_table_attach (GTK_TABLE (priv->props_table), 
186                           value_w, 1, 2, 
187                           n_rows, n_rows + 1, 
188                           GTK_EXPAND|GTK_FILL, 
189                           GTK_SHRINK|GTK_FILL, 
190                           0, 0);
191 }
192
193
194 static void
195 modest_details_dialog_set_header_default (ModestDetailsDialog *self,
196                                           TnyHeader *header)
197 {
198         gchar *from, *subject, *to, *cc;
199         time_t received, sent;
200         guint size;
201         gchar *size_s;
202         TnyFolder *folder;
203         TnyFolderType folder_type;
204         ModestDatetimeFormatter *datetime_formatter;
205         const gchar *date_time_str;
206
207         datetime_formatter = modest_datetime_formatter_new ();
208         
209         /* Set window title */
210         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_message_properties"));
211
212         folder = tny_header_get_folder (header);
213         folder_type = modest_tny_folder_guess_folder_type (folder);
214         g_object_unref (folder);
215
216         g_return_if_fail (folder_type != TNY_FOLDER_TYPE_INVALID);
217         
218         /* Get header data */
219         from = tny_header_dup_from (header);
220         to = tny_header_dup_to (header);
221         subject = tny_header_dup_subject (header);
222         cc = tny_header_dup_cc (header);
223         received = tny_header_get_date_received (header);
224         sent = tny_header_get_date_sent (header);
225         size = tny_header_get_message_size (header);
226
227         if (from == NULL)
228                 from = g_strdup ("");
229         if (to == NULL)
230                 to = g_strdup ("");
231         if (subject == NULL)
232                 subject = g_strdup ("");
233         if (cc == NULL)
234                 cc = g_strdup ("");
235
236         /* Add from and subject for all folders */
237         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_from"), from);
238         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_subject"), subject);
239
240
241         /* for inbox, user-created folders and archive: Received */
242         if (received && (folder_type != TNY_FOLDER_TYPE_SENT) &&
243             (folder_type != TNY_FOLDER_TYPE_DRAFTS) &&
244             (folder_type != TNY_FOLDER_TYPE_OUTBOX)) {
245                 date_time_str = modest_datetime_formatter_display_long_datetime (datetime_formatter, 
246                                                                             received);
247                 
248                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_received"),
249                                                 date_time_str);
250         }
251
252         /* for drafts (created) */
253         if (folder_type == TNY_FOLDER_TYPE_DRAFTS) {
254                 date_time_str = modest_datetime_formatter_display_long_datetime (datetime_formatter, 
255                                                                             received);
256                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_created"),
257                                                 date_time_str);
258         }
259
260         /* for everyting except outbox, drafts: Sent */
261         if (sent && (folder_type != TNY_FOLDER_TYPE_DRAFTS)&&
262             (folder_type != TNY_FOLDER_TYPE_OUTBOX)) {
263                 
264                 date_time_str = modest_datetime_formatter_display_long_datetime (datetime_formatter, 
265                                                                             sent);
266                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_sent"),
267                                                 date_time_str);
268         }
269
270         /* Set To and CC */
271         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_to"), to);
272
273         /* only show cc when it's there */
274         if (cc && strlen(cc) > 0)
275                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_cc"), cc);
276
277         /* Set size */
278         size_s = modest_text_utils_get_display_size (size);
279         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_size"), size_s);
280         g_free (size_s);
281
282         /* Frees */
283         g_object_unref (datetime_formatter);
284         g_free (to);
285         g_free (from);
286         g_free (subject);
287         g_free (cc);
288 }
289
290 static void
291 modest_details_dialog_set_folder_default (ModestDetailsDialog *self,
292                                           TnyFolder *folder)
293 {
294         gchar *count_s, *size_s, *name = NULL;
295         gchar *tmp = NULL;
296         guint size, count;
297
298         g_return_if_fail (folder && TNY_IS_FOLDER (folder));
299         g_return_if_fail (modest_tny_folder_guess_folder_type (folder)
300                           != TNY_FOLDER_TYPE_INVALID);
301
302         /* Set window title */
303         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_folder_properties"));
304
305         /* Get data. We use our function because it's recursive */
306         count = tny_folder_get_all_count (TNY_FOLDER (folder));
307         size = tny_folder_get_local_size (TNY_FOLDER (folder));
308
309         /* Format count and size */
310         count_s = g_strdup_printf ("%d", count);
311         size_s = modest_text_utils_get_display_size (size);
312
313         /* Different names for the local folders */
314         if (modest_tny_folder_is_local_folder (folder) ||
315             modest_tny_folder_is_memory_card_folder (folder)) {
316                 gint type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
317                 if (type != TNY_FOLDER_TYPE_UNKNOWN)
318                         name = g_strdup(modest_local_folder_info_get_type_display_name (type));
319         }
320
321         if (!name) {
322                 if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX)
323                         name = g_strdup (_("mcen_me_folder_inbox"));
324                 else
325                         name = g_strdup (tny_folder_get_name (folder));
326         }
327
328         tmp = g_strconcat (_("mcen_fi_folder_properties_foldername"), ":", NULL);
329         modest_details_dialog_add_data (self, tmp, name);
330         g_free (tmp);
331
332         tmp = g_strconcat (_("mcen_fi_folder_properties_messages"), ":", NULL);
333         modest_details_dialog_add_data (self, tmp, count_s);
334         g_free (tmp);
335
336         tmp = g_strconcat (_("mcen_fi_folder_properties_size"), ":", NULL);
337         modest_details_dialog_add_data (self, tmp, size_s);
338         g_free (tmp);
339
340         /* Frees */
341         g_free (name);
342         g_free (size_s);
343         g_free (count_s);
344 }
345
346 static gboolean
347 on_key_press_event (GtkWindow *window, GdkEventKey *event, gpointer userdata)
348 {
349         GtkWidget *focused;
350
351         focused = gtk_window_get_focus (window);
352         if (GTK_IS_SCROLLED_WINDOW (focused)) {
353                 GtkAdjustment *vadj;
354                 gboolean return_value;
355
356                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (focused));
357                 switch (event->keyval) {
358                 case GDK_Up:
359                 case GDK_KP_Up:
360                         if (vadj->value > 0.0) {
361                                 g_signal_emit_by_name (G_OBJECT (focused), "scroll-child", GTK_SCROLL_STEP_UP, FALSE, 
362                                                        &return_value);
363                                 return TRUE;
364                         }
365                         break;
366                 case GDK_Down:
367                 case GDK_KP_Down:
368                         if (vadj->value < vadj->upper - vadj->page_size) {
369                                 g_signal_emit_by_name (G_OBJECT (focused), "scroll-child", GTK_SCROLL_STEP_DOWN, FALSE, 
370                                                        &return_value);
371                                 return TRUE;
372                         }
373                         break;
374                 }
375         }
376
377         return FALSE;
378 }
379
380 static void
381 modest_details_dialog_create_container_default (ModestDetailsDialog *self)
382 {
383         ModestDetailsDialogPrivate *priv;
384         GtkWidget *scrollbar;
385
386         priv = MODEST_DETAILS_DIALOG_GET_PRIVATE (self);
387         scrollbar = gtk_scrolled_window_new (NULL, NULL);
388
389         gtk_window_set_default_size (GTK_WINDOW (self), 400, 220);
390
391         priv->props_table = gtk_table_new (0, 2, FALSE);
392         gtk_table_set_col_spacings (GTK_TABLE (priv->props_table), 12);
393         gtk_table_set_row_spacings (GTK_TABLE (priv->props_table), 1);
394         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollbar), priv->props_table);
395         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollbar), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
396         gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->props_table), 
397                                              gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollbar)));
398         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), scrollbar);
399
400         gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
401
402         g_signal_connect (self, "key-press-event", G_CALLBACK (on_key_press_event), self);
403 }