Update called vala api.
[stuffkeeper.git] / src / stuffkeeper-data-tag.gob
blob4ad9de0cd0ad9e877bb099d20edce97c87c993c2
1 requires 2.0.10
3 %h{
4 #include "stuffkeeperglue.h"
5 %}
6 %{
7 #include "stuffkeeper-data-item.h"
8 %}
10 %ph{
11 #include <glib/gstdio.h>
12 #include <stdio.h>
13 #include <sqlite3.h>
14 #include "stuffkeeper-data-backend.h"
17 #define KEYFILE_GENERAL "general"
18 #define SAVE_TIMEOUT 30000
22 class Stuffkeeper:Data:Tag from G:Object 
24     private GList *items = {NULL} destroywith g_list_free;
25     private sqlite3 *sqlHandle = {NULL};
26     private StuffkeeperDataBackend *skdb = {NULL};
27     private gint id = {0};
29     /**
30      * Signals 
31      */
32     private
33     signal last NONE (NONE)
34         void
35         tag_changed(self)
36         {
37         
38         }
40    
41     /**
42      * Constructors
43      */
44     public
45     StuffkeeperDataTag *
46     open_from_id(StuffkeeperDataBackend *skdb, gint id)
47     {
48         Self *obj = GET_NEW;
49         obj->_priv->id = id;
50         obj->_priv->skdb = STUFFKEEPER_DATA_BACKEND(skdb);
51         obj->_priv->sqlHandle = stuffkeeper_data_backend_get_handle(obj->_priv->skdb);;
52         return obj;
53     }
54     public
55         StuffkeeperDataTag*
56         new(StuffkeeperDataBackend *skdb)
57         {
58             Self *obj = GET_NEW;
59             int result = 0;
60             char *error = NULL;
61             char *query  = NULL;
62             obj->_priv->skdb = STUFFKEEPER_DATA_BACKEND(skdb);
63             obj->_priv->sqlHandle = stuffkeeper_data_backend_get_handle(obj->_priv->skdb);;
65             query = sqlite3_mprintf
66                 ("INSERT INTO 'Tags' ('mtime','name')"
67                  "values (%i,'%q');",
68                  (int)time(NULL), "New Tag");
70             result = sqlite3_exec(obj->_priv->sqlHandle, query, NULL, NULL, &error);
71             if (result != SQLITE_OK) {
72                 printf("failed: %s\n", error);
73             }
74             sqlite3_free(query);
75             obj->_priv->id = sqlite3_last_insert_rowid(obj->_priv->sqlHandle);
77             return obj;
78         }
80     public
81     StuffkeeperDataTag*
82     new_with_id(StuffkeeperDataBackend *skdb, gint id)
83     {
84         Self *obj = GET_NEW;
85         int result = 0;
86         char *error = NULL;
87         char *query  = NULL;
89         obj->_priv->skdb = STUFFKEEPER_DATA_BACKEND(skdb);
90         obj->_priv->sqlHandle = stuffkeeper_data_backend_get_handle(obj->_priv->skdb);;
92         query = sqlite3_mprintf
93             ("INSERT INTO 'Tags' ('id','mtime','name')"
94              "values (%i,%i,'%q');",
95              id,(int)time(NULL), "New Tag");
97         result = sqlite3_exec(obj->_priv->sqlHandle, query, NULL, NULL, &error);
98         if (result != SQLITE_OK) {
99             printf("failed: %s\n", error);
100         }
101         sqlite3_free(query);
102         obj->_priv->id = id; 
104         return obj;
105     }
108     /**
109      * ID (ro)
110      */
111     public
112         gint
113         get_id(self)
114         {
115             return self->_priv->id;
116         }
118     /**
119      * Title (rw)
120      */
121     public 
122     gchar *
123     get_title(self)
124     {
125         char *retv = NULL;
126         char *query = sqlite3_mprintf("SELECT name FROM Tags WHERE id=%i", self->_priv->id);
127         sqlite3_stmt *stmt;
128         const char *tail;
129         int r;
130         r = sqlite3_prepare_v2(self->_priv->sqlHandle, query, -1,  &stmt,  &tail);
131         if (r == SQLITE_OK)
132         {
133             if((r = sqlite3_step(stmt)) == SQLITE_ROW)
134             {
135                 retv = g_strdup((gchar *)sqlite3_column_text(stmt, 0));
136             }
137         }
138         sqlite3_finalize(stmt);
140         if(retv == NULL)
141             retv = g_strdup("Failed to get row");
143         sqlite3_free(query);
144         return retv; 
145     }
148     public 
149     void
150     set_title(self, const gchar *title)
151     {
152         int result;
153         char *error;
154         char *query =  sqlite3_mprintf("UPDATE Tags SET mtime=%i,name=%Q WHERE id=%i",(int)time(NULL), title, self->_priv->id);
155         result = sqlite3_exec(self->_priv->sqlHandle, query, NULL, NULL, &error);
156         if (result != SQLITE_OK) {
157             printf("failed: %s\n", error);
158         }
159         sqlite3_free(query);
160         self_tag_changed(self);
161     }
164     /**
165      * Functions to manage 
166      */
169     public
170         void
171         delete_yourself(self)
172         {
173             int result;
174             char *error;
175             char *query =  sqlite3_mprintf("DELETE FROM Tags WHERE id=%i",self->_priv->id);
176             result = sqlite3_exec(self->_priv->sqlHandle, query, NULL, NULL, &error);
177             if (result != SQLITE_OK) {
178                 printf("failed: %s\n", error);
179             }
180             sqlite3_free(query);
181             self->_priv->id = 0;
182         }
184     public
185     int
186     num_items(self)
187     {
188         return g_list_length(self->_priv->items);
189     }
190     public
191     void
192     add_item(self, StuffkeeperDataItem *item)
193     {
194         self->_priv->items = g_list_append(self->_priv->items, STUFFKEEPER_DATA_ITEM(item));
195         self_tag_changed(self);
196     }
197     public 
198     void
199     remove_item(self, StuffkeeperDataItem *item)
200     {
201         self->_priv->items = g_list_remove(self->_priv->items, STUFFKEEPER_DATA_ITEM(item));
202         self_tag_changed(self);
203     }
204     override (G:Object)
205         void
206         finalize(G:Object *obj)
207         {
208             g_debug("destroying tag\n");
209             PARENT_HANDLER(obj);
210         }
212         public
213         GList *
214         get_items(self)
215         {
216                 return g_list_copy(self->_priv->items);
217         }