~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/history.c

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* history.c generated by valac 0.12.0, the Vala compiler
 
2
 * generated from history.vala, do not modify */
 
3
 
 
4
 
 
5
#include <glib.h>
 
6
#include <glib-object.h>
 
7
#include <gio/gio.h>
 
8
#include <sqlite3.h>
 
9
#include <stdlib.h>
 
10
#include <string.h>
 
11
#include <gobject/gvaluecollector.h>
 
12
 
 
13
 
 
14
#define TYPE_HISTORY (history_get_type ())
 
15
#define HISTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_HISTORY, History))
 
16
#define HISTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_HISTORY, HistoryClass))
 
17
#define IS_HISTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_HISTORY))
 
18
#define IS_HISTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_HISTORY))
 
19
#define HISTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_HISTORY, HistoryClass))
 
20
 
 
21
typedef struct _History History;
 
22
typedef struct _HistoryClass HistoryClass;
 
23
typedef struct _HistoryPrivate HistoryPrivate;
 
24
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
 
25
#define _sqlite3_close0(var) ((var == NULL) ? NULL : (var = (sqlite3_close (var), NULL)))
 
26
#define _g_free0(var) (var = (g_free (var), NULL))
 
27
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
 
28
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))
 
29
#define __g_list_free__g_object_unref0_0(var) ((var == NULL) ? NULL : (var = (_g_list_free__g_object_unref0_ (var), NULL)))
 
30
 
 
31
#define TYPE_PGN (pgn_get_type ())
 
32
#define PGN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PGN, PGN))
 
33
#define PGN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PGN, PGNClass))
 
34
#define IS_PGN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PGN))
 
35
#define IS_PGN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PGN))
 
36
#define PGN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PGN, PGNClass))
 
37
 
 
38
typedef struct _PGN PGN;
 
39
typedef struct _PGNClass PGNClass;
 
40
typedef struct _PGNPrivate PGNPrivate;
 
41
 
 
42
#define TYPE_PGN_GAME (pgn_game_get_type ())
 
43
#define PGN_GAME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PGN_GAME, PGNGame))
 
44
#define PGN_GAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PGN_GAME, PGNGameClass))
 
45
#define IS_PGN_GAME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PGN_GAME))
 
46
#define IS_PGN_GAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PGN_GAME))
 
47
#define PGN_GAME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PGN_GAME, PGNGameClass))
 
48
 
 
49
typedef struct _PGNGame PGNGame;
 
50
typedef struct _PGNGameClass PGNGameClass;
 
51
#define _pgn_game_unref0(var) ((var == NULL) ? NULL : (var = (pgn_game_unref (var), NULL)))
 
52
#define _pgn_unref0(var) ((var == NULL) ? NULL : (var = (pgn_unref (var), NULL)))
 
53
typedef struct _ParamSpecHistory ParamSpecHistory;
 
54
 
 
55
struct _History {
 
56
        GTypeInstance parent_instance;
 
57
        volatile int ref_count;
 
58
        HistoryPrivate * priv;
 
59
};
 
60
 
 
61
struct _HistoryClass {
 
62
        GTypeClass parent_class;
 
63
        void (*finalize) (History *self);
 
64
};
 
65
 
 
66
struct _HistoryPrivate {
 
67
        GFile* history_dir;
 
68
        gboolean loaded;
 
69
        sqlite3* db;
 
70
};
 
71
 
 
72
struct _PGN {
 
73
        GTypeInstance parent_instance;
 
74
        volatile int ref_count;
 
75
        PGNPrivate * priv;
 
76
        GList* games;
 
77
};
 
78
 
 
79
struct _PGNClass {
 
80
        GTypeClass parent_class;
 
81
        void (*finalize) (PGN *self);
 
82
};
 
83
 
 
84
struct _ParamSpecHistory {
 
85
        GParamSpec parent_instance;
 
86
};
 
87
 
 
88
 
 
89
static gpointer history_parent_class = NULL;
 
90
 
 
91
gpointer history_ref (gpointer instance);
 
92
void history_unref (gpointer instance);
 
