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>
2013-10-19 09:25:06 -04:00
# include <QMimeData>
2008-12-10 20:19:34 -05:00
2010-09-18 12:05:32 -04:00
# include <retroshare/rsfiles.h>
2012-11-02 09:52:29 -04:00
# include <retroshare/rstypes.h>
2012-11-08 19:46:41 -05:00
# include <retroshare/rspeers.h>
2010-09-18 12:05:32 -04:00
# include "ShareManager.h"
# include "ShareDialog.h"
# include "settings/rsharesettings.h"
2016-10-31 11:28:26 -04:00
# include "gui/common/GroupFlagsWidget.h"
# include "gui/common/GroupSelectionBox.h"
2012-11-08 19:46:41 -05:00
# include "gui/common/GroupDefs.h"
# include "gui/notifyqt.h"
2013-10-19 09:25:06 -04:00
# include "util/QtVersion.h"
2010-09-18 12:05:32 -04:00
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
2012-10-21 10:06:33 -04:00
# define COLUMN_SHARE_FLAGS 2
# define COLUMN_GROUPS 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 */
2012-11-06 18:26:47 -05:00
ShareManager : : ShareManager ( )
: QDialog ( NULL , Qt : : WindowSystemMenuHint | Qt : : WindowTitleHint | Qt : : WindowMinMaxButtonsHint | Qt : : WindowCloseButtonHint )
2008-12-10 20:19:34 -05:00
{
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 ;
2008-12-10 20:19:34 -05:00
2010-09-18 12:05:32 -04:00
Settings - > loadWidgetInformation ( this ) ;
2016-10-31 11:28:26 -04:00
connect ( ui . addButton , SIGNAL ( clicked ( bool ) ) , this , SLOT ( addShare ( ) ) ) ;
2012-11-14 17:56:25 -05:00
connect ( ui . closeButton , SIGNAL ( clicked ( ) ) , this , SLOT ( applyAndClose ( ) ) ) ;
2016-10-31 11:28:26 -04:00
connect ( ui . cancelButton , SIGNAL ( clicked ( ) ) , this , SLOT ( cancel ( ) ) ) ;
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 ) ) ) ;
2016-10-31 11:28:26 -04:00
connect ( ui . shareddirList , SIGNAL ( cellDoubleClicked ( int , int ) ) , this , SLOT ( doubleClickedCell ( int , int ) ) ) ;
2016-11-01 06:11:28 -04:00
connect ( ui . shareddirList , SIGNAL ( cellChanged ( int , int ) ) , this , SLOT ( handleCellChange ( int , int ) ) ) ;
2012-11-08 19:46:41 -05:00
2016-10-31 11:28:26 -04:00
connect ( NotifyQt : : getInstance ( ) , SIGNAL ( groupsChanged ( int ) ) , this , SLOT ( reload ( ) ) ) ;
2009-07-12 17:03:26 -04:00
2011-08-15 19:42:07 -04:00
QHeaderView * header = ui . shareddirList - > horizontalHeader ( ) ;
2015-06-26 04:14:09 -04:00
QHeaderView_setSectionResizeModeColumn ( header , COLUMN_PATH , QHeaderView : : Stretch ) ;
2009-08-08 07:17:49 -04:00
2011-08-15 19:42:07 -04:00
header - > setHighlightSections ( false ) ;
2009-08-04 16:46:26 -04:00
2011-04-26 19:38:29 -04:00
setAcceptDrops ( true ) ;
2010-09-17 14:27:30 -04:00
setAttribute ( Qt : : WA_DeleteOnClose , true ) ;
2016-11-01 05:30:36 -04:00
reload ( ) ;
2008-12-10 20:19:34 -05:00
}
2009-01-01 12:47:35 -05:00
2016-11-01 06:11:28 -04:00
void ShareManager : : handleCellChange ( int row , int column )
{
if ( isLoading )
return ;
if ( column = = COLUMN_VIRTUALNAME )
{
// check if the thing already exists
for ( uint32_t i = 0 ; i < mDirInfos . size ( ) ; + + i )
if ( i ! = ( uint32_t ) row & & ( mDirInfos [ row ] . virtualname = = std : : string ( ui . shareddirList - > item ( i , COLUMN_VIRTUALNAME ) - > text ( ) . toUtf8 ( ) ) ) )
{
ui . shareddirList - > item ( row , COLUMN_VIRTUALNAME ) - > setText ( QString : : fromUtf8 ( mDirInfos [ row ] . virtualname . c_str ( ) ) ) ;
return ;
}
mDirInfos [ row ] . virtualname = std : : string ( ui . shareddirList - > item ( row , COLUMN_VIRTUALNAME ) - > text ( ) . toUtf8 ( ) ) ;
}
}
2016-10-31 11:28:26 -04:00
void ShareManager : : doubleClickedCell ( int row , int column )
{
if ( column = = COLUMN_PATH )
{
QString dirname = QFileDialog : : getExistingDirectory ( NULL , tr ( " Choose directory " ) , QString ( ) , QFileDialog : : DontUseNativeDialog | QFileDialog : : ShowDirsOnly ) ;
if ( ! dirname . isNull ( ) )
{
std : : string new_name ( dirname . toUtf8 ( ) ) ;
for ( uint32_t i = 0 ; i < mDirInfos . size ( ) ; + + i )
if ( mDirInfos [ row ] . filename = = new_name )
return ;
mDirInfos [ row ] . filename = new_name ;
load ( ) ;
}
}
else if ( column = = COLUMN_GROUPS )
{
2016-10-31 18:24:35 -04:00
std : : list < RsNodeGroupId > selected_groups = GroupSelectionDialog : : selectGroups ( mDirInfos [ row ] . parent_groups ) ;
2016-10-31 11:28:26 -04:00
mDirInfos [ row ] . parent_groups = selected_groups ;
load ( ) ;
}
}
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
2016-10-31 11:28:26 -04:00
void ShareManager : : cancel ( )
{
close ( ) ;
}
2012-11-14 17:56:25 -05:00
void ShareManager : : applyAndClose ( )
{
2016-10-31 11:28:26 -04:00
// This is the only place where we change things.
std : : list < SharedDirInfo > infos ;
for ( uint32_t i = 0 ; i < mDirInfos . size ( ) ; + + i )
infos . push_back ( mDirInfos [ i ] ) ;
rsFiles - > setSharedDirectories ( infos ) ;
2012-11-14 17:56:25 -05:00
close ( ) ;
}
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
}
2016-10-31 11:28:26 -04:00
void ShareManager : : reload ( )
{
std : : list < SharedDirInfo > dirs ;
rsFiles - > getSharedDirectories ( dirs ) ;
2016-11-01 05:46:29 -04:00
mDirInfos . clear ( ) ;
2016-10-31 11:28:26 -04:00
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirs . begin ( ) ) ; it ! = dirs . end ( ) ; + + it )
mDirInfos . push_back ( * it ) ;
load ( ) ;
}
2008-12-10 20:19:34 -05:00
/** Loads the settings for this page */
void ShareManager : : load ( )
{
2012-11-19 16:19:44 -05:00
if ( isLoading )
return ;
2010-09-17 14:27:30 -04:00
isLoading = true ;
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 */
2016-10-31 11:28:26 -04:00
listWidget - > setRowCount ( mDirInfos . size ( ) ) ;
2009-08-03 15:43:52 -04:00
2016-10-31 11:28:26 -04:00
for ( uint32_t row = 0 ; row < mDirInfos . size ( ) ; + + row )
2010-09-17 14:27:30 -04:00
{
2016-10-31 11:28:26 -04:00
listWidget - > setItem ( row , COLUMN_PATH , new QTableWidgetItem ( QString : : fromUtf8 ( mDirInfos [ row ] . filename . c_str ( ) ) ) ) ;
listWidget - > setItem ( row , COLUMN_VIRTUALNAME , new QTableWidgetItem ( QString : : fromUtf8 ( mDirInfos [ row ] . virtualname . c_str ( ) ) ) ) ;
2010-04-24 18:09:47 -04:00
2016-10-31 11:28:26 -04:00
GroupFlagsWidget * widget = new GroupFlagsWidget ( NULL , mDirInfos [ row ] . shareflags ) ;
2011-08-15 19:42:07 -04:00
2016-10-31 11:28:26 -04:00
listWidget - > setRowHeight ( row , 32 * QFontMetricsF ( font ( ) ) . height ( ) / 14.0 ) ;
listWidget - > setCellWidget ( row , COLUMN_SHARE_FLAGS , widget ) ;
2011-08-15 19:42:07 -04:00
2016-10-31 11:28:26 -04:00
listWidget - > setItem ( row , COLUMN_GROUPS , new QTableWidgetItem ( ) ) ;
listWidget - > item ( row , COLUMN_GROUPS ) - > setBackgroundColor ( QColor ( 183 , 236 , 181 ) ) ;
2012-11-02 09:52:29 -04:00
2016-10-31 11:28:26 -04:00
connect ( widget , SIGNAL ( flagsChanged ( FileStorageFlags ) ) , this , SLOT ( updateFlags ( ) ) ) ;
2008-12-10 20:19:34 -05:00
2016-10-31 11:28:26 -04:00
listWidget - > item ( row , COLUMN_PATH ) - > setToolTip ( tr ( " Double click to change shared directory path " ) ) ;
listWidget - > item ( row , COLUMN_GROUPS ) - > setToolTip ( tr ( " Double click to select which groups of friends can see the files " ) ) ;
2016-10-31 18:24:35 -04:00
listWidget - > item ( row , COLUMN_VIRTUALNAME ) - > setToolTip ( tr ( " Double click to change the name that friends will see " ) ) ;
listWidget - > item ( row , COLUMN_GROUPS ) - > setText ( getGroupString ( mDirInfos [ row ] . parent_groups ) ) ;
QFont font = listWidget - > item ( row , COLUMN_GROUPS ) - > font ( ) ;
font . setBold ( mDirInfos [ row ] . shareflags & DIR_FLAGS_BROWSABLE ) ;
listWidget - > item ( row , COLUMN_GROUPS ) - > setTextColor ( ( mDirInfos [ row ] . shareflags & DIR_FLAGS_BROWSABLE ) ? ( Qt : : black ) : ( Qt : : lightGray ) ) ;
listWidget - > item ( row , COLUMN_GROUPS ) - > setFont ( font ) ;
2016-10-31 11:28:26 -04:00
}
2012-10-21 15:39:56 -04:00
2016-10-31 11:28:26 -04:00
listWidget - > setColumnWidth ( COLUMN_SHARE_FLAGS , 132 * QFontMetricsF ( font ( ) ) . height ( ) / 14.0 ) ;
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 )
2012-11-06 18:26:47 -05:00
_instance = new ShareManager ( ) ;
2008-12-10 20:19:34 -05:00
2016-10-31 11:28:26 -04:00
_instance - > reload ( ) ;
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 ) {
2016-10-31 11:28:26 -04:00
_instance - > reload ( ) ;
2010-09-17 14:27:30 -04:00
}
2008-12-10 20:19:34 -05:00
}
2012-11-02 09:52:29 -04:00
void ShareManager : : updateFlags ( )
2009-08-03 15:43:52 -04:00
{
2016-10-31 11:28:26 -04:00
for ( int row = 0 ; row < ui . shareddirList - > rowCount ( ) ; + + row )
{
FileStorageFlags flags = ( dynamic_cast < GroupFlagsWidget * > ( ui . shareddirList - > cellWidget ( row , COLUMN_SHARE_FLAGS ) ) ) - > flags ( ) ;
mDirInfos [ row ] . shareflags = flags ;
}
load ( ) ; // update the GUI.
2009-08-03 15:43:52 -04:00
}
2016-10-31 11:28:26 -04:00
QString ShareManager : : getGroupString ( const std : : list < RsNodeGroupId > & groups )
2012-11-08 19:46:41 -05:00
{
2016-10-31 18:24:35 -04:00
if ( groups . empty ( ) )
return tr ( " [Everyone] " ) ;
2016-10-31 11:28:26 -04:00
int n = 0 ;
QString group_string ;
2012-11-08 19:46:41 -05:00
2016-10-31 11:28:26 -04:00
for ( std : : list < RsNodeGroupId > : : const_iterator it ( groups . begin ( ) ) ; it ! = groups . end ( ) ; + + it , + + n )
2012-11-08 19:46:41 -05:00
{
2016-10-31 11:28:26 -04:00
if ( n > 0 )
group_string + = " , " ;
2012-11-08 19:46:41 -05:00
2016-10-31 11:28:26 -04:00
RsGroupInfo groupInfo ;
rsPeers - > getGroupInfo ( * it , groupInfo ) ;
group_string + = GroupDefs : : name ( groupInfo ) ;
2012-11-08 19:46:41 -05:00
}
2016-10-31 11:28:26 -04:00
return group_string ;
2010-09-18 12:05:32 -04:00
}
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
{
2016-10-31 11:28:26 -04:00
for ( uint32_t i = row ; i + 1 < mDirInfos . size ( ) ; + + i )
mDirInfos [ i ] = mDirInfos [ i + 1 ] ;
2010-09-17 14:27:30 -04:00
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
2016-10-31 11:28:26 -04:00
void ShareManager : : addShare ( )
{
QString fname = QFileDialog : : getExistingDirectory ( NULL , tr ( " Choose a directory to share " ) , QString ( ) , QFileDialog : : DontUseNativeDialog | QFileDialog : : ShowDirsOnly ) ;
if ( fname . isNull ( ) )
return ;
std : : string dir_name ( fname . toUtf8 ( ) ) ;
// check that the directory does not already exist
for ( uint32_t i = 0 ; i < mDirInfos . size ( ) ; + + i )
if ( mDirInfos [ i ] . filename = = dir_name )
return ;
mDirInfos . push_back ( SharedDirInfo ( ) ) ;
mDirInfos . back ( ) . filename = dir_name ;
mDirInfos . back ( ) . virtualname = std : : string ( ) ;
mDirInfos . back ( ) . shareflags = DIR_FLAGS_ANONYMOUS_DOWNLOAD | DIR_FLAGS_ANONYMOUS_SEARCH ;
mDirInfos . back ( ) . parent_groups . clear ( ) ;
load ( ) ;
}
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 ( ) ;
2016-09-18 15:41:18 -04:00
load ( ) ;
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 ) ;
}
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 ;
2014-10-21 18:33:02 -04:00
for ( it = urls . begin ( ) ; it ! = urls . end ( ) ; + + it ) {
2011-04-26 19:38:29 -04:00
QString localpath = it - > toLocalFile ( ) ;
if ( localpath . isEmpty ( ) = = false ) {
QDir dir ( localpath ) ;
if ( dir . exists ( ) ) {
SharedDirInfo sdi ;
sdi . filename = localpath . toUtf8 ( ) . constData ( ) ;
sdi . virtualname . clear ( ) ;
2012-11-02 09:52:29 -04:00
sdi . shareflags . clear ( ) ;
2011-04-26 19:38:29 -04:00
/* 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 ( ) ;
2016-09-18 15:41:18 -04:00
load ( ) ;
2011-04-26 19:38:29 -04:00
}