Skip to content

Commit

Permalink
code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mhogomchungu committed Oct 29, 2022
1 parent a3f2775 commit df67c0f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
7 changes: 4 additions & 3 deletions src/logwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

#include <QDateTime>

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 ) ;

Expand All @@ -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 = "*************************************************************************" ;

Expand Down
3 changes: 2 additions & 1 deletion src/logwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ;
Expand All @@ -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
37 changes: 9 additions & 28 deletions src/qcheckgmail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) ;
Expand Down Expand Up @@ -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() ;
Expand Down Expand Up @@ -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() ){

Expand All @@ -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 ) ;
}
Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions src/qcheckgmail.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down Expand Up @@ -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 ;
Expand Down
58 changes: 58 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QLockFile>
#include <QThread>
#include <QEventLoop>
#include <QProcess>

#include "lxqt_wallet.h"

Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit df67c0f

Please sign in to comment.