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

« back to all changes in this revision

Viewing changes to extensions/Services/DBusService/DBusProxy.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.Collections.Generic;
 
3
using System.IO;
 
4
using System.Text;
 
5
 
 
6
using FSpot;
 
7
using NDesk.DBus;
 
8
 
 
9
namespace DBusService {
 
10
        public delegate void RemoteUp();
 
11
        public delegate void RemoteDown();
 
12
 
 
13
        public class DBusException : Exception {
 
14
                public DBusException (string message)
 
15
                        : base (message)
 
16
                {}
 
17
 
 
18
                public DBusException (string message, params object [] format_params)
 
19
                        : this (string.Format (message, format_params))
 
20
                {}
 
21
        }
 
22
 
 
23
        public class DBusProxy {
 
24
                public event RemoteUp RemoteUp;
 
25
                public event RemoteDown RemoteDown;
 
26
 
 
27
                internal void OnRemoteUp ()
 
28
                {
 
29
                        if (RemoteUp != null)
 
30
                                RemoteUp ();
 
31
                }
 
32
 
 
33
                internal void OnRemoteDown ()
 
34
                {
 
35
                        if (RemoteDown != null)
 
36
                                RemoteDown ();
 
37
                }
 
38
        }
 
39
 
 
40
        public class DBusProxyFactory {
 
41
                private const string SERVICE_PATH = "org.gnome.FSpot";
 
42
                private const string TAG_PROXY_PATH = "/org/gnome/FSpot/TagRemoteControl";
 
43
                private const string PHOTO_PROXY_PATH = "/org/gnome/FSpot/PhotoRemoteControl";
 
44
 
 
45
                private static TagProxy tag_remote;
 
46
                private static PhotoProxy photo_remote;
 
47
 
 
48
                public static void Load (Db db)
 
49
                {
 
50
                        tag_remote = new TagProxy (db.Tags);
 
51
                        Bus.Session.Register (SERVICE_PATH, new ObjectPath (TAG_PROXY_PATH), tag_remote);
 
52
                        tag_remote.OnRemoteUp ();
 
53
 
 
54
                        photo_remote = new PhotoProxy (db);
 
55
                        Bus.Session.Register (SERVICE_PATH, new ObjectPath (PHOTO_PROXY_PATH), photo_remote);
 
56
                        photo_remote.OnRemoteUp ();
 
57
                }
 
58
 
 
59
                public static void EmitRemoteDown ()
 
60
                {
 
61
                        tag_remote.OnRemoteDown ();
 
62
                        photo_remote.OnRemoteDown ();
 
63
                }
 
64
 
 
65
        }
 
66
 
 
67
        [Interface ("org.gnome.FSpot.TagRemoteControl")]
 
68
        public interface ITagRemoteControl {
 
69
                // info, included for backward compatibility with times
 
70
                // where this was embedded in f-spot core
 
71
                bool IsReadOnly ();
 
72
 
 
73
                // get all
 
74
                string[] GetTagNames ();
 
75
                uint[] GetTagIds ();
 
76
 
 
77
                // get info for one tag
 
78
                IDictionary<string, object> GetTagByName (string name);
 
79
                IDictionary<string, object> GetTagById (int id);
 
80
 
 
81
                event RemoteUp RemoteUp;
 
82
                event RemoteDown RemoteDown;
 
83
 
 
84
                // tag creators
 
85
                int CreateTag (string name);
 
86
                int CreateTagWithParent (string parent, string name);
 
87
 
 
88
                // tag removers
 
89
                bool RemoveTagByName (string name);
 
90
                bool RemoveTagById (int id);
 
91
        }
 
92
 
 
93
        // Class exposing all photos on the dbus
 
94
        public class TagProxy : DBusProxy, ITagRemoteControl {
 
95
                protected TagStore tag_store;
 
96
 
 
97
                public TagStore Store {
 
98
                        get { return tag_store; }
 
99
                }
 
100
 
 
101
                internal TagProxy (TagStore store)
 
102
                {
 
103
                        tag_store = store;
 
104
                }
 
105
 
 
106
                #region Interface methods
 
107
                public bool IsReadOnly () {
 
108
                        return false;
 
109
                }
 
110
 
 
111
                public string[] GetTagNames ()
 
112
                {
 
113
                        List<string> tags = new List<string> ();
 
114
                        AddTagNameToList (tags, tag_store.RootCategory);
 
115
 
 
116
                        return tags.ToArray ();
 
117
                }
 
118
 
 
119
                public uint[] GetTagIds ()
 
120
                {
 
121
                        List<uint> tags = new List<uint> ();
 
122
                        AddTagIdToList (tags, tag_store.RootCategory);
 
123
 
 
124
                        return tags.ToArray ();
 
125
                }
 
126
 
 
127
                public IDictionary<string, object> GetTagByName (string name)
 
128
                {
 
129
                        Tag t = tag_store.GetTagByName (name);
 
130
 
 
131
                        if (t == null)
 
132
                                throw new DBusException ("Tag with name {0} does not exist.", name);
 
133
 
 
134
                        return CreateDictFromTag (t);
 
135
                }
 
136
 
 
137
                public IDictionary<string, object> GetTagById (int id)
 
138
                {
 
139
                        Tag t = tag_store.GetTagById (id);
 
140
 
 
141
                        if (t == null)
 
142
                                throw new DBusException ("Tag with id {0} does not exist.", id);
 
143
 
 
144
                        return CreateDictFromTag (t);
 
145
                }
 
146
 
 
147
                public int CreateTag (string name)
 
148
                {
 
149
                        return CreateTagPriv (null, name);
 
150
                }
 
151
 
 
152
                public int CreateTagWithParent (string parent, string name)
 
153
                {
 
154
                        Tag parent_tag = tag_store.GetTagByName (parent);
 
155
 
 
156
                        if (!(parent_tag is Category))
 
157
                                parent_tag = null;
 
158
 
 
159
                        return CreateTagPriv (parent_tag as Category, name);
 
160
                }
 
161
 
 
162
                public bool RemoveTagByName (string name)
 
163
                {
 
164
                        Tag tag = tag_store.GetTagByName (name);
 
165
 
 
166
                        return RemoveTag (tag);
 
167
                }
 
168
 
 
169
                public bool RemoveTagById (int id)
 
170
                {
 
171
                        Tag tag = tag_store.GetTagById (id);
 
172
 
 
173
                        return RemoveTag (tag);
 
174
                }
 
175
 
 
176
                #endregion
 
177
 
 
178
                #region Helper methods
 
179
                private void AddTagNameToList (List<string> list, Tag tag)
 
180
                {
 
181
                        if (tag != tag_store.RootCategory)
 
182
                                list.Add (tag.Name);
 
183
 
 
184
                        if (tag is Category) {
 
185
                                foreach (Tag child in (tag as Category).Children)
 
186
                                        AddTagNameToList (list, child);
 
187
                        }
 
188
                }
 
189
 
 
190
                private void AddTagIdToList (List<uint> list, Tag tag)
 
191
                {
 
192
                        if (tag != tag_store.RootCategory)
 
193
                                list.Add (tag.Id);
 
194
 
 
195
                        if (tag is Category) {
 
196
                                foreach (Tag child in (tag as Category).Children)
 
197
                                        AddTagIdToList (list, child);
 
198
                        }
 
199
                }
 
200
 
 
201
                private IDictionary<string, object> CreateDictFromTag (Tag t)
 
202
                {
 
203
                        Dictionary<string, object> result = new Dictionary<string, object> ();
 
204
 
 
205
                        result.Add ("Id", t.Id);
 
206
                        result.Add ("Name", t.Name);
 
207
 
 
208
                        StringBuilder builder = new StringBuilder ();
 
209
 
 
210
                        if (t is Category) {
 
211
                                foreach (Tag child in (t as Category).Children) {
 
212
                                        if (builder.Length > 0)
 
213
                                                builder.Append (",");
 
214
 
 
215
                                        builder.Append (child.Name);
 
216
                                }
 
217
                        }
 
218
 
 
219
                        result.Add ("Children", builder.ToString ());
 
220
 
 
221
                        return result;
 
222
                }
 
223
 
 
224
                private int CreateTagPriv (Category parent_tag, string name)
 
225
                {
 
226
                        try {
 
227
                                Tag created = tag_store.CreateCategory (parent_tag, name);
 
228
                                return (int)created.Id;
 
229
                        }
 
230
                        catch {
 
231
                                throw new DBusException ("Failed to create tag.");
 
232
                        }
 
233
                }
 
234
 
 
235
                private bool RemoveTag (Tag t)
 
236
                {
 
237
                        if (t == null)
 
238
                                return false;
 
239
 
 
240
                        try {
 
241
                                // remove tags from photos first
 
242
                                Core.Database.Photos.Remove (new Tag [] { t });
 
243
                                // then remove tag
 
244
                                tag_store.Remove (t);
 
245
                                return true;
 
246
                        }
 
247
                        catch {
 
248
                                return false;
 
249
                        }
 
250
                }
 
251
                #endregion
 
252
        }
 
253
 
 
254
 
 
255
        [Interface ("org.gnome.FSpot.PhotoRemoteControl")]
 
256
        public interface IPhotoRemoteControl {
 
257
                // info; included for backward compatibility
 
258
                // with previous version where this was embedded in f-spot
 
259
                bool IsReadOnly ();
 
260
 
 
261
                // get all
 
262
                uint[] GetPhotoIds ();
 
263
 
 
264
                // import prepare
 
265
                void PrepareRoll ();
 
266
                void FinishRoll ();
 
267
 
 
268
                // import
 
269
                int ImportPhoto (string path, bool copy, string []tags);
 
270
 
 
271
                // photo properties
 
272
                IDictionary<string, object> GetPhotoProperties (uint id);
 
273
 
 
274
                // photo remove
 
275
                void RemovePhoto (uint id);
 
276
 
 
277
                // query
 
278
                uint[] Query (string []tags);
 
279
 
 
280
                // events
 
281
                event RemoteUp RemoteUp;
 
282
                event RemoteDown RemoteDown;
 
283
        }
 
284
 
 
285
        // Class exposing all photos on the dbus
 
286
        public class PhotoProxy : DBusProxy, IPhotoRemoteControl {
 
287
                protected Db db;
 
288
                private Roll current_roll;
 
289
 
 
290
                public Db Database {
 
291
                        get { return db; }
 
292
                }
 
293
 
 
294
                internal PhotoProxy (Db db)
 
295
                {
 
296
                        this.db = db;
 
297
                }
 
298
 
 
299
                public bool IsReadOnly ()
 
300
                {
 
301
                        return false;
 
302
                }
 
303
 
 
304
                public uint[] GetPhotoIds ()
 
305
                {
 
306
                        List<uint> ids = new List<uint> ();
 
307
 
 
308
                        foreach (Photo p in QueryAll ())
 
309
                                ids.Add (p.Id);
 
310
 
 
311
                        return ids.ToArray ();
 
312
                }
 
313
 
 
314
                public IDictionary<string, object> GetPhotoProperties (uint id)
 
315
                {
 
316
                        Dictionary<string, object> dict = new Dictionary<string, object> ();
 
317
 
 
318
                        Photo p = db.Photos.Get (id) as Photo;
 
319
 
 
320
                        if (p == null)
 
321
                                throw new DBusException ("Photo with id {0} does not exist.", id);
 
322
 
 
323
                        dict.Add ("Uri", p.DefaultVersionUri.ToString());
 
324
                        dict.Add ("Id", p.Id);
 
325
                        dict.Add ("Name", p.Name);
 
326
                        dict.Add ("Description", p.Description ?? string.Empty);
 
327
 
 
328
                        StringBuilder builder = new StringBuilder ();
 
329
 
 
330
                        foreach (Tag t in p.Tags) {
 
331
                                if (builder.Length > 0)
 
332
                                        builder.Append (",");
 
333
 
 
334
                                builder.AppendFormat (t.Name);
 
335
                        }
 
336
 
 
337
                        dict.Add ("Tags", builder.ToString ());
 
338
 
 
339
                        return dict;
 
340
                }
 
341
 
 
342
                public uint[] Query (string []tags)
 
343
                {
 
344
                        List<Tag> tag_list = GetTagsByNames (tags);
 
345
 
 
346
                        Photo []photos = db.Photos.Query (tag_list.ToArray ());
 
347
 
 
348
                        uint []ids = new uint[photos.Length];
 
349
 
 
350
                        for (int i = 0; i < ids.Length; i++)
 
351
                                ids[i] = photos[i].Id;
 
352
 
 
353
                        return ids;
 
354
                }
 
355
 
 
356
                protected Photo[] QueryAll ()
 
357
                {
 
358
                        return db.Photos.Query ((Tag [])null);
 
359
                }
 
360
 
 
361
                protected List<Tag> GetTagsByNames (string []names)
 
362
                {
 
363
                        // add tags that exist in tag store
 
364
                        List<Tag> tag_list = new List<Tag> ();
 
365
 
 
366
                        foreach (string tag_name in names) {
 
367
                                Tag t = db.Tags.GetTagByName (tag_name);
 
368
 
 
369
                                if (t == null)
 
370
                                        continue;
 
371
 
 
372
                                tag_list.Add (t);
 
373
                        }
 
374
 
 
375
                        return tag_list;
 
376
                }
 
377
 
 
378
                public void PrepareRoll ()
 
379
                {
 
380
                        if (current_roll != null)
 
381
                                return;
 
382
 
 
383
                        current_roll = db.Rolls.Create ();
 
384
                }
 
385
 
 
386
                public void FinishRoll ()
 
387
                {
 
388
                        current_roll = null;
 
389
                }
 
390
 
 
391
                public int ImportPhoto (string path, bool copy, string []tags)
 
392
                {
 
393
                        if (current_roll == null)
 
394
                                throw new DBusException ("You must use PrepareRoll before you can import a photo.");
 
395
 
 
396
                        // add tags that exist in tag store
 
397
                        List<Tag> tag_list = GetTagsByNames (tags);
 
398
 
 
399
                        Gdk.Pixbuf pixbuf = null;
 
400
 
 
401
                        // FIXME: this is more or less a copy of the file import backend code
 
402
                        // this should be streamlined
 
403
                        try {
 
404
                                string new_path = path;
 
405
 
 
406
                                if (copy)
 
407
                                        new_path = FileImportBackend.ChooseLocation (path);
 
408
 
 
409
                                if (new_path != path)
 
410
                                        System.IO.File.Copy (path, new_path);
 
411
 
 
412
                                Photo created = db.Photos.CreateOverDBus (new_path, path, current_roll.Id, out pixbuf);
 
413
 
 
414
                                try {
 
415
                                        File.SetAttributes (new_path, File.GetAttributes (new_path) & ~FileAttributes.ReadOnly);
 
416
                                        DateTime create = File.GetCreationTime (path);
 
417
                                        File.SetCreationTime (new_path, create);
 
418
                                        DateTime mod = File.GetLastWriteTime (path);
 
419
                                        File.SetLastWriteTime (new_path, mod);
 
420
                                } catch (IOException) {
 
421
                                        // we don't want an exception here to be fatal.
 
422
                                }
 
423
 
 
424
                                // attach tags we got
 
425
                                if (tag_list.Count > 0) {
 
426
                                        created.AddTag (tag_list.ToArray ());
 
427
                                        db.Photos.Commit (created);
 
428
                                }
 
429
 
 
430
                                return (int)created.Id;
 
431
                        // indicate failure
 
432
                        } catch {
 
433
                                throw new DBusException ("Failed to import the photo.");
 
434
                        }
 
435
                }
 
436
 
 
437
                public void RemovePhoto (uint id)
 
438
                {
 
439
                        Photo p = db.Photos.Get (id) as Photo;
 
440
 
 
441
                        if (p == null)
 
442
                                throw new DBusException ("Photo with id {0} does not exist.", id);
 
443
 
 
444
                        db.Photos.RemoveOverDBus (p);
 
445
                }
 
446
        }
 
447
}