~artem-anufrij/webby-browser/edit-functionality

« back to all changes in this revision

Viewing changes to src/DesktopFile.vala

  • Committer: Erasmo Marín
  • Date: 2015-02-24 05:12:05 UTC
  • Revision ID: erasmo.marin@gmail.com-20150224051205-fhz7ovpzwlbi9k79
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
public class DesktopFile : GLib.Object {
 
2
 
 
3
    private string template = """
 
4
                                [Desktop Entry]
 
5
                                Version=1.0
 
6
                                Name=Webby
 
7
                                GenericName=Web app
 
8
                                Comment=Webby web app
 
9
                                Exec=webby
 
10
                                Keywords=webby;webapp;internet;
 
11
                                Icon=application-default-icon
 
12
                                Terminal=false
 
13
                                Type=Application
 
14
                                Categories=Network;
 
15
                                X-GNOME-FullName=Webby
 
16
                                StartupWMClass=Webby""";
 
17
 
 
18
 
 
19
    private GLib.KeyFile file;
 
20
    public string uri;
 
21
 
 
22
    public DesktopFile (string name, string url, string icon) {
 
23
        file = new GLib.KeyFile();
 
24
        file.load_from_data (template, -1, GLib.KeyFileFlags.NONE);
 
25
        //TODO: Category
 
26
        file.set_string ("Desktop Entry", "Name", name);
 
27
        file.set_string ("Desktop Entry", "GenericName", name);
 
28
        file.set_string ("Desktop Entry", "X-GNOME-FullName", name);
 
29
        file.set_string ("Desktop Entry", "Exec", "webby " + url);
 
30
        file.set_string ("Desktop Entry", "Icon", icon);
 
31
        file.set_string ("Desktop Entry", "StartupWMClass", url);
 
32
    }
 
33
 
 
34
    public bool save_to_file () {
 
35
        string filename = GLib.Environment.get_user_data_dir () + "/applications/" +file.get_string("Desktop Entry", "Name") + "-webby.desktop";
 
36
        print("Desktop file created: " + filename);
 
37
        file.save_to_file (filename);
 
38
        return false;
 
39
    }
 
40
 
 
41
    public static Gee.HashMap<string, GLib.AppInfo> get_webby_applications() {
 
42
 
 
43
        var list = new Gee.HashMap<string, GLib.AppInfo>();
 
44
 
 
45
        foreach (GLib.AppInfo app in GLib.AppInfo.get_all()) {
 
46
 
 
47
            var desktop_app = new GLib.DesktopAppInfo(app.get_id ());
 
48
 
 
49
            //FIXME: This is not working, vala problem?
 
50
            //var keywords = desktop_app.get_keywords ();
 
51
 
 
52
            string keywords = desktop_app.get_string ("Keywords");
 
53
 
 
54
            if (keywords != null && keywords.contains ("webby")) {
 
55
                list.set(app.get_name(), app);
 
56
                print(app.get_name());
 
57
            }
 
58
        }
 
59
        return list;
 
60
    }
 
61
 
 
62
    public static Gee.HashMap<string, GLib.AppInfo> get_applications() {
 
63
 
 
64
        var list = new Gee.HashMap<string, GLib.AppInfo>();
 
65
 
 
66
        foreach (GLib.AppInfo app in GLib.AppInfo.get_all()) {
 
67
            list.set(app.get_name(), app);
 
68
        }
 
69
 
 
70
        return list;
 
71
    }
 
72
}