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

« back to all changes in this revision

Viewing changes to extensions/Exporters/GalleryExport/GalleryExport.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
using System;
 
2
using System.Net;
 
3
using System.IO;
 
4
using System.Text;
 
5
using System.Collections;
 
6
using System.Collections.Specialized;
 
7
using System.Web;
 
8
using Mono.Unix;
 
9
 
 
10
using FSpot;
 
11
using FSpot.Filters;
 
12
using FSpot.Widgets;
 
13
using FSpot.Utils;
 
14
using FSpot.UI.Dialog;
 
15
using FSpot.Extensions;
 
16
 
 
17
using GalleryRemote;
 
18
 
 
19
namespace G2Export {
 
20
        public class GalleryAccount {
 
21
                public GalleryAccount (string name, string url, string username, string password) : this (name, url, username, password, GalleryVersion.VersionUnknown) {}
 
22
                public GalleryAccount (string name, string url, string username, string password, GalleryVersion version)
 
23
                {
 
24
                        this.name = name;
 
25
                        this.username = username;
 
26
                        this.password = password;
 
27
                        this.Url = url;
 
28
 
 
29
                        if (version != GalleryVersion.VersionUnknown) {
 
30
                                this.version = version;
 
31
                        } else {
 
32
                                this.version = Gallery.DetectGalleryVersion(Url);
 
33
                        }
 
34
                }
 
35
 
 
36
                public const string EXPORT_SERVICE = "gallery/";
 
37
                public const string LIGHTTPD_WORKAROUND_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "lighttpd_workaround";
 
38
 
 
39
                public Gallery Connect ()
 
40
                {
 
41
                        //System.Console.WriteLine ("GalleryAccount.Connect()");
 
42
                        Gallery gal = null;
 
43
 
 
44
                        if (version == GalleryVersion.VersionUnknown)
 
45
                                this.version = Gallery.DetectGalleryVersion(Url);
 
46
 
 
47
                        if (version == GalleryVersion.Version1) {
 
48
                                gal = new Gallery1 (url, url);
 
49
                        } else if (version == GalleryVersion.Version2) {
 
50
                                gal = new Gallery2 (url, url);
 
51
                        } else {
 
52
                                throw new GalleryException (Catalog.GetString("Cannot connect to a Gallery for which the version is unknown.\nPlease check that you have Remote plugin 1.0.8 or later"));
 
53
                        }
 
54
 
 
55
                        System.Console.WriteLine ("Gallery created: " + gal);
 
56
 
 
57
                        gal.Login (username, password);
 
58
 
 
59
                        gallery = gal;
 
60
                        connected = true;
 
61
 
 
62
                        object val = Preferences.Get (LIGHTTPD_WORKAROUND_KEY);
 
63
                        if (val != null)
 
64
                                gallery.expect_continue = !(bool)val;
 
65
 
 
66
                        return gallery;
 
67
                }
 
68
 
 
69
                GalleryVersion version;
 
70
                public GalleryVersion Version{
 
71
                        get {
 
72
                                return version;
 
73
                        }
 
74
                }
 
75
 
 
76
                private bool connected;
 
77
                public bool Connected {
 
78
                        get {
 
79
                                bool retVal = false;
 
80
                                if(gallery != null) {
 
81
                                        retVal = gallery.IsConnected ();
 
82
                                }
 
83
                                if (connected != retVal) {
 
84
                                        System.Console.WriteLine ("Connected and retVal for IsConnected() don't agree");
 
85
                                }
 
86
                                return retVal;
 
87
                        }
 
88
                }
 
89
 
 
90
                public void MarkChanged ()
 
91
                {
 
92
                        connected = false;
 
93
                        gallery = null;
 
94
                }
 
95
 
 
96
                Gallery gallery;
 
97
                public Gallery Gallery {
 
98
                        get {
 
99
                                return gallery;
 
100
                        }
 
101
                }
 
102
 
 
103
                string name;
 
104
                public string Name {
 
105
                        get {
 
106
                                return name;
 
107
                        }
 
108
                        set {
 
109
                                name = value;
 
110
                        }
 
111
                }
 
112
 
 
113
                string url;
 
114
                public string Url {
 
115
                        get {
 
116
                                return url;
 
117
                        }
 
118
                        set {
 
119
                                if (url != value) {
 
120
                                        url = value;
 
121
                                        MarkChanged ();
 
122
                                }
 
123
                        }
 
124
                }
 
125
 
 
126
                string username;
 
127
                public string Username {
 
128
                        get {
 
129
                                return username;
 
130
                        }
 
131
                        set {
 
132
                                if (username != value) {
 
133
                                        username = value;
 
134
                                        MarkChanged ();
 
135
                                }
 
136
                        }
 
137
                }
 
138
 
 
139
                string password;
 
140
                public string Password {
 
141
                        get {
 
142
                                return password;
 
143
                        }
 
144
                        set {
 
145
                                if (password != value) {
 
146
                                        password = value;
 
147
                                        MarkChanged ();
 
148
                                }
 
149
                        }
 
150
                }
 
151
        }
 
152
 
 
153
 
 
154
        public class GalleryAccountManager
 
155
        {
 
156
                private static GalleryAccountManager instance;
 
157
                string xml_path;
 
158
                ArrayList accounts;
 
159
 
 
160
                public delegate void AccountListChangedHandler (GalleryAccountManager manager, GalleryAccount changed_account);
 
161
                public event AccountListChangedHandler AccountListChanged;
 
162
 
 
163
                public static GalleryAccountManager GetInstance ()
 
164
                {
 
165
                        if (instance == null) {
 
166
                                instance = new GalleryAccountManager ();
 
167
                        }
 
168
 
 
169
                        return instance;
 
170
                }
 
171
 
 
172
                private GalleryAccountManager ()
 
173
                {
 
174
                        // FIXME this xml file path should be be retrieved from a central location not hard coded there
 
175
                        this.xml_path = System.IO.Path.Combine (FSpot.Global.BaseDirectory, "Accounts.xml");
 
176
 
 
177
                        accounts = new ArrayList ();
 
178
                        ReadAccounts ();
 
179
                }
 
180
 
 
181
                public void MarkChanged ()
 
182
                {
 
183
                        MarkChanged (true, null);
 
184
                }
 
185
 
 
186
                public void MarkChanged (bool write, GalleryAccount changed_account)
 
187
                {
 
188
                        if (write)
 
189
                                WriteAccounts ();
 
190
 
 
191
                        if (AccountListChanged != null)
 
192
                                AccountListChanged (this, changed_account);
 
193
                }
 
194
 
 
195
                public ArrayList GetAccounts ()
 
196
                {
 
197
                        return accounts;
 
198
                }
 
199
 
 
200
                public void AddAccount (GalleryAccount account)
 
201
                {
 
202
                        AddAccount (account, true);
 
203
                }
 
204
 
 
205
                public void AddAccount (GalleryAccount account, bool write)
 
206
                {
 
207
                        accounts.Add (account);
 
208
                        MarkChanged (write, account);
 
209
                }
 
210
 
 
211
                public void RemoveAccount (GalleryAccount account)
 
212
                {
 
213
                        accounts.Remove (account);
 
214
                        MarkChanged ();
 
215
                }
 
216
 
 
217
                public void WriteAccounts ()
 
218
                {
 
219
                        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter (xml_path, System.Text.Encoding.Default);
 
220
 
 
221
                        writer.Formatting = System.Xml.Formatting.Indented;
 
222
                        writer.Indentation = 2;
 
223
                        writer.IndentChar = ' ';
 
224
 
 
225
                        writer.WriteStartDocument (true);
 
226
 
 
227
                        writer.WriteStartElement ("GalleryRemote");
 
228
                        foreach (GalleryAccount account in accounts) {
 
229
                                writer.WriteStartElement ("Account");
 
230
                                writer.WriteElementString ("Name", account.Name);
 
231
 
 
232
                                writer.WriteElementString ("Url", account.Url);
 
233
                                writer.WriteElementString ("Username", account.Username);
 
234
                                writer.WriteElementString ("Password", account.Password);
 
235
                                writer.WriteElementString ("Version", account.Version.ToString());
 
236
                                writer.WriteEndElement (); //Account
 
237
                        }
 
238
                        writer.WriteEndElement ();
 
239
                        writer.WriteEndDocument ();
 
240
                        writer.Close ();
 
241
                }
 
242
 
 
243
                private GalleryAccount ParseAccount (System.Xml.XmlNode node)
 
244
                {
 
245
                        if (node.Name != "Account")
 
246
 
 
247
                                return null;
 
248
 
 
249
                        string name = null;
 
250
                        string url = null;
 
251
                        string username = null;
 
252
                        string password = null;
 
253
                        GalleryVersion version = GalleryVersion.VersionUnknown;
 
254
 
 
255
                        foreach (System.Xml.XmlNode child in node.ChildNodes) {
 
256
                                if (child.Name == "Name") {
 
257
                                        name = child.ChildNodes [0].Value;
 
258
 
 
259
                                } else if (child.Name == "Url") {
 
260
                                        url = child.ChildNodes [0].Value;
 
261
                                } else if (child.Name == "Password") {
 
262
                                        password = child.ChildNodes [0].Value;
 
263
                                } else if (child.Name == "Username") {
 
264
                                        username = child.ChildNodes [0].Value;
 
265
                                } else if (child.Name == "Version") {
 
266
                                        string versionString = child.ChildNodes [0].Value;
 
267
                                        if (versionString == "Version1")
 
268
                                                version = GalleryVersion.Version1;
 
269
                                        else if (versionString == "Version2")
 
270
                                                version = GalleryVersion.Version2;
 
271
                                        else
 
272
                                                Console.WriteLine ("Unexpected versions string: " + versionString);
 
273
                                }
 
274
                        }
 
275
                        return new GalleryAccount (name, url, username, password, version);
 
276
                }
 
277
 
 
278
                private void ReadAccounts ()
 
279
                {
 
280
 
 
281
                        if (! File.Exists (xml_path)) {
 
282
                                MarkChanged ();
 
283
                                return;
 
284
                        }
 
285
 
 
286
                        try {
 
287
                                string query = "//GalleryRemote/Account";
 
288
                                System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();
 
289
 
 
290
                                //System.Console.WriteLine ("xml_path: " + xml_path);
 
291
                                doc.Load (xml_path);
 
292
                                System.Xml.XmlNodeList nodes = doc.SelectNodes (query);
 
293
 
 
294
                                //System.Console.WriteLine ("selected {0} nodes match {1}", nodes.Count, query);
 
295
                                foreach (System.Xml.XmlNode node in nodes) {
 
296
                                        GalleryAccount account = ParseAccount (node);
 
297
                                        if (account != null)
 
298
                                                AddAccount (account, false);
 
299
 
 
300
                                }
 
301
                        } catch (System.Exception e) {
 
302
                                // FIXME do something
 
303
                                System.Console.WriteLine ("Exception loading gallery accounts");
 
304
                                System.Console.WriteLine (e);
 
305
                        }
 
306
 
 
307
                        MarkChanged ();
 
308
                }
 
309
        }
 
310
 
 
311
        public class AccountDialog {
 
312
                public AccountDialog (Gtk.Window parent) : this (parent, null, false) {
 
313
                        add_dialog.Response += HandleAddResponse;
 
314
                        add_button.Sensitive = false;
 
315
                }
 
316
 
 
317
                public AccountDialog (Gtk.Window parent, GalleryAccount account, bool show_error)
 
318
                {
 
319
                        Glade.XML xml = new Glade.XML (null, "GalleryExport.glade", "gallery_add_dialog", "f-spot");
 
320
                        xml.Autoconnect (this);
 
321
                        add_dialog = (Gtk.Dialog) xml.GetWidget ("gallery_add_dialog");
 
322
                        add_dialog.Modal = false;
 
323
                        add_dialog.TransientFor = parent;
 
324
                        add_dialog.DefaultResponse = Gtk.ResponseType.Ok;
 
325
 
 
326
                        this.account = account;
 
327
 
 
328
                        status_area.Visible = show_error;
 
329
 
 
330
                        if (account != null) {
 
331
                                gallery_entry.Text = account.Name;
 
332
                                url_entry.Text = account.Url;
 
333
                                password_entry.Text = account.Password;
 
334
                                username_entry.Text = account.Username;
 
335
                                add_button.Label = Gtk.Stock.Ok;
 
336
                                add_dialog.Response += HandleEditResponse;
 
337
                        }
 
338
 
 
339
                        if (remove_button != null)
 
340
                                remove_button.Visible = account != null;
 
341
 
 
342
                        add_dialog.Show ();
 
343
 
 
344
                        gallery_entry.Changed += HandleChanged;
 
345
                        url_entry.Changed += HandleChanged;
 
346
                        password_entry.Changed += HandleChanged;
 
347
                        username_entry.Changed += HandleChanged;
 
348
                        HandleChanged (null, null);
 
349
                }
 
350
 
 
351
                private void HandleChanged (object sender, System.EventArgs args)
 
352
                {
 
353
                        name = gallery_entry.Text;
 
354
                        url = url_entry.Text;
 
355
                        password = password_entry.Text;
 
356
                        username = username_entry.Text;
 
357
 
 
358
                        if (name == String.Empty || url == String.Empty || password == String.Empty || username == String.Empty)
 
359
                                add_button.Sensitive = false;
 
360
                        else
 
361
                                add_button.Sensitive = true;
 
362
 
 
363
                }
 
364
 
 
365
                [GLib.ConnectBefore]
 
366
                protected void HandleAddResponse (object sender, Gtk.ResponseArgs args)
 
367
                {
 
368
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
 
369
                                try {
 
370
                                        Uri uri = new Uri (url);
 
371
                                        if (uri.Scheme != Uri.UriSchemeHttp &&
 
372
                                            uri.Scheme != Uri.UriSchemeHttps)
 
373
                                                throw new System.UriFormatException ();
 
374
 
 
375
                                        //Check for name uniqueness
 
376
                                        foreach (GalleryAccount acc in GalleryAccountManager.GetInstance ().GetAccounts ())
 
377
                                                if (acc.Name == name)
 
378
                                                        throw new ArgumentException ("name");
 
379
                                        GalleryAccount created = new GalleryAccount (name,
 
380
                                                                                     url,
 
381
                                                                                     username,
 
382
                                                                                     password);
 
383
 
 
384
                                        created.Connect ();
 
385
                                        GalleryAccountManager.GetInstance ().AddAccount (created);
 
386
                                        account = created;
 
387
                                } catch (System.UriFormatException) {
 
388
                                        HigMessageDialog md =
 
389
                                                new HigMessageDialog (add_dialog,
 
390
                                                                      Gtk.DialogFlags.Modal |
 
391
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
392
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
393
                                                                      Catalog.GetString ("Invalid URL"),
 
394
                                                                      Catalog.GetString ("The gallery URL entry does not appear to be a valid URL"));
 
395
                                        md.Run ();
 
396
                                        md.Destroy ();
 
397
                                        return;
 
398
                                } catch (GalleryRemote.GalleryException e) {
 
399
                                        HigMessageDialog md =
 
400
                                                new HigMessageDialog (add_dialog,
 
401
                                                                      Gtk.DialogFlags.Modal |
 
402
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
403
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
404
                                                                      Catalog.GetString ("Error while connecting to Gallery"),
 
405
                                                                      String.Format (Catalog.GetString ("The following error was encountered while attempting to log in: {0}"), e.Message));
 
406
                                        if (e.ResponseText != null) {
 
407
                                                System.Console.WriteLine (e.Message);
 
408
                                                System.Console.WriteLine (e.ResponseText);
 
409
                                        }
 
410
                                        md.Run ();
 
411
                                        md.Destroy ();
 
412
                                        return;
 
413
                                } catch (ArgumentException ae) {
 
414
                                        HigMessageDialog md =
 
415
                                                new HigMessageDialog (add_dialog,
 
416
                                                                      Gtk.DialogFlags.Modal |
 
417
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
418
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
419
                                                                      Catalog.GetString ("A Gallery with this name already exists"),
 
420
                                                                      String.Format (Catalog.GetString ("There is already a Gallery with the same name in your registered Galleries. Please choose a unique name.")));
 
421
                                        System.Console.WriteLine (ae);
 
422
                                        md.Run ();
 
423
                                        md.Destroy ();
 
424
                                        return;
 
425
                                } catch (System.Net.WebException we) {
 
426
                                        HigMessageDialog md =
 
427
                                                new HigMessageDialog (add_dialog,
 
428
                                                                      Gtk.DialogFlags.Modal |
 
429
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
430
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
431
                                                                      Catalog.GetString ("Error while connecting to Gallery"),
 
432
                                                                      String.Format (Catalog.GetString ("The following error was encountered while attempting to log in: {0}"), we.Message));
 
433
                                        md.Run ();
 
434
                                        md.Destroy ();
 
435
                                        return;
 
436
                                } catch (System.Exception se) {
 
437
                                        HigMessageDialog md =
 
438
                                                new HigMessageDialog (add_dialog,
 
439
                                                                      Gtk.DialogFlags.Modal |
 
440
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
441
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
442
                                                                      Catalog.GetString ("Error while connecting to Gallery"),
 
443
                                                                      String.Format (Catalog.GetString ("The following error was encountered while attempting to log in: {0}"), se.Message));
 
444
                                        Console.WriteLine (se);
 
445
                                        md.Run ();
 
446
                                        md.Destroy ();
 
447
                                        return;
 
448
                                }
 
449
                        }
 
450
                        add_dialog.Destroy ();
 
451
                }
 
452
 
 
453
                protected void HandleEditResponse (object sender, Gtk.ResponseArgs args)
 
454
                {
 
455
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
 
456
                                account.Name = name;
 
457
                                account.Url = url;
 
458
                                account.Username = username;
 
459
                                account.Password = password;
 
460
                                GalleryAccountManager.GetInstance ().MarkChanged (true, account);
 
461
                        } else if (args.ResponseId == Gtk.ResponseType.Reject) {
 
462
                                // NOTE we are using Reject to signal the remove action.
 
463
                                GalleryAccountManager.GetInstance ().RemoveAccount (account);
 
464
                        }
 
465
                        add_dialog.Destroy ();
 
466
                }
 
