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