1 # pragma once
2 
3 # include <iostream>
4 # include <vector>
5 # include <string>
6 # include <functional>
7 # include <memory>
8 # include <boost/filesystem.hpp>
9 # include <thread>
10 # include <mutex>
11 # include <condition_variable>
12 
13 # include <gmime/gmime.h>
14 
15 # include "astroid.hh"
16 # include "proto.hh"
17 
18 namespace bfs = boost::filesystem;
19 
20 namespace Astroid {
21 
22   class ComposeMessage : public sigc::trackable {
23     public:
24       ComposeMessage  ();
25       ~ComposeMessage ();
26 
27       GMimeMessage    * message = NULL;
28       Account         * account = NULL;
29       InternetAddress * from    = NULL;
30 
31       ustring to, cc, bcc, id, subject, references, inreplyto;
32 
33       std::ostringstream body;
34 
35       void set_from (Account *);
36       void set_to   (ustring);
37       void set_cc   (ustring);
38       void set_bcc  (ustring);
39       void set_subject (ustring);
40       void set_id   (ustring);
41       void set_inreplyto (ustring);
42       void set_references (ustring);
43 
44       bool include_signature = false;
45       bool markdown = false;
46       bool encrypt = false;
47       bool sign    = false;
48 
49       bool markdown_success = false;
50       ustring markdown_error = "";
51 
52       struct Attachment {
53         public:
54           Attachment ();
55           Attachment (bfs::path);
56           Attachment (refptr<Chunk>);
57           Attachment (refptr<Message>);
58           ~Attachment ();
59           ustring name;
60           bfs::path    fname;
61           bool    is_mime_message = false;
62           bool    dispostion_inline = false;
63           bool    valid;
64 
65           refptr<Glib::ByteArray> contents;
66           std::string             content_type;
67           refptr<Message>         message;
68 
69           int     chunk_id = -1;
70       };
71 
72       void add_attachment (std::shared_ptr<Attachment>);
73       std::vector<std::shared_ptr<Attachment>> attachments;
74 
75       void load_message (ustring, ustring); // load draft or message as new
76 
77       void build ();    // call to build message from content
78       void finalize (); // call before sending
79       bool send ();
80       void send_threaded ();
81       bool cancel_sending ();
82       ustring write_tmp (); // write message to tmpfile
83       void write (ustring); // write message to some file
84       void write (GMimeStream *); // write to stream
85 
86       /* encryption */
87       bool encryption_success = false;
88       ustring encryption_error = "";
89 
90     private:
91       ustring message_file;
92       bfs::path save_to;
93       bool      dryrun;
94 
95       /* sendmail process */
96       bool cancel_send_during_delay = false;
97       int pid;
98 
99       std::thread send_thread;
100       std::mutex  send_cancel_m;
101       std::condition_variable  send_cancel_cv;
102 
103     public:
104       /* message sent */
105       typedef sigc::signal <void, bool> type_message_sent;
106       type_message_sent message_sent ();
107 
108       void emit_message_sent (bool);
109 
110       bool message_sent_result;
111       void message_sent_event ();
112       Glib::Dispatcher d_message_sent;
113 
114       /* message send status update */
115       typedef sigc::signal <void, bool, ustring> type_message_send_status;
116       type_message_send_status message_send_status ();
117 
118       void emit_message_send_status (bool, ustring);
119 
120       bool    message_send_status_warn = false;
121       ustring message_send_status_msg = "";
122 
123       void message_send_status_event ();
124       Glib::Dispatcher d_message_send_status;
125 
126     protected:
127       type_message_sent m_message_sent;
128       type_message_send_status m_message_send_status;
129   };
130 }
131