6
class AppInstallPackage {
7
public AppInstallDirectory directory { get; private set; }
8
public string name { get; private set; }
9
public string icon { get; private set; }
10
public string summary { get; private set; }
11
public string id { get; private set; }
13
public AppInstallPackage (AppInstallDirectory directory, string name, string icon, string summary, string id) {
14
this.directory = directory;
17
this.summary = summary;
22
class AppInstallDirectory {
23
public string name { get; private set; }
24
public string directory { get; private set; }
26
public AppInstallDirectory (string name, string directory) {
28
this.directory = directory;
34
private AppInstallDirectory tmp_dir;
35
private Transaction trans;
37
private string desktop_dir;
38
private Gee.HashMap<string, AppInstallPackage> packages;
40
public void add_pkg (string id, Transaction trs) {
41
AppInstallPackage app_pkg = packages[id];
42
if (app_pkg == null) { return; }
43
GLib.debug ("Inserting %s in %s\n", id, app_pkg.directory.name);
44
trs.execute_insert("INSERT INTO '%s' VALUES (:id, :name, :description, :icon);".printf(app_pkg.directory.name),
45
":id", typeof (string), app_pkg.id,
46
":name", typeof (string), app_pkg.name,
47
":description", typeof (string), app_pkg.summary,
48
":icon", typeof (string), app_pkg.icon);
51
private bool get_if_included (MenuDir dir, string[] categories) {
52
foreach (string in_dir in dir.excluded) {
53
if (in_dir in categories) {
57
foreach (string in_dir in dir.included) {
58
if (in_dir in categories) {
65
private string get_safe_key (string val) {
67
return keys.get_string ("Desktop Entry", val);
68
} catch (GLib.Error e) {
69
GLib.debug ("Error retrieving key %s: %s\n", val, e.message);
74
private void get_apps_from_dir (MenuDir dir) {
76
tmp_dir = new AppInstallDirectory (dir.name, dir.directory);
78
while ((name = root.read_name ()) != null) {
79
if (! name.has_suffix (".desktop")) { continue; }
81
keys.load_from_file (desktop_dir+name, 0);
82
if (keys.has_key("Desktop Entry", "Categories") == false) { continue; }
84
string[] categories = keys.get_string_list ("Desktop Entry", "Categories");
85
if (get_if_included (dir, categories)) {
86
AppInstallPackage pkg = new AppInstallPackage (tmp_dir, get_safe_key ("Name"), get_safe_key ("Icon"), get_safe_key ("Comment"), get_safe_key ("X-AppInstall-Package"));
87
packages.set (pkg.id, pkg);
90
GLib.debug ("%s is not a valid .desktop file", name);
95
public DbBuilder (Database db) {
96
packages = new Gee.HashMap<string, AppInstallPackage> ();
98
desktop_dir = "/usr/share/app-install/desktop/";
99
root = Dir.open (desktop_dir, 0);
100
keys = new KeyFile ();
102
MenuParser parser = new MenuParser ("/usr/share/app-install/desktop/applications.menu");
104
trans = db.begin_transaction ();
106
stdout.printf ("Collecting app-install datas...\n");
108
foreach (unowned MenuDir item in parser.parse ()) {
109
if (item.level == 1) { // We just want the Applications childs
110
trans.execute ("CREATE TABLE '%s' (id, name, summary, icon);".printf (item.name));
111
get_apps_from_dir (item);
117
} catch (SQLHeavy.Error e) {
118
GLib.error ("Error: %s\n", e.message);
129
void pkg_received (Package pkg) {
130
ids += pkg.get_name ();
133
// FIXME: Awful OOP code
134
// FIXME: doesn't allow same packages in multiple categories
138
if (Environment.get_user_name () != "root") {
139
GLib.error ("This program needs to be run as root!\n");
142
FileUtils.remove ("/var/cache/lsc-vala.db");
143
db = new Database ("/var/cache/lsc-vala.db");
145
dbbuilder = new DbBuilder (db);
147
client = new Client ();
148
trans = db.begin_transaction ();
150
stdout.printf ("Querying PackageKit...\n");
154
field = Filter.bitfield_from_string ("arch");
155
Results result = client.get_packages(field, null, null);
156
result.get_package_array().foreach ((Func) pkg_received);
157
} catch (GLib.Error e) {
158
GLib.error ("Error: %s\n", e.message);
161
stdout.printf ("Building database...\n");
163
foreach (string id in ids) {
164
dbbuilder.add_pkg (id, trans);
169
} catch (SQLHeavy.Error e) {
170
GLib.error ("Error: %s\n", e.message);
173
stdout.printf ("Done, no error reported\n");