File: clientconnectionmanager.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (293 lines) | stat: -rw-r--r-- 8,918 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
  clientconnectionmanager.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2013 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "clientconnectionmanager.h"

#include "client.h"
#include "enumrepositoryclient.h"
#include "classesiconsrepositoryclient.h"
#include "remotemodel.h"
#include "selectionmodelclient.h"
#include "propertycontrollerclient.h"
#include "probecontrollerclient.h"
#include "processtracker.h"
#include "paintanalyzerclient.h"
#include "remoteviewclient.h"
#include "favoriteobjectclient.h"
#include <toolmanagerclient.h>

#include <common/objectbroker.h>
#include <common/streamoperators.h>

#include <ui/mainwindow.h>
#include <ui/splashscreen.h>
#include <ui/clienttoolmanager.h>

#include <QApplication>
#include <QMessageBox>
#include <QTimer>

using namespace GammaRay;

static QAbstractItemModel *modelFactory(const QString &name)
{
    return new RemoteModel(name, qApp);
}

static QItemSelectionModel *selectionModelFactory(QAbstractItemModel *model)
{
    return new SelectionModelClient(model->objectName() + ".selection", model, qApp);
}

static QObject *createPropertyController(const QString &name, QObject *parent)
{
    return new PropertyControllerClient(name, parent);
}

static QObject *createProbeController(const QString &name, QObject *parent)
{
    QObject *o = new ProbeControllerClient(parent);
    ObjectBroker::registerObject(name, o);
    return o;
}

static QObject *createToolManager(const QString &name, QObject *parent)
{
    QObject *o = new ToolManagerClient(parent);
    ObjectBroker::registerObject(name, o);
    return o;
}

static QObject *createPaintAnalyzerClient(const QString &name, QObject *parent)
{
    return new PaintAnalyzerClient(name, parent);
}

static QObject *createRemoteViewClient(const QString &name, QObject *parent)
{
    return new RemoteViewClient(name, parent);
}

static QObject *createEnumRepositoryClient(const QString &, QObject *parent)
{
    return new EnumRepositoryClient(parent);
}

static QObject *createClassesIconsRepositoryClient(const QString &, QObject *parent)
{
    return new ClassesIconsRepositoryClient(parent);
}

static QObject *createFavoriteObjectClient(const QString &, QObject *parent)
{
    return new FavoriteObjectClient(parent);
}

void ClientConnectionManager::init()
{
    StreamOperators::registerOperators();

    ObjectBroker::registerClientObjectFactoryCallback<PropertyControllerInterface *>(
        createPropertyController);
    ObjectBroker::registerClientObjectFactoryCallback<ProbeControllerInterface *>(
        createProbeController);
    ObjectBroker::registerClientObjectFactoryCallback<ToolManagerInterface *>(createToolManager);
    ObjectBroker::registerClientObjectFactoryCallback<PaintAnalyzerInterface *>(
        createPaintAnalyzerClient);
    ObjectBroker::registerClientObjectFactoryCallback<RemoteViewInterface *>(createRemoteViewClient);
    ObjectBroker::registerClientObjectFactoryCallback<EnumRepository *>(createEnumRepositoryClient);
    ObjectBroker::registerClientObjectFactoryCallback<ClassesIconsRepository *>(createClassesIconsRepositoryClient);
    ObjectBroker::registerClientObjectFactoryCallback<FavoriteObjectInterface *>(createFavoriteObjectClient);

    ObjectBroker::setModelFactoryCallback(modelFactory);
    ObjectBroker::setSelectionModelFactoryCallback(selectionModelFactory);
}

ClientConnectionManager::ClientConnectionManager(QObject *parent, bool showSplashScreenOnStartUp)
    : QObject(parent)
    , m_client(new Client(this))
    , m_processTracker(new GammaRay::ProcessTracker(this))
    , m_toolManager(new ClientToolManager(this))
    , m_mainWindow(nullptr)
    , m_ignorePersistentError(false)
    , m_tries(0)
{
    if (showSplashScreenOnStartUp)
        showSplashScreen();
    connect(m_processTracker, &ProcessTracker::backendChanged, this,
            &ClientConnectionManager::processTrackerBackendChanged);
    connect(m_processTracker, &ProcessTracker::infoChanged, this,
            &ClientConnectionManager::processTrackerInfoChanged);
    connect(this, &ClientConnectionManager::ready, this, &ClientConnectionManager::clientConnected);
    connect(this, &ClientConnectionManager::disconnected, this, &ClientConnectionManager::clientDisconnected);
    connect(m_client, &Endpoint::disconnected, this, &ClientConnectionManager::disconnected);
    connect(m_client, &Client::transientConnectionError, this, &ClientConnectionManager::transientConnectionError);
    connect(m_client, &Client::persisitentConnectionError,
            this, &ClientConnectionManager::persistentConnectionError);
    connect(this, &ClientConnectionManager::persistentConnectionError, this, &ClientConnectionManager::delayedHideSplashScreen);
    connect(this, &ClientConnectionManager::ready, this, &ClientConnectionManager::delayedHideSplashScreen);
    connect(m_toolManager, &ClientToolManager::toolListAvailable, this, &ClientConnectionManager::ready);
}

