Do

Merge lp:~cszikszoy/do/proxy into lp:do

Proposed by Chris S.
Status: Work in progress
Proposed branch: lp:~cszikszoy/do/proxy
Merge into: lp:do
Diff against target: 421 lines (+210/-18)
10 files modified
Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs (+29/-4)
Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs (+108/-4)
Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs (+15/-5)
Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs (+7/-0)
Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs (+13/-1)
Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs (+16/-3)
Do.Platform/src/Do.Platform/INetworkService.cs (+2/-0)
Do.Platform/src/Do.Platform/IPreferences.cs (+3/-0)
Do.Platform/src/Do.Platform/IPreferencesService.cs (+3/-0)
Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs (+14/-1)
To merge this branch: bzr merge lp:~cszikszoy/do/proxy
Reviewer Review Type Date Requested Status
Chris Halse Rogers Needs Information
Review via email: mp+22351@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Chris Halse Rogers (raof) wrote :

I'd prefer it if there weren't whitespace changes mixed in here, but I know that can be difficult to do.

If I understand this code correctly, you've extended the Preferences service to also handle system-wide preferences. I'm not sure how good an idea this is - could we instead have something like a SystemConfiguration service that would deal with this sort of system-wide configuration? An advantage I see from that is that the proxy configuration work could get factored out into a platform-neutral place - the proxy setting code is platform independent.

That would also remove the somewhat smelly AddNotify/RemoveNotify addition to IPreferencesService.

review: Needs Fixing
Revision history for this message
Chris Halse Rogers (raof) wrote :

Actually, make that “need info”

review: Needs Information

Unmerged revisions

1320. By Chris S.

