* 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>
+#if QT_VERSION >= 0x050000
+#include <QtWidgets>
+#endif
-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)
connect(submitCommandPB, SIGNAL(clicked()), this, SLOT(submitCommand()));
connect(donePB, SIGNAL(clicked()), this, SLOT(reject()));
- QVBoxLayout *mainLayout = new QVBoxLayout;
+ QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->addWidget(gridGB);
mainLayout->addWidget(horizontalGB);
setLayout(mainLayout);
void LyXServerMonitor::createGridGroupBox()
{
gridGB = new QGroupBox;
- QGridLayout *layout = new QGridLayout;
+ QGridLayout * layout = new QGridLayout;
labels[0] = new QLabel("Pipe name");
pipeNameLE = new QLineEdit;
void LyXServerMonitor::createCmdsGroupBox()
{
horizontalGB = new QGroupBox;
- QHBoxLayout *layout = new QHBoxLayout;
+ QHBoxLayout * layout = new QHBoxLayout;
openPipesPB = new QPushButton("&Open pipes");
layout->addWidget(openPipesPB);
if (fromLyX.contains("bye")) {
qWarning() << "monitor: LyX has closed "
"connection!";
- infoLB->clear();
- notifyLB->setText(fromLyX);
+ pipethread->emitNotice(fromLyX);
notified = true;
break;
}
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.
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;
}
}
#endif
perror("monitor");
- infoLB->clear();
- notifyLB->setText("An error occurred, closing pipes");
+ pipethread->emitNotice("An error occurred, closing pipes");
notified = true;
break;
} else
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) {
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
buffer[BUFSIZE - 1] = '\0';
::write(pipein, buffer, strlen(buffer));
}
- /* Say goodbye */
+ // Say goodbye
snprintf(buffer, BUFSIZE - 1, "LYXSRV:%s:bye\n",
clientname.toUtf8().constData());
buffer[BUFSIZE - 1] = '\0';
}
-int main(int argc, char *argv[])
+int main(int argc, char * argv[])
{
QApplication app(argc, argv);
LyXServerMonitor dialog;