From df67c0ff2886e947c8a58fc08eee625e09d255ed Mon Sep 17 00:00:00 2001 From: mhogomchungu Date: Sat, 29 Oct 2022 13:14:57 +0300 Subject: [PATCH] code improvement --- src/logwindow.cpp | 7 +++--- src/logwindow.h | 3 ++- src/qcheckgmail.cpp | 37 +++++++---------------------- src/qcheckgmail.h | 5 ++-- src/util.hpp | 58 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 35 deletions(-) diff --git a/src/logwindow.cpp b/src/logwindow.cpp index e7e4b26..a06d8f2 100644 --- a/src/logwindow.cpp +++ b/src/logwindow.cpp @@ -24,10 +24,11 @@ #include -logWindow::logWindow( settings& s ) : +logWindow::logWindow( settings& s,bool e ) : QWidget( nullptr ), m_ui( new Ui::logWindow ), - m_settings( s ) + m_settings( s ), + m_alwaysAddLogs( e ) { m_ui->setupUi( this ) ; @@ -51,7 +52,7 @@ logWindow::~logWindow() void logWindow::update( logWindow::TYPE type,const QString& msg ) { - if( this->isVisible() ){ + if( this->isVisible() || m_alwaysAddLogs ){ const auto bars = "*************************************************************************" ; diff --git a/src/logwindow.h b/src/logwindow.h index f2f1916..4bea056 100644 --- a/src/logwindow.h +++ b/src/logwindow.h @@ -35,7 +35,7 @@ class logWindow : public QWidget { Q_OBJECT public: - logWindow( settings& ) ; + logWindow( settings&,bool ) ; ~logWindow() override ; enum class TYPE{ REQUEST,RESPONCE,INFO,ERROR } ; void update( logWindow::TYPE,const QString& e ) ; @@ -46,6 +46,7 @@ class logWindow : public QWidget void closeEvent( QCloseEvent * ) override ; Ui::logWindow * m_ui ; settings& m_settings ; + bool m_alwaysAddLogs ; }; #endif // LOGWINDOW_H diff --git a/src/qcheckgmail.cpp b/src/qcheckgmail.cpp index d19b147..1d6acbb 100644 --- a/src/qcheckgmail.cpp +++ b/src/qcheckgmail.cpp @@ -32,9 +32,9 @@ qCheckGMail::qCheckGMail( const qCheckGMail::args& args ) : m_manager( m_settings.networkTimeOut() ), m_networkRequest( QUrl( m_auth ) ), - m_logWindow( m_settings ), m_qApp( args.app ), m_args( m_qApp.arguments() ), + m_logWindow( m_settings,m_args.contains( "-d" ) ), m_statusicon( m_settings,this->clickActions() ) { m_networkRequest.setRawHeader( "Content-Type","application/x-www-form-urlencoded" ) ; @@ -140,7 +140,6 @@ void qCheckGMail::start() m_statusicon.setCategory( m_statusicon.ApplicationStatus ) ; QCoreApplication::setApplicationName( "qCheckGMail" ) ; - m_enableDebug = m_statusicon.enableDebug() ; m_audioNotify = m_settings.audioNotify() ; m_interval = m_settings.checkForUpdatesInterval() ; m_newEmailIcon = m_settings.newEmailIcon() ; @@ -178,7 +177,7 @@ void qCheckGMail::start() this->getAccountsInfo() ; } -static void _start_detached( bool debug,QString& exe,const QString& url ) +static void _start_detached( logWindow& logger,QString& exe,const QString& url ) { if( exe.isEmpty() ){ @@ -189,31 +188,13 @@ static void _start_detached( bool debug,QString& exe,const QString& url ) QDesktopServices::openUrl( QUrl( url ) ) ; }else{ - auto s = util::split( exe,' ' ) ; + exe.replace( "%{url}",url ) ; - auto m = s.takeAt( 0 ) ; - - for( auto& it : s ){ - - if( it == "%{url}" ){ - - it = url ; - - break ; - } - } - - if( debug ){ - - std::cout << "\"" + m.toStdString() + "\"" ; - - for( const auto& it : s ){ + auto s = util::splitPreserveQuotes( exe ) ; - std::cout << " \"" + it.toStdString() + "\"" ; - } + auto m = s.takeAt( 0 ) ; - std::cout << std::endl ; - } + logger.update( logWindow::TYPE::INFO,"Running Command\n" + exe + " " + s.join( " " ) ) ; QProcess::startDetached( m,s ) ; } @@ -234,15 +215,15 @@ void qCheckGMail::openMail( const accounts& acc ) url.truncate( url.size() - int( ( sizeof( "/feed/atom/" ) - 1 ) ) ) ; } - _start_detached( m_enableDebug,m_defaultApplication,url ) ; + _start_detached( m_logWindow,m_defaultApplication,url ) ; }else{ - _start_detached( m_enableDebug,m_defaultApplication,"https://mail.google.com/" ) ; + _start_detached( m_logWindow,m_defaultApplication,"https://mail.google.com/" ) ; } } void qCheckGMail::openMail() { - _start_detached( m_enableDebug,m_defaultApplication,"https://mail.google.com/" ) ; + _start_detached( m_logWindow,m_defaultApplication,"https://mail.google.com/" ) ; } void qCheckGMail::addActionsToMenu() diff --git a/src/qcheckgmail.h b/src/qcheckgmail.h index b8aaf6e..4f963a0 100644 --- a/src/qcheckgmail.h +++ b/src/qcheckgmail.h @@ -187,7 +187,6 @@ class qCheckGMail : public QObject bool m_displayEmailCount ; bool m_newMailFound ; bool m_checkingMail ; - bool m_enableDebug ; bool m_accountUpdated ; bool m_timeExpired ; bool m_accountFailed ; @@ -226,11 +225,11 @@ class qCheckGMail : public QObject QVector< accounts > m_accounts ; - logWindow m_logWindow ; - QApplication& m_qApp ; QStringList m_args ; + logWindow m_logWindow ; + statusicon m_statusicon ; int m_counter = -1 ; diff --git a/src/util.hpp b/src/util.hpp index e9a59ce..66c19d3 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -37,6 +37,7 @@ #include #include #include +#include #include "lxqt_wallet.h" @@ -79,6 +80,63 @@ namespace util #else return e.split( token,Qt::SkipEmptyParts ) ; #endif + } + static inline QStringList splitPreserveQuotes( const QString& e ) + { + #if QT_VERSION < QT_VERSION_CHECK( 5,15,0 ) + QStringList args ; + QString tmp ; + int quoteCount = 0 ; + bool inQuote = false ; + + for( int i = 0 ; i < e.size() ; ++i ) { + + const auto& s = e.at( i ) ; + + if( s == '"' ){ + + quoteCount++ ; + + if( quoteCount == 3 ) { + + quoteCount = 0 ; + tmp.append( s ) ; + } + + continue ; + } + + if( quoteCount ){ + + if( quoteCount == 1 ){ + + inQuote = !inQuote ; + } + + quoteCount = 0 ; + } + + if( !inQuote && s.isSpace() ){ + + if( !tmp.isEmpty() ){ + + args.append( tmp ) ; + tmp.clear() ; + } + }else{ + tmp.append( s ) ; + } + } + + if( !tmp.isEmpty() ){ + + args.append( tmp ) ; + } + + return args ; + #else + return QProcess::splitCommand( e ) ; + #endif } template< typename Labels > QString labelsToJson( const QString& userLabels,const Labels& labels )