1 /*
2   clientconnectionmanager.h
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2013-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_CLIENTCONNECTIONMANAGER_H
30 #define GAMMARAY_CLIENTCONNECTIONMANAGER_H
31 
32 #include "gammaray_client_export.h"
33 
34 #include <QObject>
35 #include <QTime>
36 #include <QUrl>
37 #include <QPointer>
38 
39 QT_BEGIN_NAMESPACE
40 class QAbstractItemModel;
41 class QMainWindow;
42 QT_END_NAMESPACE
43 
44 namespace GammaRay {
45 class Client;
46 class MainWindow;
47 class ProcessTracker;
48 class ProcessTrackerBackend;
49 class ProcessTrackerInfo;
50 class ClientToolManager;
51 
52 /*! Pre-MainWindow connection setup logic.
53  *
54  * This is useful for embedding the GammaRay client into another application
55  *
56  * @since 2.3
57  */
58 class GAMMARAY_CLIENT_EXPORT ClientConnectionManager : public QObject
59 {
60     Q_OBJECT
61 public:
62     explicit ClientConnectionManager(QObject *parent = nullptr, bool showSplashScreenOnStartUp = true);
63     ~ClientConnectionManager() override;
64 
65     ClientToolManager *toolManager() const;
66     QMainWindow *mainWindow() const;
67 
68     /*! Connect to a GammaRay probe at @p url. */
69     void connectToHost(const QUrl &url, int tryAgain = 0);
70 
71     /*! Manually show the splash screen. */
72     void showSplashScreen();
73 
74     GammaRay::ProcessTrackerBackend *processTrackerBackend() const;
75     void setProcessTrackerBackend(GammaRay::ProcessTrackerBackend *backend);
76 
77     qint64 processTrackerPid() const;
78     void setProcessTrackerPid(qint64 pid);
79 
80     QString endPointLabel() const;
81     QString endPointKey() const;
82     qint64 endPointPid() const;
83 
84     /*! One-time initialization of stream operators and factory callbacks. */
85     static void init();
86 
87 signals:
88     /*! Emitted when the connection is established and the tool model is populated.
89      *  If you want to bring up the standard main window, connect this to createMainWindow(),
90      *  otherwise use this to show your own UI at this point.
91      */
92     void ready();
93 
94     /*! Emitted when there has been a persistent connection error.
95      *  You can connect this to handlePersistentConnectionError() for a standard
96      *  message box and application exit handling.
97      */
98     void persistentConnectionError(const QString &msg);
99 
100     /*! Emitted when the connection to the target has been closed, for whatever reason.
101      *  For a stand-alone client you probably want to connect this to QApplication::quit().
102      */
103     void disconnected();
104 
105     void processTrackerBackendChanged(GammaRay::ProcessTrackerBackend *backend);
106     void processTrackerInfoChanged(const GammaRay::ProcessTrackerInfo &info);
107 
108 public slots:
109     /*! Disconnect GammaRay. */
110     void disconnectFromHost();
111 
112     /*! Brings up a client main window for the current connection.
113      *  If you want to use this, connect this slot to ready().
114      */
115     QMainWindow *createMainWindow();
116 
117     /*! Standard persistent connection error handler.
118      *  @see persistentConnectionError()
119      */
120     void handlePersistentConnectionError(const QString &msg);
121 
122 private slots:
123     void doConnectToHost();
124     void transientConnectionError();
125 
126     void delayedHideSplashScreen();
127     void hideSplashScreen();
128     void targetQuitRequested();
129 
130     void updateProcessTrackerState();
131     void clientConnected();
132     void clientDisconnected();
133 
134 private:
135     QUrl m_serverUrl;
136     Client *m_client;
137     GammaRay::ProcessTracker *m_processTracker;
138     ClientToolManager *m_toolManager;
139     QPointer<MainWindow> m_mainWindow;
140     QTime m_connectionTimeout;
141     bool m_ignorePersistentError;
142     int m_tries;
143 };
144 }
145 
146 #endif // GAMMARAY_CLIENTCONNECTIONMANAGER_H
147