2017-04-20 06:56:49 +01:00
//
// Author:
// Natalia Portillo claunia@claunia.com
//
// Copyright (c) 2017, © Canary Islands Computer Museum
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
// * Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
using System ;
using System.IO ;
using System.Xml.Serialization ;
using Claunia.PropertyList ;
2017-05-11 15:57:02 +01:00
using Microsoft.Win32 ;
2017-04-20 06:56:49 +01:00
2017-05-10 21:04:42 +01:00
namespace osrepodbmgr.Core
2017-04-20 06:56:49 +01:00
{
public class SetSettings
{
public string TemporaryFolder ;
public string DatabasePath ;
public string RepositoryPath ;
public string UnArchiverPath ;
2017-05-11 19:01:47 +01:00
public AlgoEnum CompressionAlgorithm ;
2017-05-18 22:23:49 +01:00
public bool UseAntivirus ;
public bool UseClamd ;
public string ClamdHost ;
public ushort ClamdPort ;
public bool ClamdIsLocal ;
2017-05-19 01:13:53 +01:00
public bool UseVirusTotal ;
public string VirusTotalKey ;
2017-04-20 06:56:49 +01:00
}
public static class Settings
{
public static SetSettings Current ;
public static void LoadSettings ( )
{
Current = new SetSettings ( ) ;
DiscImageChef . Interop . PlatformID ptID = DiscImageChef . Interop . DetectOS . GetRealPlatformID ( ) ;
2017-05-15 07:30:27 +01:00
FileStream prefsFs = null ;
StreamReader prefsSr = null ;
2017-04-20 06:56:49 +01:00
try
{
switch ( ptID )
{
case DiscImageChef . Interop . PlatformID . MacOSX :
case DiscImageChef . Interop . PlatformID . iOS :
{
string preferencesPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , "Library" , "Preferences" ) ;
string preferencesFilePath = Path . Combine ( preferencesPath , "com.claunia.museum.osrepodbmgr.plist" ) ;
if ( ! File . Exists ( preferencesFilePath ) )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
}
2017-05-15 07:30:27 +01:00
prefsFs = new FileStream ( preferencesFilePath , FileMode . Open ) ;
NSDictionary parsedPreferences = ( NSDictionary ) BinaryPropertyListParser . Parse ( prefsFs ) ;
2017-04-20 06:56:49 +01:00
if ( parsedPreferences ! = null )
{
NSObject obj ;
if ( parsedPreferences . TryGetValue ( "TemporaryFolder" , out obj ) )
{
Current . TemporaryFolder = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . TemporaryFolder = Path . GetTempPath ( ) ;
if ( parsedPreferences . TryGetValue ( "DatabasePath" , out obj ) )
{
Current . DatabasePath = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . DatabasePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "osrepodbmgr.db" ) ;
if ( parsedPreferences . TryGetValue ( "RepositoryPath" , out obj ) )
{
Current . RepositoryPath = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . RepositoryPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "osrepo" ) ;
if ( parsedPreferences . TryGetValue ( "UnArchiverPath" , out obj ) )
{
Current . UnArchiverPath = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . UnArchiverPath = null ;
2017-05-11 03:46:19 +01:00
if ( parsedPreferences . TryGetValue ( "CompressionAlgorithm" , out obj ) )
{
if ( ! Enum . TryParse ( ( ( NSString ) obj ) . ToString ( ) , true , out Current . CompressionAlgorithm ) )
2017-05-11 19:01:47 +01:00
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-05-11 03:46:19 +01:00
}
else
2017-05-11 19:01:47 +01:00
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-05-15 07:30:27 +01:00
2017-05-18 22:23:49 +01:00
if ( parsedPreferences . TryGetValue ( "UseAntivirus" , out obj ) )
{
Current . UseAntivirus = ( ( NSNumber ) obj ) . ToBool ( ) ;
}
else
Current . UseAntivirus = false ;
if ( parsedPreferences . TryGetValue ( "UseClamd" , out obj ) )
{
Current . UseClamd = ( ( NSNumber ) obj ) . ToBool ( ) ;
}
else
Current . UseClamd = false ;
if ( parsedPreferences . TryGetValue ( "ClamdHost" , out obj ) )
{
Current . ClamdHost = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . ClamdHost = null ;
if ( parsedPreferences . TryGetValue ( "ClamdPort" , out obj ) )
{
Current . ClamdPort = ( ushort ) ( ( NSNumber ) obj ) . ToLong ( ) ;
}
else
Current . ClamdPort = 3310 ;
if ( parsedPreferences . TryGetValue ( "ClamdIsLocal" , out obj ) )
{
Current . ClamdIsLocal = ( ( NSNumber ) obj ) . ToBool ( ) ;
}
else
Current . ClamdIsLocal = false ;
2017-05-19 01:13:53 +01:00
if ( parsedPreferences . TryGetValue ( "UseVirusTotal" , out obj ) )
{
Current . ClamdIsLocal = ( ( NSNumber ) obj ) . ToBool ( ) ;
}
else
Current . ClamdIsLocal = false ;
if ( parsedPreferences . TryGetValue ( "VirusTotalKey" , out obj ) )
{
Current . ClamdHost = ( ( NSString ) obj ) . ToString ( ) ;
}
else
Current . ClamdHost = null ;
2017-05-15 07:30:27 +01:00
prefsFs . Close ( ) ;
2017-04-20 06:56:49 +01:00
}
else {
2017-05-15 07:30:27 +01:00
prefsFs . Close ( ) ;
2017-04-20 06:56:49 +01:00
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
}
}
break ;
case DiscImageChef . Interop . PlatformID . Win32NT :
case DiscImageChef . Interop . PlatformID . Win32S :
case DiscImageChef . Interop . PlatformID . Win32Windows :
case DiscImageChef . Interop . PlatformID . WinCE :
case DiscImageChef . Interop . PlatformID . WindowsPhone :
{
RegistryKey parentKey = Registry . CurrentUser . OpenSubKey ( "SOFTWARE" ) . OpenSubKey ( "Canary Islands Computer Museum" ) ;
if ( parentKey = = null )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
return ;
}
RegistryKey key = parentKey . OpenSubKey ( "OSRepoDBMgr" ) ;
if ( key = = null )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
return ;
}
Current . TemporaryFolder = ( string ) key . GetValue ( "TemporaryFolder" ) ;
Current . DatabasePath = ( string ) key . GetValue ( "DatabasePath" ) ;
Current . RepositoryPath = ( string ) key . GetValue ( "RepositoryPath" ) ;
Current . UnArchiverPath = ( string ) key . GetValue ( "UnArchiverPath" ) ;
2017-05-11 03:46:19 +01:00
if ( ! Enum . TryParse ( ( string ) key . GetValue ( "CompressionAlgorithm" ) , true , out Current . CompressionAlgorithm ) )
2017-05-11 19:01:47 +01:00
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-05-18 22:23:49 +01:00
Current . UseAntivirus = ( bool ) key . GetValue ( "UseAntivirus" ) ;
Current . UseClamd = ( bool ) key . GetValue ( "UseClamd" ) ;
Current . ClamdHost = ( string ) key . GetValue ( "ClamdHost" ) ;
Current . ClamdPort = ( ushort ) key . GetValue ( "ClamdPort" ) ;
Current . ClamdIsLocal = ( bool ) key . GetValue ( "ClamdIsLocal" ) ;
2017-05-19 01:13:53 +01:00
Current . UseVirusTotal = ( bool ) key . GetValue ( "UseVirusTotal" ) ;
Current . VirusTotalKey = ( string ) key . GetValue ( "VirusTotalKey" ) ;
2017-04-20 06:56:49 +01:00
}
break ;
default :
{
string configPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".config" ) ;
string settingsPath = Path . Combine ( configPath , "OSRepoDBMgr.xml" ) ;
if ( ! Directory . Exists ( configPath ) )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
return ;
}
XmlSerializer xs = new XmlSerializer ( Current . GetType ( ) ) ;
2017-05-15 07:30:27 +01:00
prefsSr = new StreamReader ( settingsPath ) ;
Current = ( SetSettings ) xs . Deserialize ( prefsSr ) ;
prefsSr . Close ( ) ;
2017-04-20 06:56:49 +01:00
}
break ;
}
}
catch
{
2017-05-15 07:30:27 +01:00
if ( prefsFs ! = null )
prefsFs . Close ( ) ;
if ( prefsSr ! = null )
prefsSr . Close ( ) ;
2017-05-19 18:22:54 +01:00
2017-04-20 06:56:49 +01:00
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
}
}
public static void SaveSettings ( )
{
try
{
DiscImageChef . Interop . PlatformID ptID = DiscImageChef . Interop . DetectOS . GetRealPlatformID ( ) ;
switch ( ptID )
{
case DiscImageChef . Interop . PlatformID . MacOSX :
case DiscImageChef . Interop . PlatformID . iOS :
{
NSDictionary root = new NSDictionary ( ) ;
root . Add ( "TemporaryFolder" , Current . TemporaryFolder ) ;
root . Add ( "DatabasePath" , Current . DatabasePath ) ;
root . Add ( "RepositoryPath" , Current . RepositoryPath ) ;
root . Add ( "UnArchiverPath" , Current . UnArchiverPath ) ;
2017-05-15 07:30:27 +01:00
root . Add ( "CompressionAlgorithm" , Current . CompressionAlgorithm . ToString ( ) ) ;
2017-05-18 22:23:49 +01:00
root . Add ( "UseAntivirus" , Current . UseAntivirus ) ;
root . Add ( "UseClamd" , Current . UseClamd ) ;
root . Add ( "ClamdHost" , Current . ClamdHost ) ;
root . Add ( "ClamdPort" , Current . ClamdPort ) ;
root . Add ( "ClamdIsLocal" , Current . ClamdIsLocal ) ;
2017-05-19 01:13:53 +01:00
root . Add ( "UseVirusTotal" , Current . UseVirusTotal ) ;
root . Add ( "VirusTotalKey" , Current . VirusTotalKey ) ;
2017-04-20 06:56:49 +01:00
string preferencesPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , "Library" , "Preferences" ) ;
string preferencesFilePath = Path . Combine ( preferencesPath , "com.claunia.museum.osrepodbmgr.plist" ) ;
FileStream fs = new FileStream ( preferencesFilePath , FileMode . Create ) ;
BinaryPropertyListWriter . Write ( fs , root ) ;
fs . Close ( ) ;
}
break ;
case DiscImageChef . Interop . PlatformID . Win32NT :
case DiscImageChef . Interop . PlatformID . Win32S :
case DiscImageChef . Interop . PlatformID . Win32Windows :
case DiscImageChef . Interop . PlatformID . WinCE :
case DiscImageChef . Interop . PlatformID . WindowsPhone :
{
2017-05-16 14:43:48 +01:00
RegistryKey parentKey = Registry . CurrentUser . OpenSubKey ( "SOFTWARE" , true ) . CreateSubKey ( "Canary Islands Computer Museum" ) ;
2017-04-20 06:56:49 +01:00
RegistryKey key = parentKey . CreateSubKey ( "OSRepoDBMgr" ) ;
key . SetValue ( "TemporaryFolder" , Current . TemporaryFolder ) ;
key . SetValue ( "DatabasePath" , Current . DatabasePath ) ;
key . SetValue ( "RepositoryPath" , Current . RepositoryPath ) ;
2017-05-19 18:22:54 +01:00
if ( Current . UnArchiverPath ! = null )
2017-05-16 14:43:48 +01:00
key . SetValue ( "UnArchiverPath" , Current . UnArchiverPath ) ;
2017-05-11 03:46:19 +01:00
key . SetValue ( "CompressionAlgorithm" , Current . CompressionAlgorithm ) ;
2017-05-18 22:23:49 +01:00
key . SetValue ( "UseAntivirus" , Current . UseAntivirus ) ;
key . SetValue ( "UseClamd" , Current . UseClamd ) ;
key . SetValue ( "ClamdHost" , Current . ClamdHost ) ;
key . SetValue ( "ClamdPort" , Current . ClamdPort ) ;
key . SetValue ( "ClamdIsLocal" , Current . ClamdIsLocal ) ;
2017-05-19 01:13:53 +01:00
key . SetValue ( "UseVirusTotal" , Current . UseVirusTotal ) ;
key . SetValue ( "VirusTotalKey" , Current . VirusTotalKey ) ;
2017-04-20 06:56:49 +01:00
}
break ;
default :
{
string configPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".config" ) ;
string settingsPath = Path . Combine ( configPath , "OSRepoDBMgr.xml" ) ;
if ( ! Directory . Exists ( configPath ) )
Directory . CreateDirectory ( configPath ) ;
FileStream fs = new FileStream ( settingsPath , FileMode . Create ) ;
XmlSerializer xs = new XmlSerializer ( Current . GetType ( ) ) ;
xs . Serialize ( fs , Current ) ;
fs . Close ( ) ;
}
break ;
}
}
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
{
2017-05-04 11:02:45 +01:00
if ( System . Diagnostics . Debugger . IsAttached )
throw ;
2017-04-20 06:56:49 +01:00
}
}
public static void SetDefaultSettings ( )
{
Current = new SetSettings ( ) ;
Current . TemporaryFolder = Path . GetTempPath ( ) ;
Current . DatabasePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "osrepodbmgr.db" ) ;
Current . RepositoryPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "osrepo" ) ;
Current . UnArchiverPath = null ;
2017-05-11 19:01:47 +01:00
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-05-18 22:23:49 +01:00
Current . UseAntivirus = false ;
Current . UseClamd = false ;
Current . ClamdHost = null ;
Current . ClamdPort = 3310 ;
Current . ClamdIsLocal = false ;
2017-05-19 01:13:53 +01:00
Current . UseVirusTotal = false ;
Current . VirusTotalKey = null ;
2017-04-20 06:56:49 +01:00
}
}
}