/
PersistentInformation.cs
2090 lines (1928 loc) · 64.5 KB
/
PersistentInformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using GConf;
using System;
using System.Data;
using System.IO;
using System.Collections;
using Mono.Data.SqliteClient;
public class PersistentInformation
{
private static PersistentInformation info = null;
private GConf.Client client;
private readonly object _photolock;
private readonly object _commentlock;
private readonly object _albumlock;
private readonly object _setphotolock;
private readonly object _taglock;
private readonly object _poollock;
private readonly object _poolphotolock;
private readonly object _bloglock;
private readonly object _blogphotolock;
private readonly object _downloadlock;
private readonly object _uploadlock;
private readonly object _writelock;
private static string GCONF_APP_PATH = "/apps/DesktopFlickrOrganizer";
private static string SECRET_TOKEN = GCONF_APP_PATH + "/token";
private static string ORDERED_SETS_LIST = GCONF_APP_PATH + "/sets";
private static string DOWNLOAD_FOLDER = GCONF_APP_PATH + "/downloadfolder";
private static string UPLOAD_FILE = GCONF_APP_PATH + "/uploadfile";
private static string USER_NSID = GCONF_APP_PATH + "/userid";
private static string USER_NAME = GCONF_APP_PATH + "/username";
private static string HEIGHT = GCONF_APP_PATH + "/height";
private static string WIDTH = GCONF_APP_PATH + "/width";
private static string VPOSITION = GCONF_APP_PATH + "/vpos";
private static string HPOSITION = GCONF_APP_PATH + "/hpos";
private static string HOME = System.IO.Path.Combine(
System.Environment.GetEnvironmentVariable("HOME"), ".desktopflickr");
private static string THUMBNAIL_DIR = System.IO.Path.Combine(HOME, "thumbnails");
private static string SMALL_IMAGE_DIR = System.IO.Path.Combine(HOME, "small_images");
private static int NUM_SHARDS = 37;
private static string DB_PATH = "URI=file:" + HOME + "/sqlite.db,version=3,busy_timeout=30000";
private static string CREATE_PHOTO_TABLE =
"create table photo (\n"
+ " id varchar(25) primary key,\n"
+ " title varchar(256),\n"
+ " desc text,\n"
+ " license integer,\n"
+ " ispublic integer,\n"
+ " isfriend integer,\n"
+ " isfamily integer,\n"
+ " lastupdate varchar(25) default '',\n"
// Entries after this, can be ordered in any way. They're not
// being read by select all.
+ " dateposted varchar(25) default '',\n"
+ " isdeleted integer default 0,\n"
+ " isdirty integer default 0\n"
+ ");";
private static string CREATE_ORIGINAL_PHOTO_TABLE =
"create table originalphoto (\n"
+ " id varchar(25) primary key,\n"
+ " title varchar(256),\n"
+ " desc text,\n"
+ " license integer,\n"
+ " ispublic integer,\n"
+ " isfriend integer,\n"
+ " isfamily integer,\n"
+ " lastupdate varchar(25) default '',\n"
+ " tags text\n"
+ ");";
private static string CREATE_COMMENT_TABLE =
"create table comment (\n"
+ " photoid varchar(25),\n"
+ " commentid varchar(25),\n"
+ " commenthtml text,\n"
+ " username varchar(25) default '',\n"
+ " isdirty integer default 0,\n"
+ " isdeleted integer default 0\n"
+ ");";
private static string CREATE_ALBUM_TABLE =
"create table album (\n"
+ " setid varchar(25) primary key,\n"
+ " title varchar(256),\n"
+ " desc text,\n"
+ " photoid varchar(10),\n"
+ " isdirty integer default 0,\n"
+ " isnew integer default 0\n"
+ ");";
private static string CREATE_ALBUM_PHOTO_MAPPING_TABLE =
"create table setphoto (\n"
+ " setid varchar(25),\n"
+ " photoid varchar(10)\n"
+ ");";
private static string CREATE_PHOTO_TAG_TABLE =
"create table tag (\n"
+ " photoid varchar(25),\n"
+ " tag varchar(56)\n"
+ ");";
private static string CREATE_POOL_TABLE =
"create table pool (\n"
+ " groupid varchar(25) primary key,\n"
+ " title varchar(256)\n"
+ ");";
private static string CREATE_POOL_PHOTO_TABLE =
"create table poolphoto (\n"
+ "groupid varchar(25),\n"
+ "photoid varchar(25),\n"
+ "isadded integer default 0,\n"
+ "isdeleted integer default 0\n"
+ ");";
private static string CREATE_BLOG_TABLE =
"create table blog (\n"
+ " blogid varchar(25),\n"
+ " blogtitle varchar(256)\n"
+ ");";
private static string CREATE_BLOG_PHOTO_TABLE =
"create table blogphoto (\n"
+ " blogid varchar(25),\n" //composite of (blogid, photoid) is primary key.
+ " photoid varchar(25),\n"
+ " title varchar(256),\n"
+ " desc text\n"
+ ");";
private static string CREATE_DOWNLOAD_TABLE =
"create table download (\n"
+ " photoid varchar(25) primary key,\n"
+ " foldername varchar(256)\n"
+ ");";
private static string CREATE_UPLOAD_TABLE =
"create table upload (\n"
+ " filename varchar(256) primary key,\n"
+ " title varchar(256),\n"
+ " desc text,\n"
+ " license integer default -1,\n"
+ " ispublic integer default 0,\n"
+ " isfriend integer default 0,\n"
+ " isfamily integer default 0,\n"
+ " tags text\n"
+ ");";
public class Entry {
public string entry1;
public string entry2;
public Entry(string entry1, string entry2) {
this.entry1 = entry1;
this.entry2 = entry2;
}
}
private System.Collections.Generic.Dictionary<string, Gdk.Pixbuf> _thumbnailbuffer;
private System.Collections.Generic.Dictionary<string, Gdk.Pixbuf> _smallimagebuffer;
private PersistentInformation()
{
Utils.EnsureDirectoryExists(HOME);
Utils.EnsureDirectoryExists(THUMBNAIL_DIR);
Utils.EnsureDirectoryExists(SMALL_IMAGE_DIR);
// Shard the photos, so they're stored in 37 directories, instead of one.
for (int i = 0; i < NUM_SHARDS; ++i) {
string s = String.Format("{0:00}", i);
Utils.EnsureDirectoryExists(System.IO.Path.Combine(THUMBNAIL_DIR, s));
Utils.EnsureDirectoryExists(System.IO.Path.Combine(SMALL_IMAGE_DIR, s));
}
// Attempt creation of tables. If the table exists, the command
// execution will throw an exception. Dirty way, but had to be done.
// The cleaner alternative through sql command using "if not exists"
// is only available in the latest versions of sqlite; which is
// currently not ported over to mono.
if (CreateTable(CREATE_PHOTO_TABLE)) {
AddIndex("create index iphoto1 on photo(id);");
AddIndex("create index iphoto2 on photo(isdirty);");
AddIndex("create index iphoto3 on photo(isdeleted);");
}
if (CreateTable(CREATE_ORIGINAL_PHOTO_TABLE)) {
AddIndex("create index ioriginalphoto1 on originalphoto(id);");
}
if (CreateTable(CREATE_COMMENT_TABLE)) {
AddIndex("create index icomment1 on comment(photoid);");
AddIndex("create index icomment2 on comment(commentid);");
}
if (CreateTable(CREATE_ALBUM_TABLE)) {
AddIndex("create index ialbum1 on album(setid);");
AddIndex("create index ialbum2 on album(photoid);");
AddIndex("create index ialbum3 on album(isdirty);");
}
if (CreateTable(CREATE_ALBUM_PHOTO_MAPPING_TABLE)) {
AddIndex("create index isetphoto1 on setphoto(setid);");
AddIndex("create index isetphoto2 on setphoto(photoid);");
}
if (CreateTable(CREATE_PHOTO_TAG_TABLE)) {
AddIndex("create index itag1 on tag(photoid);");
AddIndex("create index itag2 on tag(tag);");
}
if (CreateTable(CREATE_POOL_TABLE)) {
AddIndex("create index ipool1 on pool(groupid);");
}
if (CreateTable(CREATE_POOL_PHOTO_TABLE)) {
AddIndex("create index ipoolphoto1 on poolphoto(groupid);");
AddIndex("create index ipoolphoto2 on poolphoto(photoid);");
}
if (CreateTable(CREATE_BLOG_TABLE)) {
AddIndex("create index iblog1 on blog(blogid);");
}
if (CreateTable(CREATE_BLOG_PHOTO_TABLE)) {
AddIndex("create index iblogphoto1 on blogphoto(blogid);");
AddIndex("create index iblogphoto2 on blogphoto(photoid);");
}
CreateTable(CREATE_DOWNLOAD_TABLE);
CreateTable(CREATE_UPLOAD_TABLE);
client = new GConf.Client();
_photolock = new object();
_commentlock = new object();
_albumlock = new object();
_setphotolock = new object();
_taglock = new object();
_poollock = new object();
_poolphotolock = new object();
_bloglock = new object();
_blogphotolock = new object();
_downloadlock = new object();
_uploadlock = new object();
_writelock = new object();
_thumbnailbuffer = new System.Collections.Generic.Dictionary<string, Gdk.Pixbuf>();
_smallimagebuffer = new System.Collections.Generic.Dictionary<string, Gdk.Pixbuf>();
}
public static PersistentInformation GetInstance() {
if (info == null) {
info = new PersistentInformation();
}
return info;
}
public bool CreateTable(string query) {
try {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
dbcmd.ExecuteNonQuery();
dbcmd.Dispose();
dbcon.Close();
return true;
} catch (SqliteSyntaxException ex) {
if (!ex.Message.Contains("already exists")) throw ex;
else return false;
}
}
public void AddIndex(string query) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
dbcmd.ExecuteNonQuery();
dbcmd.Dispose();
dbcon.Close();
}
/*
* Helper functions.
*/
private bool RunIsTrueQuery(string query) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
IDataReader reader = dbcmd.ExecuteReader();
int istrue = 0;
if (reader.Read()) {
istrue = reader.GetInt32(0);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return (istrue == 1);
}
private bool RunExistsQuery(string query) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
IDataReader reader = dbcmd.ExecuteReader();
bool exists = reader.Read();
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return exists;
}
private void RunNonQuery(string query) {
lock (_writelock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
dbcmd.ExecuteNonQuery();
dbcmd.Dispose();
dbcon.Close();
}
}
/*
* Photo retrieval and setting methods.
*/
private string GetThumbnailPath(string photoid) {
string picname = photoid + ".png";
string subdir = System.IO.Path.Combine(THUMBNAIL_DIR,
String.Format("{0:00}",
Convert.ToInt32(photoid.Substring(photoid.Length - 2)) % NUM_SHARDS));
return System.IO.Path.Combine(subdir, picname);
}
// Retrive the thumbnail of the photo.
public Gdk.Pixbuf GetThumbnail(string photoid) {
Gdk.Pixbuf buf;
try {
string safephotoid = photoid.Replace("/", "_");
if (_thumbnailbuffer.ContainsKey(safephotoid)) {
return _thumbnailbuffer[safephotoid];
}
// Load the photo from the thumbnails directory.
if (System.IO.File.Exists(GetThumbnailPath(safephotoid))) {
buf = new Gdk.Pixbuf(GetThumbnailPath(safephotoid));
} else {
// Photo isn't present in thumbnails directory, so load it from
// file directly. This photo must be an new upload photo.
string filename = photoid;
Gdk.Pixbuf original = new Gdk.Pixbuf(filename);
buf = original.ScaleSimple(75, 75, Gdk.InterpType.Bilinear);
original.Dispose();
SetThumbnail(photoid, buf);
}
if (buf != null && !_thumbnailbuffer.ContainsKey(safephotoid)) {
_thumbnailbuffer.Add(safephotoid, buf);
}
return buf;
} catch(Exception) {
return null;
}
}
public void SetThumbnail(string photoid, Gdk.Pixbuf buf) {
if (buf == null) return;
photoid = photoid.Replace("/", "_");
buf.Save(GetThumbnailPath(photoid), "png");
}
public void DeleteThumbnail(string photoid) {
photoid = photoid.Replace("/", "_");
FileInfo info = new FileInfo(GetThumbnailPath(photoid));
if (info.Exists) info.Delete();
if (_thumbnailbuffer.ContainsKey(photoid))
_thumbnailbuffer.Remove(photoid);
}
private string GetSmallImagePath(string photoid) {
string picname = photoid + ".png";
string subdir = System.IO.Path.Combine(SMALL_IMAGE_DIR,
String.Format("{0:00}",
Convert.ToInt32(photoid.Substring(photoid.Length - 2)) % NUM_SHARDS));
return System.IO.Path.Combine(subdir, picname);
}
// Retrive the small size image to be shown when editing of photos.
public Gdk.Pixbuf GetSmallImage(string photoid) {
Gdk.Pixbuf buf;
try {
string safephotoid = photoid.Replace("/", "_");
if (_smallimagebuffer.ContainsKey(safephotoid)) {
return _smallimagebuffer[safephotoid];
}
if (System.IO.File.Exists(GetSmallImagePath(safephotoid))) {
buf = new Gdk.Pixbuf(GetSmallImagePath(safephotoid));
} else {
string filename = photoid; // an upload photo.
Gdk.Pixbuf original = new Gdk.Pixbuf(filename);
buf = original.ScaleSimple(240, 180, Gdk.InterpType.Bilinear);
original.Dispose();
SetSmallImage(photoid, buf);
}
if (buf != null && !_smallimagebuffer.ContainsKey(safephotoid)) {
_smallimagebuffer.Add(safephotoid, buf);
}
return buf;
} catch(Exception) {
return null;
}
}
public void SetSmallImage(string photoid, Gdk.Pixbuf buf) {
if (buf == null) return;
photoid = photoid.Replace("/", "_");
buf.Save(GetSmallImagePath(photoid), "png");
}
public void DeleteSmallImage(string photoid) {
photoid = photoid.Replace("/", "_");
FileInfo finfo = new FileInfo(GetSmallImagePath(photoid));
if (finfo.Exists) finfo.Delete();
if (_smallimagebuffer.ContainsKey(photoid))
_smallimagebuffer.Remove(photoid);
}
public Photo GetPhoto(string photoid) {
Photo photo = null;
lock (_photolock) {
string sqlQuery = String.Format(
"select * from photo where id='{0}';", photoid);
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
if(reader.Read()) {
string id = reader.GetString(0);
string title = reader.GetString(1);
string desc = reader.GetString(2);
int license = reader.GetInt32(3);
int isPublic = reader.GetInt32(4);
int isFriend = reader.GetInt32(5);
int isFamily = reader.GetInt32(6);
string lastupdated = reader.GetString(7);
// Retrieve the thumbnail from thumbnail home directory.
photo = new Photo(id, title, desc, license, isPublic,
isFriend, isFamily, lastupdated);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
}
if (photo == null) {
return null;
}
photo.Tags = GetTags(photoid);
return photo;
}
public Photo GetOriginalPhoto(string photoid) {
Photo photo = null;
lock (_photolock) {
string sqlQuery = String.Format(
"select * from originalphoto where id='{0}';", photoid);
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
if(reader.Read()) {
string id = reader.GetString(0);
string title = reader.GetString(1);
string desc = reader.GetString(2);
int license = reader.GetInt32(3);
int isPublic = reader.GetInt32(4);
int isFriend = reader.GetInt32(5);
int isFamily = reader.GetInt32(6);
string lastupdated = reader.GetString(7);
photo = new Photo(id, title, desc, license, isPublic,
isFriend, isFamily, lastupdated);
string tagstring = reader.GetString(8);
photo.Tags = Utils.ParseTagsFromString(tagstring);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
}
return photo;
}
public int GetCountPhotos() {
lock(_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = "select count(id) from photo where isdeleted=0;";
IDataReader reader = dbcmd.ExecuteReader();
int count = 0;
if (reader.Read()) {
count = reader.GetInt32(0);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return count;
}
}
// This method only returns those photos whose isdeleted bit is set to 0.
public ArrayList GetAllPhotos() {
lock(_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = "select * from photo where isdeleted=0 order by dateposted desc;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList photos = new ArrayList();
while(reader.Read()) {
string id = reader.GetString(0);
string title = reader.GetString(1);
string desc = reader.GetString(2);
int license = reader.GetInt32(3);
int isPublic = reader.GetInt32(4);
int isFriend = reader.GetInt32(5);
int isFamily = reader.GetInt32(6);
string lastupdated = reader.GetString(7);
// Retrieve the thumbnail from thumbnail home directory.
Photo photo = new Photo(id, title, desc, license, isPublic,
isFriend, isFamily, lastupdated);
photo.Tags = GetTags(id);
photos.Add(photo);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return photos;
}
}
// This method returns _all_ the photos present in table.
public ArrayList GetAllPhotoIds() {
lock(_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = "select id from photo;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList photoids = new ArrayList();
while(reader.Read()) {
photoids.Add(reader.GetString(0));
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return photoids;
}
}
public string GetDatePosted(string photoid) {
lock (_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select dateposted from photo where id='{0}';", photoid);
IDataReader reader = dbcmd.ExecuteReader();
string dateposted = "0";
if (reader.Read()) {
dateposted = reader.GetString(0);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return dateposted;
}
}
private bool HasPhoto(string photoid, string tablename) {
lock (_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select id from {0} where id='{1}';", tablename, photoid);
IDataReader reader = dbcmd.ExecuteReader();
bool hasphoto = reader.Read();
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return hasphoto;
}
}
public bool HasPhoto(string photoid) {
return HasPhoto(photoid, "photo");
}
public bool HasOriginalPhoto(string photoid) {
return HasPhoto(photoid, "originalphoto");
}
public bool HasLatestPhoto(string photoid, string lastupdate) {
lock (_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select lastupdate from photo where id='{0}';", photoid);
IDataReader reader = dbcmd.ExecuteReader();
bool haslatestphoto = reader.Read();
reader.Close();
dbcmd.Dispose();
dbcon.Close();
if (haslatestphoto) {
string storedlastupdate = reader.GetString(0);
if (storedlastupdate.Equals(lastupdate)) {
return true;
}
}
return false;
}
}
public void SetPhotoDirty(string photoid, bool isdirty) {
if (!HasPhoto(photoid)) throw new Exception();
lock (_photolock) {
int dirtvalue = 0;
if (isdirty) dirtvalue = 1;
RunNonQuery(String.Format(
"update photo set isdirty={0} where id='{1}';", dirtvalue, photoid));
}
}
public bool IsPhotoDirty(string photoid) {
lock (_photolock) {
return RunIsTrueQuery(String.Format(
"select isdirty from photo where id = '{0}';", photoid));
}
}
public ArrayList GetDirtyPhotos() {
lock (_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = "select * from photo where isdirty=1;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList photos = new ArrayList();
while(reader.Read()) {
string id = reader.GetString(0);
string title = reader.GetString(1);
string desc = reader.GetString(2);
int license = reader.GetInt32(3);
int isPublic = reader.GetInt32(4);
int isFriend = reader.GetInt32(5);
int isFamily = reader.GetInt32(6);
string lastupdated = reader.GetString(7);
Photo photo = new Photo(id, title, desc, license, isPublic,
isFriend, isFamily, lastupdated);
photo.Tags = GetTags(id);
photos.Add(photo);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return photos;
}
}
public void UpdateMetaInfoPhoto(Photo p) {
lock (_photolock) {
RunNonQuery(String.Format(
"update photo set title='{0}', desc='{1}', license={2}, ispublic={3}"
+ ", isfriend={4}, isfamily={5} where id='{6}';", EscapeStr(p.Title),
EscapeStr(p.Description),
p.License, p.IsPublic, p.IsFriend, p.IsFamily, p.Id));
}
}
public void InsertPhoto(Photo p) {
lock (_photolock) {
RunNonQuery(String.Format(
"insert into photo (id, title, desc, license, ispublic, "
+ "isfriend, isfamily, lastupdate, dateposted) values('{0}','{1}','{2}',{3}"
+ ",{4},{5},{6},'{7}','{8}');",
p.Id, EscapeStr(p.Title), EscapeStr(p.Description), p.License,
p.IsPublic, p.IsFriend, p.IsFamily, p.LastUpdate, p.DatePosted));
}
foreach (string tag in p.Tags) {
InsertTag(p.Id, tag);
}
}
public void InsertOriginalPhoto(Photo p) {
lock (_photolock) {
RunNonQuery(String.Format(
"insert into originalphoto (id, title, desc, license, ispublic, "
+ "isfriend, isfamily, lastupdate, tags) values('{0}','{1}','{2}',{3}"
+ ",{4},{5},{6},'{7}','{8}');",
p.Id, EscapeStr(p.Title), EscapeStr(p.Description), p.License,
p.IsPublic, p.IsFriend, p.IsFamily, p.LastUpdate, p.TagString));
}
}
// This method also deletes the tags associated with the photo.
// This method doesn't delete the photo from album. The reason is
// that insertion and deletion of photo is independent of its
// inclusion in certain sets.
public void DeletePhoto(string photoid) {
// Delete the photo from database.
lock (_photolock) {
RunNonQuery(String.Format(
"delete from photo where id = '{0}';", photoid));
}
// Now delete the tags.
DeleteAllTags(photoid);
}
public void DeleteAllOriginalPhotos() {
lock (_photolock) {
RunNonQuery("delete from originalphoto;");
}
}
// This method is mainly meant to be used for updating of photos
// done by FlickrCommunicator; during the periodic online checks.
// The method updates the latest copy of the photo in the database,
// except in the case when photo has been updated both at flickr server
// and in the application; in which case, it temporarily stores the
// server copy in-memory, and shows it up as a conflict.
public void UpdatePhoto(Photo src) {
if (!HasPhoto(src.Id)) {
InsertPhoto(src);
return;
}
// We have the photo, call it dst.
Photo dst = GetPhoto(src.Id);
if (!dst.LastUpdate.Equals(src.LastUpdate)) {
if (IsPhotoDirty(src.Id)) {
DeskFlickrUI.GetInstance().AddServerPhoto(src);
} else {
DeletePhoto(src.Id);
InsertPhoto(src);
}
}
}
// This method would set the deleted bit of photo.
public void SetPhotoForDeletion(string photoid) {
lock (_photolock) {
RunNonQuery(String.Format(
"update photo set isdeleted=1 where id='{0}';", photoid));
}
}
public ArrayList GetPhotoIdsDeleted() {
lock (_photolock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = "select id from photo where isdeleted=1;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList photoids = new ArrayList();
while(reader.Read()) {
photoids.Add(reader.GetString(0));
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return photoids;
}
}
/*
* Comments retrieval and setting methods.
*/
public bool HasComment(string photoid) {
lock (_commentlock) {
return RunExistsQuery(String.Format(
"select photoid from comment where photoid='{0}';", photoid));
}
}
public bool HasComment(string photoid, string commentid) {
lock (_commentlock) {
return RunExistsQuery(String.Format(
"select photoid from comment where photoid='{0}' and commentid='{1}';",
photoid, commentid));
}
}
public bool HasCommentText(string photoid, string text) {
lock (_commentlock) {
return RunExistsQuery(String.Format(
"select photoid from comment where photoid='{0}' and"
+ " (username like '%{1}%' or commenthtml like '%{1}%');",
photoid, text));
}
}
public void InsertComment(string photoid, string commentid,
string commenthtml, string username) {
lock (_commentlock) {
RunNonQuery(String.Format(
"insert into comment (photoid, commentid, commenthtml, username)"
+ " values ('{0}','{1}','{2}','{3}');",
photoid, commentid, EscapeStr(commenthtml), EscapeStr(username)));
}
}
public void InsertNewComment(string photoid, string commenthtml) {
Random r = new Random();
string commentid = "new:" + r.Next(1000).ToString();
while (HasComment(photoid, commentid)) {
commentid = "new:" + r.Next(1000).ToString();
}
InsertComment(photoid, commentid, commenthtml, UserName);
SetCommentDirty(photoid, commentid, true);
}
public void UpdateComment(string photoid, string commentid,
string commenthtml, bool isdirty) {
lock (_commentlock) {
int dirty = isdirty ? 1 : 0;
RunNonQuery(String.Format(
"update comment set commenthtml='{0}', isdirty={1}"
+ " where photoid='{2}' and commentid='{3}';",
EscapeStr(commenthtml), dirty, photoid, commentid));
}
}
public void SetCommentDirty(string photoid, string commentid, bool isdirty) {
lock (_commentlock) {
int dirty = 0;
if (isdirty) dirty = 1;
RunNonQuery(String.Format(
"update comment set isdirty={0} where photoid='{1}' and commentid='{2}';",
dirty, photoid, commentid));
}
}
public void MarkCommentForDeletion(string photoid, string commentid) {
lock (_commentlock) {
RunNonQuery(String.Format(
"update comment set isdeleted=1 where photoid='{0}' and commentid='{1}';",
photoid, commentid));
}
}
public void DeleteComment(string photoid, string commentid) {
lock (_commentlock) {
RunNonQuery(String.Format(
"delete from comment where photoid='{0}' and commentid='{1}';",
photoid, commentid));
}
}
public void DeleteCleanComments(string photoid) {
lock (_commentlock) {
RunNonQuery(String.Format(
"delete from comment where photoid='{0}' and isdirty=0;",
photoid));
}
}
// Returns (photoid, commenthtml) pair entries.
public ArrayList GetNewComments() {
lock (_commentlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText =
"select photoid, commenthtml from comment where commentid like '%new:%';";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList comments = new ArrayList();
while (reader.Read()) {
string photoid = reader.GetString(0);
string commenthtml = reader.GetString(1);
PersistentInformation.Entry entry = new Entry(photoid, commenthtml);
comments.Add(entry);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return comments;
}
}
public ArrayList GetDirtyComments() {
lock (_commentlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText =
"select photoid, commentid, commenthtml, username from comment where isdirty=1;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList comments = new ArrayList();
while (reader.Read()) {
string photoid = reader.GetString(0);
string commentid = reader.GetString(1);
string commenthtml = reader.GetString(2);
string username = reader.GetString(3);
Comment comment = new Comment(photoid, commentid, commenthtml, username);
comments.Add(comment);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return comments;
}
}
public ArrayList GetDeletedComments() {
lock (_commentlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText =
"select photoid, commentid, commenthtml, username from comment where isdeleted=1;";
IDataReader reader = dbcmd.ExecuteReader();
ArrayList comments = new ArrayList();
while (reader.Read()) {
string photoid = reader.GetString(0);
string commentid = reader.GetString(1);
string commenthtml = reader.GetString(2);
string username = reader.GetString(3);
Comment comment = new Comment(photoid, commentid, commenthtml, username);
comments.Add(comment);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return comments;
}
}
public ArrayList GetCommentsForPhoto(string photoid) {
lock (_commentlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select commentid, commenthtml, username from comment where photoid='{0}'"
+ " and isdeleted=0;", photoid);
IDataReader reader = dbcmd.ExecuteReader();
ArrayList comments = new ArrayList();
while (reader.Read()) {
string commentid = reader.GetString(0);
string commenthtml = reader.GetString(1);
string username = reader.GetString(2);
Comment comment = new Comment(commentid, commenthtml, username);
comments.Add(comment);
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return comments;
}
}
/*
* Album retrieval and setting methods.
*/
public void InsertAlbum(Album a) {
lock (_albumlock) {
RunNonQuery(String.Format(
"insert into album (setid, title, desc, photoid) "
+ "values ('{0}','{1}','{2}','{3}');",
a.SetId, EscapeStr(a.Title), EscapeStr(a.Desc), a.PrimaryPhotoid));
}
}
public bool HasAlbum(string setid) {
lock (_albumlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select setid from album where setid='{0}';", setid);
IDataReader reader = dbcmd.ExecuteReader();
bool hasalbum = reader.Read();
reader.Close();
dbcmd.Dispose();
dbcon.Close();
return hasalbum;
}
}
public Album GetAlbum(string setid) {
lock (_albumlock) {
IDbConnection dbcon = (IDbConnection) new SqliteConnection(DB_PATH);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = String.Format(
"select * from album where setid = '{0}';", setid);
IDataReader reader = dbcmd.ExecuteReader();
Album album;
if (reader.Read()) {
setid = reader.GetString(0);
string title = reader.GetString(1);