ClientConnectionManager::~ClientConnectionManager()
{
    delete m_mainWindow;
}

ClientToolManager *ClientConnectionManager::toolManager() const
{
    return m_toolManager;
}

QMainWindow *ClientConnectionManager::mainWindow() const
{
    return m_mainWindow;
}

void ClientConnectionManager::connectToHost(const QUrl &url, int tryAgain)
{
    m_serverUrl = url;
    m_connectionTimeout.start();
    m_tries = tryAgain;
    doConnectToHost();
}

void ClientConnectionManager::showSplashScreen()
{
    ::showSplashScreen();
}

GammaRay::ProcessTrackerBackend *ClientConnectionManager::processTrackerBackend() const
{
    return m_processTracker->backend();
}

void ClientConnectionManager::setProcessTrackerBackend(GammaRay::ProcessTrackerBackend *backend)
{
    m_processTracker->setBackend(backend);
    updateProcessTrackerState();
}

qint64 ClientConnectionManager::processTrackerPid() const
{
    return m_processTracker->pid();
}

void ClientConnectionManager::setProcessTrackerPid(qint64 pid)
{
    m_processTracker->setPid(pid);
    updateProcessTrackerState();
}

QString ClientConnectionManager::endPointLabel() const
{
    return m_client->label();
}

QString ClientConnectionManager::endPointKey() const
{
    return m_client->key();
}

qint64 ClientConnectionManager::endPointPid() const
{
    return m_client->pid();
}

void ClientConnectionManager::disconnectFromHost()
{
    targetQuitRequested();
    m_client->disconnectFromHost();
}

void ClientConnectionManager::doConnectToHost()
{
    m_client->connectToHost(m_serverUrl, m_tries ? m_tries-- : 0);
}

QMainWindow *ClientConnectionManager::createMainWindow()
{
    delete m_mainWindow;
    m_mainWindow = new MainWindow;
    m_mainWindow->setupFeedbackProvider();
    connect(m_mainWindow.data(), &MainWindow::targetQuitRequested, this, &ClientConnectionManager::targetQuitRequested);
    m_ignorePersistentError = false;
    m_mainWindow->show();
    return m_mainWindow;
}

void ClientConnectionManager::transientConnectionError()
{
    if (m_connectionTimeout.elapsed() < 60 * 1000) {
        // client wasn't up yet, keep trying
        QTimer::singleShot(1000, this, &ClientConnectionManager::doConnectToHost);
    } else {
        emit persistentConnectionError(tr("Connection refused."));
    }
}

void ClientConnectionManager::handlePersistentConnectionError(const QString &msg)
{
    if (m_ignorePersistentError)
        return;

    QString errorMsg;
    if (m_mainWindow)
        errorMsg = tr("Lost connection to remote host: %1").arg(msg);
    else
        errorMsg = tr("Could not establish connection to remote host: %1").arg(msg);

    QMessageBox::critical(m_mainWindow, tr("GammaRay - Connection Error"), errorMsg);
    QApplication::exit(1);
}

void ClientConnectionManager::delayedHideSplashScreen()
{
    QTimer::singleShot(0, this, &ClientConnectionManager::hideSplashScreen);
}

void ClientConnectionManager::hideSplashScreen()
{
    ::hideSplashScreen();
}

void ClientConnectionManager::targetQuitRequested()
{
    m_ignorePersistentError = true;
}

void ClientConnectionManager::updateProcessTrackerState()
{
    if (!m_client->isConnected()) {
        m_processTracker->stop();
    } else if (m_processTracker->isActive()) {
        if (!m_processTracker->backend() || m_processTracker->pid() < 0) {
            m_processTracker->stop();
        }
    } else {
        if (m_processTracker->backend() && m_processTracker->pid() >= 0) {
            m_processTracker->start();
        }
    }
}

void ClientConnectionManager::clientConnected()
{
    setProcessTrackerPid(m_client->pid());
}

void ClientConnectionManager::clientDisconnected()
{
    setProcessTrackerPid(-1);
    emit processTrackerInfoChanged(ProcessTrackerInfo());
}