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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-01-01 12:47:35 -05:00
# include <QContextMenuEvent>
# include <QMenu>
2009-08-03 15:43:52 -04:00
# include <QCheckBox>
2009-08-04 16:46:26 -04:00
# include <QHeaderView>
2008-12-10 20:19:34 -05:00
# include <QMessageBox>
2011-04-26 19:38:29 -04:00
# include <QUrl>
2008-12-10 20:19:34 -05:00
2010-09-18 12:05:32 -04:00
# include <retroshare/rsfiles.h>
# include "ShareManager.h"
# include "ShareDialog.h"
# include "settings/rsharesettings.h"
2009-01-01 12:47:35 -05:00
/* Images for context menu icons */
# define IMAGE_CANCEL ": / images / delete.png"
2010-09-23 17:08:22 -04:00
# define IMAGE_EDIT ": / images / edit_16.png"
2009-01-01 12:47:35 -05:00
2010-09-17 14:27:30 -04:00
# define COLUMN_PATH 0
# define COLUMN_VIRTUALNAME 1
# define COLUMN_NETWORKWIDE 2
# define COLUMN_BROWSABLE 3
2010-09-18 12:05:32 -04:00
# define COLUMN_COUNT 3
2010-09-17 14:27:30 -04:00
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 )
{
2010-09-17 14:27:30 -04:00
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
2008-12-10 20:19:34 -05:00
2012-08-29 12:19:27 -04:00
ui . headerFrame - > setHeaderImage ( QPixmap ( " :/images/fileshare64.png " ) ) ;
2012-08-24 06:49:08 -04:00
ui . headerFrame - > setHeaderText ( tr ( " Share Manager " ) ) ;
2010-09-17 14:27:30 -04:00
isLoading = false ;
load ( ) ;
2008-12-10 20:19:34 -05:00
2010-09-18 12:05:32 -04:00
Settings - > loadWidgetInformation ( this ) ;
2010-09-17 14:27:30 -04:00
connect ( ui . addButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( showShareDialog ( ) ) ) ;
2010-09-18 12:05:32 -04:00
connect ( ui . editButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( editShareDirectory ( ) ) ) ;
2010-09-17 14:27:30 -04:00
connect ( ui . removeButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( removeShareDirectory ( ) ) ) ;
connect ( ui . closeButton , SIGNAL ( clicked ( ) ) , this , SLOT ( close ( ) ) ) ;
2008-12-10 20:19:34 -05:00
2010-09-18 12:05:32 -04:00
connect ( ui . shareddirList , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( shareddirListCostumPopupMenu ( QPoint ) ) ) ;
connect ( ui . shareddirList , SIGNAL ( currentCellChanged ( int , int , int , int ) ) , this , SLOT ( shareddirListCurrentCellChanged ( int , int , int , int ) ) ) ;
2009-01-01 12:47:35 -05:00
2010-09-18 12:05:32 -04:00
ui . editButton - > setEnabled ( false ) ;
ui . removeButton - > setEnabled ( false ) ;
2009-07-12 17:03:26 -04:00
2011-08-15 19:42:07 -04:00
QHeaderView * header = ui . shareddirList - > horizontalHeader ( ) ;
header - > setResizeMode ( COLUMN_PATH , QHeaderView : : Stretch ) ;
2009-08-08 07:17:49 -04:00
2011-08-15 19:42:07 -04:00
header - > setResizeMode ( COLUMN_NETWORKWIDE , QHeaderView : : Fixed ) ;
header - > setResizeMode ( COLUMN_BROWSABLE , QHeaderView : : Fixed ) ;
header - > setHighlightSections ( false ) ;
2009-08-04 16:46:26 -04:00
2010-09-18 12:05:32 -04:00
ui . shareddirList - > setRangeSelected ( QTableWidgetSelectionRange ( 0 , 0 , 0 , COLUMN_COUNT ) , true ) ;
2011-04-26 19:38:29 -04:00
setAcceptDrops ( true ) ;
2010-09-18 12:05:32 -04:00
2010-09-17 14:27:30 -04:00
setAttribute ( Qt : : WA_DeleteOnClose , true ) ;
2008-12-10 20:19:34 -05:00
}
2009-01-01 12:47:35 -05:00
2010-04-24 18:09:47 -04:00
ShareManager : : ~ ShareManager ( )
2009-01-01 12:47:35 -05:00
{
2010-09-17 14:27:30 -04:00
_instance = NULL ;
2010-09-18 12:05:32 -04:00
Settings - > saveWidgetInformation ( this ) ;
2010-04-24 18:09:47 -04:00
}
2009-01-01 12:47:35 -05:00
2011-08-12 10:06:29 -04:00
void ShareManager : : shareddirListCostumPopupMenu ( QPoint /*point*/ )
2010-04-24 18:09:47 -04:00
{
2010-09-17 14:27:30 -04:00
QMenu contextMnu ( this ) ;
2009-01-01 12:47:35 -05:00
2010-09-23 17:08:22 -04:00
QAction * editAct = new QAction ( QIcon ( IMAGE_EDIT ) , tr ( " Edit " ) , & contextMnu ) ;
2010-09-18 12:05:32 -04:00
connect ( editAct , SIGNAL ( triggered ( ) ) , this , SLOT ( editShareDirectory ( ) ) ) ;
QAction * removeAct = new QAction ( QIcon ( IMAGE_CANCEL ) , tr ( " Remove " ) , & contextMnu ) ;
2010-09-17 14:27:30 -04:00
connect ( removeAct , SIGNAL ( triggered ( ) ) , this , SLOT ( removeShareDirectory ( ) ) ) ;
2009-01-01 12:47:35 -05:00
2010-09-18 12:05:32 -04:00
contextMnu . addAction ( editAct ) ;
2010-09-17 14:27:30 -04:00
contextMnu . addAction ( removeAct ) ;
2010-05-14 16:55:44 -04:00
2010-09-17 14:27:30 -04:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
2009-01-01 12:47:35 -05:00
}
2008-12-10 20:19:34 -05:00
/** Loads the settings for this page */
void ShareManager : : load ( )
{
2010-09-17 14:27:30 -04:00
isLoading = true ;
std : : cerr < < " ShareManager:: In load !!!!! " < < std : : endl ;
2009-08-09 16:00:25 -04:00
2010-09-17 14:27:30 -04:00
std : : list < SharedDirInfo > : : const_iterator it ;
std : : list < SharedDirInfo > dirs ;
rsFiles - > getSharedDirectories ( dirs ) ;
2009-07-12 17:03:26 -04:00
2010-09-17 14:27:30 -04:00
/* get a link to the table */
QTableWidget * listWidget = ui . shareddirList ;
2009-07-12 17:03:26 -04:00
2010-09-17 14:27:30 -04:00
/* set new row count */
listWidget - > setRowCount ( dirs . size ( ) ) ;
2009-08-03 15:43:52 -04:00
2010-09-17 14:27:30 -04:00
connect ( this , SIGNAL ( itemClicked ( QTableWidgetItem * ) ) , this , SLOT ( updateFlags ( QTableWidgetItem * ) ) ) ;
2009-07-12 17:03:26 -04:00
2010-04-24 18:09:47 -04:00
# ifndef USE_COMBOBOX
2010-09-18 12:05:32 -04:00
QString ToolTips [ 2 ] = { tr ( " If checked, the share is anonymously shared to anybody. " ) ,
tr ( " If checked, the share is browsable by your friends. " ) } ;
2010-09-17 14:27:30 -04:00
int Flags [ 2 ] = { RS_FILE_HINTS_NETWORK_WIDE , RS_FILE_HINTS_BROWSABLE } ;
2010-04-24 18:09:47 -04:00
# endif
2010-09-17 14:27:30 -04:00
int row = 0 ;
for ( it = dirs . begin ( ) ; it ! = dirs . end ( ) ; it + + , + + row )
{
listWidget - > setItem ( row , COLUMN_PATH , new QTableWidgetItem ( QString : : fromUtf8 ( ( * it ) . filename . c_str ( ) ) ) ) ;
listWidget - > setItem ( row , COLUMN_VIRTUALNAME , new QTableWidgetItem ( QString : : fromUtf8 ( ( * it ) . virtualname . c_str ( ) ) ) ) ;
2010-04-24 18:09:47 -04:00
2009-08-03 15:43:52 -04:00
# ifdef USE_COMBOBOX
2010-09-17 14:27:30 -04:00
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 ) ;
2009-08-03 15:43:52 -04:00
# else
2010-09-17 14:27:30 -04:00
int col ;
for ( col = 0 ; col < = 1 ; col + + ) {
QModelIndex index = listWidget - > model ( ) - > index ( row , col + COLUMN_NETWORKWIDE , QModelIndex ( ) ) ;
2011-08-15 19:42:07 -04:00
QWidget * widget = dynamic_cast < QWidget * > ( listWidget - > indexWidget ( index ) ) ;
QCheckBox * cb = NULL ;
if ( widget ) {
cb = dynamic_cast < QCheckBox * > ( widget - > children ( ) . front ( ) ) ;
}
2010-09-17 14:27:30 -04:00
if ( cb = = NULL ) {
2011-08-15 19:42:07 -04:00
QWidget * widget = new QWidget ;
cb = new QCheckBox ( widget ) ;
2010-09-17 14:27:30 -04:00
cb - > setToolTip ( ToolTips [ col ] ) ;
2011-08-15 19:42:07 -04:00
QHBoxLayout * layout = new QHBoxLayout ( widget ) ;
layout - > addWidget ( cb , 0 , Qt : : AlignCenter ) ;
layout - > setSpacing ( 0 ) ;
2011-11-13 08:58:39 -05:00
layout - > setContentsMargins ( 10 , 0 , 0 , 0 ) ; // to be centered
2011-08-15 19:42:07 -04:00
widget - > setLayout ( layout ) ;
listWidget - > setCellWidget ( row , col + COLUMN_NETWORKWIDE , widget ) ;
2010-09-17 14:27:30 -04:00
QObject : : connect ( cb , SIGNAL ( toggled ( bool ) ) , this , SLOT ( updateFlags ( bool ) ) ) ;
}
cb - > setChecked ( ( * it ) . shareflags & Flags [ col ] ) ;
}
2009-08-03 15:43:52 -04:00
# endif
2010-09-17 14:27:30 -04:00
}
2008-12-10 20:19:34 -05:00
2010-09-17 14:27:30 -04:00
//ui.incomingDir->setText(QString::fromStdString(rsFiles->getDownloadDirectory()));
2009-07-12 17:03:26 -04:00
2010-09-17 14:27:30 -04:00
listWidget - > update ( ) ; /* update display */
update ( ) ;
2010-04-24 18:09:47 -04:00
2010-09-17 14:27:30 -04:00
isLoading = false ;
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 ( )
{
2010-09-17 14:27:30 -04:00
if ( _instance = = NULL )
_instance = new ShareManager ( NULL , 0 ) ;
2008-12-10 20:19:34 -05:00
2010-09-17 14:27:30 -04:00
_instance - > show ( ) ;
_instance - > activateWindow ( ) ;
2008-12-10 20:19:34 -05:00
}
2010-04-24 18:09:47 -04:00
/*static*/ void ShareManager : : postModDirectories ( bool update_local )
{
2010-09-17 14:27:30 -04:00
if ( _instance = = NULL | | _instance - > isHidden ( ) ) {
return ;
}
2008-12-10 20:19:34 -05:00
2010-09-17 14:27:30 -04:00
if ( update_local ) {
_instance - > load ( ) ;
}
2008-12-10 20:19:34 -05:00
}
2009-08-03 15:43:52 -04:00
void ShareManager : : updateFlags ( bool b )
{
2010-09-17 14:27:30 -04:00
if ( isLoading )
return ;
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 ;
2011-08-15 19:42:07 -04:00
current_flags | = ( dynamic_cast < QCheckBox * > ( ui . shareddirList - > cellWidget ( row , COLUMN_NETWORKWIDE ) - > children ( ) . front ( ) ) ) - > isChecked ( ) ? RS_FILE_HINTS_NETWORK_WIDE : 0 ;
current_flags | = ( dynamic_cast < QCheckBox * > ( ui . shareddirList - > cellWidget ( row , COLUMN_BROWSABLE ) - > children ( ) . front ( ) ) ) - > isChecked ( ) ? RS_FILE_HINTS_BROWSABLE : 0 ;
2010-09-17 14:27:30 -04:00
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 ;
}
}
2009-08-03 15:43:52 -04:00
}
2010-09-18 12:05:32 -04:00
void ShareManager : : editShareDirectory ( )
{
/* id current dir */
int row = ui . shareddirList - > currentRow ( ) ;
QTableWidgetItem * item = ui . shareddirList - > item ( row , COLUMN_PATH ) ;
if ( item ) {
std : : string filename = item - > text ( ) . toUtf8 ( ) . constData ( ) ;
std : : list < SharedDirInfo > dirs ;
rsFiles - > getSharedDirectories ( dirs ) ;
std : : list < SharedDirInfo > : : const_iterator it ;
for ( it = dirs . begin ( ) ; it ! = dirs . end ( ) ; it + + ) {
if ( it - > filename = = filename ) {
/* file name found, show dialog */
ShareDialog sharedlg ( it - > filename , this ) ;
sharedlg . exec ( ) ;
break ;
}
}
}
}
2008-12-10 20:19:34 -05:00
void ShareManager : : removeShareDirectory ( )
{
2010-09-17 14:27:30 -04:00
/* id current dir */
/* ask for removal */
QTableWidget * listWidget = ui . shareddirList ;
int row = listWidget - > currentRow ( ) ;
QTableWidgetItem * qdir = listWidget - > item ( row , COLUMN_PATH ) ;
if ( qdir )
{
2010-09-18 12:05:32 -04:00
if ( ( QMessageBox : : question ( this , tr ( " Warning! " ) , tr ( " Do you really want to stop sharing this directory ? " ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : Yes ) ) = = QMessageBox : : Yes )
2010-09-17 14:27:30 -04:00
{
rsFiles - > removeSharedDirectory ( qdir - > text ( ) . toUtf8 ( ) . constData ( ) ) ;
load ( ) ;
}
}
2008-12-10 20:19:34 -05:00
}
2009-07-12 17:03:26 -04:00
void ShareManager : : showEvent ( QShowEvent * event )
{
2010-09-17 14:27:30 -04:00
if ( ! event - > spontaneous ( ) )
{
load ( ) ;
}
2009-07-12 17:03:26 -04:00
}
2010-04-08 12:33:10 -04:00
void ShareManager : : showShareDialog ( )
{
2010-09-18 12:05:32 -04:00
ShareDialog sharedlg ( " " , this ) ;
2010-09-17 14:27:30 -04:00
sharedlg . exec ( ) ;
2010-04-08 12:33:10 -04:00
}
2010-09-18 12:05:32 -04:00
void ShareManager : : shareddirListCurrentCellChanged ( int currentRow , int currentColumn , int previousRow , int previousColumn )
{
Q_UNUSED ( currentColumn ) ;
Q_UNUSED ( previousRow ) ;
Q_UNUSED ( previousColumn ) ;
if ( currentRow > = 0 ) {
ui . editButton - > setEnabled ( true ) ;
ui . removeButton - > setEnabled ( true ) ;
} else {
ui . editButton - > setEnabled ( false ) ;
ui . removeButton - > setEnabled ( false ) ;
}
}
2011-04-26 19:38:29 -04:00
void ShareManager : : dragEnterEvent ( QDragEnterEvent * event )
{
if ( event - > mimeData ( ) - > hasUrls ( ) ) {
event - > acceptProposedAction ( ) ;
}
}
void ShareManager : : dropEvent ( QDropEvent * event )
{
if ( ! ( Qt : : CopyAction & event - > possibleActions ( ) ) ) {
/* can't do it */
return ;
}
QStringList formats = event - > mimeData ( ) - > formats ( ) ;
QStringList : : iterator it ;
bool errorShown = false ;
if ( event - > mimeData ( ) - > hasUrls ( ) ) {
QList < QUrl > urls = event - > mimeData ( ) - > urls ( ) ;
QList < QUrl > : : iterator it ;
for ( it = urls . begin ( ) ; it ! = urls . end ( ) ; it + + ) {
QString localpath = it - > toLocalFile ( ) ;
if ( localpath . isEmpty ( ) = = false ) {
QDir dir ( localpath ) ;
if ( dir . exists ( ) ) {
SharedDirInfo sdi ;
sdi . filename = localpath . toUtf8 ( ) . constData ( ) ;
sdi . virtualname . clear ( ) ;
sdi . shareflags = 0 ;
/* add new share */
rsFiles - > addSharedDirectory ( sdi ) ;
} else if ( QFile : : exists ( localpath ) ) {
if ( errorShown = = false ) {
QMessageBox mb ( tr ( " Drop file error. " ) , tr ( " File can't be dropped, only directories are accepted. " ) , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 , this ) ;
mb . exec ( ) ;
errorShown = true ;
}
} else {
QMessageBox mb ( tr ( " Drop file error. " ) , tr ( " Directory not found or directory name not accepted. " ) , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 , this ) ;
mb . exec ( ) ;
}
}
}
}
event - > setDropAction ( Qt : : CopyAction ) ;
event - > accept ( ) ;
}