mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-03-09 20:01:08 +00:00
Some improvements of requesting pid of running instance code.
This commit is contained in:
parent
b78ea79d30
commit
dbf8675de3
@ -75,6 +75,10 @@ Q_IMPORT_PLUGIN(qico)
|
|||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "preferences.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 {
|
class UsageDisplay: public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@ -207,7 +211,7 @@ int main(int argc, char *argv[]) {
|
|||||||
qDebug("qBittorrent is already running for this user.");
|
qDebug("qBittorrent is already running for this user.");
|
||||||
// Read torrents given on command line
|
// Read torrents given on command line
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
DWORD pid = app.getRunningPid();
|
DWORD pid = (DWORD)app.getRunningPid();
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
BOOL b = AllowSetForegroundWindow(pid);
|
BOOL b = AllowSetForegroundWindow(pid);
|
||||||
qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE");
|
qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE");
|
||||||
|
@ -202,10 +202,12 @@ void QtLocalPeer::receiveConnection()
|
|||||||
}
|
}
|
||||||
QString message(QString::fromUtf8(uMsg));
|
QString message(QString::fromUtf8(uMsg));
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
if (message == "qbt://pid")
|
if (message == "qbt://pid") {
|
||||||
socket->write(QByteArray::number(qApp->applicationPid()));
|
qint64 pid = GetCurrentProcessId();
|
||||||
else
|
socket->write((const char *)&pid, sizeof pid);
|
||||||
|
} else {
|
||||||
socket->write(ack, qstrlen(ack));
|
socket->write(ack, qstrlen(ack));
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
socket->write(ack, qstrlen(ack));
|
socket->write(ack, qstrlen(ack));
|
||||||
#endif
|
#endif
|
||||||
@ -219,9 +221,9 @@ void QtLocalPeer::receiveConnection()
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
ulong QtLocalPeer::getRunningPid() {
|
qint64 QtLocalPeer::getRunningPid() {
|
||||||
if (!isClient())
|
if (!isClient())
|
||||||
return false;
|
return 0;
|
||||||
|
|
||||||
QLocalSocket socket;
|
QLocalSocket socket;
|
||||||
bool connOk = false;
|
bool connOk = false;
|
||||||
@ -231,32 +233,23 @@ ulong QtLocalPeer::getRunningPid() {
|
|||||||
connOk = socket.waitForConnected(5000/2);
|
connOk = socket.waitForConnected(5000/2);
|
||||||
if (connOk || i)
|
if (connOk || i)
|
||||||
break;
|
break;
|
||||||
Sleep(DWORD(250));
|
Sleep(250);
|
||||||
}
|
}
|
||||||
if (!connOk)
|
if (!connOk) return -1;
|
||||||
return false;
|
|
||||||
QByteArray uMsg("qbt://pid");
|
|
||||||
QDataStream ds(&socket);
|
|
||||||
ds.writeBytes(uMsg.constData(), uMsg.size());
|
|
||||||
bool res = socket.waitForBytesWritten(5000);
|
|
||||||
res &= socket.waitForReadyRead(5000);
|
|
||||||
if (!res)
|
|
||||||
return -1;
|
|
||||||
while (socket.bytesAvailable() < (int)sizeof(quint32))
|
|
||||||
socket.waitForReadyRead();
|
|
||||||
uMsg.clear();
|
|
||||||
quint32 remaining = socket.bytesAvailable();
|
|
||||||
uMsg.resize(remaining);
|
|
||||||
int got = 0;
|
|
||||||
char* uMsgBuf = uMsg.data();
|
|
||||||
do {
|
|
||||||
got = ds.readRawData(uMsgBuf, remaining);
|
|
||||||
remaining -= got;
|
|
||||||
uMsgBuf += got;
|
|
||||||
} while (remaining && got >= 0 && socket.waitForReadyRead(2000));
|
|
||||||
if (got < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return uMsg.toULong();
|
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
|
#endif
|
||||||
|
@ -65,7 +65,7 @@ public:
|
|||||||
QString applicationId() const
|
QString applicationId() const
|
||||||
{ return id; }
|
{ return id; }
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
ulong getRunningPid();
|
qint64 getRunningPid();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
@ -351,7 +351,7 @@ void QtSingleApplication::activateWindow()
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
ulong QtSingleApplication::getRunningPid() {
|
qint64 QtSingleApplication::getRunningPid() {
|
||||||
return peer->getRunningPid();
|
return peer->getRunningPid();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -89,7 +89,8 @@ public:
|
|||||||
void initialize(bool dummy = true)
|
void initialize(bool dummy = true)
|
||||||
{ isRunning(); Q_UNUSED(dummy) }
|
{ isRunning(); Q_UNUSED(dummy) }
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
ulong getRunningPid();
|
#define QBT_HAS_GETCURRENTPID
|
||||||
|
qint64 getRunningPid();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user