From db79e37dc2710b4c64d7ccb3c7002004aafef755 Mon Sep 17 00:00:00 2001 From: mhogomchungu Date: Sun, 8 Sep 2024 23:55:55 +0300 Subject: [PATCH] code improvements --- src/basicdownloader.cpp | 19 ++++-- src/utility.cpp | 126 +++++++++++++++++++++++++++------------- 2 files changed, 98 insertions(+), 47 deletions(-) diff --git a/src/basicdownloader.cpp b/src/basicdownloader.cpp index e448e238..e7324175 100644 --- a/src/basicdownloader.cpp +++ b/src/basicdownloader.cpp @@ -98,15 +98,22 @@ basicdownloader::basicdownloader( const Context& ctx ) : utility::contextMenuForDirectUrl( m,obj,m_ctx ) ; - m.addSeparator() ; - } + m.addSeparator() ; - connect( m.addAction( tr( "Hide List" ) ),&QAction::triggered,[ this ](){ + connect( m.addAction( tr( "Hide List" ) ),&QAction::triggered,[ this ](){ - m_tableList.setVisible( false ) ; - } ) ; + m_tableList.setVisible( false ) ; + } ) ; - m.exec( QCursor::pos() ) ; + m.exec( QCursor::pos() ) ; + }else{ + connect( m.addAction( tr( "Hide List" ) ),&QAction::triggered,[ this ](){ + + m_tableList.setVisible( false ) ; + } ) ; + + m.exec( QCursor::pos() ) ; + } } ) ; connect( m_ui.pbOptionsDownloadOptions,&QPushButton::clicked,[ this ](){ diff --git a/src/utility.cpp b/src/utility.cpp index 77f33b4a..c5d74afb 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -2199,95 +2199,139 @@ bool utility::addData( const QByteArray& e ) void utility::contextMenuForDirectUrl( QMenu& m,const QJsonObject& obj,const Context& ctx ) { - auto arr = obj.value( "urls" ).toArray() ; - - auto mediaPlayer = ctx.Settings().openWith( ctx.logger() ) ; + class mediaPlayerActions + { + public: + mediaPlayerActions( const QJsonObject& obj,QMenu& m,const Context& ctx ) : + m_obj( obj ), + m_array( m_obj.value( "urls" ).toArray() ), + m_menu( m ), + m_mediaPlayer( ctx.Settings().openWith( ctx.logger() ) ), + m_ctx( ctx ) + { + if( m_array.empty() ){ - if( arr.size() == 0 ){ + this->disable() ; + }else{ + this->clipboard() ; + this->setOpenUrl() ; + } + } + void disable() + { + m_menu.addAction( QObject::tr( "Copy Url" ) )->setEnabled( false ) ; - m.addAction( QObject::tr( "Copy Url" ) )->setEnabled( false ) ; + if( m_mediaPlayer.valid() ){ - if( mediaPlayer.valid() ){ + auto mm = QObject::tr( "Open Url With %1" ) ; - for( const auto& e : mediaPlayer.opts() ){ + for( const auto& e : m_mediaPlayer.opts() ){ - auto s = QObject::tr( "Open Url With %1" ).arg( e.name ) ; + auto s = mm.arg( e.name ) ; - m.addAction( s )->setEnabled( false ) ; + m_menu.addAction( s )->setEnabled( false ) ; + } } } - }else{ - auto clipBoard = QApplication::clipboard() ; + void clipboard() + { + auto cpb = QApplication::clipboard() ; - auto act = &QAction::triggered ; + if( !cpb ){ - if( clipBoard ){ + return ; + } - if( arr.size() == 1 ){ + class cb + { + public: + cb( const QJsonValue& m,QClipboard& cb ) : + m_url( m.toString() ), + m_clipboard( cb ) + { + } + void operator()() + { + m_clipboard.setText( m_url ) ; + } + private: + QString m_url ; + QClipboard& m_clipboard ; + } ; - auto url = arr[ 0 ].toString() ; + auto act = &QAction::triggered ; - auto ee = m.addAction( QObject::tr( "Copy Url" ) ) ; + if( m_array.size() == 1 ){ - QObject::connect( ee,act,[ clipBoard,url ](){ + auto ee = m_menu.addAction( QObject::tr( "Copy Url" ) ) ; - clipBoard->setText( url ) ; - } ) ; + QObject::connect( ee,act,cb( m_array[ 0 ],*cpb ) ) ; }else{ - for( int i = 0 ; i < arr.size() ; i++ ){ + for( int i = 0 ; i < m_array.size() ; i++ ){ auto e = QString::number( i + 1 ) ; auto s = QObject::tr( "Copy Url %1" ).arg( e ) ; - auto url = arr[ i ].toString() ; - - auto ee = m.addAction( s ) ; - - QObject::connect( ee,act,[ clipBoard,url ](){ + auto ee = m_menu.addAction( s ) ; - clipBoard->setText( url ) ; - } ) ; + QObject::connect( ee,act,cb( m_array[ i ],*cpb ) ) ; } } } + void setOpenUrl() + { + auto act = &QAction::triggered ; + + const auto& adp = m_ctx.Settings().appDataPath() ; - if( mediaPlayer.valid() ){ + if( m_array.size() == 1 ){ - const auto& adp = ctx.Settings().appDataPath() ; + auto mm = QObject::tr( "Open Url With %1" ) ; - if( arr.size() == 1 ){ + for( const auto& e : m_mediaPlayer.opts() ){ - for( const auto& e : mediaPlayer.opts() ){ + auto s = mm.arg( e.name ) ; - auto s = QObject::tr( "Open Url With %1" ).arg( e.name ) ; + auto ee = m_menu.addAction( s ) ; - auto ee = m.addAction( s ) ; + auto ss = m_array[ 0 ].toString() ; - auto ac = mediaPlayer.ac( arr[ 0 ].toString(),e,adp,obj ) ; + auto ac = m_mediaPlayer.ac( ss,e,adp,m_obj ) ; QObject::connect( ee,act,ac.move() ) ; } - }else{ - for( int i = 0 ; i < arr.size() ; i++ ){ + for( int i = 0 ; i < m_array.size() ; i++ ){ auto e = QString::number( i + 1 ) ; - for( const auto& a : mediaPlayer.opts() ){ + auto mm = QObject::tr( "Open Url %1 With %2" ) ; - auto s = QObject::tr( "Open Url %1 With %2" ).arg( e,a.name ) ; + for( const auto& a : m_mediaPlayer.opts() ){ - auto ee = m.addAction( s ) ; + auto s = mm.arg( e,a.name ) ; - auto ac = mediaPlayer.ac( arr[ i ].toString(),a,adp,obj ) ; + auto ee = m_menu.addAction( s ) ; + + auto ss = m_array[ i ].toString() ; + + auto ac = m_mediaPlayer.ac( ss,a,adp,m_obj ) ; QObject::connect( ee,act,ac.move() ) ; } } } } - } + private: + const QJsonObject& m_obj ; + const QJsonArray m_array ; + QMenu& m_menu ; + settings::mediaPlayer m_mediaPlayer ; + const Context& m_ctx ; + } ; + + mediaPlayerActions( obj,m,ctx ) ; } void utility::deleteTmpFiles( const QString& df,std::vector< QByteArray > files )