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
|
/* Copyright 2016 Software Freedom Conservancy Inc.
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class FacebookService : Object, Spit.Pluggable, Spit.Publishing.Service {
private const string ICON_FILENAME = "facebook.png";
private static Gdk.Pixbuf[] icon_pixbuf_set = null;
public FacebookService(GLib.File resource_directory) {
if (icon_pixbuf_set == null)
icon_pixbuf_set = Resources.load_from_resource
(Resources.RESOURCE_PATH + "/" + ICON_FILENAME);
}
public int get_pluggable_interface(int min_host_interface, int max_host_interface) {
return Spit.negotiate_interfaces(min_host_interface, max_host_interface,
Spit.Publishing.CURRENT_INTERFACE);
}
public unowned string get_id() {
return "org.yorba.shotwell.publishing.facebook";
}
public unowned string get_pluggable_name() {
return "Facebook";
}
public void get_info(ref Spit.PluggableInfo info) {
info.authors = "Lucas Beeler";
info.copyright = _("Copyright 2016 Software Freedom Conservancy Inc.");
info.translators = Resources.TRANSLATORS;
info.version = _VERSION;
info.website_name = Resources.WEBSITE_NAME;
info.website_url = Resources.WEBSITE_URL;
info.is_license_wordwrapped = false;
info.license = Resources.LICENSE;
info.icons = icon_pixbuf_set;
}
public void activation(bool enabled) {
}
public Spit.Publishing.Publisher create_publisher(Spit.Publishing.PluginHost host) {
return new Publishing.Facebook.FacebookPublisher(this, host);
}
public Spit.Publishing.Publisher.MediaType get_supported_media() {
return (Spit.Publishing.Publisher.MediaType.PHOTO |
Spit.Publishing.Publisher.MediaType.VIDEO);
}
}
namespace Publishing.Facebook {
// global parameters for the Facebook publishing plugin -- don't touch these (unless you really,
// truly, deep-down know what you're doing)
public const string SERVICE_NAME = "facebook";
internal const string USER_VISIBLE_NAME = "Facebook";
internal const string DEFAULT_ALBUM_NAME = _("Shotwell Connect");
internal const int EXPIRED_SESSION_STATUS_CODE = 400;
internal class Album {
public string name;
public string id;
public Album(string name, string id) {
this.name = name;
this.id = id;
}
}
internal enum Resolution {
STANDARD,
HIGH;
public string get_name() {
switch (this) {
case STANDARD:
return _("Standard (720 pixels)");
case HIGH:
return _("Large (2048 pixels)");
default:
error("Unknown resolution %s", this.to_string());
}
}
public int get_pixels() {
switch (this) {
case STANDARD:
return 720;
case HIGH:
return 2048;
default:
error("Unknown resolution %s", this.to_string());
}
}
}
internal class PublishingParameters {
public const int UNKNOWN_ALBUM = -1;
public bool strip_metadata;
public Album[] albums;
public int target_album;
public string? new_album_name; // the name of the new album being created during this
// publishing interaction or null if publishing to an existing
// album
public string? privacy_object; // a serialized JSON object encoding the privacy settings of the
// published resources
public Resolution resolution;
public PublishingParameters() {
this.albums = null;
this.privacy_object = null;
this.target_album = UNKNOWN_ALBUM;
this.new_album_name = null;
this.strip_metadata = false;
this.resolution = Resolution.HIGH;
}
public void add_album(string name, string id) {
if (albums == null)
albums = new Album[0];
Album new_album = new Album(name, id);
albums += new_album;
}
public void set_target_album_by_name(string? name) {
if (name == null) {
target_album = UNKNOWN_ALBUM;
return;
}
for (int i = 0; i < albums.length; i++) {
if (albums[i].name == name) {
target_album = i;
return;
}
}
target_album = UNKNOWN_ALBUM;
}
public string? get_target_album_name() {
if (albums == null || target_album == UNKNOWN_ALBUM)
return null;
return albums[target_album].name;
}
public string? get_target_album_id() {
if (albums == null || target_album == UNKNOWN_ALBUM)
return null;
return albums[target_album].id;
}
}
public class FacebookPublisher : Spit.Publishing.Publisher, GLib.Object {
private PublishingParameters publishing_params;
private weak Spit.Publishing.PluginHost host = null;
private Spit.Publishing.ProgressCallback progress_reporter = null;
private weak Spit.Publishing.Service service = null;
private Spit.Publishing.Authenticator authenticator = null;
private bool running = false;
private GraphSession graph_session;
private PublishingOptionsPane? publishing_options_pane = null;
private Uploader? uploader = null;
private string? uid = null;
private string? username = null;
public FacebookPublisher(Spit.Publishing.Service service,
Spit.Publishing.PluginHost host) {
debug("FacebookPublisher instantiated.");
this.service = service;
this.host = host;
this.publishing_params = new PublishingParameters();
this.authenticator =
Publishing.Authenticator.Factory.get_instance().create("facebook",
host);
this.graph_session = new GraphSession();
graph_session.authenticated.connect(on_session_authenticated);
}
private bool get_persistent_strip_metadata() {
return host.get_config_bool("strip_metadata", false);
}
private void set_persistent_strip_metadata(bool strip_metadata) {
host.set_config_bool("strip_metadata", strip_metadata);
}
// Part of the fix for #3232. These have to be
// public so the legacy options pane may use them.
public int get_persistent_default_size() {
return host.get_config_int("default_size", 0);
}
public void set_persistent_default_size(int size) {
host.set_config_int("default_size", size);
}
/*
private void do_test_connection_to_endpoint() {
debug("ACTION: testing connection to Facebook endpoint.");
host.set_service_locked(true);
host.install_static_message_pane(_("Testing connection to Facebook…"));
GraphMessage endpoint_test_message = graph_session.new_endpoint_test();
endpoint_test_message.completed.connect(on_endpoint_test_completed);
endpoint_test_message.failed.connect(on_endpoint_test_error);
graph_session.send_message(endpoint_test_message);
}
*/
private void do_fetch_user_info() {
debug("ACTION: fetching user information.");
host.set_service_locked(true);
host.install_account_fetch_wait_pane();
GraphMessage user_info_message = graph_session.new_query("/me");
user_info_message.completed.connect(on_fetch_user_info_completed);
user_info_message.failed.connect(on_fetch_user_info_error);
graph_session.send_message(user_info_message);
}
private void do_fetch_album_descriptions() {
debug("ACTION: fetching album list.");
host.set_service_locked(true);
host.install_account_fetch_wait_pane();
GraphMessage albums_message = graph_session.new_query("/%s/albums".printf(uid));
albums_message.completed.connect(on_fetch_albums_completed);
albums_message.failed.connect(on_fetch_albums_error);
graph_session.send_message(albums_message);
}
private void do_extract_user_info_from_json(string json) {
debug("ACTION: extracting user info from JSON response.");
try {
Json.Parser parser = new Json.Parser();
parser.load_from_data(json);
Json.Node root = parser.get_root();
Json.Object response_object = root.get_object();
uid = response_object.get_string_member("id");
username = response_object.get_string_member("name");
} catch (Error error) {
host.post_error(new Spit.Publishing.PublishingError.MALFORMED_RESPONSE(error.message));
return;
}
on_user_info_extracted();
}
private void do_extract_albums_from_json(string json) {
debug("ACTION: extracting album info from JSON response.");
try {
Json.Parser parser = new Json.Parser();
parser.load_from_data(json);
Json.Node root = parser.get_root();
Json.Object response_object = root.get_object();
Json.Array album_list = response_object.get_array_member("data");
publishing_params.albums = new Album[0];
for (int i = 0; i < album_list.get_length(); i++) {
Json.Object current_album = album_list.get_object_element(i);
string album_id = current_album.get_string_member("id");
string album_name = current_album.get_string_member("name");
// Note that we are completely ignoring the "can_upload" flag in the list of albums
// that we pulled from facebook eariler -- effectively, we add every album to the
// publishing_params album list regardless of the value of its can_upload flag. In
// the future we may wish to make adding to the publishing_params album list
// conditional on the value of the can_upload flag being true
publishing_params.add_album(album_name, album_id);
}
} catch (Error error) {
host.post_error(new Spit.Publishing.PublishingError.MALFORMED_RESPONSE(error.message));
return;
}
on_albums_extracted();
}
private void do_create_new_album() {
debug("ACTION: creating a new album named \"%s\".\n", publishing_params.new_album_name);
host.set_service_locked(true);
host.install_static_message_pane(_("Creating album…"));
GraphMessage create_album_message = graph_session.new_create_album(
publishing_params.new_album_name, publishing_params.privacy_object);
create_album_message.completed.connect(on_create_album_completed);
create_album_message.failed.connect(on_create_album_error);
graph_session.send_message(create_album_message);
}
private void do_show_publishing_options_pane() {
debug("ACTION: showing publishing options pane.");
host.set_service_locked(false);
Gtk.Builder builder = new Gtk.Builder();
try {
// the trailing get_path() is required, since add_from_file can't cope
// with File objects directly and expects a pathname instead.
builder.add_from_resource (Resources.RESOURCE_PATH + "/" +
"facebook_publishing_options_pane.ui");
} catch (Error e) {
warning("Could not parse UI file! Error: %s.", e.message);
host.post_error(
new Spit.Publishing.PublishingError.LOCAL_FILE_ERROR(
_("A file required for publishing is unavailable. Publishing to Facebook can’t continue.")));
return;
}
publishing_options_pane = new PublishingOptionsPane(username, publishing_params.albums,
host.get_publishable_media_type(), this, builder, get_persistent_strip_metadata(),
authenticator.can_logout());
publishing_options_pane.logout.connect(on_publishing_options_pane_logout);
publishing_options_pane.publish.connect(on_publishing_options_pane_publish);
host.install_dialog_pane(publishing_options_pane,
Spit.Publishing.PluginHost.ButtonMode.CANCEL);
}
private void do_logout() {
debug("ACTION: clearing persistent session information and restaring interaction.");
this.authenticator.logout();
running = false;
start();
}
private void do_add_new_local_album_from_json(string album_name, string json) {
try {
Json.Parser parser = new Json.Parser();
parser.load_from_data(json);
Json.Node root = parser.get_root();
Json.Object response_object = root.get_object();
string album_id = response_object.get_string_member("id");
publishing_params.add_album(album_name, album_id);
} catch (Error error) {
host.post_error(new Spit.Publishing.PublishingError.MALFORMED_RESPONSE(error.message));
return;
}
publishing_params.set_target_album_by_name(album_name);
do_upload();
}
private void on_authenticator_succeeded() {
debug("EVENT: Authenticator login succeeded.");
do_authenticate_session();
}
private void on_authenticator_failed() {
}
private void do_authenticate_session() {
var parameter = this.authenticator.get_authentication_parameter();
Variant access_token;
if (!parameter.lookup_extended("AccessToken", null, out access_token)) {
critical("Authenticator signalled success, but does not provide access token");
assert_not_reached();
}
graph_session.authenticated.connect(on_session_authenticated);
graph_session.authenticate(access_token.get_string());
}
private void do_upload() {
debug("ACTION: uploading photos to album '%s'",
publishing_params.target_album == PublishingParameters.UNKNOWN_ALBUM ? "(none)" :
publishing_params.get_target_album_name());
host.set_service_locked(true);
progress_reporter = host.serialize_publishables(publishing_params.resolution.get_pixels(),
publishing_params.strip_metadata);
// Serialization is a long and potentially cancellable operation, so before we use
// the publishables, make sure that the publishing interaction is still running. If it
// isn't the publishing environment may be partially torn down so do a short-circuit
// return
if (!is_running())
return;
Spit.Publishing.Publishable[] publishables = host.get_publishables();
uploader = new Uploader(graph_session, publishing_params, publishables);
uploader.upload_complete.connect(on_upload_complete);
uploader.upload_error.connect(on_upload_error);
uploader.upload(on_upload_status_updated);
}
private void do_show_success_pane() {
debug("ACTION: showing success pane.");
host.set_service_locked(false);
host.install_success_pane();
}
private void on_generic_error(Spit.Publishing.PublishingError error) {
if (error is Spit.Publishing.PublishingError.EXPIRED_SESSION)
do_logout();
else
host.post_error(error);
}
#if 0
private void on_endpoint_test_completed(GraphMessage message) {
message.completed.disconnect(on_endpoint_test_completed);
message.failed.disconnect(on_endpoint_test_error);
if (!is_running())
return;
debug("EVENT: endpoint test transaction detected that the Facebook endpoint is alive.");
do_hosted_web_authentication();
}
private void on_endpoint_test_error(GraphMessage message,
Spit.Publishing.PublishingError error) {
message.completed.disconnect(on_endpoint_test_completed);
message.failed.disconnect(on_endpoint_test_error);
if (!is_running())
return;
debug("EVENT: endpoint test transaction failed to detect a connection to the Facebook " +
"endpoint" + error.message);
on_generic_error(error);
}
#endif
private void on_session_authenticated() {
graph_session.authenticated.disconnect(on_session_authenticated);
if (!is_running())
return;
assert(graph_session.is_authenticated());
debug("EVENT: an authenticated session has become available.");
do_fetch_user_info();
}
private void on_fetch_user_info_completed(GraphMessage message) {
message.completed.disconnect(on_fetch_user_info_completed);
message.failed.disconnect(on_fetch_user_info_error);
if (!is_running())
return;
debug("EVENT: user info fetch completed; response = '%s'.", message.get_response_body());
do_extract_user_info_from_json(message.get_response_body());
}
private void on_fetch_user_info_error(GraphMessage message,
Spit.Publishing.PublishingError error) {
message.completed.disconnect(on_fetch_user_info_completed);
message.failed.disconnect(on_fetch_user_info_error);
if (!is_running())
return;
debug("EVENT: fetching user info generated and error.");
on_generic_error(error);
}
private void on_user_info_extracted() {
if (!is_running())
return;
debug("EVENT: user info extracted from JSON response: uid = %s; name = %s.", uid, username);
do_fetch_album_descriptions();
}
private void on_fetch_albums_completed(GraphMessage message) {
message.completed.disconnect(on_fetch_albums_completed);
message.failed.disconnect(on_fetch_albums_error);
if (!is_running())
return;
debug("EVENT: album descriptions fetch transaction completed; response = '%s'.",
message.get_response_body());
do_extract_albums_from_json(message.get_response_body());
}
private void on_fetch_albums_error(GraphMessage message,
Spit.Publishing.PublishingError err) {
message.completed.disconnect(on_fetch_albums_completed);
message.failed.disconnect(on_fetch_albums_error);
if (!is_running())
return;
debug("EVENT: album description fetch attempt generated an error.");
on_generic_error(err);
}
private void on_albums_extracted() {
if (!is_running())
return;
debug("EVENT: successfully extracted %d albums from JSON response",
publishing_params.albums.length);
do_show_publishing_options_pane();
}
private void on_publishing_options_pane_logout() {
publishing_options_pane.publish.disconnect(on_publishing_options_pane_publish);
publishing_options_pane.logout.disconnect(on_publishing_options_pane_logout);
if (!is_running())
return;
debug("EVENT: user clicked 'Logout' in publishing options pane.");
do_logout();
}
private void on_publishing_options_pane_publish(string? target_album, string privacy_setting,
Resolution resolution, bool strip_metadata) {
publishing_options_pane.publish.disconnect(on_publishing_options_pane_publish);
publishing_options_pane.logout.disconnect(on_publishing_options_pane_logout);
if (!is_running())
return;
debug("EVENT: user clicked 'Publish' in publishing options pane.");
publishing_params.strip_metadata = strip_metadata;
set_persistent_strip_metadata(strip_metadata);
publishing_params.resolution = resolution;
set_persistent_default_size(resolution);
publishing_params.privacy_object = privacy_setting;
if (target_album != null) {
// we are publishing at least one photo so we need the name of an album to which
// we'll upload the photo(s)
publishing_params.set_target_album_by_name(target_album);
if (publishing_params.target_album != PublishingParameters.UNKNOWN_ALBUM) {
do_upload();
} else {
publishing_params.new_album_name = target_album;
do_create_new_album();
}
} else {
// we're publishing only videos and we don't need an album name
do_upload();
}
}
private void on_create_album_completed(GraphMessage message) {
message.completed.disconnect(on_create_album_completed);
message.failed.disconnect(on_create_album_error);
assert(publishing_params.new_album_name != null);
if (!is_running())
return;
debug("EVENT: created new album resource on remote host; response body = %s.\n",
message.get_response_body());
do_add_new_local_album_from_json(publishing_params.new_album_name,
message.get_response_body());
}
private void on_create_album_error(GraphMessage message, Spit.Publishing.PublishingError err) {
message.completed.disconnect(on_create_album_completed);
message.failed.disconnect(on_create_album_error);
if (!is_running())
return;
debug("EVENT: attempt to create new album generated an error.");
on_generic_error(err);
}
private void on_upload_status_updated(int file_number, double completed_fraction) {
if (!is_running())
return;
debug("EVENT: uploader reports upload %.2f percent complete.", 100.0 * completed_fraction);
assert(progress_reporter != null);
progress_reporter(file_number, completed_fraction);
}
private void on_upload_complete(Uploader uploader, int num_published) {
uploader.upload_complete.disconnect(on_upload_complete);
uploader.upload_error.disconnect(on_upload_error);
if (!is_running())
return;
debug("EVENT: uploader reports upload complete; %d items published.", num_published);
do_show_success_pane();
}
private void on_upload_error(Uploader uploader, Spit.Publishing.PublishingError err) {
uploader.upload_complete.disconnect(on_upload_complete);
uploader.upload_error.disconnect(on_upload_error);
if (!is_running())
return;
debug("EVENT: uploader reports upload error = '%s'.", err.message);
host.post_error(err);
}
public Spit.Publishing.Service get_service() {
return service;
}
public string get_service_name() {
return SERVICE_NAME;
}
public string get_user_visible_name() {
return USER_VISIBLE_NAME;
}
public void start() {
if (is_running())
return;
debug("FacebookPublisher: starting interaction.");
running = true;
// reset all publishing parameters to their default values -- in case this start is
// actually a restart
publishing_params = new PublishingParameters();
this.authenticator.authenticated.connect(on_authenticator_succeeded);
this.authenticator.authentication_failed.connect(on_authenticator_failed);
this.authenticator.authenticate();
}
public void stop() {
debug("FacebookPublisher: stop( ) invoked.");
if (graph_session != null)
graph_session.stop_transactions();
host = null;
running = false;
}
public bool is_running() {
return running;
}
}
internal class PublishingOptionsPane : Spit.Publishing.DialogPane, GLib.Object {
private Gtk.Builder builder;
private Gtk.Box pane_widget = null;
private Gtk.RadioButton use_existing_radio = null;
private Gtk.RadioButton create_new_radio = null;
private Gtk.ComboBoxText existing_albums_combo = null;
private Gtk.ComboBoxText visibility_combo = null;
private Gtk.Entry new_album_entry = null;
private Gtk.CheckButton strip_metadata_check = null;
private Gtk.Button publish_button = null;
private Gtk.Button logout_button = null;
private Gtk.Label how_to_label = null;
private Album[] albums = null;
private FacebookPublisher publisher = null;
private PrivacyDescription[] privacy_descriptions;
private Resolution[] possible_resolutions;
private Gtk.ComboBoxText resolution_combo = null;
private Spit.Publishing.Publisher.MediaType media_type;
private const string HEADER_LABEL_TEXT = _("You are logged into Facebook as %s.\n\n");
private const string PHOTOS_LABEL_TEXT = _("Where would you like to publish the selected photos?");
private const string RESOLUTION_LABEL_TEXT = _("Upload _size:");
private const int CONTENT_GROUP_SPACING = 32;
private const int STANDARD_ACTION_BUTTON_WIDTH = 128;
public signal void logout();
public signal void publish(string? target_album, string privacy_setting,
Resolution target_resolution, bool strip_metadata);
private class PrivacyDescription {
public string description;
public string privacy_setting;
public PrivacyDescription(string description, string privacy_setting) {
this.description = description;
this.privacy_setting = privacy_setting;
}
}
public PublishingOptionsPane(string username, Album[] albums,
Spit.Publishing.Publisher.MediaType media_type, FacebookPublisher publisher,
Gtk.Builder builder, bool strip_metadata, bool can_logout) {
this.builder = builder;
assert(builder != null);
assert(builder.get_objects().length() > 0);
this.albums = albums;
this.privacy_descriptions = create_privacy_descriptions();
this.possible_resolutions = create_resolution_list();
this.publisher = publisher;
// we'll need to know if the user is importing video or not when sorting out visibility.
this.media_type = media_type;
pane_widget = (Gtk.Box) builder.get_object("facebook_pane_box");
pane_widget.set_border_width(16);
use_existing_radio = (Gtk.RadioButton) this.builder.get_object("use_existing_radio");
create_new_radio = (Gtk.RadioButton) this.builder.get_object("create_new_radio");
existing_albums_combo = (Gtk.ComboBoxText) this.builder.get_object("existing_albums_combo");
visibility_combo = (Gtk.ComboBoxText) this.builder.get_object("visibility_combo");
publish_button = (Gtk.Button) this.builder.get_object("publish_button");
logout_button = (Gtk.Button) this.builder.get_object("logout_button");
if (!can_logout) {
logout_button.parent.remove (logout_button);
}
new_album_entry = (Gtk.Entry) this.builder.get_object("new_album_entry");
resolution_combo = (Gtk.ComboBoxText) this.builder.get_object("resolution_combo");
how_to_label = (Gtk.Label) this.builder.get_object("how_to_label");
strip_metadata_check = (Gtk.CheckButton) this.builder.get_object("strip_metadata_check");
create_new_radio.clicked.connect(on_create_new_toggled);
use_existing_radio.clicked.connect(on_use_existing_toggled);
string label_text = HEADER_LABEL_TEXT.printf(username);
if ((media_type & Spit.Publishing.Publisher.MediaType.PHOTO) != 0)
label_text += PHOTOS_LABEL_TEXT;
how_to_label.set_label(label_text);
strip_metadata_check.set_active(strip_metadata);
setup_visibility_combo();
visibility_combo.set_active(0);
publish_button.clicked.connect(on_publish_button_clicked);
logout_button.clicked.connect(on_logout_button_clicked);
setup_resolution_combo();
resolution_combo.set_active(publisher.get_persistent_default_size());
resolution_combo.changed.connect(on_size_changed);
// Ticket #3175, part 2: make sure this widget starts out sensitive
// if it needs to by checking whether we're starting with a video
// or a new gallery.
visibility_combo.set_sensitive(
(create_new_radio != null && create_new_radio.active) ||
((media_type & Spit.Publishing.Publisher.MediaType.VIDEO) != 0));
// if publishing only videos, disable all photo-specific controls
if (media_type == Spit.Publishing.Publisher.MediaType.VIDEO) {
strip_metadata_check.set_active(false);
strip_metadata_check.set_sensitive(false);
resolution_combo.set_sensitive(false);
use_existing_radio.set_sensitive(false);
create_new_radio.set_sensitive(false);
existing_albums_combo.set_sensitive(false);
new_album_entry.set_sensitive(false);
}
}
private bool publishing_photos() {
return (media_type & Spit.Publishing.Publisher.MediaType.PHOTO) != 0;
}
private void setup_visibility_combo() {
foreach (PrivacyDescription p in privacy_descriptions)
visibility_combo.append_text(p.description);
}
private void setup_resolution_combo() {
foreach (Resolution res in possible_resolutions)
resolution_combo.append_text(res.get_name());
}
private void on_use_existing_toggled() {
if (use_existing_radio.active) {
existing_albums_combo.set_sensitive(true);
new_album_entry.set_sensitive(false);
// Ticket #3175 - if we're not adding a new gallery
// or a video, then we shouldn't be allowed tof
// choose visibility, since it has no effect.
visibility_combo.set_sensitive((media_type & Spit.Publishing.Publisher.MediaType.VIDEO) != 0);
existing_albums_combo.grab_focus();
}
}
private void on_create_new_toggled() {
if (create_new_radio.active) {
existing_albums_combo.set_sensitive(false);
new_album_entry.set_sensitive(true);
new_album_entry.grab_focus();
// Ticket #3175 - if we're creating a new gallery, make sure this is
// active, since it may have possibly been set inactive.
visibility_combo.set_sensitive(true);
}
}
private void on_size_changed() {
publisher.set_persistent_default_size(resolution_combo.get_active());
}
private void on_logout_button_clicked() {
logout();
}
private void on_publish_button_clicked() {
string album_name;
string privacy_setting = privacy_descriptions[visibility_combo.get_active()].privacy_setting;
Resolution resolution_setting;
if (publishing_photos()) {
resolution_setting = possible_resolutions[resolution_combo.get_active()];
if (use_existing_radio.active) {
album_name = existing_albums_combo.get_active_text();
} else {
album_name = new_album_entry.get_text();
}
} else {
resolution_setting = Resolution.STANDARD;
album_name = null;
}
publish(album_name, privacy_setting, resolution_setting, strip_metadata_check.get_active());
}
private PrivacyDescription[] create_privacy_descriptions() {
PrivacyDescription[] result = new PrivacyDescription[0];
result += new PrivacyDescription(_("Just me"), "{ 'value' : 'SELF' }");
result += new PrivacyDescription(_("Friends"), "{ 'value' : 'ALL_FRIENDS' }");
result += new PrivacyDescription(_("Everyone"), "{ 'value' : 'EVERYONE' }");
return result;
}
private Resolution[] create_resolution_list() {
Resolution[] result = new Resolution[0];
result += Resolution.STANDARD;
result += Resolution.HIGH;
return result;
}
public void installed() {
if (publishing_photos()) {
if (albums.length == 0) {
create_new_radio.set_active(true);
new_album_entry.set_text(DEFAULT_ALBUM_NAME);
existing_albums_combo.set_sensitive(false);
use_existing_radio.set_sensitive(false);
} else {
int default_album_seq_num = -1;
int ticker = 0;
foreach (Album album in albums) {
existing_albums_combo.append_text(album.name);
if (album.name == DEFAULT_ALBUM_NAME)
default_album_seq_num = ticker;
ticker++;
}
if (default_album_seq_num != -1) {
existing_albums_combo.set_active(default_album_seq_num);
use_existing_radio.set_active(true);
new_album_entry.set_sensitive(false);
}
else {
create_new_radio.set_active(true);
existing_albums_combo.set_active(0);
existing_albums_combo.set_sensitive(false);
new_album_entry.set_text(DEFAULT_ALBUM_NAME);
}
}
}
publish_button.grab_focus();
}
private void notify_logout() {
logout();
}
private void notify_publish(string? target_album, string privacy_setting, Resolution target_resolution) {
publish(target_album, privacy_setting, target_resolution, strip_metadata_check.get_active());
}
public Gtk.Widget get_widget() {
return pane_widget;
}
public Spit.Publishing.DialogPane.GeometryOptions get_preferred_geometry() {
return Spit.Publishing.DialogPane.GeometryOptions.NONE;
}
public void on_pane_installed() {
logout.connect(notify_logout);
publish.connect(notify_publish);
installed();
}
public void on_pane_uninstalled() {
logout.disconnect(notify_logout);
publish.disconnect(notify_publish);
}
}
internal enum Endpoint {
DEFAULT,
VIDEO,
TEST_CONNECTION;
public string to_uri() {
switch (this) {
case DEFAULT:
return "https://graph.facebook.com/";
case VIDEO:
return "https://graph-video.facebook.com/";
case TEST_CONNECTION:
return "https://www.facebook.com/";
default:
assert_not_reached();
}
}
}
internal abstract class GraphMessage {
public signal void completed();
public signal void failed(Spit.Publishing.PublishingError err);
public signal void data_transmitted(int bytes_sent_so_far, int total_bytes);
public abstract string get_uri();
public abstract string get_response_body();
}
internal class GraphSession {
private abstract class GraphMessageImpl : GraphMessage {
public Publishing.RESTSupport.HttpMethod method;
public string uri;
public string access_token;
public Soup.Message soup_message;
public weak GraphSession host_session;
public int bytes_so_far;
protected GraphMessageImpl(GraphSession host_session, Publishing.RESTSupport.HttpMethod method,
string relative_uri, string access_token, Endpoint endpoint = Endpoint.DEFAULT) {
this.method = method;
this.access_token = access_token;
this.host_session = host_session;
this.bytes_so_far = 0;
string endpoint_uri = endpoint.to_uri();
try {
Regex starting_slashes = new Regex("^/+");
this.uri = endpoint_uri + starting_slashes.replace(relative_uri, -1, 0, "");
} catch (RegexError err) {
assert_not_reached();
}
}
public virtual bool prepare_for_transmission() {
return true;
}
public override string get_uri() {
return uri;
}
public override string get_response_body() {
return (string) soup_message.response_body.data;
}
public void on_wrote_body_data(Soup.Buffer chunk) {
bytes_so_far += (int) chunk.length;
data_transmitted(bytes_so_far, (int) soup_message.request_body.length);
}
}
private class GraphQueryMessage : GraphMessageImpl {
public GraphQueryMessage(GraphSession host_session, string relative_uri,
string access_token) {
base(host_session, Publishing.RESTSupport.HttpMethod.GET, relative_uri, access_token);
Soup.URI destination_uri = new Soup.URI(uri + "?access_token=" + access_token);
soup_message = new Soup.Message.from_uri(method.to_string(), destination_uri);
soup_message.wrote_body_data.connect(on_wrote_body_data);
}
}
private class GraphEndpointProbeMessage : GraphMessageImpl {
public GraphEndpointProbeMessage(GraphSession host_session) {
base(host_session, Publishing.RESTSupport.HttpMethod.GET, "/", "",
Endpoint.TEST_CONNECTION);
soup_message = new Soup.Message.from_uri(method.to_string(), new Soup.URI(uri));
soup_message.wrote_body_data.connect(on_wrote_body_data);
}
}
private class GraphUploadMessage : GraphMessageImpl {
private MappedFile mapped_file = null;
private Spit.Publishing.Publishable publishable;
public GraphUploadMessage(GraphSession host_session, string access_token,
string relative_uri, Spit.Publishing.Publishable publishable,
bool suppress_titling, string? resource_privacy = null) {
base(host_session, Publishing.RESTSupport.HttpMethod.POST, relative_uri, access_token,
(publishable.get_media_type() == Spit.Publishing.Publisher.MediaType.VIDEO) ?
Endpoint.VIDEO : Endpoint.DEFAULT);
// Video uploads require a privacy string at the per-resource level. Since they aren't
// placed in albums, they can't inherit their privacy settings from their containing
// album like photos do
assert(publishable.get_media_type() != Spit.Publishing.Publisher.MediaType.VIDEO ||
resource_privacy != null);
this.publishable = publishable;
// attempt to map the binary payload from disk into memory
try {
this.mapped_file = new MappedFile(publishable.get_serialized_file().get_path(),
false);
} catch (FileError e) {
return;
}
this.soup_message = new Soup.Message.from_uri(method.to_string(), new Soup.URI(uri));
soup_message.wrote_body_data.connect(on_wrote_body_data);
unowned uint8[] payload = (uint8[]) mapped_file.get_contents();
payload.length = (int) mapped_file.get_length();
Soup.Buffer image_data = new Soup.Buffer(Soup.MemoryUse.TEMPORARY, payload);
Soup.Multipart mp_envelope = new Soup.Multipart("multipart/form-data");
mp_envelope.append_form_string("access_token", access_token);
if (publishable.get_media_type() == Spit.Publishing.Publisher.MediaType.VIDEO)
mp_envelope.append_form_string("privacy", resource_privacy);
//Get photo title and post it as message on FB API
string publishable_title = publishable.get_param_string("title");
if (!suppress_titling && publishable_title != null)
mp_envelope.append_form_string("name", publishable_title);
//Set 'message' data field with EXIF comment field. Title has precedence.
string publishable_comment = publishable.get_param_string("comment");
if (!suppress_titling && publishable_comment != null)
mp_envelope.append_form_string("message", publishable_comment);
//Sets correct date of the picture
if (!suppress_titling)
mp_envelope.append_form_string("backdated_time", publishable.get_exposure_date_time().to_string());
string source_file_mime_type =
(publishable.get_media_type() == Spit.Publishing.Publisher.MediaType.VIDEO) ?
"video" : "image/jpeg";
mp_envelope.append_form_file("source", publishable.get_serialized_file().get_basename(),
source_file_mime_type, image_data);
mp_envelope.to_message(soup_message.request_headers, soup_message.request_body);
}
public override bool prepare_for_transmission() {
if (mapped_file == null) {
failed(new Spit.Publishing.PublishingError.LOCAL_FILE_ERROR(
"File %s is unavailable.".printf(publishable.get_serialized_file().get_path())));
return false;
} else {
return true;
}
}
}
private class GraphCreateAlbumMessage : GraphMessageImpl {
public GraphCreateAlbumMessage(GraphSession host_session, string access_token,
string album_name, string album_privacy) {
base(host_session, Publishing.RESTSupport.HttpMethod.POST, "/me/albums", access_token);
assert(album_privacy != null && album_privacy != "");
this.soup_message = new Soup.Message.from_uri(method.to_string(), new Soup.URI(uri));
Soup.Multipart mp_envelope = new Soup.Multipart("multipart/form-data");
mp_envelope.append_form_string("access_token", access_token);
mp_envelope.append_form_string("name", album_name);
mp_envelope.append_form_string("privacy", album_privacy);
mp_envelope.to_message(soup_message.request_headers, soup_message.request_body);
}
}
public signal void authenticated();
private Soup.Session soup_session;
private string? access_token;
private GraphMessage? current_message;
public GraphSession() {
this.soup_session = new Soup.Session ();
this.soup_session.request_unqueued.connect (on_request_unqueued);
this.soup_session.timeout = 15;
this.access_token = null;
this.current_message = null;
this.soup_session.ssl_use_system_ca_file = true;
}
~GraphSession() {
soup_session.request_unqueued.disconnect (on_request_unqueued);
}
private void manage_message(GraphMessage msg) {
assert(current_message == null);
current_message = msg;
}
private void unmanage_message(GraphMessage msg) {
assert(current_message != null);
current_message = null;
}
private void on_request_unqueued(Soup.Message msg) {
assert(current_message != null);
GraphMessageImpl real_message = (GraphMessageImpl) current_message;
assert(real_message.soup_message == msg);
// these error types are always recoverable given the unique behavior of the Facebook
// endpoint, so try again
if (msg.status_code == Soup.KnownStatusCode.IO_ERROR ||
msg.status_code == Soup.KnownStatusCode.MALFORMED ||
msg.status_code == Soup.KnownStatusCode.TRY_AGAIN) {
real_message.bytes_so_far = 0;
soup_session.queue_message(msg, null);
return;
}
unmanage_message(real_message);
msg.wrote_body_data.disconnect(real_message.on_wrote_body_data);
Spit.Publishing.PublishingError? error = null;
switch (msg.status_code) {
case Soup.KnownStatusCode.OK:
case Soup.KnownStatusCode.CREATED: // HTTP code 201 (CREATED) signals that a new
// resource was created in response to a PUT
// or POST
break;
case EXPIRED_SESSION_STATUS_CODE:
error = new Spit.Publishing.PublishingError.EXPIRED_SESSION(
"OAuth Access Token has Expired. Logout user.");
break;
case Soup.KnownStatusCode.CANT_RESOLVE:
case Soup.KnownStatusCode.CANT_RESOLVE_PROXY:
error = new Spit.Publishing.PublishingError.NO_ANSWER(
"Unable to resolve %s (error code %u)", real_message.get_uri(), msg.status_code);
break;
case Soup.KnownStatusCode.CANT_CONNECT:
case Soup.KnownStatusCode.CANT_CONNECT_PROXY:
error = new Spit.Publishing.PublishingError.NO_ANSWER(
"Unable to connect to %s (error code %u)", real_message.get_uri(), msg.status_code);
break;
default:
// status codes below 100 are used by Soup, 100 and above are defined HTTP
// codes
if (msg.status_code >= 100) {
error = new Spit.Publishing.PublishingError.NO_ANSWER(
"Service %s returned HTTP status code %u %s", real_message.get_uri(),
msg.status_code, msg.reason_phrase);
} else {
debug(msg.reason_phrase);
error = new Spit.Publishing.PublishingError.NO_ANSWER(
"Failure communicating with %s (error code %u)", real_message.get_uri(),
msg.status_code);
}
break;
}
// All valid communication with Facebook involves body data in the response
if (error == null)
if (msg.response_body.data == null || msg.response_body.data.length == 0)
error = new Spit.Publishing.PublishingError.MALFORMED_RESPONSE(
"No response data from %s", real_message.get_uri());
if (error == null)
real_message.completed();
else
real_message.failed(error);
}
public void authenticate(string access_token) {
this.access_token = access_token;
authenticated();
}
public bool is_authenticated() {
return access_token != null;
}
#if 0
public GraphMessage new_endpoint_test() {
return new GraphEndpointProbeMessage(this);
}
#endif
public GraphMessage new_query(string resource_path) {
return new GraphQueryMessage(this, resource_path, access_token);
}
public GraphMessage new_upload(string resource_path, Spit.Publishing.Publishable publishable,
bool suppress_titling, string? resource_privacy = null) {
return new GraphUploadMessage(this, access_token, resource_path, publishable,
suppress_titling, resource_privacy);
}
public GraphMessage new_create_album(string album_name, string privacy) {
return new GraphSession.GraphCreateAlbumMessage(this, access_token, album_name, privacy);
}
public void send_message(GraphMessage message) {
GraphMessageImpl real_message = (GraphMessageImpl) message;
debug("making HTTP request to URI: " + real_message.soup_message.uri.to_string(false));
if (real_message.prepare_for_transmission()) {
manage_message(message);
soup_session.queue_message(real_message.soup_message, null);
}
}
public void stop_transactions() {
soup_session.abort();
}
}
internal class Uploader {
private int current_file;
private Spit.Publishing.Publishable[] publishables;
private GraphSession session;
private PublishingParameters publishing_params;
private unowned Spit.Publishing.ProgressCallback? status_updated = null;
public signal void upload_complete(int num_photos_published);
public signal void upload_error(Spit.Publishing.PublishingError err);
public Uploader(GraphSession session, PublishingParameters publishing_params,
Spit.Publishing.Publishable[] publishables) {
this.current_file = 0;
this.publishables = publishables;
this.session = session;
this.publishing_params = publishing_params;
}
private void send_current_file() {
Spit.Publishing.Publishable publishable = publishables[current_file];
GLib.File? file = publishable.get_serialized_file();
// if the current publishable hasn't been serialized, then skip it
if (file == null) {
current_file++;
return;
}
string resource_uri =
(publishable.get_media_type() == Spit.Publishing.Publisher.MediaType.PHOTO) ?
"/%s/photos".printf(publishing_params.get_target_album_id()) : "/me/videos";
string? resource_privacy =
(publishable.get_media_type() == Spit.Publishing.Publisher.MediaType.VIDEO) ?
publishing_params.privacy_object : null;
GraphMessage upload_message = session.new_upload(resource_uri, publishable,
publishing_params.strip_metadata, resource_privacy);
upload_message.data_transmitted.connect(on_chunk_transmitted);
upload_message.completed.connect(on_message_completed);
upload_message.failed.connect(on_message_failed);
session.send_message(upload_message);
}
private void send_files() {
current_file = 0;
send_current_file();
}
private void on_chunk_transmitted(int bytes_written_so_far, int total_bytes) {
double file_span = 1.0 / publishables.length;
double this_file_fraction_complete = ((double) bytes_written_so_far) / total_bytes;
double fraction_complete = (current_file * file_span) + (this_file_fraction_complete *
file_span);
if (status_updated != null)
status_updated(current_file + 1, fraction_complete);
}
private void on_message_completed(GraphMessage message) {
message.data_transmitted.disconnect(on_chunk_transmitted);
message.completed.disconnect(on_message_completed);
message.failed.disconnect(on_message_failed);
current_file++;
if (current_file < publishables.length) {
send_current_file();
} else {
upload_complete(current_file);
}
}
private void on_message_failed(GraphMessage message, Spit.Publishing.PublishingError error) {
message.data_transmitted.disconnect(on_chunk_transmitted);
message.completed.disconnect(on_message_completed);
message.failed.disconnect(on_message_failed);
upload_error(error);
}
public void upload(Spit.Publishing.ProgressCallback? status_updated = null) {
this.status_updated = status_updated;
if (publishables.length > 0)
send_files();
}
}
}
|