4 // Copyright (C) 2004 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
28 using System
.Collections
;
30 using Mono
.Data
.SqliteClient
;
32 namespace Beagle
.Util
{
34 public class FSpotTools
{
39 public uint CategoryId
;
40 public bool IsCategory
;
41 public int SortPriority
;
49 public string Description
;
54 static private SqliteConnection connection
;
55 static Hashtable tagCache
= null;
56 static Hashtable directoryCache
= null;
59 static private string PhotoStorePath
{
61 string home
= Environment
.GetEnvironmentVariable ("HOME");
62 return Path
.Combine (home
, ".gnome2/f-spot/photos.db");
66 static private SqliteConnection PhotoStoreConnection
{
68 if (connection
== null && File
.Exists (PhotoStorePath
)) {
69 connection
= new SqliteConnection ();
70 connection
.ConnectionString
= "URI=file:" + PhotoStorePath
;
78 static private bool HavePhotoStore
{
80 return PhotoStoreConnection
!= null;
84 // FIXME: We should expire this cache if the underlying db has changed.
85 static private Tag
GetTagById (uint id
)
90 if (tagCache
== null) {
91 tagCache
= new Hashtable ();
93 SqliteCommand command
= new SqliteCommand ();
94 command
.Connection
= PhotoStoreConnection
;
95 command
.CommandText
= "SELECT id, name, category_id, is_category, sort_priority FROM tags";
97 SqliteDataReader reader
= command
.ExecuteReader ();
98 while (reader
.Read ()) {
100 tag
.Id
= Convert
.ToUInt32 (reader
[0]);
101 tag
.Name
= (string) reader
[1];
102 tag
.CategoryId
= Convert
.ToUInt32 (reader
[2]);
103 tag
.IsCategory
= (reader
[3] == "1");
104 tag
.SortPriority
= Convert
.ToInt32 (reader
[4]);
105 tagCache
[tag
.Id
] = tag
;
108 // Walk across all tags, linking to the category's Tag
109 // object. Since the tagCache is fully populated, it is
110 // safe to call GetTagById here.
111 foreach (Tag tag
in tagCache
.Values
)
112 tag
.Category
= GetTagById (tag
.CategoryId
);
117 return (Tag
) tagCache
[id
];
120 // FIXME: We should expire this cache if the underlying db has changed.
121 static bool IsPossibleDirectory (string directory
)
123 if (! HavePhotoStore
)
126 if (directoryCache
== null) {
127 directoryCache
= new Hashtable ();
129 SqliteCommand command
= new SqliteCommand ();
130 command
.Connection
= PhotoStoreConnection
;
131 command
.CommandText
= "SELECT DISTINCT directory_path FROM photos";
133 SqliteDataReader reader
= command
.ExecuteReader ();
134 while (reader
.Read ()) {
135 directoryCache
[reader
[0]] = true;
141 return directoryCache
.Contains (directory
);
144 static public Photo
GetPhoto (string path
)
146 if (! HavePhotoStore
)
149 path
= Path
.GetFullPath (path
);
150 string dir
= Path
.GetDirectoryName (path
);
151 string name
= Path
.GetFileName (path
);
153 if (! IsPossibleDirectory (dir
))
156 SqliteCommand command
;
157 SqliteDataReader reader
;
159 command
= new SqliteCommand ();
160 command
.Connection
= PhotoStoreConnection
;
161 command
.CommandText
= String
.Format ("SELECT id, description " +
163 "WHERE directory_path = \"{0}\" " +
164 " AND name = \"{1}\"",
168 reader
= command
.ExecuteReader ();
169 if (reader
.Read ()) {
170 photo
= new Photo ();
172 photo
.Id
= Convert
.ToUInt32 (reader
[0]);
173 photo
.Description
= (string) reader
[1];
179 command
= new SqliteCommand ();
180 command
.Connection
= PhotoStoreConnection
;
181 command
.CommandText
= String
.Format ("SELECT tag_id " +
183 "WHERE photo_id = {0}",
186 Hashtable tagHash
= new Hashtable ();
188 // Mark the photo with both any tags and all parents.
189 // Maybe this isn't the right thing to do, but it seems
190 // to most closely mirror the tag semantics implied by f-spot.
191 reader
= command
.ExecuteReader ();
192 while (reader
.Read ()) {
193 uint id
= Convert
.ToUInt32 (reader
[0]);
194 Tag tag
= GetTagById (id
);
195 while (tag
!= null) {
201 photo
.Tags
= new Tag
[tagHash
.Count
];
203 foreach (Tag t
in tagHash
.Values
) {