* all:
[modest] / src / gtk / modest-edit-msg-window.c
index ff5e5af..4c7afcf 100644 (file)
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-
+#include <glib/gi18n.h>
+#include <tny-account-store.h>
 #include "modest-edit-msg-window.h"
-#include <widgets/modest-msg-view.h>
-#include <modest-widget-memory.h>
-#include <modest-widget-factory.h>
-#include "modest-icon-names.h"
-#include <modest-tny-transport-actions.h>
+#include "modest-widget-memory.h"
+#include "modest-mail-operation.h"
+#include "modest-tny-platform-factory.h"
+#include "modest-tny-msg-actions.h"
+#include <tny-simple-list.h>
 
 static void  modest_edit_msg_window_class_init   (ModestEditMsgWindowClass *klass);
 static void  modest_edit_msg_window_init         (ModestEditMsgWindow *obj);
@@ -48,8 +49,8 @@ enum {
 typedef struct _ModestEditMsgWindowPrivate ModestEditMsgWindowPrivate;
 struct _ModestEditMsgWindowPrivate {
 
-       ModestConf *conf;
        ModestWidgetFactory *factory;
+       TnyPlatformFactory *fact;
        
        GtkWidget      *toolbar, *menubar;
        GtkWidget      *msg_body;
@@ -114,6 +115,7 @@ modest_edit_msg_window_init (ModestEditMsgWindow *obj)
        ModestEditMsgWindowPrivate *priv;
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
 
+       priv->fact = modest_tny_platform_factory_get_instance ();
        priv->factory = NULL;
        priv->toolbar = NULL;
        priv->menubar = NULL;
@@ -125,10 +127,12 @@ static void
 save_settings (ModestEditMsgWindow *self)
 {
        ModestEditMsgWindowPrivate *priv;
+       ModestConf *conf;
+
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self);
-       modest_widget_memory_save_settings (priv->conf,
-                                           GTK_WIDGET(self),
-                                           "modest-edit-msg-window");
+       conf = modest_tny_platform_factory_get_modest_conf_instance (priv->fact);
+
+       modest_widget_memory_save (conf, GTK_WIDGET(self), "modest-edit-msg-window");
 }
 
 
@@ -136,9 +140,12 @@ static void
 restore_settings (ModestEditMsgWindow *self)
 {
        ModestEditMsgWindowPrivate *priv;
+       ModestConf *conf;
+
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self);
-       modest_widget_memory_restore_settings (priv->conf, GTK_WIDGET(self),
-                                              "modest-edit-msg-window");
+       conf = modest_tny_platform_factory_get_modest_conf_instance (priv->fact);
+
+       modest_widget_memory_restore (conf, GTK_WIDGET(self), "modest-edit-msg-window");
 }
 
        
@@ -232,17 +239,21 @@ menubar_new (ModestEditMsgWindow *self)
 static void
 send_mail (ModestEditMsgWindow *self)
 {
-       const gchar *from, *to, *cc, *bcc, *subject;
-       gchar *body;
+       const gchar *to, *cc, *bcc, *subject;
+       gchar *body, *from;
        ModestEditMsgWindowPrivate *priv;
+       TnyTransportAccount *transport_account;
+       ModestMailOperation *mail_operation;
+       ModestAccountData *data;
        
        GtkTextBuffer *buf;
        GtkTextIter b, e;
        
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self);
+       data = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
 
-       /* don't free these */
-       from    = "djcb@djcbsoftware.nl";
+       /* don't free these (except from) */
+       from    =  g_strdup_printf ("%s <%s>", data->full_name, data->email) ;
        to      =  gtk_entry_get_text (GTK_ENTRY(priv->to_field));
        cc      =  gtk_entry_get_text (GTK_ENTRY(priv->cc_field));
        bcc     =  gtk_entry_get_text (GTK_ENTRY(priv->bcc_field));
@@ -255,9 +266,40 @@ send_mail (ModestEditMsgWindow *self)
        body  = gtk_text_buffer_get_text (buf, &b, &e,
                                          FALSE); /* free this one */
 
