diff --git a/src/GUI.cpp b/src/GUI.cpp index d28522a24..97ac21100 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -71,6 +71,7 @@ void qt_mac_set_dock_menu(QMenu *menu); #endif #include "lineedit.h" +#include "sessionapplication.h" using namespace libtorrent; @@ -88,6 +89,8 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for ui_locked = Preferences::isUILocked(); setWindowTitle(tr("qBittorrent %1", "e.g: qBittorrent v0.x").arg(QString::fromUtf8(VERSION))); displaySpeedInTitle = Preferences::speedInTitleBar(); + // Clean exit on log out + connect(static_cast(qApp), SIGNAL(sessionIsShuttingDown()), this, SLOT(deleteBTSession())); // Setting icons this->setWindowIcon(QIcon(QString::fromUtf8(":/Icons/skin/qbittorrent32.png"))); actionOpen->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/open.png"))); @@ -248,6 +251,16 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for #endif } +void GUI::deleteBTSession() { + guiUpdater->stop(); + status_bar->stopTimer(); + if(BTSession) { + delete BTSession; + BTSession = 0; + } + QTimer::singleShot(0, this, SLOT(close())); +} + // Destructor GUI::~GUI() { qDebug("GUI destruction"); @@ -258,7 +271,9 @@ GUI::~GUI() { #endif // Async deletion of Bittorrent session as early as possible // in order to speed up exit - session_proxy sp = BTSession->asyncDeletion(); + session_proxy sp; + if(BTSession) + sp = BTSession->asyncDeletion(); // Some saving properties->saveSettings(); disconnect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int))); @@ -663,7 +678,7 @@ void GUI::closeEvent(QCloseEvent *e) { e->accept(); return; } - if(settings.value(QString::fromUtf8("Preferences/General/ExitConfirm"), true).toBool() && BTSession->hasActiveTorrents()) { + if(settings.value(QString::fromUtf8("Preferences/General/ExitConfirm"), true).toBool() && BTSession && BTSession->hasActiveTorrents()) { if(e->spontaneous() || force_exit) { if(!isVisible()) show(); diff --git a/src/GUI.h b/src/GUI.h index f177957ba..35920b63c 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -80,6 +80,7 @@ public slots: void downloadFromURLList(const QStringList& urls); void updateAltSpeedsBtn(bool alternative); void updateNbTorrents(unsigned int nb_downloading, unsigned int nb_seeding, unsigned int nb_active, unsigned int nb_inactive, unsigned int nb_paused); + void deleteBTSession(); protected slots: // GUI related slots diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index bf57c9d79..1c51d1caa 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -165,6 +165,7 @@ Bittorrent::~Bittorrent() { #endif saveSessionState(); saveFastResumeData(); + qDebug("Deleting the session"); // Delete session session_proxy sp = s->abort(); delete s; @@ -1541,6 +1542,7 @@ void Bittorrent::saveTempFastResumeData() { // Only save fast resume data for unfinished and unpaused torrents (Optimization) // Called periodically and on exit void Bittorrent::saveFastResumeData() { + qDebug("Saving fast resume data..."); // Stop listening for alerts resumeDataTimer.stop(); timerAlerts->stop(); diff --git a/src/main.cpp b/src/main.cpp index f7d6d2e9a..ae94ce8f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,11 +38,7 @@ #include #include #include -#ifdef Q_WS_MAC -#include "qmacapplication.h" -#else -#include "qtsingleapplication.h" -#endif +#include "sessionapplication.h" #include "GUI.h" #include "ico.h" #else @@ -160,15 +156,11 @@ void useStyle(QString style){ int main(int argc, char *argv[]){ // Create Application QString uid = misc::getUserIDString(); - #ifdef DISABLE_GUI +#ifdef DISABLE_GUI QtSingleCoreApplication app("qBittorrent-"+uid, argc, argv); - #else - #ifndef Q_WS_MAC - QtSingleApplication app("qBittorrent-"+uid, argc, argv); - #else - QMacApplication app("qBittorrent-"+uid, argc, argv); - #endif - #endif +#else + SessionApplication app("qBittorrent-"+uid, argc, argv); +#endif // Check if qBittorrent is already running for this user if(app.isRunning()) { diff --git a/src/sessionapplication.h b/src/sessionapplication.h new file mode 100644 index 000000000..8cb88a729 --- /dev/null +++ b/src/sessionapplication.h @@ -0,0 +1,39 @@ +#ifndef SESSIONAPPLICATION_H +#define SESSIONAPPLICATION_H + +#ifdef Q_WS_MAC +#include "qmacapplication.h" +#else +#include "qtsingleapplication.h" +#endif + +#include + +class SessionApplication : +#ifdef Q_WS_MAC + public QMacApplication +#else + public QtSingleApplication +#endif +{ + Q_OBJECT + +public: + SessionApplication(const QString &id, int &argc, char **argv) : +#ifdef Q_WS_MAC + QMacApplication(id, argc, argv) +#else + QtSingleApplication(id, argc, argv) +#endif + {} + + void commitData(QSessionManager & manager) { + Q_UNUSED(manager); + emit sessionIsShuttingDown(); + } + + signals: + void sessionIsShuttingDown(); +}; + +#endif // SESSIONAPPLICATION_H diff --git a/src/src.pro b/src/src.pro index 8a66f4646..e32a1db37 100644 --- a/src/src.pro +++ b/src/src.pro @@ -342,7 +342,8 @@ contains(DEFINES, DISABLE_GUI) { advancedsettings.h \ cookiesdlg.h \ rsssettings.h \ - hidabletabwidget.h + hidabletabwidget.h \ + sessionapplication.h macx { HEADERS += qmacapplication.h diff --git a/src/statusbar.h b/src/statusbar.h index d84323324..64269311b 100644 --- a/src/statusbar.h +++ b/src/statusbar.h @@ -170,6 +170,10 @@ public slots: bar->insertWidget(1, new QLabel(tr("qBittorrent needs to be restarted"))); } + void stopTimer() { + refreshTimer->stop(); + } + void refreshStatusBar() { // Update connection status session_status sessionStatus = BTSession->getSessionStatus();