PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/src/AddIns/Misc/PackageManagement/Project/Src/ThreadSafePackageManagementEvents.cs

https://github.com/ajadex/SharpDevelop
C# | 244 lines | 188 code | 39 blank | 17 comment | 22 complexity | 52708c11e4fff93a9bceba5c1a31a88a MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Collections.Generic;
 21using NuGet;
 22
 23namespace ICSharpCode.PackageManagement
 24{
 25	public class ThreadSafePackageManagementEvents : IThreadSafePackageManagementEvents
 26	{
 27		IPackageManagementEvents unsafeEvents;
 28		IPackageManagementWorkbench workbench;
 29		
 30		public ThreadSafePackageManagementEvents(IPackageManagementEvents unsafeEvents)
 31			: this(unsafeEvents, new PackageManagementWorkbench())
 32		{
 33		}
 34		
 35		public ThreadSafePackageManagementEvents(
 36			IPackageManagementEvents unsafeEvents,
 37			IPackageManagementWorkbench workbench)
 38		{
 39			this.unsafeEvents = unsafeEvents;
 40			this.workbench = workbench;
 41			
 42			RegisterEventHandlers();
 43		}
 44		
 45		void RegisterEventHandlers()
 46		{
 47			unsafeEvents.PackageOperationsStarting += RaisePackageOperationStartingEventIfHasSubscribers;
 48			unsafeEvents.PackageOperationError += RaisePackageOperationErrorEventIfHasSubscribers;
 49			unsafeEvents.ParentPackageInstalled += RaiseParentPackageInstalledEventIfHasSubscribers;
 50			unsafeEvents.ParentPackageUninstalled += RaiseParentPackageUninstalledEventIfHasSubscribers;
 51			unsafeEvents.ParentPackagesUpdated += RaiseParentPackagesUpdatedEventIfHasSubscribers;
 52		}
 53		
 54		public void Dispose()
 55		{
 56			UnregisterEventHandlers();
 57		}
 58		
 59		void UnregisterEventHandlers()
 60		{
 61			unsafeEvents.PackageOperationsStarting -= RaisePackageOperationStartingEventIfHasSubscribers;
 62			unsafeEvents.PackageOperationError -= RaisePackageOperationErrorEventIfHasSubscribers;
 63			unsafeEvents.ParentPackageInstalled -= RaiseParentPackageInstalledEventIfHasSubscribers;
 64			unsafeEvents.ParentPackageUninstalled -= RaiseParentPackageUninstalledEventIfHasSubscribers;
 65			unsafeEvents.ParentPackagesUpdated -= RaiseParentPackagesUpdatedEventIfHasSubscribers;
 66		}
 67		
 68		void RaisePackageOperationStartingEventIfHasSubscribers(object sender, EventArgs e)
 69		{
 70			if (PackageOperationsStarting != null) {
 71				RaisePackageOperationStartingEvent(sender, e);
 72			}
 73		}
 74		
 75		void RaisePackageOperationStartingEvent(object sender, EventArgs e)
 76		{
 77			if (InvokeRequired) {
 78				Action<object, EventArgs> action = RaisePackageOperationStartingEvent;
 79				SafeThreadAsyncCall(action, sender, e);
 80			} else {
 81				PackageOperationsStarting(sender, e);
 82			}
 83		}
 84		
 85		bool InvokeRequired {
 86			get { return workbench.InvokeRequired; }
 87		}
 88		
 89		void SafeThreadAsyncCall<A, B>(Action<A, B> method, A arg1, B arg2)
 90		{
 91			workbench.SafeThreadAsyncCall<A, B>(method, arg1, arg2);
 92		}
 93		
 94		R SafeThreadFunction<R>(Func<R> method)
 95		{
 96			return workbench.SafeThreadFunction<R>(method);
 97		}
 98		
 99		public event EventHandler PackageOperationsStarting;
100		
101		void RaisePackageOperationErrorEventIfHasSubscribers(object sender, PackageOperationExceptionEventArgs e)
102		{
103			if (PackageOperationError != null) {
104				RaisePackageOperationErrorEvent(sender, e);
105			}
106		}
107		
108		void RaisePackageOperationErrorEvent(object sender, PackageOperationExceptionEventArgs e)
109		{
110			if (PackageOperationError != null) {
111				if (InvokeRequired) {
112					Action<object, PackageOperationExceptionEventArgs> action = RaisePackageOperationErrorEvent;
113					SafeThreadAsyncCall(action, sender, e);
114				} else {
115					PackageOperationError(sender, e);
116				}
117			}
118		}
119		
120		public event EventHandler<PackageOperationExceptionEventArgs> PackageOperationError;
121		
122		void RaiseParentPackageInstalledEventIfHasSubscribers(object sender, ParentPackageOperationEventArgs e)
123		{
124			if (ParentPackageInstalled != null) {
125				RaiseParentPackageInstalledEvent(sender, e);
126			}
127		}
128		
129		void RaiseParentPackageInstalledEvent(object sender, ParentPackageOperationEventArgs e)
130		{
131			if (InvokeRequired) {
132				Action<object, ParentPackageOperationEventArgs> action = RaiseParentPackageInstalledEvent;
133				SafeThreadAsyncCall(action, sender, e);
134			} else {
135				ParentPackageInstalled(sender, e);
136			}
137		}
138		
139		public event EventHandler<ParentPackageOperationEventArgs> ParentPackageInstalled;
140		
141		void RaiseParentPackageUninstalledEventIfHasSubscribers(object sender, ParentPackageOperationEventArgs e)
142		{
143			if (ParentPackageUninstalled != null) {
144				RaiseParentPackageUninstalledEvent(sender, e);
145			}
146		}
147		
148		void RaiseParentPackageUninstalledEvent(object sender, ParentPackageOperationEventArgs e)
149		{
150			if (InvokeRequired) {
151				Action<object, ParentPackageOperationEventArgs> action = RaiseParentPackageUninstalledEvent;
152				SafeThreadAsyncCall(action, sender, e);
153			} else {
154				ParentPackageUninstalled(sender, e);
155			}
156		}
157		
158		public event EventHandler<ParentPackageOperationEventArgs> ParentPackageUninstalled;
159		
160		public event EventHandler<AcceptLicensesEventArgs> AcceptLicenses {
161			add { unsafeEvents.AcceptLicenses += value; }
162			remove { unsafeEvents.AcceptLicenses -= value; }
163		}
164		
165		public event EventHandler<PackageOperationMessageLoggedEventArgs> PackageOperationMessageLogged {
166			add { unsafeEvents.PackageOperationMessageLogged += value; }
167			remove { unsafeEvents.PackageOperationMessageLogged -= value; }
168		}
169		
170		public event EventHandler<SelectProjectsEventArgs> SelectProjects {
171			add { unsafeEvents.SelectProjects += value; }
172			remove { unsafeEvents.SelectProjects -= value; }
173		}
174		
175		public void OnPackageOperationsStarting()
176		{
177			unsafeEvents.OnPackageOperationsStarting();
178		}
179		
180		public void OnPackageOperationError(Exception ex)
181		{
182			unsafeEvents.OnPackageOperationError(ex);
183		}
184		
185		public bool OnAcceptLicenses(IEnumerable<IPackage> packages)
186		{
187			return unsafeEvents.OnAcceptLicenses(packages);
188		}
189		
190		public void OnParentPackageInstalled(IPackage package)
191		{
192			unsafeEvents.OnParentPackageInstalled(package);
193		}
194		
195		public void OnParentPackageUninstalled(IPackage package)
196		{
197			unsafeEvents.OnParentPackageUninstalled(package);
198		}
199		
200		public void OnPackageOperationMessageLogged(MessageLevel level, string message, params object[] args)
201		{
202			unsafeEvents.OnPackageOperationMessageLogged(level, message, args);
203		}
204		
205		public bool OnSelectProjects(IEnumerable<IPackageManagementSelectedProject> selectedProjects)
206		{
207			return unsafeEvents.OnSelectProjects(selectedProjects);
208		}
209		
210		public event EventHandler<ResolveFileConflictEventArgs> ResolveFileConflict {
211			add { unsafeEvents.ResolveFileConflict += value; }
212			remove { unsafeEvents.ResolveFileConflict -= value; }
213		}
214		
215		public FileConflictResolution OnResolveFileConflict(string message)
216		{
217			return unsafeEvents.OnResolveFileConflict(message);
218		}
219		
220		public event EventHandler<ParentPackagesOperationEventArgs> ParentPackagesUpdated;
221		
222		public void OnParentPackagesUpdated(IEnumerable<IPackage> packages)
223		{
224			unsafeEvents.OnParentPackagesUpdated(packages);
225		}
226		
227		void RaiseParentPackagesUpdatedEventIfHasSubscribers(object sender, ParentPackagesOperationEventArgs e)
228		{
229			if (ParentPackagesUpdated != null) {
230				RaiseParentPackagesUpdatedEvent(sender, e);
231			}
232		}
233		
234		void RaiseParentPackagesUpdatedEvent(object sender, ParentPackagesOperationEventArgs e)
235		{
236			if (InvokeRequired) {
237				Action<object, ParentPackagesOperationEventArgs> action = RaiseParentPackagesUpdatedEvent;
238				SafeThreadAsyncCall(action, sender, e);
239			} else {
240				ParentPackagesUpdated(sender, e);
241			}
242		}
243	}
244}