From 623dfc028678cbbfc1cefa69630fab8a2e7be870 Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Wed, 2 Jul 2014 10:10:22 +0200 Subject: [PATCH] Socket API: Add test client --- CMakeLists.txt | 1 + shell_integration/CMakeLists.txt | 1 + shell_integration/testclient/CMakeLists.txt | 9 ++ shell_integration/testclient/main.cpp | 27 +++++ shell_integration/testclient/socketclient.cpp | 22 ++++ shell_integration/testclient/socketclient.h | 21 ++++ shell_integration/testclient/window.cpp | 101 ++++++++++++++++++ shell_integration/testclient/window.h | 32 ++++++ shell_integration/testclient/window.ui | 31 ++++++ 9 files changed, 245 insertions(+) create mode 100644 shell_integration/CMakeLists.txt create mode 100644 shell_integration/testclient/CMakeLists.txt create mode 100644 shell_integration/testclient/main.cpp create mode 100644 shell_integration/testclient/socketclient.cpp create mode 100644 shell_integration/testclient/socketclient.h create mode 100644 shell_integration/testclient/window.cpp create mode 100644 shell_integration/testclient/window.h create mode 100644 shell_integration/testclient/window.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index d3730846d..670759dfe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ set(TRANSLATIONS ${TRANS_FILES}) add_subdirectory(csync) add_subdirectory(src) +add_subdirectory(shell_integration) add_subdirectory(doc) if(UNIT_TESTING) diff --git a/shell_integration/CMakeLists.txt b/shell_integration/CMakeLists.txt new file mode 100644 index 000000000..8f561fbd3 --- /dev/null +++ b/shell_integration/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(testclient) diff --git a/shell_integration/testclient/CMakeLists.txt b/shell_integration/testclient/CMakeLists.txt new file mode 100644 index 000000000..0b171ef14 --- /dev/null +++ b/shell_integration/testclient/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.6) +project(testclient) +set(testclient_SOURCES main.cpp window.cpp) +set(testclient_HEADERS window.h) +qt_wrap_ui(testclient_UI_SRCS window.ui) +add_executable(testclient ${testclient_SOURCES} ${testclient_HEADERS} ${testclient_UI_SRCS}) +target_link_libraries(testclient ${QT_LIBRARIES}) +set_target_properties(testclient PROPERTIES AUTOMOC TRUE) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/shell_integration/testclient/main.cpp b/shell_integration/testclient/main.cpp new file mode 100644 index 000000000..901767dc4 --- /dev/null +++ b/shell_integration/testclient/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + QLocalSocket sock; + QString sockAddr; +#ifdef Q_OS_UNIX + sockAddr = QDir::homePath() + QLatin1String("/.local/share/data/ownCloud/socket"); +#else + sockAddr = QLatin1String("\\\\.\\pipe\\ownCloud"); +#endif + Window win(&sock); + QObject::connect(&sock, SIGNAL(readyRead()), &win, SLOT(receive())); + QObject::connect(&sock, SIGNAL(error(QLocalSocket::LocalSocketError)), + &win, SLOT(receiveError(QLocalSocket::LocalSocketError))); + + win.show(); + sock.connectToServer(sockAddr, QIODevice::ReadWrite); + qDebug() << "Connecting to" << sockAddr; + + return app.exec(); +} diff --git a/shell_integration/testclient/socketclient.cpp b/shell_integration/testclient/socketclient.cpp new file mode 100644 index 000000000..dbe8bdfc1 --- /dev/null +++ b/shell_integration/testclient/socketclient.cpp @@ -0,0 +1,22 @@ + +#include "socketclient.h" + +SocketClient::SocketClient(QObject *parent) : + QObject(parent) + , sock(new QLocalSocket(this)) +{ + QString sockAddr; +#ifdef Q_OS_UNIX + sockAddr = QDir::homePath() + QLatin1String("/.local/share/data/ownCloud/"); +#else + sockAddr = QLatin1String("\\\\.\\pipe\\ownCloud"); +#endif + sock->connectToServer(sockAddr); + sock->open(QIODevice::ReadWrite); + connect(sock, SIGNAL(readyRead()), SLOT(writeData())); +} + +void SocketClient::writeData() +{ + qDebug() << sock->readAll(); +} diff --git a/shell_integration/testclient/socketclient.h b/shell_integration/testclient/socketclient.h new file mode 100644 index 000000000..59bb08863 --- /dev/null +++ b/shell_integration/testclient/socketclient.h @@ -0,0 +1,21 @@ +#ifndef SOCKETCLIENT_H +#define SOCKETCLIENT_H + +#include + +class QLocalSocket; + +class SocketClient : public QObject +{ + Q_OBJECT +public: + explicit SocketClient(QObject *parent = 0); + +public slots: + void writeData(); + +private: + QLocalSocket *sock; +}; + +#endif // SOCKETCLIENT_H diff --git a/shell_integration/testclient/window.cpp b/shell_integration/testclient/window.cpp new file mode 100644 index 000000000..a3f185b9e --- /dev/null +++ b/shell_integration/testclient/window.cpp @@ -0,0 +1,101 @@ +#include "window.h" +#include "ui_window.h" + +#include +#include + +class LogWindowHighlighter : public QSyntaxHighlighter +{ +public: + LogWindowHighlighter(QTextDocument *parent = 0); + +protected: + void highlightBlock(const QString &text); + void highlightHelper(const QString& text, const QTextCharFormat &format, const QString &exp); +}; + +Window::Window(QIODevice *dev, QWidget *parent) : + QWidget(parent), + ui(new Ui::Window) + , device(dev) +{ + ui->setupUi(this); + connect(ui->inputEdit->lineEdit(), SIGNAL(returnPressed()), SLOT(handleReturn())); + addDefaultItems(); + QFont f("Courier"); + f.setStyleHint(QFont::Monospace); + ui->outputs->setFont(f); + new LogWindowHighlighter(ui->outputs->document()); +} + +Window::~Window() +{ + delete ui; +} + +void Window::receive() +{ + QByteArray ba = device->readAll(); + ui->outputs->insertPlainText(QString::fromLatin1(ba)); +} + +void Window::receiveError(QLocalSocket::LocalSocketError error) +{ + qDebug() << "Error connecting to socket:" << error; +} + +void Window::handleReturn() +{ + QString cmd = ui->inputEdit->currentText()+QLatin1Char('\n'); + ui->outputs->insertPlainText(cmd); + device->write(cmd.toLatin1()); + ui->inputEdit->lineEdit()->clear(); +} + +void Window::addDefaultItems() +{ + QStringList commands; + commands << "RETRIEVE_FOLDER_STATUS:" << "RETRIEVE_FILE_STATUS:"; + ui->inputEdit->addItems(commands); +} + + +LogWindowHighlighter::LogWindowHighlighter(QTextDocument *parent) + : QSyntaxHighlighter(parent) +{ + +} + +void LogWindowHighlighter::highlightBlock(const QString &text) +{ + QTextCharFormat responseKeywordFormat; + responseKeywordFormat.setFontWeight(QFont::Bold); + responseKeywordFormat.setForeground(Qt::darkMagenta); + QString responsePattern = "(UPDATE_VIEW|BROADCAST:|STATUS:)"; + highlightHelper(text, responseKeywordFormat, responsePattern); + + QTextCharFormat messageKeywordFormat; + messageKeywordFormat.setFontWeight(QFont::Bold); + messageKeywordFormat.setForeground(Qt::darkYellow); + QString messagePattern = "(RETRIEVE_(FOLDER|FILE)_STATUS):"; + highlightHelper(text, messageKeywordFormat, messagePattern); +} + +void LogWindowHighlighter::highlightHelper(const QString& text, const QTextCharFormat &format, + const QString &pattern) +{ + QRegExp rex(pattern); + int index = text.indexOf(rex); + if (index >= 0) { + int len = rex.matchedLength(); + setFormat(index, len, format); + int lastMatchedEnd=index+len; + int secondDoubleDotIndex = text.indexOf(':', lastMatchedEnd); + if (secondDoubleDotIndex >= 0) { + QTextCharFormat boldFormat; + boldFormat.setFontWeight(QFont::Bold); + setFormat(lastMatchedEnd, secondDoubleDotIndex-lastMatchedEnd, boldFormat); + } + } + +} diff --git a/shell_integration/testclient/window.h b/shell_integration/testclient/window.h new file mode 100644 index 000000000..ec37d2154 --- /dev/null +++ b/shell_integration/testclient/window.h @@ -0,0 +1,32 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include +#include + +namespace Ui { +class Window; +} + +class Window : public QWidget +{ + Q_OBJECT + +public: + explicit Window(QIODevice *dev, QWidget *parent = 0); + ~Window(); + +public slots: + void receive(); + void receiveError(QLocalSocket::LocalSocketError); + +private slots: + void handleReturn(); + +private: + void addDefaultItems(); + Ui::Window *ui; + QIODevice *device; +}; + +#endif // WINDOW_H diff --git a/shell_integration/testclient/window.ui b/shell_integration/testclient/window.ui new file mode 100644 index 000000000..82ce2a901 --- /dev/null +++ b/shell_integration/testclient/window.ui @@ -0,0 +1,31 @@ + + + Window + + + + 0 + 0 + 400 + 300 + + + + SocketAPI Test Client + + + + + + true + + + + + + + + + + +