~gotwig/gazette/global-service-states

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
using Gtk;
using Pantheon;

public class GazetteSettings : Pantheon.Switchboard.Plug {

    Gee.Collection<Gazette.Plugin.Base> services;
    Gazette.Service current;
    Settings settings;
    string [] enabled_services;
    public GazetteSettings () {
        settings = new Settings ("org.pantheon.gazette");
        enabled_services = settings.get_strv("enabled-services");
        settings.bind ("enabled-services", this, "enabled_services", SettingsBindFlags.DEFAULT);
        var manager = new PluginManager (settings, "enabled-services", Constants.PLUGINDIR);
        manager.load_plugins ();
        services = manager.get_plugins ();
        
        var box  = new Gtk.Box   (Gtk.Orientation.HORIZONTAL, 0);
        var list = new Gtk.Box   (Gtk.Orientation.VERTICAL, 0);
        var view = new Gtk.Box   (Gtk.Orientation.VERTICAL, 0);
        var scrl = new Gtk.ScrolledWindow (null, null);
        
        scrl.add_with_viewport (list);
        scrl.width_request = 300;
        list.margin = 12;
        
        box.pack_start (scrl, false);
        box.pack_start (view);
        var cur_box   = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 20);
        var cur_label = new Gtk.Label ( _("Setup your\nnews services") );
        var cur_image = new Gtk.Image.from_icon_name (
            "gazette", Gtk.IconSize.DIALOG);
        cur_image.pixel_size = 64;
       
        var attrs = new Pango.AttrList ();
        attrs.insert (new Pango.AttrFontDesc (
            Pango.FontDescription.from_string ("bold 25")));
        cur_label.set_attributes (attrs);
        
        cur_label.halign = Gtk.Align.START;
        cur_box.pack_start (cur_image, false);
        cur_box.pack_start (cur_label);
        
        view.pack_start (cur_box, false);
        view.margin = 30;

        foreach (Gazette.Plugin.Base p in services) {
            var list_item = get_item_for(p);
            list.pack_start (list_item, false);
            if (p is Gazette.Plugin.Configurable)
                view.add(p.create_config_widget ());
        }
        this.add (box);
    }

    private string[] remove_value (string val, string[] values) {
        for (var i = 0; i < values.length; i++) {
            if (values[i] == val) {
                string[] aux = values[0:i-1];
                foreach (var value in values[i:values.length])
                    aux += value;
                return aux;
            }
        }
        return values;
    }
    
    private Gtk.EventBox get_item_for (Gazette.Plugin.Base p) {
        var item = new Gtk.EventBox ();
        var item_c = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
        item.add(item_c);
        var label = new Gtk.Label (p.name);
        var toggle = new Gtk.Switch ();
        toggle.valign = Gtk.Align.CENTER;
        toggle.set_active(p.id in enabled_services);
        toggle.notify["active"].connect(() => {
            if (toggle.active)
                enabled_services += p.id;
            else
                enabled_services = remove_value(p.id, enabled_services);
        });
        item_c.pack_start (label, false);
        item_c.pack_start (toggle, false);
        return item;
    }
}

public static int main(string[] args){

    Gtk.init(ref args);
    
    var SwitchBoard_Plug = new GazetteSettings();
    SwitchBoard_Plug.register(_("Column News"));
    SwitchBoard_Plug.show_all();
    Gtk.main();
	return 0;
}