1 /*
2  * Stellarium Remote Sync plugin
3  * Copyright (C) 2015 Florian Schaukowitsch
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18  */
19 
20 #ifndef SYNCCLIENT_HPP
21 #define SYNCCLIENT_HPP
22 
23 #include <QLoggingCategory>
24 #include <QObject>
25 #include <QTcpSocket>
26 
27 Q_DECLARE_LOGGING_CATEGORY(syncClient)
28 
29 class SyncMessageHandler;
30 class SyncRemotePeer;
31 
32 //! A client which can connect to a SyncServer to receive state changes, and apply them
33 class SyncClient : public QObject
34 {
35 	Q_OBJECT
36 	Q_FLAGS(SyncOptions)
37 public:
38 	//! Bitflag-enum which determines the message types the client instance reacts to,
39 	//! and other boolean options
40 	enum SyncOption
41 	{
42 		NONE		= 0x0000,
43 		SyncTime	= 0x0001,
44 		SyncLocation	= 0x0002,
45 		SyncSelection	= 0x0004,
46 		SyncStelProperty= 0x0008,
47 		SyncView	= 0x0010,
48 		SyncFov		= 0x0020,
49 		SkipGUIProps	= 0x0040,
50 		ALL		= 0xFFFF
51 	};
52 	Q_DECLARE_FLAGS(SyncOptions, SyncOption)
53 
54 	SyncClient(SyncOptions options, const QStringList& excludeProperties, QObject* parent = Q_NULLPTR);
55 	virtual ~SyncClient() Q_DECL_OVERRIDE;
56 
errorString() const57 	QString errorString() const { return errorStr; }
58 
59 public slots:
60 	void connectToServer(const QString& host, const int port);
61 	void disconnectFromServer();
62 
63 protected:
64 	void timerEvent(QTimerEvent* evt) Q_DECL_OVERRIDE;
65 signals:
66 	void connected();
67 	void disconnected(bool cleanExit);
68 private slots:
69 	void serverDisconnected(bool clean);
70 	void socketConnected();
71 	void emitServerError(const QString& errorStr);
72 
73 private:
74 	void checkTimeout();
75 
76 	SyncOptions options;
77 	QStringList stelPropFilter; // list of excluded properties
78 	QString errorStr;
79 	bool isConnecting;
80 	SyncRemotePeer* server;
81 	int timeoutTimerId;
82 	QVector<SyncMessageHandler*> handlerList;
83 
84 	friend class ClientErrorHandler;
85 };
86 
87 Q_DECLARE_OPERATORS_FOR_FLAGS(SyncClient::SyncOptions)
88 
89 #endif
90