use proxy settings from GConf on linux

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs'
2--- Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs 2009-08-31 01:58:13 +0000
3+++ Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs 2010-03-29 03:16:18 +0000
4@@ -43,8 +43,13 @@
5
6 void HandleGConfChanged (object sender, GConf.NotifyEventArgs args)
7 {
8+ // if this key falls under the RootPath, strip away that part of the key,
9+ // otherwise we leave the full path to the key.
10+ string key = args.Key;
11+ if (key.StartsWith (RootPath))
12+ key = args.Key.Substring (RootPath.Length + 1);
13 if (PreferencesChanged != null)
14- PreferencesChanged (this, new PreferencesChangedEventArgs (args.Key.Substring(RootPath.Length + 1), args.Value));
15+ PreferencesChanged (this, new PreferencesChangedEventArgs (key, args.Value));
16 }
17
18 string AbsolutePathForKey (string key)
19@@ -74,9 +79,9 @@
20 public bool TryGet<T> (string key, out T val)
21 {
22 bool success = true;
23- val = default (T);
24+ val = default(T);
25 try {
26- val = (T) client.Get (AbsolutePathForKey (key));
27+ val = (T)client.Get (AbsolutePathForKey (key));
28 } catch (GConf.NoSuchKeyException) {
29 // We don't need to log this, because many keys that do not
30 // exist are asked for.
31@@ -88,7 +93,27 @@
32 }
33 return success;
34 }
35+
36+ public void AddNotify (string path)
37+ {
38+ try {
39+ client.AddNotify (path, HandleGConfChanged);
40+ } catch (Exception e) {
41+ Log.Error ("Error removing notification handler, {0}", e.Message);
42+ Log.Debug (e.StackTrace);
43+ }
44+ }
45+
46+ public void RemoveNotify (string path)
47+ {
48+ try {
49+ client.RemoveNotify (path, HandleGConfChanged);
50+ } catch (Exception e) {
51+ Log.Error ("Error removing notification handler, {0}", e.Message);
52+ Log.Debug (e.StackTrace);
53+ }
54+ }
55
56 #endregion
57 }
58-}
59+}
60\ No newline at end of file
61
62=== modified file 'Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs'
63--- Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 2009-10-21 07:06:25 +0000
64+++ Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 2010-03-29 03:16:18 +0000
65@@ -18,6 +18,7 @@
66 //
67
68 using System;
69+using System.Net;
70 using System.Reflection;
71
72 using NDesk.DBus;
73@@ -28,9 +29,65 @@
74
75 namespace Do.Platform.Linux
76 {
77-
78- public class NetworkService : INetworkService
79+
80+ public class NetworkService : INetworkService, IInitializedService
81 {
82+ class NetworkPreferencesWrapper
83+ {
84+ const string PROXY = "/system/http_proxy";
85+ const string PROXY_USE_PROXY = PROXY + "/" + "use_http_proxy";
86+ const string PROXY_HOST = PROXY + "/" + "host";
87+ const string PROXY_PORT = PROXY + "/" + "port";
88+ const string PROXY_USER = PROXY + "/" + "authentication_user";
89+ const string PROXY_PASSWORD = PROXY + "/" + "authentication_password";
90+ const string PROXY_BYPASS_LIST = PROXY + "/" + "ignore_hosts";
91+
92+ IPreferences Preferences { get; set; }
93+
94+ public event EventHandler ProxyChanged;
95+
96+ public bool UseProxy {
97+ get { return Preferences.Get<bool> (PROXY_USE_PROXY, false); }
98+ }
99+ public string ProxyHost {
100+ get { return Preferences.Get<string> (PROXY_HOST, ""); }
101+ }
102+ public int ProxyPort {
103+ get { return Preferences.Get<int> (PROXY_PORT, 0); }
104+ }
105+ public string ProxyUser {
106+ get { return Preferences.Get<string> (PROXY_USER, ""); }
107+ }
108+ public string ProxyPassword {
109+ get { return Preferences.Get<string> (PROXY_PASSWORD, ""); }
110+ }
111+ public string[] ProxyBypassList {
112+ get { return Preferences.Get<string[]> (PROXY_BYPASS_LIST, new[] { "" }); }
113+ }
114+
115+ public NetworkPreferencesWrapper ()
116+ {
117+ Preferences = Services.Preferences.Get<NetworkPreferencesWrapper> ();
118+ Preferences.AddNotify (PROXY);
119+ Preferences.PreferencesChanged += OnPrefsChanged;
120+ }
121+
122+ void OnPrefsChanged (object sender, PreferencesChangedEventArgs e)
123+ {
124+ switch (e.Key) {
125+ case PROXY_USE_PROXY:
126+ case PROXY_HOST:
127+ case PROXY_PORT:
128+ case PROXY_USER:
129+ case PROXY_PASSWORD:
130+ case PROXY_BYPASS_LIST:
131+ if (ProxyChanged != null)
132+ ProxyChanged (this, EventArgs.Empty);
133+ break;
134+ }
135+ }
136+ }
137+
138 const string NetworkManagerName = "org.freedesktop.NetworkManager";
139 const string NetworkManagerPath = "/org/freedesktop/NetworkManager";
140
141@@ -43,6 +100,7 @@
142 delegate void StateChangedHandler (uint state);
143
144 INetworkManager network;
145+ static NetworkPreferencesWrapper prefs;
146
147 public event EventHandler<NetworkStateChangedEventArgs> StateChanged;
148
149@@ -62,6 +120,15 @@
150 Log<NetworkService>.Debug (e.StackTrace);
151 }
152 }
153+
154+ public void Initialize ()
155+ {
156+ prefs = new NetworkPreferencesWrapper ();
157+ UpdateProxySettings ();
158+ prefs.ProxyChanged += delegate {
159+ UpdateProxySettings ();
160+ };
161+ }
162
163 void OnStateChanged (uint state)
164 {
165@@ -81,18 +148,55 @@
166 }
167
168 NetworkState State {
169- get {
170+ get {
171 try {
172- return (NetworkState) Enum.ToObject (typeof (NetworkState), network.Get (NetworkManagerName, "State"));
173+ return (NetworkState)Enum.ToObject (typeof(NetworkState), network.Get (NetworkManagerName, "State"));
174 } catch (Exception) {
175 return NetworkState.Unknown;
176 }
177 }
178 }
179
180+ void UpdateProxySettings ()
181+ {
182+ WebProxy proxy;
183+
184+ if (!prefs.UseProxy) {
185+ WebRequest.DefaultWebProxy = null;
186+ return;
187+ }
188+
189+ try {
190+ string proxyUri = string.Format ("http://{0}:{1}", prefs.ProxyHost, prefs.ProxyPort);
191+
192+ proxy = new WebProxy (proxyUri);
193+ string[] bypassList = prefs.ProxyBypassList;
194+ if (bypassList != null) {
195+ foreach (string host in bypassList) {
196+ if (host.Contains ("*.local")) {
197+ proxy.BypassProxyOnLocal = true;
198+ continue;
199+ }
200+ proxy.BypassArrayList.Add (string.Format ("http://{0}", host));
201+ }
202+ }
203+ proxy.Credentials = new NetworkCredential (prefs.ProxyUser, prefs.ProxyPassword);
204+ } catch (Exception e) {
205+ Log.Error ("Error creating web proxy, {0}", e.Message);
206+ Log.Debug (e.StackTrace);
207+ proxy = null;
208+ }
209+
210+ WebRequest.DefaultWebProxy = proxy;
211+ }
212+
213 #region INetworkService
214
215 public bool IsConnected { get; private set; }
216+
217+ public string UserAgent {
218+ get { return @"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100308 Ubuntu/10.04 (lucid) Firefox/3.6"; }
219+ }
220
221 #endregion
222 }
223
224=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs'
225--- Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs 2009-08-31 01:58:13 +0000
226+++ Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs 2010-03-29 03:16:18 +0000
227@@ -57,15 +57,25 @@
228 object val_object;
229 bool success = Store.TryGetValue (key, out val_object);
230
231- val = default (T);
232+ val = default(T);
233 if (success)
234- val = (T) val_object;
235+ val = (T)val_object;
236
237 return success;
238 }
239+
240+ public void AddNotify (string path)
241+ {
242+ Log.Debug ("Dictionary preferences service cannot add notifications");
243+ return;
244+ }
245+
246+ public void RemoveNotify (string path)
247+ {
248+ Log.Debug ("Dictionary preferences service cannot remove notifications");
249+ return;
250+ }
251
252 #endregion
253-
254 }
255-
256-}
257+}
258\ No newline at end of file
259
260=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs'
261--- Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 2009-06-10 05:30:30 +0000
262+++ Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 2010-03-29 03:16:18 +0000
263@@ -19,6 +19,7 @@
264 */
265
266 using System;
267+using System.Net;
268
269 using Do.Platform;
270
271@@ -43,6 +44,12 @@
272 get { return connected; }
273 }
274
275+ public string UserAgent {
276+ get {
277+ return @"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100308 Ubuntu/10.04 (lucid) Firefox/3.6";
278+ }
279+ }
280+
281 #endregion
282 }
283 }
284
285=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs'
286--- Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs 2009-08-28 05:42:11 +0000
287+++ Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs 2010-03-29 03:16:18 +0000
288@@ -40,9 +40,21 @@
289 public bool TryGet<T> (string key, out T val)
290 {
291 Log.Debug ("Default IPreferencesService cannot get key \"{0}\".", key);
292- val = default (T);
293+ val = default(T);
294 return false;
295 }
296+
297+ public void AddNotify (string path)
298+ {
299+ Log.Debug ("Default IPreferencesService cannot add notifications");
300+ return;
301+ }
302+
303+ public void RemoveNotify (string path)
304+ {
305+ Log.Debug ("Default IPreferencesService cannot remove notifications");
306+ return;
307+ }
308
309 #endregion
310
311
312=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs'
313--- Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs 2009-08-31 01:58:13 +0000
314+++ Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs 2010-03-29 03:16:18 +0000
315@@ -43,10 +43,13 @@
316
317 void HandlePreferencesChanged (object o, PreferencesChangedEventArgs e)
318 {
319- if (e.Key.Length <= OwnerString.Length + 1 || e.Key.Substring(0, OwnerString.Length) != OwnerString)
320- return;
321+ // if the key starts with the OwnerString, just send the relative key
322+ // otherwise we send the full path to the key
323+ string key = e.Key;
324+ if (e.Key.StartsWith (OwnerString))
325+ key = e.Key.Substring (OwnerString.Length + 1);
326 if (PreferencesChanged != null)
327- PreferencesChanged (this, new PreferencesChangedEventArgs (e.Key.Substring(OwnerString.Length + 1), e.Value));
328+ PreferencesChanged (this, new PreferencesChangedEventArgs (key, e.Value));
329 }
330
331 #region IPreferences
332@@ -93,6 +96,16 @@
333 {
334 return Set (SecureService, key, val);
335 }
336+
337+ public void AddNotify (string path)
338+ {
339+ Service.AddNotify (path);
340+ }
341+
342+ public void RemoveNotify (string path)
343+ {
344+ Service.RemoveNotify (path);
345+ }
346
347 #endregion
348
349
350=== modified file 'Do.Platform/src/Do.Platform/INetworkService.cs'
351--- Do.Platform/src/Do.Platform/INetworkService.cs 2009-06-10 05:30:30 +0000
352+++ Do.Platform/src/Do.Platform/INetworkService.cs 2010-03-29 03:16:18 +0000
353@@ -19,6 +19,7 @@
354 */
355
356 using System;
357+using System.Net;
358
359 using Do.Platform.ServiceStack;
360
361@@ -38,5 +39,6 @@
362 event EventHandler<NetworkStateChangedEventArgs> StateChanged;
363
364 bool IsConnected { get; }
365+ string UserAgent { get; }
366 }
367 }
368\ No newline at end of file
369
370=== modified file 'Do.Platform/src/Do.Platform/IPreferences.cs'
371--- Do.Platform/src/Do.Platform/IPreferences.cs 2008-12-22 01:33:29 +0000
372+++ Do.Platform/src/Do.Platform/IPreferences.cs 2010-03-29 03:16:18 +0000
373@@ -33,5 +33,8 @@
374 T Get<T> (string key, T def);
375 bool SetSecure<T> (string key, T def);
376 T GetSecure<T> (string key, T def);
377+
378+ void AddNotify (string path);
379+ void RemoveNotify (string path);
380 }
381 }
382\ No newline at end of file
383
384=== modified file 'Do.Platform/src/Do.Platform/IPreferencesService.cs'
385--- Do.Platform/src/Do.Platform/IPreferencesService.cs 2009-08-28 05:42:11 +0000
386+++ Do.Platform/src/Do.Platform/IPreferencesService.cs 2010-03-29 03:16:18 +0000
387@@ -32,5 +32,8 @@
388
389 bool Set<T> (string key, T val);
390 bool TryGet<T> (string key, out T val);
391+
392+ void AddNotify (string path);
393+ void RemoveNotify (string path);
394 }
395 }
396
397=== modified file 'Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs'
398--- Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs 2009-08-28 05:42:11 +0000
399+++ Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs 2010-03-29 03:16:18 +0000
400@@ -61,7 +61,20 @@
401
402 void EnsureString<T> ()
403 {
404- if (typeof (T) != typeof (string)) throw new NotImplementedException ("Unimplemented for non string values");
405+ if (typeof(T) != typeof(string))
406+ throw new NotImplementedException ("Unimplemented for non string values");
407+ }
408+
409+ public void AddNotify (string path)
410+ {
411+ Log.Debug ("Secure preferences service cannot add notifications");
412+ return;
413+ }
414+
415+ public void RemoveNotify (string path)
416+ {
417+ Log.Debug ("Secure preferences service cannot remove notifications");
418+ return;
419 }
420 }
421 }