Add test for embeded mode

This commit is contained in:
Oleg Korshul
2020-10-31 14:45:54 +03:00
parent 8b523427bf
commit 0c0124a9e0
4 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,35 @@
#-------------------------------------------------
#
# Project created by QtCreator 2020-10-30T15:22:04
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = DesktopInject
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
win32 {
LIBS += -ladvapi32
}

View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View File

@ -0,0 +1,164 @@
#include "mainwindow.h"
#include <QDebug>
#include <QtGlobal>
#ifdef Q_OS_WIN
#include "windows.h"
#endif
CProcessEditor::CProcessEditor(const QString& sPath, const OpenFileType& type, const int& nId) : QProcess(nullptr)
{
m_nId = nId;
m_sPath = sPath;
m_eType = type;
}
void CProcessEditor::StartEditor()
{
switch (m_eType)
{
case oftEdit:
m_sParams = "--edit=";
break;
case oftView:
m_sParams = "--view=";
break;
case oftReview:
m_sParams = "--review=";
break;
default:
break;
}
QObject::connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotFinish(int,QProcess::ExitStatus)));
QStringList args;
args.push_back("--single-window-app");
args.push_back(m_sParams + m_sPath);
QString sApplicationPath;
#ifdef Q_OS_WIN
wchar_t sBuffer[2048];
DWORD dwBufferSize = sizeof(sBuffer);
QString sApplicationDir;
HKEY hKey = 0;
HKEY hRoot = HKEY_LOCAL_MACHINE;
if (ERROR_SUCCESS == RegOpenKeyExW(hRoot, L"SOFTWARE\\ONLYOFFICE\\DesktopEditors", 0, KEY_READ, &hKey))
{
if (ERROR_SUCCESS == RegQueryValueExW(hKey, L"AppPath", 0, NULL, (LPBYTE)sBuffer, &dwBufferSize))
{
std::wstring sValue = sBuffer;
sApplicationDir = QString::fromStdWString(sValue);
sApplicationPath = sApplicationDir + "\\editors.exe";
}
}
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH", sApplicationDir + "\\converter;" + env.value("PATH"));
setProcessEnvironment(env);
#endif
#ifdef Q_OS_LINUX
return "onlyoffice-desktopeditors";
#endif
start(sApplicationPath, args);
}
void CProcessEditor::slotFinish(int nCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "File: " << m_sPath << ";; ExitCode: " << nCode;
emit signalFinish(m_nId);
}
CSubProcesses::CSubProcesses()
{
m_nCounterId = 0;
}
CProcessEditor* CSubProcesses::Create(const QString& sPath, const OpenFileType& type)
{
++m_nCounterId;
CProcessEditor* pProcess = new CProcessEditor(sPath, type, m_nCounterId);
QObject::connect(pProcess, &CProcessEditor::signalFinish, this, &CSubProcesses::slotFinish);
m_mapProcesses.insert(m_nCounterId, pProcess);
pProcess->StartEditor();
return pProcess;
}
void CSubProcesses::CloseAll()
{
QList<CProcessEditor*> arProcesses = m_mapProcesses.values();
int nCount = arProcesses.count();
for (int nI = 0; nI < nCount; ++nI)
{
CProcessEditor* pProcess = arProcesses.at(nI);
pProcess->close();
}
m_mapProcesses.clear();
}
void CSubProcesses::slotFinish(int nId)
{
CProcessEditor* pProcess = m_mapProcesses.value(nId);
if (pProcess)
{
m_mapProcesses.remove(nId);
pProcess->deleteLater();
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
m_pButtonOpenEdit = new QPushButton(this);
m_pButtonOpenEdit->setText("Open in editor");
m_pButtonOpenView = new QPushButton(this);
m_pButtonOpenView->setText("Open in viewer");
QObject::connect(m_pButtonOpenEdit, &QPushButton::clicked, this, &MainWindow::pushButtonOpenEdit);
QObject::connect(m_pButtonOpenView, &QPushButton::clicked, this, &MainWindow::pushButtonOpenView);
setGeometry(200, 200, 800, 600);
calculatePlaces();
}
MainWindow::~MainWindow()
{
}
void MainWindow::pushButtonOpenEdit()
{
QString sPath = QFileDialog::getOpenFileName();
if (sPath.isEmpty())
return;
m_oProcesses.Create(sPath, oftEdit);
}
void MainWindow::pushButtonOpenView()
{
QString sPath = QFileDialog::getOpenFileName();
if (sPath.isEmpty())
return;
m_oProcesses.Create(sPath, oftView);
}
void MainWindow::calculatePlaces()
{
int nButtonW = 100;
int nButtonH = 40;
int nBetween = 40;
int nX = (width() - nButtonW) >> 1;
int nY = (height() - (nButtonH + nBetween + nButtonH)) >> 1;
m_pButtonOpenEdit->setGeometry(nX, nY, nButtonW, nButtonH);
m_pButtonOpenView->setGeometry(nX, nY + nButtonH + nBetween, nButtonW, nButtonH);
}
void MainWindow::resizeEvent(QResizeEvent*)
{
calculatePlaces();
}
void MainWindow::closeEvent(QCloseEvent*)
{
m_oProcesses.CloseAll();
}

View File

@ -0,0 +1,79 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QFileDialog>
#include <QProcess>
#include <QResizeEvent>
#include <QCloseEvent>
enum OpenFileType
{
oftEdit = 1,
oftView = 2,
oftReview = 3
};
class CProcessEditor : public QProcess
{
Q_OBJECT
public:
QString m_sPath;
QString m_sParams;
OpenFileType m_eType;
int m_nId;
public:
CProcessEditor(const QString& sPath, const OpenFileType& type, const int& nId);
void StartEditor();
signals:
void signalFinish(int id);
public slots:
void slotFinish(int nCode, QProcess::ExitStatus exitStatus);
};
class CSubProcesses : public QProcess
{
Q_OBJECT
private:
QMap<int, CProcessEditor*> m_mapProcesses;
int m_nCounterId;
public:
CSubProcesses();
CProcessEditor* Create(const QString& sPath, const OpenFileType& type);
void CloseAll();
public slots:
void slotFinish(int nId);
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPushButton* m_pButtonOpenEdit;
QPushButton* m_pButtonOpenView;
CSubProcesses m_oProcesses;
public slots:
void pushButtonOpenEdit();
void pushButtonOpenView();
public:
void calculatePlaces();
void resizeEvent(QResizeEvent*);
void closeEvent(QCloseEvent*);
};
#endif // MAINWINDOW_H