93
GParamSpec* param_spec_history (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
 
94
void value_set_history (GValue* value, gpointer v_object);
 
95
void value_take_history (GValue* value, gpointer v_object);
 
96
gpointer value_get_history (const GValue* value);
 
97
GType history_get_type (void) G_GNUC_CONST;
 
98
#define HISTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_HISTORY, HistoryPrivate))
 
99
enum  {
 
100
        HISTORY_DUMMY_PROPERTY
 
101
};
 
102
History* history_new (GFile* data_dir);
 
103
History* history_construct (GType object_type, GFile* data_dir);
 
104
GFile* history_add (History* self, const gchar* date, const gchar* _result_, GError** error);
 
105
static void history_load (History* self);
 
106
void history_remove (History* self, GFile* file);
 
107
void history_update (History* self, GFile* file, const gchar* fen, const gchar* _result_);
 
108
GList* history_get_unfinished (History* self);
 
109
static void _g_object_unref0_ (gpointer var);
 
110
static void _g_list_free__g_object_unref0_ (GList* self);
 
111
static void history_load_history_recursive (History* self, GFile* base_dir, GFile* dir, gboolean load_files, GError** error);
 
112
PGN* pgn_new_from_file (GFile* file, GError** error);
 
113
PGN* pgn_construct_from_file (GType object_type, GFile* file, GError** error);
 
114
gpointer pgn_ref (gpointer instance);
 
115
void pgn_unref (gpointer instance);
 
116
GParamSpec* param_spec_pgn (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
 
117
void value_set_pgn (GValue* value, gpointer v_object);
 
118
void value_take_pgn (GValue* value, gpointer v_object);
 
119
gpointer value_get_pgn (const GValue* value);
 
120
GType pgn_get_type (void) G_GNUC_CONST;
 
121
gpointer pgn_game_ref (gpointer instance);
 
122
void pgn_game_unref (gpointer instance);
 
123
GParamSpec* param_spec_pgn_game (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
 
124
void value_set_pgn_game (GValue* value, gpointer v_object);
 
125
void value_take_pgn_game (GValue* value, gpointer v_object);
 
126
gpointer value_get_pgn_game (const GValue* value);
 
127
GType pgn_game_get_type (void) G_GNUC_CONST;
 
128
const gchar* pgn_game_get_date (PGNGame* self);
 
129
const gchar* pgn_game_get_result (PGNGame* self);
 
130
static void history_finalize (History* obj);
 
131
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
 
132
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
 
133
static gint _vala_array_length (gpointer array);
 
134
 
 
135
 
 
136
History* history_construct (GType object_type, GFile* data_dir) {
 
137
        History* self = NULL;
 
138
        gchar* _tmp0_ = NULL;
 
139
        gchar* _tmp1_;
 
140
        gchar* _tmp2_ = NULL;
 
141
        gchar* _tmp3_;
 
142
        GFile* _tmp4_ = NULL;
 
143
        g_return_val_if_fail (data_dir != NULL, NULL);
 
144
        self = (History*) g_type_create_instance (object_type);
 
145
        _tmp0_ = g_file_get_path (data_dir);
 
146
        _tmp1_ = _tmp0_;
 
147
        _tmp2_ = g_build_filename (_tmp1_, "history", NULL, NULL);
 
148
        _tmp3_ = _tmp2_;
 
149
        _tmp4_ = g_file_new_for_path (_tmp3_);
 
150
        _g_object_unref0 (self->priv->history_dir);
 
151
        self->priv->history_dir = _tmp4_;
 
152
        _g_free0 (_tmp3_);
 
153
        _g_free0 (_tmp1_);
 
154
        return self;
 
155
}
 
156
 
 
157
 
 
158
History* history_new (GFile* data_dir) {
 
159
        return history_construct (TYPE_HISTORY, data_dir);
 
160
}
 
161
 
 
162
 
 
163
static gpointer _g_error_copy0 (gpointer self) {
 
164
        return self ? g_error_copy (self) : NULL;
 
165
}
 
166
 
 
167
 
 
168
GFile* history_add (History* self, const gchar* date, const gchar* _result_, GError** error) {
 
169
        GFile* result = NULL;
 
170
        gchar** _tmp0_;
 
171
        gchar** _tmp1_ = NULL;
 
172
        gchar** tokens;
 
173
        gint tokens_length1;
 
174
        gint _tokens_size_;
 
175
        gchar* _tmp2_;
 
176
        gchar* year;
 
177
        gchar* _tmp3_;
 
178
        gchar* month;
 
179
        gchar* _tmp4_;
 
180
        gchar* day;
 
181
        gchar* relative_path = NULL;
 
182
        GFile* file = NULL;
 
183
        gint version;
 
184
        sqlite3_stmt* statement = NULL;
 
185
        gchar* _tmp23_ = NULL;
 
186
        gchar* _tmp24_;
 
187
        sqlite3_stmt* _tmp25_ = NULL;
 
188
        gint _tmp26_;
 
189
        gint _tmp27_;
 
190
        GError * _inner_error_ = NULL;
 
191
        g_return_val_if_fail (self != NULL, NULL);
 
192
        g_return_val_if_fail (date != NULL, NULL);
 
193
        g_return_val_if_fail (_result_ != NULL, NULL);
 
194
        history_load (self);
 
195
        _tmp1_ = _tmp0_ = g_strsplit (date, ".", 0);
 
196
        tokens = _tmp1_;
 
197
        tokens_length1 = _vala_array_length (_tmp0_);
 
198
        _tokens_size_ = _vala_array_length (_tmp0_);
 
199
        _tmp2_ = g_strdup ("????");
 
200
        year = _tmp2_;
 
201
        _tmp3_ = g_strdup ("??");
 
202
        month = _tmp3_;
 
203
        _tmp4_ = g_strdup ("??");
 
204
        day = _tmp4_;
 
205
        if (tokens_length1 == 3) {
 
206
                gchar* _tmp5_;
 
207
                gchar* _tmp6_;
 
208
                gchar* _tmp7_;
 
209
                _tmp5_ = g_strdup (tokens[0]);
 
210
                _g_free0 (year);
 
211
                year = _tmp5_;
 
212
                _tmp6_ = g_strdup (tokens[1]);
 
213
                _g_free0 (month);
 
214
                month = _tmp6_;
 
215
                _tmp7_ = g_strdup (tokens[2]);
 
216
                _g_free0 (day);
 
217
                day = _tmp7_;
 
218
        }
 
219
        version = 0;
 
220
        while (TRUE) {
 
221
                gchar* filename = NULL;
 
222
                gchar* _tmp10_ = NULL;
 
223
                gchar* _tmp11_ = NULL;
 
224
                gchar* _tmp12_;
 
225
                gchar* _tmp13_ = NULL;
 
226
                gchar* _tmp14_;
 
227
                GFile* _tmp15_ = NULL;
 
228
                gchar* _tmp16_ = NULL;
 
229
                gchar* _tmp17_;
 
230
                gchar* _tmp18_ = NULL;
 
231
                gchar* _tmp19_;
 
232
                GFileOutputStream* _tmp20_ = NULL;
 
233
                GFileOutputStream* _tmp21_;
 
234
                if (version == 0) {
 
235
                        gchar* _tmp8_ = NULL;
 
236
                        _tmp8_ = g_strdup_printf ("%s-%s-%s.pgn", year, month, day);
 
237
                        _g_free0 (filename);
 
238
                        filename = _tmp8_;
 
239
                } else {
 
240
                        gchar* _tmp9_ = NULL;
 
241
                        _tmp9_ = g_strdup_printf ("%s-%s-%s-%d.pgn", year, month, day, version);
 
242
                        _g_free0 (filename);
 
243
                        filename = _tmp9_;
 
244
                }
 
245
                _tmp10_ = g_build_filename (year, month, day, filename, NULL, NULL);
 
246
                _g_free0 (relative_path);
 
247
                relative_path = _tmp10_;
 
248
                _tmp11_ = g_file_get_path (self->priv->history_dir);
 
249
                _tmp12_ = _tmp11_;
 
250
                _tmp13_ = g_build_filename (_tmp12_, relative_path, NULL, NULL);
 
251
                _tmp14_ = _tmp13_;
 
252
                _tmp15_ = g_file_new_for_path (_tmp14_);
 
253
                _g_object_unref0 (file);
 
254
                file = _tmp15_;
 
255
                _g_free0 (_tmp14_);
 
256
                _g_free0 (_tmp12_);
 
257
                _tmp16_ = g_file_get_path (file);
 
258
                _tmp17_ = _tmp16_;
 
259
                _tmp18_ = g_path_get_dirname (_tmp17_);
 
260
                _tmp19_ = _tmp18_;
 
261
                g_mkdir_with_parents (_tmp19_, 0755);
 
262
                _g_free0 (_tmp19_);
 
263
                _g_free0 (_tmp17_);
 
264
                _tmp20_ = g_file_create (file, G_FILE_CREATE_NONE, NULL, &_inner_error_);
 
265
                _tmp21_ = _tmp20_;
 
266
                _g_object_unref0 (_tmp21_);
 
267
                if (_inner_error_ != NULL) {
 
268
                        goto __catch15_g_error;
 
269
                }
 
270
                _g_free0 (filename);
 
271
                break;
 
272
                goto __finally15;
 
273
                __catch15_g_error:
 
274
                {
 
275
                        GError * e;
 
276
                        e = _inner_error_;
 
277
                        _inner_error_ = NULL;
 
278
                        if (!g_error_matches (e, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
 
279
                                GError* _tmp22_;
 
280
                                _tmp22_ = _g_error_copy0 (e);
 
281
                                _inner_error_ = _tmp22_;
 
282
                                _g_error_free0 (e);
 
283
                                goto __finally15;
 
284
                        }
 
285
                        _g_error_free0 (e);
 
286
                }
 
287
                __finally15:
 
288
                if (_inner_error_ != NULL) {
 
289
                        g_propagate_error (error, _inner_error_);
 
290
                        _g_free0 (filename);
 
291
                        _g_object_unref0 (file);
 
292
                        _g_free0 (relative_path);
 
293
                        _g_free0 (day);
 
294
                        _g_free0 (month);
 
295
                        _g_free0 (year);
 
296
                        tokens = (_vala_array_free (tokens, tokens_length1, (GDestroyNotify) g_free), NULL);
 
297
                        return NULL;
 
298
                }
 
299
                version++;
 
300
                _g_free0 (filename);
 
301
        }
 
302
        _tmp23_ = g_strdup_printf ("INSERT INTO GameTable (date, path, result) VALUES (0, \"%s\", \"%s\")", relative_path, _result_);
 
303
        _tmp24_ = _tmp23_;
 
304
        _tmp26_ = sqlite3_prepare_v2 (self->priv->db, _tmp24_, -1, &_tmp25_, NULL);
 
305
        _sqlite3_finalize0 (statement);
 
306
        statement = _tmp25_;
 
307
        g_assert (_tmp26_ == SQLITE_OK);
 
308
        _g_free0 (_tmp24_);
 
309
        _tmp27_ = sqlite3_step (statement);
 
310
        if (_tmp27_ != SQLITE_DONE) {
 
311
                const gchar* _tmp28_ = NULL;
 
312
                _tmp28_ = sqlite3_errmsg (self->priv->db);
 
313
                g_warning ("history.vala:56: Failed to insert game into history index: %s", _tmp28_);
 
314
        }
 
315
        result = file;
 
316
        _sqlite3_finalize0 (statement);
 
317
        _g_free0 (relative_path);
 
318
        _g_free0 (day);
 
319
        _g_free0 (month);
 
320
        _g_free0 (year);
 
321
        tokens = (_vala_array_free (tokens, tokens_length1, (GDestroyNotify) g_free), NULL);
 
322
        return result;
 
323
}
 
324
 
 
325
 
 
326
void history_remove (History* self, GFile* file) {
 
327
        gchar* _tmp0_ = NULL;
 
328
        gchar* relative_path;
 
329
        sqlite3_stmt* statement = NULL;
 
330
        gchar* _tmp1_ = NULL;
 
331
        gchar* _tmp2_;
 
332
        sqlite3_stmt* _tmp3_ = NULL;
 
333
        gint _tmp4_;
 
334
        gint _tmp5_;
 
335
        g_return_if_fail (self != NULL);
 
336
        g_return_if_fail (file != NULL);
 
337
        _tmp0_ = g_file_get_relative_path (self->priv->history_dir, file);
 
338
        relative_path = _tmp0_;
 
339
        _tmp1_ = g_strdup_printf ("DELETE FROM GameTable WHERE path=\"%s\"", relative_path);
 
340
        _tmp2_ = _tmp1_;
 
341
        _tmp4_ = sqlite3_prepare_v2 (self->priv->db, _tmp2_, -1, &_tmp3_, NULL);
 
342
        _sqlite3_finalize0 (statement);
 
343
        statement = _tmp3_;
 
344
        g_assert (_tmp4_ == SQLITE_OK);
 
345
        _g_free0 (_tmp2_);
 
346
        _tmp5_ = sqlite3_step (statement);
 
347
        if (_tmp5_ != SQLITE_DONE) {
 
348
                const gchar* _tmp6_ = NULL;
 
349
                _tmp6_ = sqlite3_errmsg (self->priv->db);
 
350
                g_warning ("history.vala:68: Failed to remove game from history index: %s", _tmp6_);
 
351
        }
 
352
        _sqlite3_finalize0 (statement);
 
353
        _g_free0 (relative_path);
 
354
}
 
355
 
 
356
 
 
357
void history_update (History* self, GFile* file, const gchar* fen, const gchar* _result_) {
 
358
        gchar* _tmp0_ = NULL;
 
359
        gchar* relative_path;
 
360
        sqlite3_stmt* statement = NULL;
 
361
        gchar* _tmp1_ = NULL;
 
362
        gchar* _tmp2_;
 
363
        sqlite3_stmt* _tmp3_ = NULL;
 
364
        gint _tmp4_;
 
365
        gint _tmp5_;
 
366
        g_return_if_fail (self != NULL);
 
367
        g_return_if_fail (file != NULL);
 
368
        g_return_if_fail (fen != NULL);
 
369
        g_return_if_fail (_result_ != NULL);
 
370
        _tmp0_ = g_file_get_relative_path (self->priv->history_dir, file);
 
371
        relative_path = _tmp0_;
 
372
        _tmp1_ = g_strdup_printf ("UPDATE GameTable SET fen=\"%s\", result=\"%s\" WHERE path=\"%s\"", fen, _result_, relative_path);
 
373
        _tmp2_ = _tmp1_;
 
374
        _tmp4_ = sqlite3_prepare_v2 (self->priv->db, _tmp2_, -1, &_tmp3_, NULL);
 
375
        _sqlite3_finalize0 (statement);
 
376
        statement = _tmp3_;
 
377
        g_assert (_tmp4_ == SQLITE_OK);
 
378
        _g_free0 (_tmp2_);
 
379
        _tmp5_ = sqlite3_step (statement);
 
380
        if (_tmp5_ != SQLITE_DONE) {
 
381
                const gchar* _tmp6_ = NULL;
 
382
                _tmp6_ = sqlite3_errmsg (self->priv->db);
 
383
                g_warning ("history.vala:78: Failed to update game in history index: %s", _tmp6_);
 
384
        }
 
385
        _sqlite3_finalize0 (statement);
 
386
        _g_free0 (relative_path);
 
387
}
 
388
 
 
389
 
 
390
static void _g_object_unref0_ (gpointer var) {
 
391
        (var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
 
392
}
 
393
 
 
394
 
 
395
static void _g_list_free__g_object_unref0_ (GList* self) {
 
396
        g_list_foreach (self, (GFunc) _g_object_unref0_, NULL);
 
397
        g_list_free (self);
 
398
}
 
399
 
 
400
 
 
401
GList* history_get_unfinished (History* self) {
 
402
        GList* result = NULL;
 
403
        GList* values;
 
404
        sqlite3_stmt* statement = NULL;
 
405
        sqlite3_stmt* _tmp0_ = NULL;
 
406
        gint _tmp1_;
 
407
        gint _result_;
 
408
        g_return_val_if_fail (self != NULL, NULL);
 
409
        history_load (self);
 
410
        values = NULL;
 
411
        _tmp1_ = sqlite3_prepare_v2 (self->priv->db, "SELECT path FROM GameTable WHERE result=\"*\"", -1, &_tmp0_, NULL);
 
412
        _sqlite3_finalize0 (statement);
 
413
        statement = _tmp0_;
 
414
        _result_ = _tmp1_;
 
415
        g_assert (_result_ == SQLITE_OK);
 
416
        while (TRUE) {
 
417
                gint _tmp2_;
 
418
                const gchar* _tmp3_ = NULL;
 
419
                gchar* _tmp4_;
 
420
                gchar* path;
 
421
                gchar* _tmp5_ = NULL;
 
422
                gchar* _tmp6_;
 
423
                gchar* _tmp7_ = NULL;
 
424
                gchar* _tmp8_;
 
425
                GFile* _tmp9_ = NULL;
 
426
                _tmp2_ = sqlite3_step (statement);
 
427
                _result_ = _tmp2_;
 
428
                if (!(_result_ == SQLITE_ROW)) {
 
429
                        break;
 
430
                }
 
431
                _tmp3_ = sqlite3_column_text (statement, 0);
 
432
                _tmp4_ = g_strdup (_tmp3_);
 
433
                path = _tmp4_;
 
434
                g_debug ("history.vala:94: %s is unfinished", path);
 
435
                _tmp5_ = g_file_get_path (self->priv->history_dir);
 
436
                _tmp6_ = _tmp5_;
 
437
                _tmp7_ = g_build_filename (_tmp6_, path, NULL, NULL);
 
438
                _tmp8_ = _tmp7_;
 
439
                _tmp9_ = g_file_new_for_path (_tmp8_);
 
440
                values = g_list_append (values, _tmp9_);
 
441
                _g_free0 (_tmp8_);
 
442
                _g_free0 (_tmp6_);
 
443
                _g_free0 (path);
 
444
        }
 
445
        if (_result_ != SQLITE_DONE) {
 
446
                const gchar* _tmp10_ = NULL;
 
447
                _tmp10_ = sqlite3_errmsg (self->priv->db);
 
448
                g_warning ("history.vala:99: Failed to get unfinished games: %s", _tmp10_);
 
449
        }
 
450
        result = values;
 
451
        _sqlite3_finalize0 (statement);
 
452
        return result;
 
453
}
 
454
 
 
455
 
 
456
static void history_load (History* self) {
 
457
        gboolean _tmp0_;
 
458
        gboolean have_history;
 
459
        gchar* _tmp1_ = NULL;
 
460
        gchar* _tmp2_;
 
461
        gchar* _tmp3_ = NULL;
 
462
        gchar* _tmp4_;
 
463
        gchar* _tmp5_ = NULL;
 
464
        gchar* _tmp6_;
 
465
        sqlite3* _tmp7_ = NULL;
 
466
        gint _tmp8_;
 
467
        gboolean _tmp9_;
 
468
        sqlite3_stmt* statement = NULL;
 
469
        sqlite3_stmt* _tmp11_ = NULL;
 
470
        gint _tmp12_;
 
471
        gint _result_;
 
472
        gint _tmp13_;
 
473
        const gchar* _tmp15_ = NULL;
 
474
        gchar* _tmp16_ = NULL;
 
475
        gchar* _tmp17_;
 
476
        GFile* _tmp18_ = NULL;
 
477
        GFile* _tmp19_;
 
478
        GFile* old_history_dir;
 
479
        gboolean _tmp20_ = FALSE;
 
480
        GError * _inner_error_ = NULL;
 
481
        g_return_if_fail (self != NULL);
 
482
        if (self->priv->loaded) {
 
483
                return;
 
484
        }
 
485
        _tmp0_ = g_file_query_exists (self->priv->history_dir, NULL);
 
486
        have_history = _tmp0_;
 
487
        _tmp1_ = g_file_get_path (self->priv->history_dir);
 
488
        _tmp2_ = _tmp1_;
 
489
        g_mkdir_with_parents (_tmp2_, 0755);
 
490
        _g_free0 (_tmp2_);
 
491
        _tmp3_ = g_file_get_path (self->priv->history_dir);
 
492
        _tmp4_ = _tmp3_;
 
493
        _tmp5_ = g_build_filename (_tmp4_, "index.db", NULL);
 
494
        _tmp6_ = _tmp5_;
 
495
        _tmp8_ = sqlite3_open_v2 (_tmp6_, &_tmp7_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
 
496
        _sqlite3_close0 (self->priv->db);
 
497
        self->priv->db = _tmp7_;
 
498
        _tmp9_ = _tmp8_ != SQLITE_OK;
 
499
        _g_free0 (_tmp6_);
 
500
        _g_free0 (_tmp4_);
 
501
        if (_tmp9_) {
 
502
                const gchar* _tmp10_ = NULL;
 
503
                _tmp10_ = sqlite3_errmsg (self->priv->db);
 
504
                g_warning ("history.vala:114: Failed to load history index: %s", _tmp10_);
 
505
        }
 
506
        _tmp12_ = sqlite3_prepare_v2 (self->priv->db, "CREATE TABLE IF NOT EXISTS GameTable (id INTEGER PRIMARY KEY, date INT" \
 
507
"EGER, path TEXT, fen TEXT, result TEXT)", -1, &_tmp11_, NULL);
 
508
        _sqlite3_finalize0 (statement);
 
509
        statement = _tmp11_;
 
510
        _result_ = _tmp12_;
 
511
        g_assert (_result_ == SQLITE_OK);
 
512
        _tmp13_ = sqlite3_step (statement);
 
513
        if (_tmp13_ != SQLITE_DONE) {
 
514
                const gchar* _tmp14_ = NULL;
 
515
                _tmp14_ = sqlite3_errmsg (self->priv->db);
 
516
                g_warning ("history.vala:121: Failed to create game table: %s", _tmp14_);
 
517
        }
 
518
        _tmp15_ = g_get_home_dir ();
 
519
        _tmp16_ = g_build_filename (_tmp15_, ".gnome2", "glchess", "history", NULL, NULL);
 
520
        _tmp17_ = _tmp16_;
 
521
        _tmp18_ = g_file_new_for_path (_tmp17_);
 
522
        _tmp19_ = _tmp18_;
 
523
        _g_free0 (_tmp17_);
 
524
        old_history_dir = _tmp19_;
 
525
        if (!have_history) {
 
526
                gboolean _tmp21_;
 
527
                _tmp21_ = g_file_query_exists (old_history_dir, NULL);
 
528
                _tmp20_ = _tmp21_;
 
529
        } else {
 
530
                _tmp20_ = FALSE;
 
531
        }
 
532
        if (_tmp20_) {
 
533
                gchar* _tmp22_ = NULL;
 
534
                gchar* _tmp23_;
 
535
                gchar* _tmp24_ = NULL;
 
536
                gchar* _tmp25_;
 
537
                _tmp22_ = g_file_get_path (old_history_dir);
 
538
                _tmp23_ = _tmp22_;
 
539
                _tmp24_ = g_file_get_path (self->priv->history_dir);
 
540
                _tmp25_ = _tmp24_;
 
541
                g_debug ("history.vala:127: Migrating history from %s to %s", _tmp23_, _tmp25_);
 
542
                _g_free0 (_tmp25_);
 
543
                _g_free0 (_tmp23_);
 
544
                history_load_history_recursive (self, old_history_dir, old_history_dir, FALSE, &_inner_error_);
 
545
                if (_inner_error_ != NULL) {
 
546
                        goto __catch16_g_error;
 
547
                }
 
548
                goto __finally16;
 
549
                __catch16_g_error:
 
550
                {
 
551
                        GError * e;
 
552
                        e = _inner_error_;
 
553
                        _inner_error_ = NULL;
 
554
                        g_warning ("history.vala:134: Failed to migrate history: %s", e->message);
 
555
                        _g_error_free0 (e);
 
556
                }
 
557
                __finally16:
 
558
                if (_inner_error_ != NULL) {
 
559
                        _g_object_unref0 (old_history_dir);
 
560
                        _sqlite3_finalize0 (statement);
 
561
                        g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
 
562
                        g_clear_error (&_inner_error_);
 
563
                        return;
 
564
                }
 
565
        }
 
566
        self->priv->loaded = TRUE;
 
567
        _g_object_unref0 (old_history_dir);
 
568
        _sqlite3_finalize0 (statement);
 
569
}
 
570
 
 
571
 
 
572
static gpointer _pgn_game_ref0 (gpointer self) {
 
573
        return self ? pgn_game_ref (self) : NULL;
 
574
}
 
575
 
 
576
 
 
577
static void history_load_history_recursive (History* self, GFile* base_dir, GFile* dir, gboolean load_files, GError** error) {
 
578
        GFileEnumerator* _tmp0_ = NULL;
 
579
        GFileEnumerator* children;
 
580
        GError * _inner_error_ = NULL;
 
581
        g_return_if_fail (self != NULL);
 
582
        g_return_if_fail (base_dir != NULL);
 
583
        g_return_if_fail (dir != NULL);
 
584
        _tmp0_ = g_file_enumerate_children (dir, "standard::*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &_inner_error_);
 
585
        children = _tmp0_;
 
586
        if (_inner_error_ != NULL) {
 
587
                g_propagate_error (error, _inner_error_);
 
588
                return;
 
589
        }
 
590
        while (TRUE) {
 
591
                GFileInfo* _tmp1_ = NULL;
 
592
                GFileInfo* info;
 
593
                GFileType _tmp2_;
 
594
                _tmp1_ = g_file_enumerator_next_file (children, NULL, &_inner_error_);
 
595
                info = _tmp1_;
 
596
                if (_inner_error_ != NULL) {
 
597
                        g_propagate_error (error, _inner_error_);
 
598
                        _g_object_unref0 (children);
 
599
                        return;
 
600
                }
 
601
                if (info == NULL) {
 
602
                        _g_object_unref0 (info);
 
603
                        _g_object_unref0 (children);
 
604
                        return;
 
605
                }
 
606
                _tmp2_ = g_file_info_get_file_type (info);
 
607
                switch (_tmp2_) {
 
608
                        case G_FILE_TYPE_REGULAR:
 
609
                        {
 
610
                                gchar* _tmp3_ = NULL;
 
611
                                gchar* _tmp4_;
 
612
                                const gchar* _tmp5_ = NULL;
 
613
                                gchar* _tmp6_ = NULL;
 
614
                                gchar* _tmp7_;
 
615
                                GFile* _tmp8_ = NULL;
 
616
                                GFile* _tmp9_;
 
617
                                GFile* f;
 
618
                                gchar* _tmp10_ = NULL;
 
619
                                gchar* _tmp11_;
 
620
                                PGN* _tmp12_ = NULL;
 
621
                                PGN* pgn;
 
622
                                gconstpointer _tmp13_ = NULL;
 
623
                                PGNGame* _tmp14_;
 
624
                                PGNGame* game;
 
625
                                const gchar* _tmp15_ = NULL;
 
626
                                const gchar* _tmp16_ = NULL;
 
627
                                GFile* _tmp17_ = NULL;
 
628
                                GFile* new_file;
 
629
                                if (!load_files) {
 
630
                                        break;
 
631
                                }
 
632
                                _tmp3_ = g_file_get_path (dir);
 
633
                                _tmp4_ = _tmp3_;
 
634
                                _tmp5_ = g_file_info_get_name (info);
 
635
                                _tmp6_ = g_build_filename (_tmp4_, _tmp5_, NULL, NULL);
 
636
                                _tmp7_ = _tmp6_;
 
637
                                _tmp8_ = g_file_new_for_path (_tmp7_);
 
638
                                _tmp9_ = _tmp8_;
 
639
                                _g_free0 (_tmp7_);
 
640
                                _g_free0 (_tmp4_);
 
641
                                f = _tmp9_;
 
642
                                _tmp10_ = g_file_get_path (f);
 
643
                                _tmp11_ = _tmp10_;
 
644
                                g_debug ("history.vala:158: Migrating %s", _tmp11_);
 
645
                                _g_free0 (_tmp11_);
 
646
                                _tmp12_ = pgn_new_from_file (f, &_inner_error_);
 
647
                                pgn = _tmp12_;
 
648
                                if (_inner_error_ != NULL) {
 
649
                                        goto __catch17_g_error;
 
650
                                }
 
651
                                _tmp13_ = g_list_nth_data (pgn->games, (guint) 0);
 
652
                                _tmp14_ = _pgn_game_ref0 ((PGNGame*) _tmp13_);
 
653
                                game = _tmp14_;
 
654
                                _tmp15_ = pgn_game_get_date (game);
 
655
                                _tmp16_ = pgn_game_get_result (game);
 
656
                                _tmp17_ = history_add (self, _tmp15_, _tmp16_, &_inner_error_);
 
657
                                new_file = _tmp17_;
 
658
                                if (_inner_error_ != NULL) {
 
659
                                        _pgn_game_unref0 (game);
 
660
                                        _pgn_unref0 (pgn);
 
661
                                        goto __catch17_g_error;
 
662
                                }
 
663
                                g_file_copy (f, new_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error_);
 
664
                                if (_inner_error_ != NULL) {
 
665
                                        _g_object_unref0 (new_file);
 
666
                                        _pgn_game_unref0 (game);
 
667
                                        _pgn_unref0 (pgn);
 
668
                                        goto __catch17_g_error;
 
669
                                }
 
670
                                _g_object_unref0 (new_file);
 
671
                                _pgn_game_unref0 (game);
 
672
                                _pgn_unref0 (pgn);
 
673
                                goto __finally17;
 
674
                                __catch17_g_error:
 
675
                                {
 
676
                                        GError * e;
 
677
                                        e = _inner_error_;
 
678
                                        _inner_error_ = NULL;
 
679
                                        g_warning ("history.vala:171: Failed to migrate file: %s", e->message);
 
680
                                        _g_error_free0 (e);
 
681
                                }
 
682
                                __finally17:
 
683
                                if (_inner_error_ != NULL) {
 
684
                                        g_propagate_error (error, _inner_error_);
 
685
                                        _g_object_unref0 (f);
 
686
                                        _g_object_unref0 (info);
 
687
                                        _g_object_unref0 (children);
 
688
                                        return;
 
689
                                }
 
690
                                _g_object_unref0 (f);
 
691
                                break;
 
692
                        }
 
693
                        case G_FILE_TYPE_DIRECTORY:
 
694
                        {
 
695
                                gchar* _tmp18_ = NULL;
 
696
                                gchar* _tmp19_;
 
697
                                const gchar* _tmp20_ = NULL;
 
698
                                gchar* _tmp21_ = NULL;
 
699
                                gchar* _tmp22_;
 
700
                                gchar* path;
 
701
                                GFile* _tmp23_ = NULL;
 
702
                                GFile* _tmp24_;
 
703
                                _tmp18_ = g_file_get_path (dir);
 
704
                                _tmp19_ = _tmp18_;
 
705
                                _tmp20_ = g_file_info_get_name (info);
 
706
                                _tmp21_ = g_build_filename (_tmp19_, _tmp20_, NULL, NULL);
 
707
                                _tmp22_ = _tmp21_;
 
708
                                _g_free0 (_tmp19_);
 
709
                                path = _tmp22_;
 
710
                                _tmp23_ = g_file_new_for_path (path);
 
711
                                _tmp24_ = _tmp23_;
 
712
                                history_load_history_recursive (self, base_dir, _tmp24_, TRUE, &_inner_error_);
 
713
                                _g_object_unref0 (_tmp24_);
 
714
                                if (_inner_error_ != NULL) {
 
715
                                        goto __catch18_g_error;
 
716
                                }
 
717
                                goto __finally18;
 
718
                                __catch18_g_error:
 
719
                                {
 
720
                                        GError * e;
 
721
                                        e = _inner_error_;
 
722
                                        _inner_error_ = NULL;
 
723
                                        g_warning ("history.vala:182: Couldn't open directory %s: %s", path, e->message);
 
724
                                        _g_error_free0 (e);
 
725
                                }
 
726
                                __finally18:
 
727
                                if (_inner_error_ != NULL) {
 
728
                                        g_propagate_error (error, _inner_error_);
 
729
                                        _g_free0 (path);
 
730
                                        _g_object_unref0 (info);
 
731
                                        _g_object_unref0 (children);
 
732
                                        return;
 
733
                                }
 
734
                                _g_free0 (path);
 
735
                                break;
 
736
                        }
 
737
                        default:
 
738
                        {
 
739
                                break;
 
740
                        }
 
741
                }
 
742
                _g_object_unref0 (info);
 
743
        }
 
744
        _g_object_unref0 (children);
 
745
}
 
746
 
 
747
 
 
748
static void value_history_init (GValue* value) {
 
749
        value->data[0].v_pointer = NULL;
 
750
}
 
751
 
 
752
 
 
753
static void value_history_free_value (GValue* value) {
 
754
        if (value->data[0].v_pointer) {
 
755
                history_unref (value->data[0].v_pointer);
 
756
        }
 
757
}
 
758
 
 
759
 
 
760
static void value_history_copy_value (const GValue* src_value, GValue* dest_value) {
 
761
        if (src_value->data[0].v_pointer) {
 
762
                dest_value->data[0].v_pointer = history_ref (src_value->data[0].v_pointer);
 
763
        } else {
 
764
                dest_value->data[0].v_pointer = NULL;
 
765
        }
 
766
}
 
767
 
 
768
 
 
769
static gpointer value_history_peek_pointer (const GValue* value) {
 
770
        return value->data[0].v_pointer;
 
771
}
 
772
 
 
773
 
 
774
static gchar* value_history_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
 
775
        if (collect_values[0].v_pointer) {
 
776
                History* object;
 
777
                object = collect_values[0].v_pointer;
 
778
                if (object->parent_instance.g_class == NULL) {
 
779
                        return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
 
780
                } else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
 
781
                        return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
 
782
                }
 
783
                value->data[0].v_pointer = history_ref (object);
 
784
        } else {
 
785
                value->data[0].v_pointer = NULL;
 
786
        }
 
787
        return NULL;
 
788
}
 
789
 
 
790
 
 
791
static gchar* value_history_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
 
792
        History** object_p;
 
793
        object_p = collect_values[0].v_pointer;
 
794
        if (!object_p) {
 
795
                return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
 
796
        }
 
797
        if (!value->data[0].v_pointer) {
 
798
                *object_p = NULL;
 
799
        } else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
 
800
                *object_p = value->data[0].v_pointer;
 
801
        } else {
 
802
                *object_p = history_ref (value->data[0].v_pointer);
 
803
        }
 
804
        return NULL;
 
805
}
 
806
 
 
807
 
 
808
GParamSpec* param_spec_history (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
 
809
        ParamSpecHistory* spec;
 
810
        g_return_val_if_fail (g_type_is_a (object_type, TYPE_HISTORY), NULL);
 
811
        spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
 
812
        G_PARAM_SPEC (spec)->value_type = object_type;
 
813
        return G_PARAM_SPEC (spec);
 
814
}
 
815
 
 
816
 
 
817
gpointer value_get_history (const GValue* value) {
 
818
        g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_HISTORY), NULL);
 
819
        return value->data[0].v_pointer;
 
820
}
 
821
 
 
822
 
 
823
void value_set_history (GValue* value, gpointer v_object) {
 
824
        History* old;
 
825
        g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_HISTORY));
 