467
 
 
468
                private GalleryAccount account;
 
469
                private string name;
 
470
                private string url;
 
471
                private string password;
 
472
                private string username;
 
473
 
 
474
                // widgets
 
475
                [Glade.Widget] Gtk.Dialog add_dialog;
 
476
 
 
477
                [Glade.Widget] Gtk.Entry url_entry;
 
478
                [Glade.Widget] Gtk.Entry password_entry;
 
479
                [Glade.Widget] Gtk.Entry gallery_entry;
 
480
                [Glade.Widget] Gtk.Entry username_entry;
 
481
 
 
482
                [Glade.Widget] Gtk.Button add_button;
 
483
                [Glade.Widget] Gtk.Button remove_button;
 
484
                [Glade.Widget] Gtk.Button cancel_button;
 
485
 
 
486
                [Glade.Widget] Gtk.HBox status_area;
 
487
        }
 
488
 
 
489
        public class GalleryAddAlbum
 
490
        {
 
491
                [Glade.Widget] Gtk.Dialog add_album_dialog;
 
492
                [Glade.Widget] Gtk.OptionMenu album_optionmenu;
 
493
 
 
494
                [Glade.Widget] Gtk.Entry name_entry;
 
495
                [Glade.Widget] Gtk.Entry description_entry;
 
496
                [Glade.Widget] Gtk.Entry title_entry;
 
497
 
 
498
                [Glade.Widget] Gtk.Button add_button;
 
499
                [Glade.Widget] Gtk.Button cancel_button;
 
500
 
 
501
                private GalleryExport export;
 
502
                private Gallery gallery;
 
503
                private string parent;
 
504
                private string name;
 
505
                private string description;
 
506
                private string title;
 
507
 
 
508
                public GalleryAddAlbum (GalleryExport export, Gallery gallery)
 
509
                {
 
510
                        Glade.XML xml = new Glade.XML (null, "GalleryExport.glade", "gallery_add_album_dialog", "f-spot");
 
511
                        xml.Autoconnect (this);
 
512
                        add_album_dialog = (Gtk.Dialog) xml.GetWidget ("gallery_add_album_dialog");
 
513
                        add_album_dialog.Modal = true;
 
514
                        this.export = export;
 
515
                        this.gallery = gallery;
 
516
                        PopulateAlbums ();
 
517
 
 
518
                        add_album_dialog.Response += HandleAddResponse;
 
519
 
 
520
                        name_entry.Changed += HandleChanged;
 
521
                        description_entry.Changed += HandleChanged;
 
522
                        title_entry.Changed += HandleChanged;
 
523
                        HandleChanged (null, null);
 
524
                }
 
525
 
 
526
                private void PopulateAlbums ()
 
527
                {
 
528
                        Gtk.Menu menu = new Gtk.Menu ();
 
529
                        if (gallery.Version == GalleryVersion.Version1) {
 
530
                                Gtk.MenuItem top_item = new Gtk.MenuItem (Catalog.GetString ("(TopLevel)"));
 
531
                                menu.Append (top_item);
 
532
                        }
 
533
 
 
534
                        foreach (Album album in gallery.Albums) {
 
535
                                System.Text.StringBuilder label_builder = new System.Text.StringBuilder ();
 
536
 
 
537
                                for (int i=0; i < album.Parents.Count; i++) {
 
538
                                        label_builder.Append ("  ");
 
539
                                }
 
540
                                label_builder.Append (album.Title);
 
541
 
 
542
                                Gtk.MenuItem item = new Gtk.MenuItem (label_builder.ToString ());
 
543
                                ((Gtk.Label)item.Child).UseUnderline = false;
 
544
                                menu.Append (item);
 
545
 
 
546
                                AlbumPermission create_sub = album.Perms & AlbumPermission.CreateSubAlbum;
 
547
 
 
548
                                if (create_sub == 0)
 
549
                                        item.Sensitive = false;
 
550
                        }
 
551
 
 
552
                        album_optionmenu.Sensitive = true;
 
553
                        menu.ShowAll ();
 
554
                        album_optionmenu.Menu = menu;
 
555
                }
 
556
 
 
557
                private void HandleChanged (object sender, EventArgs args)
 
558
                {
 
559
                        if (gallery.Version == GalleryVersion.Version1) {
 
560
                                if (gallery.Albums.Count == 0 || album_optionmenu.History <= 0) {
 
561
                                        parent = String.Empty;
 
562
                                } else {
 
563
                                        parent = ((Album) gallery.Albums [album_optionmenu.History-1]).Name;
 
564
                                }
 
565
                        } else {
 
566
                                if (gallery.Albums.Count == 0 || album_optionmenu.History < 0) {
 
567
                                        parent = String.Empty;
 
568
                                } else {
 
569
                                        parent = ((Album) gallery.Albums [album_optionmenu.History]).Name;
 
570
                                }
 
571
                        }
 
572
                        name = name_entry.Text;
 
573
                        description = description_entry.Text;
 
574
                        title = title_entry.Text;
 
575
 
 
576
                        if (name == String.Empty || title == String.Empty)
 
577
                                add_button.Sensitive = false;
 
578
                        else
 
579
                                add_button.Sensitive = true;
 
580
                }
 
581
 
 
582
                [GLib.ConnectBefore]
 
583
                protected void HandleAddResponse (object sender, Gtk.ResponseArgs args)
 
584
                {
 
585
                        if (args.ResponseId == Gtk.ResponseType.Ok) {
 
586
                                if (!System.Text.RegularExpressions.Regex.IsMatch (name, "^[A-Za-z0-9_-]+$")) {
 
587
                                        HigMessageDialog md =
 
588
                                                new HigMessageDialog (add_album_dialog,
 
589
                                                                      Gtk.DialogFlags.Modal |
 
590
                                                                      Gtk.DialogFlags.DestroyWithParent,
 
591
                                                                      Gtk.MessageType.Error, Gtk.ButtonsType.Ok,
 
592
                                                                      Catalog.GetString ("Invalid Gallery name"),
 
593
                                                                      Catalog.GetString ("The gallery name contains invalid characters.\nOnly letters, numbers, - and _ are allowed"));
 
594
                                        md.Run ();
 
595
                                        md.Destroy ();
 
596
                                        return;
 
597
                                }
 
598
                                try {
 
599
                                        gallery.NewAlbum (parent, name, title, description);
 
600
                                        export.HandleAlbumAdded (title);
 
601
                                } catch (GalleryCommandException e) {
 
602
                                        gallery.PopupException(e, add_album_dialog);
 
603
                                        return;
 
604
                                }
 
605
                        }
 
606
                        add_album_dialog.Destroy ();
 
607
                }
 
608
        }
 
