aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.c
blob: b183e2985eaaf1f8f325d5ed80fe415f70033d39 (plain)
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
/* Eye on Manga - Manga collection software for Maemo 5
   Copyright (C) 2010-2013  Tom Willemse

   Eye on Manga is free software: you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   Eye on Manga is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Eye on Manga.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "data.h"

#include <errno.h>
#include <gtk/gtk.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include "eom.h"

static gboolean check_and_create_database(gchar*);
static gint create_new_database(const gchar*);
static gboolean execute_non_query(const char *sql);
static GList *get_manga_for_query(const gchar*);
static Manga *get_manga_from_statement(sqlite3_stmt*);

gboolean
data_add_manga(const gchar *name, gint total_qty)
{
    gchar *sql =
        g_strdup_printf(" INSERT INTO manga (name, current_qty, "
                        "total_qty) VALUES ('%s', 0, %d)",
                        name, total_qty);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    return ret;
}

gboolean
data_add_to_manga(gint manga_id, gint count)
{
    gchar *sql =
        g_strdup_printf(" UPDATE manga "
                        " SET current_qty = current_qty + %d "
                        " WHERE id = %d", count, manga_id);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    return ret;
}

gboolean
data_add_volume_to_manga(gint manga_id, gint volume)
{
    char *sql = g_strdup_printf(" INSERT INTO volume "
                                " VALUES (%d, %d, 0) ",
                                manga_id, volume);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    return ret;
}

gboolean
data_delete_manga(gint manga_id)
{
    char *sql = g_strdup_printf("DELETE FROM volume "
                                "WHERE manga_id = %d", manga_id);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    if (ret) {
        sql = g_strdup_printf("DELETE FROM manga "
                              "WHERE id = %d", manga_id);
        ret = execute_non_query(sql);
        g_free(sql);
    }

    return ret;
}

GList *
data_get_incomplete_manga(void)
{
    const char *sql =
        " SELECT   id, "
        "          name, "
        "          current_qty, "
        "          total_qty "
        " FROM     manga "
        " WHERE    current_qty != total_qty "
        " ORDER BY name "
        " COLLATE NOCASE ";

    return get_manga_for_query(sql);
}

GList *
data_get_manga(void)
{
    const char *sql =
        " SELECT   id, "
        "          name, "
        "          current_qty, "
        "          total_qty "
        " FROM     manga "
        " ORDER BY name "
        " COLLATE NOCASE ";

    return get_manga_for_query(sql);
}

Manga *
data_get_manga_by_id(gint manga_id)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    gchar *data_file;
    Manga *manga = NULL;

    data_file = eom_get_data_file();

    if (check_and_create_database(data_file)) {
        if (sqlite3_open(data_file, &db) == SQLITE_OK) {
            int res;
            char *sql =
                g_strdup_printf(" SELECT id, "
                                "        name, "
                                "        current_qty, "
                                "        total_qty "
                                " FROM   manga "
                                " WHERE  id = %d ", manga_id);

            res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
            g_free(sql);

            if (res == SQLITE_OK) {
                if (sqlite3_step(stmt) == SQLITE_ROW) {
                    manga = get_manga_from_statement(stmt);
                }
            }

            sqlite3_finalize(stmt);
        }
        sqlite3_close(db);
    }

    return manga;
}

GList *
data_get_unread_manga(void)
{
    const char *sql =
        " SELECT   m.id, "
        "          m.name, "
        "          m.current_qty, "
        "          m.total_qty "
        " FROM     manga m "
        " JOIN     volume ON (m.id = manga_id) "
        " WHERE    read = 0 "
        " GROUP BY m.id "
        " ORDER BY name "
        " COLLATE NOCASE ";

    return get_manga_for_query(sql);
}

void
data_get_volumes_for_manga(Manga *manga)
{
    gint count;
    Volume *volumes = NULL;
    sqlite3 *db;
    sqlite3_stmt *stmt;
    gchar *data_file;

    data_file = eom_get_data_file();
    count = 0;

    if (check_and_create_database(data_file)) {
        if (sqlite3_open(data_file, &db) == SQLITE_OK) {
            int res;
            char *sql = g_strdup_printf(" SELECT COUNT(id) "
                                        " FROM   volume "
                                        " WHERE  manga_id = %d ",
                                        manga->id);

            res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
            g_free(sql);

            if (res == SQLITE_OK) {
                if (sqlite3_step(stmt) == SQLITE_ROW) {
                    count = sqlite3_column_int(stmt, 0);
                }
            }

            sqlite3_finalize(stmt);
            volumes = calloc(sizeof(Volume), count);

            if (count > 0) {
                sql = g_strdup_printf(" SELECT id, "
                                      "        read "
                                      " FROM   volume "
                                      " WHERE  manga_id = %d ",
                                      manga->id);

                res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt,
                                         NULL);
                g_free(sql);

                if (res == SQLITE_OK) {
                    gint i = 0;
                    while (sqlite3_step(stmt) == SQLITE_ROW) {
                        volumes[i].number = sqlite3_column_int(stmt, 0);
                        volumes[i].read   = sqlite3_column_int(stmt, 1);

                        i++;
                    };
                }

                sqlite3_finalize(stmt);
            }
        }
        sqlite3_close(db);
    }

    manga->vol_count = count;
    manga->volumes = volumes;
}

gboolean
data_mark_volume_read(int read, gint manga_id, gint volume)
{
    gchar *sql = g_strdup_printf(" UPDATE volume "
                                 " SET    read = %d "
                                 " WHERE  manga_id = %d "
                                 " AND    id = %d ",
                                 read, manga_id, volume);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    return ret;
}

gboolean
data_remove_volume_from_manga(gint manga_id, gint volume)
{
    char *sql = g_strdup_printf(" DELETE FROM volume "
                                " WHERE manga_id = %d "
                                " AND id = %d ", manga_id, volume);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    return ret;
}

gboolean
data_update_manga(gint manga_id, const gchar *name, gint total_qty)
{
    gchar *sql =
        g_strdup_printf("UPDATE manga SET "
                        "       name = '%s', "
                        "       total_qty = %d, "
                        "       current_qty = MIN(current_qty, %d) "
                        "WHERE id = %d", name, total_qty, total_qty,
                        manga_id);
    gboolean ret = execute_non_query(sql);

    g_free(sql);

    if (ret) {
        sql = g_strdup_printf("DELETE FROM volume "
                              "WHERE manga_id = %d "
                              "AND id > %d",
                              manga_id, total_qty);
        ret = execute_non_query(sql);
        g_free(sql);
    }

    return ret;
}

static gboolean
check_and_create_database(gchar *data_file)
{
  if (!access(data_file, R_OK) == 0)
    if (create_new_database(data_file)) {
      g_printerr("Couldn't create a new database\n");

      return FALSE; /* Couldn't create database, can't continue */
    }

  return TRUE;
}

