From fee437d5714d4ae5642c7f7301a6d8d5f877ba9e Mon Sep 17 00:00:00 2001
From: "konstantin.papizh" <konstantin.papizh@demlabs.net>
Date: Fri, 12 Jul 2019 14:24:39 +0300
Subject: [PATCH] Build Dashboard under Windows OS

---
 KelvinDashboardGUI/DapServiceClient.h         |  3 ++
 .../DapServiceClientNativeWin.cpp             | 30 +++++++++++++
 .../DapServiceClientNativeWin.h               | 19 ++++++++
 KelvinDashboardGUI/KelvinDashboardGUI.pro     |  4 ++
 KelvinDashboardService/DapChainLogHandler.cpp |  2 +-
 .../DapChainWalletHandler.cpp                 | 44 ++++++++++++++++---
 .../KelvinDashboardService.pro                |  2 +
 KelvinDashboardService/main.cpp               |  4 +-
 8 files changed, 99 insertions(+), 9 deletions(-)
 create mode 100644 KelvinDashboardGUI/DapServiceClientNativeWin.cpp
 create mode 100644 KelvinDashboardGUI/DapServiceClientNativeWin.h

diff --git a/KelvinDashboardGUI/DapServiceClient.h b/KelvinDashboardGUI/DapServiceClient.h
index 8f9763363..3e21f0c3b 100644
--- a/KelvinDashboardGUI/DapServiceClient.h
+++ b/KelvinDashboardGUI/DapServiceClient.h
@@ -10,6 +10,9 @@
 #if defined(Q_OS_LINUX)
 #include "DapServiceClientNativeLinux.h"
 typedef class DapServiceClientNativeLinux DapServiceClientNative;
+#elif defined(Q_OS_WIN)
+#include "DapServiceClientNativeWin.h"
+typedef class DapServiceClientNativeWin DapServiceClientNative;
 #endif
 
 typedef QLocalSocket DapUiSocket;
diff --git a/KelvinDashboardGUI/DapServiceClientNativeWin.cpp b/KelvinDashboardGUI/DapServiceClientNativeWin.cpp
new file mode 100644
index 000000000..724c1f0e6
--- /dev/null
+++ b/KelvinDashboardGUI/DapServiceClientNativeWin.cpp
@@ -0,0 +1,30 @@
+#include "DapServiceClientNativeWin.h"
+
+DapServiceClientNativeWin::DapServiceClientNativeWin()
+{
+}
+
+bool DapServiceClientNativeWin::isServiceRunning()
+{
+    return true;
+}
+
+DapServiceError DapServiceClientNativeWin::serviceInstallAndRun() {
+    return DapServiceError::NO_ERRORS;
+}
+
+DapServiceError DapServiceClientNativeWin::serviceStart() {
+    return DapServiceError::NO_ERRORS;
+}
+
+DapServiceError DapServiceClientNativeWin::serviceRestart() {
+    return DapServiceError::NO_ERRORS;
+}
+
+DapServiceError DapServiceClientNativeWin::serviceStop() {
+    return DapServiceError::NO_ERRORS;
+}
+
+DapServiceClientNativeWin::~DapServiceClientNativeWin() {
+
+}
diff --git a/KelvinDashboardGUI/DapServiceClientNativeWin.h b/KelvinDashboardGUI/DapServiceClientNativeWin.h
new file mode 100644
index 000000000..4a729fe70
--- /dev/null
+++ b/KelvinDashboardGUI/DapServiceClientNativeWin.h
@@ -0,0 +1,19 @@
+#ifndef DAPSERVICECLIENTNATIVEWIN_H
+#define DAPSERVICECLIENTNATIVEWIN_H
+
+#include "DapServiceClientNativeAbstract.h"
+
+class DapServiceClientNativeWin : public DapServiceClientNativeAbstract
+{
+public:
+    DapServiceClientNativeWin();
+    ~DapServiceClientNativeWin() override;
+    bool isServiceRunning() override;
+    DapServiceError serviceStart() override;
+    DapServiceError serviceRestart() override;
+
+    DapServiceError serviceStop() override;
+    DapServiceError serviceInstallAndRun() override;
+};
+
+#endif // DAPSERVICECLIENTNATIVEWIN_H
diff --git a/KelvinDashboardGUI/KelvinDashboardGUI.pro b/KelvinDashboardGUI/KelvinDashboardGUI.pro
index 0e5a7c2d1..6d5fa0060 100755
--- a/KelvinDashboardGUI/KelvinDashboardGUI.pro
+++ b/KelvinDashboardGUI/KelvinDashboardGUI.pro
@@ -18,9 +18,11 @@ VER_PAT = 0
 
 win32 {
     VERSION = $${VER_MAJ}.$${VER_MIN}.$$VER_PAT
+    DEFINES += CLI_PATH=\\\"./kelvin-node-cli.exe\\\"
 }
 else {
     VERSION = $$VER_MAJ\.$$VER_MIN\-$$VER_PAT
+    DEFINES += CLI_PATH=\\\"/opt/kelvin-node/bin/kelvin-node-cli\\\"
 }
 
 # The following define makes your compiler emit warnings if you use
