* add ModestTnyAttachment object
authorArne Zellentin <arne@kernelconcepts.de>
Tue, 13 Jun 2006 14:29:00 +0000 (14:29 +0000)
committerArne Zellentin <arne@kernelconcepts.de>
Tue, 13 Jun 2006 14:29:00 +0000 (14:29 +0000)
* use it in the editor and for sending messages

pmo-trunk-r258

src/Makefile.am
src/gtk-glade/modest-ui-message-editor.c
src/modest-editor-window.c
src/modest-editor-window.h
src/modest-tny-attachment.c [new file with mode: 0644]
src/modest-tny-attachment.h [new file with mode: 0644]
src/modest-tny-transport-actions.c

index 033ca0b..95670ed 100644 (file)
@@ -40,6 +40,8 @@ modest_SOURCES=\
        modest-icon-factory.h\
        modest-tny-account-store.h\
        modest-tny-account-store.c\
+       modest-tny-attachment.h\
+       modest-tny-attachment.c\
        modest-tny-folder-tree-view.h\
        modest-tny-folder-tree-view.c\
        modest-tny-header-tree-view.h\
index 5732817..c8b5173 100644 (file)
@@ -27,6 +27,8 @@
 #include "../modest-text-utils.h"
 #include "../modest-tny-msg-actions.h"
 
+#include "../modest-tny-attachment.h"
+
 #include "../modest-editor-window.h"
 
 #include "modest-ui-glade.h"
@@ -429,7 +431,7 @@ on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin)
 {
        /* open file selector */
        GtkWidget *dialog;
-       gchar *mime_type;
+       ModestTnyAttachment *attachment;
        gchar *filename = NULL;
        
        dialog = gtk_file_chooser_dialog_new ("Open File",
@@ -451,14 +453,11 @@ on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin)
        if (!filename)
                return;
        
-       g_return_if_fail(g_str_has_suffix(filename, ".jpg")); /* for now... */
-       
-       /* get mime type */
-       mime_type = "image/jpeg";
-       
-       /* attach file */
+       attachment = modest_tny_attachment_new();
+       modest_tny_attachment_set_filename(attachment, filename);
+       modest_tny_attachment_guess_mime_type(attachment);
        
-       modest_editor_window_attach_file(modest_editwin, filename);
+       modest_editor_window_attach_file(modest_editwin, attachment);
        
        g_free (filename);
 }
index 72ef31b..b232c88 100644 (file)
@@ -233,7 +233,7 @@ gboolean modest_editor_window_set_body(ModestEditorWindow *edit_win, const gchar
 }
 
 
-gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, const gchar *filename)
+gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, ModestTnyAttachment *attachment)
 {
        ModestEditorWindowPrivate *priv;
 
@@ -243,7 +243,7 @@ gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, const gc
        
        priv->attachments = g_list_append(
                                                        priv->attachments, 
-                                                       g_strdup(filename));
+                                                       attachment);
        
        return modest_ui_editor_window_update_attachments(priv->user_data);
 }
index 252b8a2..a2cad64 100644 (file)
@@ -8,7 +8,7 @@
 #include <gtk/gtkwindow.h>
 
 #include "modest-ui.h"
-
+#include "modest-tny-attachment.h"
 
 G_BEGIN_DECLS
 