static gint
create_new_database(const gchar *filename)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

    if (access(eom_get_config_dir(), R_OK) == -1) {
        if (mkdir(eom_get_config_dir(), S_IRWXU) == -1)
            g_printerr("Can't create directory: %s\n", strerror(errno));
    }

    /* Open database connection, create file */
    rc = sqlite3_open(filename, &db);

    if (rc) {
        g_printerr("Can't open database: %s\n", sqlite3_errmsg(db));
        if (db)
            sqlite3_close(db);
        return -1;
    }

    /* Create collections table */
    rc = sqlite3_exec(db,
                      "CREATE TABLE manga("
                      "id INTEGER PRIMARY KEY,"
                      "name VARCHAR(100),"
                      "current_qty INTEGER,"
                      "total_qty INTEGER"
                      ")", NULL, NULL, &zErrMsg);
    if (rc != SQLITE_OK) {
        g_printerr("Can't create manga table: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }

    /* Create items table */
    rc = sqlite3_exec(db,
                      " CREATE TABLE volume( "
                      " manga_id INTEGER, "
                      " id INTEGER, "
                      " read INTEGER, "
                      " PRIMARY KEY(manga_id, id), "
                      " FOREIGN KEY(manga_id) "
                      "   REFERENCES manga(id)) ",
                      NULL, NULL, &zErrMsg);
    if (rc != SQLITE_OK) {
        g_printerr("Can't create volume table: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }

    sqlite3_close(db);
    return 0;
}

static gboolean
execute_non_query(const gchar *sql)
{
    sqlite3 *db;
    sqlite3_stmt *stmt;
    gchar *data_file;
    gboolean result;

    result = FALSE;
    data_file = eom_get_data_file();

    if (check_and_create_database(data_file)) {
        if (sqlite3_open(data_file, &db) == SQLITE_OK) {
            int res =
                sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);

            if (res == SQLITE_OK) {
                if (sqlite3_step(stmt) == SQLITE_DONE)
                    result = TRUE;
            }

            sqlite3_finalize(stmt);
        }
        sqlite3_close(db);
    }

    return result;
}

static GList *
get_manga_for_query(const gchar *query)
{
    sqlite3 *database;
    sqlite3_stmt *statement;
    gchar *data_file;
    GList *list = NULL;

    data_file = eom_get_data_file();

    if (check_and_create_database(data_file)) {
        if (sqlite3_open(data_file, &database) == SQLITE_OK) {
            int res = sqlite3_prepare_v2(database, query, strlen(query),
                                         &statement, NULL);

            if (res == SQLITE_OK) {
                while (sqlite3_step(statement) == SQLITE_ROW) {
                    Manga *manga = get_manga_from_statement(statement);
                    list = g_list_append(list, (gpointer)manga);
                }
            }
            else
                g_print("error %d: %s\n", res, sqlite3_errmsg(database));

            /* Release the compiled statement from memory */
            sqlite3_finalize(statement);
        }
        sqlite3_close(database);
    }

    return list;
}

static Manga *
get_manga_from_statement(sqlite3_stmt *stmt)
{
  Manga *manga = (Manga *)malloc(sizeof(Manga));

  manga->id = sqlite3_column_int(stmt, 0);
  manga->name = g_strdup(sqlite3_column_text(stmt, 1));
  manga->current_qty = sqlite3_column_int(stmt, 2);
  manga->total_qty = sqlite3_column_int(stmt, 3);
  manga->volumes = NULL;
  manga->vol_count = 0;

  return manga;
}