609
 
 
610
 
 
611
        public class GalleryExport : IExporter {
 
612
                public GalleryExport ()
 
613
                {
 
614
                }
 
615
 
 
616
                public void Run (IBrowsableCollection selection)
 
617
                {
 
618
                        Glade.XML xml = new Glade.XML (null, "GalleryExport.glade", "gallery_export_dialog", "f-spot");
 
619
                        xml.Autoconnect (this);
 
620
                        export_dialog = (Gtk.Dialog) xml.GetWidget ("gallery_export_dialog");
 
621
 
 
622
                        this.items = selection.Items;
 
623
                        Array.Sort<IBrowsableItem> (this.items as Photo[], new Photo.CompareDateName());
 
624
                        album_button.Sensitive = false;
 
625
                        IconView view = new IconView (selection);
 
626
                        view.DisplayDates = false;
 
627
                        view.DisplayTags = false;
 
628
 
 
629
                        export_dialog.Modal = false;
 
630
                        export_dialog.TransientFor = null;
 
631
 
 
632
                        thumb_scrolledwindow.Add (view);
 
633
                        view.Show ();
 
634
                        export_dialog.Show ();
 
635
 
 
636
                        GalleryAccountManager manager = GalleryAccountManager.GetInstance ();
 
637
                        manager.AccountListChanged += PopulateGalleryOptionMenu;
 
638
                        PopulateGalleryOptionMenu (manager, null);
 
639
 
 
640
                        if (edit_button != null)
 
641
                                edit_button.Clicked += HandleEditGallery;
 
642
 
 
643
                        export_dialog.Response += HandleResponse;
 
644
                        connect = true;
 
645
                        HandleSizeActive (null, null);
 
646
                        Connect ();
 
647
 
 
648
                        LoadPreference (SCALE_KEY);
 
649
                        LoadPreference (SIZE_KEY);
 
650
                        LoadPreference (BROWSER_KEY);
 
651
                        LoadPreference (META_KEY);
 
652
                        LoadPreference (ROTATE_KEY);
 
653
                }
 
654
 
 
655
                public const string EXPORT_SERVICE = "gallery/";
 
656
                public const string SCALE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "scale";
 
657
                public const string SIZE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "size";
 
658
                public const string BROWSER_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "browser";
 
659
                public const string META_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "meta";
 
660
                public const string ROTATE_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "rotate";
 
661
                public const string LIGHTTPD_WORKAROUND_KEY = Preferences.APP_FSPOT_EXPORT + EXPORT_SERVICE + "lighttpd_workaround";
 
662
 
 
663
                private bool scale;
 
664
                private bool rotate;
 
665
                private int size;
 
666
                private bool browser;
 
667
                private bool meta;
 
668
                private bool connect = false;
 
669
 
 
670
                IBrowsableItem[] items;
 
671
                int photo_index;
 
672
                FSpot.ThreadProgressDialog progress_dialog;
 
673
 
 
674
                ArrayList accounts;
 
675
                private GalleryAccount account;
 
676
                private Album album;
 
677
 
 
678
                private string xml_path;
 
679
 
 
680
                // Widgets
 
681
                [Glade.Widget] Gtk.Dialog export_dialog;
 
682
                [Glade.Widget] Gtk.OptionMenu gallery_optionmenu;
 
683
                [Glade.Widget] Gtk.OptionMenu album_optionmenu;
 
684
 
 
685
                [Glade.Widget] Gtk.Entry width_entry;
 
686
                [Glade.Widget] Gtk.Entry height_entry;
 
687
 
 
688
                [Glade.Widget] Gtk.CheckButton browser_check;
 
689
                [Glade.Widget] Gtk.CheckButton scale_check;
 
690
                [Glade.Widget] Gtk.CheckButton meta_check;
 
691
                [Glade.Widget] Gtk.CheckButton rotate_check;
 
692
 
 
693
                [Glade.Widget] Gtk.SpinButton size_spin;
 
694
 
 
695
                [Glade.Widget] Gtk.Button album_button;
 
696
                [Glade.Widget] Gtk.Button add_button;
 
697
                [Glade.Widget] Gtk.Button edit_button;
 
698
 
 
699
                [Glade.Widget] Gtk.Button export_button;
 
700
                [Glade.Widget] Gtk.Button cancel_button;
 
701
 
 
702
                [Glade.Widget] Gtk.ScrolledWindow thumb_scrolledwindow;
 
703
 
 
704
                System.Threading.Thread command_thread;
 
705
 
 
706
 
 
707
                private void HandleResponse (object sender, Gtk.ResponseArgs args)
 
708
                {
 
709
                        if (args.ResponseId != Gtk.ResponseType.Ok) {
 
710
                                export_dialog.Destroy ();
 
711
                                return;
 
712
                        }
 
713
 
 
714
                        if (scale_check != null) {
 
715
                                scale = scale_check.Active;
 
716
                                size = size_spin.ValueAsInt;
 
717
                        } else
 
718
                                scale = false;
 
719
 
 
720
                        browser = browser_check.Active;
 
721
                        meta = meta_check.Active;
 
722
                        rotate = rotate_check.Active;
 
723
 
 
724
                        if (account != null) {
 
725
                                //System.Console.WriteLine ("history = {0}", album_optionmenu.History);
 
726
                                album = (Album) account.Gallery.Albums [Math.Max (0, album_optionmenu.History)];
 
727
                                photo_index = 0;
 
728
 
 
729
                                export_dialog.Destroy ();
 
730
 
 
731
                                command_thread = new System.Threading.Thread (new System.Threading.ThreadStart (this.Upload));
 
732
                                command_thread.Name = Catalog.GetString ("Uploading Pictures");
 
733
 
 
734
                                progress_dialog = new FSpot.ThreadProgressDialog (command_thread, items.Length);
 
735
                                progress_dialog.Start ();
 
736
 
 
737
                                // Save these settings for next time
 
738
                                Preferences.Set (SCALE_KEY, scale);
 
739
                                Preferences.Set (SIZE_KEY, size);
 
740
                                Preferences.Set (BROWSER_KEY, browser);
 
741
                                Preferences.Set (META_KEY, meta);
 
742
                                Preferences.Set (ROTATE_KEY, rotate);
 
743
                        }
 
744
                }
 
745
 
 
746
                private void HandleProgressChanged (ProgressItem item)
 
747
                {
 
748
                        //System.Console.WriteLine ("Changed value = {0}", item.Value);
 
749
                        progress_dialog.Fraction = (photo_index - 1.0 + item.Value) / (double) items.Length;
 
750
                }
 
751
 
 
752
                public void HandleSizeActive (object sender, EventArgs args)
 
753
                {
 
754
                        size_spin.Sensitive = scale_check.Active;
 
755
                }
 
756
 
 
757
 
 
758
                private void Upload ()
 
759
                {
 
760
                                account.Gallery.Progress = new ProgressItem ();
 
761
                                account.Gallery.Progress.Changed += HandleProgressChanged;
 
762
 
 
763
                                System.Console.WriteLine ("Starting upload");
 
764
 
 
765
                                FilterSet filters = new FilterSet ();
 
766
                                if (account.Version == GalleryVersion.Version1)
 
767
                                        filters.Add (new WhiteListFilter (new string []{".jpg", ".jpeg", ".png", ".gif"}));
 
768
                                if (scale)
 
769
                                        filters.Add (new ResizeFilter ((uint) size));
 
770
                                else if (rotate)
 
771
                                        filters.Add (new OrientationFilter ());
 
772
 
 
773
 
 
774
                                while (photo_index < items.Length) {
 
775
                                        IBrowsableItem item = items [photo_index];
 
776
 
 
777
                                        System.Console.WriteLine ("uploading {0}", photo_index);
 
778
 
 
779
                                        progress_dialog.Message = System.String.Format (Catalog.GetString ("Uploading picture \"{0}\""), item.Name);
 
780
                                        progress_dialog.Fraction = photo_index / (double) items.Length;
 
781
                                        photo_index++;
 
782
 
 
783
                                        progress_dialog.ProgressText = System.String.Format (Catalog.GetString ("{0} of {1}"), photo_index, items.Length);
 
784
 
 
785
 
 
786
                                        FilterRequest req = new FilterRequest (item.DefaultVersionUri);
 
787
 
 
788
                                        filters.Convert (req);
 
789
                                        try {
 
790
                                                int id = album.Add (item, req.Current.LocalPath);
 
791
 
 
792
                                                if (item != null && item is Photo && Core.Database != null && id != 0) {
 
793
                                                        Core.Database.Exports.Create ((item as Photo).Id, (item as Photo).DefaultVersionId,
 
794
                                                                                      ExportStore.Gallery2ExportType,
 
795
                                                                                      String.Format("{0}:{1}",album.Gallery.Uri.ToString (), id.ToString ()));
 
796
                                                }
 
797
                                        } catch (System.Exception e) {
 
798
                                                progress_dialog.Message = String.Format (Catalog.GetString ("Error uploading picture \"{0}\" to Gallery: {1}"), item.Name, e.Message);
 
799
                                                progress_dialog.ProgressText = Catalog.GetString ("Error");
 
800
                                                Console.WriteLine (e);
 
801
 
 
802
                                                if (progress_dialog.PerformRetrySkip ()) {
 
803
                                                        photo_index--;
 
804
                                                }
 
805
                                        }
 
806
                        }
 
807
 
 
808
                        progress_dialog.Message = Catalog.GetString ("Done Sending Photos");
 
809
                        progress_dialog.Fraction = 1.0;
 
810
                        progress_dialog.ProgressText = Catalog.GetString ("Upload Complete");
 
811
                        progress_dialog.ButtonLabel = Gtk.Stock.Ok;
 
812
 
 
813
                        if (browser) {
 
814
                                GnomeUtil.UrlShow (album.GetUrl());
 
815
                        }
 
816
                }
 
817
 
 
818
                private void PopulateGalleryOptionMenu (GalleryAccountManager manager, GalleryAccount changed_account)
 
819
                {
 
820
                        Gtk.Menu menu = new Gtk.Menu ();
 
821
                        this.account = changed_account;
 
822
                        int pos = -1;
 
823
 
 
824
                        accounts = manager.GetAccounts ();
 
825
                        if (accounts == null || accounts.Count == 0) {
 
826
                                Gtk.MenuItem item = new Gtk.MenuItem (Catalog.GetString ("(No Gallery)"));
 
827
                                menu.Append (item);
 
828
                                gallery_optionmenu.Sensitive = false;
 
829
                                edit_button.Sensitive = false;
 
830
                        } else {
 
831
                                int i = 0;
 
832
                                foreach (GalleryAccount account in accounts) {
 
833
                                        if (account == changed_account)
 
834
                                                pos = i;
 
835
 
 
836
                                        Gtk.MenuItem item = new Gtk.MenuItem (account.Name);
 
837
                                        menu.Append (item);
 
838
                                        i++;
 
839
                                }
 
840
                                gallery_optionmenu.Sensitive = true;
 
841
                                edit_button.Sensitive = true;
 
842
                        }
 
843
 
 
844
                        menu.ShowAll ();
 
845
                        gallery_optionmenu.Menu = menu;
 
846
                        gallery_optionmenu.SetHistory ((uint)pos);
 
847
                }
 
848
 
 
849
                private void Connect ()
 
850
                {
 
851
                        Connect (null);
 
852
                }
 
853
 
 
854
                private void Connect (GalleryAccount selected)
 
855
                {
 
856
                        try {
 
857
                                if (accounts.Count != 0 && connect) {
 
858
                                        if (selected == null)
 
859
                                                account = (GalleryAccount) accounts [gallery_optionmenu.History];
 
860
                                        else
 
861
                                                account = selected;
 
862
 
 
863
                                        if (!account.Connected)
 
864
                                                account.Connect ();
 
865
 
 
866
                                        PopulateAlbumOptionMenu (account.Gallery);
 
867
                                        album_button.Sensitive = true;
 
868
                                }
 
869
                        } catch (System.Exception ex) {
 
870
                                if (selected != null)
 
871
                                        account = selected;
 
872
 
 
873
                                System.Console.WriteLine ("{0}",ex);
 
874
                                PopulateAlbumOptionMenu (account.Gallery);
 
875
                                album_button.Sensitive = false;
 
876
 
 
877
                                new AccountDialog (export_dialog, account, true);
 
878
                        }
 
879
                }
 
880
 
 
881
                private void HandleAccountSelected (object sender, System.EventArgs args)
 
882
                {
 
883
                        Connect ();
 
884
                }
 
885
 
 
886
                public void HandleAlbumAdded (string title) {
 
887
                        GalleryAccount account = (GalleryAccount) accounts [gallery_optionmenu.History];
 
888
                        PopulateAlbumOptionMenu (account.Gallery);
 
889
 
 
890
                        // make the newly created album selected
 
891
                        ArrayList albums = account.Gallery.Albums;
 
892
                        for (int i=0; i < albums.Count; i++) {
 
893
                                if (((Album)albums[i]).Title == title) {
 
894
                                        album_optionmenu.SetHistory((uint)i);
 
895
                                }
 
896
                        }
 
897
                }
 
898
 
 
899
                private void PopulateAlbumOptionMenu (Gallery gallery)
 
900
                {
 
901
                        System.Collections.ArrayList albums = null;
 
902
                        if (gallery != null) {
 
903
                                //gallery.FetchAlbumsPrune ();
 
904
                                try {
 
905
                                        gallery.FetchAlbums ();
 
906
                                        albums = gallery.Albums;
 
907
                                } catch (GalleryCommandException e) {
 
908
                                        gallery.PopupException (e, export_dialog);
 
909
                                        return;
 
910
                                }
 
911
                        }
 
912
 
 
913
                        Gtk.Menu menu = new Gtk.Menu ();
 
914
 
 
915
                        bool disconnected = gallery == null || !account.Connected || albums == null;
 
916
 
 
917
                        if (disconnected || albums.Count == 0) {
 
918
                                string msg = disconnected ? Catalog.GetString ("(Not Connected)")
 
919
                                        : Catalog.GetString ("(No Albums)");
 
920
 
 
921
                                Gtk.MenuItem item = new Gtk.MenuItem (msg);
 
922
                                menu.Append (item);
 
923
 
 
924
                                export_button.Sensitive = false;
 
925
                                album_optionmenu.Sensitive = false;
 
926
                                album_button.Sensitive = false;
 
927
 
 
928
                                if (disconnected)
 
929
                                        album_button.Sensitive = false;
 
930
                        } else {
 
931
                                foreach (Album album in albums) {
 
932
                                        System.Text.StringBuilder label_builder = new System.Text.StringBuilder ();
 
933
 
 
934
                                        for (int i=0; i < album.Parents.Count; i++) {
 
935
                                                label_builder.Append ("  ");
 
936
                                        }
 
937
                                        label_builder.Append (album.Title);
 
938
 
 
939
                                        Gtk.MenuItem item = new Gtk.MenuItem (label_builder.ToString ());
 
940
                                        ((Gtk.Label)item.Child).UseUnderline = false;
 
941
                                        menu.Append (item);
 
942
 
 
943
                                        AlbumPermission add_permission = album.Perms & AlbumPermission.Add;
 
944
 
 
945
                                        if (add_permission == 0)
 
946
                                                item.Sensitive = false;
 
947
                                }
 
948
 
 
949
                                export_button.Sensitive = items.Length > 0;
 
950
                                album_optionmenu.Sensitive = true;
 
951
                                album_button.Sensitive = true;
 
952
                        }
 
953
 
 
954
                        menu.ShowAll ();
 
955
                        album_optionmenu.Menu = menu;
 
956
                }
 
957
 
 
958
                public void HandleAddGallery (object sender, System.EventArgs args)
 
959
                {
 
960
                        new AccountDialog (export_dialog);
 
961
                }
 
962
 
 
963
                public void HandleEditGallery (object sender, System.EventArgs args)
 
964
                {
 
965
                        new AccountDialog (export_dialog, account, false);
 
966
                }
 
967
 
 
968
                public void HandleAddAlbum (object sender, System.EventArgs args)
 
969
                {
 
970
                        if (account == null)
 
971
                                throw new GalleryException (Catalog.GetString ("No account selected"));
 
972
 
 
973
                        new GalleryAddAlbum (this, account.Gallery);
 
974
                }
 
975
 
 
976
                void LoadPreference (string key)
 
977
                {
 
978
                        object val = Preferences.Get (key);
 
979
 
 
980
                        if (val == null)
 
981
                                return;
 
982
 
 
983
                        //System.Console.WriteLine ("Setting {0} to {1}", key, val);
 
984
 
 
985
                        switch (key) {
 
986
                        case SCALE_KEY:
 
987
                                if (scale_check.Active != (bool) val)
 
988
                                        scale_check.Active = (bool) val;
 
989
                                break;
 
990
 
 
991
                        case SIZE_KEY:
 
992
                                size_spin.Value = (double) (int) val;
 
993
                                break;
 
994
 
 
995
                        case BROWSER_KEY:
 
996
                                if (browser_check.Active != (bool) val)
 
997
                                        browser_check.Active = (bool) val;
 
998
                                break;
 
999
 
 
1000
                        case META_KEY:
 
1001
                                if (meta_check.Active != (bool) val)
 
1002
                                        meta_check.Active = (bool) val;
 
1003
                                break;
 
1004
                        case ROTATE_KEY:
 
1005
                                if (rotate_check.Active != (bool) val)
 
1006
                                        rotate_check.Active = (bool) val;
 
1007
                                break;
 
1008
                        }
 
1009
                }
 
1010
        }
 
1011
}