Skip to content

Commit fa71b86

Browse files
authored
Merge pull request #235 from Hirogen/plugin-config-for-portable-mode
Plugin config for portable mode
2 parents 728a081 + 1e8834e commit fa71b86

11 files changed

Lines changed: 105 additions & 70 deletions

File tree

src/LogExpert/Classes/Columnizer/ColumnizerPicker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ public static ILogLineColumnizer CloneColumnizer(ILogLineColumnizer columnizer)
4242
return null;
4343
}
4444
ConstructorInfo cti = columnizer.GetType().GetConstructor(Type.EmptyTypes);
45+
4546
if (cti != null)
4647
{
4748
object o = cti.Invoke(new object[] { });
48-
if (o is IColumnizerConfigurator)
49+
if (o is IColumnizerConfigurator configurator)
4950
{
50-
((IColumnizerConfigurator)o).LoadConfig(ConfigManager.ConfigDir);
51+
configurator.LoadConfig(ConfigManager.Settings.preferences.PortableMode ? ConfigManager.PortableModeDir : ConfigManager.ConfigDir);
5152
}
5253
return (ILogLineColumnizer)o;
5354
}

src/LogExpert/Classes/Persister/Persister.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static string BuildPersisterFileName(string logFileName, Preferences pre
176176
}
177177
case SessionSaveLocation.ApplicationStartupDir:
178178
{
179-
dir = Application.StartupPath;
179+
dir = Application.StartupPath + Path.DirectorySeparatorChar + "sessionfiles";
180180
file = dir + Path.DirectorySeparatorChar + BuildSessionFileNameFromPath(logFileName);
181181
break;
182182
}

src/LogExpert/Classes/PluginRegistry.cs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public class PluginRegistry
2424

2525
private static readonly ILogger _logger = LogManager.GetCurrentClassLogger();
2626

27-
private static readonly object lockObject = new object();
28-
private static PluginRegistry instance = null;
27+
private static readonly object _lockObject = new object();
28+
private static PluginRegistry _instance;
2929

30-
private readonly IFileSystemCallback fileSystemCallback = new FileSystemCallback();
31-
private readonly IList<ILogExpertPlugin> pluginList = new List<ILogExpertPlugin>();
30+
private readonly IFileSystemCallback _fileSystemCallback = new FileSystemCallback();
31+
private readonly IList<ILogExpertPlugin> _pluginList = new List<ILogExpertPlugin>();
3232

33-
private readonly IDictionary<string, IKeywordAction> registeredKeywordsDict =
34-
new Dictionary<string, IKeywordAction>();
33+
private readonly IDictionary<string, IKeywordAction> _registeredKeywordsDict = new Dictionary<string, IKeywordAction>();
3534

3635
#endregion
3736

@@ -60,14 +59,14 @@ private PluginRegistry()
6059

6160
public static PluginRegistry GetInstance()
6261
{
63-
lock (lockObject)
62+
lock (_lockObject)
6463
{
65-
if (instance == null)
64+
if (_instance == null)
6665
{
67-
instance = new PluginRegistry();
66+
_instance = new PluginRegistry();
6867
}
6968

70-
return instance;
69+
return _instance;
7170
}
7271
}
7372

