* make modest_text_utils_get_display_date return a ptr to
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Thu, 1 Nov 2007 15:58:51 +0000 (15:58 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Thu, 1 Nov 2007 15:58:51 +0000 (15:58 +0000)
  as static gchar[]; the saves us a g_strdup / free for
  each of the display_date (esp. in renderer this counts)

Note: it's *not* re-entrent/thread safe anymore, but that
  should not matter, as we're only using it in the UI

pmo-trunk-r3611

src/maemo/modest-main-window.c
src/maemo/modest-platform.c
src/modest-text-utils.c
src/modest-text-utils.h
src/widgets/modest-account-view.c
src/widgets/modest-header-view-render.c

index 738b709..999b4d3 100644 (file)
@@ -1659,7 +1659,7 @@ create_details_widget (GtkWidget *styled_widget, TnyAccount *account)
                TnyAccount *account = TNY_ACCOUNT(folder_store);
                
                time_t last_updated;
-               gchar *last_updated_string;
+               const gchar *last_updated_string;
                /* Get last updated from configuration */
                last_updated = modest_account_mgr_get_last_updated (modest_runtime_get_account_mgr (), 
                                                                    tny_account_get_id (account));
@@ -1674,7 +1674,6 @@ create_details_widget (GtkWidget *styled_widget, TnyAccount *account)
                label_w = gtk_label_new (NULL);
                gtk_label_set_markup (GTK_LABEL (label_w), label);
                gtk_box_pack_start (GTK_BOX (vbox), label_w, FALSE, FALSE, 0);
-               g_free (last_updated_string);
                g_free (label);
        }
 
index eac54a7..cac2a48 100644 (file)
@@ -1256,12 +1256,14 @@ modest_platform_on_new_headers_received (TnyList *header_list)
 
        iter = tny_list_create_iterator (header_list);
        while (!tny_iterator_is_done (iter)) {
-               gchar *url = NULL, *display_address = NULL, *display_date = NULL, *summary = NULL;
+               gchar *url = NULL, *display_address = NULL,  *summary = NULL;
+               const gchar *display_date;
                TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
                TnyFolder *folder = tny_header_get_folder (header);
                gboolean first_notification = TRUE;
                gint notif_id;
-       
+
+               /* constant string, don't free */
                display_date = modest_text_utils_get_display_date (tny_header_get_date_received (header));
 
                display_address = g_strdup(tny_header_get_from (header));
@@ -1272,7 +1274,6 @@ modest_platform_on_new_headers_received (TnyList *header_list)
                                                        tny_header_get_subject (header),
                                                        "qgn_list_messagin",
                                                        "email.arrive");
-               
                /* Create the message URL */
                url = g_strdup_printf ("%s/%s", tny_folder_get_url_string (folder), 
                                       tny_header_get_uid (header));
@@ -1318,7 +1319,6 @@ modest_platform_on_new_headers_received (TnyList *header_list)
                   not to store the list in gconf */
        
                /* Free & carry on */
-               g_free (display_date);
                g_free (display_address);
                g_free (summary);
                g_free (url);
index 3e61cfc..83de27d 100644 (file)
@@ -1098,16 +1098,17 @@ modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insens
 }
 
 
-gchar*
+const gchar*
 modest_text_utils_get_display_date (time_t date)
 {
        time_t now;
-       static const guint BUF_SIZE = 64; 
+#define DATE_BUF_SIZE 64 
        static const guint ONE_DAY = 24 * 60 * 60; /* seconds in one day */
-       gchar date_buf[BUF_SIZE];  
-       gchar today_buf [BUF_SIZE];  
+       static gchar date_buf[DATE_BUF_SIZE];
+
+       gchar today_buf [DATE_BUF_SIZE];  
 
-       modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date); 
+       modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%x", date); 
 
        now = time (NULL);
 
@@ -1119,17 +1120,18 @@ modest_text_utils_get_display_date (time_t date)
                
                /* it's within the last 24 hours, but double check */
                /* use the localized dates */
-               modest_text_utils_strftime (today_buf,  BUF_SIZE, "%x", now); 
+               modest_text_utils_strftime (today_buf,  DATE_BUF_SIZE, "%x", now); 
 
                /* if it's today, use the time instead */
                if (strcmp (date_buf, today_buf) == 0)
-                       modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
+                       modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%X", date);
        }
        
-       return g_strdup(date_buf);
+       return date_buf;
 }
 
 