-//     modest_tny_transport_actions_send_message (transport_account,
-//                                                from, to, cc, bcc,
-//                                                subject, *body, NULL);
+       /* FIXME: Code added just for testing. The transport_account
+          should be provided by the account manager, maybe using
+          _get_current_account () or _get_default_account
+          (TRANSPORT_ACCOUNT). These methods do not exist currently. */
+       {
+               TnyList *accounts;
+               TnyIterator *iter;
+               TnyAccountStore *account_store;
+
+               accounts = TNY_LIST(tny_simple_list_new ());
+               account_store = tny_platform_factory_new_account_store (priv->fact);
+               tny_account_store_get_accounts (account_store, accounts,
+                                               TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
+
+               iter = tny_list_create_iterator(accounts);
+               tny_iterator_first (iter);
+               if (tny_iterator_is_done (iter)) {
+                       /* FIXME: Add error handling through mail operation */
+                       g_printerr("modest: no transport accounts defined\n");
+                       g_free (body);
+                       return;
+               }
+               transport_account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
+       }
+
+       mail_operation = modest_mail_operation_new ();
+
+       modest_mail_operation_send_new_mail (mail_operation,
+                                            transport_account,
+                                            from, to, cc, bcc,
+                                            subject, body, NULL);
+       /* Clean up */
+       g_object_unref (mail_operation);
+       g_free (from);
        g_free (body);
 }
 
@@ -332,10 +374,10 @@ init_window (ModestEditMsgWindow *obj)
 
        priv->from_field    = modest_widget_factory_get_combo_box (priv->factory,
                                                                   MODEST_COMBO_BOX_TYPE_TRANSPORTS);
-       priv->to_field      = gtk_entry_new_with_max_length (40);
-       priv->cc_field      = gtk_entry_new_with_max_length (40);
-       priv->bcc_field     = gtk_entry_new_with_max_length (40);
-       priv->subject_field = gtk_entry_new_with_max_length (40);
+       priv->to_field      = gtk_entry_new_with_max_length (80);
+       priv->cc_field      = gtk_entry_new_with_max_length (80);
+       priv->bcc_field     = gtk_entry_new_with_max_length (80);
+       priv->subject_field = gtk_entry_new_with_max_length (80);
        
        header_table = gtk_table_new (5,2, FALSE);
        
@@ -378,9 +420,6 @@ modest_edit_msg_window_finalize (GObject *obj)
 
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
 
-       g_object_unref (G_OBJECT(priv->conf));
-       priv->conf = NULL;
-
        g_object_unref (G_OBJECT(priv->factory));
        priv->factory = NULL;
        
@@ -399,13 +438,12 @@ on_delete_event (GtkWidget *widget, GdkEvent *event, ModestEditMsgWindow *self)
 
 
 GtkWidget*
-modest_edit_msg_window_new (ModestConf *conf, ModestWidgetFactory *factory,
-                           ModestEditType type, TnyMsgIface *msg)
+modest_edit_msg_window_new (ModestWidgetFactory *factory,
+                           ModestEditType type, TnyMsg *msg)
 {
        GObject *obj;
        ModestEditMsgWindowPrivate *priv;
 
-       g_return_val_if_fail (conf, NULL);
        g_return_val_if_fail (factory, NULL);
        g_return_val_if_fail (type < MODEST_EDIT_TYPE_NUM, NULL);
        g_return_val_if_fail (!(type==MODEST_EDIT_TYPE_NEW && msg), NULL); 
@@ -414,9 +452,6 @@ modest_edit_msg_window_new (ModestConf *conf, ModestWidgetFactory *factory,
        obj = g_object_new(MODEST_TYPE_EDIT_MSG_WINDOW, NULL);
        priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
 
-       g_object_ref (G_OBJECT(conf));
-       priv->conf = conf;
-
        g_object_ref (factory);
        priv->factory = factory;
 
@@ -431,5 +466,31 @@ modest_edit_msg_window_new (ModestConf *conf, ModestWidgetFactory *factory,
        g_signal_connect (G_OBJECT(obj), "delete-event",
                          G_CALLBACK(on_delete_event), obj);
        
+       if (msg) {
+               /* Testing code. Should be into a set_msg method */
+               TnyHeader *header;
+               GtkTextBuffer *buf;
+
+               header = tny_msg_get_header (msg);
+               gtk_entry_set_text (GTK_ENTRY(priv->to_field),
+                                   tny_header_get_to (header));
+               gtk_entry_set_text (GTK_ENTRY(priv->cc_field),
+                                   tny_header_get_cc (header));
+               gtk_entry_set_text (GTK_ENTRY(priv->bcc_field),
+                                   tny_header_get_bcc (header));
+               gtk_entry_set_text (GTK_ENTRY(priv->subject_field),
+                                   tny_header_get_subject (header));   
+
+               buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body));
+               gtk_text_buffer_set_text (buf,
+                                         (const gchar *) modest_tny_msg_actions_find_body (msg, TRUE),
+                                         -1);
+
+               /* TODO: lower priority, select in the From: combo to
+                  the value that comes from msg */
+
+               /* TODO: set attachments */
+       }
+
        return GTK_WIDGET (obj);
 }