Do

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

Proposed by Chris S.
Status: Merged
Merged at revision: not available
Proposed branch: lp:~cszikszoy/do/NetworkService
Merge into: lp:do
Diff against target: None lines
To merge this branch: bzr merge lp:~cszikszoy/do/NetworkService
Reviewer Review Type Date Requested Status
David Siegel (community) Disapprove
Review via email: mp+7295@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Chris S. (cszikszoy) wrote :

Fix for https://bugs.edge.launchpad.net/bugs/324484 - Add network connection info to Do.Platform

This patch adds a generic Network service to Do.Platform and a NetworkManager implementation to Do.Platform.Linux

The Network service contains one boolean property (IsConnected) and an event (StateChanged).

Do.Universe.Element was updated and given a boolean property NetAccessRequired. This property defaults to false. Elements (Actions or ItemSources) requiring network access should override this and set it to true.

Do.Core.UniverseManager was also updated to be connection state aware. If no network connection is active, Elements (Actions or ItemSources) requiring network access will be removed from universe. When a network connection is re-established, those items will be added back.

Revision history for this message
David Siegel (djsiegel-deactivatedaccount) wrote :

Removing elements from the Universe is not a good solution. Users will have no idea why they cannot find items and actions that are usually there.

Also, adding a boolean to Element is not appropriate.

review: Disapprove
Revision history for this message
David Siegel (djsiegel-deactivatedaccount) wrote :

However, the Network service seems like a reasonable way to let plugins know when they should stop trying (and failing) to talk to the network. Remove the universe stuff and we can take another look.

lp:~cszikszoy/do/NetworkService updated
1222. By Chris S.

remove code removing items from universe

1223. By Chris S.

remove more network checking

1224. By Chris S.

