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.
//
2017-12-30 00:32:21 +00:00
2017-04-20 06:56:49 +01:00
using System ;
2017-12-30 00:32:21 +00:00
using System.Diagnostics ;
2017-04-20 06:56:49 +01:00
using System.IO ;
using System.Xml.Serialization ;
using Claunia.PropertyList ;
2017-12-30 00:32:21 +00:00
using DiscImageChef.Interop ;
2017-05-11 15:57:02 +01:00
using Microsoft.Win32 ;
2017-12-30 00:32:21 +00:00
using PlatformID = DiscImageChef . Interop . PlatformID ;
2017-04-20 06:56:49 +01:00
2018-02-23 02:14:58 +00:00
namespace apprepodbmgr.Core
2017-04-20 06:56:49 +01:00
{
public class SetSettings
{
2017-12-30 00:32:21 +00:00
public string ClamdHost ;
public bool ClamdIsLocal ;
public ushort ClamdPort ;
2017-05-11 19:01:47 +01:00
public AlgoEnum CompressionAlgorithm ;
2017-12-30 00:32:21 +00:00
public string DatabasePath ;
public string RepositoryPath ;
public string TemporaryFolder ;
public string UnArchiverPath ;
public bool UseAntivirus ;
public bool UseClamd ;
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 ( )
{
2020-08-22 21:37:02 +01:00
Current = new SetSettings ( ) ;
2017-12-30 00:32:21 +00:00
PlatformID ptId = DetectOS . GetRealPlatformID ( ) ;
2017-04-20 06:56:49 +01:00
2017-12-30 00:32:21 +00:00
FileStream prefsFs = null ;
2017-05-15 07:30:27 +01:00
StreamReader prefsSr = null ;
2017-04-20 06:56:49 +01:00
try
{
2017-12-30 00:32:21 +00:00
switch ( ptId )
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
case PlatformID . MacOSX :
case PlatformID . iOS :
{
string preferencesPath =
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , "Library" ,
"Preferences" ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
string preferencesFilePath =
2018-02-23 02:14:58 +00:00
Path . Combine ( preferencesPath , "com.claunia.museum.apprepodbmgr.plist" ) ;
2017-12-30 00:32:21 +00:00
if ( ! File . Exists ( preferencesFilePath ) )
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
}
2017-04-20 06:56:49 +01:00
2020-08-22 21:37:02 +01:00
prefsFs = new FileStream ( preferencesFilePath , FileMode . Open ) ;
var parsedPreferences = ( NSDictionary ) BinaryPropertyListParser . Parse ( prefsFs ) ;
2017-12-30 00:32:21 +00:00
if ( parsedPreferences ! = null )
{
Current . TemporaryFolder = parsedPreferences . TryGetValue ( "TemporaryFolder" , out NSObject obj )
2020-08-22 21:37:02 +01:00
? ( ( NSString ) obj ) . ToString ( ) : Path . GetTempPath ( ) ;
2017-12-30 00:32:21 +00:00
Current . DatabasePath = parsedPreferences . TryGetValue ( "DatabasePath" , out obj )
? ( ( NSString ) obj ) . ToString ( )
2020-08-22 21:37:02 +01:00
: Path .
Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
2018-02-23 02:14:58 +00:00
"apprepodbmgr.db" ) ;
2017-12-30 00:32:21 +00:00
Current . RepositoryPath = parsedPreferences . TryGetValue ( "RepositoryPath" , out obj )
? ( ( NSString ) obj ) . ToString ( )
2020-08-22 21:37:02 +01:00
: Path .
Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
2018-02-23 03:33:22 +00:00
"apprepo" ) ;
2017-12-30 00:32:21 +00:00
Current . UnArchiverPath = parsedPreferences . TryGetValue ( "UnArchiverPath" , out obj )
2020-08-22 21:37:02 +01:00
? ( ( NSString ) obj ) . ToString ( ) : null ;
2017-12-30 00:32:21 +00:00
if ( parsedPreferences . TryGetValue ( "CompressionAlgorithm" , out obj ) )
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
if ( ! Enum . TryParse ( ( ( NSString ) obj ) . ToString ( ) , true , out Current . CompressionAlgorithm ) )
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-04-20 06:56:49 +01:00
}
2020-08-22 21:37:02 +01:00
else
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2017-04-20 06:56:49 +01:00
2017-12-30 00:32:21 +00:00
Current . UseAntivirus = parsedPreferences . TryGetValue ( "UseAntivirus" , out obj ) & &
( ( NSNumber ) obj ) . ToBool ( ) ;
2017-05-15 07:30:27 +01:00
2017-12-30 00:32:21 +00:00
Current . UseClamd = parsedPreferences . TryGetValue ( "UseClamd" , out obj ) & &
( ( NSNumber ) obj ) . ToBool ( ) ;
2017-05-15 07:30:27 +01:00
2017-12-30 00:32:21 +00:00
Current . ClamdHost = parsedPreferences . TryGetValue ( "ClamdHost" , out obj )
2020-08-22 21:37:02 +01:00
? ( ( NSString ) obj ) . ToString ( ) : null ;
2017-12-30 00:32:21 +00:00
if ( parsedPreferences . TryGetValue ( "ClamdPort" , out obj ) )
2020-08-22 21:37:02 +01:00
Current . ClamdPort = ( ushort ) ( ( NSNumber ) obj ) . ToLong ( ) ;
else
Current . ClamdPort = 3310 ;
2017-12-30 00:32:21 +00:00
Current . ClamdIsLocal = parsedPreferences . TryGetValue ( "ClamdIsLocal" , out obj ) & &
( ( NSNumber ) obj ) . ToBool ( ) ;
Current . ClamdIsLocal = parsedPreferences . TryGetValue ( "UseVirusTotal" , out obj ) & &
( ( NSNumber ) obj ) . ToBool ( ) ;
Current . ClamdHost = parsedPreferences . TryGetValue ( "VirusTotalKey" , out obj )
2020-08-22 21:37:02 +01:00
? ( ( NSString ) obj ) . ToString ( ) : null ;
2017-12-30 00:32:21 +00:00
prefsFs . Close ( ) ;
2017-04-20 06:56:49 +01:00
}
2017-12-30 00:32:21 +00:00
else
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
prefsFs . Close ( ) ;
2017-04-20 06:56:49 +01:00
2017-12-30 00:32:21 +00:00
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
2017-04-20 06:56:49 +01:00
}
2017-12-30 00:32:21 +00:00
}
2020-08-22 21:37:02 +01:00
2017-04-20 06:56:49 +01:00
break ;
2017-12-30 00:32:21 +00:00
case PlatformID . Win32NT :
case PlatformID . Win32S :
case PlatformID . Win32Windows :
case PlatformID . WinCE :
case PlatformID . WindowsPhone :
{
2020-08-22 21:37:02 +01:00
RegistryKey parentKey = Registry . CurrentUser . OpenSubKey ( "SOFTWARE" ) ? .
OpenSubKey ( "Canary Islands Computer Museum" ) ;
2017-12-30 00:32:21 +00:00
if ( parentKey = = null )
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
return ;
}
2017-04-20 06:56:49 +01:00
2018-02-23 03:33:22 +00:00
RegistryKey key = parentKey . OpenSubKey ( "AppRepoDBMgr" ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
if ( key = = null )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
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" ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
if ( ! Enum . TryParse ( ( string ) key . GetValue ( "CompressionAlgorithm" ) , true ,
out Current . CompressionAlgorithm ) )
Current . CompressionAlgorithm = AlgoEnum . GZip ;
2020-08-22 21:37:02 +01:00
Current . UseAntivirus = bool . Parse ( ( string ) key . GetValue ( "UseAntivirus" ) ) ;
Current . UseClamd = bool . Parse ( ( string ) key . GetValue ( "UseClamd" ) ) ;
Current . ClamdHost = ( string ) key . GetValue ( "ClamdHost" ) ;
Current . ClamdPort = ushort . Parse ( ( string ) key . GetValue ( "ClamdPort" ) ) ;
Current . ClamdIsLocal = bool . Parse ( ( string ) key . GetValue ( "ClamdIsLocal" ) ) ;
2020-08-22 21:30:21 +01:00
Current . UseVirusTotal = bool . Parse ( ( string ) key . GetValue ( "UseVirusTotal" ) ) ;
2020-08-22 21:37:02 +01:00
Current . VirusTotalKey = ( string ) key . GetValue ( "VirusTotalKey" ) ;
2017-12-30 00:32:21 +00:00
}
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
break ;
default :
{
string configPath =
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".config" ) ;
2020-08-22 21:37:02 +01:00
string settingsPath = Path . Combine ( configPath , "AppRepoDBMgr.xml" ) ;
2017-04-20 06:56:49 +01:00
2017-12-30 00:32:21 +00:00
if ( ! Directory . Exists ( configPath ) )
{
SetDefaultSettings ( ) ;
SaveSettings ( ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
return ;
2017-04-20 06:56:49 +01:00
}
2017-12-30 00:32:21 +00:00
2020-08-22 21:37:02 +01:00
var xs = new XmlSerializer ( Current . GetType ( ) ) ;
prefsSr = new StreamReader ( settingsPath ) ;
Current = ( SetSettings ) xs . Deserialize ( prefsSr ) ;
2017-12-30 00:32:21 +00:00
prefsSr . Close ( ) ;
}
2020-08-22 21:37:02 +01:00
2017-04-20 06:56:49 +01:00
break ;
}
}
catch
{
2017-12-30 00:32:21 +00:00
prefsFs ? . Close ( ) ;
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
{
2017-12-30 00:32:21 +00:00
PlatformID ptId = DetectOS . GetRealPlatformID ( ) ;
2017-04-20 06:56:49 +01:00
2017-12-30 00:32:21 +00:00
switch ( ptId )
2017-04-20 06:56:49 +01:00
{
2017-12-30 00:32:21 +00:00
case PlatformID . MacOSX :
case PlatformID . iOS :
{
2020-08-22 21:37:02 +01:00
var root = new NSDictionary
2017-04-20 06:56:49 +01:00
{
2020-08-22 21:37:02 +01:00
{
"TemporaryFolder" , Current . TemporaryFolder
} ,
{
"DatabasePath" , Current . DatabasePath
} ,
{
"RepositoryPath" , Current . RepositoryPath
} ,
{
"UnArchiverPath" , Current . UnArchiverPath
} ,
{
"CompressionAlgorithm" , Current . CompressionAlgorithm . ToString ( )
} ,
{
"UseAntivirus" , Current . UseAntivirus
} ,
{
"UseClamd" , Current . UseClamd
} ,
{
"ClamdHost" , Current . ClamdHost
} ,
{
"ClamdPort" , Current . ClamdPort
} ,
{
"ClamdIsLocal" , Current . ClamdIsLocal
} ,
{
"UseVirusTotal" , Current . UseVirusTotal
} ,
{
"VirusTotalKey" , Current . VirusTotalKey
}
2017-12-30 00:32:21 +00:00
} ;
string preferencesPath =
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , "Library" ,
"Preferences" ) ;
2020-08-22 21:37:02 +01:00
2017-12-30 00:32:21 +00:00
string preferencesFilePath =
2018-02-23 02:14:58 +00:00
Path . Combine ( preferencesPath , "com.claunia.museum.apprepodbmgr.plist" ) ;
2017-12-30 00:32:21 +00:00
2020-08-22 21:37:02 +01:00
var fs = new FileStream ( preferencesFilePath , FileMode . Create ) ;
2017-12-30 00:32:21 +00:00
BinaryPropertyListWriter . Write ( fs , root ) ;
fs . Close ( ) ;
}
2020-08-22 21:37:02 +01:00
2017-04-20 06:56:49 +01:00
break ;
2017-12-30 00:32:21 +00:00
case PlatformID . Win32NT :
case PlatformID . Win32S :
case PlatformID . Win32Windows :
case PlatformID . WinCE :
case PlatformID . WindowsPhone :
{
2020-08-22 21:37:02 +01:00
RegistryKey parentKey = Registry . CurrentUser . OpenSubKey ( "SOFTWARE" , true ) ? .
CreateSubKey ( "Canary Islands Computer Museum" ) ;
2018-02-23 03:33:22 +00:00
RegistryKey key = parentKey ? . CreateSubKey ( "AppRepoDBMgr" ) ;
2017-12-30 00:32:21 +00:00
if ( key ! = null )
2017-04-20 06:56:49 +01:00
{
2020-08-22 21:37:02 +01:00
key . SetValue ( "TemporaryFolder" , Current . TemporaryFolder ) ;
key . SetValue ( "DatabasePath" , Current . DatabasePath ) ;
key . SetValue ( "RepositoryPath" , Current . RepositoryPath ) ;
if ( Current . UnArchiverPath ! = null )
key . SetValue ( "UnArchiverPath" , Current . UnArchiverPath ) ;
key . SetValue ( "CompressionAlgorithm" , Current . CompressionAlgorithm ) ;
key . SetValue ( "UseAntivirus" , Current . UseAntivirus ) ;
key . SetValue ( "UseClamd" , Current . UseClamd ) ;
key . SetValue ( "ClamdHost" , Current . ClamdHost = = null ? "" : 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 ) ;
2020-08-22 21:30:21 +01:00
key . SetValue ( "VirusTotalKey" , Current . VirusTotalKey = = null ? "" : Current . VirusTotalKey ) ;
2017-04-20 06:56:49 +01:00
}
2017-12-30 00:32:21 +00:00
}
2020-08-22 21:37:02 +01:00
2017-04-20 06:56:49 +01:00
break ;
default :
2017-12-30 00:32:21 +00:00
{
string configPath =
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".config" ) ;
2020-08-22 21:37:02 +01:00
string settingsPath = Path . Combine ( configPath , "AppRepoDBMgr.xml" ) ;
2017-12-30 00:32:21 +00:00
2020-08-22 21:37:02 +01:00
if ( ! Directory . Exists ( configPath ) )
Directory . CreateDirectory ( configPath ) ;
var fs = new FileStream ( settingsPath , FileMode . Create ) ;
var xs = new XmlSerializer ( Current . GetType ( ) ) ;
2017-12-30 00:32:21 +00:00
xs . Serialize ( fs , Current ) ;
fs . Close ( ) ;
}
2020-08-22 21:37:02 +01:00
2017-04-20 06:56:49 +01:00
break ;
}
}
2017-12-30 00:32:21 +00:00
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
2017-04-20 06:56:49 +01:00
catch
2017-12-30 00:32:21 +00:00
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
2017-04-20 06:56:49 +01:00
{
2020-08-22 21:37:02 +01:00
if ( Debugger . IsAttached )
throw ;
2017-04-20 06:56:49 +01:00
}
}
2020-08-22 21:37:02 +01:00
static void SetDefaultSettings ( ) = > Current = new SetSettings
2017-04-20 06:56:49 +01:00
{
2020-08-22 21:37:02 +01:00
TemporaryFolder = Path . GetTempPath ( ) ,
DatabasePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
"apprepodbmgr.db" ) ,
RepositoryPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "apprepo" ) ,
UnArchiverPath = null ,
CompressionAlgorithm = AlgoEnum . GZip ,
UseAntivirus = false ,
UseClamd = false ,
ClamdHost = null ,
ClamdPort = 3310 ,
ClamdIsLocal = false ,
UseVirusTotal = false ,
VirusTotalKey = null
} ;
2017-04-20 06:56:49 +01:00
}
2017-12-30 00:32:21 +00:00
}