diff --git a/src/addnewtorrentdialog.cpp b/src/addnewtorrentdialog.cpp index 883e31032..8df66315d 100644 --- a/src/addnewtorrentdialog.cpp +++ b/src/addnewtorrentdialog.cpp @@ -140,6 +140,13 @@ void AddNewTorrentDialog::showMagnet(const QString& link) dlg.exec(); } +void AddNewTorrentDialog::showEvent(QShowEvent *event) { + QDialog::showEvent(event); + activateWindow(); + raise(); +} + + void AddNewTorrentDialog::showAdvancedSettings(bool show) { if (show) { diff --git a/src/addnewtorrentdialog.h b/src/addnewtorrentdialog.h index 9ce3d9791..dec472805 100644 --- a/src/addnewtorrentdialog.h +++ b/src/addnewtorrentdialog.h @@ -56,6 +56,9 @@ public: static void showTorrent(const QString& torrent_path, const QString& from_url = QString()); static void showMagnet(const QString& torrent_link); +protected: + void showEvent(QShowEvent *event); + private slots: void showAdvancedSettings(bool show); void displayContentTreeMenu(const QPoint&); diff --git a/src/main.cpp b/src/main.cpp index 637dd43bf..809e3a084 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,6 +75,10 @@ Q_IMPORT_PLUGIN(qico) #include "misc.h" #include "preferences.h" +#if defined(Q_OS_WIN) && !defined(QBT_HAS_GETCURRENTPID) +#error You seem to have updated QtSingleApplication without porting our custom QtSingleApplication::getRunningPid() function. Please see previous version to understate how it works. +#endif + class UsageDisplay: public QObject { Q_OBJECT @@ -206,6 +210,13 @@ int main(int argc, char *argv[]) { if (app.isRunning()) { qDebug("qBittorrent is already running for this user."); // Read torrents given on command line +#ifdef Q_OS_WIN + DWORD pid = (DWORD)app.getRunningPid(); + if (pid > 0) { + BOOL b = AllowSetForegroundWindow(pid); + qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE"); + } +#endif QStringList torrentCmdLine = app.arguments(); //Pass program parameters if any QString message; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 241ab69b0..7ad6a0b49 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -263,14 +263,8 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine) : QMa // Load Window state and sizes readSettings(); - if (!ui_locked) { - if (pref.startMinimized() && systrayIcon) { - show(); - minimizeWindow(); - // XXX: Using showMinimized() makes it impossible to restore - // the window if "Minimize to systray" is enabled. - //showMinimized(); - } else { + if (systrayIcon) { + if (!(pref.startMinimized() || ui_locked)) { show(); activateWindow(); raise(); @@ -323,10 +317,14 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine) : QMa #endif // Make sure the Window is visible if we don't have a tray icon - if (!systrayIcon && isHidden()) { - show(); - activateWindow(); - raise(); + if (!systrayIcon) { + if (pref.startMinimized()) { + showMinimized(); + } else { + show(); + activateWindow(); + raise(); + } } } diff --git a/src/qtsingleapp/qtlocalpeer.cpp b/src/qtsingleapp/qtlocalpeer.cpp index 72f8cd6db..fe770c6e1 100644 --- a/src/qtsingleapp/qtlocalpeer.cpp +++ b/src/qtsingleapp/qtlocalpeer.cpp @@ -201,8 +201,55 @@ void QtLocalPeer::receiveConnection() return; } QString message(QString::fromUtf8(uMsg)); +#ifdef Q_OS_WIN + if (message == "qbt://pid") { + qint64 pid = GetCurrentProcessId(); + socket->write((const char *)&pid, sizeof pid); + } else { + socket->write(ack, qstrlen(ack)); + } +#else socket->write(ack, qstrlen(ack)); +#endif socket->waitForBytesWritten(1000); delete socket; +#ifdef Q_OS_WIN + if (message == "qbt://pid") + return; +#endif emit messageReceived(message); //### (might take a long time to return) } + +#ifdef Q_OS_WIN +qint64 QtLocalPeer::getRunningPid() { + if (!isClient()) + return 0; + + QLocalSocket socket; + bool connOk = false; + for (int i = 0; i < 2; i++) { + // Try twice, in case the other instance is just starting up + socket.connectToServer(socketName); + connOk = socket.waitForConnected(5000/2); + if (connOk || i) + break; + Sleep(250); + } + if (!connOk) return -1; + + const char* msg = "qbt://pid"; + QDataStream ds(&socket); + ds.writeBytes(msg, qstrlen(msg)); + bool res = socket.waitForBytesWritten(5000) && socket.waitForReadyRead(5000); + if (!res) return -1; + + DWORD pid; + qint64 pid_size = sizeof pid; + while (socket.bytesAvailable() < pid_size) + socket.waitForReadyRead(); + if (socket.read((char *)&pid, pid_size) < pid_size) + return -1; + + return pid; +} +#endif diff --git a/src/qtsingleapp/qtlocalpeer.h b/src/qtsingleapp/qtlocalpeer.h index b81dbd11a..2fa091f3f 100644 --- a/src/qtsingleapp/qtlocalpeer.h +++ b/src/qtsingleapp/qtlocalpeer.h @@ -64,6 +64,9 @@ public: bool sendMessage(const QString &message, int timeout); QString applicationId() const { return id; } +#ifdef Q_OS_WIN + qint64 getRunningPid(); +#endif Q_SIGNALS: void messageReceived(const QString &message); diff --git a/src/qtsingleapp/qtsingleapplication.cpp b/src/qtsingleapp/qtsingleapplication.cpp index a3a1fd7fd..c3b894348 100644 --- a/src/qtsingleapp/qtsingleapplication.cpp +++ b/src/qtsingleapp/qtsingleapplication.cpp @@ -349,3 +349,9 @@ void QtSingleApplication::activateWindow() \obsolete */ + +#ifdef Q_OS_WIN +qint64 QtSingleApplication::getRunningPid() { + return peer->getRunningPid(); +} +#endif diff --git a/src/qtsingleapp/qtsingleapplication.h b/src/qtsingleapp/qtsingleapplication.h index 5df916561..0ffd8f6ee 100644 --- a/src/qtsingleapp/qtsingleapplication.h +++ b/src/qtsingleapp/qtsingleapplication.h @@ -88,6 +88,10 @@ public: // Obsolete: void initialize(bool dummy = true) { isRunning(); Q_UNUSED(dummy) } +#ifdef Q_OS_WIN +#define QBT_HAS_GETCURRENTPID + qint64 getRunningPid(); +#endif public Q_SLOTS: bool sendMessage(const QString &message, int timeout = 5000);