+
 gboolean
 modest_text_utils_validate_folder_name (const gchar *folder_name)
 {
index 0cba027..e11d702 100644 (file)
@@ -270,10 +270,13 @@ gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean i
  *
  * get a string representation for a date.
  * 
- * Returns: the new display date, as a newly allocated string;
- * free with g_free
+ * Returns: the new display date, as a *static* string.
+ * This string should not be modified, and will change
+ * upon recalling this function. g_strdup it if you to
+ * do so.
+ * 
  */
-gchar* modest_text_utils_get_display_date (time_t date);
+const gchar* modest_text_utils_get_display_date (time_t date);
 
 
 /**
index 20a9866..8fa5421 100644 (file)
@@ -146,22 +146,23 @@ modest_account_view_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-/* Get the string for the last updated time. Result must be g_freed */
-static gchar*
+/* Get the string for the last updated time. Result must NOT be g_freed */
+static const gchar*
 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
 {
        /* FIXME: let's assume that 'last update' applies to the store account... */
-       gchar* last_updated_string;
+       const gchar* last_updated_string;
        time_t last_updated = account_data->store_account->last_updated;
        if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) {
                if (last_updated > 0) 
                        last_updated_string = modest_text_utils_get_display_date(last_updated);
                else
-                       last_updated_string = g_strdup (_("mcen_va_never"));
+                       last_updated_string = _("mcen_va_never");
        } else  {
                /* FIXME: There should be a logical name in the UI specs */
-               last_updated_string = g_strdup(_("mcen_va_refreshing"));
+               last_updated_string = _("mcen_va_refreshing");
        }
+       
        return last_updated_string;
 }
 
@@ -207,8 +208,9 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                if (account_data->store_account) {
 
                        GtkTreeIter iter;
-                       
-                       gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
+
+                       /* don't free */
+                       const gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
                        
                        if (account_data->is_enabled) {
                                const gchar *proto_name;
@@ -224,7 +226,6 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                                        MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
                                        -1);
                        }
-                       g_free (last_updated_string);
                }
 
                modest_account_mgr_free_account_data (account_mgr, account_data);
@@ -267,11 +268,10 @@ on_account_busy_changed(ModestAccountMgr *account_mgr,
                                g_free (cur_name);
                                return;
                        }
-                       gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
+                       const gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
                        gtk_list_store_set(model, &iter, 
                                           MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
                                           -1);
-                       g_free (last_updated_string);
                        modest_account_mgr_free_account_data (account_mgr, account_data);
                        found = TRUE;
                }
index 6763caf..0b1b6e4 100644 (file)
@@ -238,7 +238,6 @@ _modest_header_view_date_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer
 {
        TnyHeaderFlags flags;
        guint date, date_col;
-       gchar *display_date = NULL;
        gboolean received = GPOINTER_TO_INT(user_data);
 
        if (received)
@@ -251,11 +250,10 @@ _modest_header_view_date_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer
                            date_col, &date,
                            -1);
        
-       display_date = modest_text_utils_get_display_date (date);
-       g_object_set (G_OBJECT(renderer), "text", display_date, NULL);  
+       g_object_set (G_OBJECT(renderer), "text", modest_text_utils_get_display_date (date),
+                     NULL);    
 
        set_common_flags (renderer, flags);
-       g_free (display_date);
 }
 
 void
@@ -297,10 +295,6 @@ void
 _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                                               GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
 {
-       g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (column));
-       g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
-       g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
-       
        /* Note that GtkTreeModel is a GtkTreeModelFilter. */
        
        /* printf ("DEBUG: %s: tree_model gtype=%s\n", __FUNCTION__, G_OBJECT_TYPE_NAME (tree_model)); */
@@ -309,7 +303,7 @@ _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCe
        gchar *address = NULL;
        gchar *subject = NULL;
        gchar *header = NULL;
-       time_t date = 0;
+       time_t date;
        
        GtkCellRenderer *recipient_cell, *date_or_status_cell, *subject_cell,
                *attach_cell, *priority_cell,
@@ -318,6 +312,11 @@ _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCe
        gchar *display_date = NULL, *tmp_date = NULL;
        TnyHeaderFlags prio = 0;
 
+
+       g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (column));
+       g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
+       g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
+       
        recipient_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "recpt-box-renderer"));
        subject_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "subject-box-renderer"));
        priority_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (subject_box), "priority-renderer"));
@@ -384,7 +383,7 @@ _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCe
        g_free (header);
        header = NULL;
        set_common_flags (recipient_cell, flags);
-
+       
        if (header_mode == MODEST_HEADER_VIEW_COMPACT_HEADER_MODE_OUTBOX) {
                ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
                const gchar *status_str = "";
@@ -402,16 +401,9 @@ _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCe
                              NULL);
                g_free (display_date);
                display_date = NULL;
-       } else {
-               /* in some rare cases, mail might have no Date: field. it case,
-                * don't show the date, instead of bogus 1/1/1970
-                */
-               if (date)
-                       tmp_date = modest_text_utils_get_display_date (date);
-               else
-                       tmp_date = g_strdup ("");
-               
-               display_date = g_strdup_printf ("<span size='small' foreground='#666666'>%s</span>", tmp_date);
+       } else {                
+               display_date = g_strdup_printf ("<span size='small' foreground='#666666'>%s</span>",
+                                               date ? modest_text_utils_get_display_date (date) : "");
                g_object_set (G_OBJECT (date_or_status_cell),
                              "markup", display_date,
                              NULL);