2023-01-07 12:44:45 +01:00
# include "mainwindow.h"
2023-01-27 18:14:50 +01:00
# include <QComboBox>
# include <QToolBar>
2023-01-07 12:44:45 +01:00
using namespace std ;
bool display = false ;
2023-01-27 18:14:50 +01:00
//extern QDialog Configuration;
2023-01-22 14:33:23 +01:00
extern Ui : : Configuration config ;
2023-01-07 12:44:45 +01:00
MainWindow : : MainWindow ( QWidget * parent )
: QMainWindow ( parent )
, ui ( new Ui : : MainWindow )
{
2023-02-02 16:10:51 +01:00
QAbstractButton * reply ;
QMessageBox msgBox ;
2023-01-07 12:44:45 +01:00
ui - > setupUi ( this ) ;
2023-01-15 15:25:22 +01:00
QCoreApplication : : setOrganizationName ( " RsyncUI " ) ;
QCoreApplication : : setApplicationName ( " RsyncUI " ) ;
2023-02-03 01:04:48 +01:00
// context menu for treewidget
2023-02-02 17:14:04 +01:00
ui - > treeWidget - > addAction ( ui - > actionDownload ) ;
2023-01-18 23:12:21 +01:00
// init shortcut
loadSettings ( ) ;
2023-01-22 14:33:23 +01:00
config . setupUi ( & Configuration ) ;
config . UnitCombobox - > addItems ( { tr ( " Byte " ) , tr ( " KB " ) , tr ( " MB " ) , tr ( " GB " ) , tr ( " TB " ) , tr ( " PB " ) } ) ;
// init of About
2023-01-26 12:02:12 +01:00
this - > about . description = tr ( " Client for rsync server \n \n You click on file to enqueue it, and RyncUI Download one file a time " ) ;
2023-01-18 23:12:21 +01:00
2023-01-15 15:25:22 +01:00
// connectors
2023-01-12 09:23:45 +01:00
connect ( & downloadO , & downloadFile : : progressSignal , ui - > progressBar , & QProgressBar : : setValue ) ;
connect ( & downloadO , & downloadFile : : finishedSignal , this , & MainWindow : : downloadFinished ) ;
connect ( this , & MainWindow : : stopDownloading , & downloadO , & downloadFile : : cancelled ) ;
2023-01-22 14:33:23 +01:00
connect ( config . buttonBox , SIGNAL ( accepted ( ) ) , this , SLOT ( on_buttonBox_accepted ( ) ) ) ;
2023-01-12 09:23:45 +01:00
2023-01-15 15:25:22 +01:00
// init of widgets
2023-01-07 12:44:45 +01:00
ui - > ktreewidgetsearchline - > setTreeWidget ( ui - > treeWidget ) ;
ui - > ktreewidgetsearchline - > setCaseSensitivity ( Qt : : CaseInsensitive ) ;
2023-01-31 00:45:53 +01:00
ui - > treeWidget - > setHeaderLabels ( { tr ( " Path " ) , tr ( " Type " ) , tr ( " Size " ) } ) ;
2023-01-27 18:14:50 +01:00
config . comboBox - > setCurrentIndex ( ui - > toolBar - > toolButtonStyle ( ) ) ;
2023-01-22 14:33:23 +01:00
if ( this - > settings . contains ( " connexion/lastServer " ) )
{
ui - > portEdit - > setText ( this - > settings . value ( " connexion/port " ) . toString ( ) ) ;
ui - > khistorycombobox - > setCurrentText ( this - > settings . value ( " connexion/lastServer " ) . toString ( ) ) ;
} else
{
ui - > portEdit - > text ( ) = QString : : number ( this - > connexion . port ) ;
ui - > khistorycombobox - > clear ( ) ;
}
2023-01-26 11:48:25 +01:00
// setting arrowcursor for treeWidget, listWidget and listDownload
ui - > treeWidget - > setCursor ( Qt : : ArrowCursor ) ;
ui - > listWidget - > setCursor ( Qt : : ArrowCursor ) ;
ui - > listDownload - > setCursor ( Qt : : ArrowCursor ) ;
// Hiding progress bar
2023-01-07 12:44:45 +01:00
ui - > progressBar - > hide ( ) ;
2023-01-15 13:26:14 +01:00
2023-02-02 16:10:51 +01:00
if ( this - > settings . value ( " Downloads/rows " ) . toInt ( ) ! = 0 )
{
this - > settings . endArray ( ) ;
msgBox . setWindowTitle ( " RsyncUI " ) ;
msgBox . setInformativeText ( tr ( " A list of interrupted downloads exists, do you want to continue downloading ? or you can delete the list " ) ) ;
QPushButton * remove = msgBox . addButton ( tr ( " Remove " ) , QMessageBox : : ActionRole ) ;
QPushButton * yes = msgBox . addButton ( QMessageBox : : Yes ) ;
msgBox . addButton ( QMessageBox : : No ) ;
msgBox . setDefaultButton ( QMessageBox : : No ) ;
msgBox . exec ( ) ;
reply = msgBox . clickedButton ( ) ;
if ( reply = = yes )
{
loadDownloadList ( ) ;
} else if ( reply = = remove )
{
this - > settings . remove ( " Downloads " ) ;
}
}
2023-01-07 12:44:45 +01:00
populateList ( ) ;
2023-01-18 23:12:21 +01:00
}
2023-01-12 09:23:45 +01:00
2023-01-22 14:33:23 +01:00
MainWindow : : ~ MainWindow ( )
2023-01-18 23:12:21 +01:00
{
2023-01-22 14:33:23 +01:00
delete ui ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
// Closing window has been clicked
2023-01-22 14:33:23 +01:00
void MainWindow : : closeEvent ( QCloseEvent * event )
2023-01-07 12:44:45 +01:00
{
2023-01-12 09:23:45 +01:00
QMessageBox : : StandardButton reply ;
2023-01-23 23:42:20 +01:00
// saving settings
2023-01-22 14:33:23 +01:00
saveSettings ( ) ;
2023-01-23 23:42:20 +01:00
if ( ui - > listDownload - > count ( ) ! = 0 ) // some downloads waiting
2023-01-12 09:23:45 +01:00
{
2023-01-23 23:42:20 +01:00
// Asking for stopping or continuing
reply = QMessageBox : : question (
2023-01-12 09:23:45 +01:00
this ,
" RsyncUI " ,
2023-02-02 16:10:51 +01:00
tr ( " Exiting will stop downloading, and will clear the download queue. \n You can save the list of downloads \n Do you want to exit ? " ) ,
QMessageBox : : Yes | QMessageBox : : Save | QMessageBox : : No ,
2023-01-12 09:23:45 +01:00
QMessageBox : : No ) ;
2023-01-22 14:33:23 +01:00
if ( reply = = QMessageBox : : No )
{
2023-01-23 23:42:20 +01:00
// continuing
2023-01-22 14:33:23 +01:00
event - > ignore ( ) ;
return ;
2023-02-02 16:10:51 +01:00
} else if ( reply = = QMessageBox : : Yes )
2023-01-12 09:23:45 +01:00
{
2023-01-23 23:42:20 +01:00
// emission of signal to downloading thread and stopping
2023-02-02 16:10:51 +01:00
emit ( stopDownloading ( this - > pid ) ) ;
waitpid ( this - > pid , NULL , WUNTRACED ) ;
} else
{
saveDownloadList ( ) ;
2023-01-12 09:23:45 +01:00
}
2023-01-15 13:26:14 +01:00
}
2023-01-22 14:33:23 +01:00
event - > accept ( ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
// Populate treeview with list of files
2023-01-26 11:31:56 +01:00
void MainWindow : : populateTree ( QTreeWidgetItem * parent )
2023-01-07 12:44:45 +01:00
{
stringstream ss ;
vector < string > path ;
2023-01-23 23:42:20 +01:00
// Clear treewidget
2023-01-18 23:12:21 +01:00
ui - > treeWidget - > clear ( ) ;
2023-01-12 09:23:45 +01:00
if ( ! this - > connexion . server . empty ( ) and this - > connexion . port > 0 and this - > connexion . port < 65536 )
2023-01-07 12:44:45 +01:00
{
2023-01-23 23:42:20 +01:00
// setting cursor to "Wait"
2023-01-15 13:26:14 +01:00
QGuiApplication : : setOverrideCursor ( Qt : : WaitCursor ) ;
2023-01-23 23:42:20 +01:00
2023-01-12 09:23:45 +01:00
if ( validateServer ( this - > connexion . server ) )
2023-01-07 12:44:45 +01:00
{
2023-01-23 23:42:20 +01:00
// server is validated
2023-01-07 12:44:45 +01:00
path = explode ( ui - > listWidget - > currentItem ( ) - > text ( ) . toStdString ( ) , ' \n ' , 2 ) ;
2023-01-26 11:31:56 +01:00
scanDir ( this - > connexion . server , this - > connexion . port , parent , path [ 0 ] . append ( " / " ) ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
// Restoring cursor
QGuiApplication : : restoreOverrideCursor ( ) ;
2023-01-07 12:44:45 +01:00
}
}
2023-01-23 23:42:20 +01:00
// Populate Listview with list of services
2023-01-07 12:44:45 +01:00
void MainWindow : : populateList ( )
{
stringstream ss ;
2023-01-15 13:26:14 +01:00
QString str ;
QString server ;
2023-01-22 14:33:23 +01:00
int port ;
2023-01-07 12:44:45 +01:00
2023-01-15 13:26:14 +01:00
server = ui - > khistorycombobox - > currentText ( ) ;
2023-01-22 14:33:23 +01:00
port = ui - > portEdit - > text ( ) . toUInt ( ) ;
if ( ( server . toStdString ( ) ! = this - > connexion . server ) or ( port ! = this - > connexion . port ) )
2023-01-07 12:44:45 +01:00
{
2023-01-23 23:42:20 +01:00
// clearing listwidget
2023-01-22 14:33:23 +01:00
ui - > listWidget - > clear ( ) ;
this - > connexion . server . assign ( server . toStdString ( ) ) ;
this - > connexion . port = port ;
2023-01-15 13:26:14 +01:00
2023-02-02 16:10:51 +01:00
QGuiApplication : : setOverrideCursor ( Qt : : WaitCursor ) ;
2023-01-23 23:42:20 +01:00
// verify if server is in history
2023-01-22 14:33:23 +01:00
this - > settings . beginGroup ( " connexion/server " ) ;
if ( this - > settings . contains ( server ) )
2023-01-07 12:44:45 +01:00
{
2023-02-02 16:10:51 +01:00
// server is in history => setting port value
2023-01-22 14:33:23 +01:00
port = this - > settings . value ( server ) . toUInt ( ) ;
ui - > portEdit - > setText ( QString : : number ( port ) ) ;
this - > connexion . port = port ;
2023-02-02 16:10:51 +01:00
listServices ( ) ;
} else
2023-01-22 14:33:23 +01:00
{
2023-02-02 16:10:51 +01:00
if ( ! server . isEmpty ( ) and ( port > 0 and port < 65536 ) )
2023-01-15 13:26:14 +01:00
{
2023-02-02 16:10:51 +01:00
if ( validateServer ( server . toStdString ( ) ) )
2023-01-22 14:33:23 +01:00
{
cout < < server . toStdString ( ) < < endl ;
// storing serverURL and port in settings
this - > settings . setValue ( server , port ) ;
this - > settings . sync ( ) ;
2023-01-31 00:45:53 +01:00
this - > downloading . server = server . toStdString ( ) ;
2023-01-15 13:26:14 +01:00
2023-01-22 14:33:23 +01:00
// storing in history of combobox
ui - > khistorycombobox - > addToHistory ( server ) ;
2023-02-02 16:10:51 +01:00
// "waiting" cursor
// load and display rsync services of the rsync server
listServices ( ) ;
2023-01-22 14:33:23 +01:00
}
2023-01-15 13:26:14 +01:00
}
2023-02-02 16:10:51 +01:00
2023-01-07 12:44:45 +01:00
}
2023-01-22 14:33:23 +01:00
this - > settings . endGroup ( ) ;
QGuiApplication : : restoreOverrideCursor ( ) ; //setOverrideCursor(Qt::ArrowCursor);
2023-01-07 12:44:45 +01:00
}
}
2023-01-23 23:42:20 +01:00
//list services of the rsync server
2023-01-12 09:23:45 +01:00
void MainWindow : : listServices ( )
2023-01-07 12:44:45 +01:00
{
char cmd [ 4096 ] ;
string line ;
string errorRsync ;
vector < string > v ;
char service [ 4096 ] ;
2023-01-12 09:23:45 +01:00
sprintf ( cmd , " rsync --contimeout=10 -P \" %s:: \" --port %d " , this - > connexion . server . c_str ( ) , this - > connexion . port ) ;
2023-01-07 12:44:45 +01:00
redi : : ipstream in ( cmd , redi : : pstreams : : pstdout | redi : : pstreams : : pstderr ) ;
while ( getline ( in . out ( ) , line ) )
{
boost : : replace_all ( line , " " , " " ) ;
boost : : replace_all ( line , " \t " , " - " ) ;
v = explode ( line , ' ' , 3 ) ;
sprintf ( service , " %s \n \t %s " , v [ 0 ] . c_str ( ) , v [ 2 ] . c_str ( ) ) ;
ui - > listWidget - > addItem ( service ) ;
}
}
2023-01-23 23:42:20 +01:00
// connect to rsync server to get list of files
2023-01-07 12:44:45 +01:00
void MainWindow : : scanDir ( string server , int portN , QTreeWidgetItem * parent , string path )
{
char cmd [ 4096 ] ;
string line ;
string errorRsync ;
vector < string > v ;
QTreeWidgetItem * item ;
2023-01-26 11:31:56 +01:00
bool isDir = false ;
2023-01-07 12:44:45 +01:00
sprintf ( cmd , " rsync --contimeout=10 -P \" %s::%s \" --port %d " , server . c_str ( ) , path . c_str ( ) , portN ) ;
redi : : ipstream in ( cmd , redi : : pstreams : : pstdout | redi : : pstreams : : pstderr ) ;
while ( getline ( in . out ( ) , line ) )
{
v = explode ( line , ' ' , 5 ) ;
if ( v . size ( ) = = 5 )
{
if ( v [ 4 ] . at ( 0 ) ! = ' . ' and ( v [ 0 ] . at ( 0 ) = = ' - ' or v [ 0 ] . at ( 0 ) = = ' d ' ) )
{
2023-01-26 11:31:56 +01:00
if ( v [ 0 ] . at ( 0 ) = = ' d ' )
2023-01-07 12:44:45 +01:00
{
2023-01-26 11:31:56 +01:00
isDir = true ;
2023-01-07 12:44:45 +01:00
} else
{
2023-01-26 11:31:56 +01:00
isDir = false ;
2023-01-07 12:44:45 +01:00
}
2023-01-26 11:31:56 +01:00
if ( parent ! = NULL )
{
item = addTreeChild ( parent , QString : : fromStdString ( v [ 4 ] ) , QString : : fromStdString ( v [ 1 ] ) , isDir ) ;
} else
2023-01-07 12:44:45 +01:00
{
2023-01-26 11:31:56 +01:00
item = addTreeRoot ( QString : : fromStdString ( v [ 4 ] ) , QString : : fromStdString ( v [ 1 ] ) , isDir ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-26 11:31:56 +01:00
2023-01-07 12:44:45 +01:00
}
}
}
2023-01-26 11:31:56 +01:00
2023-01-07 12:44:45 +01:00
// if reading stdout stopped at EOF then reset the state:
if ( in . eof ( ) & & in . fail ( ) )
in . clear ( ) ;
2023-01-26 11:31:56 +01:00
2023-01-07 12:44:45 +01:00
// read child's stderr
while ( getline ( in . err ( ) , line ) )
{
cout < < " stderr: " < < line < < endl ;
errorRsync . append ( line ) ;
errorRsync . append ( " \n " ) ;
}
2023-01-26 11:31:56 +01:00
2023-01-07 12:44:45 +01:00
if ( ! errorRsync . empty ( ) )
{
QMessageBox : : warning (
this ,
" RsyncUI " ,
errorRsync . c_str ( ) ) ;
}
}
2023-01-23 23:42:20 +01:00
// Verify if server address is IP address
2023-01-07 12:44:45 +01:00
bool MainWindow : : isIpAddress ( string server )
{
vector < string > r ;
int elementN ;
2023-01-12 09:23:45 +01:00
QString qr ;
bool ok ;
2023-01-07 12:44:45 +01:00
2023-01-12 09:23:45 +01:00
r = explode ( server , ' . ' , 5 ) ;
2023-01-07 12:44:45 +01:00
if ( r . size ( ) = = 4 )
{
for ( auto element : r )
{
2023-01-12 09:23:45 +01:00
elementN = QString : : fromStdString ( element ) . toInt ( & ok ) ;
if ( elementN < 0 or elementN > 255 or ok = = false )
2023-01-07 12:44:45 +01:00
{
2023-01-12 09:23:45 +01:00
return false ;
2023-01-07 12:44:45 +01:00
}
}
2023-01-18 23:12:21 +01:00
return true ;
} else
{
return false ;
2023-01-07 12:44:45 +01:00
}
}
2023-01-23 23:42:20 +01:00
// validate address server
2023-01-07 12:44:45 +01:00
bool MainWindow : : validateServer ( string server )
{
char cmd [ 512 ] ;
string line ;
string errorDig ;
bool flag = false ;
sprintf ( cmd , " dig %s " , server . c_str ( ) ) ;
redi : : ipstream in ( cmd , redi : : pstreams : : pstdout | redi : : pstreams : : pstderr ) ;
while ( getline ( in . out ( ) , line ) )
{
2023-01-15 13:26:14 +01:00
//cout << "stdout: " << line << '\n';
2023-01-07 12:44:45 +01:00
if ( line . find ( " ;; ANSWER SECTION: " ) ! = string : : npos )
{
flag = true ;
}
}
// if reading stdout stopped at EOF then reset the state:
if ( in . eof ( ) & & in . fail ( ) )
in . clear ( ) ;
// read child's stderr
while ( std : : getline ( in . err ( ) , line ) )
{
cout < < " stderr: " < < line < < ' \n ' ;
errorDig . append ( line ) ;
errorDig . append ( " \n " ) ;
}
if ( ! errorDig . empty ( ) )
{
QMessageBox : : warning (
this ,
" RsyncUI " ,
errorDig . c_str ( )
) ;
}
if ( flag = = false )
{
flag = isIpAddress ( server ) ;
2023-01-12 09:23:45 +01:00
}
if ( flag = = false )
{
2023-01-07 12:44:45 +01:00
QMessageBox : : warning (
this ,
" RsyncUI " ,
tr ( " server does not exists " )
) ;
}
return flag ;
}
2023-01-23 23:42:20 +01:00
// slot activated when combobox is changed
2023-01-15 13:26:14 +01:00
void MainWindow : : on_khistorycombobox_currentIndexChanged ( int i )
2023-01-07 12:44:45 +01:00
{
2023-01-22 14:33:23 +01:00
on_connectButton_clicked ( ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
// slot activated when button connection is clicked
2023-01-18 23:12:21 +01:00
void MainWindow : : on_connectButton_clicked ( )
2023-01-07 12:44:45 +01:00
{
2023-01-22 14:33:23 +01:00
populateList ( ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
// add a dir in treeview
2023-01-26 11:31:56 +01:00
QTreeWidgetItem * MainWindow : : addTreeRoot ( QString name , QString fileSize , bool isDir )
2023-01-07 12:44:45 +01:00
{
// QTreeWidgetItem(QTreeWidget * parent, int type = Type)
QTreeWidgetItem * treeItem = new QTreeWidgetItem ( ui - > treeWidget ) ;
// QTreeWidgetItem::setText(int column, const QString & text)
2023-01-26 11:31:56 +01:00
if ( isDir = = true )
{
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 1 , tr ( " Dir " ) ) ;
2023-01-26 11:31:56 +01:00
} else
{
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 1 , tr ( " File " ) ) ;
2023-01-26 11:31:56 +01:00
}
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 0 , name ) ;
2023-01-26 11:31:56 +01:00
treeItem - > setText ( 2 , fileSize ) ;
2023-01-07 12:44:45 +01:00
return treeItem ;
}
2023-01-23 23:42:20 +01:00
// add a file in treeview
2023-01-26 11:31:56 +01:00
QTreeWidgetItem * MainWindow : : addTreeChild ( QTreeWidgetItem * parent , QString name , QString fileSize , bool isDir )
2023-01-07 12:44:45 +01:00
{
// QTreeWidgetItem(QTreeWidget * parent, int type = Type)
QTreeWidgetItem * treeItem = new QTreeWidgetItem ( ) ;
// QTreeWidgetItem::setText(int column, const QString & text)
2023-01-26 11:31:56 +01:00
if ( isDir = = true )
{
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 1 , tr ( " Dir " ) ) ;
2023-01-26 11:31:56 +01:00
} else
{
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 1 , ( " File " ) ) ;
2023-01-26 11:31:56 +01:00
}
2023-01-31 00:45:53 +01:00
treeItem - > setText ( 0 , name ) ;
2023-01-26 11:31:56 +01:00
treeItem - > setText ( 2 , fileSize ) ;
2023-01-07 12:44:45 +01:00
// QTreeWidgetItem::addChild(QTreeWidgetItem * child)
parent - > addChild ( treeItem ) ;
return treeItem ;
}
2023-01-23 23:42:20 +01:00
// Slot acivated when a service in the list is clicked
2023-01-18 23:12:21 +01:00
void MainWindow : : on_listWidget_clicked ( )
2023-01-07 12:44:45 +01:00
{
2023-01-12 09:23:45 +01:00
vector < string > v ;
2023-01-31 00:45:53 +01:00
QString str ;
2023-01-12 09:23:45 +01:00
v = explode ( ui - > listWidget - > currentItem ( ) - > text ( ) . toStdString ( ) , ' \n ' , 2 ) ;
2023-02-02 16:10:51 +01:00
this - > connexion . service = v [ 0 ] ;
str = QString : : fromStdString ( " Folder/ " + this - > connexion . server + " / " + this - > connexion . service ) ;
2023-01-31 00:45:53 +01:00
if ( this - > settings . contains ( str ) )
{
this - > downloading . savePath = this - > settings . value ( str ) . toString ( ) . toStdString ( ) ;
}
2023-01-26 11:31:56 +01:00
populateTree ( NULL ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-23 23:42:20 +01:00
//Slot activated when a file is clicked in the treeview
2023-02-02 17:14:04 +01:00
void MainWindow : : on_treeWidget_itemClicked ( QTreeWidgetItem * item , bool downloadDir )
2023-01-07 12:44:45 +01:00
{
QFuture < void > future ;
QFileDialog dialog ;
2023-01-26 11:31:56 +01:00
QTreeWidgetItem * itemR ;
string path ;
2023-01-31 00:45:53 +01:00
QString str ;
2023-01-07 12:44:45 +01:00
2023-01-26 11:31:56 +01:00
//item = ui->treeWidget->currentItem();
itemR = item ;
2023-01-31 00:45:53 +01:00
path = item - > text ( 0 ) . toStdString ( ) ;
2023-01-26 11:31:56 +01:00
while ( itemR - > parent ( ) ! = NULL )
2023-01-07 12:44:45 +01:00
{
2023-01-26 11:31:56 +01:00
itemR = itemR - > parent ( ) ;
2023-01-31 00:45:53 +01:00
path = itemR - > text ( 0 ) . toStdString ( ) + " / " + path ;
2023-01-12 09:23:45 +01:00
} ;
2023-01-07 12:44:45 +01:00
2023-02-02 17:14:04 +01:00
if ( item - > text ( 1 ) = = tr ( " File " ) or downloadDir = = true )
2023-01-12 09:23:45 +01:00
{
2023-01-26 11:31:56 +01:00
// Item is a file
this - > downloading . path = path ;
2023-02-02 16:10:51 +01:00
this - > downloading . server = this - > connexion . server ;
this - > downloading . service = this - > connexion . service ;
// exists saving path in settings ?
str = QString : : fromStdString ( " Folder/ " + this - > connexion . server + " / " + this - > downloading . service ) ;
if ( ! this - > settings . contains ( str ) )
2023-01-26 11:31:56 +01:00
{
2023-02-02 16:10:51 +01:00
// saving path do not exists, asking for it
if ( ! on_DefaultSaveFolder_triggered ( ) )
2023-01-31 00:45:53 +01:00
{
2023-02-02 16:10:51 +01:00
cout < < " no directory selectioned, ignoring download request " ;
return ;
2023-01-31 00:45:53 +01:00
}
2023-02-02 16:10:51 +01:00
}
2023-01-26 11:31:56 +01:00
2023-02-02 16:10:51 +01:00
// is there a downloading process ?
if ( this - > pid = = 0 )
{
// no downloading process launching it
startDownloading ( ) ;
// wit 1 second to process start
//sleep(1);
2023-01-26 11:31:56 +01:00
}
2023-02-02 16:10:51 +01:00
// Adding download in download list
2023-01-31 00:45:53 +01:00
str = QString : : fromStdString ( this - > downloading . path + " => " + this - > connexion . server + " / " + this - > downloading . service ) ;
ui - > listDownload - > addItem ( str ) ;
2023-01-26 11:31:56 +01:00
} else
2023-01-22 14:33:23 +01:00
{
2023-01-26 11:31:56 +01:00
//Item is a Directory
2023-02-02 16:10:51 +01:00
scanDir ( this - > connexion . server , this - > connexion . port , item , this - > connexion . service + " / " + path + " / " ) ;
2023-01-26 11:31:56 +01:00
item - > setExpanded ( true ) ;
2023-01-12 09:23:45 +01:00
}
2023-01-26 11:31:56 +01:00
2023-01-12 09:23:45 +01:00
}
2023-01-23 23:42:20 +01:00
// Launch the thread which download the file
2023-01-12 09:23:45 +01:00
void MainWindow : : startDownloading ( )
{
2023-01-07 12:44:45 +01:00
ui - > progressBar - > setValue ( 0 ) ;
ui - > progressBar - > show ( ) ;
2023-01-12 09:23:45 +01:00
QtConcurrent : : run ( & this - > downloadO , & downloadFile : : download , this ) ;
}
2023-01-23 23:42:20 +01:00
// Slot stopping download
2023-01-12 09:23:45 +01:00
void MainWindow : : stoppingDownload ( )
{
2023-02-02 16:10:51 +01:00
emit ( stopDownloading ( this - > pid ) ) ;
2023-01-12 09:23:45 +01:00
}
2023-01-23 23:42:20 +01:00
// when download is finished, launch download of next file in queue
2023-01-12 09:23:45 +01:00
void MainWindow : : downloadFinished ( )
{
2023-01-31 00:45:53 +01:00
string path ;
int pos ;
string str ;
2023-02-02 16:10:51 +01:00
this - > pid = 0 ;
2023-01-12 09:23:45 +01:00
ui - > progressBar - > hide ( ) ;
delete ui - > listDownload - > takeItem ( 0 ) ;
2023-02-02 16:10:51 +01:00
this - > downloading . clear ( ) ;
2023-01-12 09:23:45 +01:00
if ( ui - > listDownload - > count ( ) ! = 0 )
{
2023-01-31 00:45:53 +01:00
path = ui - > listDownload - > item ( 0 ) - > text ( ) . toStdString ( ) ;
pos = path . rfind ( " / " ) ;
this - > downloading . service = path . substr ( pos + 1 ) ;
path . resize ( pos ) ;
pos = path . rfind ( " => " ) ;
this - > downloading . server = path . substr ( pos + 4 ) ;
path . resize ( pos ) ;
this - > downloading . path = path ;
str = " Folder/ " + this - > downloading . server + " / " + this - > downloading . service ;
2023-02-02 16:10:51 +01:00
if ( this - > settings . contains ( QString : : fromStdString ( str ) ) )
2023-01-31 00:45:53 +01:00
{
2023-02-02 16:10:51 +01:00
this - > downloading . savePath = this - > settings . value ( QString : : fromStdString ( str ) ) . toString ( ) . toStdString ( ) ;
} else
{
if ( ! on_DefaultSaveFolder_triggered ( ) )
{
cout < < " Error no save path so deleting download " ;
downloadFinished ( ) ;
return ;
}
2023-01-31 00:45:53 +01:00
}
2023-01-12 09:23:45 +01:00
startDownloading ( ) ;
}
}
2023-02-02 16:10:51 +01:00
2023-01-23 23:42:20 +01:00
// Slot activated when a line is clicked in queue list
2023-01-12 09:23:45 +01:00
void MainWindow : : on_listDownload_itemClicked ( QListWidgetItem * item )
{
QFileDialog dialog ;
QMessageBox : : StandardButton reply ;
2023-01-15 13:26:14 +01:00
//cout << item->text().toStdString() << endl;
2023-01-12 09:23:45 +01:00
if ( item - > listWidget ( ) - > row ( item ) = = 0 )
{
reply = QMessageBox : : question (
this ,
" RsyncUI " ,
tr ( " Do you want to stop downloading and delete this file from download queue ? " ) ,
QMessageBox : : Yes | QMessageBox : : No ,
QMessageBox : : No ) ;
if ( reply = = QMessageBox : : Yes )
{
2023-02-02 16:10:51 +01:00
emit ( stopDownloading ( this - > pid ) ) ;
2023-01-12 09:23:45 +01:00
}
} else
{
reply = QMessageBox : : question (
this ,
" RsyncUI " ,
tr ( " Do you want to delete this file from download queue ? " ) ,
QMessageBox : : Yes | QMessageBox : : No ,
QMessageBox : : No ) ;
if ( reply = = QMessageBox : : Yes )
{
ui - > listDownload - > removeItemWidget ( item ) ;
delete item ;
}
}
}
2023-01-23 23:42:20 +01:00
// load settings
2023-01-12 09:23:45 +01:00
void MainWindow : : loadSettings ( )
{
2023-01-26 11:31:56 +01:00
// restoring geometry and state of window and widgets
2023-02-02 16:10:51 +01:00
this - > restoreGeometry ( this - > settings . value ( " window/geometry " ) . toByteArray ( ) ) ;
this - > restoreState ( this - > settings . value ( " window/state " ) . toByteArray ( ) ) ;
ui - > treeWidget - > header ( ) - > restoreState ( this - > settings . value ( " treeWidget/state " ) . toByteArray ( ) ) ;
ui - > splitter - > restoreState ( this - > settings . value ( " splitter/state " ) . toByteArray ( ) ) ;
ui - > splitter_2 - > restoreState ( this - > settings . value ( " splitter2/state " ) . toByteArray ( ) ) ;
ui - > toolBar - > setToolButtonStyle ( ( Qt : : ToolButtonStyle ) this - > settings . value ( " toolbar/state " ) . toInt ( ) ) ;
2023-01-15 15:25:22 +01:00
2023-01-22 14:33:23 +01:00
// loading connexion settings
// loading servers history
2023-01-15 13:26:14 +01:00
this - > settings . beginGroup ( " connexion/server " ) ;
QStringList servers = this - > settings . allKeys ( ) ;
this - > settings . endGroup ( ) ;
for ( const QString & server : servers )
{
ui - > khistorycombobox - > addToHistory ( server ) ;
}
2023-01-22 14:33:23 +01:00
// loading save path
this - > downloading . savePath = this - > settings . value ( " Folder " ) . toString ( ) . toStdString ( ) ;
// loading bandwidth limit
this - > connexion . bandwidthLimit = this - > settings . value ( " bandwidthlimit " ) . toUInt ( ) ;
this - > connexion . bandwidthLimitUnit = this - > settings . value ( " bandwidthlimitunit " ) . toString ( ) . toStdString ( ) ;
2023-01-12 09:23:45 +01:00
}
2023-01-23 23:42:20 +01:00
// save settings
2023-01-12 09:23:45 +01:00
void MainWindow : : saveSettings ( )
{
2023-01-15 13:26:14 +01:00
this - > settings . setValue ( " window/geometry " , saveGeometry ( ) ) ;
2023-01-15 15:25:22 +01:00
this - > settings . setValue ( " window/state " , saveState ( ) ) ;
2023-01-26 11:31:56 +01:00
this - > settings . setValue ( " treeWidget/state " , ui - > treeWidget - > header ( ) - > saveState ( ) ) ;
2023-01-15 15:25:22 +01:00
this - > settings . setValue ( " splitter/state " , ui - > splitter - > saveState ( ) ) ;
this - > settings . setValue ( " splitter2/state " , ui - > splitter_2 - > saveState ( ) ) ;
2023-01-22 14:33:23 +01:00
this - > settings . setValue ( " connexion/lastServer " , QString : : fromStdString ( this - > connexion . server ) ) ;
this - > settings . setValue ( " connexion/lastPort " , QString : : number ( this - > connexion . port ) ) ;
2023-01-27 19:43:48 +01:00
this - > settings . setValue ( " toolbar/state " , ui - > toolBar - > toolButtonStyle ( ) ) ;
2023-01-15 13:26:14 +01:00
this - > settings . sync ( ) ;
}
2023-01-23 23:42:20 +01:00
// About
2023-01-15 13:26:14 +01:00
void MainWindow : : on_actionAbout_triggered ( )
{
2023-02-02 16:10:51 +01:00
//TODO => initialisation
2023-01-15 13:26:14 +01:00
QString text = this - > about . description + " \n \n " +
2023-01-23 23:42:20 +01:00
tr ( " Version " ) + " : " + this - > about . version + " \n " +
tr ( " Licence " ) + " : " + this - > about . licence + " \n " +
2023-01-22 14:33:23 +01:00
tr ( " Author " ) + " : " + this - > about . author + " \n " +
tr ( " EMail " ) + " : " + this - > about . email + " \n " +
tr ( " Source code " ) + " : " + this - > about . git ;
2023-01-15 13:26:14 +01:00
QMessageBox : : about ( this , this - > about . title , text ) ;
}
2023-01-23 23:42:20 +01:00
// About QT
2023-01-15 13:26:14 +01:00
void MainWindow : : on_actionAbout_Qt_triggered ( )
{
QMessageBox : : aboutQt ( this ) ;
2023-01-07 12:44:45 +01:00
}
2023-01-15 15:25:22 +01:00
2023-01-23 23:42:20 +01:00
// Activated when menu "change folder" is clicked
2023-02-02 16:10:51 +01:00
bool MainWindow : : on_DefaultSaveFolder_triggered ( )
2023-01-15 15:25:22 +01:00
{
QFileDialog dialog ;
2023-01-31 00:45:53 +01:00
string folder ;
string path ;
2023-02-02 16:10:51 +01:00
if ( this - > connexion . service . empty ( ) )
{
QMessageBox : : warning (
NULL ,
" RsyncUI " ,
" Since the save path is linked to service, you need to select a service before you can select a folder " ) ;
return false ;
}
path = dialog . getExistingDirectory ( this , tr ( " Choose directory to save file " ) , QString : : fromStdString ( getpwuid ( getuid ( ) ) - > pw_dir ) , QFileDialog : : ShowDirsOnly | QFileDialog : : DontResolveSymlinks ) . toStdString ( ) ;
2023-01-31 00:45:53 +01:00
if ( ! path . empty ( ) )
{
this - > downloading . savePath = path ;
2023-02-02 16:10:51 +01:00
if ( ! this - > connexion . service . empty ( ) and ! this - > connexion . server . empty ( ) )
2023-01-31 00:45:53 +01:00
{
2023-02-02 16:10:51 +01:00
folder = " Folder/ " + this - > connexion . server + " / " + this - > connexion . service ;
2023-01-31 00:45:53 +01:00
this - > settings . setValue ( folder . c_str ( ) , this - > downloading . savePath . c_str ( ) ) ;
this - > settings . sync ( ) ;
}
2023-02-02 16:10:51 +01:00
} else
{
return false ;
2023-01-31 00:45:53 +01:00
}
2023-02-02 16:10:51 +01:00
return true ;
2023-01-22 14:33:23 +01:00
}
2023-01-23 23:42:20 +01:00
// Activated when menu "settings" is clicked
2023-01-22 14:33:23 +01:00
void MainWindow : : on_action_Settings_triggered ( )
{
2023-01-26 11:31:56 +01:00
config . UnitCombobox - > setCurrentIndex ( bwUnixIndex [ this - > connexion . bandwidthLimitUnit [ 0 ] ] ) ;
2023-01-22 14:33:23 +01:00
config . spinBox - > setValue ( this - > connexion . bandwidthLimit ) ;
Configuration . show ( ) ;
}
2023-01-23 23:42:20 +01:00
// Acivated when "Ok" is clicked in Configuration window
2023-01-22 14:33:23 +01:00
void MainWindow : : on_buttonBox_accepted ( )
{
QString unit ;
QString bw ;
bw = config . spinBox - > text ( ) ;
if ( bw . toInt ( ) = = 0 )
{
this - > connexion . bandwidthLimit = 1000 ;
this - > connexion . bandwidthLimitUnit = tr ( " PB " ) . toStdString ( ) ;
} else
{
this - > connexion . bandwidthLimit = config . spinBox - > value ( ) ;
2023-01-26 11:31:56 +01:00
this - > connexion . bandwidthLimitUnit = config . UnitCombobox - > currentText ( ) . toStdString ( ) [ 0 ] ;
2023-01-22 14:33:23 +01:00
}
this - > settings . setValue ( " bandwidthlimit " , this - > connexion . bandwidthLimit ) ;
this - > settings . setValue ( " bandwidthlimitunit " , this - > connexion . bandwidthLimitUnit . c_str ( ) ) ;
this - > settings . sync ( ) ;
2023-01-27 18:14:50 +01:00
Configuration . hide ( ) ;
}
2023-02-02 16:10:51 +01:00
// Saving download list
void MainWindow : : saveDownloadList ( )
{
int nRows ;
nRows = ui - > listDownload - > count ( ) ;
//this->settings.beginWriteArray("Downloads/");
this - > settings . beginGroup ( " Downloads " ) ;
this - > settings . setValue ( " rows " , nRows ) ;
for ( int i = 0 ; i < nRows ; i + + )
{
this - > settings . setValue ( QString : : number ( i ) , ui - > listDownload - > item ( i ) - > text ( ) ) ;
}
this - > settings . endGroup ( ) ;
this - > settings . sync ( ) ;
}
void MainWindow : : loadDownloadList ( )
{
string path ;
string str ;
int pos ;
this - > settings . beginGroup ( " Downloads " ) ;
int size = this - > settings . value ( " rows " ) . toInt ( ) ;
for ( int i = 0 ; i < size ; + + i )
{
ui - > listDownload - > addItem ( this - > settings . value ( QString : : number ( i ) ) . toString ( ) ) ;
}
this - > settings . endGroup ( ) ;
this - > settings . remove ( " Downloads " ) ;
this - > settings . sync ( ) ;
path = ui - > listDownload - > item ( 0 ) - > text ( ) . toStdString ( ) ;
pos = path . rfind ( " / " ) ;
this - > downloading . service = path . substr ( pos + 1 ) ;
path . resize ( pos ) ;
pos = path . rfind ( " => " ) ;
this - > downloading . server = path . substr ( pos + 4 ) ;
path . resize ( pos ) ;
this - > downloading . path = path ;
str = " Folder/ " + this - > downloading . server + " / " + this - > downloading . service ;
if ( this - > settings . contains ( QString : : fromStdString ( str ) ) )
{
this - > downloading . savePath = this - > settings . value ( QString : : fromStdString ( str ) ) . toString ( ) . toStdString ( ) ;
}
startDownloading ( ) ;
}
void Downloading : : clear ( )
{
this - > path . clear ( ) ;
this - > server . clear ( ) ;
this - > savePath . clear ( ) ;
this - > service . clear ( ) ;
}
2023-02-02 17:14:04 +01:00
void MainWindow : : on_actionDownload_triggered ( )
{
// action made in qt-designer and added in init function.
QTreeWidgetItem * item ;
item = ui - > treeWidget - > currentItem ( ) ;
on_treeWidget_itemClicked ( item , true ) ;
}