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
|
#include "browse_model.h"
static void browse_model_init(BrowseModel *browse_model);
static void browse_model_class_init(BrowseModelClass *klass);
static void browse_model_tree_model_init(GtkTreeModelIface *iface);
static void browse_model_finalize(GObject *object);
static GtkTreeModelFlags browse_model_get_flags(GtkTreeModel *tree_model);
static gint browse_model_get_n_columns(GtkTreeModel *tree_model);
static GType browse_model_get_column_type(GtkTreeModel *tree_model, gint index);
static gboolean browse_model_get_iter(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreePath *path);
static GtkTreePath *browse_model_get_path(GtkTreeModel *tree_model, GtkTreeIter *iter);
static void browse_model_get_value(GtkTreeModel *tree_model, GtkTreeIter *iter, gint column, GValue *value);
static gboolean browse_model_iter_next(GtkTreeModel *tree_model, GtkTreeIter *iter);
static gboolean browse_model_iter_children(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent);
static gboolean browse_model_iter_has_child(GtkTreeModel *tree_model, GtkTreeIter *iter);
static gint browse_model_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter *iter);
static gboolean browse_model_iter_nth_child(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent, gint n);
static gboolean browse_model_iter_parent(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *child);
static GObjectClass *parent_class = NULL;
GType browse_model_get_type() {
static GType browse_model_type = 0;
if(browse_model_type == 0) {
static const GTypeInfo browse_model_info = {
sizeof(BrowseModelClass),
NULL,
NULL,
(GClassInitFunc) browse_model_class_init,
NULL,
NULL,
sizeof(BrowseModel),
0,
(GInstanceInitFunc) browse_model_init
};
static const GInterfaceInfo tree_model_info = {
(GInterfaceInitFunc) browse_model_tree_model_init,
NULL,
NULL
};
browse_model_type = g_type_register_static(G_TYPE_OBJECT, "BrowseModel", &browse_model_info, (GTypeFlags)0);
g_type_add_interface_static(browse_model_type, GTK_TYPE_TREE_MODEL, &tree_model_info);
}
return browse_model_type;
}
static void browse_model_class_init(BrowseModelClass *klass) {
GObjectClass *object_class;
parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
object_class = (GObjectClass*)klass;
object_class->finalize = browse_model_finalize;
}
static void browse_model_tree_model_init(GtkTreeModelIface *iface) {
iface->get_flags = browse_model_get_flags;
iface->get_n_columns = browse_model_get_n_columns;
iface->get_column_type = browse_model_get_column_type;
iface->get_iter = browse_model_get_iter;
iface->get_path = browse_model_get_path;
iface->get_value = browse_model_get_value;
iface->iter_next = browse_model_iter_next;
iface->iter_children = browse_model_iter_children;
iface->iter_has_child = browse_model_iter_has_child;
iface->iter_n_children = browse_model_iter_n_children;
iface->iter_nth_child = browse_model_iter_nth_child;
iface->iter_parent = browse_model_iter_parent;
}
static void browse_model_init(BrowseModel *browse_model) {
BrowseModelRecord temp;
GArray *array;
browse_model->n_columns = BROWSE_MODEL_N_COLUMNS;
browse_model->column_types[0] = G_TYPE_STRING;
g_assert(BROWSE_MODEL_N_COLUMNS == 1);
browse_model->num_rows = 0;
browse_model->rows = g_array_new(TRUE, FALSE, sizeof(BrowseModelRecord));
browse_model->stamp = g_random_int();
if(db_get_top_level_directories(&array)) {
for(int i = 0; i < array->len; i++) {
struct directory_t *dir;
dir = &g_array_index(array, struct directory_t, i);
temp.dir.name = g_strdup(dir->name);
temp.dir.dirid = dir->dirid;
temp.parent = NULL;
temp.children = NULL;
temp.pos = i;
g_free(dir->name);
g_array_append_val(browse_model->rows, temp);
browse_model->num_rows++;
}
g_array_free(array, TRUE);
}
}
static void finalize_recursive(BrowseModelRecord *record) {
if(!record->children)
return;
for(int i = 0; i < record->children->len; i++) {
BrowseModelRecord *temp;
temp = &g_array_index(record->children, BrowseModelRecord, i);
g_free(temp->dir.name);
}
}
static void browse_model_finalize(GObject *object) {
BrowseModel *browse_model;
g_assert(MODEL_IS_BROWSE(object));
browse_model = BROWSE_MODEL(object);
for(int i = 0; i < browse_model->rows->len; i++) {
BrowseModelRecord *temp;
temp = &g_array_index(browse_model->rows, BrowseModelRecord, i);
g_free(temp->dir.name);
finalize_recursive(temp);
}
g_array_free(browse_model->rows, TRUE);
}
static GtkTreeModelFlags browse_model_get_flags(GtkTreeModel *tree_model) {
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), (GtkTreeModelFlags)0);
return GTK_TREE_MODEL_ITERS_PERSIST;
}
static gint browse_model_get_n_columns(GtkTreeModel *tree_model) {
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), 0);
return BROWSE_MODEL(tree_model)->n_columns;
}
static GType browse_model_get_column_type(GtkTreeModel *tree_model, gint index) {
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), G_TYPE_INVALID);
g_return_val_if_fail(index < BROWSE_MODEL(tree_model)->n_columns && index >= 0, G_TYPE_INVALID);
return BROWSE_MODEL(tree_model)->column_types[index];
}
inline static void fill_dir(BrowseModelRecord *record) {
GArray *array;
record->children = g_array_new(FALSE, FALSE, sizeof(BrowseModelRecord));
if(db_get_directories(record->dir.dirid, &array)) {
for(int i = 0; i < array->len; i++) {
BrowseModelRecord temp;
struct directory_t *dir;
dir = &g_array_index(array, struct directory_t, i);
temp.dir.name = g_strdup(dir->name);
temp.dir.dirid = dir->dirid;
temp.parent = record;
temp.children = NULL;
temp.pos = i;
g_array_append_val(record->children, temp);
g_free(dir->name);
}
g_array_free(array, TRUE);
}
}
#define BROWSE_MODEL_ENSURE_CHILDREN(obj) if(!(obj)->children) fill_dir((obj))
inline static BrowseModelRecord *browse_model_get_record_recursive(BrowseModel *browse_model, gint *indices, gint depth) {
BrowseModelRecord *temp;
if(depth == 1) {
return &g_array_index(browse_model->rows, BrowseModelRecord, indices[depth-1]);
} else {
temp = browse_model_get_record_recursive(browse_model, indices, depth-1);
BROWSE_MODEL_ENSURE_CHILDREN(temp);
return &g_array_index(temp->children, BrowseModelRecord, indices[depth-1]);
}
}
static gboolean browse_model_get_iter(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreePath *path) {
BrowseModel *browse_model;
BrowseModelRecord *record;
gint *indices, n, depth;
g_assert(MODEL_IS_BROWSE(tree_model));
g_assert(path != NULL);
browse_model = BROWSE_MODEL(tree_model);
indices = gtk_tree_path_get_indices(path);
depth = gtk_tree_path_get_depth(path);
n = indices[depth-1];
//if(n >= browse_model->num_rows || n < 0)
// return FALSE;
//record = &g_array_index(browse_model->rows, BrowseModelRecord, n);
record = browse_model_get_record_recursive(browse_model, indices, depth);
g_assert(record != NULL);
g_assert(record->pos == n);
iter->stamp = browse_model->stamp;
iter->user_data = record;
iter->user_data2 = NULL;
iter->user_data3 = NULL;
return TRUE;
}
static GtkTreePath *browse_model_get_path(GtkTreeModel *tree_model, GtkTreeIter *iter) {
GtkTreePath *path;
BrowseModelRecord *record;
BrowseModel *browse_model;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), NULL);
g_return_val_if_fail(iter != NULL, NULL);
g_return_val_if_fail(iter->user_data != NULL, NULL);
browse_model = BROWSE_MODEL(tree_model);
record = (BrowseModelRecord*)iter->user_data;
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, record->pos);
return path;
}
static void browse_model_get_value(GtkTreeModel *tree_model, GtkTreeIter *iter, gint column, GValue *value) {
BrowseModelRecord *record;
BrowseModel *browse_model;
g_return_if_fail(MODEL_IS_BROWSE(tree_model));
g_return_if_fail(iter != NULL);
g_return_if_fail(column < BROWSE_MODEL(tree_model)->n_columns);
g_value_init(value, BROWSE_MODEL(tree_model)->column_types[column]);
browse_model = BROWSE_MODEL(tree_model);
record = (BrowseModelRecord*)iter->user_data;
g_return_if_fail(record != NULL);
//if(record->pos >= browse_model->num_rows)
// g_return_if_reached();
switch(column) {
case BROWSE_MODEL_COL_NAME:
g_value_set_string_take_ownership(value, (record->parent == NULL ? g_strdup(record->dir.name) : g_path_get_basename(record->dir.name)));
break;
}
}
static gboolean browse_model_iter_next(GtkTreeModel *tree_model, GtkTreeIter *iter) {
BrowseModelRecord *record, *nextrecord;
BrowseModel *browse_model;
GArray *array;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), FALSE);
if(iter == NULL || iter->user_data == NULL)
return FALSE;
browse_model = BROWSE_MODEL(tree_model);
record = (BrowseModelRecord*)iter->user_data;
BROWSE_MODEL_ENSURE_CHILDREN(record);
array = (record->parent ? record->parent->children : browse_model->rows);
if((record->pos + 1) >= array->len)
return FALSE;
nextrecord = &g_array_index(array, BrowseModelRecord, record->pos + 1);
g_assert(nextrecord != NULL);
g_assert(nextrecord->pos == (record->pos + 1));
iter->stamp = browse_model->stamp;
iter->user_data = nextrecord;
return TRUE;
}
static gboolean browse_model_iter_children(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent) {
BrowseModel *browse_model;
BrowseModelRecord *parent_record;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), FALSE);
browse_model = BROWSE_MODEL(tree_model);
if(!parent) {
if(browse_model->num_rows == 0)
return FALSE;
iter->stamp = browse_model->stamp;
iter->user_data = &g_array_index(browse_model->rows, BrowseModelRecord, 0);
return TRUE;
}
//g_return_val_if_fail(parent == NULL || parent->user_data != NULL, FALSE);
parent_record = (BrowseModelRecord*)parent->user_data;
BROWSE_MODEL_ENSURE_CHILDREN(parent_record);
if(parent_record->children->len == 0)
return FALSE;
iter->stamp = browse_model->stamp;
iter->user_data = &g_array_index(parent_record->children, BrowseModelRecord, 0);
return TRUE;
}
static gboolean browse_model_iter_has_child(GtkTreeModel *tree_model, GtkTreeIter *iter) {
BrowseModelRecord *record;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), FALSE);
record = (BrowseModelRecord*)iter->user_data;
BROWSE_MODEL_ENSURE_CHILDREN(record);
return (record->parent == NULL && BROWSE_MODEL(tree_model)->num_rows == 0 ? FALSE : (record->children->len > 0 ? TRUE : FALSE));
}
static gint browse_model_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter *iter) {
BrowseModel *browse_model;
BrowseModelRecord *record;
GArray *array;
int n;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), -1);
g_return_val_if_fail(iter == NULL || iter->user_data != NULL, 0);
browse_model = BROWSE_MODEL(tree_model);
if(!iter) {
return browse_model->num_rows;
}
record = (BrowseModelRecord*)iter->user_data;
//array = NULL;
//g_return_val_if_fail(db_get_wallpapers(record->dir.dirid, &array), -1);
//for(n = 0; (&g_array_index(array, struct wallpaper_t, n))->filepath; n++);
BROWSE_MODEL_ENSURE_CHILDREN(record);
return record->children->len;
return n;
}
static gboolean browse_model_iter_nth_child(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent, gint n) {
BrowseModelRecord *record, *parent_record;
BrowseModel *browse_model;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), FALSE);
browse_model = BROWSE_MODEL(tree_model);
if(!parent) {
if(n >= browse_model->num_rows)
return FALSE;
record = &g_array_index(browse_model->rows, BrowseModelRecord, n);
iter->stamp = browse_model->stamp;
iter->user_data = record;
return TRUE;
}
//if(n >= browse_model->num_rows)
// return FALSE;
parent_record = parent->user_data;
BROWSE_MODEL_ENSURE_CHILDREN(parent_record);
if(n >= parent_record->children->len)
return FALSE;
record = &g_array_index(parent_record->children, BrowseModelRecord, n);
g_assert(record != NULL);
g_assert(record->pos == n);
iter->stamp = browse_model->stamp;
iter->user_data = record;
return TRUE;
}
static gboolean browse_model_iter_parent(GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *child) {
BrowseModelRecord *record, *childrecord;
BrowseModel *browse_model;
g_return_val_if_fail(MODEL_IS_BROWSE(tree_model), FALSE);
browse_model = BROWSE_MODEL(tree_model);
childrecord = (BrowseModelRecord*)child->user_data;
iter->stamp = browse_model->stamp;
iter->user_data = childrecord->parent;
return TRUE;
}
BrowseModel *browse_model_new() {
BrowseModel *new_browse_model;
new_browse_model = (BrowseModel*)g_object_new(MODEL_TYPE_BROWSE, NULL);
g_assert(new_browse_model != NULL);
return new_browse_model;
}
void browse_model_get_dir_record(GtkTreeModel *tree_model, GtkTreeIter *iter, struct directory_t **dir) {
BrowseModel *browse_model;
BrowseModelRecord *record;
g_assert(MODEL_IS_BROWSE(tree_model));
g_assert(iter != NULL);
browse_model = BROWSE_MODEL(tree_model);
record = (BrowseModelRecord*)iter->user_data;
*dir = &record->dir;
}
|