Browse Source

- Passing parameters between qBT instances in a better way (socket)

adaptive-webui-19844
Christophe Dumez 18 years ago
parent
commit
92277992d3
  1. 76
      src/GUI.cpp
  2. 6
      src/GUI.h
  3. 73
      src/main.cpp

76
src/GUI.cpp

@ -181,11 +181,11 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent){ @@ -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(){ @@ -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(){ @@ -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){ @@ -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();
}

6
src/GUI.h

@ -96,7 +96,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{ @@ -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{ @@ -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;

73
src/main.cpp

@ -53,66 +53,31 @@ int main(int argc, char *argv[]){ @@ -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;i<argc;++i){
// out << QString(argv[i]);
// std::cout << QString(argv[i]).toStdString() << '\n';
// }
// std::cout << "writting: " << block.data() << '\n';
// out.device()->seek(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<argc;++i){
params << QString(argv[i]);
std::cout << QString(argv[i]).toStdString() << '\n';
}
QByteArray block = params.join("\n").toUtf8();
std::cout << "writting: " << block.data() << '\n';
std::cout << "size: " << block.size() << '\n';
uint val = tcpSocket.write(block);
if(tcpSocket.waitForBytesWritten(5000)){
std::cout << "written(" <<val<<"): " << block.data() << '\n';
}else{
std::cerr << "Writing to the socket timed out\n";
}
tcpSocket.disconnectFromHost();
std::cout << "disconnected\n";
}
paramsFile.remove();
// Write params to the file
if (!paramsFile.open(QIODevice::WriteOnly | QIODevice::Text)){
std:: cerr << "could not pass parameters\n";
return 1;
}
qDebug("Passing parameters");
for(int i=1;i<argc;++i){
paramsFile.write(QByteArray(argv[i]).append("\n"));
}
paramsFile.close();
tcpSocket.disconnectFromHost();
tcpSocket.close();
qDebug("exiting");
return 0;
}
QApplication app(argc, argv);

Loading…
Cancel
Save