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

« back to all changes in this revision

Viewing changes to glchess/src/history.vala

  • 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
public class History
 
2
{
 
3
    private File history_dir;
 
4
    private bool loaded = false;
 
5
    private Sqlite.Database db;
 
6
 
 
7
    public History (File data_dir)
 
8
    {
 
9
        history_dir = File.new_for_path (Path.build_filename (data_dir.get_path (), "history", null));
 
10
    }
 
11
 
 
12
    public File add (string date, string result) throws Error
 
13
    {
 
14
        load ();
 
15
 
 
16
        var tokens = date.split (".");
 
17
        string year = "????", month = "??", day = "??";
 
18
        if (tokens.length == 3)
 
19
        {
 
20
            year = tokens[0];
 
21
            month = tokens[1];
 
22
            day = tokens[2];
 
23
        }
 
24
 
 
25
        string relative_path;
 
26
        File file;
 
27
        int version = 0;
 
28
        while (true)
 
29
        {
 
30
            string filename;
 
31
            if (version == 0)
 
32
                filename = "%s-%s-%s.pgn".printf (year, month, day);
 
33
            else
 
34
                filename = "%s-%s-%s-%d.pgn".printf (year, month, day, version);
 
35
            relative_path = Path.build_filename (year, month, day, filename, null);
 
36
 
 
37
            file = File.new_for_path (Path.build_filename (history_dir.get_path (), relative_path, null));
 
38
            DirUtils.create_with_parents (Path.get_dirname (file.get_path ()), 0755);
 
39
            try
 
40
            {
 
41
                file.create (FileCreateFlags.NONE);
 
42
                break;
 
43
            }
 
44
            catch (Error e)
 
45
            {
 
46
                if (!(e is IOError.EXISTS))
 
47
                    throw e;
 
48
            }
 
49
 
 
50
            version++;
 
51
        }
 
52
 
 
53
        Sqlite.Statement statement;
 
54
        assert (db.prepare_v2 ("INSERT INTO GameTable (date, path, result) VALUES (0, \"%s\", \"%s\")".printf (relative_path, result), -1, out statement) == Sqlite.OK);
 
55
        if (statement.step () != Sqlite.DONE)
 
56
            warning ("Failed to insert game into history index: %s", db.errmsg ());
 
57
 
 
58
        return file;
 
59
    }
 
60
 
 
61
    public void remove (File file)
 
62
    {
 
63
        var relative_path = history_dir.get_relative_path (file);
 
64
 
 
65
        Sqlite.Statement statement;
 
66
        assert (db.prepare_v2 ("DELETE FROM GameTable WHERE path=\"%s\"".printf (relative_path), -1, out statement) == Sqlite.OK);
 
67
        if (statement.step () != Sqlite.DONE)
 
68
            warning ("Failed to remove game from history index: %s", db.errmsg ());
 
69
    }
 
70
 
 
71
    public void update (File file, string fen, string result)
 
72
    {
 
73
        var relative_path = history_dir.get_relative_path (file);
 
74
 
 
75
        Sqlite.Statement statement;
 
76
        assert (db.prepare_v2 ("UPDATE GameTable SET fen=\"%s\", result=\"%s\" WHERE path=\"%s\"".printf (fen, result, relative_path), -1, out statement) == Sqlite.OK);
 
77
        if (statement.step () != Sqlite.DONE)
 
78
            warning ("Failed to update game in history index: %s", db.errmsg ());
 
79
    }
 
80
 
 
81
    public List<File> get_unfinished ()
 
82
    {
 
83
        load ();
 
84
 
 
85
        List<File> values = null;
 
86
 
 
87
        Sqlite.Statement statement;
 
88
        var result = db.prepare_v2 ("SELECT path FROM GameTable WHERE result=\"*\"", -1, out statement);
 
89
        assert (result == Sqlite.OK);
 
90
 
 
91
        while ((result = statement.step ()) == Sqlite.ROW)
 
92
        {
 
93
            var path = statement.column_text (0);
 
94
            debug ("%s is unfinished", path);
 
95
            values.append (File.new_for_path (Path.build_filename (history_dir.get_path (), path, null)));
 
96
        }
 
97
 
 
98
        if (result != Sqlite.DONE)
 
99
            warning ("Failed to get unfinished games: %s", db.errmsg ());
 
100
 
 
101
        return values;
 
102
    }
 
103
 
 
104
    private void load ()
 
105
    {
 
106
        if (loaded)
 
107
            return;
 
108
 
 
109
        var have_history = history_dir.query_exists ();
 
110
        DirUtils.create_with_parents (history_dir.get_path (), 0755);
 
111
 
 
112
        /* Open the database */
 
113
        if (Sqlite.Database.open_v2 (Path.build_filename (history_dir.get_path (), "index.db"), out db) != Sqlite.OK)
 
114
            warning ("Failed to load history index: %s", db.errmsg ());
 
115
 
 
116
        /* Create table */
 
117
        Sqlite.Statement statement;
 
118
        var result = db.prepare_v2 ("CREATE TABLE IF NOT EXISTS GameTable (id INTEGER PRIMARY KEY, date INTEGER, path TEXT, fen TEXT, result TEXT)", -1, out statement);
 
119
        assert (result == Sqlite.OK);
 
120
        if (statement.step () != Sqlite.DONE)
 
121
            warning ("Failed to create game table: %s", db.errmsg ());
 
122
 
 
123
        /* Migrate from old settings */
 
124
        var old_history_dir = File.new_for_path (Path.build_filename (Environment.get_home_dir (), ".gnome2", "glchess", "history", null));
 
125
        if (!have_history && old_history_dir.query_exists ())
 
126
        {
 
127
            debug ("Migrating history from %s to %s", old_history_dir.get_path (), history_dir.get_path ());
 
128
            try
 
129
            {
 
130
                load_history_recursive (old_history_dir, old_history_dir, false);
 
131
            }
 
132
            catch (Error e)
 
133
            {
 
134
                warning ("Failed to migrate history: %s", e.message);
 
135
            }
 
136
        }
 
137
 
 
138
        loaded = true;
 
139
    }
 
140
 
 
141
    private void load_history_recursive (File base_dir, File dir, bool load_files) throws Error
 
142
    {
 
143
        var children = dir.enumerate_children ("standard::*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
 
144
 
 
145
        while (true)
 
146
        {
 
147
            var info = children.next_file ();
 
148
            if (info == null)
 
149
                return;
 
150
 
 
151
            switch (info.get_file_type ())
 
152
            {
 
153
            case FileType.REGULAR:
 
154
                if (!load_files)
 
155
                    break;
 
156
 
 
157
                var f = File.new_for_path (Path.build_filename (dir.get_path (), info.get_name (), null));
 
158
                debug ("Migrating %s", f.get_path ());
 
159
                    
 
160
                try
 
161
                {
 
162
                    var pgn = new PGN.from_file (f);
 
163
                    var game = pgn.games.nth_data (0);
 
164
 
 
165
                    /* Copy file */
 
166
                    var new_file = add (game.date, game.result);
 
167
                    f.copy (new_file, FileCopyFlags.OVERWRITE);
 
168
                }
 
169
                catch (Error e)
 
170
                {
 
171
                    warning ("Failed to migrate file: %s", e.message);
 
172
                }
 
173
                break;
 
174
            case FileType.DIRECTORY:
 
175
                var path = Path.build_filename (dir.get_path (), info.get_name (), null);
 
176
                try
 
177
                {
 
178
                    load_history_recursive (base_dir, File.new_for_path (path), true);
 
179
                }
 
180
                catch (Error e)
 
181
                {
 
182
                    warning ("Couldn't open directory %s: %s", path, e.message);
 
183
                }
 
184
                break;
 
185
            default:
 
186
                break;
 
187
            }
 
188
        }
 
189
    }
 
190
}