summaryrefslogtreecommitdiff
path: root/extensions/adblock/widgets.vala
blob: 39c391252bd22b8173b84b00e52130b4aacdddb8 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 Copyright (C) 2009-2014 Christian Dywan <christian@twotoasts.de>
 Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

namespace Adblock {


    public class StatusIcon : Midori.ContextAction  {
        Config config;
        SubscriptionManager manager;
        public State state;
        public bool debug_element_toggled;

        public StatusIcon (Adblock.Config config, SubscriptionManager manager) {
            GLib.Object (name: "AdblockStatusMenu");

            this.config = config;
            this.manager = manager;
            this.debug_element_toggled = false;

            var item = new Midori.ContextAction ("Preferences",
                _("Preferences"), null, Gtk.STOCK_PREFERENCES);
            item.activate.connect (() => {
                manager.add_subscription (null);
            });
            add (item);

            add (null);

            var checkitem = new Gtk.ToggleAction ("Disable", _("Disable"), null, null);
            checkitem.set_active (!config.enabled);
            checkitem.toggled.connect (() => {
                config.enabled = !checkitem.active;
                set_state (config.enabled ? Adblock.State.ENABLED : Adblock.State.DISABLED);
            });
            add (checkitem);

            var hideritem = new Gtk.ToggleAction ("HiddenElements",
                _("Display hidden elements"), null, null);
            hideritem.set_active (debug_element_toggled);
            hideritem.toggled.connect (() => {
                this.debug_element_toggled = hideritem.active;
            });
            add (hideritem);
            set_status (config.enabled ? "enabled" : "disabled");
        }

        void set_status (string status) {
            gicon = new GLib.FileIcon (File.new_for_path (
                Midori.Paths.get_res_filename ("adblock/%s.png".printf (status))));
        }

        public void set_state (Adblock.State state) {
            this.state = state;

            if (this.state == State.BLOCKED) {
                set_status ("blocked");
                tooltip = _("Blocking");
            } else if (this.state == State.ENABLED) {
                set_status ("enabled");
                tooltip = _("Enabled");
            } else if (this.state == State.DISABLED) {
                set_status ("disabled");
                tooltip = _("Disabled");
            } else
                assert_not_reached ();
        }
    }

    public class SubscriptionManager {
        Gtk.TreeView treeview;
        Gtk.ListStore liststore;
        Adblock.Config config;
        public Gtk.Label description_label;
        string description;

        public SubscriptionManager (Config config) {
            this.config = config;
            this.liststore = new Gtk.ListStore (1, typeof (Subscription));
            this.description_label = new Gtk.Label (null);
            this.description = _("Type the address of a preconfigured filter list in the text entry and hit Enter.\n");
            this.description += _("You can find more lists by visiting following sites:\n %s, %s\n".printf (
                "<a href=\"http://adblockplus.org/en/subscriptions\">adblockplus.org/en/subscriptions</a>",
                "<a href=\"http://easylist.adblockplus.org/\">easylist.adblockplus.org</a>"
            ));
        }

