~ubuntu-branches/debian/squeeze/f-spot/squeeze

« back to all changes in this revision

Viewing changes to extensions/SmugMugExport/SmugMugExport.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane, Mirco Bauer, Iain Lane
  • Date: 2009-02-07 20:23:32 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20090207202332-oc93rfjo1st0571s
Tags: 0.5.0.3-2
[ Mirco Bauer]
* Upload to unstable.
* debian/control:
  + Lowered GNOME# build-deps to 2.0 ABI as that transition didn't happen
    yet in unstable.

[ Iain Lane ]
* debian/patches/svn-r4545_locales-import.dpatch: Patch backported from SVN
  trunk revision 4545 - initialize the translation catalog earlier (LP: #293305)
  (Closes: #514457). Thanks to Florian Heinle for finding the patch and to
  Chris Coulson for preparing the update.
* debian/control: Build-depend on libmono-dev (>= 1.2.4) to match configure
  checks.
* debian/rules: Pass CSC=/usr/bin/csc to configure for gio-sharp to fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * SmugMugExport.cs
3
 
 *
4
 
 * Authors:
5
 
 *   Thomas Van Machelen <thomas.vanmachelen@gmail.com>
6
 
 *
7
 
 * Based on PicasaWebExport code from Stephane Delcroix.
8
 
 *
9
 
 * Copyright (C) 2006 Thomas Van Machelen
10
 
 */
11
 
 
12
 
using System;
13
 
using System.Net;
14
 
using System.IO;
15
 
using System.Text;
16
 
using System.Threading;
17
 
using System.Collections;
18
 
using System.Collections.Specialized;
19
 
using System.Web;
20
 
using Mono.Unix;
21
 
using Gtk;
22
 
 
23
 
using FSpot;
24
 
using FSpot.Filters;
25
 
using FSpot.Widgets;
26
 
using FSpot.Utils;
27
 
 
28
 
using Gnome.Keyring;
29
 
using SmugMugNet;
30
 
 
31
 
namespace FSpotSmugMugExport {
32
 
        public class SmugMugAccount {
33
 
                private string username;
34
 
                private string password;
35
 
                private SmugMugApi smugmug_proxy;
36
 
 
37
 
                public SmugMugAccount (string username, string password) 
38
 
                {
39
 
                        this.username = username;
40
 
                        this.password = password;
41
 
                }
42
 
 
43
 
                public SmugMugApi Connect ()
44
 
                {
45
 
                        System.Console.WriteLine ("SmugMug.Connect() {0}", username);
46
 
                        SmugMugApi proxy = new SmugMugApi (username, password);
47
 
                        ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy ();
48
 
                        proxy.Login ();
49
 
 
50
 
                        this.smugmug_proxy = proxy;
51
 
                        return proxy;
52
 
                }
53
 
 
54
 
                private void MarkChanged()
55
 
                {
56
 
                        smugmug_proxy = null;
57
 
                }
58
 
 
59
 
                public bool Connected {
60
 
                        get {
61
 
                                return smugmug_proxy.Connected;
62
 
                        }
63
 
                }
64
 
 
65
 
                public string Username {
66
 
                        get {
67
 
                                return username;
68
 
                        }
69
 
                        set {
70
 
                                if (username != value) {
71
 
                                        username = value;
72
 
                                        MarkChanged ();
73
 
                                }
74
 
                        }
75
 
                }
76
 
 
77
 
                public string Password {
78
 
                        get {
79
 
                                return password;
80
 
                        }
81
 
                        set {
82
 
                                if (password != value) {
83
 
                                        password = value;
84
 
                                        MarkChanged ();
85
 
                                }
86
 
                        }
87
 
                }
88
 
 
89
 
                public SmugMugApi SmugMug {
90
 
                        get {
91
 
                                return smugmug_proxy;
92
 
                        }
93
 
                }
94
 
        }
95
 
 
96
 
        
97
 
        public class SmugMugAccountManager 
98
 
        {
99
 
                private static SmugMugAccountManager instance;
100
 
                private const string keyring_item_name = "SmugMug Account";
101
 
                ArrayList accounts;
102
 
 
103
 
                public delegate void AccountListChangedHandler (SmugMugAccountManager manager, SmugMugAccount changed_account);
104
 
                public event AccountListChangedHandler AccountListChanged;
105
 
                
106
 
                public static SmugMugAccountManager GetInstance ()
107
 
                {
108
 
                        if (instance == null) {
109
 
                                instance = new SmugMugAccountManager ();
110
 
                        }
111
 
 
112
 
                        return instance;
113
 
                }
114
 
 
115
 
                private SmugMugAccountManager ()
116
 
                {
117
 
                        accounts = new ArrayList ();
118
 
                        ReadAccounts ();
119
 
                }       
120
 
 
121
 
                public void MarkChanged ()
122
 
                {
123
 
                        MarkChanged (true, null);
124
 
                }
125
 
 
126
 
                public void MarkChanged (bool write, SmugMugAccount changed_account)
127
 
                {
128
 
                        if (write)
129
 
                                WriteAccounts ();
130
 
 
131
 
                        if (AccountListChanged != null)
132
 
                                AccountListChanged (this, changed_account);
133
 
                }
134
 
 
135
 
                public ArrayList GetAccounts ()
136
 
                {
137
 
                        return accounts;
138
 
                }
139
 
 
140
 
                public void AddAccount (SmugMugAccount account)
141
 
                {
142
 
                        AddAccount (account, true);
143
 
                }
144
 
 
145
 
                public void AddAccount (SmugMugAccount account, bool write)
146
 
                {
147
 
                        accounts.Add (account);
148
 
                        MarkChanged (write, account);
149
 
                }
150
 
 
151
 
                public void RemoveAccount (SmugMugAccount account)
152
 
                {
153
 
                        string keyring = Ring.GetDefaultKeyring();
154
 
                        Hashtable request_attributes = new Hashtable();
155
 
                        request_attributes["name"] = keyring_item_name;
156
 
                        request_attributes["username"] = account.Username;
157
 
                        try {
158
 
                                foreach(ItemData result in Ring.Find(ItemType.GenericSecret, request_attributes)) {
159
 
                                        Ring.DeleteItem(keyring, result.ItemID);
160
 
                                }
161
 
                        } catch (Exception e) {
162
 
                                Console.WriteLine(e);
163
 
                        }
164
 
                        accounts.Remove (account);
165
 
                        MarkChanged ();
166
 
                }
167
 
 
168
 
                public void WriteAccounts ()
169
 
                {
170
 
                        string keyring = Ring.GetDefaultKeyring();
171
 
                        foreach (SmugMugAccount account in accounts) {
172
 
                                Hashtable update_request_attributes = new Hashtable();
173
 
                                update_request_attributes["name"] = keyring_item_name;
174
 
                                update_request_attributes["username"] = account.Username;
175
 
 
176
 
                                Ring.CreateItem(keyring, ItemType.GenericSecret, keyring_item_name, update_request_attributes, account.Password, true);
177
 
                        }
178
 
                }
179
 
 
180
 
                private void ReadAccounts ()
181
 
                {
182
 
 
183
 
                        Hashtable request_attributes = new Hashtable();
184
 
                        request_attributes["name"] = keyring_item_name;
185
 
                        try {
186
 
                                foreach(ItemData result in Ring.Find(ItemType.GenericSecret, request_attributes)) {
187
 
                                        if(!result.Attributes.ContainsKey("name") || !result.Attributes.ContainsKey("username") ||
188
 
                                                (result.Attributes["name"] as string) != keyring_item_name) 
189
 
                                                continue;
190
 
                                        
191
 
                                        string username = (string)result.Attributes["username"];
192
 
                                        string password = result.Secret;
193
 
        
194
 
                                        if (username == null || username == String.Empty || password == null || password == String.Empty)
195
 
                                                throw new ApplicationException ("Invalid username/password in keyring");
196
 
        
197
 
                                        SmugMugAccount account = new SmugMugAccount(username, password);
198
 
                                        if (account != null)
199
 
                                                AddAccount (account, false);
200
 
 
201
 
                                }
202
 
                        } catch (Exception e) {
203
 
                                Console.Error.WriteLine(e);
204
 
                        }
205
 
 
206
 
                        MarkChanged ();
207
 
                }
208
 
        }
209
 
        
210
 
        public class SmugMugAccountDialog {
211
 
                public SmugMugAccountDialog (Gtk.Window parent) : this (parent, null) { 
212
 
                        Dialog.Response += HandleAddResponse;
213
 
                        add_button.Sensitive = false;
214
 
                }
215
 
                
216
 
                public SmugMugAccountDialog (Gtk.Window parent, SmugMugAccount account)
217
 
                {
218
 
                        xml = new Glade.XML (null, "SmugMugExport.glade", dialog_name, "f-spot");
219
 
                        xml.Autoconnect (this);
220
 
 
221
 
                        Dialog.Modal = false;
222
 
                        Dialog.TransientFor = parent;
223
 
                        Dialog.DefaultResponse = Gtk.ResponseType.Ok;
224
 
                        
225
 
                        this.account = account;
226
 
        
227
 
                        password_entry.ActivatesDefault = true;
228
 
                        username_entry.ActivatesDefault = true;
229
 
 
230
 
                        if (account != null) {
231
 
                                password_entry.Text = account.Password;
232
 
                                username_entry.Text = account.Username;
233
 
                                add_button.Label = Gtk.Stock.Ok;
234
 
                                Dialog.Response += HandleEditResponse;
235
 
                        }
236
 
 
237
 
                        if (remove_button != null)
238
 
                                remove_button.Visible = account != null;
239
 
 
240
 
                        this.Dialog.Show ();
241
 
 
242
 
                        password_entry.Changed += HandleChanged;
243
 
                        username_entry.Changed += HandleChanged;
244
 
                        HandleChanged (null, null);
245
 
                }
246
 
 
247
 
                private void HandleChanged (object sender, System.EventArgs args)
248
 
                {
249
 
                        password = password_entry.Text;
250
 
                        username = username_entry.Text;
251
 
 
252
 
                        add_button.Sensitive = !(password == String.Empty || username == String.Empty);
253
 
                }
254
 
                
255
 
                [GLib.ConnectBefore]
256
 
                protected void HandleAddResponse (object sender, Gtk.ResponseArgs args)
257
 
                {
258
 
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
259
 
                                SmugMugAccount account = new SmugMugAccount (username, password);
260
 
                                SmugMugAccountManager.GetInstance ().AddAccount (account);
261
 
                        }
262
 
                        Dialog.Destroy ();
263
 
                }
264
 
 
265
 
                protected void HandleEditResponse (object sender, Gtk.ResponseArgs args)
266
 
                {
267
 
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
268
 
                                account.Username = username;
269
 
                                account.Password = password;
270
 
                                SmugMugAccountManager.GetInstance ().MarkChanged (true, account);
271
 
                        } else if (args.ResponseId == Gtk.ResponseType.Reject) {
272
 
                                // NOTE we are using Reject to signal the remove action.
273
 
                                SmugMugAccountManager.GetInstance ().RemoveAccount (account);
274
 
                        }
275
 
                        Dialog.Destroy ();                              
276
 
                }
277
 
 
278
 
                private Gtk.Dialog Dialog {
279
 
                        get {
280
 
                                if (dialog == null)
281
 
                                        dialog = (Gtk.Dialog) xml.GetWidget (dialog_name);
282
 
 
283
 
                                return dialog;
284
 
                        }
285
 
                }
286
 
 
287
 
                private SmugMugAccount account;
288
 
                private string password;
289
 
                private string username;
290
 
                private string dialog_name = "smugmug_add_dialog";
291
 
                private Glade.XML xml;
292
 
 
293
 
                // widgets 
294
 
                [Glade.Widget] Gtk.Dialog dialog;
295
 
                [Glade.Widget] Gtk.Entry password_entry;
296
 
                [Glade.Widget] Gtk.Entry username_entry;
297
 
 
298
 
                [Glade.Widget] Gtk.Button add_button;
299
 
                [Glade.Widget] Gtk.Button remove_button;
300
 
        }
301
 
 
302
 
        public class SmugMugAddAlbum {
303
 
                //[Glade.Widget] Gtk.OptionMenu album_optionmenu;
304
 
 
305
 
                [Glade.Widget] Gtk.Dialog dialog;
306
 
                [Glade.Widget] Gtk.Entry title_entry;
307
 
                [Glade.Widget] Gtk.CheckButton public_check;
308
 
                [Glade.Widget] Gtk.ComboBox category_combo;
309
 
                
310
 
                [Glade.Widget] Gtk.Button add_button;
311
 
 
312
 
                private string dialog_name = "smugmug_add_album_dialog";
313
 
                private Glade.XML xml;
314
 
                private SmugMugExport export;
315
 
                private SmugMugApi smugmug;
316
 
                private string title;
317
 
                private ListStore category_store;
318
 
 
319
 
                public SmugMugAddAlbum (SmugMugExport export, SmugMugApi smugmug)
320
 
                {
321
 
                        xml = new Glade.XML (null, "SmugMugExport.glade", dialog_name, "f-spot");
322
 
                        xml.Autoconnect (this);
323
 
 
324
 
                        this.export = export;
325
 
                        this.smugmug = smugmug;
326
 
                        
327
 
                        this.category_store = new ListStore (typeof(int), typeof(string));
328
 
                        CellRendererText display_cell = new CellRendererText();
329
 
                        category_combo.PackStart (display_cell, true);
330
 
                        category_combo.SetCellDataFunc (display_cell, new CellLayoutDataFunc (CategoryDataFunc));
331
 
                        this.category_combo.Model = category_store;
332
 
                        PopulateCategoryCombo ();
333
 
                        
334
 
                        Dialog.Response += HandleAddResponse;
335
 
 
336
 
                        title_entry.Changed += HandleChanged;
337
 
                        HandleChanged (null, null);
338
 
                }
339
 
                
340
 
                private void HandleChanged (object sender, EventArgs args)
341
 
                {
342
 
                        title = title_entry.Text;
343
 
 
344
 
                        if (title == String.Empty)
345
 
                                add_button.Sensitive = false;
346
 
                        else
347
 
                                add_button.Sensitive = true;
348
 
                }
349
 
                
350
 
                [GLib.ConnectBefore]
351
 
                protected void HandleAddResponse (object sender, Gtk.ResponseArgs args)
352
 
                {
353
 
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
354
 
                                smugmug.CreateAlbum (title, CurrentCategoryId, public_check.Active);
355
 
                                export.HandleAlbumAdded (title);
356
 
                        }
357
 
                        Dialog.Destroy ();
358
 
                }
359
 
 
360
 
                void CategoryDataFunc (CellLayout layout, CellRenderer renderer, TreeModel model, TreeIter iter) 
361
 
                {
362
 
                        string name = (string)model.GetValue (iter, 1);
363
 
                        (renderer as CellRendererText).Text = name;
364
 
                }
365
 
 
366
 
                protected void PopulateCategoryCombo ()
367
 
                {
368
 
                        SmugMugNet.Category[] categories = smugmug.GetCategories ();
369
 
 
370
 
                        foreach (SmugMugNet.Category category in categories) {                          
371
 
                                category_store.AppendValues (category.CategoryID, category.Title);
372
 
                        }
373
 
 
374
 
                        category_combo.Active = 0;
375
 
 
376
 
                        category_combo.ShowAll ();
377
 
                }
378
 
 
379
 
                protected int CurrentCategoryId
380
 
                {
381
 
                        get {
382
 
                                TreeIter current;
383
 
                                category_combo.GetActiveIter (out current);
384
 
                                return (int)category_combo.Model.GetValue (current, 0);
385
 
                        }
386
 
                }
387
 
 
388
 
                private Gtk.Dialog Dialog {
389
 
                        get {
390
 
                                if (dialog == null)
391
 
                                        dialog = (Gtk.Dialog) xml.GetWidget (dialog_name);
392
 
 
393
 
                                return dialog;
394
 
                        }
395
 
                }
396
 
        }
