3
3
using Rubberduck . Settings ;
4
4
using Rubberduck . Common ;
5
5
using NLog ;
6
+ using Rubberduck . SettingsProvider ;
6
7
using Rubberduck . UI . Command ;
7
8
8
9
namespace Rubberduck . UI . Settings
@@ -13,7 +14,7 @@ public enum DelimiterOptions
13
14
Slash = 47
14
15
}
15
16
16
- public class GeneralSettingsViewModel : ViewModelBase , ISettingsViewModel
17
+ public class GeneralSettingsViewModel : SettingsViewModelBase , ISettingsViewModel
17
18
{
18
19
private readonly IOperatingSystem _operatingSystem ;
19
20
@@ -28,16 +29,12 @@ public GeneralSettingsViewModel(Configuration config, IOperatingSystem operating
28
29
new DisplayLanguageSetting ( "de-DE" )
29
30
} ) ;
30
31
31
- SelectedLanguage = Languages . First ( l => l . Code == config . UserSettings . GeneralSettings . Language . Code ) ;
32
- Hotkeys = new ObservableCollection < HotkeySetting > ( config . UserSettings . HotkeySettings . Settings ) ;
33
- ShowSplashAtStartup = config . UserSettings . GeneralSettings . ShowSplash ;
34
- AutoSaveEnabled = config . UserSettings . GeneralSettings . AutoSaveEnabled ;
35
- AutoSavePeriod = config . UserSettings . GeneralSettings . AutoSavePeriod ;
36
- Delimiter = ( DelimiterOptions ) config . UserSettings . GeneralSettings . Delimiter ;
37
32
LogLevels = new ObservableCollection < MinimumLogLevel > ( LogLevelHelper . LogLevels . Select ( l => new MinimumLogLevel ( l . Ordinal , l . Name ) ) ) ;
38
- SelectedLogLevel = LogLevels . First ( l => l . Ordinal == config . UserSettings . GeneralSettings . MinimumLogLevel ) ;
33
+ TransferSettingsToView ( config . UserSettings . GeneralSettings , config . UserSettings . HotkeySettings ) ;
39
34
40
35
_showLogFolderCommand = new DelegateCommand ( LogManager . GetCurrentClassLogger ( ) , _ => ShowLogFolder ( ) ) ;
36
+ ExportButtonCommand = new DelegateCommand ( LogManager . GetCurrentClassLogger ( ) , _ => ExportSettings ( ) ) ;
37
+ ImportButtonCommand = new DelegateCommand ( LogManager . GetCurrentClassLogger ( ) , _ => ImportSettings ( ) ) ;
41
38
}
42
39
43
40
public ObservableCollection < DisplayLanguageSetting > Languages { get ; set ; }
@@ -154,24 +151,72 @@ private void ShowLogFolder()
154
151
155
152
public void UpdateConfig ( Configuration config )
156
153
{
157
- config . UserSettings . GeneralSettings . Language = SelectedLanguage ;
154
+ config . UserSettings . GeneralSettings = GetCurrentGeneralSettings ( ) ;
158
155
config . UserSettings . HotkeySettings . Settings = Hotkeys . ToArray ( ) ;
159
- config . UserSettings . GeneralSettings . ShowSplash = ShowSplashAtStartup ;
160
- config . UserSettings . GeneralSettings . AutoSaveEnabled = AutoSaveEnabled ;
161
- config . UserSettings . GeneralSettings . AutoSavePeriod = AutoSavePeriod ;
162
- config . UserSettings . GeneralSettings . Delimiter = ( char ) Delimiter ;
163
- config . UserSettings . GeneralSettings . MinimumLogLevel = SelectedLogLevel . Ordinal ;
164
156
}
165
157
166
158
public void SetToDefaults ( Configuration config )
167
159
{
168
- SelectedLanguage = Languages . First ( l => l . Code == config . UserSettings . GeneralSettings . Language . Code ) ;
169
- Hotkeys = new ObservableCollection < HotkeySetting > ( config . UserSettings . HotkeySettings . Settings ) ;
170
- ShowSplashAtStartup = config . UserSettings . GeneralSettings . ShowSplash ;
171
- AutoSaveEnabled = config . UserSettings . GeneralSettings . AutoSaveEnabled ;
172
- AutoSavePeriod = config . UserSettings . GeneralSettings . AutoSavePeriod ;
173
- Delimiter = ( DelimiterOptions ) config . UserSettings . GeneralSettings . Delimiter ;
174
- SelectedLogLevel = LogLevels . First ( l => l . Ordinal == config . UserSettings . GeneralSettings . MinimumLogLevel ) ;
160
+ TransferSettingsToView ( config . UserSettings . GeneralSettings , config . UserSettings . HotkeySettings ) ;
161
+ }
162
+
163
+ private Rubberduck . Settings . GeneralSettings GetCurrentGeneralSettings ( )
164
+ {
165
+ return new Rubberduck . Settings . GeneralSettings
166
+ {
167
+ Language = SelectedLanguage ,
168
+ ShowSplash = ShowSplashAtStartup ,
169
+ AutoSaveEnabled = AutoSaveEnabled ,
170
+ AutoSavePeriod = AutoSavePeriod ,
171
+ Delimiter = ( char ) Delimiter ,
172
+ MinimumLogLevel = SelectedLogLevel . Ordinal
173
+ } ;
174
+ }
175
+
176
+ private void TransferSettingsToView ( IGeneralSettings general , IHotkeySettings hottkey )
177
+ {
178
+ SelectedLanguage = Languages . First ( l => l . Code == general . Language . Code ) ;
179
+ Hotkeys = new ObservableCollection < HotkeySetting > ( hottkey . Settings ) ;
180
+ ShowSplashAtStartup = general . ShowSplash ;
181
+ AutoSaveEnabled = general . AutoSaveEnabled ;
182
+ AutoSavePeriod = general . AutoSavePeriod ;
183
+ Delimiter = ( DelimiterOptions ) general . Delimiter ;
184
+ SelectedLogLevel = LogLevels . First ( l => l . Ordinal == general . MinimumLogLevel ) ;
185
+ }
186
+
187
+ private void ImportSettings ( )
188
+ {
189
+ using ( var dialog = new OpenFileDialog
190
+ {
191
+ Filter = RubberduckUI . DialogMask_XmlFilesOnly ,
192
+ Title = RubberduckUI . DialogCaption_LoadGeneralSettings
193
+ } )
194
+ {
195
+ dialog . ShowDialog ( ) ;
196
+ if ( string . IsNullOrEmpty ( dialog . FileName ) ) return ;
197
+ var service = new XmlPersistanceService < Rubberduck . Settings . GeneralSettings > { FilePath = dialog . FileName } ;
198
+ var general = service . Load ( new Rubberduck . Settings . GeneralSettings ( ) ) ;
199
+ var hkService = new XmlPersistanceService < HotkeySettings > { FilePath = dialog . FileName } ;
200
+ var hotkey = hkService . Load ( new HotkeySettings ( ) ) ;
201
+ TransferSettingsToView ( general , hotkey ) ;
202
+ }
203
+ }
204
+
205
+ private void ExportSettings ( )
206
+ {
207
+ using ( var dialog = new SaveFileDialog
208
+ {
209
+ Filter = RubberduckUI . DialogMask_XmlFilesOnly ,
210
+ Title = RubberduckUI . DialogCaption_SaveGeneralSettings
211
+ } )
212
+ {
213
+ dialog . ShowDialog ( ) ;
214
+ if ( string . IsNullOrEmpty ( dialog . FileName ) ) return ;
215
+ var service = new XmlPersistanceService < Rubberduck . Settings . GeneralSettings > { FilePath = dialog . FileName } ;
216
+ service . Save ( GetCurrentGeneralSettings ( ) ) ;
217
+ var hkService = new XmlPersistanceService < HotkeySettings > { FilePath = dialog . FileName } ;
218
+ hkService . Save ( new HotkeySettings { Settings = Hotkeys . ToArray ( ) } ) ;
219
+ }
175
220
}
176
221
}
177
222
}
0 commit comments