2008-12-10 20:19:34 -05:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2006 , 2007 crypton
*
* 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-07-12 17:03:26 -04:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2008-12-10 20:19:34 -05:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "ShareManager.h"
# include "rsiface/rsfiles.h"
2009-01-01 12:47:35 -05:00
# include <QContextMenuEvent>
# include <QMenu>
2009-08-03 15:43:52 -04:00
# include <QCheckBox>
2009-01-01 12:47:35 -05:00
# include <QCursor>
# include <QPoint>
# include <QMouseEvent>
# include <QPixmap>
2008-12-10 20:19:34 -05:00
# include <QMessageBox>
2009-08-03 15:43:52 -04:00
# include <QComboBox>
2008-12-10 20:19:34 -05:00
2009-01-01 12:47:35 -05:00
/* Images for context menu icons */
# define IMAGE_CANCEL ": / images / delete.png"
2009-08-03 15:43:52 -04:00
ShareManager * ShareManager : : _instance = NULL ;
2008-12-10 20:19:34 -05:00
/** Default constructor */
ShareManager : : ShareManager ( QWidget * parent , Qt : : WFlags flags )
: QDialog ( parent , flags )
{
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
connect ( ui . addButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( addShareDirectory ( ) ) ) ;
connect ( ui . removeButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( removeShareDirectory ( ) ) ) ;
connect ( ui . buttonBox , SIGNAL ( rejected ( ) ) , this , SLOT ( reject ( ) ) ) ;
connect ( ui . buttonBox , SIGNAL ( accepted ( ) ) , this , SLOT ( accept ( ) ) ) ;
2009-01-01 12:47:35 -05:00
connect ( ui . shareddirList , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( shareddirListCostumPopupMenu ( QPoint ) ) ) ;
2008-12-10 20:19:34 -05:00
ui . addButton - > setToolTip ( tr ( " Add a Share Directory " ) ) ;
ui . removeButton - > setToolTip ( tr ( " Remove selected Shared Directory " ) ) ;
2009-07-12 17:03:26 -04:00
2008-12-11 05:14:35 -05:00
load ( ) ;
2008-12-10 20:19:34 -05:00
}
2009-01-01 12:47:35 -05:00
void ShareManager : : shareddirListCostumPopupMenu ( QPoint point )
{
QMenu contextMnu ( this ) ;
QMouseEvent * mevent = new QMouseEvent ( QEvent : : MouseButtonPress , point , Qt : : RightButton , Qt : : RightButton , Qt : : NoModifier ) ;
removeAct = new QAction ( QIcon ( IMAGE_CANCEL ) , tr ( " Remove " ) , this ) ;
connect ( removeAct , SIGNAL ( triggered ( ) ) , this , SLOT ( removeShareDirectory ( ) ) ) ;
contextMnu . clear ( ) ;
contextMnu . addAction ( removeAct ) ;
contextMnu . exec ( mevent - > globalPos ( ) ) ;
}
2008-12-10 20:19:34 -05:00
/** Loads the settings for this page */
void ShareManager : : load ( )
{
2009-08-03 15:43:52 -04:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
2008-12-10 20:19:34 -05:00
rsFiles - > getSharedDirectories ( dirs ) ;
2009-07-12 17:03:26 -04:00
2008-12-10 20:19:34 -05:00
/* get a link to the table */
2009-08-03 15:43:52 -04:00
QTableWidget * listWidget = ui . shareddirList ;
2009-07-12 17:03:26 -04:00
2008-12-10 20:19:34 -05:00
/* remove old items ??? */
2009-08-03 15:43:52 -04:00
listWidget - > clearContents ( ) ;
listWidget - > setRowCount ( 0 ) ;
connect ( this , SIGNAL ( itemClicked ( QTableWidgetItem * ) ) , this , SLOT ( updateFlags ( QTableWidgetItem * ) ) ) ;
2009-07-12 17:03:26 -04:00
2009-08-03 15:43:52 -04:00
int row = 0 ;
for ( it = dirs . begin ( ) ; it ! = dirs . end ( ) ; it + + , + + row )
2008-12-10 20:19:34 -05:00
{
2009-08-03 15:43:52 -04:00
listWidget - > insertRow ( row ) ;
listWidget - > setItem ( row , 0 , new QTableWidgetItem ( QString : : fromStdString ( ( * it ) . filename ) ) ) ;
# ifdef USE_COMBOBOX
QComboBox * cb = new QComboBox ;
cb - > addItem ( QString ( " Network Wide " ) ) ;
cb - > addItem ( QString ( " Browsable " ) ) ;
cb - > addItem ( QString ( " Universal " ) ) ;
cb - > setToolTip ( QString ( " Decide here whether this directory is \n * Network Wide: \t anonymously shared over the network (including your friends) \n * Browsable: \t browsable by your friends \n * Universal: \t \t both " ) ) ;
// TODO
// - set combobox current value depending on what rsFiles reports.
// - use a signal mapper to get the correct row that contains the combo box sending the signal:
// mapper = new SignalMapper(this) ;
//
// for(all cb)
// {
// signalMapper->setMapping(cb,...)
// }
//
int index = 0 ;
index + = ( ( * it ) . shareflags & RS_FILE_HINTS_NETWORK_WIDE ) > 0 ;
index + = ( ( ( * it ) . shareflags & RS_FILE_HINTS_BROWSABLE ) > 0 ) * 2 ;
listWidget - > setCellWidget ( row , 1 , cb ) ;
if ( index < 1 | | index > 3 )
std : : cerr < < " ******* ERROR IN FILE SHARING FLAGS. Flags = " < < ( * it ) . shareflags < < " *********** " < < std : : endl ;
else
index - - ;
cb - > setCurrentIndex ( index ) ;
# else
QCheckBox * cb1 = new QCheckBox ;
QCheckBox * cb2 = new QCheckBox ;
cb1 - > setChecked ( ( * it ) . shareflags & RS_FILE_HINTS_NETWORK_WIDE ) ;
cb2 - > setChecked ( ( * it ) . shareflags & RS_FILE_HINTS_BROWSABLE ) ;
cb1 - > setToolTip ( QString ( " If checked, the share is anonymously shared to anybody. " ) ) ;
cb2 - > setToolTip ( QString ( " If checked, the share is browsable by your friends. " ) ) ;
listWidget - > setCellWidget ( row , 1 , cb1 ) ;
listWidget - > setCellWidget ( row , 2 , cb2 ) ;
QObject : : connect ( cb1 , SIGNAL ( toggled ( bool ) ) , this , SLOT ( updateFlags ( bool ) ) ) ;
QObject : : connect ( cb2 , SIGNAL ( toggled ( bool ) ) , this , SLOT ( updateFlags ( bool ) ) ) ;
# endif
2008-12-10 20:19:34 -05:00
}
//ui.incomingDir->setText(QString::fromStdString(rsFiles->getDownloadDirectory()));
2009-07-12 17:03:26 -04:00
2008-12-10 20:19:34 -05:00
listWidget - > update ( ) ; /* update display */
2009-08-03 15:43:52 -04:00
}
2008-12-10 20:19:34 -05:00
2009-08-03 15:43:52 -04:00
void ShareManager : : showYourself ( )
{
if ( _instance = = NULL )
_instance = new ShareManager ( NULL , 0 ) ;
2008-12-10 20:19:34 -05:00
2009-08-03 15:43:52 -04:00
_instance - > show ( ) ;
2008-12-10 20:19:34 -05:00
}
void ShareManager : : addShareDirectory ( )
{
/* select a dir
*/
QString qdir = QFileDialog : : getExistingDirectory ( this , tr ( " Add Shared Directory " ) , " " ,
QFileDialog : : ShowDirsOnly | QFileDialog : : DontResolveSymlinks ) ;
2009-07-12 17:03:26 -04:00
2008-12-10 20:19:34 -05:00
/* add it to the server */
std : : string dir = qdir . toStdString ( ) ;
if ( dir ! = " " )
{
2009-08-03 15:43:52 -04:00
SharedDirInfo sdi ;
sdi . filename = dir ;
sdi . shareflags = RS_FILE_HINTS_NETWORK_WIDE | RS_FILE_HINTS_BROWSABLE ;
rsFiles - > addSharedDirectory ( sdi ) ;
2008-12-10 20:19:34 -05:00
load ( ) ;
2009-04-14 18:57:53 -04:00
messageBoxOk ( tr ( " Shared Directory Added! " ) ) ;
2008-12-10 20:19:34 -05:00
}
}
2009-08-03 15:43:52 -04:00
void ShareManager : : updateFlags ( bool b )
{
std : : cerr < < " Updating flags (b= " < < b < < " ) !!! " < < std : : endl ;
std : : list < SharedDirInfo > : : iterator it ;
std : : list < SharedDirInfo > dirs ;
rsFiles - > getSharedDirectories ( dirs ) ;
int row = 0 ;
for ( it = dirs . begin ( ) ; it ! = dirs . end ( ) ; it + + , + + row )
{
std : : cerr < < " Looking for row= " < < row < < " , file= " < < ( * it ) . filename < < " , flags= " < < ( * it ) . shareflags < < std : : endl ;
uint32_t current_flags = 0 ;
current_flags | = ( dynamic_cast < QCheckBox * > ( ui . shareddirList - > cellWidget ( row , 1 ) ) ) - > isChecked ( ) ? RS_FILE_HINTS_NETWORK_WIDE : 0 ;
current_flags | = ( dynamic_cast < QCheckBox * > ( ui . shareddirList - > cellWidget ( row , 2 ) ) ) - > isChecked ( ) ? RS_FILE_HINTS_BROWSABLE : 0 ;
if ( ( * it ) . shareflags ^ current_flags )
{
( * it ) . shareflags = current_flags ;
rsFiles - > updateShareFlags ( * it ) ; // modifies the flags
std : : cout < < " Updating share flags for directory " < < ( * it ) . filename < < std : : endl ;
}
}
}
2008-12-10 20:19:34 -05:00
void ShareManager : : removeShareDirectory ( )
{
/* id current dir */
/* ask for removal */
2009-08-03 15:43:52 -04:00
QTableWidget * listWidget = ui . shareddirList ;
int row = listWidget - > currentRow ( ) ;
QTableWidgetItem * qdir = listWidget - > item ( row , 0 ) ;
2009-01-06 17:35:22 -05:00
QString queryWrn ;
queryWrn . clear ( ) ;
2009-08-03 15:43:52 -04:00
queryWrn . append ( tr ( " Do you really want to stop sharing this directory ? " ) ) ;
2008-12-10 20:19:34 -05:00
if ( qdir )
{
2009-01-06 17:35:22 -05:00
if ( ( QMessageBox : : question ( this , tr ( " Warning! " ) , queryWrn , QMessageBox : : Ok | QMessageBox : : No , QMessageBox : : Ok ) ) = = QMessageBox : : Ok )
{
rsFiles - > removeSharedDirectory ( qdir - > text ( ) . toStdString ( ) ) ;
load ( ) ;
}
else
return ;
2008-12-10 20:19:34 -05:00
}
}
2009-01-06 17:35:22 -05:00
bool ShareManager : : messageBoxOk ( QString msg )
{
QMessageBox mb ( " Share Manager InfoBox! " , msg , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 ) ;
mb . setButtonText ( QMessageBox : : Ok , " OK " ) ;
mb . exec ( ) ;
return true ;
}
2008-12-10 20:19:34 -05:00
2009-07-12 17:03:26 -04:00
void ShareManager : : showEvent ( QShowEvent * event )
{
if ( ! event - > spontaneous ( ) )
{
load ( ) ;
}
}