Socket API: Add test client

This commit is contained in:
Daniel Molkentin 2014-07-02 10:10:22 +02:00
parent ffa7f35a87
commit 623dfc0286
9 changed files with 245 additions and 0 deletions

View File

@ -140,6 +140,7 @@ set(TRANSLATIONS ${TRANS_FILES})
add_subdirectory(csync)
add_subdirectory(src)
add_subdirectory(shell_integration)
add_subdirectory(doc)
if(UNIT_TESTING)

View File

@ -0,0 +1 @@
add_subdirectory(testclient)

View File

@ -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})

View File

@ -0,0 +1,27 @@
#include <QApplication>
#include <QLocalSocket>
#include <QDir>
#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();
}

View File

@ -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();
}

View File

@ -0,0 +1,21 @@
#ifndef SOCKETCLIENT_H
#define SOCKETCLIENT_H
#include <QObject>
class QLocalSocket;
class SocketClient : public QObject
{
Q_OBJECT
public:
explicit SocketClient(QObject *parent = 0);
public slots:
void writeData();
private:
QLocalSocket *sock;
};
#endif // SOCKETCLIENT_H

View File

@ -0,0 +1,101 @@
#include "window.h"
#include "ui_window.h"
#include <QLineEdit>
#include <QSyntaxHighlighter>
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);
}
}
}

View File

@ -0,0 +1,32 @@
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QLocalSocket>
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

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Window</class>
<widget class="QWidget" name="Window">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>SocketAPI Test Client</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="inputEdit">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="outputs"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>