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

« back to all changes in this revision

Viewing changes to src/Backend/AppsManager.vala

  • Committer: Stephen Smally
  • Date: 2012-07-04 11:46:04 UTC
  • Revision ID: eco.stefi@fastwebnet.it-20120704114604-3esw3fj1oo1j7eed
Implemented Database categories within GUI

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
//      
17
17
 
18
18
using PackageKit;
 
19
using SQLHeavy;
19
20
 
20
21
namespace Lsc.Backend {
21
22
    public enum LoadingType {
24
25
        OTHER; // Installed or so
25
26
    }
26
27
    
 
28
    public class LscCategory : Object {
 
29
        public string id;
 
30
        public string name;
 
31
        public string summary;
 
32
        public string icon;
 
33
        public int records;
 
34
        
 
35
        public LscCategory (string id, string name, string summary, string icon, int records = 0) {
 
36
            this.id = id;
 
37
            this.name = name;
 
38
            this.summary = summary;
 
39
            this.icon = icon;
 
40
            this.records = records;
 
41
        }
 
42
    }
 
43
    
 
44
    public class LscApp : Object {
 
45
                public string id { get; private set; } // A valid id retrieved from a PkPackage
 
46
                public string name { get; private set; }
 
47
                public string summary { get; private set; }
 
48
                public string icon { get; private set; }
 
49
                
 
50
                public LscApp (string id, string name, string summary, string icon) {
 
51
                        this.id = id;
 
52
                        this.name = name;
 
53
                        this.summary = summary;
 
54
                        this.icon = icon;
 
55
                }
 
56
        }
 
57
    
27
58
    public class AppsManager : Object {
28
59
        // Signals
29
 
        public signal void app_added (Package app);
30
 
        public signal void category_added (string category, string group);
 
60
        public signal void app_added (LscApp app);
 
61
        public signal void category_added (LscCategory cat);
31
62
        public signal void loading_started (string text);
32
63
        public signal void loading_finished (LoadingType load);
33
64
        public signal void details_received (Package pkg, Details details);
37
68
        private Control control;
38
69
        private Cancellable transaction;
39
70
        
 
71
        // SQLHeavy stuffs
 
72
        private Database database;
 
73
        
40
74
        // Vars
41
75
        public string[] icon_table;
42
76
        
43
77
        public void get_pkgs_cb (Progress progress, ProgressType type) {
44
78
        }
45
79
        
46
 
        public void get_pkgs (string filters) {
47
 
            Bitfield filter;
48
 
            filter = Filter.bitfield_from_string(filters);
49
 
            loading_started("Loading packages");
50
 
            
51
 
            client.get_packages_async.begin(filter, null, (p) => {
52
 
                if (p.package != null) {
53
 
                    stdout.printf("Get package: %s\n", p.package.get_name());
54
 
                    app_added(p.package);
55
 
                }
56
 
            }, () => {
57
 
                loading_finished(LoadingType.PACKAGES);
58
 
            });
 
80
        public void get_pkgs (string category) {
 
81
            try {
 
82
                                Query get_query = database.prepare ("SELECT * FROM '%s';".printf (category));
 
83
                                QueryResult result = get_query.execute ();
 
84
                                
 
85
                                for (int record = 1; ! result.finished; record++, result.next ()) {
 
86
                                        app_added (new LscApp (
 
87
                                                   result.fetch_string (0),
 
88
                                                   result.fetch_string (1),
 
89
                                                   result.fetch_string (2),
 
90
                                                   result.fetch_string (3)));
 
91
                                }
 
92
                        } catch (GLib.Error e) {
 
93
                                GLib.error ("Error retrieving packages: %s\n", e.message);
 
94
                        }
 
95
                                
59
96
        }
60
97
        
61
98
        public void search_for_packages (string filters, string name) {
62
99
            Bitfield filter;
63
100
            filter = Filter.bitfield_from_string(filters);
64
101
            loading_started("Loading packages");
65
 
            client.search_names_async(filter, {name}, null, (pkg) => {
 
102
            client.search_names_async(filter, name, (pkg) => {
66
103
                if (pkg.package != null) {
67
 
                    app_added(pkg.package);
 
104
                    //app_added(pkg.package);
68
105
                }
69
106
                while (Gtk.events_pending())
70
107
                    Gtk.main_iteration();
71
 
            }, () => {
 
108
            }, null, () => {
72
109
                loading_finished(LoadingType.PACKAGES);
73
110
            });
74
111
        }
75
112
        
76
113
        public void get_details (string pkg_id) {
77
 
            client.get_details_async({pkg_id},
78
 
            null,
79
 
            () => {},
 
114
            client.get_details_async(pkg_id,
 
115
            null,
 
116
            null,
80
117
            (obj, res) => {
81
118
                Results contains = client.generic_finish(res);
82
 
                details_received(get_package_from_id(pkg_id), contains.get_details_array()[0]);
 
119
                details_received(get_package_from_id(pkg_id), (Details) contains.get_details_array()[0]);
83
120
            });
84
121
        }
85
122
        
86
123
        public Package get_package_from_id (string pkg_id) {
87
124
            Results res = client.resolve(Filter.bitfield_from_string(""),
88
 
            {pkg_id},
 
125
            pkg_id,
89
126
            null,
90
127
            null);
91
 
            return res.get_package_array()[0];
92
 
        }
93
 
        
94
 
        private void fill_icon_table () {
 
128
            return (Package) res.get_package_array()[0];
95
129
        }
96
130
        
97
131
        public void get_categories () {
98
 
            /* Need a wrapper for /usr/share/app-install/desktop/applications.menu*/
99
 
            
100
 
            category_added("applications-accessories", "Accessories");
101
 
            category_added("applications-internet", "Internet");
102
 
            category_added("applications-office", "Office");
103
 
            category_added("applications-multimedia", "Multimedia");
104
 
            category_added("applications-development", "Development");
105
 
            category_added("category-show-all", "All");
 
132
            try {
 
133
                                Query cat_query = database.prepare ("SELECT * FROM 'DIRECTORIES';");
 
134
                                QueryResult result = cat_query.execute ();
 
135
                                
 
136
                                for (int record = 1; ! result.finished; record++, result.next ()) {
 
137
                                        category_added (new LscCategory (result.fetch_string (0),
 
138
                                                                  result.fetch_string (1),
 
139
                                                                  result.fetch_string (2),
 
140
                                                                  result.fetch_string (3), 0));
 
141
                                }
 
142
                        } catch (GLib.Error e) {
 
143
                                GLib.error ("Error retrieving packages: %s\n", e.message);
 
144
                        }
106
145
            
107
146
            loading_finished(LoadingType.CATEGORIES);
108
147
        }
115
154
            client = new Client();
116
155
            control = new Control();
117
156
            transaction = new Cancellable();
118
 
            fill_icon_table();
 
157
            
 
158
            try {
 
159
                    database = new Database ("/var/cache/lsc-vala.db", FileMode.READ);
 
160
                        } catch (GLib.Error e) {
 
161
                                GLib.error ("Error opening the database: %s\n", e.message);
 
162
                        }
119
163
        }
120
164
    }
121
165
}