diff --git a/src/GUI.cpp b/src/GUI.cpp index 46ce9d650..5df88ad19 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -181,11 +181,11 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent){ connect(myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(toggleVisibility(QSystemTrayIcon::ActivationReason))); myTrayIcon->show(); // Use a tcp server to allow only one instance of qBittorrent - tcpServer = new QTcpServer(this); - if (!tcpServer->listen(QHostAddress::LocalHost, 1666)) { + if (!tcpServer.listen(QHostAddress::LocalHost, 1666)) { std::cerr << "Couldn't create socket, single instance mode won't work...\n"; } - connect(tcpServer, SIGNAL(newConnection()), this, SLOT(AnotherInstanceConnected())); + connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection())); +// connect(tcpServer, SIGNAL(bytesWritten(qint64)), this, SLOT(readParamsOnSocket(qint64))); // Start connection checking timer checkConnect = new QTimer(this); connect(checkConnect, SIGNAL(timeout()), this, SLOT(checkConnectionStatus())); @@ -255,7 +255,6 @@ GUI::~GUI(){ delete refresher; delete myTrayIcon; delete myTrayIconMenu; - delete tcpServer; delete DLDelegate; delete DLListModel; delete SearchListModel; @@ -305,41 +304,20 @@ void GUI::balloonClicked(){ activateWindow(); } } -// Buggy with Qt 4.1.2 : should try with another version -// void GUI::readParamsOnSocket(){ -// QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); -// if(clientConnection != 0){ -// connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater())); -// std::cout << "reading...\n"; -// while (clientConnection->bytesAvailable() < (int)sizeof(quint16)) { -// std::cout << "reading size chunk\n"; -// if (!clientConnection->waitForReadyRead(5000)) { -// std::cout << clientConnection->errorString().toStdString() << '\n'; -// return; -// } -// } -// quint16 blockSize; -// QDataStream in(clientConnection); -// in.setVersion(QDataStream::Qt_4_0); -// in >> blockSize; -// while (clientConnection->bytesAvailable() < blockSize) { -// if (!clientConnection->waitForReadyRead(5000)) { -// std::cout << clientConnection->errorString().toStdString() << '\n'; -// return; -// } -// } -// QString params; -// in >> params; -// std::cout << params.toStdString() << '\n'; -// clientConnection->disconnectFromHost(); -// } -// std::cout << "end reading\n"; -// } -void GUI::AnotherInstanceConnected(){ - clientConnection = tcpServer->nextPendingConnection(); +void GUI::acceptConnection(){ + clientConnection = tcpServer.nextPendingConnection(); + connect(clientConnection, SIGNAL(disconnected()), this, SLOT(readParamsOnSocket())); + qDebug("accepted connection from another instance"); +} + +void GUI::readParamsOnSocket(){ if(clientConnection != 0){ - connect(clientConnection, SIGNAL(disconnected()), this, SLOT(readParamsInFile())); + QByteArray params = clientConnection->readAll(); + if(!params.isEmpty()){ + processParams(QString(params.data()).split("\n")); + qDebug("Received parameters from another instance"); + } } } @@ -435,30 +413,6 @@ void GUI::selectGivenRow(const QModelIndex& index){ } } -// Called when the other instance is disconnected -// Means the file is written -void GUI::readParamsInFile(){ - QFile paramsFile(QDir::tempPath()+QDir::separator()+"qBT-params.txt"); - if (!paramsFile.open(QIODevice::ReadOnly | QIODevice::Text)){ - paramsFile.remove(); - return; - } - QStringList params; - while (!paramsFile.atEnd()) { - QByteArray line = paramsFile.readLine(); - if(line.at(line.size()-1) == '\n'){ - line.truncate(line.size()-1); - } - params << line; - } - if(params.size()){ - qDebug("Received parameters from another instance"); - processParams(params); - } - paramsFile.close(); - paramsFile.remove(); -} - void GUI::clearLog(){ infoBar->clear(); } diff --git a/src/GUI.h b/src/GUI.h index 6f030acd9..accc489ef 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -96,7 +96,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{ bool no_search_results; QByteArray search_result_line_truncated; unsigned long nb_search_results; - QTcpServer *tcpServer; + QTcpServer tcpServer; QTcpSocket *clientConnection; protected slots: @@ -109,8 +109,8 @@ class GUI : public QMainWindow, private Ui::MainWindow{ void updateDlList(); void showCreateWindow(); void clearLog(); - void AnotherInstanceConnected(); - void readParamsInFile(); + void readParamsOnSocket(); + void acceptConnection(); void saveCheckedSearchEngines(int) const; void saveColWidthDLList() const; void saveColWidthSearchList() const; diff --git a/src/main.cpp b/src/main.cpp index 034e2ad6b..3e98572ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,66 +53,31 @@ int main(int argc, char *argv[]){ if(putenv("QBITTORRENT="VERSION)){ std::cerr << "Couldn't set environment variable...\n"; } - // Buggy with Qt 4.1.2 : should try with another version //Check if there is another instance running -// QTcpSocket *tcpSocket= new QTcpSocket(); -// tcpSocket->connectToHost(QHostAddress::LocalHost, 1666); -// if (tcpSocket->waitForConnected(1000)){ -// std::cout << "Another qBittorrent instance is already running...\n"; -// // Send parameters -// if(argc > 1){ -// QByteArray block; -// QDataStream out(&block, QIODevice::WriteOnly); -// out.setVersion(QDataStream::Qt_4_0); -// out << (quint16)0; -// std::cout << "deb size: " << block.data() << '\n'; -// for(int i=1;iseek(0); -// std::cout << "size: " << block.size() << '\n'; -// out << (quint16)(block.size() - sizeof(quint16)); -// tcpSocket->write(block); -// std::cout << "written: " << block.data() << '\n'; -// tcpSocket->waitForConnected(5000); -// tcpSocket->disconnectFromHost(); -// std::cout << "disconnected\n"; -// sleep(5); -// } -// tcpSocket->close(); -// delete tcpSocket; -// return 0; -// } - // Check if another instance is running QTcpSocket tcpSocket; tcpSocket.connectToHost(QHostAddress::LocalHost, 1666); if (tcpSocket.waitForConnected(1000)){ - qDebug("Another qBittorrent instance is already running..."); - // Write params to a temporary file - QFile paramsFile(QDir::tempPath()+QDir::separator()+"qBT-params.txt"); - int count = 0; - while(paramsFile.exists() && count <10){ - // If file exists, then the other instance is already reading params - // We wait... - SleeperThread::msleep(1000); - ++count; + std::cout << "Another qBittorrent instance is already running...\n"; + // Send parameters + if(argc > 1){ + QStringList params; + for(int i=1;i