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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Michael Zucchi <NotZed@ximian.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <glib/gi18n.h>
#include <shell/e-shell.h>
#include <e-util/e-util.h>
/* This is our hack, not part of libcamel. */
#include <libemail-engine/camel-null-store.h>
#include <libemail-engine/e-mail-folder-utils.h>
#include <libemail-engine/e-mail-session.h>
#include <libemail-engine/mail-folder-cache.h>
#include <libemail-engine/mail-mt.h>
#include <libemail-engine/mail-ops.h>
#include <libemail-engine/mail-tools.h>
#include "e-mail-account-store.h"
#include "e-mail-ui-session.h"
#include "em-event.h"
#include "em-filter-rule.h"
#include "em-utils.h"
#include "mail-send-recv.h"
#define d(x)
/* ms between status updates to the gui */
#define STATUS_TIMEOUT (250)
/* pseudo-uri to key the send task on */
#define SEND_URI_KEY "send-task:"
#define SEND_RECV_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
/* send/receive email */
/* ********************************************************************** */
/* This stuff below is independent of the stuff above */
/* this stuff is used to keep track of which folders filters have accessed, and
* what not. the thaw/refreeze thing doesn't really seem to work though */
struct _folder_info {
gchar *uri;
CamelFolder *folder;
time_t update;
/* How many times updated, to slow it
* down as we go, if we have lots. */
gint count;
};
struct _send_data {
GList *infos;
GtkDialog *gd;
gint cancelled;
/* Since we're never asked to update
* this one, do it ourselves. */
CamelFolder *inbox;
time_t inbox_update;
GMutex lock;
GHashTable *folders;
GHashTable *active; /* send_info's by uri */
};
typedef enum {
SEND_RECEIVE, /* receiver */
SEND_SEND, /* sender */
SEND_UPDATE, /* imap-like 'just update folder info' */
SEND_INVALID
} send_info_t;
typedef enum {
SEND_ACTIVE,
SEND_CANCELLED,
SEND_COMPLETE
} send_state_t;
struct _send_info {
send_info_t type; /* 0 = fetch, 1 = send */
GCancellable *cancellable;
CamelSession *session;
CamelService *service;
gboolean keep_on_server;
send_state_t state;
GtkWidget *progress_bar;
GtkWidget *cancel_button;
gint again; /* need to run send again */
gint timeout_id;
gchar *what;
gint pc;
GtkWidget *send_account_label;
gchar *send_url;
/*time_t update;*/
struct _send_data *data;
};
static CamelFolder *
receive_get_folder (CamelFilterDriver *d,
const gchar *uri,
gpointer data,
GError **error);
static void send_done (gpointer data);
static struct _send_data *send_data = NULL;
static GtkWidget *send_recv_dialog = NULL;
static void
free_folder_info (struct _folder_info *info)
{
/*camel_folder_thaw (info->folder); */
mail_sync_folder (info->folder, FALSE, NULL, NULL);
g_object_unref (info->folder);
g_free (info->uri);
g_free (info);
}
static void
free_send_info (struct _send_info *info)
{
if (info->cancellable != NULL)
g_object_unref (info->cancellable);
if (info->session != NULL)
g_object_unref (info->session);
if (info->service != NULL)
g_object_unref (info->service);
if (info->timeout_id != 0)
g_source_remove (info->timeout_id);
g_free (info->what);
g_free (info->send_url);
g_free (info);
}
static struct _send_data *
setup_send_data (EMailSession *session)
{
struct _send_data *data;
if (send_data == NULL) {
send_data = data = g_malloc0 (sizeof (*data));
g_mutex_init (&data->lock);
data->folders = g_hash_table_new_full (
g_str_hash, g_str_equal,
(GDestroyNotify) NULL,
(GDestroyNotify) free_folder_info);
data->inbox =
e_mail_session_get_local_folder (
session, E_MAIL_LOCAL_FOLDER_LOCAL_INBOX);
g_object_ref (data->inbox);
data->active = g_hash_table_new_full (
g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) free_send_info);
}
return send_data;
}
static void
receive_cancel (GtkButton *button,
struct _send_info *info)
{
if (info->state == SEND_ACTIVE) {
g_cancellable_cancel (info->cancellable);
if (info->progress_bar != NULL)
gtk_progress_bar_set_text (
GTK_PROGRESS_BAR (info->progress_bar),
_("Canceling..."));
info->state = SEND_CANCELLED;
}
if (info->cancel_button)
gtk_widget_set_sensitive (info->cancel_button, FALSE);
}
static void
free_send_data (void)
{
struct _send_data *data = send_data;
g_return_if_fail (g_hash_table_size (data->active) == 0);
if (data->inbox) {
mail_sync_folder (data->inbox, FALSE, NULL, NULL);
/*camel_folder_thaw (data->inbox); */
g_object_unref (data->inbox);
}
g_list_free (data->infos);
g_hash_table_destroy (data->active);
g_hash_table_destroy (data->folders);
g_mutex_clear (&data->lock);
g_free (data);
send_data = NULL;
}
static void
cancel_send_info (gpointer key,
struct _send_info *info,
gpointer data)
{
receive_cancel (GTK_BUTTON (info->cancel_button), info);
}
static void
hide_send_info (gpointer key,
struct _send_info *info,
gpointer data)
{
info->cancel_button = NULL;
info->progress_bar = NULL;
if (info->timeout_id != 0) {
g_source_remove (info->timeout_id);
info->timeout_id = 0;
}
}
static void
dialog_destroy_cb (struct _send_data *data,
GObject *deadbeef)
{
g_hash_table_foreach (data->active, (GHFunc) hide_send_info, NULL);
data->gd = NULL;
send_recv_dialog = NULL;
}
static void
dialog_response (GtkDialog *gd,
gint button,
struct _send_data *data)
{
switch (button) {
case GTK_RESPONSE_CANCEL:
d (printf ("cancelled whole thing\n"));
if (!data->cancelled) {
data->cancelled = TRUE;
g_hash_table_foreach (data->active, (GHFunc) cancel_send_info, NULL);
}
gtk_dialog_set_response_sensitive (gd, GTK_RESPONSE_CANCEL, FALSE);
break;
default:
d (printf ("hiding dialog\n"));
g_hash_table_foreach (data->active, (GHFunc) hide_send_info, NULL);
data->gd = NULL;
/*gtk_widget_destroy((GtkWidget *)gd);*/
break;
}
}
static GMutex status_lock;
static gchar *format_service_name (CamelService *service);
static gint
operation_status_timeout (gpointer data)
{
struct _send_info *info = data;
if (info->progress_bar) {
GtkProgressBar *progress_bar;
g_mutex_lock (&status_lock);
progress_bar = GTK_PROGRESS_BAR (info->progress_bar);
gtk_progress_bar_set_fraction (progress_bar, info->pc / 100.0);
if (info->what != NULL)
gtk_progress_bar_set_text (progress_bar, info->what);
if (info->service != NULL && info->send_account_label) {
gchar *tmp = format_service_name (info->service);
gtk_label_set_markup (
GTK_LABEL (info->send_account_label), tmp);
g_free (tmp);
}
g_mutex_unlock (&status_lock);
return TRUE;
}
return FALSE;
}
static void
set_send_status (struct _send_info *info,
const gchar *desc,
gint pc)
{
g_mutex_lock (&status_lock);
g_free (info->what);
info->what = g_strdup (desc);
info->pc = pc;
g_mutex_unlock (&status_lock);
}
static void
set_transport_service (struct _send_info *info,
const gchar *transport_uid)
{
CamelService *service;
g_mutex_lock (&status_lock);
service = camel_session_ref_service (info->session, transport_uid);
if (CAMEL_IS_TRANSPORT (service)) {
if (info->service != NULL)
g_object_unref (info->service);
info->service = g_object_ref (service);
}
if (service != NULL)
g_object_unref (service);
g_mutex_unlock (&status_lock);
}
/* for camel operation status */
static void
operation_status (CamelOperation *op,
const gchar *what,
gint pc,
struct _send_info *info)
{
set_send_status (info, what, pc);
}
static gchar *
format_service_name (CamelService *service)
{
CamelProvider *provider;
CamelSettings *settings;
gchar *service_name = NULL;
const gchar *display_name;
gchar *pretty_url = NULL;
gchar *host = NULL;
gchar *path = NULL;
gchar *user = NULL;
gchar *cp;
gboolean have_host = FALSE;
gboolean have_path = FALSE;
gboolean have_user = FALSE;
provider = camel_service_get_provider (service);
display_name = camel_service_get_display_name (service);
settings = camel_service_ref_settings (service);
if (CAMEL_IS_NETWORK_SETTINGS (settings)) {
host = camel_network_settings_dup_host (
CAMEL_NETWORK_SETTINGS (settings));
have_host = (host != NULL) && (*host != '\0');
user = camel_network_settings_dup_user (
CAMEL_NETWORK_SETTINGS (settings));
have_user = (user != NULL) && (*user != '\0');
}
if (CAMEL_IS_LOCAL_SETTINGS (settings)) {
path = camel_local_settings_dup_path (
CAMEL_LOCAL_SETTINGS (settings));
have_path = (path != NULL) && (*path != '\0');
}
g_object_unref (settings);
/* Shorten user names with '@', since multiple '@' in a
* 'user@host' label look weird. This is just supposed
* to be a hint anyway so it doesn't matter if it's not
* strictly correct. */
if (have_user && (cp = strchr (user, '@')) != NULL)
*cp = '\0';
g_return_val_if_fail (provider != NULL, NULL);
/* This should never happen, but if the service has no
* display name, fall back to the generic service name. */
if (display_name == NULL || *display_name == '\0') {
service_name = camel_service_get_name (service, TRUE);
display_name = service_name;
}
if (have_host && have_user) {
pretty_url = g_markup_printf_escaped (
"<b>%s</b> <small>(%s@%s)</small>",
display_name, user, host);
} else if (have_host) {
pretty_url = g_markup_printf_escaped (
"<b>%s</b> <small>(%s)</small>",
display_name, host);
} else if (have_path) {
pretty_url = g_markup_printf_escaped (
"<b>%s</b> <small>(%s)</small>",
display_name, path);
} else {
pretty_url = g_markup_printf_escaped (
"<b>%s</b>", display_name);
}
g_free (service_name);
g_free (host);
g_free (path);
g_free (user);
return pretty_url;
}
static send_info_t
get_receive_type (CamelService *service)
{
CamelURL *url;
CamelProvider *provider;
const gchar *uid;
gboolean is_local_delivery;
/* Disregard CamelNullStores. */
if (CAMEL_IS_NULL_STORE (service))
return SEND_INVALID;
url = camel_service_new_camel_url (service);
is_local_delivery = em_utils_is_local_delivery_mbox_file (url);
camel_url_free (url);
/* mbox pointing to a file is a 'Local delivery'
* source which requires special processing. */
if (is_local_delivery)
return SEND_RECEIVE;
provider = camel_service_get_provider (service);
if (provider == NULL)
return SEND_INVALID;
/* skip some well-known services */
uid = camel_service_get_uid (service);
if (g_strcmp0 (uid, E_MAIL_SESSION_LOCAL_UID) == 0)
return SEND_INVALID;
if (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0)
return SEND_INVALID;
if (provider->object_types[CAMEL_PROVIDER_STORE]) {
if (provider->flags & CAMEL_PROVIDER_IS_STORAGE)
return SEND_UPDATE;
else
return SEND_RECEIVE;
}
if (provider->object_types[CAMEL_PROVIDER_TRANSPORT])
return SEND_SEND;
return SEND_INVALID;
}
static gboolean
get_keep_on_server (CamelService *service)
{
GObjectClass *class;
CamelSettings *settings;
gboolean keep_on_server = FALSE;
settings = camel_service_ref_settings (service);
class = G_OBJECT_GET_CLASS (settings);
/* XXX This is a POP3-specific setting. */
if (g_object_class_find_property (class, "keep-on-server") != NULL)
g_object_get (
settings, "keep-on-server",
&keep_on_server, NULL);
g_object_unref (settings);
return keep_on_server;
}
static struct _send_data *
build_dialog (GtkWindow *parent,
EMailSession *session,
CamelFolder *outbox,
CamelService *transport,
gboolean allow_send)
{
GtkDialog *gd;
GtkWidget *wgrid;
GtkGrid *grid;
gint row;
GList *list = NULL;
struct _send_data *data;
GtkWidget *container;
GtkWidget *send_icon;
GtkWidget *recv_icon;
GtkWidget *scrolled_window;
GtkWidget *label;
GtkWidget *progress_bar;
GtkWidget *cancel_button;
EMailAccountStore *account_store;
struct _send_info *info;
gchar *pretty_url;
EMEventTargetSendReceive *target;
GQueue queue = G_QUEUE_INIT;
account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (session));
send_recv_dialog = gtk_dialog_new ();
gd = GTK_DIALOG (send_recv_dialog);
gtk_window_set_modal (GTK_WINDOW (send_recv_dialog), FALSE);
gtk_window_set_icon_name (GTK_WINDOW (gd), "mail-send-receive");
gtk_window_set_default_size (GTK_WINDOW (gd), 600, 200);
gtk_window_set_title (GTK_WINDOW (gd), _("Send & Receive Mail"));
gtk_window_set_transient_for (GTK_WINDOW (gd), parent);
e_restore_window (
GTK_WINDOW (gd),
"/org/gnome/evolution/mail/send-recv-window/",
E_RESTORE_WINDOW_SIZE);
gtk_widget_ensure_style ((GtkWidget *) gd);
container = gtk_dialog_get_action_area (gd);
gtk_container_set_border_width (GTK_CONTAINER (container), 6);
container = gtk_dialog_get_content_area (gd);
gtk_container_set_border_width (GTK_CONTAINER (container), 0);
cancel_button = gtk_button_new_with_mnemonic (_("Cancel _All"));
gtk_button_set_image (
GTK_BUTTON (cancel_button),
gtk_image_new_from_stock (
GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON));
gtk_widget_show (cancel_button);
gtk_dialog_add_action_widget (gd, cancel_button, GTK_RESPONSE_CANCEL);
wgrid = gtk_grid_new ();
grid = GTK_GRID (wgrid);
gtk_container_set_border_width (GTK_CONTAINER (grid), 6);
gtk_grid_set_column_spacing (grid, 6);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_container_set_border_width (
GTK_CONTAINER (scrolled_window), 6);
gtk_scrolled_window_set_policy (
GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_widget_set_size_request (scrolled_window, 50, 50);
container = gtk_dialog_get_content_area (gd);
gtk_scrolled_window_add_with_viewport (
GTK_SCROLLED_WINDOW (scrolled_window), wgrid);
gtk_box_pack_start (
GTK_BOX (container), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
/* must bet setup after send_recv_dialog as it may re-trigger send-recv button */
data = setup_send_data (session);
row = 0;
e_mail_account_store_queue_enabled_services (account_store, &queue);
while (!g_queue_is_empty (&queue)) {
CamelService *service;
const gchar *uid;
service = g_queue_pop_head (&queue);
uid = camel_service_get_uid (service);
/* see if we have an outstanding download active */
info = g_hash_table_lookup (data->active, uid);
if (info == NULL) {
send_info_t type = SEND_INVALID;
type = get_receive_type (service);
if (type == SEND_INVALID || type == SEND_SEND)
continue;
info = g_malloc0 (sizeof (*info));
info->type = type;
info->session = g_object_ref (session);
info->service = g_object_ref (service);
info->keep_on_server = get_keep_on_server (service);
info->cancellable = camel_operation_new ();
info->state = allow_send ? SEND_ACTIVE : SEND_COMPLETE;
info->timeout_id = g_timeout_add (
STATUS_TIMEOUT, operation_status_timeout, info);
g_signal_connect (
info->cancellable, "status",
G_CALLBACK (operation_status), info);
g_hash_table_insert (
data->active, g_strdup (uid), info);
list = g_list_prepend (list, info);
} else if (info->progress_bar != NULL) {
/* incase we get the same source pop up again */
continue;
} else if (info->timeout_id == 0)
info->timeout_id = g_timeout_add (
STATUS_TIMEOUT, operation_status_timeout, info);
recv_icon = gtk_image_new_from_icon_name (
"mail-inbox", SEND_RECV_ICON_SIZE);
gtk_widget_set_valign (recv_icon, GTK_ALIGN_START);
pretty_url = format_service_name (service);
label = gtk_label_new (NULL);
gtk_label_set_ellipsize (
GTK_LABEL (label), PANGO_ELLIPSIZE_END);
gtk_label_set_markup (GTK_LABEL (label), pretty_url);
g_free (pretty_url);
progress_bar = gtk_progress_bar_new ();
gtk_progress_bar_set_show_text (
GTK_PROGRESS_BAR (progress_bar), TRUE);
gtk_progress_bar_set_text (
GTK_PROGRESS_BAR (progress_bar),
(info->type == SEND_UPDATE) ?
_("Updating...") : _("Waiting..."));
gtk_widget_set_margin_bottom (progress_bar, 12);
cancel_button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
gtk_widget_set_valign (cancel_button, GTK_ALIGN_END);
gtk_widget_set_margin_bottom (cancel_button, 12);
/* g_object_set(data->label, "bold", TRUE, NULL); */
gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_grid_attach (grid, recv_icon, 0, row, 1, 2);
gtk_grid_attach (grid, label, 1, row, 1, 1);
gtk_grid_attach (grid, progress_bar, 1, row + 1, 1, 1);
gtk_grid_attach (grid, cancel_button, 2, row, 1, 2);
info->progress_bar = progress_bar;
info->cancel_button = cancel_button;
info->data = data;
g_signal_connect (
cancel_button, "clicked",
G_CALLBACK (receive_cancel), info);
row = row + 2;
}
/* we also need gd during emition to be able to catch Cancel All */
data->gd = gd;
target = em_event_target_new_send_receive (
em_event_peek (), wgrid, data, row, EM_EVENT_SEND_RECEIVE);
e_event_emit (
(EEvent *) em_event_peek (), "mail.sendreceive",
(EEventTarget *) target);
/* Skip displaying the SMTP row if we've got no outbox,
* outgoing account or unsent mails. */
if (allow_send && outbox && CAMEL_IS_TRANSPORT (transport)
&& (camel_folder_get_message_count (outbox) -
camel_folder_get_deleted_message_count (outbox)) != 0) {
info = g_hash_table_lookup (data->active, SEND_URI_KEY);
if (info == NULL) {
info = g_malloc0 (sizeof (*info));
info->type = SEND_SEND;
info->session = g_object_ref (session);
info->service = g_object_ref (transport);
info->keep_on_server = FALSE;
info->cancellable = camel_operation_new ();
info->state = SEND_ACTIVE;
info->timeout_id = g_timeout_add (
STATUS_TIMEOUT, operation_status_timeout, info);
g_signal_connect (
info->cancellable, "status",
G_CALLBACK (operation_status), info);
g_hash_table_insert (
data->active, g_strdup (SEND_URI_KEY), info);
list = g_list_prepend (list, info);
} else if (info->timeout_id == 0)
info->timeout_id = g_timeout_add (
STATUS_TIMEOUT, operation_status_timeout, info);
send_icon = gtk_image_new_from_icon_name (
"mail-outbox", SEND_RECV_ICON_SIZE);
gtk_widget_set_valign (send_icon, GTK_ALIGN_START);
pretty_url = format_service_name (transport);
label = gtk_label_new (NULL);
gtk_label_set_ellipsize (
GTK_LABEL (label), PANGO_ELLIPSIZE_END);
gtk_label_set_markup (GTK_LABEL (label), pretty_url);
g_free (pretty_url);
progress_bar = gtk_progress_bar_new ();
gtk_progress_bar_set_show_text (
GTK_PROGRESS_BAR (progress_bar), TRUE);
gtk_progress_bar_set_text (
GTK_PROGRESS_BAR (progress_bar), _("Waiting..."));
gtk_widget_set_margin_bottom (progress_bar, 12);
cancel_button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
gtk_widget_set_valign (cancel_button, GTK_ALIGN_END);
gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_grid_attach (grid, send_icon, 0, row, 1, 2);
gtk_grid_attach (grid, label, 1, row, 1, 1);
gtk_grid_attach (grid, progress_bar, 1, row + 1, 1, 1);
gtk_grid_attach (grid, cancel_button, 2, row, 1, 2);
info->progress_bar = progress_bar;
info->cancel_button = cancel_button;
info->data = data;
info->send_account_label = label;
g_signal_connect (
cancel_button, "clicked",
G_CALLBACK (receive_cancel), info);
}
gtk_widget_show_all (wgrid);
if (parent != NULL)
gtk_widget_show (GTK_WIDGET (gd));
g_signal_connect (
gd, "response",
G_CALLBACK (dialog_response), data);
g_object_weak_ref ((GObject *) gd, (GWeakNotify) dialog_destroy_cb, data);
data->infos = list;
return data;
}
static void
update_folders (gchar *uri,
struct _folder_info *info,
gpointer data)
{
time_t now = *((time_t *) data);
d (printf ("checking update for folder: %s\n", info->uri));
/* let it flow through to the folders every 10 seconds */
/* we back off slowly as we progress */
if (now > info->update + 10 + info->count *5) {
d (printf ("upating a folder: %s\n", info->uri));
/*camel_folder_thaw(info->folder);
camel_folder_freeze (info->folder);*/
info->update = now;
info->count++;
}
}
static void
receive_status (CamelFilterDriver *driver,
enum camel_filter_status_t status,
gint pc,
const gchar *desc,
gpointer data)
{
struct _send_info *info = data;
time_t now = time (NULL);
/* let it flow through to the folder, every now and then too? */
g_hash_table_foreach (info->data->folders, (GHFunc) update_folders, &now);
if (info->data->inbox && now > info->data->inbox_update + 20) {
d (printf ("updating inbox too\n"));
/* this doesn't seem to work right :( */
/*camel_folder_thaw(info->data->inbox);
camel_folder_freeze (info->data->inbox);*/
info->data->inbox_update = now;
}
/* we just pile them onto the port, assuming it can handle it.
* We could also have a receiver port and see if they've been processed
* yet, so if this is necessary its not too hard to add */
/* the mail_gui_port receiver will free everything for us */
switch (status) {
case CAMEL_FILTER_STATUS_START:
case CAMEL_FILTER_STATUS_END:
set_send_status (info, desc, pc);
break;
case CAMEL_FILTER_STATUS_ACTION:
set_transport_service (info, desc);
break;
default:
break;
}
}
/* when receive/send is complete */
static void
receive_done (gint still_more,
gpointer data)
{
struct _send_info *info = data;
const gchar *uid;
uid = camel_service_get_uid (info->service);
g_return_if_fail (uid != NULL);
/* if we've been called to run again - run again */
if (info->type == SEND_SEND && info->state == SEND_ACTIVE && info->again) {
CamelFolder *local_outbox;
local_outbox =
e_mail_session_get_local_folder (
E_MAIL_SESSION (info->session),
E_MAIL_LOCAL_FOLDER_OUTBOX);
g_return_if_fail (CAMEL_IS_TRANSPORT (info->service));
info->again = 0;
mail_send_queue (
E_MAIL_SESSION (info->session),
local_outbox,
CAMEL_TRANSPORT (info->service),
E_FILTER_SOURCE_OUTGOING,
info->cancellable,
receive_get_folder, info,
receive_status, info,
send_done, info);
return;
}
if (info->progress_bar) {
const gchar *text;
gtk_progress_bar_set_fraction (
GTK_PROGRESS_BAR (info->progress_bar), 1.0);
if (info->state == SEND_CANCELLED)
text = _("Canceled");
else {
text = _("Complete");
info->state = SEND_COMPLETE;
}
gtk_progress_bar_set_text (
GTK_PROGRESS_BAR (info->progress_bar), text);
}
if (info->cancel_button)
gtk_widget_set_sensitive (info->cancel_button, FALSE);
/* remove/free this active download */
d (printf ("%s: freeing info %p\n", G_STRFUNC, info));
if (info->type == SEND_SEND) {
gpointer key = NULL, value = NULL;
if (!g_hash_table_lookup_extended (info->data->active, SEND_URI_KEY, &key, &value))
key = NULL;
g_hash_table_steal (info->data->active, SEND_URI_KEY);
g_free (key);
} else {
gpointer key = NULL, value = NULL;
if (!g_hash_table_lookup_extended (info->data->active, uid, &key, &value))
key = NULL;
g_hash_table_steal (info->data->active, uid);
g_free (key);
}
info->data->infos = g_list_remove (info->data->infos, info);
if (g_hash_table_size (info->data->active) == 0) {
if (info->data->gd)
gtk_widget_destroy ((GtkWidget *) info->data->gd);
free_send_data ();
}
free_send_info (info);
}
static void
send_done (gpointer data)
{
receive_done (-1, data);
}
/* although we dont do anythign smart here yet, there is no need for this interface to
* be available to anyone else.
* This can also be used to hook into which folders are being updated, and occasionally
* let them refresh */
static CamelFolder *
receive_get_folder (CamelFilterDriver *d,
const gchar *uri,
gpointer data,
GError **error)
{
struct _send_info *info = data;
CamelFolder *folder;
struct _folder_info *oldinfo;
gpointer oldkey, oldinfoptr;
g_mutex_lock (&info->data->lock);
oldinfo = g_hash_table_lookup (info->data->folders, uri);
g_mutex_unlock (&info->data->lock);
if (oldinfo) {
g_object_ref (oldinfo->folder);
return oldinfo->folder;
}
/* FIXME Not passing a GCancellable here. */
folder = e_mail_session_uri_to_folder_sync (
E_MAIL_SESSION (info->session), uri, 0, NULL, error);
if (!folder)
return NULL;
/* we recheck that the folder hasn't snuck in while we were loading it... */
/* and we assume the newer one is the same, but unref the old one anyway */
g_mutex_lock (&info->data->lock);
if (g_hash_table_lookup_extended (
info->data->folders, uri, &oldkey, &oldinfoptr)) {
oldinfo = (struct _folder_info *) oldinfoptr;
g_object_unref (oldinfo->folder);
oldinfo->folder = folder;
} else {
oldinfo = g_malloc0 (sizeof (*oldinfo));
oldinfo->folder = folder;
oldinfo->uri = g_strdup (uri);
g_hash_table_insert (info->data->folders, oldinfo->uri, oldinfo);
}
g_object_ref (folder);
g_mutex_unlock (&info->data->lock);
return folder;
}
/* ********************************************************************** */
static void
get_folders (CamelStore *store,
GPtrArray *folders,
CamelFolderInfo *info)
{
while (info) {
if (camel_store_can_refresh_folder (store, info, NULL)) {
if ((info->flags & CAMEL_FOLDER_NOSELECT) == 0) {
gchar *folder_uri;
folder_uri = e_mail_folder_uri_build (
store, info->full_name);
g_ptr_array_add (folders, folder_uri);
}
}
get_folders (store, folders, info->child);
info = info->next;
}
}
static void
main_op_cancelled_cb (GCancellable *main_op,
GCancellable *refresh_op)
{
g_cancellable_cancel (refresh_op);
}
struct _refresh_folders_msg {
MailMsg base;
struct _send_info *info;
GPtrArray *folders;
CamelStore *store;
CamelFolderInfo *finfo;
};
static gchar *
refresh_folders_desc (struct _refresh_folders_msg *m)
{
return g_strdup_printf (
_("Checking for new mail at '%s'"),
camel_service_get_display_name (CAMEL_SERVICE (m->store)));
}
static void
refresh_folders_exec (struct _refresh_folders_msg *m,
GCancellable *cancellable,
GError **error)
{
CamelFolder *folder;
gint i;
gboolean success;
GError *local_error = NULL;
gulong handler_id = 0;
if (cancellable)
handler_id = g_signal_connect (
m->info->cancellable, "cancelled",
G_CALLBACK (main_op_cancelled_cb), cancellable);
success = camel_service_connect_sync (
CAMEL_SERVICE (m->store), cancellable, error);
if (!success)
goto exit;
get_folders (m->store, m->folders, m->finfo);
camel_operation_push_message (m->info->cancellable, _("Updating..."));
for (i = 0; i < m->folders->len; i++) {
folder = e_mail_session_uri_to_folder_sync (
E_MAIL_SESSION (m->info->session),
m->folders->pdata[i], 0,
cancellable, &local_error);
if (folder && camel_folder_synchronize_sync (folder, FALSE, cancellable, &local_error))
camel_folder_refresh_info_sync (folder, cancellable, &local_error);
if (local_error != NULL) {
if (!g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
const gchar *account_name = NULL, *full_name;
if (folder) {
CamelStore *store = camel_folder_get_parent_store (folder);
account_name = camel_service_get_display_name (CAMEL_SERVICE (store));
full_name = camel_folder_get_full_name (folder);
} else
full_name = (const gchar *) m->folders->pdata[i];
g_warning (
"Failed to refresh folder '%s%s%s': %s",
account_name ? account_name : "",
account_name ? ": " : "",
full_name, local_error->message);
}
g_clear_error (&local_error);
}
if (folder)
g_object_unref (folder);
if (g_cancellable_is_cancelled (m->info->cancellable) ||
g_cancellable_is_cancelled (cancellable))
break;
if (m->info->state != SEND_CANCELLED)
camel_operation_progress (
m->info->cancellable, 100 * i / m->folders->len);
}
camel_operation_pop_message (m->info->cancellable);
exit:
if (handler_id > 0)
g_signal_handler_disconnect (m->info->cancellable, handler_id);
}
static void
refresh_folders_done (struct _refresh_folders_msg *m)
{
receive_done (-1, m->info);
}
static void
refresh_folders_free (struct _refresh_folders_msg *m)
{
gint i;
for (i = 0; i < m->folders->len; i++)
g_free (m->folders->pdata[i]);
g_ptr_array_free (m->folders, TRUE);
camel_folder_info_free (m->finfo);
g_object_unref (m->store);
}
static MailMsgInfo refresh_folders_info = {
sizeof (struct _refresh_folders_msg),
(MailMsgDescFunc) refresh_folders_desc,
(MailMsgExecFunc) refresh_folders_exec,
(MailMsgDoneFunc) refresh_folders_done,
(MailMsgFreeFunc) refresh_folders_free
};
static void
receive_update_got_folderinfo (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
CamelFolderInfo *info = NULL;
struct _send_info *send_info = user_data;
GError *local_error = NULL;
mail_folder_cache_note_store_finish (
MAIL_FOLDER_CACHE (source_object),
result, &info, &local_error);
/* Ignore cancellations. */
if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
g_warn_if_fail (info != NULL);
g_error_free (local_error);
/* XXX Need to hand this off to an EAlertSink. */
} else if (local_error != NULL) {
g_warn_if_fail (info != NULL);
g_warning ("%s: %s", G_STRFUNC, local_error->message);
g_error_free (local_error);
/* CamelFolderInfo may be NULL even if no error occurred. */
} else if (info != NULL) {
GPtrArray *folders = g_ptr_array_new ();
struct _refresh_folders_msg *m;
m = mail_msg_new (&refresh_folders_info);
m->store = g_object_ref (send_info->service);
m->folders = folders;
m->info = send_info;
m->finfo = info; /* takes ownership */
mail_msg_unordered_push (m);
} else {
receive_done (-1, send_info);
}
}
static void
receive_update_got_store (CamelStore *store,
struct _send_info *info)
{
MailFolderCache *folder_cache;
folder_cache = e_mail_session_get_folder_cache (
E_MAIL_SESSION (info->session));
if (store != NULL) {
CamelProvider *provider;
/* do not update remote stores in offline */
provider = camel_service_get_provider (CAMEL_SERVICE (store));
if (provider && (provider->flags & CAMEL_PROVIDER_IS_REMOTE) != 0 &&
!camel_session_get_online (info->session))
store = NULL;
}
if (store != NULL) {
mail_folder_cache_note_store (
folder_cache, store, info->cancellable,
receive_update_got_folderinfo, info);
} else {
receive_done (-1, info);
}
}
static CamelService *
ref_default_transport (EMailSession *session)
{
ESource *source;
ESourceRegistry *registry;
CamelService *service;
const gchar *extension_name;
const gchar *uid;
registry = e_mail_session_get_registry (session);
source = e_source_registry_ref_default_mail_identity (registry);
if (source == NULL)
return NULL;
extension_name = E_SOURCE_EXTENSION_MAIL_SUBMISSION;
if (e_source_has_extension (source, extension_name)) {
ESourceMailSubmission *extension;
gchar *uid;
extension = e_source_get_extension (source, extension_name);
uid = e_source_mail_submission_dup_transport_uid (extension);
g_object_unref (source);
source = e_source_registry_ref_source (registry, uid);
g_free (uid);
} else {
g_object_unref (source);
source = NULL;
}
if (source == NULL)
return NULL;
uid = e_source_get_uid (source);
service = camel_session_ref_service (CAMEL_SESSION (session), uid);
g_object_unref (source);
return service;
}
static GtkWidget *
send_receive (GtkWindow *parent,
EMailSession *session,
gboolean allow_send)
{
CamelFolder *local_outbox;
CamelService *transport;
struct _send_data *data;
GList *scan, *siter;
if (send_recv_dialog != NULL) {
if (parent != NULL && gtk_widget_get_realized (send_recv_dialog)) {
gtk_window_present (GTK_WINDOW (send_recv_dialog));
}
return send_recv_dialog;
}
transport = ref_default_transport (session);
local_outbox =
e_mail_session_get_local_folder (
session, E_MAIL_LOCAL_FOLDER_OUTBOX);
data = build_dialog (
parent, session, local_outbox, transport, allow_send);
if (transport != NULL)
g_object_unref (transport);
scan = g_list_copy (data->infos);
for (siter = scan; siter != NULL; siter = siter->next) {
struct _send_info *info = siter->data;
if (!CAMEL_IS_SERVICE (info->service))
continue;
switch (info->type) {
case SEND_RECEIVE:
mail_fetch_mail (
CAMEL_STORE (info->service),
CAMEL_FETCH_OLD_MESSAGES, -1,
E_FILTER_SOURCE_INCOMING,
NULL, NULL, NULL,
info->cancellable,
receive_get_folder, info,
receive_status, info,
receive_done, info);
break;
case SEND_SEND:
/* todo, store the folder in info? */
mail_send_queue (
session, local_outbox,
CAMEL_TRANSPORT (info->service),
E_FILTER_SOURCE_OUTGOING,
info->cancellable,
receive_get_folder, info,
receive_status, info,
send_done, info);
break;
case SEND_UPDATE:
receive_update_got_store (
CAMEL_STORE (info->service), info);
break;
default:
break;
}
}
g_list_free (scan);
return send_recv_dialog;
}
GtkWidget *
mail_send_receive (GtkWindow *parent,
EMailSession *session)
{
return send_receive (parent, session, TRUE);
}
GtkWidget *
mail_receive (GtkWindow *parent,
EMailSession *session)
{
return send_receive (parent, session, FALSE);
}
/* We setup the download info's in a hashtable, if we later
* need to build the gui, we insert them in to add them. */
void
mail_receive_service (CamelService *service)
{
struct _send_info *info;
struct _send_data *data;
CamelSession *session;
CamelFolder *local_outbox;
const gchar *uid;
send_info_t type = SEND_INVALID;
g_return_if_fail (CAMEL_IS_SERVICE (service));
uid = camel_service_get_uid (service);
session = camel_service_ref_session (service);
data = setup_send_data (E_MAIL_SESSION (session));
info = g_hash_table_lookup (data->active, uid);
if (info != NULL)
goto exit;
type = get_receive_type (service);
if (type == SEND_INVALID || type == SEND_SEND)
goto exit;
info = g_malloc0 (sizeof (*info));
info->type = type;
info->progress_bar = NULL;
info->session = g_object_ref (session);
info->service = g_object_ref (service);
info->keep_on_server = get_keep_on_server (service);
info->cancellable = camel_operation_new ();
info->cancel_button = NULL;
info->data = data;
info->state = SEND_ACTIVE;
info->timeout_id = 0;
g_signal_connect (
info->cancellable, "status",
G_CALLBACK (operation_status), info);
d (printf ("Adding new info %p\n", info));
g_hash_table_insert (data->active, g_strdup (uid), info);
switch (info->type) {
case SEND_RECEIVE:
mail_fetch_mail (
CAMEL_STORE (service),
CAMEL_FETCH_OLD_MESSAGES, -1,
E_FILTER_SOURCE_INCOMING,
NULL, NULL, NULL,
info->cancellable,
receive_get_folder, info,
receive_status, info,
receive_done, info);
break;
case SEND_SEND:
/* todo, store the folder in info? */
local_outbox =
e_mail_session_get_local_folder (
E_MAIL_SESSION (session),
E_MAIL_LOCAL_FOLDER_OUTBOX);
mail_send_queue (
E_MAIL_SESSION (session),
local_outbox,
CAMEL_TRANSPORT (service),
E_FILTER_SOURCE_OUTGOING,
info->cancellable,
receive_get_folder, info,
receive_status, info,
send_done, info);
break;
case SEND_UPDATE:
receive_update_got_store (CAMEL_STORE (service), info);
break;
default:
g_return_if_reached ();
}
exit:
g_object_unref (session);
}
void
mail_send (EMailSession *session)
{
CamelFolder *local_outbox;
CamelService *service;
struct _send_info *info;
struct _send_data *data;
send_info_t type = SEND_INVALID;
g_return_if_fail (E_IS_MAIL_SESSION (session));
service = ref_default_transport (session);
if (service == NULL)
return;
data = setup_send_data (session);
info = g_hash_table_lookup (data->active, SEND_URI_KEY);
if (info != NULL) {
info->again++;
d (printf ("send of %s still in progress\n", transport->url));
g_object_unref (service);
return;
}
d (printf ("starting non-interactive send of '%s'\n", transport->url));
type = get_receive_type (service);
if (type == SEND_INVALID) {
g_object_unref (service);
return;
}
info = g_malloc0 (sizeof (*info));
info->type = SEND_SEND;
info->progress_bar = NULL;
info->session = g_object_ref (session);
info->service = g_object_ref (service);
info->keep_on_server = FALSE;
info->cancellable = NULL;
info->cancel_button = NULL;
info->data = data;
info->state = SEND_ACTIVE;
info->timeout_id = 0;
d (printf ("Adding new info %p\n", info));
g_hash_table_insert (data->active, g_strdup (SEND_URI_KEY), info);
/* todo, store the folder in info? */
local_outbox =
e_mail_session_get_local_folder (
session, E_MAIL_LOCAL_FOLDER_OUTBOX);
mail_send_queue (
session, local_outbox,
CAMEL_TRANSPORT (service),
E_FILTER_SOURCE_OUTGOING,
info->cancellable,
receive_get_folder, info,
receive_status, info,
send_done, info);
g_object_unref (service);
}
|