initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

37
roboute/CMakeLists.txt Normal file
View file

@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 2.8.12)
project(roboute)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
roboute.h
mainwindow.h
)
set(SOURCES
main.cpp
roboute.cpp
mainwindow.cpp
)
add_executable(roboute ${HEADERS} ${SOURCES})
add_subdirectory(views)
add_subdirectory(models)
target_link_libraries(roboute Qt5::Core)
target_link_libraries(roboute Qt5::Widgets)
target_link_libraries(roboute Qt5::Network)
target_link_libraries(roboute wSocket)
target_link_libraries(roboute wDispatcher)
target_link_libraries(roboute utils)
target_link_libraries(roboute robouteViews)
target_link_libraries(roboute robouteModels)
install(TARGETS roboute RUNTIME DESTINATION bin)

View file

@ -0,0 +1,80 @@
#include "applistitemdelegate.h"
#include <iostream>
#define QFIXED_MAX (INT_MAX/256)
AppListItemDelegate::AppListItemDelegate(QObject* parent) :
QItemDelegate(parent)
{
}
void AppListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//Q_ASSERT(index.isValid());
QStyleOptionViewItem opt = setOptions(index, option);
// prepare
painter->save();
// if (d->clipPainting)
// painter->setClipRect(opt.rect);
// get the data and the rectangles
QVariant value;
QPixmap pixmap;
QRect decorationRect;
// value = index.data(Qt::DecorationRole);
// if (value.isValid()) {
// // ### we need the pixmap to call the virtual function
// pixmap = decoration(opt, value);
// if (value.type() == QVariant::Icon) {
// d->tmp.icon = qvariant_cast<QIcon>(value);
// d->tmp.mode = d->iconMode(option.state);
// d->tmp.state = d->iconState(option.state);
// const QSize size = d->tmp.icon.actualSize(option.decorationSize,
// d->tmp.mode, d->tmp.state);
// decorationRect = QRect(QPoint(0, 0), size);
// } else {
// d->tmp.icon = QIcon();
// decorationRect = QRect(QPoint(0, 0), pixmap.size());
// }
// } else {
// d->tmp.icon = QIcon();
// decorationRect = QRect();
// }
QString text;
QRect displayRect;
value = index.data(Qt::DisplayRole);
std::cout << "ha" << std::endl;
if (value.isValid() && !value.isNull()) {
text = value.toMap().value("name").toString();
displayRect = opt.rect;
displayRect.setWidth(QFIXED_MAX);
}
QRect checkRect;
Qt::CheckState checkState = Qt::Unchecked;
// value = index.data(Qt::CheckStateRole);
// if (value.isValid()) {
// checkState = static_cast<Qt::CheckState>(value.toInt());
// checkRect = check(opt, opt.rect, value);
// }
// do the layout
doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
// draw the item
drawBackground(painter, opt, index);
drawCheck(painter, opt, checkRect, checkState);
drawDecoration(painter, opt, decorationRect, pixmap);
drawDisplay(painter, opt, displayRect, text);
drawFocus(painter, opt, displayRect);
// done
painter->restore();
}

View file

@ -0,0 +1,17 @@
#ifndef APPLISTITEMDELEGATE_H
#define APPLISTITEMDELEGATE_H
#include <QtWidgets/QItemDelegate>
#include <QtGui/QPainter>
class AppListItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
AppListItemDelegate(QObject* parent = 0);
void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
};
#endif // APPLISTITEMDELEGATE_H

64
roboute/main.cpp Normal file
View file

