lantool

ein feines Tool für LANs (damals)
git clone https://git.clttr.info/lantool.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

Config.cs (15336B)


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Diagnostics;
      4 using System.IO;
      5 using System.Windows.Forms;
      6 
      7 namespace LanTool.Classes
      8 {
      9 	/// <summary>
     10 	/// Enthält alle über die Konfiguration steuerbaren Parameter des LanTools
     11 	/// </summary>
     12 	public class Config
     13 	{
     14 		#region Konstanten für die Konfiguration
     15 
     16 		/// Name der Configdatei
     17 		private const string _configFile = "LanTool.cfg";
     18 
     19 		#endregion
     20 
     21 		#region Fields für Configsettings
     22 
     23 		private List<string> _excludeExtension = new List<string>();
     24 		private List<string> _restrictExtension = new List<string>();
     25 		private int _md5FileSize = 10000;
     26 		private bool _autoScanMyDisks = false;
     27 		private bool _autoScanOtherDisks = false;
     28 		private bool _showIgnoredClones = false;
     29 		private bool _hideExcludedFiles = false;
     30 		private bool _hideExistingFiles = false;
     31 		private bool _removeMarks = false;
     32 		private bool _treatEqualNames = false;
     33 		private bool _lowerPriority = false;
     34 		private string _defaultOfflineFile = String.Empty;
     35 		private string _basePath;
     36 		private string _headerExtension = String.Empty;
     37 
     38 		#endregion
     39 
     40 		/// <summary>
     41 		/// Erstellt eine neue Instanz der Konfiguration
     42 		/// </summary>
     43 		/// <param name="basePath">Pfad zur LanTool-Executable, ist auch der Pfad zur Konfig-Datei</param>
     44 		public Config( string basePath )
     45 		{
     46 			this._basePath = basePath;
     47 
     48 			ReadConfig();
     49 		}
     50 
     51 		#region Properties für die Zugriffe auf die Konfiguration
     52 
     53 		/// <summary>
     54 		/// Aktuelles Md5FileSize, Anzahl Bytes, die für die Hashwertberechnung genommen werden
     55 		/// </summary>
     56 		public string HeaderExtension
     57 		{
     58 			get
     59 			{
     60 				return _headerExtension;
     61 			}
     62 		}
     63 
     64 		/// <summary>
     65 		/// Aktuelles Md5FileSize, Anzahl Bytes, die für die Hashwertberechnung genommen werden
     66 		/// </summary>
     67 		public int MD5FileSize
     68 		{
     69 			get
     70 			{
     71 				return _md5FileSize;
     72 			}
     73 		}
     74 
     75 		/// <summary>
     76 		/// Standard-Offlinedatei, die beim Programmstart gelesen wird
     77 		/// </summary>
     78 		public string DefaultOfflineFile
     79 		{
     80 			get
     81 			{
     82 				return _defaultOfflineFile;
     83 			}
     84 		}
     85 
     86 		/// <summary>
     87 		/// Prüfen, ob ein Pfadname durch die Extension weggelassen wird.
     88 		/// Die Restriction-Liste bleibt dabei unberücksichtig, da sie nur für Files gilt
     89 		/// </summary>
     90 		/// <param name="path">Pfad</param>
     91 		/// <returns>true wenn weggelassen</returns>
     92 		public bool IsDirExludedByExtension( string path )
     93 		{
     94 			// Wenn kein Pfad angegeben ist, kann dieser auch nicht ausgeschlossen sein
     95 			if ( String.IsNullOrEmpty( path ) )
     96 			{
     97 				return false;
     98 			}
     99 
    100 			foreach ( string ext in _excludeExtension )
    101 			{
    102 				if ( path.ToLower().EndsWith( ext.ToLower() ) )
    103 				{
    104 					return true;
    105 				}
    106 			}
    107 			return false;
    108 		}
    109 
    110 		/// <summary>
    111 		/// Prüfen, ob ein Pfadname durch Extension weggelassen wird. Dabei wird
    112 		/// zusätzlich die Restriktionsliste für Filenamen berücksichtigt, die nur bestimmte
    113 		/// Files zulässt.
    114 		/// </summary>
    115 		/// <param name="path"></param>
    116 		/// <returns></returns>
    117 		public bool IsFileExludedByExtension( string path )
    118 		{
    119 			// Wenn kein Pfad angegeben ist, kann dieser auch nicht ausgeschlossen sein
    120 			if ( String.IsNullOrEmpty( path ) )
    121 			{
    122 				return false;
    123 			}
    124 
    125 			if ( IsDirExludedByExtension( path ) )
    126 			{
    127 				return true;
    128 			}
    129 			// Wenn es Restriktionen gibt, nachsehen, ob diese Dateitypen angeschaut werden
    130 			if ( _restrictExtension.Count > 0 )
    131 			{
    132 				foreach ( string ext in _restrictExtension )
    133 				{
    134 					if ( path.ToLower().EndsWith( ext.ToLower() ) )
    135 					{
    136 						return false;
    137 					}
    138 				}
    139 				return true;
    140 			}
    141 			return false;
    142 		}
    143 
    144 		/// <summary>
    145 		/// Sollen die eigenen Platten automatisch neu eingescannt werden?
    146 		/// </summary>
    147 		public bool IsAutoScanMyDisks
    148 		{
    149 			get { return _autoScanMyDisks; }
    150 		}
    151 
    152 		/// <summary>
    153 		/// Sollen die fremden Platten automatisch neu eingescannt werden?
    154 		/// </summary>
    155 		public bool IsAutoScanOtherDisks
    156 		{
    157 			get { return _autoScanOtherDisks; }
    158 		}
    159 
    160 		/// <summary>
    161 		/// Gibt an ob erlaubte Dubletten eingeblendet werden sollen oder nicht
    162 		/// </summary>
    163 		public bool IsShowIgnoredClones
    164 		{
    165 			get { return _showIgnoredClones; }
    166 		}
    167 
    168 		/// <summary>
    169 		/// Gibt an ob gesperrte Dateien im TreeView "Fremde Platten" 
    170 		/// angezeigt werden sollen
    171 		/// </summary>
    172 		public bool IsHideExcludedFiles
    173 		{
    174 			get { return _hideExcludedFiles; }
    175 		}
    176 
    177 		/// <summary>
    178 		/// Gibt an ob vorhandene Dateien im Tree "Fremde Platten"
    179 		/// angezeigt werden sollen
    180 		/// </summary>
    181 		public bool IsHideExistingFiles
    182 		{
    183 			get { return _hideExistingFiles; }
    184 		}
    185 
    186 		/// <summary>
    187 		/// Liefert "true", wenn die Option "Markierungen nach Aktion entfernen" aktiviert ist
    188 		/// </summary>
    189 		public bool IsRemoveMarks
    190 		{
    191 			get { return _removeMarks; }
    192 		}
    193 
    194 		/// <summary>
    195 		/// Liefert "true", wenn die Option "identische Dateinamen ausschließen" aktiviert ist
    196 		/// </summary>
    197 		public bool IsTreatEqualNames
    198 		{
    199 			get { return _treatEqualNames; }
    200 		}
    201 
    202 		/// <summary>
    203 		/// Gibt an, ob das Programm mit verringerter Priorität ausgeführt werden soll
    204 		/// </summary>
    205 		public bool IsLowerPriority
    206 		{
    207 			get { return _lowerPriority; }
    208 		}
    209 
    210 		#endregion
    211 
    212 		/// <summary>
    213 		/// Config-File einlesen und MyDisks vorbelegen bzw. OtherDisks aktualisieren
    214 		/// </summary>
    215 		public void ReadConfig()
    216 		{
    217 			Boolean requireConfig = false;
    218 
    219 			String file = Path.Combine( _basePath, _configFile );
    220 
    221 			// Standard-Konfig schreiben wenn keine vorhanden ist
    222 			if ( !File.Exists( file ) )
    223 			{
    224 				requireConfig = CreateDefaultConfig( file );
    225 			}
    226 
    227 			Program.MainWindow.Log( "" );
    228 			Program.MainWindow.Log( "Konfiguration:" );
    229 
    230 			// Konfigdatei einlesen
    231 			using ( StreamReader tr = File.OpenText( file ) )
    232 			{
    233 				string line;
    234 				while ( ( line = tr.ReadLine() ) != null )
    235 				{
    236 					string startChar = line.Substring( 0, 1 );
    237 
    238 					switch ( startChar )
    239 					{
    240 						// Kommentarzeilen ignorieren
    241 						case "#":
    242 							continue;
    243 
    244 						// Datei-Negativ-Filter
    245 						case "-":
    246 							_excludeExtension.Add( line.Substring( 1 ) );
    247 							Program.MainWindow.Log( "Dateien/Ordner ausnehmen: *" + line.Substring( 1 ) );
    248 							continue;
    249 
    250 						// Datei-Positiv-Filter
    251 						case "+":
    252 							_restrictExtension.Add( line.Substring( 1 ) );
    253 							Program.MainWindow.Log( "Erlaubte Dateien: *" + line.Substring( 1 ) );
    254 							continue;
    255 
    256 						// Konfigurationszeile
    257 						case "%":
    258 							GetSpecialSetting( line );
    259 							continue;
    260 
    261 						// meine Platten
    262 						default:
    263 							// bei nur Laufwerksbuchstaben ein \ dranhängen
    264 							if ( line.EndsWith( @":" ) )
    265 							{
    266 								line = line + @"\";
    267 							}
    268 							Program.MainWindow.Log( "Meine Platte: " + line );
    269 							Program.MainWindow.m_MyDisks.AddOrGetChildItemByPath( line );
    270 
    271 							continue;
    272 					}
    273 					
    274 				}
    275 			}
    276 			Program.MainWindow.Log( "" );
    277 
    278 			// Konfig-Dialog aufrufen
    279 			if ( requireConfig )
    280 			{
    281 				EditConfig();
    282 			}
    283 		}
    284 
    285 		private void GetSpecialSetting( string line )
    286 		{
    287 			string[] cfg = line.Split( '=' );
    288 			try
    289 			{
    290 				switch ( cfg[ 0 ].ToLower() )
    291 				{
    292 					case "%autoscanmydisks":
    293 						_autoScanMyDisks = Convert.ToBoolean( cfg[ 1 ] );
    294 						Program.MainWindow.Log( "AutoScanMyDisks: " + _autoScanMyDisks );
    295 						break;
    296 
    297 					case "%autoscanotherdisks":
    298 						_autoScanOtherDisks = Convert.ToBoolean( cfg[ 1 ] );
    299 						Program.MainWindow.Log( "AutoScanOtherDisks: " + _autoScanOtherDisks );
    300 						break;
    301 
    302 					case "%showignoredclones":
    303 						_showIgnoredClones = Convert.ToBoolean( cfg[ 1 ] );
    304 						Program.MainWindow.Log( "ShowIgnoredClones: " + _showIgnoredClones );
    305 						break;
    306 
    307 					case "%hideexcludedfiles":
    308 						_hideExcludedFiles = Convert.ToBoolean( cfg[ 1 ] );
    309 						Program.MainWindow.Log( "HideExcludedFiles: " + _hideExcludedFiles );
    310 						break;
    311 
    312 					case "%hideexistingfiles":
    313 						_hideExistingFiles = Convert.ToBoolean( cfg[ 1 ] );
    314 						Program.MainWindow.Log( "HideExistingFiles: " + _hideExistingFiles );
    315 						break;
    316 
    317 					case "%head":
    318 						_md5FileSize = Convert.ToInt32( cfg[ 1 ] );
    319 						Program.MainWindow.Log( "Headerlänge für MD5-Hash: " + MD5FileSize );
    320 						break;
    321 
    322 					case "%foreign":
    323 						Program.MainWindow.Log( "Fremde Platte: " + cfg[ 1 ] );
    324 						Program.MainWindow.m_OtherDisks.AddOrGetChildItemByPath( cfg[ 1 ] );
    325 						break;
    326 
    327 					case "%removemarks":
    328 						_removeMarks = Convert.ToBoolean( cfg[ 1 ] );
    329 						Program.MainWindow.Log( "RemoveMarks: " + _removeMarks );
    330 						break;
    331 
    332 					case "%treatequalnames":
    333 						_treatEqualNames = Convert.ToBoolean( cfg[ 1 ] );
    334 						Program.MainWindow.Log( "TreatEqualNames: " + _treatEqualNames );
    335 						break;
    336 
    337 					case "%defaultofflinefile":
    338 						_defaultOfflineFile = cfg[ 1 ];
    339 						Program.MainWindow.Log( "DefaultOfflineFile: " + _defaultOfflineFile );
    340 						break;
    341 
    342 					case "%lowerpriority":
    343 						_lowerPriority = Convert.ToBoolean( cfg[ 1 ] );
    344 						Program.MainWindow.Log( "LowerPriority: " + _lowerPriority );
    345 						break;
    346 
    347 					case "%headerextension":
    348 						_headerExtension = cfg[ 1 ];
    349 						Program.MainWindow.Log( "HeaderExtension: " + _headerExtension );
    350 						break;
    351 				}
    352 			}
    353 			catch ( Exception e )
    354 			{
    355 				Program.MainWindow.LogError( "Fehlerhaftes Setting: " + line + " / " + e.Message );
    356 			}
    357 		}
    358 
    359 		private static Boolean CreateDefaultConfig( String file )
    360 		{
    361 			using ( StreamWriter cfgOut = new StreamWriter( file ) )
    362 			{
    363 				cfgOut.Write(
    364 					@"# Laenge des MD5-Headers zum Dateivergleich
    365 %head=10000
    366 # Endungen/Dateinamen die explizit ausgeschlossen werden (auch Ordner)
    367 -.bmp
    368 -.gif
    369 -.m3u
    370 -.tif
    371 -.ini
    372 -.db
    373 -.nfo
    374 -.htm
    375 -.html
    376 -.js
    377 -lost+found
    378 -System Volume Information
    379 -RECYCLE.BIN
    380 -RECYCLER
    381 -RECYCLED" );
    382 			}
    383 
    384 			Program.MainWindow.Log( "Konfigurationsdatei LanTool.cfg wurde nicht gefunden. Erstelle neue Default-Datei." );
    385 			return true;
    386 		}
    387 
    388 		/// <summary>
    389 		/// Abspeichern der aktuellen Settings
    390 		/// </summary>
    391 		public void WriteConfig()
    392 		{
    393 			String file = Path.Combine( _basePath, _configFile );
    394 
    395 			using ( StreamWriter sw = File.CreateText( file ) )
    396 			{
    397 				sw.WriteLine( "# Laenge des MD5-Headers zum Dateivergleich" );
    398 				sw.WriteLine( "%head=" + _md5FileSize.ToString() );
    399 				sw.WriteLine( "# Endungen/Dateinamen die explizit ausgeschlossen werden (auch Ordner)" );
    400 				foreach ( string s in _excludeExtension )
    401 				{
    402 					sw.WriteLine( "-" + s );
    403 				}
    404 				sw.WriteLine( "# Endungen/Dateinamen die überhaupt gelesen werden (nur Dateien, keine Ordner)" );
    405 				foreach ( string s in _restrictExtension )
    406 				{
    407 					sw.WriteLine( "+" + s );
    408 				}
    409 				sw.WriteLine( "# Eigene Platten" );
    410 				foreach ( StuffItem si in Program.MainWindow.m_MyDisks.ChildItems )
    411 				{
    412 					sw.WriteLine( si.Path );
    413 				}
    414 				sw.WriteLine( "%AutoScanMyDisks=" + _autoScanMyDisks.ToString() );
    415 				sw.WriteLine( "%ShowIgnoredClones=" + _showIgnoredClones.ToString() );
    416 				sw.WriteLine( "# Fremde Platten" );
    417 				foreach ( StuffItem si in Program.MainWindow.m_OtherDisks.ChildItems )
    418 				{
    419 					sw.WriteLine( "%foreign=" + si.Path );
    420 				}
    421 				sw.WriteLine( "%AutoScanOtherDisks=" + _autoScanOtherDisks.ToString() );
    422 				sw.WriteLine( "%HideExcludedFiles=" + _hideExcludedFiles.ToString() );
    423 				sw.WriteLine( "%HideExistingFiles=" + _hideExistingFiles.ToString() );
    424 				sw.WriteLine( "%RemoveMarks=" + _removeMarks.ToString() );
    425 				sw.WriteLine( "%TreatEqualNames=" + _treatEqualNames.ToString() );
    426 				sw.WriteLine( "%DefaultOfflineFile=" + _defaultOfflineFile );
    427 				sw.WriteLine( "%LowerPriority=" + _lowerPriority.ToString() );
    428 				sw.WriteLine( "%HeaderExtension=" + _headerExtension );
    429 			}
    430 		}
    431 
    432 		/// <summary>
    433 		/// Konfiguration mittels Dialog aktualisieren
    434 		/// </summary>
    435 		/// <returns></returns>
    436 		public bool EditConfig()
    437 		{
    438 			// Dialog erstellen
    439 			using ( ConfigEdit ce = new ConfigEdit( _basePath ) )
    440 			{
    441 				// alle aktuellen Einstellungen an den Dialog übergeben
    442 				ce.Md5FileSize = _md5FileSize;
    443 				ce.AutoScanMyDisks = _autoScanMyDisks;
    444 				ce.AutoScanOtherDisks = _autoScanOtherDisks;
    445 				ce.ShowIgnoredClones = _showIgnoredClones;
    446 				ce.HideExcludedFiles = _hideExcludedFiles;
    447 				ce.HideExistingFiles = _hideExistingFiles;
    448 				ce.RemoveMarks = _removeMarks;
    449 				ce.TreatEqualNames = _treatEqualNames;
    450 				ce.LowerPriority = _lowerPriority;
    451 				ce.DefaultOfflineFile = _defaultOfflineFile;
    452 				ce.HeaderExtension = _headerExtension;
    453 
    454 				foreach ( StuffItem si in Program.MainWindow.m_OtherDisks.ChildItems )
    455 				{
    456 					ce.otherDisks.Add( si.Path );
    457 				}
    458 				foreach ( StuffItem si in Program.MainWindow.m_MyDisks.ChildItems )
    459 				{
    460 					ce.myDisks.Add( si.Path );
    461 				}
    462 				foreach ( string ex in _excludeExtension )
    463 				{
    464 					ce.excludeExtension.Add( ex );
    465 				}
    466 				foreach ( string ex in _restrictExtension )
    467 				{
    468 					ce.restrictExtension.Add( ex );
    469 				}
    470 
    471 				// Wenn der Dialog mit "OK" abgeschlossen wurde, Einstellungen wieder zurückübernehmen
    472 				if ( ce.ShowDialog( Program.MainWindow ) == DialogResult.OK )
    473 				{
    474 					_md5FileSize = ce.Md5FileSize;
    475 					_autoScanMyDisks = ce.AutoScanMyDisks;
    476 					_autoScanOtherDisks = ce.AutoScanOtherDisks;
    477 					_showIgnoredClones = ce.ShowIgnoredClones;
    478 					_hideExcludedFiles = ce.HideExcludedFiles;
    479 					_hideExistingFiles = ce.HideExistingFiles;
    480 					_removeMarks = ce.RemoveMarks;
    481 					_treatEqualNames = ce.TreatEqualNames;
    482 					_excludeExtension = ce.excludeExtension;
    483 					_restrictExtension = ce.restrictExtension;
    484 					_defaultOfflineFile = ce.DefaultOfflineFile;
    485 					_lowerPriority = ce.LowerPriority;
    486 					_headerExtension = ce.HeaderExtension;
    487 
    488 
    489 
    490 					// nimmt nachher alle Elemente auf, die aus den RootItems entfernt werden
    491 					// müssen
    492 					List<StuffItem> delList = new List<StuffItem>();
    493 
    494 					// alle fremden Platten hinzufügen, alte entfernen
    495 					foreach ( string s in ce.otherDisks )
    496 					{
    497 						Program.MainWindow.m_OtherDisks.AddOrGetChildItemByPath( s );
    498 					}
    499 
    500 					// alte Einträge in fremden Platten merken
    501 					foreach ( StuffItem csi in Program.MainWindow.m_OtherDisks.ChildItems )
    502 					{
    503 						if ( !ce.otherDisks.Contains( csi.Path ) )
    504 						{
    505 							delList.Add( csi );
    506 						}
    507 					}
    508 
    509 					// alte Einträge entfernen
    510 					foreach ( StuffItem csi in delList )
    511 					{
    512 						Program.MainWindow.m_OtherDisks.ChildItems.Remove( csi );
    513 					}
    514 
    515 					delList.Clear();
    516 
    517 					// alle eigenen Platten hinzufügen
    518 					foreach ( string s in ce.myDisks )
    519 					{
    520 						Program.MainWindow.m_MyDisks.AddOrGetChildItemByPath( s );
    521 					}
    522 
    523 					// alte Einträge in meinen Platten merken
    524 					foreach ( StuffItem csi in Program.MainWindow.m_MyDisks.ChildItems )
    525 					{
    526 						if ( !ce.myDisks.Contains( csi.Path ) )
    527 						{
    528 							delList.Add( csi );
    529 						}
    530 					}
    531 
    532 					// alte Einträge entfernen
    533 					foreach ( StuffItem csi in delList )
    534 					{
    535 						Program.MainWindow.m_MyDisks.ChildItems.Remove( csi );
    536 					}
    537 
    538 					// Programm mit niedriger Priorität ausführen
    539 					if ( _lowerPriority )
    540 					{
    541 						Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal;
    542 					}
    543 					else
    544 					{
    545 						Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
    546 					}
    547 
    548 					// Fenstertitel setzen
    549 					if ( !String.IsNullOrEmpty( _headerExtension ) )
    550 					{
    551 						Program.MainWindow.Text = String.Format( "[{0}] {1}", HeaderExtension, MainForm.DefaultHeader );
    552 					}
    553 					else
    554 					{
    555 						Program.MainWindow.Text = MainForm.DefaultHeader;
    556 					}
    557 
    558 					// Konfigdatei abspeichern
    559 					WriteConfig();
    560 
    561 					return true;
    562 				}
    563 			}
    564 
    565 			return false;
    566 		}
    567 	}
    568 }