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. 69
      src/main.cpp

76
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))); connect(myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(toggleVisibility(QSystemTrayIcon::ActivationReason)));
myTrayIcon->show(); myTrayIcon->show();
// Use a tcp server to allow only one instance of qBittorrent // 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"; 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 // Start connection checking timer
checkConnect = new QTimer(this); checkConnect = new QTimer(this);
connect(checkConnect, SIGNAL(timeout()), this, SLOT(checkConnectionStatus())); connect(checkConnect, SIGNAL(timeout()), this, SLOT(checkConnectionStatus()));
@ -255,7 +255,6 @@ GUI::~GUI(){
delete refresher; delete refresher;
delete myTrayIcon; delete myTrayIcon;
delete myTrayIconMenu; delete myTrayIconMenu;
delete tcpServer;
delete DLDelegate; delete DLDelegate;
delete DLListModel; delete DLListModel;
delete SearchListModel; delete SearchListModel;
@ -305,41 +304,20 @@ void GUI::balloonClicked(){
activateWindow(); 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(){ void GUI::acceptConnection(){
clientConnection = tcpServer->nextPendingConnection(); clientConnection = tcpServer.nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(readParamsOnSocket()));
qDebug("accepted connection from another instance");
}
void GUI::readParamsOnSocket(){
if(clientConnection != 0){ 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(){ void GUI::clearLog(){
infoBar->clear(); infoBar->clear();
} }

6
src/GUI.h

@ -96,7 +96,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{
bool no_search_results; bool no_search_results;
QByteArray search_result_line_truncated; QByteArray search_result_line_truncated;
unsigned long nb_search_results; unsigned long nb_search_results;
QTcpServer *tcpServer; QTcpServer tcpServer;
QTcpSocket *clientConnection; QTcpSocket *clientConnection;
protected slots: protected slots:
@ -109,8 +109,8 @@ class GUI : public QMainWindow, private Ui::MainWindow{
void updateDlList(); void updateDlList();
void showCreateWindow(); void showCreateWindow();
void clearLog(); void clearLog();
void AnotherInstanceConnected(); void readParamsOnSocket();
void readParamsInFile(); void acceptConnection();
void saveCheckedSearchEngines(int) const; void saveCheckedSearchEngines(int) const;
void saveColWidthDLList() const; void saveColWidthDLList() const;
void saveColWidthSearchList() const; void saveColWidthSearchList() const;

69
src/main.cpp

@ -53,66 +53,31 @@ int main(int argc, char *argv[]){
if(putenv("QBITTORRENT="VERSION)){ if(putenv("QBITTORRENT="VERSION)){
std::cerr << "Couldn't set environment variable...\n"; 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 //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; QTcpSocket tcpSocket;
tcpSocket.connectToHost(QHostAddress::LocalHost, 1666); tcpSocket.connectToHost(QHostAddress::LocalHost, 1666);
if (tcpSocket.waitForConnected(1000)){ if (tcpSocket.waitForConnected(1000)){
qDebug("Another qBittorrent instance is already running..."); std::cout << "Another qBittorrent instance is already running...\n";
// Write params to a temporary file // Send parameters
QFile paramsFile(QDir::tempPath()+QDir::separator()+"qBT-params.txt"); if(argc > 1){
int count = 0; QStringList params;
while(paramsFile.exists() && count <10){
// If file exists, then the other instance is already reading params
// We wait...
SleeperThread::msleep(1000);
++count;
}
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){ for(int i=1;i<argc;++i){
paramsFile.write(QByteArray(argv[i]).append("\n")); 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";
} }
paramsFile.close();
tcpSocket.disconnectFromHost(); tcpSocket.disconnectFromHost();
std::cout << "disconnected\n";
}
tcpSocket.close(); tcpSocket.close();
qDebug("exiting");
return 0; return 0;
} }
QApplication app(argc, argv); QApplication app(argc, argv);

Loading…
Cancel
Save