* Simple UTF8 support for string comparison.
[modest] / src / modest-tny-msg-view.c
index 6b3221f..13ece7a 100644 (file)
@@ -72,7 +72,7 @@ typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
 struct _ModestTnyMsgViewPrivate {
        GtkWidget *gtkhtml;
        TnyMsgIface *msg;
-       ModestConf *conf;
+       gboolean show_attachments_inline;
 };
 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                  MODEST_TYPE_TNY_MSG_VIEW, \
@@ -129,7 +129,7 @@ modest_tny_msg_view_init (ModestTnyMsgView *obj)
        
        priv->gtkhtml = gtk_html_new();
        
-       priv->conf = NULL;
+       priv->show_attachments_inline = FALSE;
 
        gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
        gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
@@ -148,11 +148,11 @@ modest_tny_msg_view_init (ModestTnyMsgView *obj)
 static void
 modest_tny_msg_view_finalize (GObject *obj)
 {      
-       
+       /* TODO! */
 }
 
 GtkWidget*
-modest_tny_msg_view_new (TnyMsgIface *msg, ModestConf *conf)
+modest_tny_msg_view_new (TnyMsgIface *msg, const gboolean show_attachments_inline)
 {
        GObject *obj;
        ModestTnyMsgView* self;
@@ -167,18 +167,17 @@ modest_tny_msg_view_new (TnyMsgIface *msg, ModestConf *conf)
                                       GTK_POLICY_AUTOMATIC);
 
        if (priv->gtkhtml) 
-               gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);  
+               gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
        
        if (msg)
                modest_tny_msg_view_set_message (self, msg);
        
-       priv->conf = conf;
+       modest_tny_msg_view_set_show_attachments_inline_flag(self, show_attachments_inline);
 
        return GTK_WIDGET(self);
 }
 
 
-
 static gboolean
 on_link_clicked (GtkWidget *widget, const gchar *uri,
                                 ModestTnyMsgView *msg_view)
@@ -262,8 +261,7 @@ on_url_requested (GtkWidget *widget, const gchar *uri,
 
        g_message ("url requested: %s", uri);
        
-       if (!modest_conf_get_bool(priv->conf,
-                   MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE, NULL))
+       if (!modest_tny_msg_view_get_show_attachments_inline_flag(msg_view))
                return TRUE; /* debatable */
        
        if (g_str_has_prefix (uri, "cid:")) {
@@ -304,15 +302,39 @@ typedef struct  {
 
 
 static gchar *
+secure_filename(const gchar *fn)
+{
+       gchar *tmp, *p;
+       GString *s;
+       
+       s = g_string_new("");
+#if 1 || DEBUG
+       tmp = g_strdup(fn);
+       for (p = tmp; p[0] ; p++ ) {
+               p[0] &= 0x5f; /* 01011111 */
+               p[0] |= 0x40; /* 01000000 */
+       }
+       g_string_printf(s, "0x%x:%s", g_str_hash(fn), tmp);
+       g_free(tmp);
+       return g_string_free(s, FALSE);
+#else
+       g_string_printf(s, "0x%x", g_str_hash(fn));
+       return g_string_free(s, FALSE);
+#endif
+}
+       
+       
+static gchar *
 construct_virtual_filename(const gchar *filename,
                            const gint position,
                            const gchar *id,
                            const gboolean active)
 {
        GString *s;
+       gchar *fn;
        
        if (position < 0)
-               return "AttachmentInvalid";
+               return g_strdup("AttachmentInvalid");
 
        s = g_string_new("");
        if (active)
@@ -323,8 +345,11 @@ construct_virtual_filename(const gchar *filename,
        if (id)
                g_string_append(s, id);
        g_string_append_c(s, ':');
-       if (filename)
-               g_string_append(s, filename);
+       
+       fn = secure_filename(filename);
+       if (fn)
+               g_string_append(s, fn);
+       g_free(fn);
        g_string_append_c(s, ':');
        return g_string_free(s, FALSE);
 }
@@ -401,7 +426,6 @@ attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
        const GList *attachment_list, *attachment;
        const gchar *content_type, *filename, *id;
        gchar *virtual_filename;
-       gboolean show_attachments_inline;
        
        if (!msg)
                return g_malloc0(1);
@@ -430,9 +454,7 @@ attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
                                attachments_found = TRUE;
                        id = tny_msg_mime_part_iface_get_content_id(
                                                                                TNY_MSG_MIME_PART_IFACE(attachment->data));
-                       show_attachments_inline = modest_conf_get_bool(priv->conf,
-                               MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE, NULL);
-                       if (show_attachments_inline) {
+                       if (modest_tny_msg_view_get_show_attachments_inline_flag(self)) {
                                virtual_filename = construct_virtual_filename(filename,
                                        g_list_position((GList *)attachment_list, (GList *) attachment),
                                        id, TRUE);
@@ -752,25 +774,19 @@ modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
        priv->msg = msg;
        
        fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), "", msg);
-
        if (!msg) 
                return;
        
-       body = modest_tny_msg_actions_find_body_part (msg, "text/html");
+       body = modest_tny_msg_actions_find_body_part (msg, TRUE);
        if (body) {
-               set_html_message (self, body, msg);
-               return;
-       }
-       
-       body = modest_tny_msg_actions_find_body_part (msg, "text/plain");
-       if (body) {
-               set_text_message (self, body, msg);
+               if (tny_msg_mime_part_iface_content_type_is (body, "text/html"))
+                       set_html_message (self, body, msg);
+               else
+                       set_text_message (self, body, msg);
                return;
+       } else {
+               /* nothing to show */
        }
-
-       /* hmmmmm */
-       fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml),
-                               _("Unsupported message type"), msg);
 }
 
 void
@@ -782,3 +798,28 @@ modest_tny_msg_view_redraw (ModestTnyMsgView *self)
        priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
        modest_tny_msg_view_set_message(self, priv->msg);
 }
+
+gboolean
+modest_tny_msg_view_get_show_attachments_inline_flag (ModestTnyMsgView *self)
+{
+       ModestTnyMsgViewPrivate *priv;
+
+       g_return_val_if_fail (self, FALSE);
+       priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
+       return priv->show_attachments_inline;
+}
+
+gboolean
+modest_tny_msg_view_set_show_attachments_inline_flag (ModestTnyMsgView *self, gboolean flag)
+{
+       ModestTnyMsgViewPrivate *priv;
+       gboolean oldflag;
+
+       g_return_val_if_fail (self, FALSE);
+       priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
+       oldflag = priv->show_attachments_inline;
+       priv->show_attachments_inline = flag;
+       if (priv->show_attachments_inline != oldflag)
+               modest_tny_msg_view_redraw(self);
+       return priv->show_attachments_inline;
+}