From 2ff7e4e6d04d1c8a36a52523d3078ba2f526310e Mon Sep 17 00:00:00 2001 From: Abs62 Date: Tue, 27 Mar 2018 17:46:03 +0300 Subject: [PATCH] Refactor quit/end-of-session handling --- hotkeywrapper.cc | 25 +++++++++++++++++++++---- hotkeywrapper.hh | 26 +++++++++++++++++++++++--- mainwindow.cc | 33 ++++++++++++++++++++++----------- mainwindow.hh | 4 ++-- 4 files changed, 68 insertions(+), 20 deletions(-) diff --git a/hotkeywrapper.cc b/hotkeywrapper.cc index 6c496231..345d70bb 100644 --- a/hotkeywrapper.cc +++ b/hotkeywrapper.cc @@ -16,11 +16,17 @@ ////////////////////////////////////////////////////////////////////////// QHotkeyApplication::QHotkeyApplication( int & argc, char ** argv ): - QtSingleApplication( argc, argv ) + QIntermediateApplication( argc, argv ) #ifdef Q_OS_WIN32 , mainWindow( 0 ) #endif { + connect( this, SIGNAL( commitDataRequest( QSessionManager& ) ), + this, SLOT( hotkeyAppCommitData( QSessionManager& ) ), Qt::DirectConnection ); + + connect( this, SIGNAL( saveStateRequest( QSessionManager& ) ), + this, SLOT( hotkeyAppSaveState( QSessionManager& ) ), Qt::DirectConnection ); + #if defined( Q_OS_WIN ) && IS_QT_5 installNativeEventFilter( this ); #endif @@ -28,11 +34,17 @@ QHotkeyApplication::QHotkeyApplication( int & argc, char ** argv ): QHotkeyApplication::QHotkeyApplication( QString const & id, int & argc, char ** argv ): - QtSingleApplication( id, argc, argv ) + QIntermediateApplication( id, argc, argv ) #ifdef Q_OS_WIN32 , mainWindow( 0 ) #endif { + connect( this, SIGNAL( commitDataRequest( QSessionManager& ) ), + this, SLOT( hotkeyAppCommitData( QSessionManager& ) ), Qt::DirectConnection ); + + connect( this, SIGNAL( saveStateRequest( QSessionManager& ) ), + this, SLOT( hotkeyAppSaveState( QSessionManager& ) ), Qt::DirectConnection ); + #if defined( Q_OS_WIN ) && IS_QT_5 installNativeEventFilter( this ); #endif @@ -48,10 +60,15 @@ void QHotkeyApplication::removeDataCommiter( DataCommitter & d ) dataCommitters.removeAll( &d ); } -void QHotkeyApplication::commitData( QSessionManager & s ) +void QHotkeyApplication::hotkeyAppCommitData( QSessionManager & mgr ) { for( int x = 0; x < dataCommitters.size(); ++x ) - dataCommitters[ x ]->commitData( s ); + dataCommitters[ x ]->commitData( mgr ); +} + +void QHotkeyApplication::hotkeyAppSaveState(QSessionManager & mgr ) +{ + mgr.setRestartHint( QSessionManager::RestartNever ); } void QHotkeyApplication::registerWrapper(HotkeyWrapper *wrapper) diff --git a/hotkeywrapper.hh b/hotkeywrapper.hh index a27873ee..a7967d0d 100644 --- a/hotkeywrapper.hh +++ b/hotkeywrapper.hh @@ -192,11 +192,28 @@ public: {} }; -class QHotkeyApplication : public QtSingleApplication +// Intermediate class to avoid misunderstanding of #ifdef's +// by Qt meta-object compiler + +class QIntermediateApplication : public QtSingleApplication #if defined( Q_OS_WIN ) && IS_QT_5 - , public QAbstractNativeEventFilter + , public QAbstractNativeEventFilter #endif { +public: + QIntermediateApplication( int & argc, char ** argv ) : + QtSingleApplication( argc, argv ) + {} + + QIntermediateApplication( QString const & id, int & argc, char ** argv ) : + QtSingleApplication( id, argc, argv ) + {} +}; + +class QHotkeyApplication : public QIntermediateApplication +{ + Q_OBJECT + friend class HotkeyWrapper; QList< DataCommitter * > dataCommitters; @@ -208,8 +225,11 @@ public: void addDataCommiter( DataCommitter & ); void removeDataCommiter( DataCommitter & ); +private slots: /// This calls all data committers. - virtual void commitData( QSessionManager & ); + void hotkeyAppCommitData( QSessionManager & ); + + void hotkeyAppSaveState( QSessionManager & ); protected: void registerWrapper(HotkeyWrapper *wrapper); diff --git a/mainwindow.cc b/mainwindow.cc index 2e625ff3..fd9d4c02 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -89,7 +89,6 @@ class InitSSLRunnable : public QRunnable #endif MainWindow::MainWindow( Config::Class & cfg_ ): - commitDataCompleted( false ), trayIcon( 0 ), groupLabel( &searchPaneTitleBar ), foundInDictsLabel( &dictsPaneTitleBar ), @@ -368,7 +367,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): trayIconMenu.addAction( enableScanPopup ); trayIconMenu.addSeparator(); connect( trayIconMenu.addAction( tr( "&Quit" ) ), SIGNAL( triggered() ), - qApp, SLOT( quit() ) ); + this, SLOT( quitApp() ) ); addGlobalAction( &escAction, SLOT( handleEsc() ) ); escAction.setShortcut( QKeySequence( "Esc" ) ); @@ -642,7 +641,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): #endif connect( ui.quit, SIGNAL( triggered() ), - qApp, SLOT( quit() ) ); + this, SLOT( quitApp() ) ); connect( ui.dictionaries, SIGNAL( triggered() ), this, SLOT( editDictionaries() ) ); @@ -997,8 +996,6 @@ MainWindow::~MainWindow() #ifndef NO_EPWING_SUPPORT Epwing::finalize(); #endif - - commitData(); } void MainWindow::addGlobalAction( QAction * action, const char * slot ) @@ -1025,15 +1022,13 @@ void MainWindow::addGlobalActionsToDialog( QDialog * dialog ) void MainWindow::commitData( QSessionManager & ) { - commitData(); + commitData( true ); } -void MainWindow::commitData() +void MainWindow::commitData( bool init_popup ) { - if ( !commitDataCompleted ) + try { - commitDataCompleted = true; - // Save MainWindow state and geometry cfg.mainWindowState = saveState( 1 ); cfg.mainWindowGeometry = saveGeometry(); @@ -1052,9 +1047,19 @@ void MainWindow::commitData() gdWarning( "Configuration saving failed, error: %s\n", e.what() ); } + // Save history history.save(); + // Save favorites ui.favoritesPaneWidget->saveData(); + + // Reinit popup window if necessary (when called from session manager) + if( init_popup ) + makeScanPopup(); + } + catch( std::exception & e ) + { + gdWarning( "Commit data failed, error: %s\n", e.what() ); } } @@ -1176,10 +1181,16 @@ void MainWindow::closeEvent( QCloseEvent * ev ) else { ev->accept(); - qApp->quit(); + quitApp(); } } +void MainWindow::quitApp() +{ + commitData( false ); + qApp->quit(); +} + void MainWindow::applyProxySettings() { if( cfg.preferences.proxyServer.enabled && cfg.preferences.proxyServer.useSystemProxy ) diff --git a/mainwindow.hh b/mainwindow.hh index 7543291e..d5fbdea0 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -86,13 +86,13 @@ public slots: void headwordReceived( QString const &, QString const & ); void setExpandMode( bool expand ); void headwordFromFavorites( QString const &, QString const & ); + void quitApp(); private: void addGlobalAction( QAction * action, const char * slot ); void addGlobalActionsToDialog( QDialog * dialog ); - void commitData(); - bool commitDataCompleted; + void commitData( bool init_popup ); QSystemTrayIcon * trayIcon;