826
        old = value->data[0].v_pointer;
 
827
        if (v_object) {
 
828
                g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_HISTORY));
 
829
                g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
 
830
                value->data[0].v_pointer = v_object;
 
831
                history_ref (value->data[0].v_pointer);
 
832
        } else {
 
833
                value->data[0].v_pointer = NULL;
 
834
        }
 
835
        if (old) {
 
836
                history_unref (old);
 
837
        }
 
838
}
 
839
 
 
840
 
 
841
void value_take_history (GValue* value, gpointer v_object) {
 
842
        History* old;
 
843
        g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_HISTORY));
 
844
        old = value->data[0].v_pointer;
 
845
        if (v_object) {
 
846
                g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_HISTORY));
 
847
                g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
 
848
                value->data[0].v_pointer = v_object;
 
849
        } else {
 
850
                value->data[0].v_pointer = NULL;
 
851
        }
 
852
        if (old) {
 
853
                history_unref (old);
 
854
        }
 
855
}
 
856
 
 
857
 
 
858
static void history_class_init (HistoryClass * klass) {
 
859
        history_parent_class = g_type_class_peek_parent (klass);
 
860
        HISTORY_CLASS (klass)->finalize = history_finalize;
 
861
        g_type_class_add_private (klass, sizeof (HistoryPrivate));
 
862
}
 
