2011-11-09 16:26:51 -05:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2011 - 2011 RetroShare Team
*
* Cyril Soler ( csoler @ users . sourceforge . net )
*
* 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
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <QCheckBox>
# include <QMessageBox>
# include <QDir>
2011-11-13 08:58:39 -05:00
# include <QKeyEvent>
2011-11-09 16:26:51 -05:00
# include "RsCollectionDialog.h"
# include "RsCollectionFile.h"
2012-05-16 14:58:52 -04:00
# include "util/misc.h"
2011-11-09 16:26:51 -05:00
RsCollectionDialog : : RsCollectionDialog ( const QString & CollectionFileName , const std : : vector < RsCollectionFile : : DLinfo > & dlinfos )
: _dlinfos ( dlinfos ) , _filename ( CollectionFileName )
{
setupUi ( this ) ;
2012-05-16 14:58:52 -04:00
setWindowFlags ( Qt : : Window ) ; // for maximize button
setWindowFlags ( windowFlags ( ) & ~ Qt : : WindowMinimizeButtonHint ) ;
2011-12-08 04:16:45 -05:00
setWindowTitle ( QString ( " %1 - %2 " ) . arg ( windowTitle ( ) ) . arg ( QFileInfo ( _filename ) . completeBaseName ( ) ) ) ;
2011-11-13 08:58:39 -05:00
2011-11-09 16:26:51 -05:00
// 1 - add all elements to the list.
2012-05-16 14:58:52 -04:00
_fileEntriesTW - > setColumnCount ( 3 ) ;
2011-11-09 16:26:51 -05:00
2012-05-16 14:58:52 -04:00
QTreeWidgetItem * headerItem = _fileEntriesTW - > headerItem ( ) ;
headerItem - > setText ( 0 , tr ( " File " ) ) ;
headerItem - > setText ( 1 , tr ( " Size " ) ) ;
headerItem - > setText ( 2 , tr ( " Hash " ) ) ;
2011-11-13 08:58:39 -05:00
2012-05-16 14:58:52 -04:00
uint32_t size = dlinfos . size ( ) ;
2011-11-13 08:58:39 -05:00
2011-11-09 16:26:51 -05:00
uint64_t total_size ;
uint32_t total_files ;
2012-12-13 16:53:27 -05:00
bool wrong_chars = false ;
2011-11-09 16:26:51 -05:00
2012-05-16 14:58:52 -04:00
for ( uint32_t i = 0 ; i < size ; + + i )
2011-11-09 16:26:51 -05:00
{
2012-05-16 14:58:52 -04:00
const RsCollectionFile : : DLinfo & dlinfo = dlinfos [ i ] ;
2011-11-13 08:58:39 -05:00
2012-05-16 14:58:52 -04:00
QTreeWidgetItem * item = new QTreeWidgetItem ;
2011-11-09 16:26:51 -05:00
2012-05-16 14:58:52 -04:00
item - > setFlags ( Qt : : ItemIsUserCheckable | item - > flags ( ) ) ;
item - > setCheckState ( 0 , Qt : : Checked ) ;
item - > setData ( 0 , Qt : : UserRole , i ) ;
item - > setText ( 0 , dlinfo . path + " / " + dlinfo . name ) ;
item - > setText ( 1 , misc : : friendlyUnit ( dlinfo . size ) ) ;
item - > setText ( 2 , dlinfo . hash ) ;
2011-11-09 16:26:51 -05:00
2012-12-13 16:53:27 -05:00
if ( dlinfo . filename_has_wrong_characters )
{
wrong_chars = true ;
item - > setTextColor ( 0 , QColor ( 255 , 80 , 120 ) ) ;
}
2012-05-16 14:58:52 -04:00
_fileEntriesTW - > addTopLevelItem ( item ) ;
2011-11-09 16:26:51 -05:00
2012-05-16 14:58:52 -04:00
total_size + = dlinfo . size ;
2011-11-09 16:26:51 -05:00
total_files + + ;
}
_filename_TL - > setText ( _filename ) ;
2012-05-16 14:58:52 -04:00
for ( int column = 0 ; column < _fileEntriesTW - > columnCount ( ) ; + + column ) {
_fileEntriesTW - > resizeColumnToContents ( column ) ;
}
2011-11-09 16:26:51 -05:00
updateSizes ( ) ;
// 2 - connect necessary signals/slots
2012-05-16 14:58:52 -04:00
connectUpdate ( true ) ;
2011-11-09 16:26:51 -05:00
connect ( _selectAll_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( selectAll ( ) ) ) ;
connect ( _deselectAll_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( deselectAll ( ) ) ) ;
connect ( _cancel_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( cancel ( ) ) ) ;
connect ( _download_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( download ( ) ) ) ;
2011-11-13 08:58:39 -05:00
_fileEntriesTW - > installEventFilter ( this ) ;
2012-12-13 16:53:27 -05:00
if ( wrong_chars )
QMessageBox : : warning ( NULL , tr ( " Bad filenames have been cleaned " ) , tr ( " Some filenames or directory names in this collection file \n contained '/' or ' \\ ' characters. They have been substitued to '_', \n and are listed in red. " ) ) ;
2011-11-09 16:26:51 -05:00
}
2011-11-13 08:58:39 -05:00
bool RsCollectionDialog : : eventFilter ( QObject * obj , QEvent * event )
{
if ( obj = = _fileEntriesTW ) {
if ( event - > type ( ) = = QEvent : : KeyPress ) {
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( event ) ;
if ( keyEvent & & keyEvent - > key ( ) = = Qt : : Key_Space ) {
// Space pressed
2012-05-16 14:58:52 -04:00
// get state of current item
QTreeWidgetItem * item = _fileEntriesTW - > currentItem ( ) ;
if ( item ) {
Qt : : CheckState checkState = ( item - > checkState ( 0 ) = = Qt : : Checked ) ? Qt : : Unchecked : Qt : : Checked ;
connectUpdate ( false ) ;
// set state of all selected items
QList < QTreeWidgetItem * > selectedItems = _fileEntriesTW - > selectedItems ( ) ;
QList < QTreeWidgetItem * > : : iterator it ;
for ( it = selectedItems . begin ( ) ; it ! = selectedItems . end ( ) ; + + it ) {
( * it ) - > setCheckState ( 0 , checkState ) ;
2011-11-13 08:58:39 -05:00
}
2012-05-16 14:58:52 -04:00
updateSizes ( ) ;
connectUpdate ( true ) ;
2011-11-13 08:58:39 -05:00
}
return true ; // eat event
}
}
}
// pass the event on to the parent class
return QDialog : : eventFilter ( obj , event ) ;
}
2012-05-16 14:58:52 -04:00
void RsCollectionDialog : : connectUpdate ( bool doConnect )
{
if ( doConnect ) {
connect ( _fileEntriesTW , SIGNAL ( itemChanged ( QTreeWidgetItem * , int ) ) , this , SLOT ( itemChanged ( QTreeWidgetItem * , int ) ) ) ;
} else {
disconnect ( _fileEntriesTW , SIGNAL ( itemChanged ( QTreeWidgetItem * , int ) ) , this , SLOT ( itemChanged ( QTreeWidgetItem * , int ) ) ) ;
}
}
2011-11-13 08:58:39 -05:00
void RsCollectionDialog : : updateSizes ( )
2011-11-09 16:26:51 -05:00
{
uint64_t total_size = 0 ;
uint32_t total_files = 0 ;
2012-05-16 14:58:52 -04:00
QTreeWidgetItemIterator itemIterator ( _fileEntriesTW ) ;
QTreeWidgetItem * item ;
while ( ( item = * itemIterator ) ! = NULL ) {
itemIterator + + ;
if ( item - > checkState ( 0 ) = = Qt : : Checked ) {
total_size + = _dlinfos [ item - > data ( 0 , Qt : : UserRole ) . toInt ( ) ] . size ;
2011-11-09 16:26:51 -05:00
+ + total_files ;
}
2012-05-16 14:58:52 -04:00
}
2011-11-09 16:26:51 -05:00
_selectedFiles_TL - > setText ( QString : : number ( total_files ) ) ;
2012-05-16 14:58:52 -04:00
_totalSize_TL - > setText ( misc : : friendlyUnit ( total_size ) ) ;
}
void RsCollectionDialog : : itemChanged ( QTreeWidgetItem */ * item */ , int /*column*/ )
{
updateSizes ( ) ;
2011-11-09 16:26:51 -05:00
}
2012-05-16 14:58:52 -04:00
void RsCollectionDialog : : selectDeselectAll ( bool select )
{
connectUpdate ( false ) ;
QTreeWidgetItemIterator itemIterator ( _fileEntriesTW ) ;
QTreeWidgetItem * item ;
while ( ( item = * itemIterator ) ! = NULL ) {
itemIterator + + ;
item - > setCheckState ( 0 , select ? Qt : : Checked : Qt : : Unchecked ) ;
}
updateSizes ( ) ;
connectUpdate ( true ) ;
}
void RsCollectionDialog : : selectAll ( )
2011-11-09 16:26:51 -05:00
{
std : : cerr < < " Selecting all ! " < < std : : endl ;
2012-05-16 14:58:52 -04:00
selectDeselectAll ( true ) ;
2011-11-09 16:26:51 -05:00
}
2012-05-16 14:58:52 -04:00
void RsCollectionDialog : : deselectAll ( )
2011-11-09 16:26:51 -05:00
{
std : : cerr < < " Deselecting all ! " < < std : : endl ;
2012-05-16 14:58:52 -04:00
selectDeselectAll ( false ) ;
2011-11-09 16:26:51 -05:00
}
void RsCollectionDialog : : cancel ( )
{
std : : cerr < < " Canceling! " < < std : : endl ;
close ( ) ;
}
void RsCollectionDialog : : download ( )
{
std : : cerr < < " Downloading! " < < std : : endl ;
QString dldir = QString : : fromUtf8 ( rsFiles - > getDownloadDirectory ( ) . c_str ( ) ) ;
std : : cerr < < " downloading all these files: " < < std : : endl ;
2012-05-16 14:58:52 -04:00
QTreeWidgetItemIterator itemIterator ( _fileEntriesTW ) ;
QTreeWidgetItem * item ;
while ( ( item = * itemIterator ) ! = NULL ) {
itemIterator + + ;
const RsCollectionFile : : DLinfo & dlinfo = _dlinfos [ item - > data ( 0 , Qt : : UserRole ) . toInt ( ) ] ;
if ( item - > checkState ( 0 ) = = Qt : : Checked ) {
std : : cerr < < dlinfo . name . toStdString ( ) < < " " < < dlinfo . hash . toStdString ( ) < < " " < < dlinfo . size < < " " < < dlinfo . path . toStdString ( ) < < std : : endl ;
QString cleanPath = dldir + dlinfo . path ;
2011-11-09 16:26:51 -05:00
std : : cerr < < " making directory " < < cleanPath . toStdString ( ) < < std : : endl ;
2011-12-08 04:16:45 -05:00
if ( ! QDir ( QApplication : : applicationDirPath ( ) ) . mkpath ( cleanPath ) )
2011-11-09 16:26:51 -05:00
QMessageBox : : warning ( NULL , QObject : : tr ( " Unable to make path " ) , QObject : : tr ( " Unable to make path: " ) + " <br> " + cleanPath ) ;
2012-11-02 09:52:29 -04:00
rsFiles - > FileRequest ( dlinfo . name . toUtf8 ( ) . constData ( ) , dlinfo . hash . toUtf8 ( ) . constData ( ) , dlinfo . size , cleanPath . toUtf8 ( ) . constData ( ) , RS_FILE_REQ_ANONYMOUS_ROUTING , std : : list < std : : string > ( ) ) ;
2011-11-09 16:26:51 -05:00
}
else
2012-05-16 14:58:52 -04:00
std : : cerr < < " Skipping file : " < < dlinfo . name . toStdString ( ) < < std : : endl ;
}
2011-11-09 16:26:51 -05:00
close ( ) ;
}