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
=== modified file 'Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs'
--- Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs 2009-08-31 01:58:13 +0000
+++ Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/GConfPreferencesService.cs 2010-03-29 03:16:18 +0000
@@ -43,8 +43,13 @@
43 43
44 void HandleGConfChanged (object sender, GConf.NotifyEventArgs args)44 void HandleGConfChanged (object sender, GConf.NotifyEventArgs args)
45 {45 {
46 // if this key falls under the RootPath, strip away that part of the key,
47 // otherwise we leave the full path to the key.
48 string key = args.Key;
49 if (key.StartsWith (RootPath))
50 key = args.Key.Substring (RootPath.Length + 1);
46 if (PreferencesChanged != null)51 if (PreferencesChanged != null)
47 PreferencesChanged (this, new PreferencesChangedEventArgs (args.Key.Substring(RootPath.Length + 1), args.Value));52 PreferencesChanged (this, new PreferencesChangedEventArgs (key, args.Value));
48 }53 }
4954
50 string AbsolutePathForKey (string key)55 string AbsolutePathForKey (string key)
@@ -74,9 +79,9 @@
74 public bool TryGet<T> (string key, out T val)79 public bool TryGet<T> (string key, out T val)
75 {80 {
76 bool success = true;81 bool success = true;
77 val = default (T);82 val = default(T);
78 try {83 try {
79 val = (T) client.Get (AbsolutePathForKey (key));84 val = (T)client.Get (AbsolutePathForKey (key));
80 } catch (GConf.NoSuchKeyException) {85 } catch (GConf.NoSuchKeyException) {
81 // We don't need to log this, because many keys that do not86 // We don't need to log this, because many keys that do not
82 // exist are asked for.87 // exist are asked for.
@@ -88,7 +93,27 @@
88 }93 }
89 return success;94 return success;
90 }95 }
96
97 public void AddNotify (string path)
98 {
99 try {
100 client.AddNotify (path, HandleGConfChanged);
101 } catch (Exception e) {
102 Log.Error ("Error removing notification handler, {0}", e.Message);
103 Log.Debug (e.StackTrace);
104 }
105 }
106
107 public void RemoveNotify (string path)
108 {
109 try {
110 client.RemoveNotify (path, HandleGConfChanged);
111 } catch (Exception e) {
112 Log.Error ("Error removing notification handler, {0}", e.Message);
113 Log.Debug (e.StackTrace);
114 }
115 }
91116
92 #endregion117 #endregion
93 }118 }
94}119}
95\ No newline at end of file120\ No newline at end of file
96121
=== modified file 'Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs'
--- Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 2009-10-21 07:06:25 +0000
+++ Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 2010-03-29 03:16:18 +0000
@@ -18,6 +18,7 @@
18//18//
1919
20using System;20using System;
21using System.Net;
21using System.Reflection;22using System.Reflection;
2223
23using NDesk.DBus;24using NDesk.DBus;
@@ -28,9 +29,65 @@
2829
29namespace Do.Platform.Linux30namespace Do.Platform.Linux
30{31{
31 32
32 public class NetworkService : INetworkService33 public class NetworkService : INetworkService, IInitializedService
33 {34 {
35 class NetworkPreferencesWrapper
36 {
37 const string PROXY = "/system/http_proxy";
38 const string PROXY_USE_PROXY = PROXY + "/" + "use_http_proxy";
39 const string PROXY_HOST = PROXY + "/" + "host";
40 const string PROXY_PORT = PROXY + "/" + "port";
41 const string PROXY_USER = PROXY + "/" + "authentication_user";
42 const string PROXY_PASSWORD = PROXY + "/" + "authentication_password";
43 const string PROXY_BYPASS_LIST = PROXY + "/" + "ignore_hosts";
44
45 IPreferences Preferences { get; set; }
46
47 public event EventHandler ProxyChanged;
48
49 public bool UseProxy {
50 get { return Preferences.Get<bool> (PROXY_USE_PROXY, false); }
51 }
52 public string ProxyHost {
53 get { return Preferences.Get<string> (PROXY_HOST, ""); }
54 }
55 public int ProxyPort {
56 get { return Preferences.Get<int> (PROXY_PORT, 0); }
57 }
58 public string ProxyUser {
59 get { return Preferences.Get<string> (PROXY_USER, ""); }
60 }
61 public string ProxyPassword {
62 get { return Preferences.Get<string> (PROXY_PASSWORD, ""); }
63 }
64 public string[] ProxyBypassList {
65 get { return Preferences.Get<string[]> (PROXY_BYPASS_LIST, new[] { "" }); }
66 }
67
68 public NetworkPreferencesWrapper ()
69 {
70 Preferences = Services.Preferences.Get<NetworkPreferencesWrapper> ();
71 Preferences.AddNotify (PROXY);
72 Preferences.PreferencesChanged += OnPrefsChanged;
73 }
74
75 void OnPrefsChanged (object sender, PreferencesChangedEventArgs e)
76 {
77 switch (e.Key) {
78 case PROXY_USE_PROXY:
79 case PROXY_HOST:
80 case PROXY_PORT:
81 case PROXY_USER:
82 case PROXY_PASSWORD:
83 case PROXY_BYPASS_LIST:
84 if (ProxyChanged != null)
85 ProxyChanged (this, EventArgs.Empty);
86 break;
87 }
88 }
89 }
90
34 const string NetworkManagerName = "org.freedesktop.NetworkManager";91 const string NetworkManagerName = "org.freedesktop.NetworkManager";
35 const string NetworkManagerPath = "/org/freedesktop/NetworkManager";92 const string NetworkManagerPath = "/org/freedesktop/NetworkManager";
36 93
@@ -43,6 +100,7 @@
43 delegate void StateChangedHandler (uint state);100 delegate void StateChangedHandler (uint state);
44 101
45 INetworkManager network;102 INetworkManager network;
103 static NetworkPreferencesWrapper prefs;
46 104
47 public event EventHandler<NetworkStateChangedEventArgs> StateChanged;105 public event EventHandler<NetworkStateChangedEventArgs> StateChanged;
48 106
@@ -62,6 +120,15 @@
62 Log<NetworkService>.Debug (e.StackTrace);120 Log<NetworkService>.Debug (e.StackTrace);
63 }121 }
64 }122 }
123
124 public void Initialize ()
125 {
126 prefs = new NetworkPreferencesWrapper ();
127 UpdateProxySettings ();
128 prefs.ProxyChanged += delegate {
129 UpdateProxySettings ();
130 };
131 }
65132
66 void OnStateChanged (uint state)133 void OnStateChanged (uint state)
67 {134 {
@@ -81,18 +148,55 @@
81 }148 }
82 149
83 NetworkState State {150 NetworkState State {
84 get { 151 get {
85 try {152 try {
86 return (NetworkState) Enum.ToObject (typeof (NetworkState), network.Get (NetworkManagerName, "State"));153 return (NetworkState)Enum.ToObject (typeof(NetworkState), network.Get (NetworkManagerName, "State"));
87 } catch (Exception) {154 } catch (Exception) {
88 return NetworkState.Unknown;155 return NetworkState.Unknown;
89 }156 }
90 }157 }
91 }158 }
92 159
160 void UpdateProxySettings ()
161 {
162 WebProxy proxy;
163
164 if (!prefs.UseProxy) {
165 WebRequest.DefaultWebProxy = null;
166 return;
167 }
168
169 try {
170 string proxyUri = string.Format ("http://{0}:{1}", prefs.ProxyHost, prefs.ProxyPort);
171
172 proxy = new WebProxy (proxyUri);
173 string[] bypassList = prefs.ProxyBypassList;
174 if (bypassList != null) {
175 foreach (string host in bypassList) {
176 if (host.Contains ("*.local")) {
177 proxy.BypassProxyOnLocal = true;
178 continue;
179 }
180 proxy.BypassArrayList.Add (string.Format ("http://{0}", host));
181 }
182 }
183 proxy.Credentials = new NetworkCredential (prefs.ProxyUser, prefs.ProxyPassword);
184 } catch (Exception e) {
185 Log.Error ("Error creating web proxy, {0}", e.Message);
186 Log.Debug (e.StackTrace);
187 proxy = null;
188 }
189
190 WebRequest.DefaultWebProxy = proxy;
191 }
192
93 #region INetworkService193 #region INetworkService
94194
95 public bool IsConnected { get; private set; }195 public bool IsConnected { get; private set; }
196
197 public string UserAgent {
198 get { return @"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100308 Ubuntu/10.04 (lucid) Firefox/3.6"; }
199 }
96200
97 #endregion201 #endregion
98 }202 }
99203
=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs'
--- Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs 2009-08-31 01:58:13 +0000
+++ Do.Platform/src/Do.Platform/Do.Platform.Common/DictionaryPreferencesService.cs 2010-03-29 03:16:18 +0000
@@ -57,15 +57,25 @@
57 object val_object;57 object val_object;
58 bool success = Store.TryGetValue (key, out val_object);58 bool success = Store.TryGetValue (key, out val_object);
59 59
60 val = default (T);60 val = default(T);
61 if (success)61 if (success)
62 val = (T) val_object;62 val = (T)val_object;
63 63
64 return success;64 return success;
65 }65 }
66
67 public void AddNotify (string path)
68 {
69 Log.Debug ("Dictionary preferences service cannot add notifications");
70 return;
71 }
72
73 public void RemoveNotify (string path)
74 {
75 Log.Debug ("Dictionary preferences service cannot remove notifications");
76 return;
77 }
6678
67 #endregion79 #endregion
68
69 }80 }
7081}
71}
72\ No newline at end of file82\ No newline at end of file
7383
=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs'
--- Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 2009-06-10 05:30:30 +0000
+++ Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 2010-03-29 03:16:18 +0000
@@ -19,6 +19,7 @@
19 */19 */
2020
21using System;21using System;
22using System.Net;
2223
23using Do.Platform;24using Do.Platform;
2425
@@ -43,6 +44,12 @@
43 get { return connected; }44 get { return connected; }
44 }45 }
45 46
47 public string UserAgent {
48 get {
49 return @"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100308 Ubuntu/10.04 (lucid) Firefox/3.6";
50 }
51 }
52
46 #endregion53 #endregion
47 }54 }
48}55}
4956
=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs'
--- Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs 2009-08-28 05:42:11 +0000
+++ Do.Platform/src/Do.Platform/Do.Platform.Default/PreferencesService.cs 2010-03-29 03:16:18 +0000
@@ -40,9 +40,21 @@
40 public bool TryGet<T> (string key, out T val)40 public bool TryGet<T> (string key, out T val)
41 {41 {
42 Log.Debug ("Default IPreferencesService cannot get key \"{0}\".", key);42 Log.Debug ("Default IPreferencesService cannot get key \"{0}\".", key);
43 val = default (T);43 val = default(T);
44 return false;44 return false;
45 }45 }
46
47 public void AddNotify (string path)
48 {
49 Log.Debug ("Default IPreferencesService cannot add notifications");
50 return;
51 }
52
53 public void RemoveNotify (string path)
54 {
55 Log.Debug ("Default IPreferencesService cannot remove notifications");
56 return;
57 }
4658
47 #endregion59 #endregion
48 60
4961
=== modified file 'Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs'
--- Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs 2009-08-31 01:58:13 +0000
+++ Do.Platform/src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs 2010-03-29 03:16:18 +0000
@@ -43,10 +43,13 @@
43 43
44 void HandlePreferencesChanged (object o, PreferencesChangedEventArgs e)44 void HandlePreferencesChanged (object o, PreferencesChangedEventArgs e)
45 {45 {
46 if (e.Key.Length <= OwnerString.Length + 1 || e.Key.Substring(0, OwnerString.Length) != OwnerString)46 // if the key starts with the OwnerString, just send the relative key
47 return;47 // otherwise we send the full path to the key
48 string key = e.Key;
49 if (e.Key.StartsWith (OwnerString))
50 key = e.Key.Substring (OwnerString.Length + 1);
48 if (PreferencesChanged != null)51 if (PreferencesChanged != null)
49 PreferencesChanged (this, new PreferencesChangedEventArgs (e.Key.Substring(OwnerString.Length + 1), e.Value));52 PreferencesChanged (this, new PreferencesChangedEventArgs (key, e.Value));
50 }53 }
5154
52 #region IPreferences55 #region IPreferences
@@ -93,6 +96,16 @@
93 {96 {
94 return Set (SecureService, key, val);97 return Set (SecureService, key, val);
95 }98 }
99
100 public void AddNotify (string path)
101 {
102 Service.AddNotify (path);
103 }
104
105 public void RemoveNotify (string path)
106 {
107 Service.RemoveNotify (path);
108 }
96109
97 #endregion110 #endregion
98111
99112
=== modified file 'Do.Platform/src/Do.Platform/INetworkService.cs'
--- Do.Platform/src/Do.Platform/INetworkService.cs 2009-06-10 05:30:30 +0000
+++ Do.Platform/src/Do.Platform/INetworkService.cs 2010-03-29 03:16:18 +0000
@@ -19,6 +19,7 @@
19 */19 */
2020
21using System;21using System;
22using System.Net;
2223
23using Do.Platform.ServiceStack;24using Do.Platform.ServiceStack;
2425
@@ -38,5 +39,6 @@
38 event EventHandler<NetworkStateChangedEventArgs> StateChanged;39 event EventHandler<NetworkStateChangedEventArgs> StateChanged;
39 40
40 bool IsConnected { get; }41 bool IsConnected { get; }
42 string UserAgent { get; }
41 }43 }
42}44}
43\ No newline at end of file45\ No newline at end of file
4446
=== modified file 'Do.Platform/src/Do.Platform/IPreferences.cs'
--- Do.Platform/src/Do.Platform/IPreferences.cs 2008-12-22 01:33:29 +0000
+++ Do.Platform/src/Do.Platform/IPreferences.cs 2010-03-29 03:16:18 +0000
@@ -33,5 +33,8 @@
33 T Get<T> (string key, T def);33 T Get<T> (string key, T def);
34 bool SetSecure<T> (string key, T def);34 bool SetSecure<T> (string key, T def);
35 T GetSecure<T> (string key, T def);35 T GetSecure<T> (string key, T def);
36
37 void AddNotify (string path);
38 void RemoveNotify (string path);
36 }39 }
37}40}
38\ No newline at end of file41\ No newline at end of file
3942
=== modified file 'Do.Platform/src/Do.Platform/IPreferencesService.cs'
--- Do.Platform/src/Do.Platform/IPreferencesService.cs 2009-08-28 05:42:11 +0000
+++ Do.Platform/src/Do.Platform/IPreferencesService.cs 2010-03-29 03:16:18 +0000
@@ -32,5 +32,8 @@
32 32
33 bool Set<T> (string key, T val);33 bool Set<T> (string key, T val);
34 bool TryGet<T> (string key, out T val);34 bool TryGet<T> (string key, out T val);
35
36 void AddNotify (string path);
37 void RemoveNotify (string path);
35 }38 }
36}39}
3740
=== modified file 'Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs'
--- Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs 2009-08-28 05:42:11 +0000
+++ Do.Platform/src/Do.Platform/SecurePreferencesWrapper.cs 2010-03-29 03:16:18 +0000
@@ -61,7 +61,20 @@
6161
62 void EnsureString<T> ()62 void EnsureString<T> ()
63 {63 {
64 if (typeof (T) != typeof (string)) throw new NotImplementedException ("Unimplemented for non string values");64 if (typeof(T) != typeof(string))
65 throw new NotImplementedException ("Unimplemented for non string values");
66 }
67
68 public void AddNotify (string path)
69 {
70 Log.Debug ("Secure preferences service cannot add notifications");
71 return;
72 }
73
74 public void RemoveNotify (string path)
75 {
76 Log.Debug ("Secure preferences service cannot remove notifications");
77 return;
65 }78 }
66 }79 }
67}80}