Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mhogomchungu committed Sep 8, 2024
1 parent 694c831 commit db79e37
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 47 deletions.
19 changes: 13 additions & 6 deletions src/basicdownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ](){
Expand Down
126 changes: 85 additions & 41 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit db79e37

Please sign in to comment.