merge trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Do.Platform.Linux/Do.Platform.Linux.mdp'
--- Do.Platform.Linux/Do.Platform.Linux.mdp 2009-06-05 19:45:45 +0000
+++ Do.Platform.Linux/Do.Platform.Linux.mdp 2009-06-10 05:30:30 +0000
@@ -49,6 +49,7 @@
49 <File name="src/Do.Platform/Do.Platform.Linux/Do.Platform.Linux.DBus/Registrar.cs" subtype="Code" buildaction="Compile" />49 <File name="src/Do.Platform/Do.Platform.Linux/Do.Platform.Linux.DBus/Registrar.cs" subtype="Code" buildaction="Compile" />
50 <File name="src/Do.Platform/Do.Platform.Linux/SystemService.cs" subtype="Code" buildaction="Compile" />50 <File name="src/Do.Platform/Do.Platform.Linux/SystemService.cs" subtype="Code" buildaction="Compile" />
51 <File name="src/Do.Universe/NullApplicationItem.cs" subtype="Code" buildaction="Compile" />51 <File name="src/Do.Universe/NullApplicationItem.cs" subtype="Code" buildaction="Compile" />
52 <File name="src/Do.Platform/Do.Platform.Linux/NetworkService.cs" subtype="Code" buildaction="Compile" />
52 </Contents>53 </Contents>
53 <References>54 <References>
54 <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />55 <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
5556
=== modified file 'Do.Platform.Linux/Makefile.am'
--- Do.Platform.Linux/Makefile.am 2009-05-29 01:26:23 +0000
+++ Do.Platform.Linux/Makefile.am 2009-06-10 05:30:30 +0000
@@ -21,6 +21,7 @@
21 src/Do.Platform/Do.Platform.Linux/TrayIconPreferences.cs \21 src/Do.Platform/Do.Platform.Linux/TrayIconPreferences.cs \
22 src/Do.Platform/Do.Platform.Linux/TrayIconService.cs \22 src/Do.Platform/Do.Platform.Linux/TrayIconService.cs \
23 src/Do.Platform/Do.Platform.Linux/UniverseFactoryService.cs \23 src/Do.Platform/Do.Platform.Linux/UniverseFactoryService.cs \
24 src/Do.Platform/Do.Platform.Linux/NetworkService.cs \
24 src/Do.Universe/ApplicationItem.cs \25 src/Do.Universe/ApplicationItem.cs \
25 src/Do.Universe/CategoryItem.cs \26 src/Do.Universe/CategoryItem.cs \
26 src/Do.Universe/NullApplicationItem.cs \27 src/Do.Universe/NullApplicationItem.cs \
2728
=== modified file 'Do.Platform.Linux/Resources/Do.Platform.Linux.addin.xml'
--- Do.Platform.Linux/Resources/Do.Platform.Linux.addin.xml 2009-02-17 22:48:40 +0000
+++ Do.Platform.Linux/Resources/Do.Platform.Linux.addin.xml 2009-06-10 05:30:30 +0000
@@ -23,6 +23,7 @@
23 <Service type="Do.Platform.Common.EventsOnlyNotificationsService" />23 <Service type="Do.Platform.Common.EventsOnlyNotificationsService" />
2424
25 <Service type="Do.Platform.Linux.SystemService" />25 <Service type="Do.Platform.Linux.SystemService" />
26 <Service type="Do.Platform.Linux.NetworkService" />
26 <Service type="Do.Platform.Linux.TrayIconService" />27 <Service type="Do.Platform.Linux.TrayIconService" />
27 <Service type="Do.Platform.Linux.ConsoleLogService" />28 <Service type="Do.Platform.Linux.ConsoleLogService" />
28 <Service type="Do.Platform.Linux.EnvironmentService" />29 <Service type="Do.Platform.Linux.EnvironmentService" />
2930
=== added file 'Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs'
--- Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 1970-01-01 00:00:00 +0000
+++ Do.Platform.Linux/src/Do.Platform/Do.Platform.Linux/NetworkService.cs 2009-06-10 16:44:20 +0000
@@ -0,0 +1,94 @@
1// NetworkService.cs
2//
3// GNOME Do is the legal property of its developers. Please refer to the
4// COPYRIGHT file distributed with this source distribution.
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18//
19
20using System;
21using System.Reflection;
22
23using NDesk.DBus;
24using org.freedesktop.DBus;
25
26using Do.Platform.ServiceStack;
27using Do.Platform.Linux.DBus;
28
29namespace Do.Platform.Linux
30{
31
32 public class NetworkService : INetworkService
33 {
34 const string NetworkManagerName = "org.freedesktop.NetworkManager";
35 const string NetworkManagerPath = "/org/freedesktop/NetworkManager";
36
37 [Interface(NetworkManagerName)]
38 interface INetworkManager : org.freedesktop.DBus.Properties
39 {
40 event StateChangedHandler StateChanged;
41 }
42
43 delegate void StateChangedHandler (uint state);
44
45 INetworkManager network;
46
47 public event EventHandler<NetworkStateChangedEventArgs> StateChanged;
48
49 public NetworkService ()
50 {
51 try {
52 BusG.Init ();
53 if (Bus.System.NameHasOwner (NetworkManagerName)) {
54 network = Bus.System.GetObject<INetworkManager> (NetworkManagerName, new ObjectPath (NetworkManagerPath));
55 network.StateChanged += OnStateChanged;
56 }
57 } catch (Exception e) {
58 Log<NetworkService>.Error ("Could not initialize dbus: {0}", e.Message);
59 Log<NetworkService>.Debug (e.StackTrace);
60 }
61
62 SetConnected ();
63 }
64
65 void OnStateChanged (uint state)
66 {
67 NetworkState newState = (NetworkState) Enum.ToObject (typeof (NetworkState), state);
68 SetConnected ();
69 if (StateChanged != null) {
70 StateChanged (this, new NetworkStateChangedEventArgs (newState));
71 }
72 }
73
74 void SetConnected ()
75 {
76 if (this.State == NetworkState.Connected)
77 IsConnected = true;
78 else
79 IsConnected = false;
80 }
81
82 NetworkState State {
83 get {
84 return (NetworkState) Enum.ToObject (typeof (NetworkState), network.Get (NetworkManagerName, "State"));
85 }
86 }
87
88 #region INetworkService
89
90 public bool IsConnected { get; private set; }
91
92 #endregion
93 }
94}
0\ No newline at end of file95\ No newline at end of file
196
=== modified file 'Do.Platform/Do.Platform.mdp'
--- Do.Platform/Do.Platform.mdp 2009-06-05 19:45:45 +0000
+++ Do.Platform/Do.Platform.mdp 2009-06-10 05:30:30 +0000
@@ -76,6 +76,9 @@
76 <File name="src/System/Linq/EnumerableExtensions.cs" subtype="Code" buildaction="Compile" />76 <File name="src/System/Linq/EnumerableExtensions.cs" subtype="Code" buildaction="Compile" />
77 <File name="src/Do.Platform/AbstractSystemService.cs" subtype="Code" buildaction="Compile" />77 <File name="src/Do.Platform/AbstractSystemService.cs" subtype="Code" buildaction="Compile" />
78 <File name="src/Do.Platform/Do.Platform.Default/DefaultSystemService.cs" subtype="Code" buildaction="Compile" />78 <File name="src/Do.Platform/Do.Platform.Default/DefaultSystemService.cs" subtype="Code" buildaction="Compile" />
79 <File name="src/Do.Platform/INetworkService.cs" subtype="Code" buildaction="Compile" />
80 <File name="src/Do.Platform/NetworkStateChangedEventArgs.cs" subtype="Code" buildaction="Compile" />
81 <File name="src/Do.Platform/Do.Platform.Default/NetworkService.cs" subtype="Code" buildaction="Compile" />
79 </Contents>82 </Contents>
80 <References>83 <References>
81 <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />84 <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
8285
=== modified file 'Do.Platform/Makefile.am'
--- Do.Platform/Makefile.am 2009-02-17 21:58:50 +0000
+++ Do.Platform/Makefile.am 2009-06-10 05:30:30 +0000
@@ -25,6 +25,7 @@
25 src/Do.Platform/Do.Platform.Default/SecurePreferencesService.cs \25 src/Do.Platform/Do.Platform.Default/SecurePreferencesService.cs \
26 src/Do.Platform/Do.Platform.Default/UniverseFactoryService.cs \26 src/Do.Platform/Do.Platform.Default/UniverseFactoryService.cs \
27 src/Do.Platform/Do.Platform.Default/WindowingService.cs \27 src/Do.Platform/Do.Platform.Default/WindowingService.cs \
28 src/Do.Platform/Do.Platform.Default/NetworkService.cs \
28 src/Do.Platform/Do.Platform.Preferences/PreferencesFactory.cs \29 src/Do.Platform/Do.Platform.Preferences/PreferencesFactory.cs \
29 src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs \30 src/Do.Platform/Do.Platform.Preferences/PreferencesImplementation.cs \
30 src/Do.Platform/Do.Platform.ServiceStack/IInitializedService.cs \31 src/Do.Platform/Do.Platform.ServiceStack/IInitializedService.cs \
@@ -49,6 +50,8 @@
49 src/Do.Platform/PreferencesChangedEventArgs.cs \50 src/Do.Platform/PreferencesChangedEventArgs.cs \
50 src/Do.Platform/SecurePreferencesWrapper.cs \51 src/Do.Platform/SecurePreferencesWrapper.cs \
51 src/Do.Platform/Services.cs \52 src/Do.Platform/Services.cs \
53 src/Do.Platform/INetworkService.cs \
54 src/Do.Platform/NetworkStateChangedEventArgs.cs \
52 src/Do.Universe/Do.Universe.Common/EmailAction.cs \55 src/Do.Universe/Do.Universe.Common/EmailAction.cs \
53 src/Do.Universe/Do.Universe.Common/OpenAction.cs \56 src/Do.Universe/Do.Universe.Common/OpenAction.cs \
54 src/Do.Universe/Do.Universe.Common/OpenUrlAction.cs \57 src/Do.Universe/Do.Universe.Common/OpenUrlAction.cs \
5558
=== added file 'Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs'
--- Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 1970-01-01 00:00:00 +0000
+++ Do.Platform/src/Do.Platform/Do.Platform.Default/NetworkService.cs 2009-06-10 05:30:30 +0000
@@ -0,0 +1,48 @@
1/* NetworkService.cs
2 *
3 * GNOME Do is the legal property of its developers. Please refer to the
4 * COPYRIGHT file distributed with this
5 * source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21using System;
22
23using Do.Platform;
24
25namespace Do.Platform.Default
26{
27
28 public class NetworkService : INetworkService
29 {
30 public event EventHandler<NetworkStateChangedEventArgs> StateChanged;
31
32 public NetworkService ()
33 {
34 connected = true;
35 }
36
37 bool connected;
38
39 #region INetworkService
40
41 public bool IsConnected
42 {
43 get { return connected; }
44 }
45
46 #endregion
47 }
48}
049
=== added file 'Do.Platform/src/Do.Platform/INetworkService.cs'
--- Do.Platform/src/Do.Platform/INetworkService.cs 1970-01-01 00:00:00 +0000
+++ Do.Platform/src/Do.Platform/INetworkService.cs 2009-06-10 05:30:30 +0000
@@ -0,0 +1,42 @@
1/* INetworkService.cs
2 *
3 * GNOME Do is the legal property of its developers. Please refer to the
4 * COPYRIGHT file distributed with this
5 * source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21using System;
22
23using Do.Platform.ServiceStack;
24
25namespace Do.Platform
26{
27
28 public enum NetworkState {
29 Unknown = 0,
30 Asleep,
31 Connecting,
32 Connected,
33 Disconnected
34 }
35
36 public interface INetworkService : IService
37 {
38 event EventHandler<NetworkStateChangedEventArgs> StateChanged;
39
40 bool IsConnected { get; }
41 }
42}
0\ No newline at end of file43\ No newline at end of file
144
=== added file 'Do.Platform/src/Do.Platform/NetworkStateChangedEventArgs.cs'
--- Do.Platform/src/Do.Platform/NetworkStateChangedEventArgs.cs 1970-01-01 00:00:00 +0000
+++ Do.Platform/src/Do.Platform/NetworkStateChangedEventArgs.cs 2009-06-10 05:30:30 +0000
@@ -0,0 +1,35 @@
1/* NetworkStatusChangedEventArgs.cs
2 *
3 * GNOME Do is the legal property of its developers. Please refer to the
4 * COPYRIGHT file distributed with this
5 * source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21using System;
22
23namespace Do.Platform {
24
25 public class NetworkStateChangedEventArgs : EventArgs
26 {
27 public NetworkState NewState { get; private set; }
28
29 public NetworkStateChangedEventArgs (NetworkState newState)
30 {
31 NewState = newState;
32 }
33 }
34
35}
0\ No newline at end of file36\ No newline at end of file
137
=== modified file 'Do.Platform/src/Do.Platform/Services.cs'
--- Do.Platform/src/Do.Platform/Services.cs 2009-02-17 21:58:50 +0000
+++ Do.Platform/src/Do.Platform/Services.cs 2009-06-10 05:30:30 +0000
@@ -43,6 +43,7 @@
43 static IEnvironmentService environment;43 static IEnvironmentService environment;
44 static INotificationsService notifications;44 static INotificationsService notifications;
45 static IUniverseFactoryService universe_factory;45 static IUniverseFactoryService universe_factory;
46 static INetworkService network;
4647
47 /// <summary>48 /// <summary>
48 /// Initializes the class. Must be called after Mono.Addins is initialized; if this is49 /// Initializes the class. Must be called after Mono.Addins is initialized; if this is
@@ -99,6 +100,8 @@
99 application = null;100 application = null;
100 if (service is AbstractSystemService)101 if (service is AbstractSystemService)
101 system = null;102 system = null;
103 if (service is INetworkService)
104 network = null;
102 }105 }
103106
104 /// <summary>107 /// <summary>
@@ -175,6 +178,14 @@
175 return universe_factory;178 return universe_factory;
176 }179 }
177 }180 }
181
182 public static INetworkService Network {
183 get {
184 if (network == null)
185 network = LocateService<INetworkService, Default.NetworkService> ();
186 return network;
187 }
188 }
178 189
179 public static PreferencesFactory Preferences {190 public static PreferencesFactory Preferences {
180 get {191 get {
181192
=== modified file 'Do.Universe/src/Do.Universe/Element.cs'
--- Do.Universe/src/Do.Universe/Element.cs 2009-01-17 22:13:37 +0000
+++ Do.Universe/src/Do.Universe/Element.cs 2009-06-10 07:29:46 +0000
@@ -116,6 +116,14 @@
116 116
117 public abstract string Icon { get; }117 public abstract string Icon { get; }
118 118
119 /// <value>
120 /// Provides an indication of whether or not this element requires an active
121 /// network connection.
122 /// </value>
123 public virtual bool NetworkRequired {
124 get { return false; }
125 }
126
119 public override int GetHashCode ()127 public override int GetHashCode ()
120 {128 {
121 return UniqueId.GetHashCode ();129 return UniqueId.GetHashCode ();
122130
=== modified file 'Do/src/Do.Core/UniverseManager.cs'
--- Do/src/Do.Core/UniverseManager.cs 2009-01-25 20:32:52 +0000
+++ Do/src/Do.Core/UniverseManager.cs 2009-06-10 07:42:46 +0000
@@ -80,6 +80,13 @@
80 update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));80 update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));
81 update_thread.IsBackground = true;81 update_thread.IsBackground = true;
82 update_thread.Priority = ThreadPriority.Lowest;82 update_thread.Priority = ThreadPriority.Lowest;
83
84 Services.Network.StateChanged += OnNetworkStateChanged;
85 }
86
87 void OnNetworkStateChanged (object sender, NetworkStateChangedEventArgs e)
88 {
89 Reload ();
83 }90 }
84 91
85 public void Initialize ()92 public void Initialize ()
@@ -190,11 +197,11 @@
190 Log<UniverseManager>.Debug ("Reloading actions...");197 Log<UniverseManager>.Debug ("Reloading actions...");
191 lock (universe) {198 lock (universe) {
192 foreach (Act action in PluginManager.Actions) {199 foreach (Act action in PluginManager.Actions) {
193 universe.Remove (action.UniqueId);200 if (universe.ContainsKey (action.UniqueId))
194 }201 universe.Remove (action.UniqueId);
195 foreach (Act action in PluginManager.Actions) {202 }
196 universe [action.UniqueId] = action; 203 foreach (Act action in PluginManager.Actions.Where (a => ShouldUpdate (a)))
197 }204 universe [action.UniqueId] = action;
198 }205 }
199 }206 }
200 207
@@ -211,24 +218,37 @@
211 if (source == null) throw new ArgumentNullException ("source");218 if (source == null) throw new ArgumentNullException ("source");
212 219
213 safeSource = source.RetainSafe ();220 safeSource = source.RetainSafe ();
214 Log<UniverseManager>.Debug ("Reloading item source \"{0}\"...", safeSource.Name);
215 oldItems = safeSource.Items;221 oldItems = safeSource.Items;
216 // We call UpdateItems outside of the lock so as not to block other222 // We call UpdateItems outside of the lock so as not to block other
217 // threads in contention for the lock if UpdateItems blocks.223 // threads in contention for the lock if UpdateItems blocks.
218 safeSource.UpdateItems ();224 if (ShouldUpdate (source)) {
219 newItems = safeSource.Items;225 Log<UniverseManager>.Debug ("Reloading item source \"{0}\"...", safeSource.Name);
226 safeSource.UpdateItems ();
227 newItems = safeSource.Items;
228 } else {
229 newItems = Enumerable.Empty<Item> ();
230 }
220 231
221 lock (universe) {232 lock (universe) {
222 foreach (Item item in oldItems) {233 foreach (Item item in oldItems) {
223 if (universe.ContainsKey (item.UniqueId))234 if (universe.ContainsKey (item.UniqueId))
224 universe.Remove (item.UniqueId);235 universe.Remove (item.UniqueId);
225 }236 }
237 //only add the items if no net access is required, or if it is, if we have net access
226 foreach (Item item in newItems) {238 foreach (Item item in newItems) {
227 universe [item.UniqueId] = item;239 universe [item.UniqueId] = item;
228 }240 }
229 }241 }
230 }242 }
231 243
244 bool ShouldUpdate (Element element)
245 {
246 if (element.NetworkRequired && !Services.Network.IsConnected)
247 return false;
248 else
249 return true;
250 }
251
232 void ReloadUniverse ()252 void ReloadUniverse ()
233 {253 {
234 Log<UniverseManager>.Info ("Reloading universe...");254 Log<UniverseManager>.Info ("Reloading universe...");