1 /*
2 MIT License
3 
4 Copyright (c) 2020 Mikhail Milovidov
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 #pragma once
25 
26 #include <QObject>
27 
28 #include <DaggyCore/Result.h>
29 
30 #include "ISystemSignalHandler.h"
31 
32 class QCoreApplication;
33 
34 namespace daggycore {
35 class DaggyCore;
36 }
37 
38 class CConsoleDaggy : public QObject,
39                       public ISystemSignalHandler
40 {
41     Q_OBJECT
42 public:
43     CConsoleDaggy(QObject* parent = nullptr);
44 
45     daggycore::Result initialize();
46     daggycore::Result start();
47     void stop();
48 
49     bool isError() const;
50     const QString& errorMessage() const;
51 
52 signals:
53     void interrupt(const int signal);
54 
55 private slots:
56     void onDaggyCoreStateChanged(int state);
57 
58 private:
59     bool handleSystemSignal(const int signal);
60 
61     QStringList supportedConvertors() const;
62     QString textDataSourcesType(const QString& file_name) const;
63 
64     struct Settings {
65         QString data_source_text_type;
66         QString data_source_text;
67         QString output_folder;
68         QString data_sources_name;
69         unsigned int timeout = 0;
70     };
71     Settings parse() const;
72 
73     daggycore::DaggyCore* daggyCore() const;
74     QCoreApplication* application() const;
75 
76     QString getTextFromFile(QString file_path) const;
77     QString homeFolder() const;
78 
79     QString generateOutputFolder(const QString& data_sources_name) const;
80 
81     QString mustache(const QString& text, const QString& output_folder) const;
82 
83     daggycore::DaggyCore* daggy_core_;
84     bool need_hard_stop_;
85 
86     QString error_message_;
87 };
88 
89