397
 
 
398
 
        
399
 
        public class SmugMugExport : FSpot.Extensions.IExporter {
400
 
                public SmugMugExport ()
401
 
                {
402
 
                }
403
 
                public void Run (IBrowsableCollection selection)
404
 
                {
405
 
                        xml = new Glade.XML (null, "SmugMugExport.glade", dialog_name, "f-spot");
406
 
                        xml.Autoconnect (this);
407
 
 
408
 
                        this.items = selection.Items;
409
 
                        album_button.Sensitive = false;
410
 
                        FSpot.Widgets.IconView view = new FSpot.Widgets.IconView (selection);
411
 
                        view.DisplayDates = false;
412
 
                        view.DisplayTags = false;
413
 
 
414
 
                        Dialog.Modal = false;
415
 
                        Dialog.TransientFor = null;
416
 
                        Dialog.Close += HandleCloseEvent;
417
 
 
418
 
                        thumb_scrolledwindow.Add (view);
419
 
                        view.Show ();
420
 
                        Dialog.Show ();
421
 
 
422
 
                        SmugMugAccountManager manager = SmugMugAccountManager.GetInstance ();
423
 
                        manager.AccountListChanged += PopulateSmugMugOptionMenu;
424
 
                        PopulateSmugMugOptionMenu (manager, null);
425
 
 
426
 
                        if (edit_button != null)
427
 
                                edit_button.Clicked += HandleEditGallery;
428
 
                        
429
 
                        rh = new Gtk.ResponseHandler (HandleResponse);
430
 
                        Dialog.Response += HandleResponse;
431
 
                        connect = true;
432
 
                        HandleSizeActive (null, null);
433
 
                        Connect ();
434
 
 
435
 
                        scale_check.Toggled += HandleScaleCheckToggled;
436
 
                        
437
 
                        LoadPreference (SCALE_KEY);
438
 
                        LoadPreference (SIZE_KEY);
439
 
                        LoadPreference (ROTATE_KEY);
440
 
                        LoadPreference (BROWSER_KEY);
441
 
                }
442
 
                
443
 
                Gtk.ResponseHandler rh;
444
 
                
445
 
                private bool scale;
446
 
                private int size;
447
 
                private bool browser;
448
 
                private bool rotate;
449
 
//              private bool meta;
450
 
                private bool connect = false;
451
 
 
452
 
                private long approx_size = 0;
453
 
                private long sent_bytes = 0;
454
 
 
455
 
                IBrowsableItem [] items;
456
 
                int photo_index;
457
 
                FSpot.ThreadProgressDialog progress_dialog;
458
 
                
459
 
                ArrayList accounts;
460
 
                private SmugMugAccount account;
461
 
                private Album album;
462
 
 
463
 
                private string dialog_name = "smugmug_export_dialog";
464
 
                private Glade.XML xml;
465
 
 
466
 
                // Dialogs
467
 
                private SmugMugAccountDialog gallery_add;
468
 
                private SmugMugAddAlbum album_add;
469
 
 
470
 
                // Widgets
471
 
                [Glade.Widget] Gtk.Dialog dialog;
472
 
                [Glade.Widget] Gtk.OptionMenu gallery_optionmenu;
473
 
                [Glade.Widget] Gtk.OptionMenu album_optionmenu;
474
 
                
475
 
                [Glade.Widget] Gtk.Label status_label;
476
 
 
477
 
                [Glade.Widget] Gtk.CheckButton browser_check;
478
 
                [Glade.Widget] Gtk.CheckButton scale_check;
479
 
                [Glade.Widget] Gtk.CheckButton rotate_check;
480
 
                
481
 
                [Glade.Widget] Gtk.SpinButton size_spin;
482
 
 
483
 
                [Glade.Widget] Gtk.Button album_button;
484
 
                [Glade.Widget] Gtk.Button edit_button;
485
 
                
486
 
                [Glade.Widget] Gtk.Button export_button;
487
 
 
488
 
                [Glade.Widget] Gtk.ScrolledWindow thumb_scrolledwindow;
489
 
 
490
 
                System.Threading.Thread command_thread;
491
 
                
492
 
                public const string EXPORT_SERVICE = "smugmug/";
493
 
                public const string SCALE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "scale";
494
 
                public const string SIZE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "size";
495
 
                public const string ROTATE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "rotate";
496
 
                public const string BROWSER_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "browser";
497
 
 
498
 
                private void HandleResponse (object sender, Gtk.ResponseArgs args)
499
 
                {
500
 
                        if (args.ResponseId != Gtk.ResponseType.Ok) {
501
 
                                Dialog.Destroy ();
502
 
                                return;
503
 
                        }
504
 
 
505
 
                        if (scale_check != null) {
506
 
                                scale = scale_check.Active;
507
 
                                size = size_spin.ValueAsInt;
508
 
                        } else
509
 
                                scale = false;
510
 
 
511
 
                        browser = browser_check.Active;
512
 
                        rotate = rotate_check.Active;
513
 
//                      meta = meta_check.Active;
514
 
 
515
 
                        if (account != null) { 
516
 
                                //System.Console.WriteLine ("history = {0}", album_optionmenu.History);
517
 
                                album = (Album) account.SmugMug.GetAlbums() [Math.Max (0, album_optionmenu.History)]; 
518
 
                                photo_index = 0;
519
 
                                
520
 
                                Dialog.Destroy ();
521
 
 
522
 
                                command_thread = new System.Threading.Thread (new System.Threading.ThreadStart (this.Upload));
523
 
                                command_thread.Name = Mono.Unix.Catalog.GetString ("Uploading Pictures");
524
 
                                
525
 
                                progress_dialog = new FSpot.ThreadProgressDialog (command_thread, items.Length);
526
 
                                progress_dialog.Start ();
527
 
 
528
 
                                // Save these settings for next time
529
 
                                Preferences.Set (SCALE_KEY, scale);
530
 
                                Preferences.Set (SIZE_KEY, size);
531
 
                                Preferences.Set (ROTATE_KEY, rotate);
532
 
                                Preferences.Set (BROWSER_KEY, browser);
533
 
                        }
534
 
                }
535
 
                
536
 
                public void HandleSizeActive (object sender, EventArgs args)
537
 
                {
538
 
                        size_spin.Sensitive = scale_check.Active;
539
 
                }
540
 
 
541
 
                private void Upload ()
542
 
                {
543
 
                        sent_bytes = 0;
544
 
                        approx_size = 0;
545
 
 
546
 
                        System.Uri album_uri = null;
547
 
 
548
 
                        System.Console.WriteLine ("Starting Upload to Smugmug, album {0} - {1}", album.Title, album.AlbumID);
549
 
 
550
 
                        FilterSet filters = new FilterSet ();
551
 
                        filters.Add (new JpegFilter ());
552
 
 
553
 
                        if (scale)
554
 
                                filters.Add (new ResizeFilter ((uint)size));
555
 
 
556
 
                        if (rotate)
557
 
                                filters.Add (new OrientationFilter ());
558
 
 
559
 
                        while (photo_index < items.Length) {
560
 
                                try {
561
 
                                        IBrowsableItem item = items[photo_index];
562
 
 
563
 
                                        FileInfo file_info;
564
 
                                        Console.WriteLine ("uploading {0}", photo_index);
565
 
 
566
 
                                        progress_dialog.Message = String.Format (Catalog.GetString ("Uploading picture \"{0}\" ({1} of {2})"), 
567
 
                                                                                 item.Name, photo_index+1, items.Length);
568
 
                                        progress_dialog.ProgressText = string.Empty;
569
 
                                        progress_dialog.Fraction = ((photo_index) / (double) items.Length);
570
 
                                        photo_index++;
571
 
                                        
572
 
                                        FilterRequest request = new FilterRequest (item.DefaultVersionUri);
573
 
 
574
 
                                        filters.Convert (request);
575
 
 
576
 
                                        file_info = new FileInfo (request.Current.LocalPath);
577
 
 
578
 
                                        if (approx_size == 0) //first image
579
 
                                                approx_size = file_info.Length * items.Length;
580
 
                                        else
581
 
                                                approx_size = sent_bytes * items.Length / (photo_index - 1);
582
 
 
583
 
                                        int image_id = account.SmugMug.Upload (request.Current.LocalPath, album.AlbumID);
584
 
                                        if (Core.Database != null && item is Photo)
585
 
                                                Core.Database.Exports.Create ((item as Photo).Id,
586
 
                                                                              (item as Photo).DefaultVersionId,
587
 
                                                                              ExportStore.SmugMugExportType,
588
 
                                                                              account.SmugMug.GetAlbumUrl (image_id).ToString ());
589
 
 
590
 
                                        sent_bytes += file_info.Length;
591
 
 
592
 
                                        if (album_uri == null)
593
 
                                                album_uri = account.SmugMug.GetAlbumUrl (image_id);
594
 
                                } catch (System.Exception e) {
595
 
                                        progress_dialog.Message = String.Format (Mono.Unix.Catalog.GetString ("Error Uploading To Gallery: {0}"),
596
 
                                                                                 e.Message);
597
 
                                        progress_dialog.ProgressText = Mono.Unix.Catalog.GetString ("Error");
598
 
                                        System.Console.WriteLine (e);
599
 
 
600
 
                                        if (progress_dialog.PerformRetrySkip ())
601
 
                                                photo_index--;
602
 
                                }
603
 
                        }
604
 
                                
605
 
                        progress_dialog.Message = Catalog.GetString ("Done Sending Photos");
606
 
                        progress_dialog.Fraction = 1.0;
607
 
                        progress_dialog.ProgressText = Mono.Unix.Catalog.GetString ("Upload Complete");
608
 
                        progress_dialog.ButtonLabel = Gtk.Stock.Ok;
609
 
 
610
 
                        if (browser && album_uri != null) {
611
 
                                GnomeUtil.UrlShow (album_uri.ToString ());
612
 
                        }
613
 
                }
614
 
 
615
 
                private void HandleScaleCheckToggled (object o, EventArgs e)
616
 
                {
617
 
                        rotate_check.Sensitive = !scale_check.Active;
618
 
                }
619
 
                
620
 
                private void PopulateSmugMugOptionMenu (SmugMugAccountManager manager, SmugMugAccount changed_account)
621
 
                {
622
 
                        Gtk.Menu menu = new Gtk.Menu ();
623
 
                        this.account = changed_account;
624
 
                        int pos = -1;
625
 
 
626
 
                        accounts = manager.GetAccounts ();
627
 
                        if (accounts == null || accounts.Count == 0) {
628
 
                                Gtk.MenuItem item = new Gtk.MenuItem (Mono.Unix.Catalog.GetString ("(No Gallery)"));
629
 
                                menu.Append (item);
630
 
                                gallery_optionmenu.Sensitive = false;
631
 
                                edit_button.Sensitive = false;
632
 
                        } else {
633
 
                                int i = 0;
634
 
                                foreach (SmugMugAccount account in accounts) {
635
 
                                        if (account == changed_account)
636
 
                                                pos = i;
637
 
                                        
638
 
                                        Gtk.MenuItem item = new Gtk.MenuItem (account.Username);
639
 
                                        menu.Append (item);             
640
 
                                        i++;
641
 
                                }
642
 
                                gallery_optionmenu.Sensitive = true;
643
 
                                edit_button.Sensitive = true;
644
 
                        }
645
 
 
646
 
                        menu.ShowAll ();
647
 
                        gallery_optionmenu.Menu = menu;
648
 
                        gallery_optionmenu.SetHistory ((uint)pos);
649
 
                }
650
 
 
651
 
                private void Connect ()
652
 
                {
653
 
                        Connect (null);
654
 
                }
655
 
 
656
 
                private void Connect (SmugMugAccount selected)
657
 
                {
658
 
                        Connect (selected, null);
659
 
                }
660
 
 
661
 
                private void Connect (SmugMugAccount selected, string text)
662
 
                {
663
 
                        try {
664
 
                                if (accounts.Count != 0 && connect) {
665
 
                                        if (selected == null)
666
 
                                                account = (SmugMugAccount) accounts [gallery_optionmenu.History];
667
 
                                        else
668
 
                                                account = selected;
669
 
 
670
 
                                        if (!account.Connected)
671
 
                                                account.Connect ();
672
 
                                        
673
 
                                        PopulateAlbumOptionMenu (account.SmugMug);
674
 
                                }
675
 
                        } catch (System.Exception) {
676
 
                                System.Console.WriteLine ("Can not connect to SmugMug. Bad username ? password ? network connection ?");
677
 
                                //System.Console.WriteLine ("{0}",ex);
678
 
                                if (selected != null)
679
 
                                        account = selected;
680
 
 
681
 
                                PopulateAlbumOptionMenu (account.SmugMug);
682
 
 
683
 
                                status_label.Text = String.Empty;
684
 
                                album_button.Sensitive = false;
685
 
                                
686
 
                                new SmugMugAccountDialog (this.Dialog, account);
687
 
                        } 
688
 
                }
689
 
 
690
 
                private void HandleAccountSelected (object sender, System.EventArgs args)
691
 
                {
692
 
                        Connect ();
693
 
                }
694
 
 
695
 
                public void HandleAlbumAdded (string title) {
696
 
                        SmugMugAccount account = (SmugMugAccount) accounts [gallery_optionmenu.History];
697
 
                        PopulateAlbumOptionMenu (account.SmugMug);
698
 
                                
699
 
                        // make the newly created album selected
700
 
                        Album[] albums = account.SmugMug.GetAlbums();
701
 
                        for (int i=0; i < albums.Length; i++) {
702
 
                                if (((Album)albums[i]).Title == title) {
703
 
                                        album_optionmenu.SetHistory((uint)i);
704
 
                                }
705
 
                        }
706
 
                }
707
 
 
708
 
                private void PopulateAlbumOptionMenu (SmugMugApi smugmug)
709
 
                {
710
 
                        Album[] albums = null;
711
 
                        if (smugmug != null) {
712
 
                                try {
713
 
                                        albums = smugmug.GetAlbums();
714
 
                                } catch (Exception) {
715
 
                                        Console.WriteLine("Can't get the albums");
716
 
                                        smugmug = null;
717
 
                                }
718
 
                        }
719
 
 
720
 
                        Gtk.Menu menu = new Gtk.Menu ();
721
 
 
722
 
                        bool disconnected = smugmug == null || !account.Connected || albums == null;
723
 
 
724
 
                        if (disconnected || albums.Length == 0) {
725
 
                                string msg = disconnected ? Mono.Unix.Catalog.GetString ("(Not Connected)") 
726
 
                                        : Mono.Unix.Catalog.GetString ("(No Albums)");
727
 
 
728
 
                                Gtk.MenuItem item = new Gtk.MenuItem (msg);
729
 
                                menu.Append (item);
730
 
 
731
 
                                export_button.Sensitive = false;
732
 
                                album_optionmenu.Sensitive = false;
733
 
                                album_button.Sensitive = false;
734
 
                        } else {
735
 
                                foreach (Album album in albums) {
736
 
                                        System.Text.StringBuilder label_builder = new System.Text.StringBuilder ();
737
 
                                        
738
 
                                        label_builder.Append (album.Title);
739
 
 
740
 
                                        Gtk.MenuItem item = new Gtk.MenuItem (label_builder.ToString ());
741
 
                                        ((Gtk.Label)item.Child).UseUnderline = false;
742
 
                                        menu.Append (item);
743
 
                                }
744
 
 
745
 
                                export_button.Sensitive = items.Length > 0;
746
 
                                album_optionmenu.Sensitive = true;
747
 
                                album_button.Sensitive = true;
748
 
                        }
749
 
 
750
 
                        menu.ShowAll ();
751
 
                        album_optionmenu.Menu = menu;
752
 
                }
753
 
                
754
 
                public void HandleAddGallery (object sender, System.EventArgs args)
755
 
                {
756
 
                        gallery_add = new SmugMugAccountDialog (this.Dialog);
757
 
                }
758
 
                
759
 
                public void HandleEditGallery (object sender, System.EventArgs args)
760
 
                {
761
 
                        gallery_add = new SmugMugAccountDialog (this.Dialog, account);
762
 
                }
763
 
 
764
 
                public void HandleAddAlbum (object sender, System.EventArgs args)
765
 
                {
766
 
                        if (account == null)
767
 
                                throw new Exception (Catalog.GetString ("No account selected"));
768
 
                                
769
 
                        album_add = new SmugMugAddAlbum (this, account.SmugMug);
770
 
                }
771
 
 
772
 
                void LoadPreference (string key)
773
 
                {
774
 
                        object val = Preferences.Get (key);
775
 
 
776
 
                        if (val == null)
777
 
                                return;
778
 
 
779
 
                        //System.Console.WriteLine ("Setting {0} to {1}", key, val);
780
 
 
781
 
                        switch (key) {
782
 
                        case SCALE_KEY:
783
 
                                if (scale_check.Active != (bool) val) {
784
 
                                        scale_check.Active = (bool) val;
785
 
                                        rotate_check.Sensitive = !(bool) val;
786
 
                                }
787
 
                                break;
788
 
 
789
 
                        case SIZE_KEY:
790
 
                                size_spin.Value = (double) (int) val;
791
 
                                break;
792
 
                        
793
 
                        case BROWSER_KEY:
794
 
                                if (browser_check.Active != (bool) val)
795
 
                                        browser_check.Active = (bool) val;
796
 
                                break;
797
 
                        
798
 
                        case ROTATE_KEY:
799
 
                                if (rotate_check.Active != (bool) val)
800
 
                                        rotate_check.Active = (bool) val;
801
 
                                break;
802
 
                        }
803
 
                }
804
 
 
805
 
                protected void HandleCloseEvent (object sender, System.EventArgs args) 
806
 
                {
807
 
                        account.SmugMug.Logout ();
808
 
                }
809
 
 
810
 
                private Gtk.Dialog Dialog {
811
 
                        get {
812
 
                                if (dialog == null)
813
 
                                        dialog = (Gtk.Dialog) xml.GetWidget (dialog_name);
814
 
 
815
 
                                return dialog;
816
 
                        }
817
 
                }
818
 
        }
819
 
}