863
 
 
864
 
 
865
static void history_instance_init (History * self) {
 
866
        self->priv = HISTORY_GET_PRIVATE (self);
 
867
        self->priv->loaded = FALSE;
 
868
        self->ref_count = 1;
 
869
}
 
870
 
 
871
 
 
872
static void history_finalize (History* obj) {
 
873
        History * self;
 
874
        self = HISTORY (obj);
 
875
        _g_object_unref0 (self->priv->history_dir);
 
876
        _sqlite3_close0 (self->priv->db);
 
877
}
 
878
 
 
879
 
 
880
GType history_get_type (void) {
 
881
        static volatile gsize history_type_id__volatile = 0;
 
882
        if (g_once_init_enter (&history_type_id__volatile)) {
 
883
                static const GTypeValueTable g_define_type_value_table = { value_history_init, value_history_free_value, value_history_copy_value, value_history_peek_pointer, "p", value_history_collect_value, "p", value_history_lcopy_value };
 
884
                static const GTypeInfo g_define_type_info = { sizeof (HistoryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) history_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (History), 0, (GInstanceInitFunc) history_instance_init, &g_define_type_value_table };
 
885
                static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
 
886
                GType history_type_id;
 
887
                history_type_id = g_type_register_fundamental (g_type_fundamental_next (), "History", &g_define_type_info, &g_define_type_fundamental_info, 0);
 
888
                g_once_init_leave (&history_type_id__volatile, history_type_id);
 
889
        }
 
890
        return history_type_id__volatile;
 
891
}
 
