1 //
2 // Copyright (C) 2017 James Turner  zakalawe@mac.com
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 
18 #ifndef APPLICATIONCONTROLLER_H
19 #define APPLICATIONCONTROLLER_H
20 
21 #include <QObject>
22 #include <QAbstractListModel>
23 #include <QNetworkAccessManager>
24 #include <QQmlListProperty>
25 #include <QVariantList>
26 
27 class CanvasConnection;
28 class QWindow;
29 class QTimer;
30 class WindowData;
31 
32 class ApplicationController : public QObject
33 {
34     Q_OBJECT
35 
36 
37     Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
38     Q_PROPERTY(unsigned int port READ port WRITE setPort NOTIFY portChanged)
39 
40     Q_PROPERTY(QVariantList canvases READ canvases NOTIFY canvasListChanged)
41     Q_PROPERTY(QVariantList configs READ configs NOTIFY configListChanged)
42     Q_PROPERTY(QVariantList snapshots READ snapshots NOTIFY snapshotListChanged)
43 
44 
45     Q_PROPERTY(QQmlListProperty<CanvasConnection> activeCanvases READ activeCanvases NOTIFY activeCanvasesChanged)
46     Q_PROPERTY(QQmlListProperty<WindowData> windowList READ windowList NOTIFY windowListChanged)
47 
48     Q_ENUMS(Status)
49 
50     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
51 
52     Q_PROPERTY(bool showUI READ showUI  NOTIFY showUIChanged)
53     Q_PROPERTY(bool blockUIIdle READ blockUIIdle WRITE setBlockUIIdle NOTIFY blockUIIdleChanged)
54 
55     Q_PROPERTY(QString gettingStartedText READ gettingStartedText CONSTANT)
56     Q_PROPERTY(bool showGettingStarted READ showGettingStarted WRITE setShowGettingStarted NOTIFY showGettingStartedChanged)
57 public:
58     explicit ApplicationController(QObject *parent = nullptr);
59     ~ApplicationController() override;
60 
61     void loadFromFile(QString path);
62 
63     void setDaemonMode();
64     void createWindows();
65 
66     Q_INVOKABLE void query();
67     Q_INVOKABLE void cancelQuery();
68     Q_INVOKABLE void clearQuery();
69 
70     Q_INVOKABLE void save(QString configName);
71     Q_INVOKABLE void restoreConfig(int index);
72     Q_INVOKABLE void deleteConfig(int index);
73     Q_INVOKABLE void saveConfigChanges(int index);
74 
75     Q_INVOKABLE void openCanvas(QString path);
76     Q_INVOKABLE void closeCanvas(CanvasConnection* canvas);
77 
78     Q_INVOKABLE void saveSnapshot(QString snapshotName);
79     Q_INVOKABLE void restoreSnapshot(int index);
80 
81     QString host() const;
82 
83     unsigned int port() const;
84 
85     QVariantList canvases() const;
86 
87     QQmlListProperty<CanvasConnection> activeCanvases();
88     QQmlListProperty<WindowData> windowList();
89 
90     QNetworkAccessManager* netAccess() const;
91 
92     enum Status {
93         Idle,
94         Querying,
95         SuccessfulQuery,
96         QueryFailed
97     };
98 
status()99     Status status() const
100     {
101         return m_status;
102     }
103 
configs()104     QVariantList configs() const
105     {
106         return m_configs;
107     }
108 
snapshots()109     QVariantList snapshots() const
110     {
111         return m_snapshots;
112     }
113 
114     bool showUI() const;
115 
blockUIIdle()116     bool blockUIIdle() const
117     {
118         return m_blockUIIdle;
119     }
120 
121     QString gettingStartedText() const;
122 
123     bool showGettingStarted() const;
124 
125 signals:
126 
127     void hostChanged(QString host);
128 
129     void portChanged(unsigned int port);
130 
131     void activeCanvasesChanged();
132     void windowListChanged();
133 
134     void canvasListChanged();
135     void statusChanged(Status status);
136 
137     void configListChanged(QVariantList configs);
138 
139     void snapshotListChanged();
140 
141     void showUIChanged();
142     void blockUIIdleChanged(bool blockUIIdle);
143 
144     void showGettingStartedChanged(bool showGettingStarted);
145 
146 public slots:
147     void setHost(QString host);
148 
149     void setPort(unsigned int port);
150 
setBlockUIIdle(bool blockUIIdle)151     void setBlockUIIdle(bool blockUIIdle)
152     {
153         if (m_blockUIIdle == blockUIIdle)
154             return;
155 
156         m_blockUIIdle = blockUIIdle;
157         emit blockUIIdleChanged(m_blockUIIdle);
158     }
159 
160     void setShowGettingStarted(bool showGettingStarted);
161 
162 protected:
163     bool eventFilter(QObject* obj, QEvent* event) override;
164 
165 private slots:
166     void onFinishedGetCanvasList();
167     void onUIIdleTimeout();
168 
169 private:
170     void setStatus(Status newStatus);
171 
172     void rebuildConfigData();
173     void rebuildSnapshotData();
174     void clearConnections();
175 
176     void doSaveToFile(QString path, QString configName);
177 
178     QByteArray saveState(QString name) const;
179     void restoreState(QByteArray bytes);
180 
181     QByteArray createSnapshot(QString name) const;
182 
183     void defineDefaultWindow();
184 
185     QString m_host;
186     unsigned int m_port;
187     QVariantList m_canvases;
188     QList<CanvasConnection*> m_activeCanvases;
189     QNetworkAccessManager* m_netAccess;
190     Status m_status;
191     QVariantList m_configs;
192     QNetworkReply* m_query = nullptr;
193     QVariantList m_snapshots;
194 
195     QList<WindowData*> m_windowList;
196 
197     bool m_daemonMode = false;
198     bool m_showUI = true;
199     bool m_blockUIIdle = false;
200     QTimer* m_uiIdleTimer;
201 };
202 
203 #endif // APPLICATIONCONTROLLER_H
204