@@ -152,7 +152,7 @@ gboolean modest_editor_window_set_body(ModestEditorWindow *edit_win, const gchar
  *
  * Returns: TRUE on success, FALSE otherwise
  */
-gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, const gchar *filename);
+gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, ModestTnyAttachment *attachment);
 
 /**
  * modest_editor_window_set_attachments:
diff --git a/src/modest-tny-attachment.c b/src/modest-tny-attachment.c
new file mode 100644 (file)
index 0000000..33e1b10
--- /dev/null
@@ -0,0 +1,228 @@
+/* modest-tny-attachment.c */
+
+/* insert (c)/licensing information) */
+
+#include "modest-tny-attachment.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <tny-stream-camel.h>
+#include <tny-fs-stream.h>
+#include <camel/camel.h>
+
+
+/* include other impl specific header files */
+
+/* 'private'/'protected' functions */
+static void                       modest_tny_attachment_class_init    (ModestTnyAttachmentClass *klass);
+static void                       modest_tny_attachment_init          (ModestTnyAttachment *obj);
+static void                       modest_tny_attachment_finalize      (GObject *obj);
+
+/* list my signals */
+enum {
+       /* MY_SIGNAL_1, */
+       /* MY_SIGNAL_2, */
+       LAST_SIGNAL
+};
+
+typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
+struct _ModestTnyAttachmentPrivate {
+       gchar *name;
+       gchar *filename;
+       gchar *mime_type;
+       gchar *disposition;
+       gchar *content_id;
+       TnyStreamIface *stream;
+};
+#define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
+                                                   MODEST_TYPE_TNY_ATTACHMENT, \
+                                                   ModestTnyAttachmentPrivate))
+/* globals */
+static GObjectClass *parent_class = NULL;
+
+/* uncomment the following if you have defined any signals */
+/* static guint signals[LAST_SIGNAL] = {0}; */
+
+GType
+modest_tny_attachment_get_type (void)
+{
+       static GType my_type = 0;
+       if (!my_type) {
+               static const GTypeInfo my_info = {
+                       sizeof(ModestTnyAttachmentClass),
+                       NULL,           /* base init */
+                       NULL,           /* base finalize */
+                       (GClassInitFunc) modest_tny_attachment_class_init,
+                       NULL,           /* class finalize */
+                       NULL,           /* class data */
+                       sizeof(ModestTnyAttachment),
+                       1,              /* n_preallocs */
+                       (GInstanceInitFunc) modest_tny_attachment_init,
+               };
+               my_type = g_type_register_static (G_TYPE_OBJECT,
+                                                 "ModestTnyAttachment",
+                                                 &my_info, 0);
+       }
+       return my_type;
+}
+
+static void
+modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
+{
+       GObjectClass *gobject_class;
+       gobject_class = (GObjectClass*) klass;
+
+       parent_class            = g_type_class_peek_parent (klass);
+       gobject_class->finalize = modest_tny_attachment_finalize;
+
+       g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
+
+       /* signal definitions go here, e.g.: */
+/*     signals[MY_SIGNAL_1] = */
+/*             g_signal_new ("my_signal_1",....); */
+/*     signals[MY_SIGNAL_2] = */
+/*             g_signal_new ("my_signal_2",....); */
+/*     etc. */
+}
+
+static void
+modest_tny_attachment_init (ModestTnyAttachment *obj)
+{
+       ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
+
+       priv->name = NULL;
+       priv->filename = NULL;
+       priv->mime_type = NULL;
+       priv->disposition = NULL;
+       priv->content_id = NULL;
+       priv->stream = NULL;
+}
+
+static void
+modest_tny_attachment_finalize (GObject *obj)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
+       g_free(priv->name);
+       g_free(priv->mime_type);
+       g_free(priv->disposition);
+       g_free(priv->content_id);
+       g_object_unref(G_OBJECT(priv->stream));
+}
+
+ModestTnyAttachment *
+modest_tny_attachment_new (void)
+{
+       return MODEST_TNY_ATTACHMENT(G_OBJECT(g_object_new(MODEST_TYPE_TNY_ATTACHMENT, NULL)));
+}
+
+
+void
+modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       g_free(priv->name);
+       priv->name = g_strdup(thing);
+}
+
+const gchar *
+modest_tny_attachment_get_name (ModestTnyAttachment *self)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       return priv->name;
+}
+
+
+void
+modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       g_free(priv->filename);
+       priv->filename = g_strdup(thing);
+}
+
+const gchar *
+modest_tny_attachment_get_filename (ModestTnyAttachment *self)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       return priv->filename;
+}
+
+
+void
+modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       g_free(priv->mime_type);
+       priv->mime_type = g_strdup(thing);
+}
+
+const gchar *
+modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       return priv->mime_type;
+}
+
+
+
+void
+modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
+{
+       ModestTnyAttachmentPrivate *priv;
+       gchar *suffixes[] = {".jpg", ".gif", ".mp3", NULL};
+       gchar *types[]    = {"image/jpeg", "image/gif", "audio/mpeg", NULL};
+       gint pos;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       if (!priv->filename)
+               return;
+       
+       for (pos = 0 ; suffixes[pos] ; pos++) {
+               if (g_str_has_suffix(priv->filename, suffixes[pos]))
+                       break;
+       }
+       
+       g_free(priv->mime_type);
+       if (suffixes[pos])
+               priv->mime_type = types[pos];
+       else
+               priv->mime_type = NULL;
+}
+
+static TnyStreamIface *
+make_stream_from_file(const gchar * filename)
+{
+       gint file;
+       
+       file = open(filename, O_RDONLY);
+       if (file < 0)
+               return NULL;
+
+       return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
+}
+
+TnyStreamIface *
+modest_tny_attachment_get_stream (ModestTnyAttachment *self)
+{
+       ModestTnyAttachmentPrivate *priv;
+       
+       priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
+       if (!priv->stream)
+               if (priv->filename)
+                       priv->stream = make_stream_from_file(priv->filename);
+       return priv->stream;
+}
diff --git a/src/modest-tny-attachment.h b/src/modest-tny-attachment.h
new file mode 100644 (file)
index 0000000..3855f66
--- /dev/null
@@ -0,0 +1,63 @@
+/* modest-tny-attachment.h */
+/* insert (c)/licensing information) */
+
+#ifndef __MODEST_TNY_ATTACHMENT_H__
+#define __MODEST_TNY_ATTACHMENT_H__
+
+#include <glib-object.h>
+#include <tny-stream-iface.h>
+/* other include files */
+
+G_BEGIN_DECLS
+
+/* convenience macros */
+#define MODEST_TYPE_TNY_ATTACHMENT             (modest_tny_attachment_get_type())
+#define MODEST_TNY_ATTACHMENT(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),MODEST_TYPE_TNY_ATTACHMENT,ModestTnyAttachment))
+#define MODEST_TNY_ATTACHMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),MODEST_TYPE_TNY_ATTACHMENT,GObject))
+#define MODEST_IS_TNY_ATTACHMENT(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),MODEST_TYPE_TNY_ATTACHMENT))
+#define MODEST_IS_TNY_ATTACHMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),MODEST_TYPE_TNY_ATTACHMENT))
+#define MODEST_TNY_ATTACHMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),MODEST_TYPE_TNY_ATTACHMENT,ModestTnyAttachmentClass))
+
+typedef struct _ModestTnyAttachment      ModestTnyAttachment;
+typedef struct _ModestTnyAttachmentClass ModestTnyAttachmentClass;
+
+struct _ModestTnyAttachment {
+        GObject parent;
+       /* insert public members, if any */
+};
+
+struct _ModestTnyAttachmentClass {
+       GObjectClass parent_class;
+       /* insert signal callback declarations, eg. */
+       /* void (* my_event) (ModestTnyAttachment* obj); */
+};
+
+/* member functions */
+GType        modest_tny_attachment_get_type    (void) G_GNUC_CONST;
+
+/* typical parameter-less _new function */
+/* if this is a kind of GtkWidget, it should probably return at GtkWidget*, */
+/*    otherwise probably a GObject*. */
+ModestTnyAttachment*    modest_tny_attachment_new         (void);
+
+/* fill in other public functions, eg.: */
+/*     void       modest_tny_attachment_do_something (ModestTnyAttachment *self, const gchar* param); */
+/*     gboolean   modest_tny_attachment_has_foo      (ModestTnyAttachment *self, gint value); */
+
+void modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing);
+const gchar *modest_tny_attachment_get_name (ModestTnyAttachment *self);
+
+void modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing);
+const gchar *modest_tny_attachment_get_filename (ModestTnyAttachment *self);
+
+void modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing);
+const gchar *modest_tny_attachment_get_mime_type (ModestTnyAttachment *self);
+
+void modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self);
+
+TnyStreamIface * modest_tny_attachment_get_stream (ModestTnyAttachment *self);
+
+G_END_DECLS
+
+#endif /* __MODEST_TNY_ATTACHMENT_H__ */
+
index a998161..eee55b3 100644 (file)
@@ -20,9 +20,8 @@
 #include <camel/camel.h>
 #include <camel/camel-folder-summary.h>
 
