Wtx ~ Wt Extension Library
WtxLib
Composer.cpp
1 
2 #include <Wt/WApplication.h>
3 #include <Wt/WPushButton.h>
4 #include <Wt/WContainerWidget.h>
5 #include <Wt/WTable.h>
6 #include <Wt/WText.h>
7 #include <Wt/WLineEdit.h>
8 #include <Wt/WComboBox.h>
9 #include <Wt/WImage.h>
10 #include <Wt/WFileUpload.h>
11 #include <Wt/WTextArea.h>
12 #include <Wt/WTextEdit.h>
13 
14 #include "Composer.h"
15 #include "AttachmentEditor.h"
16 
17 class DetailForm
18 : public Wt::WContainerWidget
19 {
20  public:
21 
22  DetailForm();
23 
24  void do_send();
25  void do_save();
26  void do_discard();
27  void do_attach();
28 
29  void setMessage( const Wtx::Mail::Message & message );
30 
31  Wt::WLineEdit * m_fromEdit;
32  Wt::WLineEdit * m_subjectEdit;
33 
35  {
36  Wt::WComboBox * recipientType;
37  Wt::WLineEdit * emailAddress;
38  int row;
39  };
40 
41  RecipientEditor & addRecipientEditor();
42  std::vector< RecipientEditor > m_recipientEditors;
43 
45  {
47  int row;
48  };
49 
50  void addAttachmentEditor();
51  std::vector< AttachmentEditor > m_attachmentEditors;
52 
53  void loadMessage();
54 
55  Wtx::Mail::Message m_message;
56  Wt::WTable * m_table;
57  Wt::WTextArea * m_body;
58  Wt::Signal<> m_send;
59  Wt::Signal<> m_discard;
60 
61 }; // endclass DetailForm
62 
63 DetailForm::DetailForm()
64 : Wt::WContainerWidget()
65 {
66  setStyleClass( "darker" );
67  setOverflow( Wt::Overflow::Auto );
68 
69  auto _controlButtons = [=]()
70  {
71  auto cw = addNew< Wt::WContainerWidget >();
72 
73  cw-> setPadding( 5 );
74  auto pbSend = cw-> addNew< Wt::WPushButton >( Wt::WString::tr( "wtx.eml.composer.send" ) );
75  pbSend-> setStyleClass( "default btn-xs" );
76  pbSend-> clicked().connect( [=](){ m_send.emit(); } );
77 
78  auto pbSave = cw-> addNew< Wt::WPushButton >( Wt::WString::tr( "wtx.eml.composer.savenow" ) );
79  pbSave-> setStyleClass( "btn-xs" );
80  pbSave-> clicked().connect( this, &DetailForm::do_save );
81 
82  auto pbDiscard = cw-> addNew< Wt::WPushButton >( Wt::WString::tr( "wtx.eml.composer.discard" ) );
83  pbDiscard-> setStyleClass( "btn-xs" );
84  pbDiscard-> clicked().connect( [=](){ m_discard.emit(); } );
85 
86  return cw;
87  };
88 
89  _controlButtons();
90 
91  m_table = addNew< Wt::WTable >();
92 
93  m_body = addNew< Wt::WTextEdit >();
94 // m_body-> setHeight( "375px" );
95  m_body-> setHeight( "200px" );
96 
97  _controlButtons();
98 
99  loadMessage();
100 
101 } // endDetailForm::DetailForm()
102 
103 void DetailForm::loadMessage()
104 {
105  m_table-> clear();
106  m_recipientEditors.clear();
107 
108  int row = 0;
109 
110  m_table-> setStyleClass( "lighter" );
111  m_table-> resize( Wt::WLength( 100, Wt::LengthUnit::Percentage ), Wt::WLength::Auto );
112  m_table-> elementAt( row, 0 )-> resize( Wt::WLength( 1, Wt::LengthUnit::Percentage ), Wt::WLength::Auto );
113 
114  m_table-> elementAt( row, 0 )-> addNew< Wt::WText >( Wt::WString::tr( "wtx.eml.composer.from" ) );
115  m_fromEdit = m_table-> elementAt( row, 1 )-> addNew< Wt::WLineEdit >();
116  m_fromEdit-> setText( m_message.from().displayName() + " <" + m_message.from().address() + ">" );
117  m_fromEdit-> setReadOnly( true );
118 
119  for( auto recipient : m_message.recipients() )
120  {
121  auto recipientEditor = addRecipientEditor();
122  recipientEditor.emailAddress-> setValueText( recipient.mailbox.address() );
123  }
124 
125  addRecipientEditor();
126 
127  row = m_table-> rowCount();
128  m_table-> elementAt( row, 0 )-> addNew< Wt::WText >( Wt::WString::tr( "wtx.eml.composer.subject" ) );
129  m_subjectEdit = m_table-> elementAt( row, 1 )-> addNew< Wt::WLineEdit >();
130  m_subjectEdit-> setValueText( m_message.subject() );
131 
132  for( auto attachment : m_message.attachments() )
133  {
134  int row = m_table-> rowCount();
135  m_table-> elementAt( row, 0 )-> addNew< Wt::WImage >( "resources/icons/paperclip.png" );
136  m_table-> elementAt( row, 0 )-> setContentAlignment( Wt::AlignmentFlag::Right | Wt::AlignmentFlag::Top );
137  m_table-> elementAt( row, 0 )-> setPadding( 3 );
138 // auto pb = m_table-> elementAt( row, 1 )-> addNew< Wt::WPushButton >( "O" );
139 // pb-> setStyleClass( "btn-xs" );
140  m_table-> elementAt( row, 1 )-> addNew< Wt::WText >( attachment.fileName );
141  }
142 
143  addAttachmentEditor();
144 
145 } // endvoid DetailForm::loadMessage()
146 
147 DetailForm::RecipientEditor & DetailForm::addRecipientEditor()
148 {
149  auto _tcb = [=]()
150  {
151  auto retVal = std::make_unique< Wt::WComboBox >();
152 
153  retVal-> addItem( Wt::WString::tr( "wtx.eml.composer.to" ) );
154  retVal-> addItem( Wt::WString::tr( "wtx.eml.composer.cc" ) );
155  retVal-> addItem( Wt::WString::tr( "wtx.eml.composer.bcc" ) );
156 
157  return retVal;
158  };
159 
160  int row = 0;
161  for( auto recipientEditor : m_recipientEditors )
162  if( row < recipientEditor.row )
163  row = recipientEditor.row;
164  row++;
165 
166  m_table-> insertRow( row );
167 
168  RecipientEditor editor;
169  editor.recipientType = m_table-> elementAt( row, 0 )-> addWidget( _tcb() );
170  editor.emailAddress = m_table-> elementAt( row, 1 )-> addNew< Wt::WLineEdit >();
171  editor.row = row;
172 
173  m_recipientEditors.push_back( editor );
174 
175  return m_recipientEditors.at( m_recipientEditors.size()-1 );
176 
177 } // endDetailForm::RecipientEditor & DetailForm::addRecipientEditor()
178 
179 void DetailForm::addAttachmentEditor()
180 {
181  int row = m_table-> rowCount();
182  m_table-> elementAt( row, 0 )-> addNew< Wt::WImage >( "resources/icons/paperclip.png" );
183  m_table-> elementAt( row, 0 )-> setContentAlignment( Wt::AlignmentFlag::Right | Wt::AlignmentFlag::Top );
184  m_table-> elementAt( row, 0 )-> setPadding( 3 );
185  auto editor = m_table-> elementAt( row, 1 )-> addNew< Wtx::Eml::AttachmentEditor >( m_attachmentEditors.size() );
186 
187  AttachmentEditor attachmentEditor;
188  attachmentEditor.editor = editor;
189  attachmentEditor.row = row;
190  m_attachmentEditors.push_back( attachmentEditor );
191 
192  editor-> attached().connect( [=]()
193  {
194  std::cout << __FILE__ << ":" << __LINE__ << " " << row << std::endl;
195 
196  addAttachmentEditor();
197  });
198 
199  editor-> removed().connect( [=]()
200  {
201  std::cout << __FILE__ << ":" << __LINE__ << " " << row << std::endl;
202 
203  });
204 
205 }
206 
207 void DetailForm::setMessage( const Wtx::Mail::Message & message )
208 {
209  m_message = message;
210 
211  loadMessage();
212 
213 } // endvoid DetailForm::setMessage( const Wt::Mail::Message & message )
214 
215 void DetailForm::do_save()
216 {
217  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
218 
219 }
220 
221 void DetailForm::do_attach()
222 {
223  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
224 
225 }
226 
227 
228 /***************************************************************************/
229 /***************************************************************************/
230 /***************************************************************************/
231 
232 Wtx::Eml::Composer::Composer()
233 {
234  m_impl = setImplementation( std::make_unique< DetailForm >() );
235 }
236 
237 #ifdef DONT_USE_THIS_IT_DOESNT_WORK
238 Wtx::Eml::Composer::Composer( const Wt::Mail::Message & message )
239 {
240  auto u_impl = std::make_unique< DetailForm >();
241  u_impl-> setMessage( message );
242 
243  wApp-> processEvents();
244 
245  m_impl = setImplementation( std::move( u_impl ) );
246 
247 }
248 #endif
249 
250 
251 const Wtx::Mail::Message & Wtx::Eml::Composer::message() const
252 {
253  return dynamic_cast< DetailForm* >( m_impl )-> m_message;
254 }
255 
256 void Wtx::Eml::Composer::setMessage( const Wtx::Mail::Message & message )
257 {
258  dynamic_cast< DetailForm* >( m_impl )-> setMessage( message );
259 }
260 
261 Wt::Signal<> & Wtx::Eml::Composer::send()
262 {
263  return dynamic_cast< DetailForm* >( m_impl )-> m_send;
264 }
265 
266 Wt::Signal<> & Wtx::Eml::Composer::discard()
267 {
268  return dynamic_cast< DetailForm* >( m_impl )-> m_discard;
269 }
270 
271