Refactor quit/end-of-session handling

This commit is contained in:
Abs62 2018-03-27 17:46:03 +03:00
parent 5552c18a39
commit 2ff7e4e6d0
4 changed files with 68 additions and 20 deletions

View file

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

View file

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

View file

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

View file

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