summaryrefslogtreecommitdiff
path: root/plugins/shotwell-data-imports/FSpotTagsTable.vala
blob: 8ac98078915e1579fcf03e098485bcfea65c2c79 (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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

namespace DataImports.FSpot.Db {

/**
 * The value object for the "tags" table, representing a single database row.
 */
public class FSpotTagRow : Object {
    public int64 tag_id;
    public string name;
    public int64 category_id;
    public bool is_category;
    public int sort_priority;
    public string stock_icon; // only store stock icons
}

/**
 * This class represents the F-Spot tags table.
 */
public class FSpotTagsTable : FSpotDatabaseTable<FSpotTagRow> {
    public static const string TABLE_NAME = "Tags";
    
    public static const string PREFIX_STOCK_ICON = "stock_icon:";
    public static const string STOCK_ICON_FAV    = "stock_icon:emblem-favorite";
    public static const string STOCK_ICON_PEOPLE = "stock_icon:emblem-people";
    public static const string STOCK_ICON_PLACES = "stock_icon:emblem-places";
    public static const string STOCK_ICON_EVENTS = "stock_icon:emblem-event";
    
    private FSpotTableBehavior<FSpotPhotoTagRow> photo_tags_behavior;
    
    public FSpotTagsTable(Sqlite.Database db, FSpotDatabaseBehavior db_behavior) {
        base(db);
        set_behavior(db_behavior.get_tags_behavior());
        photo_tags_behavior = db_behavior.get_photo_tags_behavior();
    }
    
    public FSpotTagRow? get_by_id(int64 tag_id) throws DatabaseError {
        Sqlite.Statement stmt;
        FSpotTagRow? row = null;
        string column_list = get_joined_column_list();
        string sql = "SELECT %s FROM %s WHERE id=?".printf(column_list, table_name);

        int res = fspot_db.prepare_v2(sql, -1, out stmt);
        if (res != Sqlite.OK)
            throw_error("Statement failed: %s".printf(sql), res);
        
        res = stmt.bind_int64(1, tag_id);
        assert(res == Sqlite.OK);
        
        res = stmt.step();
        if (res == Sqlite.ROW)
            behavior.build_row(stmt, out row);
        else if (res == Sqlite.DONE)
            message("Could not find tag row with ID %d", (int)tag_id);
        
        return row;
    }
    
    public Gee.ArrayList<FSpotTagRow> get_by_photo_id(int64 photo_id) throws DatabaseError {
        Gee.ArrayList<FSpotTagRow> rows = new Gee.ArrayList<FSpotTagRow?>();
        
        Sqlite.Statement stmt;
        
        string column_list = get_joined_column_list(true);
        string sql = "SELECT %1$s FROM %2$s, %3$s WHERE %3$s.photo_id=? AND %3$s.tag_id = %2$s.id".printf(
            column_list, table_name, photo_tags_behavior.get_table_name()
        );

        int res = fspot_db.prepare_v2(sql, -1, out stmt);
        if (res != Sqlite.OK)
            throw_error("Statement failed: %s".printf(sql), res);
        
        res = stmt.bind_int64(1, photo_id);
        if (res != Sqlite.OK)
            throw_error("Bind failed for photo_id", res);
        
        res = stmt.step();
        while (res == Sqlite.ROW) {
            FSpotTagRow row;
            behavior.build_row(stmt, out row);
            rows.add(row);
            res = stmt.step();
        }
        
        return rows;
    }
}

public class FSpotTagsV0Behavior : FSpotTableBehavior<FSpotTagRow>, Object {
    private static FSpotTagsV0Behavior instance;
    
    private FSpotTagsV0Behavior() {
    }
    
    public static FSpotTagsV0Behavior get_instance() {
        if (instance == null)
            instance = new FSpotTagsV0Behavior();
        return instance;
    }
    
    public string get_table_name() {
        return FSpotTagsTable.TABLE_NAME;
    }

    public string[] list_columns() {
        return { "id", "name", "category_id", "is_category", "sort_priority", "icon" };
    }
    
    public void build_row(Sqlite.Statement stmt, out FSpotTagRow row, int offset = 0) {
        row = new FSpotTagRow();
        row.tag_id = stmt.column_int64(offset + 0);
        row.name = stmt.column_text(offset + 1);
        row.category_id = stmt.column_int64(offset + 2);
        row.is_category = (stmt.column_int(offset + 3) > 0);
        row.sort_priority = stmt.column_int(offset + 4);
        string icon_str = stmt.column_text(offset + 5);
        if (icon_str != null && icon_str.has_prefix(FSpotTagsTable.PREFIX_STOCK_ICON))
            row.stock_icon = icon_str;
        else
            row.stock_icon = "";
    }
}

}