Merge lp:~midori/midori/per-site-preferences into lp:midori

Proposed by André Stösel
Status: Work in progress
Proposed branch: lp:~midori/midori/per-site-preferences
Merge into: lp:midori
Diff against target: 332 lines (+328/-0)
1 file modified
extensions/per-site-preferences.vala (+328/-0)
To merge this branch: bzr merge lp:~midori/midori/per-site-preferences
Reviewer Review Type Date Requested Status
Midori Devs Pending
Review via email: mp+200608@code.launchpad.net
To post a comment you must log in.

Unmerged revisions

6530. By André Stösel

add old per-site-preferences extension (doesn't build)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'extensions/per-site-preferences.vala'
--- extensions/per-site-preferences.vala 1970-01-01 00:00:00 +0000
+++ extensions/per-site-preferences.vala 2014-01-06 23:18:23 +0000
@@ -0,0 +1,328 @@
1/*
2 Copyright (C) 2011 André Stösel <andre@stoesel.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 See the file COPYING for the full license text.
10*/
11
12using Gtk;
13using Soup;
14using WebKit;
15using Midori;
16
17/*
18 ToDo: it's possible that the request is reused (404 for favicon)
19*/
20
21private PSP.Manager? PSPMI; // var name "PSPManager" causes some vala error -> todo: report it
22
23namespace PSP {
24 namespace Actions {
25 }
26
27 namespace Conditions {
28 private interface NavigationPolicyDescision : GLib.Object {
29 public abstract bool match_navdes (WebKit.WebFrame p0, WebKit.NetworkRequest p1, WebKit.WebNavigationAction p2, WebKit.WebPolicyDecision p3);
30 }
31
32 private interface NewWindowsPolicyDescision : GLib.Object {
33 public abstract bool match_windes (WebKit.WebFrame p0, WebKit.NetworkRequest p1, WebKit.WebNavigationAction p2, WebKit.WebPolicyDecision p3);
34 }
35
36 private interface ResourceRequestStarting : GLib.Object {
37 public abstract bool match_resreq (WebFrame p0, WebResource p1, NetworkRequest p2, NetworkResponse? p3);
38 }
39
40
41 private abstract class Base : GLib.Object {
42 public string key; // unique! ToDo: check with some kind of metaclass or something like that | GType-Name?
43 public string name;
44 public string description;
45
46 public abstract bool is_ready ();
47 }
48
49 private class Uri : Base, NavigationPolicyDescision {
50 public string key = "uri-match";
51
52 private GLib.Regex? regex;
53
54 public override bool is_ready () {
55 if (this.regex != null) {
56 return true;
57 }
58 return false;
59 }
60
61 public void set_pattern (string pattern) {
62 this.regex = new GLib.Regex (pattern, GLib.RegexCompileFlags.CASELESS);
63 }
64
65 public bool match_navdes (WebKit.WebFrame p0, WebKit.NetworkRequest p1, WebKit.WebNavigationAction p2, WebKit.WebPolicyDecision p3) {
66 return this.regex.match (p1.uri);
67 }
68 }
69 }
70
71 enum RuleOperator {
72 OR,
73 AND,
74 AND_NOT
75 }
76
77 private errordomain RuleError {
78 TYPE_ALREADY_DEFINED,
79 UNKNOWN_CONDITION_TYPE,
80 ILLEGAL_CONDITION_TYPE,
81 UNREADY_CONDITION
82 }
83
84 private class Rule : GLib.Object {
85 private GLib.Type? condition_type;
86 private RuleOperator operator;
87 private GLib.PtrArray actions;
88 private GLib.PtrArray conditions;
89
90 public void set_condition_type (GLib.Type type) throws RuleError {
91 if (this.condition_type == null) {
92 if (
93 type == typeof (Conditions.NavigationPolicyDescision) ||
94 type == typeof (Conditions.NewWindowsPolicyDescision) ||
95 type == typeof (Conditions.ResourceRequestStarting)
96 ) {
97 this.condition_type = type;
98 } else {
99 throw new RuleError.UNKNOWN_CONDITION_TYPE ("Can't set unknown condition type!");
100 }
101 } else {
102 throw new RuleError.TYPE_ALREADY_DEFINED ("Condition type is already defined and can't be changed!");
103 }
104 }
105
106 public void add_condition (PSP.Conditions.Base condition) throws RuleError {
107 if (condition.is_ready () == true) {
108 if (Type.from_instance (condition).is_a (this.condition_type)) {
109 condition.ref ();
110 this.conditions.add (condition);
111 } else {
112 throw new RuleError.ILLEGAL_CONDITION_TYPE ("One rule can only contain one type of conditions!");
113 }
114 } else {
115 throw new RuleError.UNREADY_CONDITION ("The condition must be fully initialized before it can be added to a rule!");
116 }
117 }
118
119 public void connect (Midori.View view) throws RuleError {
120 unowned WebKit.WebView web_view = view.web_view;
121 if (this.condition_type == typeof(Conditions.NavigationPolicyDescision)) {
122 web_view.navigation_policy_decision_requested.connect (this.navigation_policy_decision_requested);
123 } else {
124 throw new RuleError.UNKNOWN_CONDITION_TYPE ("Can't connect unknown condition type!");
125 }
126 }
127
128 public void disconnect (Midori.View view) throws RuleError {
129 unowned WebKit.WebView web_view = view.web_view;
130 if (this.condition_type == typeof(Conditions.NavigationPolicyDescision)) {
131 web_view.navigation_policy_decision_requested.disconnect (this.navigation_policy_decision_requested);
132 } else {
133 throw new RuleError.UNKNOWN_CONDITION_TYPE ("Can't disconnect unknown condition type!");
134 }
135 }
136
137 /* if return is true: decision was made -> break the loop */
138 private bool evaluate_match (bool condition, int index, ref bool rule) {
139 if (this.operator == RuleOperator.AND_NOT) {
140 if (condition == true && index == 0) {
141 rule = true;
142 } else if(condition == true) {
143 rule = false;
144 return true;
145 }
146 } else if (this.operator == RuleOperator.AND) {
147 if (condition) {
148 rule = true;
149 } else {
150 rule = false;
151 return true;
152 }
153 } else if (condition == true) { // RuleOperator is OR
154 rule = true;
155 return true;
156 }
157 return false;
158 }
159
160 public bool navigation_policy_decision_requested (WebKit.WebFrame p0, WebKit.NetworkRequest p1, WebKit.WebNavigationAction p2, WebKit.WebPolicyDecision p3) {
161 bool match_rule = false;
162 for (int i = 0; i < this.conditions.len; i++) {
163 Conditions.NavigationPolicyDescision condition = this.conditions.index (i) as Conditions.NavigationPolicyDescision;
164 bool match_condition = condition.match_navdes (p0, p1, p2, p3);
165 if (this.evaluate_match (match_condition, i, ref match_rule))
166 break;
167 }
168
169 if (match_rule) {
170 stdout.printf("Rule matches!\n");
171 // do stuff
172 }
173
174 return false;
175 }
176
177 construct {
178 this.conditions = new GLib.PtrArray ();
179 }
180 }
181
182 private class Manager : GLib.Object {
183 private Midori.App app;
184 private GLib.PtrArray rules;
185 public void add_rules (GLib.PtrArray rules) {
186 foreach (var browser in this.app.get_browsers ()) {
187 foreach (var tab in browser.get_tabs ()) {
188 this.tab_add_rules (tab, rules);
189 }
190 }
191
192 for (int i = 0; i < rules.len; i++) {
193 this.rules.add (rules.index (i));
194 }
195 }
196
197 public void remove_rules (GLib.PtrArray rules) {
198 foreach (var browser in this.app.get_browsers ()) {
199 foreach (var tab in browser.get_tabs ()) {
200 this.tab_remove_rules (tab, rules);
201 }
202 }
203
204 for (int i = 0; i < rules.len; i++) {
205 this.rules.remove (rules.index (i));
206 }
207 }
208
209 private void tab_add_rules (Midori.View view, GLib.PtrArray rules) {
210 for (int i = 0; i < rules.len; i++) {
211 PSP.Rule rule = rules.index (i) as PSP.Rule;
212 rule.connect (view);
213 }
214 }
215
216 private void tab_remove_rules (Midori.View view, GLib.PtrArray rules) {
217 for (int i = 0; i < rules.len; i++) {
218 PSP.Rule rule = rules.index (i) as PSP.Rule;
219 rule.disconnect (view);
220 }
221 }
222
223 private void tab_added (Midori.Browser browser, Midori.View view) {
224 this.tab_add_rules (view, this.rules);
225 }
226
227 private void tab_removed (Midori.Browser browser, Midori.View view) {
228 this.tab_remove_rules (view, this.rules);
229 }
230
231 private void browser_added (Midori.Browser browser) {
232 foreach (var tab in browser.get_tabs ()) {
233 this.tab_added (browser, tab);
234 }
235 browser.add_tab.connect (this.tab_added);
236 browser.remove_tab.connect (tab_removed);
237 }
238
239 private void browser_removed (Midori.Browser browser) {
240 foreach (var tab in browser.get_tabs ()) {
241 tab_removed (browser, tab);
242 }
243 browser.add_tab.disconnect (tab_added);
244 browser.remove_tab.disconnect (tab_removed);
245 }
246
247 public void activated (Midori.App app) {
248 this.app = app;
249 foreach (var browser in app.get_browsers ()) {
250 browser_added (browser);
251 }
252 app.add_browser.connect (browser_added);
253 }
254
255 public void deactivated () {
256 foreach (var browser in this.app.get_browsers ()) {
257 browser_removed (browser);
258 }
259 this.app.add_browser.disconnect (browser_added);
260 }
261
262 construct {
263 this.rules = new GLib.PtrArray ();
264 }
265 }
266}
267
268namespace PSPExtensions {
269 private class Base : Midori.Extension {
270 private GLib.PtrArray rules;
271
272 public void activated (Midori.App app) {
273 if (PSPMI == null) {
274 PSPMI = new PSP.Manager ();
275 PSPMI.activated (app);
276 } else {
277 PSPMI.ref ();
278 }
279
280 PSPMI.add_rules (this.rules);
281 }
282
283 public void deactivated () {
284 uint rcount = PSPMI.ref_count;
285 PSPMI.remove_rules (this.rules);
286 if (rcount == 1) {
287 PSPMI.deactivated ();
288 PSPMI = null;
289 } else {
290 PSPMI.unref ();
291 }
292 }
293
294 public void add_rule (PSP.Rule rule) {
295 rule.ref ();
296 this.rules.add (rule);
297 }
298
299 construct {
300 this.rules = new GLib.PtrArray ();
301 }
302 }
303
304 private class Test : Base {
305 internal Test () {
306 GLib.Object (name: _("Just a test"),
307 description: _("...."),
308 version: "0.1",
309 authors: "André Stösel <andre@stoesel.de>");
310
311 activate.connect (this.activated);
312 deactivate.connect (this.deactivated);
313
314 PSP.Rule rule = new PSP.Rule ();
315 rule.set_condition_type (typeof (PSP.Conditions.NavigationPolicyDescision));
316 var c1 = new PSP.Conditions.Uri ();
317 c1.set_pattern ("pyit\\.de");
318 rule.add_condition (c1);
319
320 this.add_rule (rule);
321 }
322 }
323}
324
325public Midori.Extension extension_init () {
326 return new PSPExtensions.Test ();
327}
328

Subscribers

People subscribed via source and target branches

to all changes: