/
edit_message.cc
1187 lines (891 loc) · 31.6 KB
/
edit_message.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include <iostream>
# include <vector>
# include <algorithm>
# include <random>
# include <ctime>
# include <memory>
# include <gtkmm.h>
# include <gdk/gdkx.h>
# include <gtkmm/socket.h>
# include <boost/filesystem.hpp>
# include "astroid.hh"
# include "config.hh"
# include "account_manager.hh"
# include "edit_message.hh"
# include "compose_message.hh"
# include "db.hh"
# include "thread_view/thread_view.hh"
# include "raw_message.hh"
# include "main_window.hh"
# include "message_thread.hh"
# include "chunk.hh"
# include "utils/utils.hh"
# include "utils/ustring_utils.hh"
# include "utils/resource.hh"
# include "actions/onmessage.hh"
# include "editor/plugin.hh"
# include "editor/external.hh"
using namespace std;
using namespace boost::filesystem;
namespace Astroid {
int EditMessage::edit_id = 0;
EditMessage::EditMessage (MainWindow * mw, ustring _to, ustring _from) :
EditMessage (mw, false) { // {{{
to = _to;
if (!_from.empty ()) {
set_from (Address (_from));
}
/* reload message */
prepare_message ();
read_edited_message ();
edit_when_ready ();
}
EditMessage::EditMessage (MainWindow * mw, refptr<Message> msg) :
EditMessage (mw, false) {
/* load draft */
LOG (info) << "em: loading draft from: " << msg->fname;
/* if this is a previously saved draft: use it, otherwise make new draft
* based on original message */
if (any_of (Db::draft_tags.begin (),
Db::draft_tags.end (),
[&](ustring t) {
return has (msg->tags, t);
}))
{
draft_msg = msg;
msg_id = msg->mid;
}
for (auto &c : msg->mime_messages_and_attachments ()) {
add_attachment (new ComposeMessage::Attachment (c));
}
inreplyto = msg->inreplyto;
references = msg->references;
/* write msg to new tmpfile */
msg->save_to (tmpfile_path.c_str ());
/* reload message */
read_edited_message ();
/* TODO: read encryption / signing / signature state from message properties */
edit_when_ready ();
}
EditMessage::EditMessage (MainWindow * mw, bool _edit_when_ready) : Mode (mw) {
editor_config = astroid->config ("editor");
embed_editor = !editor_config.get<bool> ("external_editor");
save_draft_on_force_quit = editor_config.get <bool> ("save_draft_on_force_quit");
ustring attachment_words_s = editor_config.get<string> ("attachment_words");
attachment_words = VectorUtils::split_and_trim (attachment_words_s.lowercase (), ",");
sort (attachment_words.begin (), attachment_words.end ());
tmpfile_path = astroid->standard_paths ().runtime_dir;
set_label ("New message");
path ui = Resource (false, "ui/edit-message.glade").get_path ();
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(ui.c_str(), "box_message");
builder->get_widget ("box_message", box_message);
builder->get_widget ("from_combo", from_combo);
builder->get_widget ("switch_encrypt", switch_encrypt);
builder->get_widget ("switch_sign", switch_sign);
builder->get_widget ("reply_mode_combo", reply_mode_combo);
builder->get_widget ("fields_revealer", fields_revealer);
builder->get_widget ("reply_revealer", reply_revealer);
builder->get_widget ("encryption_revealer", encryption_revealer);
builder->get_widget ("editor_box", editor_box);
builder->get_widget ("switch_signature", switch_signature);
/*
builder->get_widget ("editor_rev", editor_rev);
builder->get_widget ("thread_rev", thread_rev);
*/
pack_start (*box_message, true, 5);
/* set up message id and random server name for editor */
id = edit_id++;
ustring _mid = "";
msg_time = time(0);
# ifndef DISABLE_PLUGINS
if (!astroid->plugin_manager->astroid_extension->generate_mid (_mid)) {
# endif
char _hostname[1024];
_hostname[1023] = 0;
gethostname (_hostname, 1023);
char _domainname[1024];
_domainname[1023] = 0;
if (getdomainname (_domainname, 1023) < 0) {
*_domainname = '\0';
}
ustring hostname = astroid->config ().get <string> ("mail.message_id_fqdn");
UstringUtils::trim (hostname);
if (hostname.empty ()) {
if (*_hostname != 0) {
hostname = _hostname;
if (*_domainname != 0) {
ustring d (_domainname);
d = UstringUtils::replace (d, "(", ""); // often (none) is returned
d = UstringUtils::replace (d, ")", "");
hostname += ".";
hostname += d;
}
if (hostname.find (".", 0) == std::string::npos) {
/* add a top level domain */
hostname += ".none";
}
} else {
hostname = UstringUtils::random_alphanumeric (10) + ".none";
}
}
ustring user = astroid->config ().get<string> ("mail.message_id_user");
UstringUtils::trim (user);
if (user.empty ()) user = "astroid";
_mid = UstringUtils::random_alphanumeric (10);
_mid = ustring::compose ("%1.%2.%3@%4", msg_time, _mid, user, hostname);
# ifndef DISABLE_PLUGINS
}
# endif
if (msg_id == "") {
msg_id = _mid;
}
LOG (info) << "em: msg id: " << msg_id;
make_tmpfile ();
if (embed_editor) {
editor = new Plugin (this, _mid);
//editor_rev->add (*editor_socket);
editor_box->pack_start (dynamic_cast<Plugin *> (editor)->bin, false, false, 2);
} else {
editor = new External (this);
}
thread_view = Gtk::manage(new ThreadView(main_window));
thread_view->edit_mode = true;
//thread_rev->add (*thread_view);
editor_box->pack_start (*thread_view, false, false, 2);
thread_view->signal_ready().connect (
sigc::mem_fun (this, &EditMessage::on_tv_ready));
thread_view->signal_element_action().connect (
sigc::mem_fun (this, &EditMessage::on_element_action));
/* defaults */
accounts = astroid->accounts;
/* from combobox */
from_store = Gtk::ListStore::create (from_columns);
from_combo->set_model (from_store);
account_no = 0;
for (Account &a : accounts->accounts) {
auto row = *(from_store->append ());
row[from_columns.name_and_address] = a.full_address();
row[from_columns.account] = &a;
if (a.isdefault) {
from_combo->set_active (account_no);
}
account_no++;
}
from_combo->pack_start (from_columns.name_and_address);
/*
from_combo->signal_key_press_event ().connect (
sigc::mem_fun (this, &EditMessage::on_from_combo_key_press));
*/
show_all_children ();
prepare_message ();
sending_in_progress.store (false);
if (!embed_editor) {
thread_view->show ();
gtk_box_set_child_packing (editor_box->gobj (), GTK_WIDGET(thread_view->gobj ()), true, true, 5, GTK_PACK_START);
grab_modal ();
thread_view->grab_focus ();
}
editor_toggle (false);
from_combo->signal_changed().connect (
sigc::mem_fun (this, &EditMessage::on_from_combo_changed));
/* editor->start_editor_when_ready = true; */
switch_signature->property_active().signal_changed ().connect (
sigc::mem_fun (*this, &EditMessage::switch_signature_set));
switch_encrypt->property_active().signal_changed ().connect (
sigc::mem_fun (*this, &EditMessage::switch_signature_set));
switch_sign->property_active().signal_changed ().connect (
sigc::mem_fun (*this, &EditMessage::switch_signature_set));
reset_signature ();
/* register keys {{{ */
keys.title = "Edit mode";
keys.register_key (Key (GDK_KEY_Return), { Key (GDK_KEY_KP_Enter) },
"edit_message.edit",
"Edit message in editor",
[&] (Key) {
if (!message_sent && !sending_in_progress.load()) {
editor_toggle (true);
}
return true;
});
keys.register_key ("y", "edit_message.send",
"Send message",
[&] (Key) {
if (!message_sent && !sending_in_progress.load()) {
if (editor->started ()) {
thread_view->set_warning (thread_view->focused_message, "Cannot send message when editing.");
return true;
}
if (!check_fields ()) {
/* warning str is set in check_fields () */
LOG (error) << "em: error problem with some of the input fields..";
return true;
}
ask_yes_no ("Really send message?", [&](bool yes){ if (yes) send_message (); });
}
return true;
});
keys.register_key ("C-c", "edit_message.cancel",
"Cancel sending message (unreliable)",
[&] (Key) {
if (!message_sent && sending_in_progress.load ()) {
sending_message->cancel_sending ();
/* send_message_finished will still be called to clean up sending_message */
}
return true;
});
keys.register_key ("V", "edit_message.view_raw",
"View raw message",
[&] (Key) {
/* view raw source of to be sent message */
ComposeMessage * c = make_message ();
ustring tmpf = c->write_tmp ();
main_window->add_mode (new RawMessage (main_window, tmpf.c_str(), true));
/* tmp file deleted by RawMessage */
delete c;
return true;
});
keys.register_key ("f", "edit_message.cycle_from",
"Cycle through From selector",
[&] (Key) {
if (editor->started ()) {
thread_view->set_warning (thread_view->focused_message, "Cannot change from address when editing.");
return true;
}
/* cycle through from combo box */
if (!message_sent && !sending_in_progress.load()) {
if (from_store->children().size() > 1) {
int i = from_combo->get_active_row_number ();
if (i >= (static_cast<int>(from_store->children().size())-1)) i = 0;
else i++;
from_combo->set_active (i);
reset_signature ();
}
}
return true;
});
keys.register_key ("a", "edit_message.attach",
"Attach file",
[&] (Key) {
attach_file ();
return true;
});
keys.register_key ("s", "edit_message.save_draft",
"Save draft",
[&] (Key) {
if (editor->started ()) {
thread_view->set_warning (thread_view->focused_message, "Cannot save draft when editing.");
return true;
}
if (sending_in_progress.load ()) {
/* block closing the window while sending */
LOG (error) << "em: message is being sent, it cannot be saved as draft anymore.";
} else {
bool r;
r = save_draft ();
if (!r) {
on_tv_ready ();
} else {
close (true);
}
}
return true;
});
keys.register_key ("D", "edit_message.delete_draft",
"Delete draft",
[&] (Key) {
if (!draft_msg) {
LOG (debug) << "em: not a draft.";
return true;
}
if (sending_in_progress.load ()) {
/* block closing the window while sending */
LOG (error) << "em: message is being sent, cannot delete draft now. it will be deleted upon successfully sent message.";
} else if (!message_sent) {
ask_yes_no ("Do you want to delete this draft and close it? (any changes will be lost)",
[&](bool yes) {
if (yes) {
delete_draft ();
close (true);
}
});
} else {
// message has been sent successfully, no need to complain.
close ();
}
return true;
});
keys.register_key ("S", "edit_message.toggle_signature",
"Toggle signature",
[&] (Key) {
auto iter = from_combo->get_active ();
auto row = *iter;
Account * a = row[from_columns.account];
if (a->has_signature) {
switch_signature->set_state (!switch_signature->get_active ());
}
return true;
});
keys.register_key ("E", "edit_message.toggle_encrypt",
"Toggle encryption and signature",
[&] (Key) {
auto iter = from_combo->get_active ();
if (!iter) {
return true;
}
auto row = *iter;
Account * a = row[from_columns.account];
if (a->has_gpg) {
if (!switch_encrypt->get_active () && !switch_sign->get_active ()) {
switch_encrypt->set_active (false);
switch_sign->set_active (true);
} else if (!switch_encrypt->get_active() && switch_sign->get_active ()) {
switch_encrypt->set_active (true);
switch_sign->set_active (true);
} else if (switch_encrypt->get_active () && switch_sign->get_active ()) {
switch_encrypt->set_active (true);
switch_sign->set_active (false);
} else {
switch_encrypt->set_active (false);
switch_sign->set_active (false);
}
}
return true;
});
// }}}
if (_edit_when_ready) edit_when_ready ();
} // }}}
void EditMessage::edit_when_ready () {
if (!embed_editor) {
editor_toggle (true);
} else {
if (!editor->ready ()) editor->start_editor_when_ready = true;
else editor_toggle (true);
}
}
EditMessage::~EditMessage () {
LOG (debug) << "em: deconstruct.";
if (status_icon_visible) {
main_window->notebook.remove_widget (&message_sending_status_icon);
}
if (editor->started ()) {
editor->stop ();
}
if (is_regular_file (tmpfile_path)) {
boost::filesystem::remove (tmpfile_path);
}
}
void EditMessage::close (bool force) {
if (sending_in_progress.load ()) {
/* block closing the window while sending */
} else if (!force && !message_sent && !draft_saved) {
if (editor->started ()) {
thread_view->set_warning (thread_view->focused_message, "Cannot close when editing.");
return;
}
ask_yes_no ("Do you want to close this message? (unsaved changes will be lost)", [&](bool yes){ if (yes) { Mode::close (force); } });
} else if (force && !message_sent && !draft_saved && save_draft_on_force_quit) {
LOG (warn) << "em: force quit, trying to save draft..";
bool r = save_draft ();
if (!r) {
LOG (error) << "em: cannot save draft! check account config. changes will be lost.";
}
Mode::close (force);
} else {
// message has been sent successfully, no need to complain.
Mode::close (force);
}
}
/* drafts */
bool EditMessage::save_draft () {
LOG (info) << "em: saving draft..";
draft_saved = false;
ComposeMessage * c = make_message ();
ustring fname;
bool add_to_notmuch = false;
if (!draft_msg) {
/* make new message */
path ddir = path(c->account->save_drafts_to.c_str ());
if (!is_directory(ddir)) {
LOG (error) << "em: no draft directory specified!";
warning_str = "draft could not be saved, no suitable draft directory for account specified.";
/* move to key handler:
* warning_str = "draft could not be saved!"; */
/* on_tv_ready (); */
delete c;
return false;
} else {
/* msg_id might come from external client or server */
ddir = ddir / path(Utils::safe_fname (msg_id));
fname = ddir.c_str ();
add_to_notmuch = true;
}
} else {
fname = draft_msg->fname; // overwrite
}
c->write (fname);
LOG (info) << "em: saved draft to: " << fname;
if (add_to_notmuch) {
astroid->actions->doit (refptr<Action> (
new AddDraftMessage (fname)));
}
delete c;
draft_saved = true;
return true;
}
void EditMessage::delete_draft () {
if (draft_msg) {
delete_draft (draft_msg);
draft_msg = refptr<Message> ();
draft_saved = true; /* avoid saving on exit */
} else {
LOG (warn) << "em: not a draft, not deleting.";
}
}
void EditMessage::delete_draft (refptr<Message> draft_msg) {
LOG (info) << "em: deleting draft.";
path fname = path(draft_msg->fname.c_str());
if (is_regular_file (fname)) {
boost::filesystem::remove (fname);
/* first remove tag in case it has been sent */
astroid->actions->doit (refptr<Action>(
new OnMessageAction (draft_msg->mid, draft_msg->tid,
[fname] (Db * db, notmuch_message_t * msg) {
for (ustring t : Db::draft_tags) {
notmuch_message_remove_tag (msg,
t.c_str ());
}
bool persists = !db->remove_message (fname.c_str ());
if (persists && db->maildir_synchronize_flags) {
/* sync in case there are other copies of the message */
notmuch_message_tags_to_maildir_flags (msg);
}
})));
}
}
/* edit / read message cycling {{{ */
void EditMessage::on_from_combo_changed () {
prepare_message ();
read_edited_message ();
}
void EditMessage::prepare_message () {
LOG (debug) << "em: preparing message from fields..";
auto iter = from_combo->get_active ();
if (!iter) {
LOG (warn) << "em: error: no from account selected.";
return;
}
auto row = *iter;
Account * a = row[from_columns.account];
auto from_ia = internet_address_mailbox_new (a->name.c_str(), a->email.c_str());
ustring from = internet_address_to_string (from_ia, true);
tmpfile.open (tmpfile_path.c_str(), std::fstream::out);
tmpfile << "From: " << from << endl;
tmpfile << "To: " << to << endl;
tmpfile << "Cc: " << cc << endl;
if (bcc.size() > 0) {
tmpfile << "Bcc: " << bcc << endl;
}
tmpfile << "Subject: " << subject << endl;
tmpfile << endl;
tmpfile << body.raw();
tmpfile.close ();
LOG (debug) << "em: prepare message done.";
}
void EditMessage::set_from (Address a) {
if (!accounts->is_me (a)) {
LOG (error) << "em: from address is not a defined account.";
}
set_from (accounts->get_account_for_address (a));
}
void EditMessage::set_from (Account * a) {
int rn = from_combo->get_active_row_number ();
account_no = 0;
for (Gtk::TreeRow row : from_store->children ()) {
if (row[from_columns.account] == a) {
from_combo->set_active (row);
break;
}
account_no ++;
}
bool same_account = (rn == from_combo->get_active_row_number ());
LOG (debug) << "same account: " << same_account;
if (!same_account) {
reset_signature ();
}
}
void EditMessage::reset_signature () {
/* should not be run unless the account has been changed */
auto it = from_combo->get_active ();
Account * a = (*it)[from_columns.account];
switch_signature->set_sensitive (a->has_signature);
switch_signature->set_state (a->has_signature && a->signature_default_on);
encryption_revealer->set_reveal_child (a->has_gpg);
switch_sign->set_sensitive (a->has_gpg);
switch_encrypt->set_sensitive (a->has_gpg);
switch_encrypt->set_state (false);
switch_sign->set_state (a->has_gpg && a->always_gpg_sign);
}
void EditMessage::switch_signature_set () {
LOG (debug) << "got sig: " << switch_signature->get_active ();
prepare_message ();
read_edited_message ();
}
void EditMessage::read_edited_message () {
/* make message */
draft_saved = false; // we expect changes to have been made
warning_str = "";
if (astroid->hint_level () < 1) {
info_str = "Edit message with 'Enter'.";
} else {
info_str = "";
}
ComposeMessage * c = make_message ();
if (c == NULL) {
LOG (error) << "err: could not make message.";
return;
}
if (c->encrypt || c->sign) {
if (!c->encryption_success) {
warning_str = "Failed encrypting: " + UstringUtils::replace (c->encryption_error, "\n", "<br />");
}
}
/* set account selector to from address email */
set_from (c->account);
to = c->to;
cc = c->cc;
bcc = c->bcc;
references = c->references;
inreplyto = c->inreplyto;
subject = c->subject;
body = ustring(c->body.str());
ustring tmpf = c->write_tmp ();
auto msgt = refptr<MessageThread>(new MessageThread());
msgt->add_message (tmpf);
thread_view->load_message_thread (msgt);
delete c;
unlink (tmpf.c_str());
}
/* }}} */
void EditMessage::on_tv_ready () {
LOG (debug) << "em: got tv ready.";
if (warning_str.length() > 0)
thread_view->set_warning (thread_view->focused_message, warning_str);
else
thread_view->hide_warning (thread_view->focused_message);
if (info_str.length () > 0)
thread_view->set_info (thread_view->focused_message, info_str);
else
thread_view->hide_info (thread_view->focused_message);
}
/* swapping between edit and read mode {{{ */
void EditMessage::fields_hide () {
fields_revealer->set_reveal_child (false);
}
void EditMessage::fields_show () {
fields_revealer->set_reveal_child (true);
}
/* turn on or off the editor or set up for the editor
* only run for embedded editor */
void EditMessage::editor_toggle (bool on) {
LOG (debug) << "em: editor toggle: " << on;
if (embed_editor) {
if (on) {
prepare_message ();
editor_active = true;
/*
editor_rev->set_reveal_child (true);
thread_rev->set_reveal_child (false);
*/
dynamic_cast<Plugin *> (editor)->bin.show ();
thread_view->hide ();
gtk_box_set_child_packing (editor_box->gobj (), GTK_WIDGET(dynamic_cast<Plugin *> (editor)->bin.gobj ()), true, true, 5, GTK_PACK_START);
gtk_box_set_child_packing (editor_box->gobj (), GTK_WIDGET(thread_view->gobj ()), false, false, 5, GTK_PACK_START);
/* future Gtk
editor_box->set_child_packing (editor_rev, true, true, 2);
editor_box->set_child_packing (thread_rev, false, false, 2);
*/
fields_hide ();
editor->start ();
} else {
editor_active = false;
if (editor->started ()) {
editor->stop ();
}
/*
editor_rev->set_reveal_child (false);
thread_rev->set_reveal_child (true);
*/
dynamic_cast<Plugin *> (editor)->bin.hide ();
thread_view->show ();
gtk_box_set_child_packing (editor_box->gobj (), GTK_WIDGET(dynamic_cast<Plugin *>(editor)->bin.gobj ()), false, false, 5, GTK_PACK_START);
gtk_box_set_child_packing (editor_box->gobj (), GTK_WIDGET(thread_view->gobj ()), true, true, 5, GTK_PACK_START);
/* future Gtk
editor_box->set_child_packing (editor_rev, false, false, 2);
editor_box->set_child_packing (thread_rev, true, true, 2);
*/
fields_show ();
read_edited_message ();
grab_modal ();
thread_view->grab_focus ();
}
} else {
if (on && !editor->started ()) {
/* start editor */
editor_active = true;
prepare_message ();
/* TODO: race condition: info_str is set by read_edited_message, may
* happen in conflict with when the thread_view emits the ready
* signal (race will also be in external.cc) */
editor->start ();
read_edited_message ();
info_str = "Editing..";
} else {
/* return from editor */
editor_active = false;
info_str = "";
read_edited_message ();
}
}
}
void EditMessage::activate_editor () {
/* used by Plugin */
LOG (debug) << "em: activate editor.";
editor_active = true;
LOG (debug) << "em: activate editor.";
if (!editor->ready ()) {
LOG (warn) << "em: activate editor: not ready.";
return;
}
/*
* https://bugzilla.gnome.org/show_bug.cgi?id=729248
*/
release_modal ();
if (editor->ready ()) {
editor->focus ();
} else {
LOG (warn) << "em: activate editor, editor not yet started!";
}
}
/* }}} */
void EditMessage::on_element_action (int id, ThreadView::ElementAction action) {
if (sending_in_progress.load ()) return;
if (action == ThreadView::ElementAction::EDelete) {
/* delete attachment */
auto e = thread_view->state[thread_view->focused_message].elements[id];
LOG (info) << "em: remove attachment: " << id << ", cid: " << e.id;
attachments.erase (std::remove_if (attachments.begin (), attachments.end (), [&] (shared_ptr<ComposeMessage::Attachment> &a) { return a->chunk_id == e.id; }));
prepare_message ();
read_edited_message ();
}
}
/* }}} */
/* send message {{{ */
bool EditMessage::check_fields () {
if (to.empty () && cc.empty () && bcc.empty ()) {
warning_str = "No recipients defined!";
on_tv_ready ();
return false;
}
/* check if attachments are mentioned */
if (attachments.empty () && !attachment_words.empty ()) {
ustring bl = body.lowercase ();
if (any_of (attachment_words.begin (),
attachment_words.end (),
[&] (ustring w) {
return bl.find (w) != string::npos;
}))
{
warning_str = "Attachments have been mentioned in the message, but none are attached, do you still want to send?";
on_tv_ready ();
}
}
return true;
}
bool EditMessage::send_message () {
LOG (info) << "em: sending message..";
/* load body */
editor_toggle (false); // resets warning and info
warning_str = "";
info_str = "sending message..";
on_tv_ready ();
ComposeMessage * c = make_message ();
if (c == NULL) return false;
if (c->encrypt || c->sign) {
if (!c->encryption_success) {
warning_str = "Cannot send, failed encrypting: " + UstringUtils::replace (c->encryption_error, "\n", "<br />");
delete c;
return false;
}
}
c->message_sent().connect (
sigc::mem_fun (this, &EditMessage::send_message_finished));
c->message_send_status ().connect (
sigc::mem_fun (this, &EditMessage::update_send_message_status));
fields_hide ();
sending_in_progress.store (true);
/* set message sending icon */
status_icon_visible = true;
Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_default();
Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon (
"mail-send",
Notebook::icon_size,
Gtk::ICON_LOOKUP_USE_BUILTIN | Gtk::ICON_LOOKUP_FORCE_SIZE);
message_sending_status_icon.set (pixbuf);
main_window->notebook.add_widget (&message_sending_status_icon);
c->send_threaded ();
sending_message = c;
return true;
}
void EditMessage::update_send_message_status (bool warn, ustring msg) {
if (warn) {
info_str = "";
warning_str = msg;
} else {
info_str = msg;
warning_str = "";
}
on_tv_ready ();
}
void EditMessage::send_message_finished (bool result_from_sender) {
LOG (info) << "em: message sending done.";
status_icon_visible = true;