PackageSourceViewModelCheckedEventArgs CheckPackageSourceUrl (PackageSourceViewModel packageSource)
		{
			if (IsHttpPackageSource (packageSource.SourceUrl)) {
				return CheckHttpPackageSource (packageSource);
			}
			return CheckFileSystemPackageSource (packageSource);
		}
		public void Check(PackageSourceViewModel packageSource)
		{
			ITask<PackageSourceViewModelCheckedEventArgs> task = taskFactory.CreateTask (
				() => CheckPackageSourceUrl (packageSource),
				(t) => OnPackageSourceChecked (this, t));

			tasks.Add (task);
			task.Start ();
		}
		PackageSourceViewModelCheckedEventArgs CheckHttpPackageSource (PackageSourceViewModel packageSource)
		{
			HttpClient httpClient = CreateHttpClient (packageSource);
			try {
				using (var response = (HttpWebResponse)httpClient.GetResponse ()) {
					if (response.StatusCode == HttpStatusCode.OK) {
						return new PackageSourceViewModelCheckedEventArgs (packageSource);
					} else {
						LoggingService.LogInfo ("Status code {0} returned from package source url '{1}'", response.StatusCode, packageSource.SourceUrl);
						return new PackageSourceViewModelCheckedEventArgs (packageSource, GettextCatalog.GetString ("Unreachable"));
					}
				}
			} catch (WebException ex) {
				return CreatePackageSourceViewModelCheckedEventArgs (packageSource, ex);
			} catch (Exception ex) {
				return new PackageSourceViewModelCheckedEventArgs (packageSource, ex.Message);
			}
		}
		public PackageSourceViewModelCheckedEventArgs (PackageSourceViewModel packageSource)
			: this (packageSource, "")
		{
		}
		void LogPackageSourceException (PackageSourceViewModel packageSource, Exception ex)
		{
			LoggingService.LogInfo (String.Format ("Package source '{0}' returned exception.", packageSource.SourceUrl), ex);
		}
		PackageSourceViewModelCheckedEventArgs CreatePackageSourceViewModelCheckedEventArgs (PackageSourceViewModel packageSource, WebException ex)
		{
			string errorMessage = ex.Message;
			var response = ex.Response as HttpWebResponse;
			if (response != null) {
				switch (response.StatusCode) {
				case HttpStatusCode.Unauthorized:
					errorMessage = GettextCatalog.GetString ("Invalid credentials");
					break;
				case HttpStatusCode.NotFound:
					errorMessage = GettextCatalog.GetString ("Not found");
					break;
				case HttpStatusCode.GatewayTimeout:
				case HttpStatusCode.RequestTimeout:
					errorMessage = GettextCatalog.GetString ("Unreachable");
					break;
				case HttpStatusCode.ProxyAuthenticationRequired:
					errorMessage = GettextCatalog.GetString ("Proxy authentication required");
					break;
				}
			}

			LogPackageSourceException (packageSource, ex);

			switch (ex.Status) {
			case WebExceptionStatus.ConnectFailure:
			case WebExceptionStatus.ConnectionClosed:
			case WebExceptionStatus.NameResolutionFailure:
			case WebExceptionStatus.ProxyNameResolutionFailure:
			case WebExceptionStatus.Timeout:
				errorMessage = GettextCatalog.GetString ("Unreachable");
				break;
			}

			return new PackageSourceViewModelCheckedEventArgs (packageSource, errorMessage);
		}
		PackageSourceViewModelCheckedEventArgs CheckFileSystemPackageSource (PackageSourceViewModel packageSource)
		{
			var dir = packageSource.SourceUrl;
			if (dir.StartsWith ("file://", StringComparison.OrdinalIgnoreCase)) {
				dir = new Uri (dir).LocalPath;
			}
			if (Directory.Exists (dir)) {
				return new PackageSourceViewModelCheckedEventArgs (packageSource);
			}
			return new PackageSourceViewModelCheckedEventArgs (packageSource, GettextCatalog.GetString ("Directory not found"));
		}
		public PackageSourceViewModelChangedEventArgs (PackageSourceViewModel packageSource)
		{
			PackageSource = packageSource;
		}
		void CreateViewModel(PackageSource packageSource)
		{
			viewModel = new PackageSourceViewModel(packageSource);
		}
		void AddPackageSourceToTreeView (PackageSourceViewModel packageSourceViewModel)
		{
			packageSourcesStore.AppendValues (
				null,
				packageSourceViewModel.IsEnabled,
				new IconId ("md-nuget-package-source"),
				packageSourceViewModel);
		}
		TreeIter GetTreeIter (PackageSourceViewModel packageSourceViewModel)
		{
			TreeIter foundIter = TreeIter.Zero;
			packageSourcesStore.Foreach ((model, path, iter) => {
				var currentViewModel = model.GetValue (iter, PackageSourceViewModelColumn) as PackageSourceViewModel;
				if (currentViewModel == packageSourceViewModel) {
					foundIter = iter;
					return true;
				}
				return false;
			});
			return foundIter;
		}
 string GetPackageSourceDescriptionMarkup(PackageSourceViewModel packageSourceViewModel)
 {
     string format = "{0}\n<span foreground='blue' underline='single'>{1}</span>";
     return MarkupString.Format (format, packageSourceViewModel.Name, packageSourceViewModel.SourceUrl);
 }
 void AddPackageSourceToTreeView(PackageSourceViewModel packageSourceViewModel)
 {
     packageSourcesStore.AppendValues (packageSourceViewModel.IsEnabled, GetPackageSourceDescriptionMarkup (packageSourceViewModel), packageSourceViewModel);
 }
 void CreateViewModel(PackageSource packageSource)
 {
     viewModel = new PackageSourceViewModel(packageSource);
 }
		public PackageSourceViewModelCheckedEventArgs (PackageSourceViewModel packageSource, string errorMessage)
		{
			PackageSource = packageSource;
			ValidationFailureMessage = errorMessage;
		}
		/// <summary>
		/// Do not use cached credentials for the first request sent to a package source.
		/// 
		/// Once NuGet has a valid credential for a package source it is used from that point
		/// onwards. So if the user changes a valid username/password in Preferences
		/// to a non-valid username/password they will not see the 'Invalid credentials'
		/// warning. To workaround this caching we remove the credentials for the request
		/// the first time it is sent. Also the UseDefaultCredentials is set to true.
		/// This forces NuGet to get the credentials from the IPackageSourceProvider,
		/// which is implemented by the RegisteredPackageSourcesViewModel, and will 
		/// contain the latest usernames and passwords for all package sources.
		/// </summary>
		HttpClient CreateHttpClient(PackageSourceViewModel packageSource)
		{
			var httpClient = new HttpClient (new Uri (packageSource.SourceUrl));

			bool resetCredentials = true;
			httpClient.SendingRequest += (sender, e) => {
				if (resetCredentials && packageSource.HasPassword ()) {
					resetCredentials = false;
					e.Request.Credentials = null;
					e.Request.UseDefaultCredentials = true;
				}
			};

			return httpClient;
		}
        public void Save_OnePackageSourceAddedAfterLoadAndBeforeSave_TwoRegisteredPackageSourcesInOptions()
        {
            CreateViewModel();
            AddPackageSourceToOptions("Test", "http://sharpdevelop.com/1");
            viewModel.Load();

            var newSource = new PackageSource("http://sharpdevelop.com/2", "Test");

            var newPackageSourceViewModel = new PackageSourceViewModel(newSource);
            viewModel.PackageSourceViewModels.Add(newPackageSourceViewModel);

            viewModel.Save();

            var expectedSource = new PackageSource("http://sharpdevelop.com/1", "Test");

            var expectedSources = new PackageSource[] {
                expectedSource,
                newSource
            };

            PackageSourceCollectionAssert.AreEqual(expectedSources, packageSources);
        }
		PackageSourceViewModelCheckedEventArgs CheckFileSystemPackageSource (PackageSourceViewModel packageSource)
		{
			if (Directory.Exists (packageSource.SourceUrl)) {
				return new PackageSourceViewModelCheckedEventArgs (packageSource);
			}
			return new PackageSourceViewModelCheckedEventArgs (packageSource, GettextCatalog.GetString ("Directory not found"));
		}