@ -0,0 +1,64 @@
#include <QtWidgets/QApplication>
#include <QtCore/QTimer>
#include <utils/signalcatcher.h>
#include "roboute.h"
#include "mainwindow.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("RadioW");
QCoreApplication::setApplicationName("Roboute");
QCoreApplication::setApplicationVersion("0.0.1");
W::SignalCatcher sc(&app);
Roboute* roboute = new Roboute(&app);
MainWindow* wnd = new MainWindow();;
QObject::connect(roboute, SIGNAL(debugMessage(const QString&)), wnd, SLOT(robouteMessage(const QString&)));
QObject::connect(roboute, SIGNAL(newService(uint64_t, const QString&)), wnd, SLOT(newService(uint64_t, const QString&)));
QObject::connect(roboute, SIGNAL(serviceConnecting(uint64_t)), wnd, SLOT(serviceConnecting(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceConnected(uint64_t)), wnd, SLOT(serviceConnected(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceDisconnecting(uint64_t)), wnd, SLOT(serviceDisconnecting(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceDisconnected(uint64_t)), wnd, SLOT(serviceDisconnected(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceConnectionFailed(uint64_t)), wnd, SLOT(serviceConnectionFailed(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceLaunched(uint64_t)), wnd, SLOT(serviceLaunched(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceStopped(uint64_t)), wnd, SLOT(serviceStopped(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceLaunching(uint64_t)), wnd, SLOT(serviceLaunching(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceStopping(uint64_t)), wnd, SLOT(serviceStopping(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceRemoved(uint64_t)), wnd, SLOT(serviceRemoved(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceAttrChange(uint64_t, const QString&, const QString&)),
wnd, SLOT(serviceAttrChange(uint64_t, const QString&, const QString&)));
QObject::connect(roboute, SIGNAL(log(uint64_t, const QString&)), wnd, SLOT(serviceLog(uint64_t, const QString&)));
QObject::connect(roboute, SIGNAL(serviceAddCommand(uint64_t, const QString&, const QMap<QString, uint64_t>)),
wnd, SLOT(serviceAddCommand(uint64_t, const QString&, const QMap<QString, uint64_t>)));
QObject::connect(roboute, SIGNAL(serviceRemoveCommand(uint64_t, const QString&)),
wnd, SLOT(serviceRemoveCommand(uint64_t, const QString&)));
QObject::connect(roboute, SIGNAL(serviceClearCommands(uint64_t)), wnd, SLOT(serviceClearCommands(uint64_t)));
QObject::connect(roboute, SIGNAL(serviceChangeName(uint64_t, const QString&)), wnd, SLOT(serviceNameChange(uint64_t, const QString&)));
QObject::connect(roboute, SIGNAL(serviceEdit(uint64_t, const QMap<QString, QString>&)), wnd, SLOT(serviceEdit(uint64_t, const QMap<QString, QString>&)));
QObject::connect(wnd, SIGNAL(addService(const QMap<QString, QString>&)), roboute, SLOT(addService(const QMap<QString, QString>&)));
QObject::connect(wnd, SIGNAL(connectService(uint64_t)), roboute, SLOT(connectService(uint64_t)));
QObject::connect(wnd, SIGNAL(disconnectService(uint64_t)), roboute, SLOT(disconnectService(uint64_t)));
QObject::connect(wnd, SIGNAL(launchService(uint64_t)), roboute, SLOT(launchService(uint64_t)));
QObject::connect(wnd, SIGNAL(stopService(uint64_t)), roboute, SLOT(stopService(uint64_t)));
QObject::connect(wnd, SIGNAL(removeService(uint64_t)), roboute, SLOT(removeService(uint64_t)));
QObject::connect(wnd, SIGNAL(launchCommand(uint64_t, const QString&, const QMap<QString, QVariant>&)),
roboute, SLOT(launchCommand(uint64_t, const QString&, const QMap<QString, QVariant>&)));
QObject::connect(wnd, SIGNAL(editService(uint64_t)), roboute, SLOT(editService(uint64_t)));
QObject::connect(wnd, SIGNAL(changeService(uint64_t, const QMap<QString, QString>&)), roboute, SLOT(changeService(uint64_t, const QMap<QString, QString>&)));
QTimer::singleShot(0, roboute, SLOT(start()));
QObject::connect(&app, SIGNAL(aboutToQuit()), roboute, SLOT(stop()));
QObject::connect(&app, SIGNAL(aboutToQuit()), wnd, SLOT(saveSettings()));
wnd->show();
int result = app.exec();
delete wnd;
return result;
}

328
roboute/mainwindow.cpp Normal file
View file

@ -0,0 +1,328 @@
#include <QtWidgets/QMenuBar>
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow():
QMainWindow(),
apps(new AppListModel(this)),
widget(new MainView(apps, this)),
newApp(0),
commandForm(0),
rightBar(new QToolBar(this)),
editingService(0)
{
createActions();
createToolbar();
setCentralWidget(widget);
apps->push_back(0, "Roboute");
apps->setLaunched(0, true);
apps->setConnected(0, true);
apps->setEditable(0, false);
QItemSelectionModel* as = widget->list->selectionModel();
connect(
as, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
this, SLOT(selectionChanged(const QItemSelection&, const QItemSelection&))
);
connect(widget->details, SIGNAL(connect(uint64_t)), this, SIGNAL(connectService(uint64_t)));
connect(widget->details, SIGNAL(disconnect(uint64_t)), this, SIGNAL(disconnectService(uint64_t)));
connect(widget->details, SIGNAL(launch(uint64_t)), this, SIGNAL(launchService(uint64_t)));
connect(widget->details, SIGNAL(stop(uint64_t)), this, SIGNAL(stopService(uint64_t)));
connect(widget->details, SIGNAL(remove(uint64_t)), this, SIGNAL(removeService(uint64_t)));
connect(widget->details, SIGNAL(edit(uint64_t)), this, SIGNAL(editService(uint64_t)));
connect(widget->details, SIGNAL(clearLog(uint64_t)), this, SLOT(clearServiceLog(uint64_t)));
connect(widget->details, SIGNAL(launchCommand(uint64_t, const QString&)), this, SLOT(onLaunchedCommand(uint64_t, const QString&)));
restoreSettings();
}
void MainWindow::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
QModelIndexList deselectedIndexes = deselected.indexes();
QModelIndexList::const_iterator dItr = deselectedIndexes.begin();
QModelIndexList::const_iterator dEnd = deselectedIndexes.end();
for (; dItr != dEnd; ++dItr) {
unsubscribeDetailsById(apps->data(*dItr, Qt::UserRole).toInt());
}
QModelIndexList selectedIndexes = selected.indexes();
QModelIndexList::const_iterator sItr = selectedIndexes.begin();
QModelIndexList::const_iterator sEnd = selectedIndexes.end();
for (; sItr != sEnd; ++sItr) {
subscribeDetailsById(apps->data(*sItr, Qt::UserRole).toInt());
}
if (deselectedIndexes.size() == 1 && selectedIndexes.size() == 0) {
widget->hideDetails();
rightBar->hide();
} else if (deselectedIndexes.size() == 0 && selectedIndexes.size() == 1) {
widget->showDetails();
rightBar->show();
}
}
void MainWindow::subscribeDetailsById(quint64 id)
{
widget->details->setModel(apps->getApp(id));
}
void MainWindow::unsubscribeDetailsById(quint64 id)
{
widget->details->clearModel();
}
void MainWindow::robouteMessage(const QString& msg)
{
apps->logMessage(0, msg);
}
void MainWindow::unselectAll()
{
widget->list->selectionModel()->clearSelection();
}
void MainWindow::createActions()
{
QMenu *actionsMenu = menuBar()->addMenu(tr("Actions"));
const QIcon newIcon = QIcon::fromTheme("document-new");
QAction *newAct = new QAction(newIcon, tr("New application"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Add new application"));
connect(newAct, &QAction::triggered, this, &MainWindow::newApplication);
actionsMenu->addAction(newAct);
}
void MainWindow::createToolbar()
{
addToolBar(Qt::RightToolBarArea, rightBar);
rightBar->setMovable(false);
rightBar->setObjectName("rightBar");
rightBar->hide();
QAction* attrs = rightBar->addAction(QIcon::fromTheme("dialog-object-properties"), tr("Attributes"));
QAction* commands = rightBar->addAction(QIcon::fromTheme("dialog-scripts"), tr("Commands"));
attrs->setCheckable(true);
commands->setCheckable(true);
QActionGroup* ag = new QActionGroup(rightBar);
ag->setExclusive(true);
ag->addAction(attrs);
ag->addAction(commands);
connect(attrs, SIGNAL(toggled(bool)), SLOT(attrsToggled(bool)));
connect(commands, SIGNAL(toggled(bool)), SLOT(commandsToggled(bool)));
}
void MainWindow::newApplication()
{
newApp = new NewAppDialogue(this);
connect(newApp, SIGNAL(accepted()), SLOT(newAppAccepted()));
connect(newApp, SIGNAL(rejected()), SLOT(newAppRejected()));
newApp->setModal(true);
newApp->setWindowTitle(tr("New application"));
newApp->show();
}
void MainWindow::newAppAccepted()
{
if (editingService == 0) {
emit addService(newApp->getData());
} else {
emit changeService(editingService, newApp->getData());
editingService = 0;
}
delete newApp;
newApp = 0;
}
void MainWindow::newAppRejected()
{
editingService = 0;
delete newApp;
newApp = 0;
}
void MainWindow::newService(uint64_t id, const QString& name)
{
apps->push_back(id, name);
apps->setConnectable(id, true);
apps->setEditable(id, true);
}
void MainWindow::serviceConnecting(uint64_t id)
{
apps->setConnectable(id, false);
apps->setConnected(id, false);
apps->setEditable(id, false);
}
void MainWindow::serviceConnected(uint64_t id)
{
apps->setConnectable(id, true);
apps->setConnected(id, true);
apps->setEditable(id, false);
}
void MainWindow::serviceDisconnecting(uint64_t id)
{
apps->setConnectable(id, false);
apps->setConnected(id, true);
apps->setEditable(id, false);
}
void MainWindow::serviceDisconnected(uint64_t id)
{
apps->setConnectable(id, true);
apps->setConnected(id, false);
apps->setEditable(id, true);
}
void MainWindow::serviceConnectionFailed(uint64_t id)
{
apps->setConnected(id, false);
apps->setEditable(id, true);
}
void MainWindow::serviceLaunched(uint64_t id)
{
apps->setLaunched(id, true);
apps->setLaunchable(id, true);
}
void MainWindow::serviceStopped(uint64_t id)
{
apps->setLaunched(id, false);
apps->setLaunchable(id, true);
}
void MainWindow::serviceLaunching(uint64_t id)
{
apps->setLaunched(id, false);
apps->setLaunchable(id, false);
}
void MainWindow::serviceStopping(uint64_t id)
{
apps->setLaunched(id, true);
apps->setLaunchable(id, false);
}
void MainWindow::serviceLog(uint64_t id, const QString& log)
{
apps->logMessage(id, log);
}
void MainWindow::serviceRemoved(uint64_t id)
{
apps->removeElement(id);
}
void MainWindow::restoreSettings()
{
QSettings settings;
restoreGeometry(settings.value("window/geometry").toByteArray());
restoreState(settings.value("window/state").toByteArray());
widget->readSettings();
rightBar->hide();
}
void MainWindow::saveSettings()
{
QSettings settings;
settings.beginGroup("window");
settings.setValue("geometry", saveGeometry());
settings.setValue("state", saveState());
settings.endGroup();
widget->saveSettings();
}
void MainWindow::serviceAttrChange(uint64_t id, const QString& key, const QString& value)
{
apps->setAttribute(id, key, value);
}
void MainWindow::attrsToggled(bool checked)
{
widget->details->showAttrs(checked);
}
void MainWindow::commandsToggled(bool checked)
{
widget->details->showCommands(checked);
}
void MainWindow::serviceAddCommand(uint64_t id, const QString& key, const QMap<QString, uint64_t>& arguments)
{
apps->addCommand(id, key, arguments);
}
void MainWindow::serviceRemoveCommand(uint64_t id, const QString& key)
{
apps->removeCommand(id, key);
}
void MainWindow::serviceClearCommands(uint64_t id)
{
apps->clearCommands(id);
}
void MainWindow::onLaunchedCommand(uint64_t id, const QString& name)
{
commandForm = new CommandForm(name, apps->getApp(id)->commands.getCommandArgs(name), this);
connect(commandForm, SIGNAL(accepted()), SLOT(commandFormAccepted()));
connect(commandForm, SIGNAL(rejected()), SLOT(commandFormRejected()));
commandForm->setModal(true);
commandForm->setWindowTitle(tr("Execute the command"));
commandForm->show();
}
void MainWindow::commandFormAccepted()
{
emit launchCommand(widget->details->getModelId(), commandForm->getName(), commandForm->getData());
delete commandForm;
commandForm = 0;
}
void MainWindow::commandFormRejected()
{
delete commandForm;
commandForm = 0;
}
void MainWindow::clearServiceLog(uint64_t id)
{
apps->clearLog(id);
}
void MainWindow::serviceEdit(uint64_t id, const QMap<QString, QString>& data)
{
if (editingService == 0) {
editingService = id;
newApp = new NewAppDialogue(data, this);
connect(newApp, SIGNAL(accepted()), SLOT(newAppAccepted()));
connect(newApp, SIGNAL(rejected()), SLOT(newAppRejected()));
newApp->setModal(true);
newApp->setWindowTitle(tr("Edit application"));
newApp->show();
}
}
void MainWindow::serviceNameChange(uint64_t id, const QString& name)
{
apps->setName(id, name);
}

86
roboute/mainwindow.h Normal file
View file

@ -0,0 +1,86 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QToolBar>
#include <QtCore/QSettings>
#include "views/mainview.h"
#include "views/newappdialogue.h"
#include "views/commandform.h"
#include "models/applistmodel.h"
#include "models/appmodel.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private:
AppListModel* apps;
MainView* widget;
NewAppDialogue* newApp;
CommandForm* commandForm;
QToolBar* rightBar;
uint64_t editingService;
private:
void createActions();
void createToolbar();
void subscribeDetailsById(quint64 id);
void unsubscribeDetailsById(quint64 id);
void restoreSettings();
signals:
void addService(const QMap<QString, QString>&);
void connectService(uint64_t);
void disconnectService(uint64_t);
void launchService(uint64_t);
void stopService(uint64_t);
void removeService(uint64_t id);
void editService(uint64_t id);
void changeService(uint64_t id, const QMap<QString, QString>&);
void launchCommand(uint64_t id, const QString& name, const QMap<QString, QVariant>& args);
public slots:
void saveSettings();
void robouteMessage(const QString& msg);
void newService(uint64_t id, const QString& name);
void serviceAttrChange(uint64_t id, const QString& key, const QString& value);
void serviceConnected(uint64_t id);
void serviceConnecting(uint64_t id);
void serviceDisconnected(uint64_t id);
void serviceDisconnecting(uint64_t id);
void serviceConnectionFailed(uint64_t id);
void serviceLaunched(uint64_t id);
void serviceLaunching(uint64_t id);
void serviceStopped(uint64_t id);
void serviceStopping(uint64_t id);
void serviceLog(uint64_t id, const QString& log);
void serviceRemoved(uint64_t id);
void serviceAddCommand(uint64_t id, const QString& key, const QMap<QString, uint64_t>& arguments);
void serviceRemoveCommand(uint64_t id, const QString& key);
void serviceNameChange(uint64_t id, const QString& name);
void serviceClearCommands(uint64_t id);
void serviceEdit(uint64_t id, const QMap<QString, QString>& data);
private slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
void unselectAll();
void newApplication();
void newAppAccepted();
void newAppRejected();
void commandFormAccepted();
void commandFormRejected();
void attrsToggled(bool checked);
void commandsToggled(bool checked);
void onLaunchedCommand(uint64_t id, const QString& name);
void clearServiceLog(uint64_t id);
};
#endif // MAINWINDOW_H

View file

@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 2.8.12)
project(robouteModels)
find_package(Qt5Core REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
applistmodel.h
appmodel.h
service.h
apppropertiesmodel.h
appcommandsmodel.h
)
set(SOURCES
applistmodel.cpp
appmodel.cpp
service.cpp
apppropertiesmodel.cpp
appcommandsmodel.cpp
)
add_library(robouteModels STATIC ${HEADERS} ${SOURCES})
target_link_libraries(robouteModels Qt5::Core)
target_link_libraries(robouteModels wType)
target_link_libraries(robouteModels wSocket)
target_link_libraries(robouteModels wSsh)
target_link_libraries(robouteModels wController)

View file

@ -0,0 +1,110 @@
#include "appcommandsmodel.h"
AppCommandsModel::AppCommandsModel(QObject* parent):
QAbstractListModel(parent),
index(),
map(),
toInsert()
{
}
AppCommandsModel::~AppCommandsModel()
{
clear();
}
void AppCommandsModel::inserCommand(const QString& name, const ArgsMap& args)
{
toInsert.push_back(Pair(name, new ArgsMap(args)));
insertRows(rowCount(), 1);
}
void AppCommandsModel::removeCommand(const QString& name)
{
for (int i = 0; i < index.size(); ++i) {
if (index[i].key() == name) {
removeRows(i, 1);
break;
}
}
}
int AppCommandsModel::rowCount(const QModelIndex& parent) const
{
return index.size();
}
QVariant AppCommandsModel::data(const QModelIndex& i, int role) const
{
Map::iterator itr = index[i.row()];
switch(role) {
case Qt::DisplayRole:
return itr.key();
case Qt::TextAlignmentRole:
return Qt::AlignCenter + Qt::AlignVCenter;
}
return QVariant();
}
bool AppCommandsModel::insertRows(int row, int count, const QModelIndex& parent)
{
if (toInsert.size() != count) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
Index::const_iterator target = index.begin() + row;
for (int i = 0; i < count; ++i) {
List::iterator itr = toInsert.begin();
Map::iterator mItr = map.insert(itr->first, itr->second);
index.insert(target, mItr);
toInsert.erase(itr);
}
endInsertRows();
return true;
}
bool AppCommandsModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (row + count > index.size()) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
Index::iterator itr;
Index::iterator beg = index.begin() + row;
Index::iterator end = beg + count;
for (itr = beg; itr != end; ++itr) {
Map::iterator mItr = *itr;
ArgsMap* app = *mItr;
delete app;
map.erase(mItr);
}
index.erase(beg, end);
endRemoveRows();
return true;
}
AppCommandsModel::ArgsMap AppCommandsModel::getCommandArgs(const QString& name)
{
return *(map[name]);
}
void AppCommandsModel::clear()
{
beginResetModel();
Map::iterator itr = map.begin();
Map::iterator end = map.end();
for (; itr != end; ++itr) {
delete itr.value();
}
map.clear();
index.clear();
endResetModel();
}

View file

@ -0,0 +1,40 @@
#ifndef APPCOMMANDSMODEL_H
#define APPCOMMANDSMODEL_H
#include <QtCore/QAbstractListModel>
#include <QtCore/QMap>
#include <deque>
#include <list>
class AppCommandsModel : public QAbstractListModel
{
Q_OBJECT
typedef QMap<QString, uint64_t> ArgsMap;
public:
AppCommandsModel(QObject* parent = 0);
~AppCommandsModel();
void inserCommand(const QString& name, const ArgsMap& args);
void removeCommand(const QString& name);
ArgsMap getCommandArgs(const QString& name);
void clear();
QVariant data(const QModelIndex &i, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
private:
typedef QMap<QString, ArgsMap*> Map;
typedef std::deque<Map::iterator> Index;
typedef std::pair<QString, ArgsMap*> Pair;
typedef std::list<Pair> List;
Index index;
Map map;
List toInsert;
};
#endif // APPCOMMANDSMODEL_H

View file

@ -0,0 +1,187 @@
#include "applistmodel.h"
#include <iostream>
AppListModel::AppListModel(QObject* parent):
QAbstractListModel(parent),
index(),
helper(),
map(),
toInsert()
{
}
AppListModel::~AppListModel()
{
clear();
}
void AppListModel::push_back(uint64_t id, const QString& name)
{
AppModel* item = new AppModel(id, name);
toInsert.push_back(Pair(id, item));
insertRows(rowCount(), 1);
}
void AppListModel::removeElement(uint64_t id)
{
int index = *(helper.find(id));
removeRows(index, 1);
}
int AppListModel::rowCount(const QModelIndex& parent) const
{
//std::cout << index.size() << std::endl;
return index.size();
}
QVariant AppListModel::data(const QModelIndex& i, int role) const
{
Map::iterator itr = index[i.row()];
switch(role) {
case Qt::DisplayRole:
return itr.value()->getName();
case Qt::TextAlignmentRole:
return Qt::AlignCenter + Qt::AlignVCenter;
case Qt::UserRole:
quint64 id = itr.key();
return id;
}
return QVariant();
}
bool AppListModel::insertRows(int row, int count, const QModelIndex& parent)
{
if (toInsert.size() != count) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
Index::const_iterator target = index.begin() + row;
for (int i = 0; i < count; ++i) {
List::iterator itr = toInsert.begin();
Map::iterator mItr = map.insert(itr->first, itr->second);
index.insert(target, mItr);
helper.insert(itr->first, row + i);
toInsert.erase(itr);
}
endInsertRows();
return true;
}
bool AppListModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (row + count > index.size()) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
Index::iterator itr;
Index::iterator beg = index.begin() + row;
Index::iterator end = beg + count;
for (itr = beg; itr != end; ++itr) {
Map::iterator mItr = *itr;
AppModel* app = *mItr;
IndexHelper::iterator hItr = helper.find(app->id);
delete app;
map.erase(mItr);
helper.erase(hItr);
}
index.erase(beg, end);
endRemoveRows();
return true;
}
void AppListModel::logMessage(uint64_t id, const QString& msg)
{
map[id]->logMessage(msg);
}
AppModel* AppListModel::getApp(uint64_t id)
{
return map[id];
}
void AppListModel::clear()
{
beginResetModel();
Map::iterator itr = map.begin();
Map::iterator end = map.end();
for (; itr != end; ++itr) {
delete itr.value();
}
map.clear();
index.clear();
helper.clear();
endResetModel();
}
void AppListModel::setConnectable(uint64_t id, bool value)
{
map[id]->setConnectable(value);
}
void AppListModel::setConnected(uint64_t id, bool value)
{
map[id]->setConnected(value);
}
void AppListModel::setLaunchable(uint64_t id, bool value)
{
map[id]->setLaunchable(value);
}
void AppListModel::setLaunched(uint64_t id, bool value)
{
map[id]->setLaunched(value);
}
void AppListModel::setEditable(uint64_t id, bool value)
{
map[id]->setEditable(value);
}
void AppListModel::setAttribute(uint64_t id, const QString& key, const QString& value)
{
map[id]->props.setProp(key, value);
}
void AppListModel::addCommand(uint64_t id, const QString& key, const QMap<QString, uint64_t>& arguments)
{
map[id]->commands.inserCommand(key, arguments);
}
void AppListModel::removeCommand(uint64_t id, const QString& key)
{
map[id]->commands.removeCommand(key);
}
void AppListModel::clearCommands(uint64_t id)
{
map[id]->commands.clear();
}
void AppListModel::clearLog(uint64_t id)
{
map[id]->clearLog();
}
void AppListModel::setName(uint64_t id, const QString& name)
{
map[id]->setName(name);
int row = *(helper.find(id));
emit dataChanged(QAbstractListModel::index(row), QAbstractListModel::index(row), {Qt::DisplayRole});
}

View file

@ -0,0 +1,58 @@
#ifndef APPLISTMODEL_H
#define APPLISTMODEL_H
#include <QtCore/QAbstractListModel>
#include <QtCore/QMap>
#include <QtCore/QVariant>
#include <deque>
#include <list>
#include "appmodel.h"
class AppListModel : public QAbstractListModel
{
Q_OBJECT
public:
AppListModel(QObject* parent = 0);
~AppListModel();
void push_back(uint64_t id, const QString& name);
void removeElement(uint64_t id);
AppModel* getApp(uint64_t id);
void clear();
QVariant data(const QModelIndex &i, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
void setConnectable(uint64_t id, bool value);
void setConnected(uint64_t id, bool value);
void setLaunchable(uint64_t id, bool value);
void setLaunched(uint64_t id, bool value);
void setEditable(uint64_t id, bool value);
void setName(uint64_t id, const QString& name);
void setAttribute(uint64_t id, const QString& key, const QString& value);
void addCommand(uint64_t id, const QString& key, const QMap<QString, uint64_t>& arguments);
void removeCommand(uint64_t id, const QString& key);
void clearCommands(uint64_t id);
void clearLog(uint64_t id);
private:
typedef QMap<uint64_t, AppModel*> Map;
typedef QMap<uint64_t, uint64_t> IndexHelper;
typedef std::deque<Map::iterator> Index;
typedef std::pair<uint64_t, AppModel*> Pair;
typedef std::list<Pair> List;
Index index;
IndexHelper helper;
Map map;
List toInsert;
public slots:
void logMessage(uint64_t id, const QString& msg);
};
#endif // APPLISTMODEL_H

119
roboute/models/appmodel.cpp Normal file
View file

@ -0,0 +1,119 @@
#include "appmodel.h"
AppModel::AppModel(uint64_t p_id, const QString& p_name):
QObject(),
id(p_id),
props(),
commands(),
name(p_name),
log(),
connectable(false),
connected(false),
launchable(false),
launched(false),
editable(false)
{
}
const QString & AppModel::getName() const
{
return name;
}
void AppModel::logMessage(const QString& msg)
{
log.push_back(msg);
emit newLogMessage(msg);
}
QString* AppModel::getHistory() const
{
List::const_iterator itr = log.begin();
List::const_iterator end = log.end();
QString* history = new QString();
for (; itr != end; ++itr) {
history->append(*itr);
}
return history;
}
bool AppModel::getConnectable() const
{
return connectable;
}
bool AppModel::getConnected() const
{
return connected;
}
bool AppModel::getLaunchable() const
{
return launchable && connected;
}
bool AppModel::getLaunched() const
{
return launched;
}
bool AppModel::getEditable() const
{
return editable && !connected;
}
void AppModel::setConnectable(bool value)
{
if (value != connectable) {
connectable = value;
emit changedConnectable(connectable);
}
}
void AppModel::setConnected(bool value)
{
if (value != connected) {
connected = value;
emit changedConnected(connected);
emit changedLaunchable(launchable && connected);
}
}
void AppModel::setLaunchable(bool value)
{
if (value != launchable) {
launchable = value;
emit changedLaunchable(launchable && connected);
}
}
void AppModel::setLaunched(bool value)
{
if (value != launched) {
launched = value;
emit changedLaunched(launched);
}
}
void AppModel::setEditable(bool value)
{
if (value != editable) {
editable = value;
emit changedEditable(editable && !connected);
}
}
void AppModel::clearLog()
{
log.clear();
emit clearedLog();
}
void AppModel::setName(const QString& p_name)
{
name = p_name;
}

61
roboute/models/appmodel.h Normal file
View file

@ -0,0 +1,61 @@
#ifndef APPMODEL_H
#define APPMODEL_H
#include "apppropertiesmodel.h"
#include "appcommandsmodel.h"
#include <QtCore/QObject>
#include <list>
class AppModel : public QObject
{
Q_OBJECT
public:
AppModel(uint64_t p_id, const QString& p_name);
const QString& getName() const;
void setName(const QString& p_name);
void logMessage(const QString& msg);
QString* getHistory() const;
bool getConnectable() const;
bool getConnected() const;
bool getLaunchable() const;
bool getLaunched() const;
bool getEditable() const;
void clearLog();
public:
const uint64_t id;
AppPropertiesModel props;
AppCommandsModel commands;
signals:
void newLogMessage(const QString& msg);
void changedConnectable(bool value);
void changedConnected(bool value);
void changedLaunchable(bool value);
void changedLaunched(bool value);
void changedEditable(bool value);
void clearedLog();
public slots:
void setConnectable(bool value);
void setConnected(bool value);
void setLaunchable(bool value);
void setLaunched(bool value);
void setEditable(bool value);
private:
typedef std::list<QString> List;
QString name;
List log;
bool connectable;
bool connected;
bool launchable;
bool launched;
bool editable;
};
#endif // APPMODEL_H

View file

@ -0,0 +1,143 @@
#include "apppropertiesmodel.h"
AppPropertiesModel::AppPropertiesModel(QObject* parent):
QAbstractTableModel(parent),
index(),
helper(),
map(),
toInsert()
{
}
AppPropertiesModel::~AppPropertiesModel()
{
clear();
}
void AppPropertiesModel::clear()
{
beginResetModel();
Map::iterator itr = map.begin();
Map::iterator end = map.end();
for (; itr != end; ++itr) {
delete itr.value();
}
map.clear();
index.clear();
helper.clear();
endResetModel();
}
int AppPropertiesModel::columnCount(const QModelIndex& parent) const
{
return 2;
}
bool AppPropertiesModel::insertRows(int row, int count, const QModelIndex& parent)
{
if (toInsert.size() != count) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
Index::const_iterator target = index.begin() + row;
for (int i = 0; i < count; ++i) {
List::iterator itr = toInsert.begin();
Map::iterator mItr = map.insert(itr->first, itr->second);
index.insert(target, mItr);
helper.insert(itr->first, row + i);
toInsert.erase(itr);
}
endInsertRows();
return true;
}
bool AppPropertiesModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (row + count > index.size()) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
Index::iterator itr;
Index::iterator beg = index.begin() + row;
Index::iterator end = beg + count;
for (itr = beg; itr != end; ++itr) {
Map::iterator mItr = *itr;
AppProp* prop = *mItr;
IndexHelper::iterator hItr = helper.find(prop->key);
delete prop;
map.erase(mItr);
helper.erase(hItr);
}
index.erase(beg, end);
endRemoveRows();
return true;
}
int AppPropertiesModel::rowCount(const QModelIndex& parent) const
{
return index.size();
}
void AppPropertiesModel::setProp(const QString& key, const QString& value)
{
Map::iterator itr = map.find(key);
if (itr != map.end()) {
itr.value()->value = value;
const QModelIndex ind = QAbstractTableModel::index(*(helper.find(key)), 1);
emit dataChanged(ind, ind);
} else {
AppProp* item = new AppProp{key, value};
toInsert.push_back(Pair(key, item));
insertRows(rowCount(), 1);
}
}
void AppPropertiesModel::removeProp(const QString& key)
{
IndexHelper::iterator itr = helper.find(key);
if (itr != helper.end()) {
removeRows(*itr, 1);
}
}
QVariant AppPropertiesModel::data(const QModelIndex& i, int role) const
{
Map::iterator itr = index[i.row()];
int col = i.column();
switch(role) {
case Qt::DisplayRole:
if (col == 0) {
return itr.key();
} else if (col == 1) {
return itr.value()->value;
}
}
return QVariant();
}
QVariant AppPropertiesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch (orientation) {
case Qt::Horizontal:
switch(role) {
case Qt::DisplayRole:
if (section == 0) {
return "Key";
} else if (section == 1) {
return "Value";
}
}
}
return QVariant();
}

View file

@ -0,0 +1,51 @@
#ifndef APPPROPERTIESMODEL_H
#define APPPROPERTIESMODEL_H
#include <QtCore/QAbstractTableModel>
#include <QtCore/QMap>
#include <QtCore/QVariant>
#include <deque>
#include <list>
class AppPropertiesModel : public QAbstractTableModel
{
Q_OBJECT
struct AppProp
{
QString key;
QString value;
};
public:
AppPropertiesModel(QObject* parent = 0);
~AppPropertiesModel();
void setProp(const QString& key, const QString& value);
void removeProp(const QString& key);
void clear();
QVariant data(const QModelIndex &i, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
private:
typedef QMap<QString, AppProp*> Map;
typedef QMap<QString, uint64_t> IndexHelper;
typedef std::deque<Map::iterator> Index;
typedef std::pair<QString, AppProp*> Pair;
typedef std::list<Pair> List;
Index index;
IndexHelper helper;
Map map;
List toInsert;
};
#endif // APPPROPERTIESMODEL_H

View file

@ -0,0 +1,10 @@
#include "commands.h"
Commands::Commands(const W::Address& address, QObject* parent):
C::Vocabulary(address, parent)
{
}
Commands::~Commands()
{
}

13
roboute/models/commands.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef ROBOUTE_COMMANDS_H
#define ROBOUTE_COMMANDS_H
#include <wController/vocabulary.h>
class Commands : public C::Vocabulary
{
public:
Commands(const W::Address& address, QObject* parent = 0);
~Commands();
};
#endif // ROBOUTE_COMMANDS_H

489
roboute/models/service.cpp Normal file
View file

@ -0,0 +1,489 @@
#include "service.h"
uint64_t Service::lastId = 0;
Service::Service(
uint64_t p_id,
const QString& p_name,
const QString& p_address,
const QString& p_port,
const QString& p_login,
const QString& p_password,
const QString& p_logFile,
const QString& p_command
):
QObject(),
socket(new W::Socket(W::String(u"Roboute"))),
dataSsh(new W::SshSocket(p_login, p_password)),
commandSsh(new W::SshSocket(p_login, p_password)),
attributes(new C::Attributes(W::Address{u"attributes"})),
commands(new C::Vocabulary(W::Address{u"management"})),
login(p_login),
password(p_password),
logFile(p_logFile),
command(p_command),
psResults(),
pid(),
state(Disconnected),
appState(Unknown),
name(p_name),
address(p_address),
port(p_port),
id(p_id)
{
QObject::connect(dataSsh, SIGNAL(opened()), this, SLOT(onDataSshOpened()));
QObject::connect(dataSsh, SIGNAL(closed()), this, SLOT(onSshClosed()));
QObject::connect(dataSsh, SIGNAL(data(const QString&)), this, SLOT(onDataSshData(const QString&)));
QObject::connect(dataSsh, SIGNAL(error(W::SshSocket::Error, const QString&)), this, SLOT(onSshError(W::SshSocket::Error, const QString&)));
QObject::connect(dataSsh, SIGNAL(finished()), this, SLOT(onDataSshFinished()));
QObject::connect(commandSsh, SIGNAL(opened()), this, SLOT(onCommandSshOpened()));
QObject::connect(commandSsh, SIGNAL(closed()), this, SLOT(onSshClosed()));
QObject::connect(commandSsh, SIGNAL(data(const QString&)), this, SLOT(onCommandSshData( const QString&)));
QObject::connect(commandSsh, SIGNAL(error(W::SshSocket::Error, const QString&)), this, SLOT(onSshError(W::SshSocket::Error, const QString&)));
QObject::connect(commandSsh, SIGNAL(finished()), this, SLOT(onCommandSshFinished()));
QObject::connect(socket, SIGNAL(connected()), this, SLOT(onSocketConnected()));
QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
QObject::connect(socket, SIGNAL(error(W::Socket::SocketError, const QString&)), this, SLOT(onSocketError(W::Socket::SocketError, const QString&)));
QObject::connect(attributes, SIGNAL(attributeChange(const W::String&, const W::Object&)),
this, SLOT(onAttrChange(const W::String&, const W::Object&)));
QObject::connect(attributes, SIGNAL(serviceMessage(const QString&)), SIGNAL(serviceMessage(const QString&)));
QObject::connect(commands, SIGNAL(serviceMessage(const QString&)), SIGNAL(serviceMessage(const QString&)));
QObject::connect(commands, SIGNAL(newElement(const W::String&, const W::Object&)), SLOT(onAddCommand(const W::String&, const W::Object&)));
QObject::connect(commands, SIGNAL(removeElement(const W::String&)), SLOT(onRemoveCommand(const W::String&)));
QObject::connect(commands, SIGNAL(clear()), SLOT(onClearCommands()));
}
Service::~Service()
{
delete commands;
delete attributes;
delete commandSsh;
delete dataSsh;
delete socket;
}
Service* Service::create(const QMap<QString, QString>& params)
{
QString name = params["name"];
QString address = params["address"];
QString port = params["port"];
QString login = params["login"];
QString password = params["password"];
QString logFile = params["logFile"];
QString command = params["command"];
Service* srv = new Service(++lastId, name, address, port, login, password, logFile, command);
return srv;
}
Service* Service::fromSerialized(const QMap<QString, QVariant>& params)
{
QString name = params["name"].toString();
QString address = params["address"].toString();
QString port = params["port"].toString();
QString login = params["login"].toString();
QString password = params["password"].toString();
QString logFile = params["logFile"].toString();
QString command = params["command"].toString();
uint64_t id = params["id"].toUInt();
if (id > lastId) {
lastId = id;
}
Service* srv = new Service(id, name, address, port, login, password, logFile, command);
return srv;
}
void Service::onDataSshOpened()
{
if (state == Connecting) {
state = Echo;
dataSsh->execute("echo === Roboute connected === >> " + logFile);
emit serviceMessage("checking log file");
} else {
//TODO;
}
}
void Service::onCommandSshOpened()
{
if (appState == Unknown) {
appState = Checking;
requestPid();
emit serviceMessage("checking if the process launched");
}
}
void Service::onSshClosed()
{
if (state == Disconnected) {
emit serviceMessage("connection clozed");
emit stopped();
emit disconnected();
}
if (state == Disconnecting) {
state = Disconnected;
}
}
void Service::onSshError(W::SshSocket::Error errCode, const QString& msg)
{
emit serviceMessage(msg);
switch (state) {
case Disconnected:
break;
case Connecting:
state = Disconnected;
emit disconnected();
break;
case Echo:
case Listening:
case Connected:
disconnect();
break;
default:
break;
}
}
void Service::onDataSshData(const QString& data)
{
switch (state) {
case Listening:
state = Connected;
emit connected();
emit serviceMessage("first data from log file, connected!");
case Connected:
if (appState == Launching) {
if (data.contains("ready")) {
connectWebsocket();
requestPid();
}
}
emit log(data);
break;
default:
break;
}
}
void Service::onCommandSshData(const QString& data)
{
QStringList list = data.split("\n");
psResults.insert(psResults.end(), list.begin(), list.end());
}
void Service::onCommandSshFinished()
{
switch (appState) {
case Checking:
case WaitingWebSocket:
case Active: //that's very bad!
{
bool found = false;
std::list<QString>::const_iterator itr = psResults.begin();
std::list<QString>::const_iterator end = psResults.end();
QString option;
for (; itr != end; ++itr) {
option = *itr;
if (!option.contains(" grep ") && option.contains(command)) {
found = true;
break;
}
}
if (found) {
QStringList mems = option.split(QRegExp("\\s"));
QStringList::const_iterator mItr = mems.begin();
QStringList::const_iterator mEnd = mems.end();
found = false;
for (; mItr != mEnd; ++mItr) {
QString candidate = *mItr;
if (candidate.contains(QRegExp("\\d{2,}"))) {
pid = candidate;
found = true;
break;
}
}
if (found) {
emit serviceMessage("got the process id: " + pid + ", correct?");
} else {
emit serviceMessage("Couldn't find process id");
}
emit serviceMessage("process seems to be launched");
if (appState == Checking) {
connectWebsocket();
}
} else {
appState = Dead;
emit stopped();
emit serviceMessage("process seems to be not launched");
}
break;
}
case Launching:
emit serviceMessage(QString("process launch command sent,") +
" requesting pid, waiting for specific 'ready' key in log"); //need to do smthing about this
break;
default:
break;
}
}
void Service::connect()
{
if (state == Disconnected) {
dataSsh->open(address);
commandSsh->open(address);
state = Connecting;
emit serviceMessage("connecting to " + address);
emit connecting();
} else {
//TODO;
}
}
void Service::disconnect()
{
if (state != Disconnected) {
state = Disconnecting;
if (appState == Active) {
commands->unsubscribe();
attributes->unsubscribe();
socket->close();
}
pid = "";
psResults.clear();
appState = Unknown;
emit serviceMessage("disconnecting");
emit disconnecting();
emit stopped();
dataSsh->interrupt();
dataSsh->close();
commandSsh->close();
}
}
void Service::onDataSshFinished()
{
switch (state) {
case Echo:
emit serviceMessage("log file checked");
dataSsh->execute("tail -f " + logFile);
state = Listening;
emit serviceMessage("listening to the log file");
break;
default:
break;
}
}
QVariant Service::saveState() const
{
QMap<QString, QVariant> state;
quint64 qid = id;
state.insert("id", qid);
state.insert("login", login);
state.insert("password", password);
state.insert("logFile", logFile);
state.insert("name", name);
state.insert("address", address);
state.insert("port", port);
state.insert("command", command);
return state;
}
void Service::launch()
{
if (state == Connected && appState == Dead) {
appState = Launching;
commandSsh->execute("nohup " + command + " >> " + logFile + " 2>&1 &");
emit launching();
}
}
void Service::stop()
{
if (state == Connected && appState == Active) {
QString file = command.section("/", -1);
commandSsh->execute("kill -s SIGINT " + pid);
appState = Stopping;
emit stopping();
}
}
void Service::onSocketConnected()
{
appState = Active; //this is a fail It's not right!
attributes->subscribe();
commands->subscribe();
emit launched();
}
void Service::onSocketDisconnected()
{
appState = Dead; //this is not correct!
emit stopped();
}
void Service::onSocketError(W::Socket::SocketError err, const QString& msg)
{
emit serviceMessage(msg); //this is not correct!
appState = Dead;
emit stopped();
}
void Service::registerContollers(W::Dispatcher* dp)
{
QObject::connect(socket, SIGNAL(message(const W::Event&)), dp, SLOT(pass(const W::Event&)));
attributes->registerController(dp, socket);
commands->registerController(dp, socket);
}
void Service::unregisterControllers(W::Dispatcher* dp)
{
QObject::disconnect(socket, SIGNAL(message(const W::Event&)), dp, SLOT(pass(const W::Event&)));
commands->unregisterController();
attributes->unregisterController();
}
void Service::requestPid()
{
pid = "";
psResults.clear();
commandSsh->execute("ps -ax | grep '" + command + "'");
}
void Service::connectWebsocket()
{
appState = WaitingWebSocket;
socket->open(W::String(address.toStdString()), W::Uint64(port.toInt()));
emit serviceMessage("trying to reach service by websocket");
}
void Service::onAttrChange(const W::String& key, const W::Object& value)
{
emit attributeChanged(QString::fromStdString(key.toString()), QString::fromStdString(value.toString()));
}
void Service::onAddCommand(const W::String& key, const W::Object& value)
{
QMap<QString, uint64_t> arguments;
const W::Vocabulary& vc = static_cast<const W::Vocabulary&>(value);
const W::Vocabulary& args = static_cast<const W::Vocabulary&>(vc.at(u"arguments"));
W::Vector keys = args.keys();
uint64_t size = keys.length();
for (int i = 0; i < size; ++i) {
const W::String& name = static_cast<const W::String&>(keys.at(i));
const W::Uint64& type = static_cast<const W::Uint64&>(args.at(name));
arguments.insert(QString::fromStdString(name.toString()), type);
}
emit addCommand(QString::fromStdString(key.toString()), arguments);
}
void Service::onRemoveCommand(const W::String& key)
{
emit removeCommand(QString::fromStdString(key.toString()));
}
void Service::onClearCommands()
{
emit clearCommands();
}
void Service::launchCommand(const QString& name, const QMap<QString, QVariant>& args)
{
const W::Vocabulary& val = static_cast<const W::Vocabulary&>(commands->at(W::String(name.toStdString())));
const W::Vocabulary& aT = static_cast<const W::Vocabulary&>(val.at(u"arguments"));
QMap<QString, QVariant>::const_iterator itr = args.begin();
QMap<QString, QVariant>::const_iterator end = args.end();
W::Vocabulary* vc = new W::Vocabulary();
for (; itr != end; ++itr) {
W::String wKey(itr.key().toStdString());
const W::Uint64& wType = static_cast<const W::Uint64&>(aT.at(wKey));
int type = wType;
W::Object* value;
switch (type) {
case 0:
value = new W::String(itr.value().toString().toStdString());
break;
case 2:
value = new W::Uint64(itr.value().toInt());
break;
default:
throw 1;
}
vc->insert(wKey, value);
}
W::Event ev(static_cast<const W::Address&>(val.at(u"address")), vc);
ev.setSenderId(socket->getId());
socket->send(ev);
}
QMap<QString, QString> Service::getData() const
{
QMap<QString, QString> data;
data["name"] = name;
data["address"] = address;
data["port"] = port;
data["login"] = login;
data["password"] = password;
data["logFile"] = logFile;
data["command"] = command;
return data;
}
void Service::passNewData(const QMap<QString, QString> data)
{
if (data.contains("name") && data.value("name") != name) {
name = data.value("name");
emit changeName(name);
}
if (data.contains("address") && data.value("address") != address) {
address = data.value("address");
}
if (data.contains("port") && data.value("port") != port) {
port = data.value("port");
}
if (data.contains("login") && data.value("login") != login) {
login = data.value("login");
dataSsh->setLogin(login);
commandSsh->setLogin(login);
}
if (data.contains("password") && data.value("password") != password) {
password = data.value("password");
dataSsh->setPassword(password);
commandSsh->setPassword(password);
}
if (data.contains("logFile") && data.value("logFile") != logFile) {
logFile = data.value("logFile");
}
if (data.contains("command") && data.value("command") != command) {
command = data.value("command");
}
}

131
roboute/models/service.h Normal file
View file

@ -0,0 +1,131 @@
#ifndef SERVICE_H
#define SERVICE_H
#include <list>
#include <wSocket/socket.h>
#include <wSsh/sshsocket.h>
#include <wType/string.h>
#include <wType/uint64.h>
#include <wController/attributes.h>
#include <wController/vocabulary.h>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QMap>
#include <QtCore/QVariant>
class Service : public QObject
{
Q_OBJECT
private:
Service(uint64_t p_id,
const QString& p_name,
const QString& p_address,
const QString& p_port,
const QString& p_login,
const QString& p_password,
const QString& p_logFile,
const QString& p_command
);
public:
~Service();
static Service* create(const QMap<QString, QString>& params);
static Service* fromSerialized(const QMap<QString, QVariant>& params);
QVariant saveState() const;
void registerContollers(W::Dispatcher* dp);
void unregisterControllers(W::Dispatcher* dp);
QMap<QString, QString> getData() const;
void passNewData(const QMap<QString, QString> data);
private:
enum State {
Disconnected,
Connecting,
Echo,
Listening,
Connected,
Disconnecting
};
enum AppState {
Unknown,
Checking,
Dead,
Launching,
WaitingWebSocket,
Active,
Stopping
};
W::Socket* socket;
W::SshSocket* dataSsh;
W::SshSocket* commandSsh;
C::Attributes* attributes;
C::Vocabulary* commands;
static uint64_t lastId;
QString login;
QString password;
QString logFile;
QString command;
std::list<QString> psResults;
QString pid;
State state;
AppState appState;
void requestPid();
void connectWebsocket();
public:
QString name;
QString address;
QString port;
const uint64_t id;
signals:
void serviceMessage(const QString& msg);
void connecting();
void connected();
void disconnecting();
void disconnected();
void launching();
void launched();
void stopping();
void stopped();
void log(const QString& data);
void attributeChanged(const QString& name, const QString& value);
void addCommand(const QString& name, const QMap<QString, uint64_t>& arguments);
void removeCommand(const QString& name);
void clearCommands();
void changeName(const QString& name);
public slots:
void connect();
void disconnect();
void launch();
void stop();
void launchCommand(const QString& name, const QMap<QString, QVariant>& args);
private slots:
void onDataSshOpened();
void onCommandSshOpened();
void onSshClosed();
void onDataSshData(const QString& data);
void onCommandSshData(const QString& data);
void onSshError(W::SshSocket::Error errCode, const QString& msg);
void onDataSshFinished();
void onCommandSshFinished();
void onSocketConnected();
void onSocketDisconnected();
void onSocketError(W::Socket::SocketError err, const QString& msg);
void onAttrChange(const W::String& key, const W::Object& value);
void onAddCommand(const W::String& key, const W::Object& value);
void onRemoveCommand(const W::String& key);
void onClearCommands();
};
#endif // SERVICE_H

295
roboute/roboute.cpp Normal file
View file

@ -0,0 +1,295 @@
#include "roboute.h"
#include <iostream>
using std::cout;
using std::endl;
Roboute* Roboute::roboute = 0;
Roboute::Roboute(QObject *parent):
QObject(parent),
logger(new W::Logger()),
services(),
dispatcher(new W::Dispatcher())
{
if (roboute != 0)
{
throw SingletonError();
}
Roboute::roboute = this;
dispatcher->registerDefaultHandler(logger);
}
Roboute::~Roboute()
{
QMap<uint64_t, Service*>::iterator beg = services.begin();
QMap<uint64_t, Service*>::iterator end = services.end();
for (; beg != end; ++beg) {
delete *beg;
}
dispatcher->unregisterDefaultHandler(logger);
delete logger;
delete dispatcher;
Roboute::roboute = 0;
}
void Roboute::start()
{
debug("Starting roboute...");
readSettings();
debug("Roboute is ready");
}
void Roboute::stop()
{
debug("Stopping roboute...");
saveSettings();
QMap<uint64_t, Service*>::iterator beg = services.begin();
QMap<uint64_t, Service*>::iterator end = services.end();
for (; beg != end; ++beg) {
Service* srv = *beg;
srv->disconnect();
srv->unregisterControllers(dispatcher);
}
}
void Roboute::debug(std::string str) const
{
cout << str << endl;
QString dbg = str.c_str();
dbg.append("\n");
emit debugMessage(dbg);
}
void Roboute::debug(uint64_t id, const QString& msg) const
{
Service* srv = services[id];
QString dbg = srv->name + ": " + msg;
debug(dbg.toStdString());
}
void Roboute::addService(const QMap<QString, QString>& params)
{
Service* srv = Service::create(params);
addService(srv);
}
void Roboute::removeService(uint64_t id)
{
QMap<uint64_t, Service*>::iterator itr = services.find(id);
if (itr != services.end()) {
Service* srv = *itr;
debug(id, "removing...");
srv->unregisterControllers(dispatcher);
srv->disconnect();
srv->deleteLater();
services.erase(itr);
emit serviceRemoved(id);
}
}
void Roboute::addService(Service* srv)
{
services.insert(srv->id, srv);
connect(srv, SIGNAL(serviceMessage(const QString&)), this, SLOT(onServiceMessage(const QString&)));
connect(srv, SIGNAL(changeName(const QString&)), this, SLOT(onServiceChangeName(const QString&)));
connect(srv, SIGNAL(connecting()), this, SLOT(onServiceConnecting()));
connect(srv, SIGNAL(connected()), this, SLOT(onServiceConnected()));
connect(srv, SIGNAL(disconnecting()), this, SLOT(onServiceDisconnecting()));
connect(srv, SIGNAL(disconnected()), this, SLOT(onServiceDisconnected()));
connect(srv, SIGNAL(launching()), this, SLOT(onServiceLaunching()));
connect(srv, SIGNAL(launched()), this, SLOT(onServiceLaunched()));
connect(srv, SIGNAL(stopping()), this, SLOT(onServiceStopping()));
connect(srv, SIGNAL(stopped()), this, SLOT(onServiceStopped()));
connect(srv, SIGNAL(attributeChanged(const QString&, const QString&)), this, SLOT(onAttributeChanged(const QString&, const QString&)));
connect(srv, SIGNAL(log(const QString&)), this, SLOT(onServiceLog(const QString&)));
connect(srv, SIGNAL(addCommand(const QString&, const QMap<QString, uint64_t>&)), SLOT(onAddCommand(const QString&, const QMap<QString, uint64_t>&)));
connect(srv, SIGNAL(removeCommand(const QString&)), SLOT(onRemoveCommand(const QString&)));
connect(srv, SIGNAL(clearCommands()), SLOT(onClearCommands()));
srv->registerContollers(dispatcher);
emit newService(srv->id, srv->name);
}
void Roboute::connectService(uint64_t id)
{
Service* srv = services[id];
srv->connect();
}
void Roboute::disconnectService(uint64_t id)
{
Service* srv = services[id];
srv->disconnect();
}
void Roboute::launchService(uint64_t id)
{
Service* srv = services[id];
srv->launch();
}
void Roboute::stopService(uint64_t id)
{
Service* srv = services[id];
srv->stop();
}
void Roboute::onServiceMessage(const QString& msg)
{
Service* srv = static_cast<Service*>(sender());
debug(srv->id, msg);
}
void Roboute::onServiceConnecting()
{
Service* srv = static_cast<Service*>(sender());
emit serviceConnecting(srv->id);
}
void Roboute::onServiceConnected()
{
Service* srv = static_cast<Service*>(sender());
emit serviceConnected(srv->id);
}
void Roboute::onServiceDisconnecting()
{
Service* srv = static_cast<Service*>(sender());
emit serviceDisconnecting(srv->id);
}
void Roboute::onServiceDisconnected()
{
Service* srv = static_cast<Service*>(sender());
emit serviceDisconnected(srv->id);
}
void Roboute::onServiceLog(const QString& msg)
{
Service* srv = static_cast<Service*>(sender());
emit log(srv->id, msg);
}
void Roboute::saveSettings() const
{
debug("Saving settings...");
QSettings settings;
settings.beginGroup("services");
QList<QVariant> list;
QMap<uint64_t, Service*>::const_iterator beg = services.begin();
QMap<uint64_t, Service*>::const_iterator end = services.end();
for (; beg != end; ++beg) {
list.push_back((*beg)->saveState());
}
settings.setValue("list", list);
settings.endGroup();
}
void Roboute::readSettings()
{
debug("Reading settings...");
QSettings settings;
QList<QVariant> list = settings.value("services/list").toList();
QList<QVariant>::const_iterator beg = list.begin();
QList<QVariant>::const_iterator end = list.end();
for (; beg != end; ++beg) {
addService(Service::fromSerialized(beg->toMap()));
}
}
void Roboute::onServiceLaunched()
{
Service* srv = static_cast<Service*>(sender());
emit serviceLaunched(srv->id);
}
void Roboute::onServiceLaunching()
{
Service* srv = static_cast<Service*>(sender());
emit serviceLaunching(srv->id);
}
void Roboute::onServiceStopped()
{
Service* srv = static_cast<Service*>(sender());
emit serviceStopped(srv->id);
}
void Roboute::onServiceStopping()
{
Service* srv = static_cast<Service*>(sender());
emit serviceStopping(srv->id);
}
void Roboute::onAttributeChanged(const QString& key, const QString& value)
{
Service* srv = static_cast<Service*>(sender());
emit serviceAttrChange(srv->id, key, value);
}
void Roboute::onAddCommand(const QString& key, const QMap<QString, uint64_t>& arguments)
{
Service* srv = static_cast<Service*>(sender());
emit serviceAddCommand(srv->id, key, arguments);
}
void Roboute::onRemoveCommand(const QString& key)
{
Service* srv = static_cast<Service*>(sender());
emit serviceRemoveCommand(srv->id, key);
}
void Roboute::onClearCommands()
{
Service* srv = static_cast<Service*>(sender());
emit serviceClearCommands(srv->id);
}
void Roboute::launchCommand(uint64_t id, const QString& name, const QMap<QString, QVariant>& args)
{
Service* srv = services[id];
srv->launchCommand(name, args);
}
void Roboute::editService(uint64_t id)
{
Service* srv = services[id];
emit serviceEdit(id, srv->getData());
}
void Roboute::changeService(uint64_t id, const QMap<QString, QString>& params)
{
Service* srv = services[id];
srv->passNewData(params);
}
void Roboute::onServiceChangeName(const QString& name)
{
Service* srv = static_cast<Service*>(sender());
emit serviceChangeName(srv->id, name);
}

115
roboute/roboute.h Normal file
View file

@ -0,0 +1,115 @@
#ifndef ROBOUTE_H
#define ROBOUTE_H
#include <QtCore/QObject>
#include <QtCore/QMap>
#include <QtCore/QList>
#include <QtCore/QVariant>
#include <QtCore/QThread>
#include <QtCore/QSettings>
#include <wType/string.h>
#include <wType/uint64.h>
#include <wType/event.h>
#include <wType/address.h>
#include <wType/vocabulary.h>
#include <wDispatcher/dispatcher.h>
#include <wDispatcher/logger.h>
#include <wDispatcher/handler.h>
#include <utils/exception.h>
#include <models/service.h>
class Roboute: public QObject
{
Q_OBJECT
public:
Roboute(QObject *parent = 0);
~Roboute();
static Roboute* roboute;
private:
W::Logger *logger;
QMap<uint64_t, Service*> services;
public:
W::Dispatcher *dispatcher;
public:
private:
void debug(std::string str) const;
void debug(uint64_t id, const QString& msg) const;
void saveSettings() const;
void readSettings();
void addService(Service* srv);
signals:
void debugMessage(const QString& msg) const;
void log(uint64_t id,const QString& msg);
void newService(uint64_t id, const QString& name);
void serviceChangeName(uint64_t id, const QString& name);
void serviceAttrChange(uint64_t id, const QString& key, const QString& value);
void serviceRemoved(uint64_t id);
void serviceConnected(uint64_t id);
void serviceConnecting(uint64_t id);
void serviceDisconnecting(uint64_t id);
void serviceDisconnected(uint64_t id);
void serviceConnectionFailed(uint64_t id);
void serviceLaunched(uint64_t id);
void serviceStopped(uint64_t id);
void serviceLaunching(uint64_t id);
void serviceStopping(uint64_t id);
void serviceEdit(uint64_t id, const QMap<QString, QString>& params);
void serviceAddCommand(uint64_t id, const QString& key, const QMap<QString, uint64_t>& arguments);
void serviceRemoveCommand(uint64_t id, const QString& key);
void serviceClearCommands(uint64_t id);
public slots:
void start();
void stop();
void addService(const QMap<QString, QString>& params);
void changeService(uint64_t id, const QMap<QString, QString>& params);
void removeService(uint64_t id);
void connectService(uint64_t id);
void disconnectService(uint64_t id);
void launchService(uint64_t id);
void editService(uint64_t id);
void stopService(uint64_t id);
void launchCommand(uint64_t id, const QString& name, const QMap<QString, QVariant>& args);
private slots:
void onServiceMessage(const QString& msg);
void onServiceChangeName(const QString& name);
void onServiceConnecting();
void onServiceConnected();
void onServiceDisconnecting();
void onServiceDisconnected();
void onServiceLaunching();
void onServiceLaunched();
void onServiceStopping();
void onServiceStopped();
void onServiceLog(const QString& msg);
void onAttributeChanged(const QString& key, const QString& value);
void onAddCommand(const QString& key, const QMap<QString, uint64_t>& arguments);
void onRemoveCommand(const QString& key);
void onClearCommands();
private:
class SingletonError:
public Utils::Exception
{
public:
SingletonError():Exception(){}
std::string getMessage() const{return "Roboute is a singleton, there was an attempt to construct it at the second time";}
};
};
#endif // ROBOUTE_H

View file

@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 2.8.12)
project(robouteViews)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
mainview.h
detailedview.h
newappdialogue.h
commandform.h
)
set(SOURCES
mainview.cpp
detailedview.cpp
newappdialogue.cpp
commandform.cpp
)
add_library(robouteViews STATIC ${HEADERS} ${SOURCES})
target_link_libraries(robouteViews Qt5::Core)
target_link_libraries(robouteViews Qt5::Widgets)

View file

@ -0,0 +1,104 @@
#include "commandform.h"
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QPushButton>
CommandForm::CommandForm(const QString& commandName, const AMap& p_args, QWidget* parent):
QDialog(parent),
args(p_args),
editors(),
name(commandName)
{
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QHBoxLayout* buttonsLayout = new QHBoxLayout();
QFormLayout* formLayout = new QFormLayout();
mainLayout->addWidget(new QLabel(name, this));
mainLayout->addLayout(formLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
createForm(formLayout);
createButtons(buttonsLayout);
}
void CommandForm::createButtons(QHBoxLayout* layout)
{
layout->addStretch();
QPushButton* accept = new QPushButton(QIcon::fromTheme("dialog-ok"), tr("OK"), this);
QPushButton* reject = new QPushButton(QIcon::fromTheme("dialog-cancel"), tr("Cancel"), this);
connect(accept, SIGNAL(clicked()), SLOT(accept()));
connect(reject, SIGNAL(clicked()), SLOT(reject()));
layout->addWidget(accept);
layout->addWidget(reject);
}
void CommandForm::createForm(QFormLayout* layout)
{
AMap::const_iterator itr = args.begin();
AMap::const_iterator end = args.end();
for (; itr != end; ++itr) {
bool supportable = false;
QWidget* editor;
switch (itr.value()) {
case 0:
editor = new QLineEdit(this);
supportable = true;
break;
case 2:
QSpinBox* spbox = new QSpinBox(this);
spbox->setMaximum(UINT16_MAX); //TODO what the hell is wrong with this shit?
editor = spbox;
supportable = true;
break;
}
if (supportable) {
layout->addRow(tr(itr.key().toStdString().c_str()), editor);
editors.insert(itr.key(), editor);
}
}
}
QMap<QString, QVariant> CommandForm::getData() const
{
AMap::const_iterator itr = args.begin();
AMap::const_iterator end = args.end();
QMap<QString, QVariant> result;
for (; itr != end; ++itr) {
bool supportable = false;
QWidget* editor = editors.find(itr.key()).value();
switch (itr.value()) {
case 0:
result.insert(itr.key(), static_cast<QLineEdit*>(editor)->text());
supportable = true;
break;
case 2:
result.insert(itr.key(), static_cast<QSpinBox*>(editor)->value());
supportable = true;
break;
}
}
return result;
}
QString CommandForm::getName() const
{
return name;
}

View file

@ -0,0 +1,31 @@
#ifndef COMMANDFORM_H
#define COMMANDFORM_H
#include <QtWidgets/QDialog>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QFormLayout>
#include <QtCore/QVariant>
#include <QtCore/QMap>
class CommandForm : public QDialog
{
Q_OBJECT
typedef QMap<QString, uint64_t> AMap;
public:
CommandForm(const QString& commandName, const AMap& p_args, QWidget* parent = 0);
QMap<QString, QVariant> getData() const;
QString getName() const;
private:
AMap args;
QMap<QString, QWidget*> editors;
QString name;
void createForm(QFormLayout* layout);
void createButtons(QHBoxLayout* layout);
};
#endif // COMMANDFORM_H

View file

@ -0,0 +1,321 @@
#include "detailedview.h"
#include <QtWidgets/QHeaderView>
DetailedView::DetailedView(QWidget* parent):
QWidget(parent),
layout(new QGridLayout(this)),
topPanel(new QHBoxLayout()),
logArea(new QTextEdit(this)),
splitter(new QSplitter(this)),
dock(new QWidget(this)),
props(new QTableView(dock)),
commands(new QListView(dock)),
connectBtn(new QPushButton(QIcon::fromTheme("state-ok"), "", this)),
launchBtn(new QPushButton(QIcon::fromTheme("system-run"), "", this)),
clearBtn(new QPushButton(QIcon::fromTheme("trash-empty"), "", this)),
removeBtn(new QPushButton(QIcon::fromTheme("delete"), "", this)),
editBtn(new QPushButton(QIcon::fromTheme("edit-rename"), "", this)),
connected(false),
launched(false),
propsShown(false),
commandsShown(false),
model(0)
{
setLayout(layout);
logArea->setReadOnly(true);
layout->addLayout(topPanel, 0, 0, 1, 1);
layout->addWidget(splitter, 1, 0, 1, 1);
splitter->addWidget(logArea);
splitter->addWidget(dock);
QHBoxLayout* lay = new QHBoxLayout();
dock->setLayout(lay);
lay->addWidget(props);
lay->addWidget(commands);
lay->setContentsMargins(0,0,0,0);
props->verticalHeader()->hide();
props->horizontalHeader()->setStretchLastSection(true);
props->setCornerButtonEnabled(false);
props->setShowGrid(false);
props->hide();
commands->hide();
dock->hide();
connectBtn->setToolTip(tr("Connect"));
connectBtn->setEnabled(false);
launchBtn->setToolTip(tr("Launch"));
launchBtn->setEnabled(false);
clearBtn->setToolTip(tr("Clear log"));
clearBtn->setEnabled(false);
removeBtn->setToolTip(tr("Remove"));
removeBtn->setEnabled(false);
editBtn->setToolTip(tr("Edit"));
editBtn->setEnabled(false);
QObject::connect(connectBtn, SIGNAL(clicked()), this, SLOT(onConnectClick()));
QObject::connect(launchBtn, SIGNAL(clicked()), this, SLOT(onLaunchClick()));
QObject::connect(clearBtn, SIGNAL(clicked()), this, SLOT(onClearClick()));
QObject::connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveClick()));
QObject::connect(editBtn, SIGNAL(clicked()), this, SLOT(onEditClick()));
QObject::connect(commands, SIGNAL(doubleClicked(const QModelIndex)), SLOT(onCommandDoubleClicked(const QModelIndex)));
topPanel->addWidget(connectBtn);
topPanel->addWidget(launchBtn);
topPanel->addWidget(clearBtn);
topPanel->addStretch();
topPanel->addWidget(editBtn);
topPanel->addWidget(removeBtn);
layout->setContentsMargins(0,0,0,0);
}
void DetailedView::appendMessage(const QString& msg)
{
QStringList list = msg.split('\n');
QStringList::const_iterator itr = list.begin();
QStringList::const_iterator end = list.end();
for (;itr != end; ++itr) {
QString str = *itr;
if (str != "") {
logArea->append(*itr);
}
}
}
void DetailedView::clear()
{
logArea->clear();
connectBtn->setToolTip(tr("Connect"));
connectBtn->setEnabled(false);
connectBtn->setIcon(QIcon::fromTheme("state-ok"));
launchBtn->setToolTip(tr("Launch"));
launchBtn->setEnabled(false);
launchBtn->setIcon(QIcon::fromTheme("kt-start"));
clearBtn->setEnabled(false);
removeBtn->setEnabled(false);
connected = false;
launched = false;
}
void DetailedView::setConnectable(bool value)
{
connectBtn->setEnabled(value);
}
void DetailedView::setConnected(bool value)
{
if (connected != value) {
connected = value;
if (connected) {
connectBtn->setToolTip(tr("Disonnect"));
connectBtn->setIcon(QIcon::fromTheme("state-error"));
} else {
connectBtn->setToolTip(tr("Connect"));
connectBtn->setIcon(QIcon::fromTheme("state-ok"));
}
}
}
void DetailedView::setLaunchable(bool value)
{
launchBtn->setEnabled(value);
}
void DetailedView::setLaunched(bool value)
{
if (launched != value) {
launched = value;
if (launched) {
launchBtn->setToolTip(tr("Stop"));
launchBtn->setIcon(QIcon::fromTheme("kt-stop"));
} else {
launchBtn->setToolTip(tr("Launch"));
launchBtn->setIcon(QIcon::fromTheme("kt-start"));
}
}
}
void DetailedView::onConnectClick()
{
if (model == 0) {
return;
}
if (connected) {
emit disconnect(model->id);
} else {
emit connect(model->id);
}
}
void DetailedView::onLaunchClick()
{
if (model == 0) {
return;
}
if (launched) {
emit stop(model->id);
} else {
emit launch(model->id);
}
}
void DetailedView::onRemoveClick()
{
if (model == 0) {
return;
}
emit remove(model->id);
}
void DetailedView::onEditClick()
{
if (model == 0) {
return;
}
emit edit(model->id);
}
void DetailedView::setRemovable(bool value)
{
removeBtn->setEnabled(value);
}
void DetailedView::setEditable(bool value)
{
editBtn->setEnabled(value);
}
void DetailedView::setModel(AppModel* p_model)
{
if (model != 0) {
clearModel();
}
model = p_model;
QString* history = model->getHistory();
appendMessage(*history);
setConnectable(model->getConnectable());
setConnected(model->getConnected());
setLaunchable(model->getLaunchable());
setLaunched(model->getLaunched());
setRemovable(model->id != 0);
setEditable(model->getEditable());
clearBtn->setEnabled(true);
delete history;
QObject::connect(model, SIGNAL(newLogMessage(const QString&)), this, SLOT(appendMessage(const QString&)));
QObject::connect(model, SIGNAL(changedConnectable(bool)), this, SLOT(setConnectable(bool)));
QObject::connect(model, SIGNAL(changedConnected(bool)), this, SLOT(setConnected(bool)));
QObject::connect(model, SIGNAL(changedLaunchable(bool)), this, SLOT(setLaunchable(bool)));
QObject::connect(model, SIGNAL(changedLaunched(bool)), this, SLOT(setLaunched(bool)));
QObject::connect(model, SIGNAL(changedEditable(bool)), this, SLOT(setEditable(bool)));
QObject::connect(model, SIGNAL(clearedLog()), this, SLOT(clearedLog()));
QItemSelectionModel *m1 = props->selectionModel();
props->setModel(&model->props);
delete m1;
QItemSelectionModel *m2 = commands->selectionModel();
commands->setModel(&model->commands);
delete m2;
}
void DetailedView::clearModel()
{
if (model != 0) {
clear();
QObject::disconnect(model, SIGNAL(newLogMessage(const QString&)), this, SLOT(appendMessage(const QString&)));
QObject::disconnect(model, SIGNAL(changedConnectable(bool)), this, SLOT(setConnectable(bool)));
QObject::disconnect(model, SIGNAL(changedConnected(bool)), this, SLOT(setConnected(bool)));
QObject::disconnect(model, SIGNAL(changedLaunchable(bool)), this, SLOT(setLaunchable(bool)));
QObject::disconnect(model, SIGNAL(changedLaunched(bool)), this, SLOT(setLaunched(bool)));
QObject::disconnect(model, SIGNAL(clearedLog()), this, SLOT(clearedLog()));
model = 0;
}
}
void DetailedView::saveSettings()
{
QSettings settings;
settings.beginGroup("detailedView");
settings.setValue("splitterState", splitter->saveState());
settings.setValue("propsHeaderState", props->horizontalHeader()->saveState());
settings.endGroup();
}
void DetailedView::readSettings()
{
QSettings settings;
splitter->restoreState(settings.value("detailedView/splitterState").toByteArray());
props->horizontalHeader()->restoreState(settings.value("detailedView/propsHeaderState").toByteArray());
}
void DetailedView::showAttrs(bool value)
{
if (value) {
props->show();
} else {
props->hide();
}
propsShown = value;
checkDock();
}
void DetailedView::showCommands(bool value)
{
if (value) {
commands->show();
} else {
commands->hide();
}
commandsShown = value;
checkDock();
}
void DetailedView::checkDock()
{
if (commandsShown || propsShown) {
dock->show();
} else {
dock->hide();
}
}
void DetailedView::onCommandDoubleClicked(const QModelIndex& index)
{
if (model == 0) {
return;
}
emit launchCommand(model->id, index.data().toString());
}
uint64_t DetailedView::getModelId()
{
if (model == 0) {
throw 1;
}
return model->id;
}
void DetailedView::onClearClick()
{
if (model == 0) {
return;
}
emit clearLog(model->id);
}
void DetailedView::clearedLog()
{
logArea->clear();
}

View file

@ -0,0 +1,86 @@
#ifndef DETAILEDVIEW_H
#define DETAILEDVIEW_H
#include "../models/appmodel.h"
#include <QtCore/QSettings>
#include <QtWidgets/QWidget>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QTableView>
#include <QtWidgets/QListView>
#include <QtWidgets/QSplitter>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QDockWidget>
class DetailedView : public QWidget
{
Q_OBJECT
public:
DetailedView(QWidget* parent = 0);
void setModel(AppModel* p_model);
void clearModel();
uint64_t getModelId();
void saveSettings();
void readSettings();
void showAttrs(bool value);
void showCommands(bool value);
private:
QGridLayout* layout;
QHBoxLayout* topPanel;
QTextEdit* logArea;
QSplitter* splitter;
QWidget* dock;
QTableView* props;
QListView* commands;
QPushButton* connectBtn;
QPushButton* launchBtn;
QPushButton* clearBtn;
QPushButton* removeBtn;
QPushButton* editBtn;
bool connected;
bool launched;
bool propsShown;
bool commandsShown;
AppModel* model;
public slots:
void appendMessage(const QString& msg);
void clear();
void setConnectable(bool value);
void setConnected(bool value);
void setLaunchable(bool value);
void setLaunched(bool value);
void setRemovable(bool value);
void setEditable(bool value);
void clearedLog();
signals:
void connect(uint64_t id);
void disconnect(uint64_t id);
void launch(uint64_t id);
void stop(uint64_t id);
void remove(uint64_t id);
void edit(uint64_t id);
void launchCommand(uint64_t id, const QString& name);
void clearLog(uint64_t id);
private slots:
void onConnectClick();
void onLaunchClick();
void onClearClick();
void onRemoveClick();
void onEditClick();
void checkDock();
void onCommandDoubleClicked(const QModelIndex& index);
};
#endif // DETAILEDVIEW_H

View file

@ -0,0 +1,59 @@
#include "mainview.h"
MainView::MainView(QAbstractListModel* model, QWidget* parent):
QWidget(parent),
splitter(new QSplitter(this)),
list(new QListView(this)),
details(new DetailedView(this)),
detailed(false)
{
QGridLayout* layout = new QGridLayout();
setLayout(layout);
//AppListItemDelegate* dlg = new AppListItemDelegate(this);
//list->setItemDelegate(dlg);
list->setModel(model);
layout->addWidget(splitter, 0, 0, 1, 1);
splitter->addWidget(list);
splitter->addWidget(details);
details->hide();
}
void MainView::hideDetails()
{
if (detailed) {
detailed = false;
details->hide();
}
}
void MainView::showDetails()
{
if (!detailed) {
detailed = true;
details->show();
}
}
void MainView::saveSettings()
{
QSettings settings;
settings.beginGroup("view");
settings.setValue("splitterState", splitter->saveState());
settings.endGroup();
details->saveSettings();
}
void MainView::readSettings()
{
QSettings settings;
splitter->restoreState(settings.value("view/splitterState").toByteArray());
details->readSettings();
}

40
roboute/views/mainview.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QtWidgets/QWidget>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QSplitter>
#include <QtWidgets/QListView>
#include <QtCore/QAbstractListModel>
#include <QtWidgets/QPlainTextEdit>
#include <QtCore/QSettings>
//#include "applistitemdelegate.h"
#include "detailedview.h"
class MainView : public QWidget
{
Q_OBJECT
public:
MainView(QAbstractListModel* model, QWidget* parent = 0);
public:
QSplitter* splitter;
QListView* list;
DetailedView* details;
void saveSettings();
void readSettings();
private:
bool detailed;
public:
void showDetails();
void hideDetails();
};
#endif // MAINVIEW_H

View file

@ -0,0 +1,114 @@
#include "newappdialogue.h"
NewAppDialogue::NewAppDialogue(QWidget* parent):
QDialog(parent),
name(new QLineEdit(this)),
address(new QLineEdit(this)),
port(new QLineEdit(this)),
login(new QLineEdit(this)),
pass(new QLineEdit(this)),
log(new QLineEdit(this)),
command(new QLineEdit(this))
{
construct();
}
NewAppDialogue::NewAppDialogue(const QMap<QString, QString>& data, QWidget* parent):
QDialog(parent),
name(new QLineEdit(this)),
address(new QLineEdit(this)),
port(new QLineEdit(this)),
login(new QLineEdit(this)),
pass(new QLineEdit(this)),
log(new QLineEdit(this)),
command(new QLineEdit(this))
{
construct();
if (data.contains("name")) {
name->setText(data.value("name"));
}
if (data.contains("address")) {
address->setText(data.value("address"));
}
if (data.contains("port")) {
port->setText(data.value("port"));
}
if (data.contains("login")) {
login->setText(data.value("login"));
}
if (data.contains("password")) {
pass->setText(data.value("password"));
}
if (data.contains("logFile")) {
log->setText(data.value("logFile"));
}
if (data.contains("command")) {
command->setText(data.value("command"));
}
}
void NewAppDialogue::construct()
{
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QHBoxLayout* buttonsLayout = new QHBoxLayout();
QFormLayout* formLayout = new QFormLayout();
mainLayout->addLayout(formLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
createButtons(buttonsLayout);
createForm(formLayout);
}
void NewAppDialogue::createButtons(QHBoxLayout* layout)
{
layout->addStretch();
QPushButton* accept = new QPushButton(QIcon::fromTheme("dialog-ok"), tr("OK"), this);
QPushButton* reject = new QPushButton(QIcon::fromTheme("dialog-cancel"), tr("Cancel"), this);
connect(accept, SIGNAL(clicked()), SLOT(accept()));
connect(reject, SIGNAL(clicked()), SLOT(reject()));
layout->addWidget(accept);
layout->addWidget(reject);
}
void NewAppDialogue::createForm(QFormLayout* layout)
{
pass->setEchoMode(QLineEdit::Password);
layout->addRow(tr("Name"), name);
layout->addRow(tr("Server address"), address);
layout->addRow(tr("Service port"), port);
layout->addRow(tr("ssh login"), login);
layout->addRow(tr("Password"), pass);
layout->addRow(tr("Log file"), log);
layout->addRow(tr("Command"), command);
}
QMap<QString, QString> NewAppDialogue::getData() const
{
QMap<QString, QString> map;
map.insert("name", name->text());
map.insert("address", address->text());
map.insert("port", port->text());
map.insert("login", login->text());
map.insert("password", pass->text());
map.insert("logFile", log->text());
map.insert("command", command->text());
return map;
}

View file

@ -0,0 +1,37 @@
#ifndef NEWAPPDIALOGUE_H
#define NEWAPPDIALOGUE_H
#include <QtWidgets/QDialog>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QLineEdit>
#include <QtCore/QMap>
class NewAppDialogue : public QDialog
{
Q_OBJECT
public:
NewAppDialogue(QWidget* parent = 0);
NewAppDialogue(const QMap<QString, QString>& data, QWidget* parent = 0);
QMap<QString, QString> getData() const;
private:
QLineEdit* name;
QLineEdit* address;
QLineEdit* port;
QLineEdit* login;
QLineEdit* pass;
QLineEdit* log;
QLineEdit* command;
private:
void construct();
void createButtons(QHBoxLayout* layout);
void createForm(QFormLayout* layout);
};
#endif // NEWAPPDIALOGUE_H