-
-
 #include "modest-tny-transport-actions.h"
+#include "modest-tny-attachment.h"
 /* include other impl specific header files */
 
 /* 'private'/'protected' functions */
@@ -161,11 +160,12 @@ modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
        TnyMsgMimePartIface *attachment_part, *text_body_part;
        TnyMsgHeaderIface *headers;
        TnyStreamIface *text_body_stream, *attachment_stream;
-       GList *attachment;
-       gchar *content_type, *attachment_content_type;
-       gchar *filename, *attachment_filename;
-       int file;
-
+       ModestTnyAttachment *attachment;
+       GList *pos;
+       gchar *content_type;
+       const gchar *attachment_content_type;
+       gchar *attachment_filename;
+       
        new_msg          = TNY_MSG_IFACE(tny_msg_new ());
        headers          = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
        text_body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
@@ -199,19 +199,17 @@ modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
                //g_object_unref (G_OBJECT(text_body_part));
        }
        
-       for (    attachment = (GList *)attachments_list;
-                    attachment;
-                attachment = attachment->next    ) {
-               filename = attachment->data;
-               attachment_filename = g_path_get_basename(filename);
-               file = open(filename, O_RDONLY);
-               attachment_stream = TNY_STREAM_IFACE(tny_stream_camel_new(
-                                                   camel_stream_fs_new_with_fd(file)));
-
+       for (    pos = (GList *)attachments_list;
+                    pos;
+                pos = pos->next    ) {
+               attachment = pos->data;
+               attachment_filename = g_path_get_basename(
+                                                         modest_tny_attachment_get_filename(attachment));
+               attachment_stream = modest_tny_attachment_get_stream(attachment);
                attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new (
                                                                camel_mime_part_new()));
                
-               attachment_content_type = "image/jpeg"; /* later... */
+               attachment_content_type = modest_tny_attachment_get_mime_type(attachment);
                tny_msg_mime_part_iface_construct_from_stream (attachment_part,
                                                               attachment_stream,
                                                               attachment_content_type);