2009-07-23 17:11:51 +00:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2006 - 2009 RetroShare Team
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
2009-08-19 22:15:16 +00:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2009-07-23 17:11:51 +00:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "DirectoriesPage.h"
2009-08-17 20:19:02 +00:00
# include "gui/ShareManager.h"
2010-10-31 20:35:31 +00:00
# include <QMessageBox>
2009-08-17 20:19:02 +00:00
2009-07-23 17:11:51 +00:00
# include "rshare.h"
2010-08-06 09:40:23 +00:00
# include <retroshare/rsfiles.h>
2009-07-23 17:11:51 +00:00
# include <algorithm>
DirectoriesPage : : DirectoriesPage ( QWidget * parent , Qt : : WFlags flags )
2009-08-19 22:15:16 +00:00
: ConfigPage ( parent , flags )
2009-07-23 17:11:51 +00:00
{
ui . setupUi ( this ) ;
setAttribute ( Qt : : WA_QuitOnClose , false ) ;
2009-09-16 00:03:51 +00:00
//load();
2009-08-19 22:15:16 +00:00
2009-08-17 20:19:02 +00:00
# ifdef TO_REMOVE
ui . addButton - > setToolTip ( tr ( " Add a Share Directory " ) ) ;
ui . removeButton - > setToolTip ( tr ( " Remove Shared Directory " ) ) ;
# endif
2009-07-23 17:11:51 +00:00
ui . incomingButton - > setToolTip ( tr ( " Browse " ) ) ;
ui . partialButton - > setToolTip ( tr ( " Browse " ) ) ;
if ( rsFiles - > getShareDownloadDirectory ( ) )
{
2010-04-24 22:09:47 +00:00
ui . checkBox - > setChecked ( true ) ; /* signal not emitted */
2009-07-23 17:11:51 +00:00
}
else
{
2010-04-24 22:09:47 +00:00
ui . checkBox - > setChecked ( false ) ; /* signal not emitted */
2009-07-23 17:11:51 +00:00
}
2010-10-31 20:35:31 +00:00
uint32_t t = rsFiles - > rememberHashFilesDuration ( ) ;
bool b = rsFiles - > rememberHashFiles ( ) ;
ui . rememberHashesSB - > setValue ( t ) ;
ui . rememberHashesCB - > setChecked ( b ) ;
2010-04-24 22:09:47 +00:00
connect ( ui . incomingButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( setIncomingDirectory ( ) ) ) ;
connect ( ui . partialButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( setPartialsDirectory ( ) ) ) ;
connect ( ui . checkBox , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( shareDownloadDirectory ( int ) ) ) ;
connect ( ui . editButton , SIGNAL ( clicked ( ) ) , this , SLOT ( editDirectories ( ) ) ) ;
2010-10-31 20:35:31 +00:00
connect ( ui . cleanHashCachePB , SIGNAL ( clicked ( ) ) , this , SLOT ( clearHashCache ( ) ) ) ;
connect ( ui . rememberHashesCB , SIGNAL ( toggled ( bool ) ) , this , SLOT ( toggleRememberHashes ( bool ) ) ) ;
connect ( ui . rememberHashesSB , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( setRememberHashesDuration ( int ) ) ) ;
2010-04-24 22:09:47 +00:00
2009-07-23 17:11:51 +00:00
/* Hide platform specific features */
# ifdef Q_WS_WIN
# endif
}
2010-10-31 20:35:31 +00:00
void DirectoriesPage : : setRememberHashesDuration ( int d )
{
rsFiles - > setRememberHashFilesDuration ( d ) ;
}
void DirectoriesPage : : toggleRememberHashes ( bool b )
{
if ( ! b )
{
if ( QMessageBox : : question ( NULL , " Cache cleaning confirmation " , " The will forget any former hash of non shared files. Do you confirm ? " ) = = QMessageBox : : Ok )
{
rsFiles - > clearHashCache ( ) ;
rsFiles - > setRememberHashFiles ( b ) ;
ui . rememberHashesSB - > setEnabled ( false ) ;
ui . cleanHashCachePB - > setEnabled ( false ) ;
}
else
ui . rememberHashesCB - > setChecked ( true ) ;
}
else
{
rsFiles - > setRememberHashFiles ( true ) ;
ui . rememberHashesSB - > setEnabled ( true ) ;
ui . cleanHashCachePB - > setEnabled ( true ) ;
}
}
void DirectoriesPage : : clearHashCache ( )
{
if ( QMessageBox : : question ( NULL , " Cache cleaning confirmation " , " The will forget any former hash of non shared files. Do you confirm ? " , QMessageBox : : Ok | QMessageBox : : Cancel ) = = QMessageBox : : Ok )
rsFiles - > clearHashCache ( ) ;
}
2009-07-23 17:11:51 +00:00
void
DirectoriesPage : : closeEvent ( QCloseEvent * event )
{
QWidget : : closeEvent ( event ) ;
}
2009-08-17 20:19:02 +00:00
void DirectoriesPage : : editDirectories ( )
{
ShareManager : : showYourself ( ) ;
}
2009-07-23 17:11:51 +00:00
/** Saves the changes on this page */
2009-08-17 20:19:02 +00:00
bool DirectoriesPage : : save ( QString & errmsg )
2009-07-23 17:11:51 +00:00
{
/* this is usefull especially when shared incoming files is
* default option and when the user don ' t check / uncheck the
* checkBox , so no signal is emitted to update the shared list */
if ( ui . checkBox - > isChecked ( ) )
{
2009-08-17 20:19:02 +00:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
2009-07-23 17:11:51 +00:00
rsFiles - > getSharedDirectories ( dirs ) ;
2009-08-17 20:19:02 +00:00
bool found = false ;
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirs . begin ( ) ) ; it ! = dirs . end ( ) ; + + it )
if ( ( * it ) . filename = = rsFiles - > getDownloadDirectory ( ) )
{
found = true ;
break ;
}
if ( ! found )
2009-07-23 17:11:51 +00:00
rsFiles - > shareDownloadDirectory ( ) ;
2009-08-17 20:19:02 +00:00
2009-07-23 17:11:51 +00:00
rsFiles - > setShareDownloadDirectory ( true ) ;
}
else
{
rsFiles - > unshareDownloadDirectory ( ) ;
rsFiles - > setShareDownloadDirectory ( false ) ;
}
return true ;
}
2009-08-19 22:15:16 +00:00
2009-07-23 17:11:51 +00:00
/** Loads the settings for this page */
void DirectoriesPage : : load ( )
{
2009-08-17 20:19:02 +00:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
2009-07-23 17:11:51 +00:00
rsFiles - > getSharedDirectories ( dirs ) ;
/* get a link to the table */
QListWidget * listWidget = ui . dirList ;
2010-04-24 22:09:47 +00:00
/* save current index */
QModelIndex rootIndex = listWidget - > rootIndex ( ) ;
2009-07-23 17:11:51 +00:00
/* remove old items ??? */
listWidget - > clear ( ) ;
for ( it = dirs . begin ( ) ; it ! = dirs . end ( ) ; it + + )
{
/* (0) Dir Name */
2010-09-27 21:42:26 +00:00
listWidget - > addItem ( QString : : fromUtf8 ( ( * it ) . filename . c_str ( ) ) ) ;
2009-07-23 17:11:51 +00:00
}
2010-04-24 22:09:47 +00:00
/* set saved index */
listWidget - > setCurrentIndex ( rootIndex ) ;
2010-09-27 21:42:26 +00:00
ui . incomingDir - > setText ( QString : : fromUtf8 ( rsFiles - > getDownloadDirectory ( ) . c_str ( ) ) ) ;
ui . partialsDir - > setText ( QString : : fromUtf8 ( rsFiles - > getPartialsDirectory ( ) . c_str ( ) ) ) ;
2009-07-23 17:11:51 +00:00
listWidget - > update ( ) ; /* update display */
}
2009-08-17 20:19:02 +00:00
# ifdef TO_REMOVE
2009-07-23 17:11:51 +00:00
void DirectoriesPage : : addShareDirectory ( )
{
/* select a dir
*/
int ind ;
QString qdir = QFileDialog : : getExistingDirectory ( this , tr ( " Add Shared Directory " ) , " " ,
QFileDialog : : ShowDirsOnly | QFileDialog : : DontResolveSymlinks ) ;
ind = qdir . lastIndexOf ( " / " ) ;
/* add it to the server */
std : : string dir = qdir . toStdString ( ) ;
if ( dir ! = " " )
{
rsFiles - > addSharedDirectory ( dir ) ;
load ( ) ;
}
}
void DirectoriesPage : : removeShareDirectory ( )
{
/* id current dir */
/* ask for removal */
QListWidget * listWidget = ui . dirList ;
QListWidgetItem * qdir = listWidget - > currentItem ( ) ;
if ( qdir )
{
rsFiles - > removeSharedDirectory ( qdir - > text ( ) . toStdString ( ) ) ;
load ( ) ;
}
}
2009-08-17 20:19:02 +00:00
# endif
2009-07-23 17:11:51 +00:00
void DirectoriesPage : : setIncomingDirectory ( )
{
QString qdir = QFileDialog : : getExistingDirectory ( this , tr ( " Set Incoming Directory " ) , " " ,
QFileDialog : : ShowDirsOnly | QFileDialog : : DontResolveSymlinks ) ;
std : : string dir = qdir . toStdString ( ) ;
if ( dir ! = " " )
{
rsFiles - > setDownloadDirectory ( dir ) ;
if ( ui . checkBox - > isChecked ( ) )
{
2009-08-17 20:19:02 +00:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
2009-07-23 17:11:51 +00:00
rsFiles - > getSharedDirectories ( dirs ) ;
2009-08-17 20:19:02 +00:00
bool found = false ;
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirs . begin ( ) ) ; it ! = dirs . end ( ) ; + + it )
if ( ( * it ) . filename = = rsFiles - > getDownloadDirectory ( ) )
{
found = true ;
break ;
}
if ( ! found )
2009-07-23 17:11:51 +00:00
rsFiles - > shareDownloadDirectory ( ) ;
}
}
load ( ) ;
}
void DirectoriesPage : : setPartialsDirectory ( )
{
QString qdir = QFileDialog : : getExistingDirectory ( this , tr ( " Set Partials Directory " ) , " " ,
QFileDialog : : ShowDirsOnly | QFileDialog : : DontResolveSymlinks ) ;
std : : string dir = qdir . toStdString ( ) ;
if ( dir ! = " " )
{
rsFiles - > setPartialsDirectory ( dir ) ;
}
load ( ) ;
}
void DirectoriesPage : : shareDownloadDirectory ( int state )
{
if ( state = = Qt : : Checked )
{
2009-08-17 20:19:02 +00:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
2009-07-23 17:11:51 +00:00
rsFiles - > getSharedDirectories ( dirs ) ;
2009-08-17 20:19:02 +00:00
bool found = false ;
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirs . begin ( ) ) ; it ! = dirs . end ( ) ; + + it )
if ( ( * it ) . filename = = rsFiles - > getDownloadDirectory ( ) )
{
found = true ;
break ;
}
if ( ! found )
2009-07-23 17:11:51 +00:00
rsFiles - > shareDownloadDirectory ( ) ;
2009-08-17 20:19:02 +00:00
2009-07-23 17:11:51 +00:00
rsFiles - > setShareDownloadDirectory ( true ) ;
}
else
{
rsFiles - > unshareDownloadDirectory ( ) ;
rsFiles - > setShareDownloadDirectory ( false ) ;
}
load ( ) ;
}