@@ -49,6 +49,8 @@ public class Services.Database : GLib.Object {
49
49
public signal void reminder_added (Objects .Reminder reminder );
50
50
public signal void reminder_deleted (Objects .Reminder reminder );
51
51
52
+ public signal void attachment_deleted (Objects .Attachment attachment );
53
+
52
54
private static Database ? _instance;
53
55
public static Database get_default () {
54
56
if (_instance == null ) {
@@ -109,6 +111,17 @@ public class Services.Database : GLib.Object {
109
111
}
110
112
}
111
113
114
+ Gee . ArrayList<Objects . Attachment > _attachments = null ;
115
+ public Gee . ArrayList<Objects . Attachment > attachments {
116
+ get {
117
+ if (_attachments == null ) {
118
+ _attachments = get_attachments_collection ();
119
+ }
120
+
121
+ return _attachments;
122
+ }
123
+ }
124
+
112
125
construct {
113
126
label_deleted. connect ((label) = > {
114
127
if (_labels. remove (label)) {
@@ -139,6 +152,12 @@ public class Services.Database : GLib.Object {
139
152
debug (" Reminder Removed: %s " , reminder. id. to_string ());
140
153
}
141
154
});
155
+
156
+ attachment_deleted. connect ((attachment) = > {
157
+ if (_attachments. remove (attachment)) {
158
+ debug (" Attachment Removed: %s " , attachment. id. to_string ());
159
+ }
160
+ });
142
161
}
143
162
144
163
public void init_database () {
@@ -342,6 +361,22 @@ public class Services.Database : GLib.Object {
342
361
);
343
362
" " " ;
344
363
364
+ if (db. exec (sql, null , out errormsg) != Sqlite . OK ) {
365
+ warning (errormsg);
366
+ }
367
+
368
+ sql = " " "
369
+ CREATE TABLE IF NOT EXISTS Attachments (
370
+ id TEXT PRIMARY KEY,
371
+ item_id TEXT,
372
+ file_type TEXT,
373
+ file_name TEXT,
374
+ file_size TEXT,
375
+ file_path TEXT,
376
+ FOREIGN KEY (item_id) REFERENCES Items (id) ON DELETE CASCADE
377
+ );
378
+ " " " ;
379
+
345
380
if (db. exec (sql, null , out errormsg) != Sqlite . OK ) {
346
381
warning (errormsg);
347
382
}
@@ -1744,7 +1779,6 @@ public class Services.Database : GLib.Object {
1744
1779
}
1745
1780
1746
1781
// Reminders
1747
-
1748
1782
public void insert_reminder (Objects .Reminder reminder ) {
1749
1783
Sqlite . Statement stmt;
1750
1784
string sql;
@@ -1842,9 +1876,98 @@ public class Services.Database : GLib.Object {
1842
1876
stmt. reset ();
1843
1877
}
1844
1878
1845
- /*
1846
- Queue
1847
- */
1879
+ // Atrachments
1880
+ public void insert_attachment (Objects .Attachment attachment ) {
1881
+ Sqlite . Statement stmt;
1882
+ string sql;
1883
+
1884
+ sql = " " "
1885
+ INSERT OR IGNORE INTO Attachments (id, item_id, file_type, file_name, file_size, file_path)
1886
+ VALUES ($id, $item_id, $file_type, $file_name, $file_size, $file_path);
1887
+ " " " ;
1888
+
1889
+ db. prepare_v2 (sql, sql. length, out stmt);
1890
+ set_parameter_str (stmt, " $id" , attachment. id);
1891
+ set_parameter_str (stmt, " $item_id" , attachment. item_id);
1892
+ set_parameter_str (stmt, " $file_type" , attachment. file_type);
1893
+ set_parameter_str (stmt, " $file_name" , attachment. file_name);
1894
+ set_parameter_int64 (stmt, " $file_size" , attachment. file_size);
1895
+ set_parameter_str (stmt, " $file_path" , attachment. file_path);
1896
+
1897
+ if (stmt. step () != Sqlite . DONE ) {
1898
+ warning (" Error: %d : %s " , db. errcode (), db. errmsg ());
1899
+ } else {
1900
+ attachments. add (attachment);
1901
+ attachment. item. attachment_added (attachment);
1902
+ }
1903
+
1904
+ stmt. reset ();
1905
+ }
1906
+
1907
+ public Gee .ArrayList<Objects . Attachment > get_attachments_collection () {
1908
+ Gee . ArrayList<Objects . Attachment > return_value = new Gee .ArrayList<Objects . Attachment > ();
1909
+ Sqlite . Statement stmt;
1910
+
1911
+ sql = " " "
1912
+ SELECT * FROM Attachments;
1913
+ " " " ;
1914
+
1915
+ db. prepare_v2 (sql, sql. length, out stmt);
1916
+
1917
+ while (stmt. step () == Sqlite . ROW ) {
1918
+ return_value. add (_fill_attachment (stmt));
1919
+ }
1920
+ stmt. reset ();
1921
+ return return_value;
1922
+ }
1923
+
1924
+ public Objects .Attachment _fill_attachment (Sqlite .Statement stmt ) {
1925
+ Objects . Attachment return_value = new Objects .Attachment ();
1926
+ return_value. id = stmt. column_text (0 );
1927
+ return_value. item_id = stmt. column_text (1 );
1928
+ return_value. file_type = stmt. column_text (2 );
1929
+ return_value. file_name = stmt. column_text (3 );
1930
+ return_value. file_size = stmt. column_int64 (4 );
1931
+ return_value. file_path = stmt. column_text (5 );
1932
+ return return_value;
1933
+ }
1934
+
1935
+ public Gee .ArrayList<Objects . Attachment > get_attachments_by_item (Objects .Item item ) {
1936
+ Gee . ArrayList<Objects . Attachment > return_value = new Gee .ArrayList<Objects . Attachment > ();
1937
+ lock (_attachments) {
1938
+ foreach (var attachment in attachments) {
1939
+ if (attachment. item_id == item. id) {
1940
+ return_value. add (attachment);
1941
+ }
1942
+ }
1943
+
1944
+ return return_value;
1945
+ }
1946
+ }
1947
+
1948
+ public void delete_attachment (Objects .Attachment attachment ) {
1949
+ Sqlite . Statement stmt;
1950
+
1951
+ sql = " " "
1952
+ DELETE FROM Attachments WHERE id=$id;
1953
+ " " " ;
1954
+
1955
+ db. prepare_v2 (sql, sql. length, out stmt);
1956
+ set_parameter_str (stmt, " $id" , attachment. id);
1957
+
1958
+ if (stmt. step () == Sqlite . DONE ) {
1959
+ attachment. deleted ();
1960
+ attachment. item. attachment_deleted (attachment);
1961
+ } else {
1962
+ warning (" Error: %d : %s " , db. errcode (), db. errmsg ());
1963
+ }
1964
+
1965
+ stmt. reset ();
1966
+ }
1967
+
1968
+ /*
1969
+ * Queue
1970
+ */
1848
1971
1849
1972
public void insert_queue (Objects .Queue queue ) {
1850
1973
Sqlite . Statement stmt;
@@ -2170,10 +2293,10 @@ public class Services.Database : GLib.Object {
2170
2293
stmt. bind_int (par_position, val);
2171
2294
}
2172
2295
2173
- // private void set_parameter_int64 (Sqlite.Statement? stmt, string par, int64 val) {
2174
- // int par_position = stmt.bind_parameter_index (par);
2175
- // stmt.bind_int64 (par_position, val);
2176
- // }
2296
+ private void set_parameter_int64 (Sqlite .Statement ? stmt , string par , int64 val ) {
2297
+ int par_position = stmt. bind_parameter_index (par);
2298
+ stmt. bind_int64 (par_position, val);
2299
+ }
2177
2300
2178
2301
private void set_parameter_str (Sqlite .Statement ? stmt , string par , string val ) {
2179
2302
int par_position = stmt. bind_parameter_index (par);
0 commit comments