        public void add_subscription (string? uri) {
            var dialog = new Gtk.Dialog.with_buttons (_("Configure Advertisement filters"),
                null,
#if !HAVE_GTK3
                Gtk.DialogFlags.NO_SEPARATOR |
#endif
                Gtk.DialogFlags.DESTROY_WITH_PARENT,
                Gtk.STOCK_HELP, Gtk.ResponseType.HELP,
                Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE);
#if HAVE_GTK3
            dialog.get_widget_for_response (Gtk.ResponseType.HELP).get_style_context ().add_class ("help_button");
#endif
            dialog.set_icon_name (Gtk.STOCK_PROPERTIES);
            dialog.set_response_sensitive (Gtk.ResponseType.HELP, false);

            var hbox = new Gtk.HBox (false, 0);
            (dialog.get_content_area () as Gtk.Box).pack_start (hbox, true, true, 12);
            var vbox = new Gtk.VBox (false, 0);
            hbox.pack_start (vbox, true, true, 4);
            this.description_label.set_markup (this.description);
            this.description_label.set_line_wrap (true);
            vbox.pack_start (this.description_label, false, false, 4);

            var entry = new Gtk.Entry ();
            if (uri != null)
                entry.set_text (uri);
            vbox.pack_start (entry, false, false, 4);

            liststore = new Gtk.ListStore (1, typeof (Subscription));
            treeview = new Gtk.TreeView.with_model (liststore);
            treeview.set_headers_visible (false);
            var column = new Gtk.TreeViewColumn ();
            var renderer_toggle = new Gtk.CellRendererToggle ();
            column.pack_start (renderer_toggle, false);
            column.set_cell_data_func (renderer_toggle, (column, renderer, model, iter) => {
                Subscription sub;
                liststore.get (iter, 0, out sub);
                renderer.set ("active", sub.active,
                              "sensitive", sub.mutable);
            });
            renderer_toggle.toggled.connect ((path) => {
                Gtk.TreeIter iter;
                if (liststore.get_iter_from_string (out iter, path)) {
                    Subscription sub;
                    liststore.get (iter, 0, out sub);
                    sub.active = !sub.active;
                }
            });
            treeview.append_column (column);

            column = new Gtk.TreeViewColumn ();
            var renderer_text = new Gtk.CellRendererText ();
            column.pack_start (renderer_text, false);
            renderer_text.set ("editable", true);
            // TODO: renderer_text.edited.connect
            column.set_cell_data_func (renderer_text, (column, renderer, model, iter) => {
                Subscription sub;
                liststore.get (iter, 0, out sub);
                string status = "";
                foreach (unowned Feature feature in sub) {
                    var updater = feature as Adblock.Updater;
                    if (updater != null) {
                        if (updater.last_updated != null)
                            status = updater.last_updated.format (_("Last update: %x %X"));
                    }
                }
                if (!sub.valid)
                    status = _("File incomplete - broken download?");
                renderer.set ("markup", (Markup.printf_escaped ("<b>%s</b>\n%s",
                    sub.title ?? sub.uri, status)));
            });
            treeview.append_column (column);

            column = new Gtk.TreeViewColumn ();
            Gtk.CellRendererPixbuf renderer_button = new Gtk.CellRendererPixbuf ();
            column.pack_start (renderer_button, false);
            column.set_cell_data_func (renderer_button, on_render_button);
            treeview.append_column (column);

            var scrolled = new Gtk.ScrolledWindow (null, null);
            scrolled.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
            scrolled.add (treeview);
            vbox.pack_start (scrolled);
            int height;
            treeview.create_pango_layout ("a\nb").get_pixel_size (null, out height);
            scrolled.set_size_request (-1, height * 5);

            foreach (unowned Subscription sub in config)
                liststore.insert_with_values (null, 0, 0, sub);
            treeview.button_release_event.connect (button_released);

            entry.activate.connect (() => {
                string? parsed_uri = Adblock.parse_subscription_uri (entry.text);
                if (parsed_uri != null) {
                    var sub = new Subscription (parsed_uri);
                    if (config.add (sub)) {
                        liststore.insert_with_values (null, 0, 0, sub);
                        try {
                            sub.parse ();
                        } catch (GLib.Error error) {
                            warning ("Error parsing %s: %s", sub.uri, error.message);
                        }
                    }
                }
                entry.text = "";
            });

            dialog.get_content_area ().show_all ();

            dialog.response.connect ((response)=>{ dialog.destroy (); });
            dialog.show ();
        }

        void on_render_button (Gtk.CellLayout column, Gtk.CellRenderer renderer,
            Gtk.TreeModel model, Gtk.TreeIter iter) {

            Subscription sub;
            liststore.get (iter, 0, out sub);

            renderer.set ("stock-id", sub.mutable ? Gtk.STOCK_DELETE : null,
                          "stock-size", Gtk.IconSize.MENU);
        }

        public bool button_released (Gdk.EventButton event) {
            Gtk.TreePath? path;
            Gtk.TreeViewColumn column;
            if (treeview.get_path_at_pos ((int)event.x, (int)event.y, out path, out column, null, null)) {
                if (path != null) {
                    if (column == treeview.get_column (2)) {
                        Gtk.TreeIter iter;
                        if (liststore.get_iter (out iter, path)) {
                            Subscription sub;
                            liststore.get (iter, 0, out sub);
                            if (sub.mutable) {
                                config.remove (sub);
                                liststore.remove (iter);
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    }

    class CustomRulesEditor {
        Gtk.Dialog dialog;
        Subscription custom;
        public string? rule { get; set; }

        public CustomRulesEditor (Subscription custom) {
            this.custom = custom;
        }

        public void set_uri (string uri) {
            this.rule = uri;
        }

        public void show () {
             this.dialog = new Gtk.Dialog.with_buttons (_("Edit rule"),
                null,
#if !HAVE_GTK3
                Gtk.DialogFlags.NO_SEPARATOR |
#endif
                Gtk.DialogFlags.DESTROY_WITH_PARENT,
                Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                Gtk.STOCK_ADD, Gtk.ResponseType.ACCEPT);
            dialog.set_icon_name (Gtk.STOCK_ADD);
            dialog.resizable = false;

            var hbox = new Gtk.HBox (false, 8);
            var sizegroup = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
            hbox.border_width = 5;
            var label = new Gtk.Label.with_mnemonic (_("_Rule:"));
            sizegroup.add_widget (label);
            hbox.pack_start (label, false, false, 0);
            (dialog.get_content_area () as Gtk.Box).pack_start (hbox, false, true, 0);

            var entry = new Gtk.Entry ();
            sizegroup.add_widget (entry);
            entry.activates_default = true;
            entry.set_text (this.rule);
            hbox.pack_start (entry, true, true, 0);

            dialog.get_content_area ().show_all ();

            dialog.set_default_response (Gtk.ResponseType.ACCEPT);
            if (dialog.run () != Gtk.ResponseType.ACCEPT)
                return;

            this.rule = entry.get_text ();
            this.dialog.destroy ();
            custom.add_rule (this.rule);
        }
    }

}