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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <libgnomevfs/gnome-vfs.h>
#include <glib.h>
#include <sys/types.h>
#include <regex.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include "wh-db.h"
#include "wh-video-model.h"
#include "wh-video-model-row.h"
#include "wh-video-model.h"
#include <string.h>
G_DEFINE_TYPE (WHDB, wh_db, G_TYPE_OBJECT);
#define DB_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), WH_TYPE_DB, WHDBPrivate))
typedef struct _WHDBPrivate WHDBPrivate;
struct _WHDBPrivate
{
sqlite3 *db;
GThreadPool *thread_pool;
};
typedef struct
{
WHDB *db;
gchar *uri;
GnomeVFSFileInfo *vfs_info;
} WHDBThreadData;
enum
{
ROW_CREATED,
ROW_DELETED,
LAST_SIGNAL
};
static guint _db_signals[LAST_SIGNAL] = { 0 };
#define SQL_CREATE_TABLES \
"CREATE TABLE IF NOT EXISTS meta(path text, n_views int, active int, " \
" vtime integer, mtime integer, thumbnail blob, " \
" primary key (path), unique(path));"
enum
{
SQL_GET_ROW_VIA_PATH = 0,
SQL_SET_ACTIVE_VIA_PATH,
SQL_ADD_NEW_ROW,
SQL_GET_ACTIVE_ROWS,
SQL_UPDATE_ROW,
N_SQL_STATEMENTS
};
static gchar *SQLStatementText[] =
{
"select n_views, vtime, mtime, thumbnail from meta where path=:path;",
"update meta set active=1, mtime=:mtime where path=:path;",
"insert into meta(path, n_views, active, vtime, mtime, thumbnail)"
" values(:path, 0, 1, 0, :mtime, 0);",
"select path, n_views, vtime, mtime, thumbnail from meta where active=1;",
"update meta set thumbnail=:thumbnail, n_views=:n_views, vtime=:vtime "
" where path=:path;"
};
static sqlite3_stmt *SQLStatements[N_SQL_STATEMENTS];
static gboolean
wh_db_walk_directory (WHDB *db, const gchar *uri);
static void
wh_db_media_file_found (WHDB *db,
const char *uri,
GnomeVFSFileInfo *vfs_info);
static void
on_vfs_monitor_event (GnomeVFSMonitorHandle *handle,
const gchar *monitor_uri,
const gchar *info_uri,
GnomeVFSMonitorEventType event_type,
gpointer user_data);
static void
wh_db_import_uri_func (gchar *uri,
WHDB *db);
static void
wh_db_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
wh_db_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
wh_db_dispose (GObject *object)
{
WHDBPrivate *priv = DB_PRIVATE (object);
if (priv->thread_pool)
{
g_thread_pool_free (priv->thread_pool, TRUE, TRUE);
priv->thread_pool = NULL;
}
if (G_OBJECT_CLASS (wh_db_parent_class)->dispose)
G_OBJECT_CLASS (wh_db_parent_class)->dispose (object);
}
static void
wh_db_finalize (GObject *object)
{
G_OBJECT_CLASS (wh_db_parent_class)->finalize (object);
}
static void
wh_db_class_init (WHDBClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (WHDBPrivate));
object_class->get_property = wh_db_get_property;
object_class->set_property = wh_db_set_property;
object_class->dispose = wh_db_dispose;
object_class->finalize = wh_db_finalize;
_db_signals[ROW_CREATED] =
g_signal_new ("row-created",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (WHDBClass, row_created),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, WH_TYPE_VIDEO_MODEL_ROW);
}
static void
wh_db_init (WHDB *self)
{
int res, i;
const gchar *data_dir;
gchar *db_filename, *path;
WHDBPrivate *priv = DB_PRIVATE(self);
gnome_vfs_init ();
data_dir = g_get_user_data_dir ();
db_filename = g_build_filename (data_dir, "woohaa", "db", NULL);
path = g_path_get_dirname (db_filename);
g_mkdir_with_parents (path, 0755);
res = sqlite3_open(db_filename, &priv->db);
g_free(path);
g_free(db_filename);
if (res)
{
g_error("Can't open database: %s\n", sqlite3_errmsg(priv->db));
sqlite3_close(priv->db);
return;
}
/* Create DB if not already existing - preexisting will silently fail */
if (sqlite3_exec(priv->db, SQL_CREATE_TABLES, NULL, NULL, NULL))
g_warning("Can't create table: %s\n", sqlite3_errmsg(priv->db));
/* Next mark fields inactive */
if (sqlite3_exec(priv->db, "update meta set active=0;", NULL, NULL, NULL))
g_warning("Can't mark table inactive: %s\n", sqlite3_errmsg(priv->db));
/* precompile statements */
for (i=0; i<N_SQL_STATEMENTS; i++)
if (sqlite3_prepare(priv->db, SQLStatementText[i], -1,
&SQLStatements[i], NULL) != SQLITE_OK)
g_warning("Failed to prepare '%s' : %s",
SQLStatementText[i], sqlite3_errmsg(priv->db));
/* Create thread pool for indexing */
priv->thread_pool = g_thread_pool_new ((GFunc)wh_db_import_uri_func,
self,
-1,
FALSE,
NULL);
}
WHDB*
wh_db_new ()
{
return g_object_new (WH_TYPE_DB, NULL);
}
gboolean
uri_is_media (const gchar *uri)
{
/* Suck */
/* FIXME: use gstreamer tag foo |gvfs mime type to identify */
return (g_str_has_suffix(uri, ".avi")
|| g_str_has_suffix(uri, ".mpeg")
|| g_str_has_suffix(uri, ".wmv")
#ifdef USE_HELIX
|| g_str_has_suffix(uri, ".rmvb")
#endif
|| g_str_has_suffix(uri, ".ogg")
|| g_str_has_suffix(uri, ".mp4")
|| g_str_has_suffix(uri, ".mpg"));
}
static gboolean
wh_db_monitor_add_idle (WHDBThreadData *data)
{
GnomeVFSMonitorHandle *monitor_handle;
gnome_vfs_monitor_add (&monitor_handle,
data->uri,
GNOME_VFS_MONITOR_DIRECTORY,
on_vfs_monitor_event,
data->db);
g_free (data->uri);
g_slice_free (WHDBThreadData, data);
return FALSE;
}
static gboolean
wh_db_media_file_found_idle (WHDBThreadData *data)
{
wh_db_media_file_found (data->db, data->uri, data->vfs_info);
if (data->vfs_info)
gnome_vfs_file_info_unref (data->vfs_info);
g_free (data->uri);
g_slice_free (WHDBThreadData, data);
return FALSE;
}
gboolean
wh_db_import_uri_private (WHDB *db, const gchar *uri)
{
GnomeVFSResult vfs_result;
GnomeVFSFileInfo *vfs_info = NULL;
GnomeVFSFileInfoOptions vfs_options;
gboolean ret = FALSE;
vfs_options =
GNOME_VFS_FILE_INFO_DEFAULT
|GNOME_VFS_FILE_INFO_FOLLOW_LINKS
|GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS;
vfs_info = gnome_vfs_file_info_new ();
vfs_result = gnome_vfs_get_file_info (uri, vfs_info, vfs_options);
if (vfs_result != GNOME_VFS_OK)
goto cleanup;
if (! (vfs_info->valid_fields & (GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS
|GNOME_VFS_FILE_INFO_FIELDS_TYPE)))
goto cleanup;
/* GNOME_VFS_PERM_ACCESS_READABLE would be better, but only the
* file method implements it */
if (! (vfs_info->permissions & GNOME_VFS_PERM_USER_READ))
goto cleanup;
if (vfs_info->type == GNOME_VFS_FILE_TYPE_DIRECTORY)
{
WHDBThreadData *data;
data = g_slice_new0 (WHDBThreadData);
data->uri = g_strdup (uri);
data->db = db;
g_idle_add ((GSourceFunc)wh_db_monitor_add_idle, data);
ret = wh_db_walk_directory (db, uri);
}
else if (vfs_info->type == GNOME_VFS_FILE_TYPE_REGULAR)
{
if (uri_is_media(uri))
{
WHDBThreadData *data;
data = g_slice_new0 (WHDBThreadData);
data->uri = g_strdup (uri);
data->db = db;
data->vfs_info = vfs_info;
g_idle_add ((GSourceFunc)wh_db_media_file_found_idle, data);
goto skip_cleanup;
}
ret = TRUE;
}
cleanup:
if (vfs_info)
gnome_vfs_file_info_unref (vfs_info);
skip_cleanup:
return ret;
}
static gboolean
wh_db_walk_directory (WHDB *db, const gchar *uri)
{
GnomeVFSResult vfs_result;
GnomeVFSDirectoryHandle *vfs_handle = NULL;
GnomeVFSFileInfoOptions vfs_options;
GnomeVFSFileInfo *vfs_info = NULL;
gboolean ret = TRUE;
vfs_options =
GNOME_VFS_FILE_INFO_DEFAULT
|GNOME_VFS_FILE_INFO_FOLLOW_LINKS
|GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS;
vfs_result = gnome_vfs_directory_open (&vfs_handle, uri, vfs_options);
if (vfs_result != GNOME_VFS_OK)
goto cleanup;
vfs_info = gnome_vfs_file_info_new ();
while (gnome_vfs_directory_read_next(vfs_handle, vfs_info) == GNOME_VFS_OK)
{
if (vfs_info->name
&& strcmp(vfs_info->name, ".")
&& strcmp(vfs_info->name, ".."))
{
gchar *entry_uri = NULL;
entry_uri = g_strconcat(uri, "/", vfs_info->name, NULL);
if (entry_uri)
{
ret |= wh_db_import_uri_private (db, entry_uri);
g_free(entry_uri);
}
}
}
cleanup:
if (vfs_info)
gnome_vfs_file_info_unref (vfs_info);
if (vfs_handle)
gnome_vfs_directory_close (vfs_handle);
return ret;
}
static void
wh_db_import_uri_func (gchar *uri, WHDB *db)
{
wh_db_import_uri_private (db, uri);
g_free (uri);
}
static gboolean
wh_db_get_uri (const gchar *uri,
gint *n_views,
gint *vtime,
gint *mtime,
GdkPixbuf **thumb)
{
gboolean res = FALSE;
sqlite3_stmt *stmt = SQLStatements[SQL_GET_ROW_VIA_PATH];
sqlite3_bind_text (stmt, 1, uri, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
if (n_views)
*n_views = sqlite3_column_int(stmt, 0);
if (vtime)
*vtime = sqlite3_column_int(stmt, 1);
if (mtime)
*mtime = sqlite3_column_int(stmt, 2);
if (thumb)
{
int len;
GdkPixdata *pixdata;
guint8 *blob = NULL;
blob = (guint8 *)sqlite3_column_blob (stmt, 3);
len = sqlite3_column_bytes (stmt, 3);
if (sqlite3_column_type (stmt,3) == SQLITE_BLOB)
{
pixdata = g_new0 (GdkPixdata, 1);
if (gdk_pixdata_deserialize (pixdata, len, (const guint8*)blob,
NULL))
*thumb = gdk_pixbuf_from_pixdata (pixdata, TRUE, NULL);
g_free (pixdata);
}
}
res = TRUE;
}
sqlite3_reset(stmt);
return res;
}
static gchar*
wh_db_parse_video_uri_info (const char *uri,
gchar **series,
gchar **episode)
{
gchar *base, *res;
regex_t *regex;
size_t nmatch = 4;
regmatch_t pmatch[4];
/* HAXOR Regexp to extract 'meta data' from common TV show naming */
#define TV_REGEXP "(.*)\\.?[Ss]+([0-9]+)[._ ]*[Ee]+[Pp]*([0-9]+)"
base = g_path_get_basename (uri);
regex = g_malloc0(sizeof(regex_t));
if (regcomp(regex, TV_REGEXP, REG_EXTENDED) != 0)
{
printf("regexp creation failed\n");
}
if (regexec(regex, base, nmatch, pmatch, 0) == 0)
{
char *name;
name = g_strndup (base + pmatch[1].rm_so,
pmatch[1].rm_eo - pmatch[1].rm_so);
name = g_strdelimit (name, "._", ' ');
*series = g_strndup (base + pmatch[2].rm_so,
pmatch[2].rm_eo - pmatch[2].rm_so);
*episode = g_strndup (base + pmatch[3].rm_so,
pmatch[3].rm_eo - pmatch[3].rm_so);
res = name;
if (res == NULL || *res == 0)
{
char *dirname;
/* Assume we have series & episode but no name so grab
* name from parent direcory - handles show-name/s01e01.avi
* style naiming.
*/
dirname = g_path_get_dirname (uri);
name = g_path_get_basename (dirname);
g_free (dirname);
name = g_strdelimit (name, "._", ' ');
res = name;
}
g_free (base);
}
else
{
gchar *p;
p = g_strrstr (base, "."); *p = '\0';
base = g_strdelimit (base, "._", ' ');
res = base;
}
g_free (regex);
return res;
}
static void
wh_db_media_file_found (WHDB *db,
const char *uri,
GnomeVFSFileInfo *vfs_info)
{
WHVideoModelRow *row;
gchar *title, *episode = NULL, *series = NULL;
gint n_views = 0, mtime = 0, vtime = 0;
GdkPixbuf *thumb = NULL;
/* See if we already have file in db.
* YES - mark active.
* NO - add it set vtime, n_views to 0 etc
*/
if (wh_db_get_uri (uri, &n_views, &vtime, &mtime, &thumb))
{
/* Update */
if (vfs_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME)
mtime = vfs_info->mtime;
sqlite3_stmt *stmt = SQLStatements[SQL_SET_ACTIVE_VIA_PATH];
sqlite3_bind_int (stmt, 1, mtime);
sqlite3_bind_text (stmt, 2, uri, -1, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_reset(stmt);
}
else
{
/* New - create row entry*/
if (vfs_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME)
mtime = vfs_info->mtime;
sqlite3_stmt *stmt = SQLStatements[SQL_ADD_NEW_ROW];
sqlite3_bind_text (stmt, 1, uri, -1, SQLITE_STATIC);
sqlite3_bind_int (stmt, 2, mtime); /* mtime */
sqlite3_step(stmt);
sqlite3_reset(stmt);
}
row = wh_video_model_row_new ();
wh_video_model_row_set_path (row, uri);
title = wh_db_parse_video_uri_info ((const char *)uri,
&series,
&episode);
wh_video_model_row_set_title (row, title);
wh_video_model_row_set_extended_info (row, series, episode);
g_free(title);
if (thumb)
{
wh_video_model_row_set_thumbnail (row, thumb);
g_object_unref (thumb);
}
wh_video_model_row_set_n_views (row, n_views);
wh_video_model_row_set_age (row, mtime);
wh_video_model_row_set_vtime (row, vtime);
g_signal_emit (db, _db_signals[ROW_CREATED], 0, row);
g_object_unref (row);
}
void
wh_db_sync_row (WHVideoModelRow *row)
{
GdkPixdata *pixdata = NULL;
GdkPixbuf *pixbuf = NULL;
guint8 *data = NULL;
sqlite3_stmt *stmt = SQLStatements[SQL_UPDATE_ROW];
sqlite3_bind_int (stmt, 2, wh_video_model_row_get_n_views (row));
sqlite3_bind_int (stmt, 3, wh_video_model_row_get_vtime (row));
pixbuf = wh_video_model_row_get_thumbnail (row);
if (pixbuf)
{
guint len = 0;
pixdata = g_new0 (GdkPixdata, 1);
gdk_pixdata_from_pixbuf (pixdata, pixbuf, FALSE);
data = gdk_pixdata_serialize (pixdata, &len);
sqlite3_bind_blob(stmt, 1, (void*)data, len, SQLITE_STATIC);
}
else
{
sqlite3_bind_null(stmt, 1);
}
sqlite3_bind_text (stmt, 4, wh_video_model_row_get_path (row),
-1, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_reset(stmt);
g_free (pixdata);
g_free (data);
}
static void
on_vfs_monitor_event (GnomeVFSMonitorHandle *handle,
const gchar *monitor_uri,
const gchar *info_uri,
GnomeVFSMonitorEventType event_type,
gpointer user_data)
{
WHDB *db = (WHDB*)user_data;
if (event_type == GNOME_VFS_MONITOR_EVENT_CREATED)
{
wh_db_import_uri_private (db, info_uri);
return;
}
if (event_type == GNOME_VFS_MONITOR_EVENT_DELETED)
printf("file '%s' deleted\n", info_uri);
if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED)
printf("file '%s' changed\n", info_uri);
}
gboolean
wh_db_import_uri (WHDB *db, const gchar *uri)
{
WHDBPrivate *priv = DB_PRIVATE (db);
if (priv->thread_pool)
g_thread_pool_push (priv->thread_pool, g_strdup (uri), NULL);
return TRUE;
}
|