Skip to content

Commit

Permalink
(GH-7) First pass at creating export function
Browse files Browse the repository at this point in the history
- Using a simple PersistenceService, i.e. Open/Save Dialog
- Provide option to export the current list of installed packages to a packages.config file
  • Loading branch information
gep13 committed Mar 24, 2015
1 parent 9c709fe commit 62f933e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Source/ChocolateyGui/ChocolateyGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@
<Compile Include="Services\IBasePackageService.cs" />
<Compile Include="Services\IChocolateyPackageService.cs" />
<Compile Include="Services\ILogService.cs" />
<Compile Include="Services\IPersistenceService.cs" />
<Compile Include="Services\IProgressService.cs" />
<Compile Include="Services\ISourceService.cs" />
<Compile Include="Services\Log4NetLoggingService.cs" />
<Compile Include="Services\PackageServices\FileSystemPackageService.cs" />
<Compile Include="Services\PackageServices\ODataPackageService.cs" />
<Compile Include="Services\PersistenceService.cs" />
<Compile Include="Services\PowerShellChocolateyPackageService.cs" />
<Compile Include="Services\ProgressService.cs" />
<Compile Include="Services\SettingsSourceService.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/ChocolateyGui/IoC/AutoFacConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static IContainer RegisterAutoFac()
builder.RegisterType<NavigationService>().As<INavigationService>().SingleInstance();
builder.RegisterType<PackageService>().As<IPackageService>().SingleInstance();
builder.RegisterType<ProgressService>().As<IProgressService>().SingleInstance();
builder.RegisterType<PersistenceService>().As<IPersistenceService>().SingleInstance();

// Register Views
builder.RegisterType<MainWindow>();
Expand Down
17 changes: 17 additions & 0 deletions Source/ChocolateyGui/Services/IPersistenceService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="IPersistenceService.cs">
// Copyright 2014 - Present Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ChocolateyGui.Services
{
using System.IO;

public interface IPersistenceService
{
Stream OpenFile(string defaultExtension, string filter);

Stream SaveFile(string defaultExtension, string filter);
}
}
32 changes: 32 additions & 0 deletions Source/ChocolateyGui/Services/PersistenceService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="PersistenceService.cs">
// Copyright 2014 - Present Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ChocolateyGui.Services
{
using System.IO;
using Microsoft.Win32;

public class PersistenceService : IPersistenceService
{
public Stream OpenFile(string defaultExtension, string filter)
{
var fd = new OpenFileDialog { DefaultExt = defaultExtension, Filter = filter };

var result = fd.ShowDialog();

return result != null && result.Value ? fd.OpenFile() : null;
}

public Stream SaveFile(string defaultExtension, string filter)
{
var fd = new SaveFileDialog { DefaultExt = defaultExtension, Filter = filter };

var result = fd.ShowDialog();

return result != null && result.Value ? fd.OpenFile() : null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public interface ILocalSourceControlViewModel
ObservableCollection<IPackageViewModel> Packages { get; }

string SearchQuery { get; set; }

bool MatchWord { get; set; }

string SortColumn { get; set; }

bool SortDescending { get; set; }

void Loaded(object sender, EventArgs e);

bool CanUpdateAll();

void UpdateAll();
void UpdateAll();

void ExportAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ namespace ChocolateyGui.ViewModels.Controls
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ChocolateyGui.Base;
Expand All @@ -27,15 +29,21 @@ public class LocalSourceControlViewModel : ObservableBase, ILocalSourceControlVi
private readonly ILogService _logService;
private readonly List<IPackageViewModel> _packages;
private readonly IProgressService _progressService;
private readonly IPersistenceService _persistenceService;
private bool _hasLoaded;
private bool _exportAll = true;
private bool _matchWord;
private bool _showOnlyPackagesWithUpdate;
private ObservableCollection<IPackageViewModel> _packageViewModels;
private string _searchQuery;
private string _sortColumn;
private bool _sortDescending;

public LocalSourceControlViewModel(IChocolateyPackageService chocolateyService, IProgressService progressService, Func<Type, ILogService> logFactory)
public LocalSourceControlViewModel(
IChocolateyPackageService chocolateyService,
IProgressService progressService,
IPersistenceService persistenceService,
Func<Type, ILogService> logFactory)
{
if (logFactory == null)
{
Expand All @@ -44,6 +52,7 @@ public LocalSourceControlViewModel(IChocolateyPackageService chocolateyService,

this._chocolateyService = chocolateyService;
this._progressService = progressService;
this._persistenceService = persistenceService;
this._logService = logFactory(typeof(LocalSourceControlViewModel));
PackagesChangedEventManager.AddListener(this._chocolateyService, this);

Expand Down Expand Up @@ -108,6 +117,11 @@ public bool CanUpdateAll()
return this.Packages.Any(p => p.CanUpdate);
}

public bool CanExportAll()
{
return this._exportAll;
}

public bool CanRefreshPackages()
{
return this._hasLoaded;
Expand Down Expand Up @@ -183,6 +197,37 @@ public async void UpdateAll()
}
}

public async void ExportAll()
{
try
{
var fileStream = this._persistenceService.SaveFile("*.config", "Config Files (.config)|*.config");

if (fileStream == null)
{
return;
}

using (StreamWriter sw = new StreamWriter(fileStream, Encoding.ASCII))
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine("<packages>");

foreach (var package in this.Packages)
{
sw.WriteLine("{0}<package id=\"{1}\" version=\"{2}\" />", "\t", package.Id, package.Version);
}

sw.WriteLine("</packages>");
}
}
catch (Exception ex)
{
this._logService.Fatal("Export all has failed.", ex);
throw;
}
}

public async void RefreshPackages()
{
this._chocolateyService.ClearPackageCache();
Expand Down
3 changes: 2 additions & 1 deletion Source/ChocolateyGui/Views/Controls/LocalSourceControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
</StackPanel>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5">
<Button Command="{commands:DataContextCommandAdapter RefreshPackages, CanRefreshPackages}" Style="{StaticResource SquareButtonStyle}" Content="Refresh Packages"></Button>
<Button Command="{commands:DataContextCommandAdapter UpdateAll, CanUpdateAll}" Style="{StaticResource SquareButtonStyle}" Content="Update All"/>
<Button Command="{commands:DataContextCommandAdapter UpdateAll, CanUpdateAll}" Style="{StaticResource SquareButtonStyle}" Content="Update All"/>
<Button Command="{commands:DataContextCommandAdapter ExportAll, CanExportAll}" Style="{StaticResource SquareButtonStyle}" Content="Export"/>
</StackPanel>
<DataGrid Grid.Row="1" ItemsSource="{Binding Packages}" Background="{StaticResource LightBackgroundColorBrush}"
AutoGenerateColumns="False" IsReadOnly="True"
Expand Down

0 comments on commit 62f933e

Please sign in to comment.