2017-05-15 07:30:27 +01:00
//
// Author:
// Natalia Portillo claunia@claunia.com
//
// Copyright (c) 2017, © Claunia.com
//
// 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-05-15 07:30:27 +01:00
using System ;
2017-05-16 03:36:57 +01:00
using System.IO ;
using System.Threading ;
2017-05-15 07:30:27 +01:00
using Eto.Forms ;
using Eto.Serialization.Xaml ;
2017-05-15 07:33:13 +01:00
using osrepodbmgr.Core ;
2017-05-15 07:30:27 +01:00
namespace osrepodbmgr.Eto
{
2017-05-15 07:33:13 +01:00
public class dlgSettings : Dialog
2017-05-15 07:30:27 +01:00
{
2017-05-15 07:33:13 +01:00
string oldUnarPath ;
2017-05-15 07:30:27 +01:00
public dlgSettings ( )
{
XamlReader . Load ( this ) ;
2017-12-30 00:32:21 +00:00
txtTmp . Text = Settings . Current . TemporaryFolder ;
txtUnar . Text = Settings . Current . UnArchiverPath ;
txtDatabase . Text = Settings . Current . DatabasePath ;
2017-05-15 07:33:13 +01:00
txtRepository . Text = Settings . Current . RepositoryPath ;
2017-12-30 00:32:21 +00:00
if ( ! string . IsNullOrWhiteSpace ( txtUnar . Text ) ) CheckUnar ( ) ;
2017-05-15 07:33:13 +01:00
cmbCompAlg = new EnumDropDown < AlgoEnum > ( ) ;
StackLayoutForAlgoEnum . Items . Add ( new StackLayoutItem ( cmbCompAlg , HorizontalAlignment . Stretch , true ) ) ;
cmbCompAlg . SelectedValue = Settings . Current . CompressionAlgorithm ;
2017-05-18 22:23:49 +01:00
2017-12-30 00:32:21 +00:00
spClamdPort . Value = 3310 ;
chkAntivirus . Checked = Settings . Current . UseAntivirus ;
frmClamd . Visible = chkAntivirus . Checked . Value ;
if ( Settings . Current . UseAntivirus & & Settings . Current . UseClamd )
2017-05-18 22:23:49 +01:00
{
2017-12-30 00:32:21 +00:00
chkClamd . Checked = Settings . Current . UseClamd ;
txtClamdHost . Text = Settings . Current . ClamdHost ;
spClamdPort . Value = Settings . Current . ClamdPort ;
chkClamdIsLocal . Checked = Settings . Current . ClamdIsLocal ;
2017-05-19 01:13:53 +01:00
}
2017-12-30 00:32:21 +00:00
if ( ! Settings . Current . UseAntivirus | | ! Settings . Current . UseVirusTotal ) return ;
chkVirusTotal . Checked = true ;
chkVirusTotal . Enabled = true ;
txtVirusTotal . Enabled = true ;
txtVirusTotal . Text = Settings . Current . VirusTotalKey ;
btnVirusTotal . Enabled = true ;
2017-05-15 07:33:13 +01:00
}
protected void OnBtnCancelClicked ( object sender , EventArgs e )
{
Close ( ) ;
}
protected void OnBtnApplyClicked ( object sender , EventArgs e )
{
// TODO: Check sanity
2017-12-30 00:32:21 +00:00
Settings . Current . TemporaryFolder = txtTmp . Text ;
Settings . Current . UnArchiverPath = txtUnar . Text ;
Settings . Current . DatabasePath = txtDatabase . Text ;
Settings . Current . RepositoryPath = txtRepository . Text ;
2017-05-15 07:33:13 +01:00
Settings . Current . CompressionAlgorithm = cmbCompAlg . SelectedValue ;
2017-05-18 22:23:49 +01:00
if ( ! chkClamd . Checked . Value | | ! chkAntivirus . Checked . Value )
{
2017-12-30 00:32:21 +00:00
Settings . Current . UseClamd = false ;
Settings . Current . ClamdHost = null ;
Settings . Current . ClamdPort = 3310 ;
Settings . Current . ClamdIsLocal = false ;
2017-05-18 22:23:49 +01:00
}
2017-12-30 00:32:21 +00:00
2017-05-19 01:13:53 +01:00
if ( chkVirusTotal . Checked . Value & & chkAntivirus . Checked . Value )
{
2017-12-30 00:32:21 +00:00
Settings . Current . UseVirusTotal = true ;
Settings . Current . VirusTotalKey = txtVirusTotal . Text ;
Workers . InitVirusTotal ( Settings . Current . VirusTotalKey ) ;
2017-05-19 01:13:53 +01:00
}
else
{
2017-12-30 00:32:21 +00:00
Settings . Current . UseVirusTotal = false ;
Settings . Current . VirusTotalKey = null ;
2017-05-19 18:22:54 +01:00
}
2017-12-30 00:32:21 +00:00
2017-05-19 18:22:54 +01:00
Settings . SaveSettings ( ) ;
2017-05-15 07:33:13 +01:00
Workers . CloseDB ( ) ;
Workers . InitDB ( ) ;
2017-12-30 00:32:21 +00:00
Context . ClamdVersion = null ;
Workers . InitClamd ( ) ;
2017-05-15 07:33:13 +01:00
Context . CheckUnar ( ) ;
Close ( ) ;
}
protected void OnBtnUnarClicked ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
OpenFileDialog dlgFile = new OpenFileDialog { Title = "Choose UnArchiver executable" , MultiSelect = false } ;
2017-05-18 16:31:46 +01:00
if ( ! string . IsNullOrWhiteSpace ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) ) )
2017-05-19 18:22:54 +01:00
dlgFile . Directory = new Uri ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) ) ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
if ( dlgFile . ShowDialog ( this ) ! = DialogResult . Ok ) return ;
txtUnar . Text = dlgFile . FileName ;
lblUnarVersion . Visible = false ;
CheckUnar ( ) ;
2017-05-15 07:33:13 +01:00
}
protected void OnBtnTmpClicked ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
SelectFolderDialog dlgFolder =
new SelectFolderDialog { Title = "Choose temporary folder" , Directory = Path . GetTempPath ( ) } ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
if ( dlgFolder . ShowDialog ( this ) = = DialogResult . Ok ) txtTmp . Text = dlgFolder . Directory ;
2017-05-15 07:33:13 +01:00
}
protected void OnBtnRepositoryClicked ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
SelectFolderDialog dlgFolder =
new SelectFolderDialog { Title = "Choose repository folder" , Directory = Path . GetTempPath ( ) } ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
if ( dlgFolder . ShowDialog ( this ) = = DialogResult . Ok ) txtRepository . Text = dlgFolder . Directory ;
2017-05-15 07:33:13 +01:00
}
protected void OnBtnDatabaseClicked ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
SaveFileDialog dlgFile = new SaveFileDialog
{
Title = "Choose database to open/create" ,
Directory = new Uri ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ) ,
CheckFileExists = false ,
FileName = "osrepodbmgr.db"
} ;
if ( dlgFile . ShowDialog ( this ) ! = DialogResult . Ok ) return ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
if ( File . Exists ( dlgFile . FileName ) )
2017-05-15 07:33:13 +01:00
{
2017-12-30 00:32:21 +00:00
DbCore dbCore = new SQLite ( ) ;
bool notDb = false ;
try { notDb | = ! dbCore . OpenDb ( dlgFile . FileName , null , null , null ) ; }
catch { notDb = true ; }
if ( notDb )
2017-05-15 07:33:13 +01:00
{
2017-12-30 00:32:21 +00:00
MessageBox . Show ( "Cannot open specified file as a database, please choose another." ,
MessageBoxType . Error ) ;
return ;
2017-05-15 07:33:13 +01:00
}
2017-12-30 00:32:21 +00:00
dbCore . CloseDb ( ) ;
}
else
{
DbCore dbCore = new SQLite ( ) ;
bool notDb = false ;
try { notDb | = ! dbCore . CreateDb ( dlgFile . FileName , null , null , null ) ; }
catch { notDb = true ; }
if ( notDb )
2017-05-15 07:33:13 +01:00
{
2017-12-30 00:32:21 +00:00
MessageBox . Show ( "Cannot create a database in the specified file as a database." ,
MessageBoxType . Error ) ;
return ;
2017-05-15 07:33:13 +01:00
}
2017-12-30 00:32:21 +00:00
dbCore . CloseDb ( ) ;
2017-05-15 07:33:13 +01:00
}
2017-12-30 00:32:21 +00:00
txtDatabase . Text = dlgFile . FileName ;
2017-05-15 07:33:13 +01:00
}
void CheckUnar ( )
{
Workers . FinishedWithText + = CheckUnarFinished ;
2017-12-30 00:32:21 +00:00
Workers . Failed + = CheckUnarFailed ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
oldUnarPath = Settings . Current . UnArchiverPath ;
2017-05-15 07:33:13 +01:00
Settings . Current . UnArchiverPath = txtUnar . Text ;
2017-12-30 00:32:21 +00:00
Thread thdCheckUnar = new Thread ( Workers . CheckUnar ) ;
2017-05-15 07:33:13 +01:00
thdCheckUnar . Start ( ) ;
}
void CheckUnarFinished ( string text )
{
Application . Instance . Invoke ( delegate
{
Workers . FinishedWithText - = CheckUnarFinished ;
2017-12-30 00:32:21 +00:00
Workers . Failed - = CheckUnarFailed ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
lblUnarVersion . Text = text ;
lblUnarVersion . Visible = true ;
2017-05-15 07:33:13 +01:00
Settings . Current . UnArchiverPath = oldUnarPath ;
} ) ;
}
void CheckUnarFailed ( string text )
{
Application . Instance . Invoke ( delegate
{
Workers . FinishedWithText - = CheckUnarFinished ;
2017-12-30 00:32:21 +00:00
Workers . Failed - = CheckUnarFailed ;
2017-05-15 07:33:13 +01:00
2017-12-30 00:32:21 +00:00
txtUnar . Text = string . IsNullOrWhiteSpace ( oldUnarPath ) ? "" : oldUnarPath ;
2017-05-15 07:33:13 +01:00
Settings . Current . UnArchiverPath = oldUnarPath ;
MessageBox . Show ( text , MessageBoxType . Error ) ;
} ) ;
2017-05-15 07:30:27 +01:00
}
2017-05-18 22:23:49 +01:00
protected void OnChkAntivirusToggled ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
frmClamd . Visible = chkAntivirus . Checked . Value ;
2017-05-19 01:13:53 +01:00
frmVirusTotal . Visible = chkAntivirus . Checked . Value ;
2017-05-18 22:23:49 +01:00
}
protected void OnChkClamdToggled ( object sender , EventArgs e )
{
2017-12-30 00:32:21 +00:00
txtClamdHost . Enabled = chkClamd . Checked . Value ;
spClamdPort . Enabled = chkClamd . Checked . Value ;
btnClamdTest . Enabled = chkClamd . Checked . Value ;
2017-05-18 22:23:49 +01:00
lblClamdVersion . Visible = false ;
chkClamdIsLocal . Enabled = chkClamd . Checked . Value ;
}
protected void OnBtnClamdTestClicked ( object sender , EventArgs e )
{
lblClamdVersion . Visible = false ;
if ( string . IsNullOrEmpty ( txtClamdHost . Text ) )
{
MessageBox . Show ( "clamd host cannot be empty" , MessageBoxType . Error ) ;
return ;
}
2017-12-30 00:32:21 +00:00
string oldVersion = Context . ClamdVersion ;
Context . ClamdVersion = null ;
2017-05-18 22:23:49 +01:00
2017-12-30 00:32:21 +00:00
string oldHost = Settings . Current . ClamdHost ;
ushort oldPort = Settings . Current . ClamdPort ;
Settings . Current . ClamdHost = txtClamdHost . Text ;
Settings . Current . ClamdPort = ( ushort ) spClamdPort . Value ;
2017-05-18 22:23:49 +01:00
Workers . TestClamd ( ) ;
2017-12-30 00:32:21 +00:00
Settings . Current . ClamdHost = oldHost ;
Settings . Current . ClamdPort = oldPort ;
2017-05-18 22:23:49 +01:00
2017-12-30 00:32:21 +00:00
if ( string . IsNullOrEmpty ( Context . ClamdVersion ) )
2017-05-18 22:23:49 +01:00
{
MessageBox . Show ( "Cannot connect to clamd" , MessageBoxType . Error ) ;
return ;
}
2017-12-30 00:32:21 +00:00
lblClamdVersion . Text = Context . ClamdVersion ;
Context . ClamdVersion = oldVersion ;
2017-05-18 22:23:49 +01:00
lblClamdVersion . Visible = true ;
}
2017-05-19 01:13:53 +01:00
protected void OnChkVirusTotalToggled ( object sender , EventArgs e )
{
txtVirusTotal . Enabled = chkVirusTotal . Checked . Value ;
btnVirusTotal . Enabled = chkVirusTotal . Checked . Value ;
lblVirusTotal . Visible = false ;
}
protected void OnBtnVirusTotalClicked ( object sender , EventArgs e )
{
Workers . Failed + = VirusTotalTestFailed ;
2017-12-30 00:32:21 +00:00
if ( ! Workers . TestVirusTotal ( txtVirusTotal . Text ) ) return ;
lblVirusTotal . Visible = true ;
lblVirusTotal . Text = "Working!" ;
2017-05-19 01:13:53 +01:00
}
2017-12-30 00:32:21 +00:00
static void VirusTotalTestFailed ( string text )
2017-05-19 01:13:53 +01:00
{
MessageBox . Show ( text , MessageBoxType . Error ) ;
}
2017-12-30 00:32:21 +00:00
#region XAML UI elements
#pragma warning disable 0649
TextBox txtTmp ;
TextBox txtUnar ;
TextBox txtDatabase ;
TextBox txtRepository ;
Label lblUnarVersion ;
EnumDropDown < AlgoEnum > cmbCompAlg ;
StackLayout StackLayoutForAlgoEnum ;
GroupBox frmClamd ;
CheckBox chkAntivirus ;
CheckBox chkClamd ;
TextBox txtClamdHost ;
NumericUpDown spClamdPort ;
Button btnClamdTest ;
Label lblClamdVersion ;
CheckBox chkClamdIsLocal ;
GroupBox frmVirusTotal ;
CheckBox chkVirusTotal ;
TextBox txtVirusTotal ;
Button btnVirusTotal ;
Label lblVirusTotal ;
#pragma warning restore 0649
#endregion XAML UI elements
2017-05-15 07:30:27 +01:00
}
2017-12-30 00:32:21 +00:00
}