/** * @brief SingleApplication::SingleApplication * Constructor. Checks and fires up LocalServer or closes the program * if another instance already exists * @param argc * @param argv */ SingleApplication::SingleApplication(int& argc, char* argv[]) : QApplication(argc, argv) { _shouldContinue = false; // By default this is not the main process socket = new QLocalSocket(); // Attempt to connect to the LocalServer socket->connectToServer("Quazaa"); if(socket->waitForConnected(100)){ socket->write("CMD:showUp"); socket->flush(); QThread::msleep(100); socket->close(); } else { // The attempt was insuccessful, so we continue the program _shouldContinue = true; server = new LocalServer(); server->start(); QObject::connect(server, SIGNAL(showUp()), this, SLOT(slotShowUp())); } }
/** * @brief SingleApplication::SingleApplication * Constructor. Checks and fires up LocalServer or closes the program * if another instance already exists * @param argc * @param argv */ SingleApplication::SingleApplication(QStringList &args) { _shouldContinue = false; // By default this is not the main process socket = new QUdpSocket(); QUdpSocket acceptor; acceptor.bind(QHostAddress::LocalHost, 58488, QUdpSocket::ReuseAddressHint|QUdpSocket::ShareAddress); // Attempt to connect to the LocalServer socket->connectToHost(QHostAddress::LocalHost, 58487); QString isServerRuns; if(socket->waitForConnected(100)) { socket->write(QString("CMD:Is editor running?").toUtf8()); socket->flush(); if(acceptor.waitForReadyRead(100)) { //QByteArray dataGram;//Yes, I'm runs! QByteArray datagram; datagram.resize(acceptor.pendingDatagramSize()); QHostAddress sender; quint16 senderPort; acceptor.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); if(QString::fromUtf8(datagram)=="Yes, I'm runs!") { isServerRuns="Yes!"; } } } if(args.contains("--force-run", Qt::CaseInsensitive)) { isServerRuns.clear(); args.removeAll("--force-run"); } _arguments = args; if(!isServerRuns.isEmpty()) { QString str = QString("CMD:showUp"); QByteArray bytes; for(int i=1; i<_arguments.size(); i++) { str.append(QString("\n%1").arg(_arguments[i])); } bytes = str.toUtf8(); socket->write(bytes); socket->flush(); QThread::msleep(100); socket->close(); } else { // The attempt was insuccessful, so we continue the program _shouldContinue = true; server = new LocalServer(); server->start(); QObject::connect(server, SIGNAL(showUp()), this, SLOT(slotShowUp())); QObject::connect(server, SIGNAL(dataReceived(QString)), this, SLOT(slotOpenFile(QString))); QObject::connect(server, SIGNAL(acceptedCommand(QString)), this, SLOT(slotAcceptedCommand(QString))); QObject::connect(this, SIGNAL(stopServer()), server, SLOT(stopServer())); } }