2017-04-20 06:56:49 +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.
//
using System ;
2017-04-20 21:20:37 +01:00
using System.IO ;
using System.Threading ;
2017-04-20 06:56:49 +01:00
using Gtk ;
2017-05-11 15:57:02 +01:00
using osrepodbmgr.Core ;
2017-04-20 06:56:49 +01:00
namespace osrepodbmgr
{
2017-05-11 15:50:07 +01:00
public partial class dlgSettings : Dialog
2017-04-20 06:56:49 +01:00
{
2017-04-20 21:20:37 +01:00
string oldUnarPath ;
2017-05-11 15:50:07 +01:00
public dlgSettings ( )
2017-04-20 06:56:49 +01:00
{
Build ( ) ;
2017-05-10 21:04:42 +01:00
txtTmp . Text = Core . Settings . Current . TemporaryFolder ;
txtUnar . Text = Core . Settings . Current . UnArchiverPath ;
txtDatabase . Text = Core . Settings . Current . DatabasePath ;
txtRepository . Text = Core . Settings . Current . RepositoryPath ;
2017-04-20 21:20:37 +01:00
if ( ! string . IsNullOrWhiteSpace ( txtUnar . Text ) )
CheckUnar ( ) ;
2017-05-11 03:46:19 +01:00
CellRendererText textCell = new CellRendererText ( ) ;
cmbCompAlg . Clear ( ) ;
ListStore lstCompAlgs = new ListStore ( typeof ( string ) ) ;
cmbCompAlg . PackStart ( textCell , true ) ;
cmbCompAlg . AddAttribute ( textCell , "text" , 0 ) ;
cmbCompAlg . Model = lstCompAlgs ;
lstCompAlgs . Clear ( ) ;
2017-05-11 19:01:47 +01:00
foreach ( AlgoEnum type in Enum . GetValues ( typeof ( AlgoEnum ) ) )
2017-05-11 03:46:19 +01:00
lstCompAlgs . AppendValues ( type . ToString ( ) ) ;
cmbCompAlg . Active = 0 ;
TreeIter iter ;
cmbCompAlg . Model . GetIterFirst ( out iter ) ;
do
{
if ( ( string ) cmbCompAlg . Model . GetValue ( iter , 0 ) = = Core . Settings . Current . CompressionAlgorithm . ToString ( ) )
{
cmbCompAlg . SetActiveIter ( iter ) ;
break ;
}
}
while ( cmbCompAlg . Model . IterNext ( ref iter ) ) ;
2017-04-20 06:56:49 +01:00
}
protected void OnBtnCancelClicked ( object sender , EventArgs e )
{
2017-05-11 14:50:29 +01:00
btnDialog . Click ( ) ;
2017-04-20 06:56:49 +01:00
}
protected void OnBtnApplyClicked ( object sender , EventArgs e )
{
// TODO: Check sanity
2017-05-10 21:04:42 +01:00
Core . Settings . Current . TemporaryFolder = txtTmp . Text ;
Core . Settings . Current . UnArchiverPath = txtUnar . Text ;
Core . Settings . Current . DatabasePath = txtDatabase . Text ;
Core . Settings . Current . RepositoryPath = txtRepository . Text ;
2017-05-11 19:01:47 +01:00
Core . Settings . Current . CompressionAlgorithm = ( AlgoEnum ) Enum . Parse ( typeof ( AlgoEnum ) , cmbCompAlg . ActiveText ) ;
2017-05-10 21:04:42 +01:00
Core . Settings . SaveSettings ( ) ;
2017-05-10 21:06:28 +01:00
Core . Workers . CloseDB ( ) ;
Core . Workers . InitDB ( ) ;
2017-05-10 21:04:42 +01:00
Context . CheckUnar ( ) ;
2017-05-11 14:50:29 +01:00
btnDialog . Click ( ) ;
2017-04-20 06:56:49 +01:00
}
protected void OnBtnUnarClicked ( object sender , EventArgs e )
{
FileChooserDialog dlgFile = new FileChooserDialog ( "Choose UnArchiver executable" , this , FileChooserAction . Open ,
"Cancel" , ResponseType . Cancel , "Choose" , ResponseType . Accept ) ;
dlgFile . SelectMultiple = false ;
dlgFile . SetCurrentFolder ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) ) ;
if ( dlgFile . Run ( ) = = ( int ) ResponseType . Accept )
{
txtUnar . Text = dlgFile . Filename ;
2017-04-20 21:20:37 +01:00
lblUnarVersion . Visible = false ;
CheckUnar ( ) ;
2017-04-20 06:56:49 +01:00
}
dlgFile . Destroy ( ) ;
}
protected void OnBtnTmpClicked ( object sender , EventArgs e )
{
FileChooserDialog dlgFolder = new FileChooserDialog ( "Choose temporary folder" , this , FileChooserAction . SelectFolder ,
"Cancel" , ResponseType . Cancel , "Choose" , ResponseType . Accept ) ;
dlgFolder . SelectMultiple = false ;
dlgFolder . SetCurrentFolder ( System . IO . Path . GetTempPath ( ) ) ;
if ( dlgFolder . Run ( ) = = ( int ) ResponseType . Accept )
{
txtTmp . Text = dlgFolder . Filename ;
}
dlgFolder . Destroy ( ) ;
}
protected void OnBtnRepositoryClicked ( object sender , EventArgs e )
{
FileChooserDialog dlgFolder = new FileChooserDialog ( "Choose repository folder" , this , FileChooserAction . SelectFolder ,
"Cancel" , ResponseType . Cancel , "Choose" , ResponseType . Accept ) ;
dlgFolder . SelectMultiple = false ;
dlgFolder . SetCurrentFolder ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ) ;
if ( dlgFolder . Run ( ) = = ( int ) ResponseType . Accept )
{
txtRepository . Text = dlgFolder . Filename ;
}
dlgFolder . Destroy ( ) ;
}
protected void OnBtnDatabaseClicked ( object sender , EventArgs e )
{
FileChooserDialog dlgFile = new FileChooserDialog ( "Choose database to open/create" , this , FileChooserAction . Save ,
"Cancel" , ResponseType . Cancel , "Choose" , ResponseType . Accept ) ;
dlgFile . SelectMultiple = false ;
dlgFile . SetCurrentFolder ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ) ;
2017-05-10 21:04:42 +01:00
dlgFile . SetFilename ( "Core.db" ) ;
2017-04-20 06:56:49 +01:00
if ( dlgFile . Run ( ) = = ( int ) ResponseType . Accept )
{
2017-04-20 21:20:37 +01:00
if ( File . Exists ( dlgFile . Filename ) )
2017-04-20 06:56:49 +01:00
{
DBCore _dbCore = new SQLite ( ) ;
bool notDb = false ;
try
{
notDb | = ! _dbCore . OpenDB ( dlgFile . Filename , null , null , null ) ;
}
catch
{
notDb = true ;
}
if ( notDb )
{
MessageDialog dlgMsg = new MessageDialog ( this , DialogFlags . Modal , MessageType . Error , ButtonsType . Ok , "Cannot open specified file as a database, please choose another." ) ;
dlgMsg . Run ( ) ;
dlgMsg . Destroy ( ) ;
dlgFile . Destroy ( ) ;
return ;
}
_dbCore . CloseDB ( ) ;
}
else {
DBCore _dbCore = new SQLite ( ) ;
bool notDb = false ;
try
{
notDb | = ! _dbCore . CreateDB ( dlgFile . Filename , null , null , null ) ;
}
catch
{
notDb = true ;
}
if ( notDb )
{
MessageDialog dlgMsg = new MessageDialog ( this , DialogFlags . Modal , MessageType . Error , ButtonsType . Ok , "Cannot create a database in the specified file as a database." ) ;
dlgMsg . Run ( ) ;
dlgMsg . Destroy ( ) ;
dlgFile . Destroy ( ) ;
return ;
}
_dbCore . CloseDB ( ) ;
}
2017-05-03 23:01:47 +01:00
txtDatabase . Text = dlgFile . Filename ;
2017-04-20 06:56:49 +01:00
}
dlgFile . Destroy ( ) ;
}
2017-04-20 21:20:37 +01:00
void CheckUnar ( )
{
2017-05-10 21:06:28 +01:00
Core . Workers . FinishedWithText + = CheckUnarFinished ;
Core . Workers . Failed + = CheckUnarFailed ;
2017-04-20 21:20:37 +01:00
2017-05-10 21:04:42 +01:00
oldUnarPath = Core . Settings . Current . UnArchiverPath ;
Core . Settings . Current . UnArchiverPath = txtUnar . Text ;
2017-05-10 21:06:28 +01:00
Thread thdCheckUnar = new Thread ( Core . Workers . CheckUnar ) ;
2017-04-20 21:20:37 +01:00
thdCheckUnar . Start ( ) ;
}
void CheckUnarFinished ( string text )
{
Application . Invoke ( delegate
{
2017-05-10 21:06:28 +01:00
Core . Workers . FinishedWithText - = CheckUnarFinished ;
Core . Workers . Failed - = CheckUnarFailed ;
2017-04-20 21:20:37 +01:00
lblUnarVersion . Text = text ;
lblUnarVersion . Visible = true ;
2017-05-10 21:04:42 +01:00
Core . Settings . Current . UnArchiverPath = oldUnarPath ;
2017-04-20 21:20:37 +01:00
} ) ;
}
void CheckUnarFailed ( string text )
{
Application . Invoke ( delegate
{
2017-05-10 21:06:28 +01:00
Core . Workers . FinishedWithText - = CheckUnarFinished ;
Core . Workers . Failed - = CheckUnarFailed ;
2017-04-20 21:20:37 +01:00
if ( string . IsNullOrWhiteSpace ( oldUnarPath ) )
txtUnar . Text = "" ;
else
txtUnar . Text = oldUnarPath ;
2017-05-10 21:04:42 +01:00
Core . Settings . Current . UnArchiverPath = oldUnarPath ;
2017-04-20 21:20:37 +01:00
MessageDialog dlgMsg = new MessageDialog ( this , DialogFlags . Modal , MessageType . Error , ButtonsType . Ok , text ) ;
dlgMsg . Run ( ) ;
dlgMsg . Destroy ( ) ;
} ) ;
}
2017-04-20 06:56:49 +01:00
}
}