2
using System.Collections.Generic;
9
namespace DBusService {
10
public delegate void RemoteUp();
11
public delegate void RemoteDown();
13
public class DBusException : Exception {
14
public DBusException (string message)
18
public DBusException (string message, params object [] format_params)
19
: this (string.Format (message, format_params))
23
public class DBusProxy {
24
public event RemoteUp RemoteUp;
25
public event RemoteDown RemoteDown;
27
internal void OnRemoteUp ()
33
internal void OnRemoteDown ()
35
if (RemoteDown != null)
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";
45
private static TagProxy tag_remote;
46
private static PhotoProxy photo_remote;
48
public static void Load (Db db)
50
tag_remote = new TagProxy (db.Tags);
51
Bus.Session.Register (SERVICE_PATH, new ObjectPath (TAG_PROXY_PATH), tag_remote);
52
tag_remote.OnRemoteUp ();
54
photo_remote = new PhotoProxy (db);
55
Bus.Session.Register (SERVICE_PATH, new ObjectPath (PHOTO_PROXY_PATH), photo_remote);
56
photo_remote.OnRemoteUp ();
59
public static void EmitRemoteDown ()
61
tag_remote.OnRemoteDown ();
62
photo_remote.OnRemoteDown ();
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
74
string[] GetTagNames ();
77
// get info for one tag
78
IDictionary<string, object> GetTagByName (string name);
79
IDictionary<string, object> GetTagById (int id);
81
event RemoteUp RemoteUp;
82
event RemoteDown RemoteDown;
85
int CreateTag (string name);
86
int CreateTagWithParent (string parent, string name);
89
bool RemoveTagByName (string name);
90
bool RemoveTagById (int id);
93
// Class exposing all photos on the dbus
94
public class TagProxy : DBusProxy, ITagRemoteControl {
95
protected TagStore tag_store;
97
public TagStore Store {
98
get { return tag_store; }
101
internal TagProxy (TagStore store)
106
#region Interface methods
107
public bool IsReadOnly () {
111
public string[] GetTagNames ()
113
List<string> tags = new List<string> ();
114
AddTagNameToList (tags, tag_store.RootCategory);
116
return tags.ToArray ();
119
public uint[] GetTagIds ()
121
List<uint> tags = new List<uint> ();
122
AddTagIdToList (tags, tag_store.RootCategory);
124
return tags.ToArray ();
127
public IDictionary<string, object> GetTagByName (string name)
129
Tag t = tag_store.GetTagByName (name);
132
throw new DBusException ("Tag with name {0} does not exist.", name);
134
return CreateDictFromTag (t);
137
public IDictionary<string, object> GetTagById (int id)
139
Tag t = tag_store.GetTagById (id);
142
throw new DBusException ("Tag with id {0} does not exist.", id);
144
return CreateDictFromTag (t);
147
public int CreateTag (string name)
149
return CreateTagPriv (null, name);
152
public int CreateTagWithParent (string parent, string name)
154
Tag parent_tag = tag_store.GetTagByName (parent);
156
if (!(parent_tag is Category))
159
return CreateTagPriv (parent_tag as Category, name);
162
public bool RemoveTagByName (string name)
164
Tag tag = tag_store.GetTagByName (name);
166
return RemoveTag (tag);
169
public bool RemoveTagById (int id)
171
Tag tag = tag_store.GetTagById (id);
173
return RemoveTag (tag);
178
#region Helper methods
179
private void AddTagNameToList (List<string> list, Tag tag)
181
if (tag != tag_store.RootCategory)
184
if (tag is Category) {
185
foreach (Tag child in (tag as Category).Children)
186
AddTagNameToList (list, child);
190
private void AddTagIdToList (List<uint> list, Tag tag)
192
if (tag != tag_store.RootCategory)
195
if (tag is Category) {
196
foreach (Tag child in (tag as Category).Children)
197
AddTagIdToList (list, child);
201
private IDictionary<string, object> CreateDictFromTag (Tag t)
203
Dictionary<string, object> result = new Dictionary<string, object> ();
205
result.Add ("Id", t.Id);
206
result.Add ("Name", t.Name);
208
StringBuilder builder = new StringBuilder ();
211
foreach (Tag child in (t as Category).Children) {
212
if (builder.Length > 0)
213
builder.Append (",");
215
builder.Append (child.Name);
219
result.Add ("Children", builder.ToString ());
224
private int CreateTagPriv (Category parent_tag, string name)
227
Tag created = tag_store.CreateCategory (parent_tag, name);
228
return (int)created.Id;
231
throw new DBusException ("Failed to create tag.");
235
private bool RemoveTag (Tag t)
241
// remove tags from photos first
242
Core.Database.Photos.Remove (new Tag [] { t });
244
tag_store.Remove (t);
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
262
uint[] GetPhotoIds ();
269
int ImportPhoto (string path, bool copy, string []tags);
272
IDictionary<string, object> GetPhotoProperties (uint id);
275
void RemovePhoto (uint id);
278
uint[] Query (string []tags);
281
event RemoteUp RemoteUp;
282
event RemoteDown RemoteDown;
285
// Class exposing all photos on the dbus
286
public class PhotoProxy : DBusProxy, IPhotoRemoteControl {
288
private Roll current_roll;
294
internal PhotoProxy (Db db)
299
public bool IsReadOnly ()
304
public uint[] GetPhotoIds ()
306
List<uint> ids = new List<uint> ();
308
foreach (Photo p in QueryAll ())
311
return ids.ToArray ();
314
public IDictionary<string, object> GetPhotoProperties (uint id)
316
Dictionary<string, object> dict = new Dictionary<string, object> ();
318
Photo p = db.Photos.Get (id) as Photo;
321
throw new DBusException ("Photo with id {0} does not exist.", id);
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);
328
StringBuilder builder = new StringBuilder ();
330
foreach (Tag t in p.Tags) {
331
if (builder.Length > 0)
332
builder.Append (",");
334
builder.AppendFormat (t.Name);
337
dict.Add ("Tags", builder.ToString ());
342
public uint[] Query (string []tags)
344
List<Tag> tag_list = GetTagsByNames (tags);
346
Photo []photos = db.Photos.Query (tag_list.ToArray ());
348
uint []ids = new uint[photos.Length];
350
for (int i = 0; i < ids.Length; i++)
351
ids[i] = photos[i].Id;
356
protected Photo[] QueryAll ()
358
return db.Photos.Query ((Tag [])null);
361
protected List<Tag> GetTagsByNames (string []names)
363
// add tags that exist in tag store
364
List<Tag> tag_list = new List<Tag> ();
366
foreach (string tag_name in names) {
367
Tag t = db.Tags.GetTagByName (tag_name);
378
public void PrepareRoll ()
380
if (current_roll != null)
383
current_roll = db.Rolls.Create ();
386
public void FinishRoll ()
391
public int ImportPhoto (string path, bool copy, string []tags)
393
if (current_roll == null)
394
throw new DBusException ("You must use PrepareRoll before you can import a photo.");
396
// add tags that exist in tag store
397
List<Tag> tag_list = GetTagsByNames (tags);
399
Gdk.Pixbuf pixbuf = null;
401
// FIXME: this is more or less a copy of the file import backend code
402
// this should be streamlined
404
string new_path = path;
407
new_path = FileImportBackend.ChooseLocation (path);
409
if (new_path != path)
410
System.IO.File.Copy (path, new_path);
412
Photo created = db.Photos.CreateOverDBus (new_path, path, current_roll.Id, out pixbuf);
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.
424
// attach tags we got
425
if (tag_list.Count > 0) {
426
created.AddTag (tag_list.ToArray ());
427
db.Photos.Commit (created);
430
return (int)created.Id;
433
throw new DBusException ("Failed to import the photo.");
437
public void RemovePhoto (uint id)
439
Photo p = db.Photos.Get (id) as Photo;
442
throw new DBusException ("Photo with id {0} does not exist.", id);
444
db.Photos.RemoveOverDBus (p);