From 864e351a678124498df59190d2398d0944aba557 Mon Sep 17 00:00:00 2001 From: mhogomchungu Date: Mon, 26 Aug 2024 22:16:55 +0300 Subject: [PATCH] add a context menu entry in batch downloader to rename already downloaded media --- src/batchdownloader.cpp | 189 ++++++++++++++++++++++++++----------- src/batchdownloader.h | 28 +++++- src/downloadmanager.hpp | 6 +- src/mainwindow.ui | 33 ++++++- src/playlistdownloader.cpp | 9 ++ src/tableWidget.cpp | 5 + src/tableWidget.h | 1 + src/utility.cpp | 34 +++++++ src/utility.h | 1 + 9 files changed, 246 insertions(+), 60 deletions(-) diff --git a/src/batchdownloader.cpp b/src/batchdownloader.cpp index 62bca24c..59a2bde5 100644 --- a/src/batchdownloader.cpp +++ b/src/batchdownloader.cpp @@ -79,7 +79,7 @@ batchdownloader::batchdownloader( const Context& ctx ) : m_table.setCurrentItemChanged( m_table.startPosition() ) ; - this->setVisibleMediaSectionCut( false ) ; + this->setVisibleWidgetOverMainTable( false ) ; m_ui.pbBDDownload->setEnabled( false ) ; @@ -258,52 +258,19 @@ batchdownloader::batchdownloader( const Context& ctx ) : connect( m_ui.pbSetTimeIntervals,&QPushButton::clicked,[ this ](){ - auto row = m_table.currentRow() ; - - if( row != -1 ){ - - auto a = m_ui.lineEditStartTimeInterval->text() ; - auto b = m_ui.lineEditEndTimeInterval->text() ; - auto c = m_ui.lineEditChapters->text() ; - - if( !a.isEmpty() && !b.isEmpty() ){ - - auto u = tableWidget::type::DownloadTimeInterval ; + if( m_widgetOverMainTable.showRenameUi() ){ - m_table.setDownloadingOptions( u,row,a + "-" + b ) ; - - }else if( a.isEmpty() && b.isEmpty() ){ - - //Left empty on purpose - }else{ - return ; - } - - if( !c.isEmpty() ){ - - auto u = tableWidget::type::DownloadChapters ; - - m_table.setDownloadingOptions( u,row,c ) ; - } - - if( m_ui.cbSplitByChapters->isChecked() ){ - - auto u = tableWidget::type::SplitByChapters ; - - m_table.setDownloadingOptions( u,row,"Yes" ) ; - }else{ - auto u = tableWidget::type::SplitByChapters ; - - m_table.setDownloadingOptions( u,row,"No" ) ; - } + this->renameFile( m_widgetOverMainTable.row() ) ; + }else{ + this->setTimeIntervals( m_widgetOverMainTable.row() ) ; } - this->setVisibleMediaSectionCut( false ) ; + this->setVisibleWidgetOverMainTable( false ) ; } ) ; connect( m_ui.pbCancelSetTimeInterval,&QPushButton::clicked,[ this ](){ - this->setVisibleMediaSectionCut( false ) ; + this->setVisibleWidgetOverMainTable( false ) ; } ) ; connect( m_ui.pbBDPasteClipboard,&QPushButton::clicked,[ this ](){ @@ -457,6 +424,19 @@ void batchdownloader::showCustomContext() } } ) ; + ac = m.addAction( tr( "Rename" ) ) ; + + auto canRename = m_table.entryAt( row ).fileNames.size() == 1 ; + + ac->setEnabled( m_table.finishedWithSuccess( row ) && canRename ) ; + + connect( ac,&QAction::triggered,[ this,row ](){ + + m_widgetOverMainTable = { true,row } ; + + this->setVisibleWidgetOverMainTable( true ) ; + } ) ; + ac = m.addAction( tr( "Remove" ) ) ; ac->setEnabled( m_table.noneAreRunning() ) ; @@ -521,9 +501,11 @@ void batchdownloader::showCustomContext() ac = m.addAction( tr( "Download Media Part" ) ) ; ac->setEnabled( !finishSuccess && engine.canDownloadMediaPart() ) ; - connect( ac,&QAction::triggered,[ this ](){ + connect( ac,&QAction::triggered,[ this,row ](){ + + m_widgetOverMainTable = { false,row } ; - this->setVisibleMediaSectionCut( true ) ; + this->setVisibleWidgetOverMainTable( true ) ; } ) ; utility::addDownloadContextMenu( running,finishSuccess,m,row,[ this ]( int row ){ @@ -544,7 +526,7 @@ void batchdownloader::showCustomContext() for( int row = 0 ; row < m_table.rowCount() ; row++ ){ - auto e = m_table.runningState( row ) ; + const auto& e = m_table.runningState( row ) ; auto visible = m_table.rowIsVisible( row ) ; auto highlighted = m_table.rowIsSelected( row ) ; @@ -1102,25 +1084,124 @@ void batchdownloader::normalizeFilePath( QString& e ) } } -void batchdownloader::setVisibleMediaSectionCut( bool e ) +void batchdownloader::setVisibleWidgetOverMainTable( bool e ) { if( e ){ - m_ui.lineEditStartTimeInterval->setFocus() ; + this->disableAll() ; }else{ - m_ui.lineEditBDUrl->setFocus() ; + this->enableAll() ; } - m_ui.labelSetTimeIntervals->setVisible( e ) ; + m_ui.labelWidgetOverMainTable->setVisible( e ) ; + m_ui.pbCancelSetTimeInterval->setVisible( e ) ; m_ui.pbSetTimeIntervals->setVisible( e ) ; - m_ui.lineEditChapters->setVisible( e ) ; - m_ui.lineEditStartTimeInterval->setVisible( e ) ; - m_ui.lineEditEndTimeInterval->setVisible( e ) ; - m_ui.cbSplitByChapters->setVisible( e ) ; - m_ui.label_7->setVisible( e ) ; - m_ui.label_8->setVisible( e ) ; - m_ui.label_9->setVisible( e ) ; + + if( m_widgetOverMainTable.showRenameUi() ){ + + m_ui.labelBDSetNewFileName->setVisible( e ) ; + m_ui.plainTextEditBD->setVisible( e ) ; + + if( e ){ + + const auto& m = m_table.entryAt( m_widgetOverMainTable.row() ).fileNames ; + + if( m.size() ){ + + m_ui.plainTextEditBD->clear() ; + + m_ui.plainTextEditBD->appendPlainText( m[ 0 ] ) ; + } + + m_ui.plainTextEditBD->setFocus() ; + } + + m_ui.lineEditChapters->setVisible( false ) ; + m_ui.lineEditStartTimeInterval->setVisible( false ) ; + m_ui.lineEditEndTimeInterval->setVisible( false ) ; + m_ui.cbSplitByChapters->setVisible( false ) ; + m_ui.label_7->setVisible( false ) ; + m_ui.label_8->setVisible( false ) ; + m_ui.label_9->setVisible( false ) ; + }else{ + if( e ){ + + m_ui.lineEditStartTimeInterval->setFocus() ; + }else{ + m_ui.lineEditBDUrl->setFocus() ; + } + + m_ui.lineEditChapters->setVisible( e ) ; + m_ui.lineEditStartTimeInterval->setVisible( e ) ; + m_ui.lineEditEndTimeInterval->setVisible( e ) ; + m_ui.cbSplitByChapters->setVisible( e ) ; + m_ui.label_7->setVisible( e ) ; + m_ui.label_8->setVisible( e ) ; + m_ui.label_9->setVisible( e ) ; + m_ui.labelBDSetNewFileName->setVisible( false ) ; + m_ui.plainTextEditBD->setVisible( false ) ; + } +} + +void batchdownloader::renameFile( int row ) +{ + const auto& df = m_ctx.Settings().downloadFolder() ; + const auto& fn = m_table.entryAt( row ).fileNames ; + const auto& nn = m_ui.plainTextEditBD->toPlainText() ; + + if( fn.size() ){ + + auto& item = m_table.item( row,m_table.startPosition() ) ; + + auto m = utility::rename( item,df,nn,fn[ 0 ] ) ; + + if( !m.isEmpty() ){ + + m_table.setFileNames( row,{ m } ) ; + } + } +} + +void batchdownloader::setTimeIntervals( int row ) +{ + if( row != -1 ){ + + auto a = m_ui.lineEditStartTimeInterval->text() ; + auto b = m_ui.lineEditEndTimeInterval->text() ; + auto c = m_ui.lineEditChapters->text() ; + + if( !a.isEmpty() && !b.isEmpty() ){ + + auto u = tableWidget::type::DownloadTimeInterval ; + + m_table.setDownloadingOptions( u,row,a + "-" + b ) ; + + }else if( a.isEmpty() && b.isEmpty() ){ + + //Left empty on purpose + }else{ + return ; + } + + if( !c.isEmpty() ){ + + auto u = tableWidget::type::DownloadChapters ; + + m_table.setDownloadingOptions( u,row,c ) ; + } + + if( m_ui.cbSplitByChapters->isChecked() ){ + + auto u = tableWidget::type::SplitByChapters ; + + m_table.setDownloadingOptions( u,row,"Yes" ) ; + }else{ + auto u = tableWidget::type::SplitByChapters ; + + m_table.setDownloadingOptions( u,row,"No" ) ; + } + } } void batchdownloader::showSubtitles( const QByteArray& e ) diff --git a/src/batchdownloader.h b/src/batchdownloader.h index c87ba67b..495fc189 100644 --- a/src/batchdownloader.h +++ b/src/batchdownloader.h @@ -234,7 +234,9 @@ private void sortComments() ; bool saveSubtitles( const QString& url,const QString& ext,const QString& title ) ; void normalizeFilePath( QString& ) ; - void setVisibleMediaSectionCut( bool ) ; + void setVisibleWidgetOverMainTable( bool ) ; + void renameFile( int ) ; + void setTimeIntervals( int ) ; QString setSubtitleString( const QJsonObject&,const QString& ) ; void parseDataFromFile( const QByteArray& ) ; void parseDataFromObject( const QJsonObject&,const QJsonArray& ) ; @@ -285,6 +287,30 @@ private batchdownloader::listType m_listType ; utility::Terminator m_terminator ; + class widgetOverMainTable + { + public: + widgetOverMainTable() + { + } + widgetOverMainTable( bool b,int r ) : m_row( r ),m_show( b ) + { + } + int row() const + { + return m_row ; + } + bool showRenameUi() const + { + return m_show ; + } + private: + int m_row ; + bool m_show ; + } ; + + widgetOverMainTable m_widgetOverMainTable ; + downloadManager m_ccmd ; downloadManager m_ccmd_metadata ; diff --git a/src/downloadmanager.hpp b/src/downloadmanager.hpp index db1651dd..8ad0a2db 100644 --- a/src/downloadmanager.hpp +++ b/src/downloadmanager.hpp @@ -77,15 +77,15 @@ class downloadManager { return finishedWithSuccess() == e ; } - static bool finishedCancelled( tableWidget& e,int row ) + static bool finishedCancelled( const tableWidget& e,int row ) { return finishedCancelled( e.runningState( row ) ) ; } - static bool finishedWithError( tableWidget& e,int row ) + static bool finishedWithError( const tableWidget& e,int row ) { return finishedWithError( e.runningState( row ) ) ; } - static bool finishedWithSuccess( tableWidget& e,int row ) + static bool finishedWithSuccess( const tableWidget& e,int row ) { return finishedWithSuccess( e.runningState( row ) ) ; } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index a4c83997..3ef640ba 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -24,7 +24,7 @@ - 4 + 1 @@ -635,7 +635,7 @@ - + 100 @@ -777,6 +777,35 @@ Split By Chapters + + + + 110 + 190 + 571 + 41 + + + + Set New File Name Below + + + Qt::AlignCenter + + + + + + 110 + 230 + 571 + 71 + + + + + + diff --git a/src/playlistdownloader.cpp b/src/playlistdownloader.cpp index e7477570..3c36bf7b 100644 --- a/src/playlistdownloader.cpp +++ b/src/playlistdownloader.cpp @@ -515,6 +515,15 @@ void playlistdownloader::customContextMenuRequested() m_terminator.terminate( row ) ; } ) ; + //ac = m.addAction( tr( "Rename" ) ) ; + + //ac->setEnabled( m_table.finishedWithSuccess( row ) ) ; + + //connect( ac,&QAction::triggered,[ this,row ](){ + + + //} ) ; + ac = m.addAction( tr( "Remove" ) ) ; ac->setEnabled( m_table.noneAreRunning() && !m_networkRunning ) ; diff --git a/src/tableWidget.cpp b/src/tableWidget.cpp index 90b774cc..ebab2e2b 100644 --- a/src/tableWidget.cpp +++ b/src/tableWidget.cpp @@ -252,6 +252,11 @@ int tableWidget::addItem( tableWidget::entry e,tableWidget::sizeHint s ) return row ; } +bool tableWidget::finishedWithSuccess( int row ) const +{ + return downloadManager::finishedStatus::finishedWithSuccess( *this,row ) ; +} + void tableWidget::selectRow( QTableWidgetItem * current,QTableWidgetItem * previous,int firstColumnNumber ) { auto _update_table_row = [ & ]( QTableWidgetItem * item,bool setSelected ){ diff --git a/src/tableWidget.h b/src/tableWidget.h index bb75de8f..3d6d600b 100644 --- a/src/tableWidget.h +++ b/src/tableWidget.h @@ -147,6 +147,7 @@ class tableWidget { return this->item( row ).runningState ; } + bool finishedWithSuccess( int row ) const ; QByteArray mediaEntry( int row ) const { auto obj = this->entryAt( row ).uiJson ; diff --git a/src/utility.cpp b/src/utility.cpp index 7760458e..c4955eb2 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -2324,3 +2324,37 @@ quint64 utility::simpleRandomNumber() { return static_cast< quint64 >( time( nullptr ) ) ; } + +QString utility::rename( QTableWidgetItem& item, + const QString& df, + const QString& newName, + const QString& oldName ) +{ + auto oldPath = df + "/" + oldName ; + + QFile ff( oldPath ) ; + + if( ff.exists() ){ + + auto newPath = df + "/" + newName ; + + if( ff.rename( newPath ) ){ + + auto txt = util::split( item.text(),"\n" ) ; + + for( int i = 0 ; i < txt.size() ; i++ ){ + + if( txt[ i ] == oldName ){ + + txt[ i ] = newName ; + + item.setText( txt.join( "\n" ) ) ; + + return newName ; + } + } + } + } + + return {} ; +} diff --git a/src/utility.h b/src/utility.h index dd1a1a63..2278f037 100644 --- a/src/utility.h +++ b/src/utility.h @@ -526,6 +526,7 @@ namespace utility bool platformIsLikeWindows() ; bool platformisFlatPak() ; bool addData( const QByteArray& ) ; + QString rename( QTableWidgetItem&,const QString&,const QString&,const QString& ) ; void contextMenuForDirectUrl( const QJsonObject&,const Context& ) ; void deleteTmpFiles( const QString&,std::vector< QByteArray > ) ; QString OSXApplicationDirPath() ;