~ubuntu-branches/ubuntu/quantal/shotwell/quantal

« back to all changes in this revision

Viewing changes to src/db/FaceLocationTable.vala

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-08-24 11:45:16 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110824114516-01cf7d83qvc9nse1
Tags: 0.11.0-0ubuntu1
New upstream version, drop patches which are in the new version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2011 Yorba Foundation
 
2
 *
 
3
 * This software is licensed under the GNU Lesser General Public License
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution. 
 
5
 */
 
6
 
 
7
#if ENABLE_FACES   
 
8
 
 
9
public struct FaceLocationID {
 
10
    public const int64 INVALID = -1;
 
11
 
 
12
    public int64 id;
 
13
    
 
14
    public FaceLocationID(int64 id = INVALID) {
 
15
        this.id = id;
 
16
    }
 
17
    
 
18
    public bool is_invalid() {
 
19
        return (id == INVALID);
 
20
    }
 
21
    
 
22
    public bool is_valid() {
 
23
        return (id != INVALID);
 
24
    }
 
25
}
 
26
 
 
27
public struct FaceLocationRow {
 
28
    public FaceLocationID face_location_id;
 
29
    public FaceID face_id;
 
30
    public PhotoID photo_id;
 
31
    public string geometry;
 
32
}
 
33
 
 
34
public class FaceLocationTable : DatabaseTable {
 
35
    private static FaceLocationTable instance = null;
 
36
    
 
37
    private FaceLocationTable() {
 
38
        set_table_name("FaceLocationTable");
 
39
        
 
40
        Sqlite.Statement stmt;
 
41
        int res = db.prepare_v2("CREATE TABLE IF NOT EXISTS "
 
42
            + "FaceLocationTable "
 
43
            + "("
 
44
            + "id INTEGER NOT NULL PRIMARY KEY, "
 
45
            + "face_id INTEGER NOT NULL, "
 
46
            + "photo_id INTEGER NOT NULL, "
 
47
            + "geometry TEXT"
 
48
            + ")", -1, out stmt);
 
49
        assert(res == Sqlite.OK);
 
50
        
 
51
        res = stmt.step();
 
52
        if (res != Sqlite.DONE)
 
53
            fatal("create FaceLocationTable", res);
 
54
    }
 
55
    
 
56
    public static FaceLocationTable get_instance() {
 
57
        if (instance == null)
 
58
            instance = new FaceLocationTable();
 
59
        
 
60
        return instance;
 
61
    }
 
62
 
 
63
    public FaceLocationRow add(FaceID face_id, PhotoID photo_id, string geometry) throws DatabaseError {
 
64
        Sqlite.Statement stmt;
 
65
        int res = db.prepare_v2(
 
66
            "INSERT INTO FaceLocationTable (face_id, photo_id, geometry) VALUES (?, ?, ?)",
 
67
             -1, out stmt);
 
68
        assert(res == Sqlite.OK);
 
69
        
 
70
        res = stmt.bind_int64(1, face_id.id);
 
71
        assert(res == Sqlite.OK);
 
72
        res = stmt.bind_int64(2, photo_id.id);
 
73
        assert(res == Sqlite.OK);
 
74
        res = stmt.bind_text(3, geometry);
 
75
        assert(res == Sqlite.OK);
 
76
        
 
77
        res = stmt.step();
 
78
        if (res != Sqlite.DONE)
 
79
            throw_error("FaceLocationTable.add", res);
 
80
        
 
81
        FaceLocationRow row = FaceLocationRow();
 
82
        row.face_location_id = FaceLocationID(db.last_insert_rowid());
 
83
        row.face_id = face_id;
 
84
        row.photo_id = photo_id;
 
85
        row.geometry = geometry;
 
86
        
 
87
        return row;
 
88
    }
 
89
  
 
90
    public Gee.List<FaceLocationRow?> get_all_rows() throws DatabaseError {
 
91
        Sqlite.Statement stmt;
 
92
        int res = db.prepare_v2(
 
93
            "SELECT id, face_id, photo_id, geometry FROM FaceLocationTable",
 
94
            -1, out stmt);
 
95
        assert(res == Sqlite.OK);
 
96
        
 
97
        Gee.List<FaceLocationRow?> rows = new Gee.ArrayList<FaceLocationRow?>();
 
98
        
 
99
        for (;;) {
 
100
            res = stmt.step();
 
101
            if (res == Sqlite.DONE)
 
102
                break;
 
103
            else if (res != Sqlite.ROW)
 
104
                throw_error("FaceLocationTable.get_all_rows", res);
 
105
            
 
106
            // res == Sqlite.ROW
 
107
            FaceLocationRow row = FaceLocationRow();
 
108
            row.face_location_id = FaceLocationID(stmt.column_int64(0));
 
109
            row.face_id = FaceID(stmt.column_int64(1));
 
110
            row.photo_id = PhotoID(stmt.column_int64(2));
 
111
            row.geometry = stmt.column_text(3);
 
112
            
 
113
            rows.add(row);
 
114
        }
 
115
        
 
116
        return rows;
 
117
    }
 
118
    
 
119
    public Gee.ArrayList<string> get_face_source_ids(FaceID face_id) {
 
120
        Sqlite.Statement stmt;
 
121
        int res = db.prepare_v2(
 
122
            "SELECT photo_id FROM FaceLocationTable WHERE face_id = ?",
 
123
            -1, out stmt);
 
124
        assert(res == Sqlite.OK);
 
125
        
 
126
        res = stmt.bind_int64(1, face_id.id);
 
127
        assert(res == Sqlite.OK);
 
128
        
 
129
        Gee.ArrayList<string> result = new Gee.ArrayList<string>();
 
130
        for(;;) {
 
131
            res = stmt.step();
 
132
            if (res == Sqlite.DONE) {
 
133
                break;
 
134
            } else if (res != Sqlite.ROW) {
 
135
                fatal("get_face_source_ids", res);
 
136
 
 
137
                break;
 
138
            }
 
139
            
 
140
            result.add(PhotoID.upgrade_photo_id_to_source_id(PhotoID(stmt.column_int64(0))));
 
141
        }
 
142
        
 
143
        return result;
 
144
    }
 
145
    
 
146
    public string? get_face_source_serialized_geometry(Face face, MediaSource source)
 
147
        throws DatabaseError {
 
148
        Sqlite.Statement stmt;
 
149
        int res = db.prepare_v2(
 
150
            "SELECT geometry FROM FaceLocationTable WHERE face_id=? AND photo_id=?",
 
151
            -1, out stmt);
 
152
        assert(res == Sqlite.OK);
 
153
        
 
154
        res = stmt.bind_int64(1, face.get_instance_id());
 
155
        assert(res == Sqlite.OK);
 
156
        res = stmt.bind_int64(2, ((Photo) source).get_instance_id());
 
157
        assert(res == Sqlite.OK);
 
158
        
 
159
        res = stmt.step();
 
160
        if (res == Sqlite.DONE)
 
161
            return null;
 
162
        else if (res != Sqlite.ROW)
 
163
            throw_error("FaceLocationTable.get_face_source_serialized_geometry", res);
 
164
        
 
165
        return stmt.column_text(0);
 
166
    }
 
167
    
 
168
    public void remove_face_from_source(FaceID face_id, PhotoID photo_id) throws DatabaseError {
 
169
        Sqlite.Statement stmt;
 
170
        int res = db.prepare_v2(
 
171
            "DELETE FROM FaceLocationTable WHERE face_id=? AND photo_id=?",
 
172
            -1, out stmt);
 
173
        assert(res == Sqlite.OK);
 
174
        
 
175
        res = stmt.bind_int64(1, face_id.id);
 
176
        assert(res == Sqlite.OK);
 
177
        res = stmt.bind_int64(2, photo_id.id);
 
178
        assert(res == Sqlite.OK);
 
179
        
 
180
        res = stmt.step();
 
181
        if (res != Sqlite.DONE)
 
182
            throw_error("FaceLocationTable.remove_face_from_source", res);
 
183
    }
 
184
    
 
185
    public void update_face_location_serialized_geometry(FaceLocation face_location)
 
186
        throws DatabaseError {
 
187
        Sqlite.Statement stmt;
 
188
        int res = db.prepare_v2("UPDATE FaceLocationTable SET geometry=? WHERE id=?", -1, out stmt);
 
189
        assert(res == Sqlite.OK);
 
190
        
 
191
        res = stmt.bind_text(1, face_location.get_serialized_geometry());
 
192
        assert(res == Sqlite.OK);
 
193
        res = stmt.bind_int64(2, face_location.get_face_location_id().id);
 
194
        assert(res == Sqlite.OK);
 
195
        
 
196
        res = stmt.step();
 
197
        if (res != Sqlite.DONE)
 
198
            throw_error("FaceLocationTable.update_face_location_serialized_geometry", res);
 
199
    }
 
200
}
 
201
 
 
202
#endif