]> git.lyx.org Git - lyx.git/blobdiff - development/lyxserver/server_monitor.cpp
Win installer: security issues in ImageMagick require new installer
[lyx.git] / development / lyxserver / server_monitor.cpp
index 83d6303586925666210f1ff5fb32cdf4ee56992b..21eb4194fefbad315e5effffdca22967e96b5953 100644 (file)
  * 3) Compile using the following command:
  *    g++ server_monitor.cpp -o monitor -I. `pkg-config --cflags --libs QtGui`
  *
+ * Alternatively, you can create a Makefile with qmake and then build
+ * the executable by running make (or nmake, if you use msvc):
+ *    qmake
+ *    make
+ *
  * Usage:
  * 1) Set the LyXserver pipe path in the LyX preferences (on *nix you can use
  *    any path, for example ~/.lyx/lyxpipe, whereas on Windows the path has
  *    push the button labeled "Open pipes" and then try issuing some commands.
  */
 
-#include "server_monitor.h"
-
 #include <QApplication>
 #include <QtGui>
 #include <QtDebug>
 
-class ReadPipe : public QThread {
-public:
-       ///
-       ReadPipe(LyXServerMonitor * monitor) : lyxmonitor(monitor) {}
-       ///
-       void run() { lyxmonitor->readPipe(); }
-
-private:
-       ///
-       LyXServerMonitor * lyxmonitor;
-};
-
-
-class DeleteThread : public QEvent {
-public:
-       ///
-       DeleteThread(ReadPipe * thread)
-       : QEvent(QEvent::User), pipethread(thread)
-       {}
-       ///
-       ReadPipe * pipeThread() const { return pipethread; }
-
-private:
-       ///
-       ReadPipe * pipethread;
-};
-
+#include "server_monitor.h"
 
 LyXServerMonitor::LyXServerMonitor()
        : pipein(-1), pipeout(-1), thread_exit(false), lyx_listen(false)
@@ -185,8 +162,7 @@ void LyXServerMonitor::readPipe()
                                if (fromLyX.contains("bye")) {
                                        qWarning() << "monitor: LyX has closed "
                                                      "connection!";
-                                       infoLB->clear();
-                                       notifyLB->setText(fromLyX);
+                                       pipethread->emitNotice(fromLyX);
                                        notified = true;
                                        break;
                                }
@@ -197,13 +173,10 @@ void LyXServerMonitor::readPipe()
                                        submitCommandPB->setDisabled(false);
                                }
                        }
-                       if (fromLyX[0] == QLatin1Char('I')) {
-                               infoLB->setText(fromLyX);
-                               notifyLB->clear();
-                       } else {
-                               infoLB->clear();
-                               notifyLB->setText(fromLyX);
-                       }
+                       if (fromLyX[0] == QLatin1Char('I'))
+                               pipethread->emitInfo(fromLyX);
+                       else
+                               pipethread->emitNotice(fromLyX);
 #ifdef _WIN32
                        // On Windows, we have to close and reopen
                        // the pipe after each use.
@@ -213,9 +186,8 @@ void LyXServerMonitor::readPipe()
                                O_RDONLY);
                        if (pipeout < 0) {
                                perror("monitor");
-                               infoLB->clear();
-                               notifyLB->setText("An error occurred, "
-                                                 "closing pipes");
+                               pipethread->emitNotice("An error occurred, "
+                                                       "closing pipes");
                                notified = true;
                                break;
                        }
@@ -236,8 +208,7 @@ void LyXServerMonitor::readPipe()
                        }
 #endif
                        perror("monitor");
-                       infoLB->clear();
-                       notifyLB->setText("An error occurred, closing pipes");
+                       pipethread->emitNotice("An error occurred, closing pipes");
                        notified = true;
                        break;
                } else
@@ -247,35 +218,46 @@ void LyXServerMonitor::readPipe()
        if (!notified) {
                if (thread_exit) {
                        qWarning() << "monitor: Closing pipes";
-                       infoLB->clear();
-                       notifyLB->setText("Closing pipes");
+                       pipethread->emitNotice("Closing pipes");
                } else {
                        qWarning() << "monitor: LyX has closed connection!";
-                       infoLB->clear();
-                       notifyLB->setText("LyX has closed connection!");
+                       pipethread->emitNotice("LyX has closed connection!");
                }
        }
-       DeleteThread * event = new DeleteThread(pipethread);
-       QCoreApplication::postEvent(this, static_cast<QEvent *>(event));
+       QEvent * event = new QEvent(QEvent::User);
+       QCoreApplication::postEvent(this, event);
        lyx_listen = false;
-       closePipes();
+       if (!thread_exit)
+               pipethread->emitClosing();
 }
 
 
 bool LyXServerMonitor::event(QEvent * e)
 {
        if (e->type() == QEvent::User) {
-               ReadPipe * pipeThread =
-                       static_cast<DeleteThread *>(e)->pipeThread();
-               pipeThread->wait();
+               pipethread->wait();
                thread_exit = false;
-               delete pipeThread;
+               delete pipethread;
                return true;
        }
        return QDialog::event(e);
 }
 
 
+void LyXServerMonitor::showInfo(QString const & msg)
+{
+       infoLB->setText(msg);
+       notifyLB->clear();
+}
+
+
+void LyXServerMonitor::showNotice(QString const & msg)
+{
+       infoLB->clear();
+       notifyLB->setText(msg);
+}
+
+
 void LyXServerMonitor::openPipes()
 {
        if (pipein == -1) {
@@ -302,6 +284,12 @@ void LyXServerMonitor::openPipes()
                        closePipes();
                        return;
                }
+               connect(pipethread, SIGNAL(info(QString const &)),
+                       this, SLOT(showInfo(QString const &)));
+               connect(pipethread, SIGNAL(notice(QString const &)),
+                       this, SLOT(showNotice(QString const &)));
+               connect(pipethread, SIGNAL(closing()),
+                       this, SLOT(closePipes()));
                openPipesPB->setDisabled(true);
                closePipesPB->setDisabled(false);
                // greet LyX