~lubuntu-software-center-team/lubuntu-software-center/vala-port

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
//  Stephen Smally © 2012
//      This program 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 2 of the License, or
//      (at your option) any later version.
//      
//      This program 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 this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//      


using Sqlite;
using Gee;

namespace Lsc.Backend {
    public class LscApp : Object {
        public LscCategory parent_category { get; private set; }
        public string id { get; private set; }
        public string name { get; private set; }
        public string comment { get; private set; }
        public string description { get; private set; }
        public string icon { get; private set; }
        public string[] dependencies { get; private set; }
        
        public LscApp (LscCategory parent, Statement stmt) {
            parent_category = parent;
            id = stmt.column_value(1).to_text();
            name = stmt.column_value(0).to_text();
            comment = stmt.column_value(3).to_text();
            icon = stmt.column_value(4).to_text();
            description = stmt.column_value(5).to_text();
            dependencies = stmt.column_value(6).to_text().split(";");
        }
    }
    
    public class LscCategory : Object {
        public string id { get; private set; }
        public bool showboth { get; private set; }
        public string name { get; private set; }
        public string comment { get; private set; }
        public string icon { get; private set; }
        
        public LscCategory (Statement stmt) {
            id = stmt.column_value(0).to_text();
            showboth = bool.parse(stmt.column_value(2).to_text().down());
            name = stmt.column_value(3).to_text();
            comment = stmt.column_value(4).to_text();
            icon = stmt.column_value(5).to_text();
        }
    }
    
    public class LscDatabase : Object {
        public HashMap<string, LscCategory> current_categories { get; private set; }
        public HashMap<LscCategory, HashMap<string, LscApp>> current_apps { get; private set; }
        private HashMap<string, LscApp> tmp_apps;
        private Database db;
        private Statement get_categories_stmt;
        private Statement get_apps_stmt;
        private string STMT_GET_CATEGORIES = "SELECT * FROM tables";
        
        public void fill_categories () {
            current_categories.clear();
            while (get_categories_stmt.step() == Sqlite.ROW) {
                LscCategory category = new LscCategory(get_categories_stmt);
                current_categories[category.id] = category;
            }
        }
        
        public LscApp get_app (string category_id, string app_id) {
            return current_apps[current_categories[category_id]][app_id];
        }
        
        public void fill_category (string category_id) {   
            current_apps.clear();
            LscCategory cat = current_categories[category_id];
            
            string STMT_GET_FROM_CAT = "SELECT * FROM "+cat.id;
            db.prepare_v2(STMT_GET_FROM_CAT, STMT_GET_FROM_CAT.length, out get_apps_stmt);
            tmp_apps = new HashMap<string, LscApp>();
            
            while (get_apps_stmt.step() == Sqlite.ROW) {
                LscApp app = new LscApp(cat, get_apps_stmt);
                tmp_apps[app.id] = app;
            }
            
            current_apps[cat] = tmp_apps;
        }
        
        public LscDatabase (string path) {
            current_categories = new HashMap<string, LscCategory>();
            current_apps = new HashMap<LscCategory, HashMap<string, LscApp>>();
            
            if (Database.open_v2(path, out db) != Sqlite.OK) {
                stdout.printf("Error opening %s\n", path);
            }
            
            db.prepare_v2(STMT_GET_CATEGORIES, STMT_GET_CATEGORIES.length, out get_categories_stmt);
        }
    }
}