@@ -53,6 +55,7 @@ SOURCES += \
     DapCommandController.cpp \
     DapServiceClientNativeAbstract.cpp \
     DapServiceClientNativeLinux.cpp \
+    DapServiceClientNativeWin.cpp \
     DapChainWalletsModel.cpp
 
 RESOURCES += qml.qrc
@@ -84,6 +87,7 @@ HEADERS += \
     DapCommandController.h \
     DapServiceClientNativeAbstract.h \
     DapServiceClientNativeLinux.h \
+    DapServiceClientNativeWin.h \
     DapChainWalletsModel.h
 
 include (../libdap/libdap.pri)
diff --git a/KelvinDashboardService/DapChainLogHandler.cpp b/KelvinDashboardService/DapChainLogHandler.cpp
index 645053a42..e484d3be0 100755
--- a/KelvinDashboardService/DapChainLogHandler.cpp
+++ b/KelvinDashboardService/DapChainLogHandler.cpp
@@ -26,7 +26,7 @@ QStringList DapChainLogHandler::request(int aiTimeStamp, int aiRowCount)
 {
     QByteArray result;
     QProcess process;
-    process.start(QString("%1 print_log ts_after %2 limit %3").arg("/opt/kelvin-node/bin/kelvin-node-cli").arg(aiTimeStamp).arg(aiRowCount));
+    process.start(QString("%1 print_log ts_after %2 limit %3").arg(CLI_PATH).arg(aiTimeStamp).arg(aiRowCount));
     process.waitForFinished(-1);
     result = process.readAll();
 
diff --git a/KelvinDashboardService/DapChainWalletHandler.cpp b/KelvinDashboardService/DapChainWalletHandler.cpp
index 993490f90..00e39b700 100755
--- a/KelvinDashboardService/DapChainWalletHandler.cpp
+++ b/KelvinDashboardService/DapChainWalletHandler.cpp
@@ -17,7 +17,7 @@ QStringList DapChainWalletHandler::createWallet(const QString &asNameWallet)
 {
     QByteArray result;
     QProcess process;
-    process.start(QString("%1 wallet new -w %2").arg("/opt/kelvin-node/bin/kelvin-node-cli").arg(asNameWallet));
+    process.start(QString("%1 wallet new -w %2").arg(CLI_PATH).arg(asNameWallet));
     process.waitForFinished(-1);
     result = process.readAll();
     QStringList list;
@@ -40,7 +40,7 @@ QMap<QString, QVariant> DapChainWalletHandler::getWallets()
 {
     QMap<QString, QVariant> map;
     QProcess process;
-    process.start(QString("%1 wallet list").arg("/opt/kelvin-node/bin/kelvin-node-cli"));
+    process.start(QString("%1 wallet list").arg(CLI_PATH));
     process.waitForFinished(-1);
     QString str = QString::fromLatin1(process.readAll());
     qDebug() << "ZDES`" << str;
@@ -70,9 +70,40 @@ QMap<QString, QVariant> DapChainWalletHandler::getWallets()
 QStringList DapChainWalletHandler::getWalletInfo(const QString &asNameWallet)
 {
     QProcess process;
-    process.start(QString("%1 wallet info -w %2 -net private").arg("/opt/kelvin-node/bin/kelvin-node-cli").arg(asNameWallet));
+    process.start(QString("%1 wallet info -w %2 -net kelvin-testnet").arg(CLI_PATH).arg(asNameWallet));
     process.waitForFinished(-1);
+    char* response = process.readAll().data();
+    //qDebug() << response;
     QStringList list;
+#ifdef Q_OS_WIN32
+    char *context = nullptr;
+    char *data = nullptr;
+    data = strtok_r(response, ":", &context);
+    if (strcmp(data, "wallet") != 0) {
+        data = strtok_r(response, ":", &context);
+    }
+    data = strtok_r(context+1, "\r", &context);
+    list.append(QString(data));
+    data = strtok_r(context+1, ":", &context);
+    data = strtok_r(context+1, "\r", &context);
+    list.append(QString(data));
+    data = strtok_r(context+1, ":", &context);
+    list.append(QString(data));
+    data = strtok_r(context+4, "\r", &context);
+
+    char *subctx;
+    char *subdata;
+    if (strlen(data) > 2) {
+        subdata = strtok_r(data+1, " ", &subctx);
+    } else {
+        subdata = strtok_r(data, " ", &subctx);
+    }
+    list.append(QString(subdata));
+    subdata = strtok_r(subctx, " ", &subctx);
+    list.append(QString(subdata));
+    subdata = strtok_r(subctx, "\r", &subctx);
+    list.append(QString(subdata));
+#else
     QString str = QString::fromLatin1(process.readAll()).replace("\\", "\\\\");
 
     QRegExp rx("[(:\\)\\t]{1,1}([^\\\\\\n\\t]+)[\\\\(|\\n|\\r]{1,1}");
@@ -84,6 +115,7 @@ QStringList DapChainWalletHandler::getWalletInfo(const QString &asNameWallet)
         list.append(rx.cap(1));
         pos += rx.matchedLength();
     }
+#endif
     qDebug() << list;
     return list;
 }
@@ -93,8 +125,8 @@ QString DapChainWalletHandler::sendToken(const QString &asSendWallet, const QStr
     QString answer;
     qInfo() << QString("sendTokenTest(%1, %2, %3, %4)").arg(asSendWallet).arg(asAddressReceiver).arg(asToken).arg(aAmount);
     QProcess processCreate;
-    processCreate.start(QString("%1 tx_create -net private -chain gdb -from_wallet %2 -to_addr %3 -token %4 -value %5")
-                  .arg("/opt/kelvin-node/bin/kelvin-node-cli")
+    processCreate.start(QString("%1 tx_create -net kelvin-testnet -chain gdb -from_wallet %2 -to_addr %3 -token %4 -value %5")
+                  .arg(CLI_PATH)
                   .arg(asSendWallet)
                   .arg(asAddressReceiver)
                   .arg(asToken)
@@ -105,7 +137,7 @@ QString DapChainWalletHandler::sendToken(const QString &asSendWallet, const QStr
     if(!(resultCreate.isEmpty() || resultCreate.isNull()))
     {
         QProcess processMempool;
-        processMempool.start(QString("%1 mempool_proc -net private -chain gdb").arg("/opt/kelvin-node/bin/kelvin-node-cli"));
+        processMempool.start(QString("%1 mempool_proc -net kelvin-testnet -chain gdb").arg(CLI_PATH));
         processMempool.waitForFinished(-1);
         answer = QString::fromLatin1(processMempool.readAll());
         qDebug() << answer;
diff --git a/KelvinDashboardService/KelvinDashboardService.pro b/KelvinDashboardService/KelvinDashboardService.pro
index 7d40ebc9e..e2e430679 100755
--- a/KelvinDashboardService/KelvinDashboardService.pro
+++ b/KelvinDashboardService/KelvinDashboardService.pro
@@ -20,9 +20,11 @@ ICON = icon.ico
 
 win32 {
     VERSION = $${VER_MAJ}.$${VER_MIN}.$$VER_PAT
+    DEFINES += CLI_PATH=\\\"./kelvin-node-cli.exe\\\"
 }
 else {
     VERSION = $$VER_MAJ\.$$VER_MIN\-$$VER_PAT
+    DEFINES += CLI_PATH=\\\"/opt/kelvin-node/bin/kelvin-node-cli\\\"
 }
 
 # The following define makes your compiler emit warnings if you use
diff --git a/KelvinDashboardService/main.cpp b/KelvinDashboardService/main.cpp
index 82ed77b94..93c2dd46d 100755
--- a/KelvinDashboardService/main.cpp
+++ b/KelvinDashboardService/main.cpp
@@ -16,9 +16,9 @@ int main(int argc, char *argv[])
 {
     // Creating a semaphore for locking external resources, as well as initializing an external resource-memory
     QSystemSemaphore systemSemaphore(QString("systemSemaphore for %1").arg("KelvinDashboardService"), 1);
-#ifndef Q_OS_WIN
+
     QSharedMemory memmoryAppBagFix(QString("memmory for %1").arg("KelvinDashboardService"));
-#endif
+
     QSharedMemory memmoryApp(QString("memmory for %1").arg("KelvinDashboardService"));
     // Check for the existence of a running instance of the program
     bool isRunning = DapHalper::getInstance().checkExistenceRunningInstanceApp(systemSemaphore, memmoryApp, memmoryAppBagFix);
-- 
GitLab