@@ -89,12 +89,23 @@ public static void Export(FileInfo fileInfo)
8989 Instance . Save ( fileInfo , Settings ) ;
9090 }
9191
92+ public static void Export ( FileInfo fileInfo , SettingsFlags flags )
93+ {
94+ Instance . Save ( fileInfo , Settings , flags ) ;
95+ }
96+
9297 public static void Import ( FileInfo fileInfo , ExportImportFlags flags )
9398 {
9499 Instance . _settings = Instance . Import ( Instance . _settings , fileInfo , flags ) ;
95100 Save ( SettingsFlags . All ) ;
96101 }
97102
103+ public static void ImportHighlightSettings ( FileInfo fileInfo , ExportImportFlags flags )
104+ {
105+ Instance . _settings . Preferences . HighlightGroupList = Instance . Import ( Instance . _settings . Preferences . HighlightGroupList , fileInfo , flags ) ;
106+ Save ( SettingsFlags . All ) ;
107+ }
108+
98109 #endregion
99110
100111 #region Private Methods
@@ -144,7 +155,7 @@ private Settings Load()
144155 /// </summary>
145156 /// <param name="fileInfo">file that has settings saved</param>
146157 /// <returns>loaded or created settings</returns>
147- private Settings LoadOrCreateNew ( FileSystemInfo fileInfo )
158+ private Settings LoadOrCreateNew ( FileInfo fileInfo )
148159 {
149160 lock ( _loadSaveLock )
150161 {
@@ -212,17 +223,9 @@ private Settings LoadOrCreateNew(FileSystemInfo fileInfo)
212223 filterParams . Init ( ) ;
213224 }
214225
215- if ( settings . hilightGroupList == null )
226+ if ( settings . Preferences . HighlightGroupList == null )
216227 {
217- settings . hilightGroupList = [ ] ;
218- // migrate old non-grouped entries
219- HilightGroup defaultGroup = new ( )
220- {
221- GroupName = "[Default]" ,
222- HilightEntryList = settings . hilightEntryList
223- } ;
224-
225- settings . hilightGroupList . Add ( defaultGroup ) ;
228+ settings . Preferences . HighlightGroupList = [ ] ;
226229 }
227230
228231 settings . Preferences . highlightMaskList ??= [ ] ;
@@ -248,8 +251,6 @@ private Settings LoadOrCreateNew(FileSystemInfo fileInfo)
248251
249252 SetBoundsWithinVirtualScreen ( settings ) ;
250253
251- ConvertSettings ( settings ) ;
252-
253254 return settings ;
254255 }
255256 }
@@ -292,7 +293,19 @@ private void Save(FileInfo fileInfo, Settings settings)
292293 SaveAsJSON ( fileInfo , settings ) ;
293294 }
294295
295- private void SaveAsJSON ( FileInfo fileInfo , Settings settings )
296+ private void Save ( FileInfo fileInfo , Settings settings , SettingsFlags flags )
297+ {
298+ switch ( flags )
299+ {
300+ case SettingsFlags . HighlightSettings :
301+ SaveHighlightgroupsAsJSON ( fileInfo , settings . Preferences . HighlightGroupList ) ;
302+ break ;
303+ }
304+
305+ OnConfigChanged ( flags ) ;
306+ }
307+
308+ private static void SaveAsJSON ( FileInfo fileInfo , Settings settings )
296309 {
297310 settings . versionBuild = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . Build ;
298311
@@ -301,55 +314,39 @@ private void SaveAsJSON(FileInfo fileInfo, Settings settings)
301314 serializer . Serialize ( sw , settings ) ;
302315 }
303316
304- /// <summary>
305- /// Convert settings loaded from previous versions.
306- /// </summary>
307- /// <param name="settings"></param>
308- private void ConvertSettings ( Settings settings )
317+ private static void SaveHighlightgroupsAsJSON ( FileInfo fileInfo , List < HighlightGroup > groups )
309318 {
310- //int oldBuildNumber = settings.versionBuild;
311-
312- //// All Versions before 3583
313- //if (oldBuildNumber < 3584)
314- //{
315- // // External tools
316- // List<ToolEntry> newList = [];
317- // foreach (ToolEntry tool in settings.preferences.toolEntries)
318- // {
319- // // set favourite to true only when name is empty, because there are always version released without this conversion fx
320- // // remove empty tool entries (there were always 3 entries before, which can be empty if not used)
321- // if (Util.IsNull(tool.name))
322- // {
323- // if (!Util.IsNull(tool.cmd))
324- // {
325- // tool.name = tool.cmd;
326- // tool.isFavourite = true;
327- // newList.Add(tool);
328- // }
329- // }
330- // else
331- // {
332- // newList.Add(tool);
333- // }
334- // if (Util.IsNull(tool.iconFile))
335- // {
336- // tool.iconFile = tool.cmd;
337- // tool.iconIndex = 0;
338- // }
339- // }
340- // settings.preferences.toolEntries = newList;
341- //}
342-
343- //if (oldBuildNumber < 3584)
344- //{
345- // // Set the color for the FilterList entries to default (black)
346- // foreach (FilterParams filterParam in settings.filterList)
347- // {
348- // filterParam.color = Color.FromKnownColor(KnownColor.Black);
349- // }
350- //}
319+ using StreamWriter sw = new ( fileInfo . Create ( ) ) ;
320+ JsonSerializer serializer = new ( ) ;
321+ serializer . Serialize ( sw , groups ) ;
351322 }
352323
324+ private List < HighlightGroup > Import ( List < HighlightGroup > currentGroups , FileInfo fileInfo , ExportImportFlags flags )
325+ {
326+ List < HighlightGroup > newGroups ;
327+
328+ try
329+ {
330+ newGroups = JsonConvert . DeserializeObject < List < HighlightGroup > > ( File . ReadAllText ( $ "{ fileInfo . FullName } ") ) ;
331+ }
332+ catch ( Exception e )
333+ {
334+ _logger . Error ( $ "Error while deserializing config data: { e } ") ;
335+ newGroups = [ ] ;
336+ }
337+
338+ if ( flags . HasFlag ( ExportImportFlags . KeepExisting ) )
339+ {
340+ currentGroups . AddRange ( newGroups ) ;
341+ }
342+ else
343+ {
344+ currentGroups . Clear ( ) ;
345+ currentGroups . AddRange ( newGroups ) ;
346+ }
347+
348+ return currentGroups ;
349+ }
353350
354351 /// <summary>
355352 /// Imports all or some of the settings/prefs stored in the input stream.
@@ -371,7 +368,7 @@ private Settings Import(Settings currentSettings, FileInfo fileInfo, ExportImpor
371368 newSettings . Preferences = ObjectClone . Clone ( importSettings . Preferences ) ;
372369 newSettings . Preferences . columnizerMaskList = ownSettings . Preferences . columnizerMaskList ;
373370 newSettings . Preferences . highlightMaskList = ownSettings . Preferences . highlightMaskList ;
374- newSettings . hilightGroupList = ownSettings . hilightGroupList ;
371+ newSettings . Preferences . HighlightGroupList = ownSettings . Preferences . HighlightGroupList ;
375372 newSettings . Preferences . toolEntries = ownSettings . Preferences . toolEntries ;
376373 }
377374 else
@@ -389,7 +386,7 @@ private Settings Import(Settings currentSettings, FileInfo fileInfo, ExportImpor
389386 }
390387 if ( ( flags & ExportImportFlags . HighlightSettings ) == ExportImportFlags . HighlightSettings )
391388 {
392- newSettings . hilightGroupList = ReplaceOrKeepExisting ( flags , ownSettings . hilightGroupList , importSettings . hilightGroupList ) ;
389+ newSettings . Preferences . HighlightGroupList = ReplaceOrKeepExisting ( flags , ownSettings . Preferences . HighlightGroupList , importSettings . Preferences . HighlightGroupList ) ;
393390 }
394391 if ( ( flags & ExportImportFlags . ToolEntries ) == ExportImportFlags . ToolEntries )
395392 {
@@ -424,13 +421,7 @@ private void SetBoundsWithinVirtualScreen(Settings settings)
424421
425422 protected void OnConfigChanged ( SettingsFlags flags )
426423 {
427- ConfigChangedEventHandler handler = ConfigChanged ;
428-
429- if ( handler != null )
430- {
431- _logger . Info ( "Fire config changed event" ) ;
432- handler ( this , new ConfigChangedEventArgs ( flags ) ) ;
433- }
424+ ConfigChanged ? . Invoke ( this , new ConfigChangedEventArgs ( flags ) ) ;
434425 }
435426
436427 public delegate void ConfigChangedEventHandler ( object sender , ConfigChangedEventArgs e ) ;
0 commit comments