892
 
 
893
 
 
894
gpointer history_ref (gpointer instance) {
 
895
        History* self;
 
896
        self = instance;
 
897
        g_atomic_int_inc (&self->ref_count);
 
898
        return instance;
 
899
}
 
900
 
 
901
 
 
902
void history_unref (gpointer instance) {
 
903
        History* self;
 
904
        self = instance;
 
905
        if (g_atomic_int_dec_and_test (&self->ref_count)) {
 
906
                HISTORY_GET_CLASS (self)->finalize (self);
 
907
                g_type_free_instance ((GTypeInstance *) self);
 
908
        }
 
909
}
 
910
 
 
911
 
 
912
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
 
913
        if ((array != NULL) && (destroy_func != NULL)) {
 
914
                int i;
 
915
                for (i = 0; i < array_length; i = i + 1) {
 
916
                        if (((gpointer*) array)[i] != NULL) {
 
917
                                destroy_func (((gpointer*) array)[i]);
 
918
                        }
 
919
                }
 
920
        }
 
921
}
 
922
 
 
923
 
 
924
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
 
925
        _vala_array_destroy (array, array_length, destroy_func);
 
926
        g_free (array);
 
927
}
 
928
 
 
929
 
 
930
static gint _vala_array_length (gpointer array) {
 
931
        int length;
 
932
        length = 0;
 
933
        if (array) {
 
934
                while (((gpointer*) array)[length]) {
 
935
                        length++;
 
936
                }
 
937
        }
 
938
        return length;
 
939
}
 
940
 
 
941
 
 
942