@@ -78,16 +77,19 @@ public static PluginRegistry GetInstance()
7877
internal void LoadPlugins()
7978
{
8079
_logger.Info("Loading plugins...");
81-
this.RegisteredColumnizers = new List<ILogLineColumnizer>();
82-
this.RegisteredColumnizers.Add(new DefaultLogfileColumnizer());
83-
this.RegisteredColumnizers.Add(new TimestampColumnizer());
84-
this.RegisteredColumnizers.Add(new SquareBracketColumnizer());
85-
this.RegisteredColumnizers.Add(new ClfColumnizer());
86-
this.RegisteredFileSystemPlugins.Add(new LocalFileSystem());
80+
81+
RegisteredColumnizers = new List<ILogLineColumnizer>();
82+
RegisteredColumnizers.Add(new DefaultLogfileColumnizer());
83+
RegisteredColumnizers.Add(new TimestampColumnizer());
84+
RegisteredColumnizers.Add(new SquareBracketColumnizer());
85+
RegisteredColumnizers.Add(new ClfColumnizer());
86+
RegisteredFileSystemPlugins.Add(new LocalFileSystem());
8787

8888
string pluginDir = Application.StartupPath + Path.DirectorySeparatorChar + "plugins";
89+
8990
AppDomain currentDomain = AppDomain.CurrentDomain;
90-
currentDomain.AssemblyResolve += new ResolveEventHandler(ColumnizerResolveEventHandler);
91+
currentDomain.AssemblyResolve += ColumnizerResolveEventHandler;
92+
9193
if (Directory.Exists(pluginDir))
9294
{
9395
string[] dllNames = Directory.GetFiles(pluginDir, "*.dll");
@@ -119,16 +121,17 @@ internal void LoadPlugins()
119121
if (cti != null)
120122
{
121123
object o = cti.Invoke(new object[] { });
122-
this.RegisteredColumnizers.Add((ILogLineColumnizer) o);
123-
if (o is IColumnizerConfigurator)
124+
RegisteredColumnizers.Add((ILogLineColumnizer) o);
125+
126+
if (o is IColumnizerConfigurator configurator)
124127
{
125-
((IColumnizerConfigurator) o).LoadConfig(ConfigManager.ConfigDir);
128+
configurator.LoadConfig(ConfigManager.Settings.preferences.PortableMode ? ConfigManager.PortableModeDir : ConfigManager.ConfigDir);
126129
}
127130

128-
if (o is ILogExpertPlugin)
131+
if (o is ILogExpertPlugin plugin)
129132
{
130-
this.pluginList.Add(o as ILogExpertPlugin);
131-
(o as ILogExpertPlugin).PluginLoaded();
133+
_pluginList.Add(plugin);
134+
plugin.PluginLoaded();
132135
}
133136

134137
_logger.Info("Added columnizer {0}", type.Name);
@@ -188,13 +191,13 @@ internal void LoadPlugins()
188191
internal IKeywordAction FindKeywordActionPluginByName(string name)
189192
{
190193
IKeywordAction action = null;
191-
this.registeredKeywordsDict.TryGetValue(name, out action);
194+
_registeredKeywordsDict.TryGetValue(name, out action);
192195
return action;
193196
}
194197

195198
internal void CleanupPlugins()
196199
{
197-
foreach (ILogExpertPlugin plugin in this.pluginList)
200+
foreach (ILogExpertPlugin plugin in _pluginList)
198201
{
199202
plugin.AppExiting();
200203
}
@@ -207,7 +210,7 @@ internal IFileSystemPlugin FindFileSystemForUri(string uriString)
207210
_logger.Debug("Trying to find file system plugin for uri {0}", uriString);
208211
}
209212

210-
foreach (IFileSystemPlugin fs in this.RegisteredFileSystemPlugins)
213+
foreach (IFileSystemPlugin fs in RegisteredFileSystemPlugins)
211214
{
212215
if (_logger.IsDebugEnabled)
213216
{
@@ -238,15 +241,15 @@ private bool TryAsContextMenu(Type type)
238241
IContextMenuEntry me = TryInstantiate<IContextMenuEntry>(type);
239242
if (me != null)
240243
{
241-
this.RegisteredContextMenuPlugins.Add(me);
244+
RegisteredContextMenuPlugins.Add(me);
242245
if (me is ILogExpertPluginConfigurator)
243246
{
244247
((ILogExpertPluginConfigurator) me).LoadConfig(ConfigManager.ConfigDir);
245248
}
246249

247250
if (me is ILogExpertPlugin)
248251
{
249-
this.pluginList.Add(me as ILogExpertPlugin);
252+
_pluginList.Add(me as ILogExpertPlugin);
250253
(me as ILogExpertPlugin).PluginLoaded();
251254
}
252255

@@ -262,16 +265,16 @@ private bool TryAsKeywordAction(Type type)
262265
IKeywordAction ka = TryInstantiate<IKeywordAction>(type);
263266
if (ka != null)
264267
{
265-
this.RegisteredKeywordActions.Add(ka);
266-
this.registeredKeywordsDict.Add(ka.GetName(), ka);
268+
RegisteredKeywordActions.Add(ka);
269+
_registeredKeywordsDict.Add(ka.GetName(), ka);
267270
if (ka is ILogExpertPluginConfigurator)
268271
{
269272
((ILogExpertPluginConfigurator) ka).LoadConfig(ConfigManager.ConfigDir);
270273
}
271274

272275
if (ka is ILogExpertPlugin)
273276
{
274-
this.pluginList.Add(ka as ILogExpertPlugin);
277+
_pluginList.Add(ka as ILogExpertPlugin);
275278
(ka as ILogExpertPlugin).PluginLoaded();
276279
}
277280

@@ -285,23 +288,23 @@ private bool TryAsKeywordAction(Type type)
285288
private bool TryAsFileSystem(Type type)
286289
{
287290
// file system plugins can have optional constructor with IFileSystemCallback argument
288-
IFileSystemPlugin fs = TryInstantiate<IFileSystemPlugin>(type, this.fileSystemCallback);
291+
IFileSystemPlugin fs = TryInstantiate<IFileSystemPlugin>(type, _fileSystemCallback);
289292
if (fs == null)
290293
{
291294
fs = TryInstantiate<IFileSystemPlugin>(type);
292295
}
293296

294297
if (fs != null)
295298
{
296-
this.RegisteredFileSystemPlugins.Add(fs);
299+
RegisteredFileSystemPlugins.Add(fs);
297300
if (fs is ILogExpertPluginConfigurator)
298301
{
299302
((ILogExpertPluginConfigurator) fs).LoadConfig(ConfigManager.ConfigDir);
300303
}
301304

302305
if (fs is ILogExpertPlugin)
303306
{
304-
this.pluginList.Add(fs as ILogExpertPlugin);
307+
_pluginList.Add(fs as ILogExpertPlugin);
305308
(fs as ILogExpertPlugin).PluginLoaded();
306309
}
307310

src/LogExpert/Config/ConfigManager.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ public static ConfigManager Instance
6363
public static string ConfigDir => Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "LogExpert";
6464

6565
/// <summary>
66-
/// Application.StartupPath + portableMode.json
66+
/// Application.StartupPath + portable
6767
/// </summary>
68-
public static string PortableMode => Application.StartupPath + Path.DirectorySeparatorChar + "portableMode.json";
68+
public static string PortableModeDir => Application.StartupPath + Path.DirectorySeparatorChar + "portable";
69+
70+
/// <summary>
71+
/// portableMode.json
72+
/// </summary>
73+
public static string PortableModeSettingsFileName => "portableMode.json";
6974

7075
public static Settings Settings => Instance._settings;
7176

@@ -99,7 +104,7 @@ private Settings Load()
99104

100105
string dir;
101106

102-
if (!File.Exists(PortableMode))
107+
if (File.Exists(PortableModeDir + Path.DirectorySeparatorChar + PortableModeSettingsFileName) == false)
103108
{
104109
_logger.Info("Load settings standard mode");
105110
dir = ConfigDir;
@@ -294,7 +299,7 @@ private void Save(Settings settings, SettingsFlags flags)
294299
_logger.Info("Saving settings");
295300
lock (this)
296301
{
297-
string dir = File.Exists(PortableMode) ? Application.StartupPath : ConfigDir;
302+
string dir = Settings.preferences.PortableMode ? Application.StartupPath : ConfigDir;
298303

299304
if (!Directory.Exists(dir))
300305
{

src/LogExpert/Config/Preferences.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public class Preferences
6060
public int pollingInterval = 250;
6161

6262
public bool reverseAlpha = false;
63-
63+
64+
public bool PortableMode { get; set; }
65+
6466
/// <summary>
6567
/// Save Directory of the last logfile
6668
/// </summary>
@@ -75,7 +77,7 @@ public class Preferences
7577
public bool setLastColumnWidth;
7678

7779
public bool showBubbles = true;
78-
80+
7981
public bool showColumnFinder;
8082

8183
public Color showTailColor = Color.FromKnownColor(KnownColor.Blue);

src/LogExpert/Dialogs/FilterSelectorForm.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LogExpert/Dialogs/FilterSelectorForm.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Windows.Forms;
44
using LogExpert.Config;
5-
// using System.Linq;
65

76
namespace LogExpert.Dialogs
87
{
@@ -22,7 +21,7 @@ public FilterSelectorForm(IList<ILogLineColumnizer> existingColumnizerList, ILog
2221
SelectedColumnizer = currentColumnizer;
2322
_callback = callback;
2423
InitializeComponent();
25-
filterComboBox.SelectedIndexChanged += filterComboBox_SelectedIndexChanged;
24+
filterComboBox.SelectedIndexChanged += OnFilterComboBoxSelectedIndexChanged;
2625

2726
// for the currently selected columnizer use the current instance and not the template instance from
2827
// columnizer registry. This ensures that changes made in columnizer config dialogs
@@ -62,7 +61,7 @@ public FilterSelectorForm(IList<ILogLineColumnizer> existingColumnizerList, ILog
6261

6362
#region Events handler
6463

65-
private void filterComboBox_SelectedIndexChanged(object sender, EventArgs e)
64+
private void OnFilterComboBoxSelectedIndexChanged(object sender, EventArgs e)
6665
{
6766
ILogLineColumnizer col = _columnizerList[filterComboBox.SelectedIndex];
6867
SelectedColumnizer = col;
@@ -73,11 +72,18 @@ private void filterComboBox_SelectedIndexChanged(object sender, EventArgs e)
7372
}
7473

7574

76-
private void configButton_Click(object sender, EventArgs e)
75+
private void OnConfigButtonClick(object sender, EventArgs e)
7776
{
78-
if (SelectedColumnizer is IColumnizerConfigurator)
77+
if (SelectedColumnizer is IColumnizerConfigurator configurator)
7978
{
80-
((IColumnizerConfigurator) SelectedColumnizer).Configure(_callback, ConfigManager.ConfigDir);
79+
string configDir = ConfigManager.ConfigDir;
80+
81+
if (ConfigManager.Settings.preferences.PortableMode)
82+
{
83+
configDir = ConfigManager.PortableModeDir;
84+
}
85+
86+
configurator.Configure(_callback, configDir);
8187
IsConfigPressed = true;
8288
}
8389
}

src/LogExpert/Dialogs/SettingsDialog.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void FillDialog()
169169

170170
private void FillPortableMode()
171171
{
172-
checkBoxPortableMode.CheckState = File.Exists(ConfigManager.PortableMode) ? CheckState.Checked : CheckState.Unchecked;
172+
checkBoxPortableMode.CheckState = Preferences.PortableMode ? CheckState.Checked : CheckState.Unchecked;
173173
}
174174

175175
private void DisplayFontName()
@@ -446,15 +446,15 @@ private void SavePluginSettings()
446446
{
447447
if (entry is ILogExpertPluginConfigurator configurator)
448448
{
449-
configurator.SaveConfig(ConfigManager.ConfigDir);
449+
configurator.SaveConfig(checkBoxPortableMode.Checked ? ConfigManager.PortableModeDir : ConfigManager.ConfigDir);
450450
}
451451
}
452452

453453
foreach (IKeywordAction entry in PluginRegistry.GetInstance().RegisteredKeywordActions)
454454
{
455455
if (entry is ILogExpertPluginConfigurator configurator)
456456
{
457-
configurator.SaveConfig(ConfigManager.ConfigDir);
457+
configurator.SaveConfig(checkBoxPortableMode.Checked ? ConfigManager.PortableModeDir : ConfigManager.ConfigDir);
458458
}
459459
}
460460
}
@@ -789,26 +789,38 @@ private void OnPortableModeCheckedChanged(object sender, EventArgs e)
789789
{
790790
switch (checkBoxPortableMode.CheckState)
791791
{
792-
case CheckState.Checked when !File.Exists(ConfigManager.PortableMode):
792+
case CheckState.Checked when !File.Exists(ConfigManager.PortableModeDir + Path.DirectorySeparatorChar + ConfigManager.PortableModeSettingsFileName):
793793
{
794-
using (File.Create(ConfigManager.PortableMode))
794+
if (Directory.Exists(ConfigManager.PortableModeDir) == false)
795+
{
796+
Directory.CreateDirectory(ConfigManager.PortableModeDir);
797+
}
798+
799+
using (File.Create(ConfigManager.PortableModeDir + Path.DirectorySeparatorChar + ConfigManager.PortableModeSettingsFileName))
795800
break;
796801
}
797-
case CheckState.Unchecked when File.Exists(ConfigManager.PortableMode):
802+
case CheckState.Unchecked when File.Exists(ConfigManager.PortableModeDir + Path.DirectorySeparatorChar + ConfigManager.PortableModeSettingsFileName):
798803
{
799-
File.Delete(ConfigManager.PortableMode);
804+
File.Delete(ConfigManager.PortableModeDir + Path.DirectorySeparatorChar + ConfigManager.PortableModeSettingsFileName);
800805
break;
801806
}
802807
}
803808

804809
switch (checkBoxPortableMode.CheckState)
805810
{
806811
case CheckState.Unchecked:
812+
{
807813
checkBoxPortableMode.Text = @"Activate Portable Mode";
814+
Preferences.PortableMode = false;
808815
break;
816+
}
817+
809818
case CheckState.Checked:
819+
{
820+
Preferences.PortableMode = true;
810821
checkBoxPortableMode.Text = @"Deactivate Portable Mode";
811822
break;
823+
}
812824
}
813825
}
814826
catch (Exception exception)

0 commit comments

Comments
 (0)