1 /* -*- mode: c++; c-basic-offset:4 -*-
2     selftest/uiservercheck.cpp
3 
4     This file is part of Kleopatra, the KDE keymanager
5     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #include <config-kleopatra.h>
11 
12 #include "uiservercheck.h"
13 
14 #include "implementation_p.h"
15 
16 #include <libkleopatraclient/core/command.h>
17 
18 #include <QTextDocument> // for Qt::escape
19 #include <QEventLoop>
20 #include <QCoreApplication>
21 
22 #include <KLocalizedString>
23 
24 
25 using namespace Kleo;
26 using namespace Kleo::_detail;
27 
28 namespace
29 {
30 
31 class UiServerCheck : public SelfTestImplementation
32 {
33 public:
UiServerCheck()34     explicit UiServerCheck()
35         : SelfTestImplementation(i18nc("@title", "UiServer Connectivity"))
36     {
37         runTest();
38     }
39 
runTest()40     void runTest()
41     {
42 
43         KleopatraClientCopy::Command command;
44 
45         {
46             QEventLoop loop;
47             loop.connect(&command, SIGNAL(finished()), SLOT(quit()));
48             QMetaObject::invokeMethod(&command, "start", Qt::QueuedConnection);
49             loop.exec();
50         }
51 
52         if (command.error()) {
53             m_passed = false;
54             m_error = i18n("not reachable");
55             m_explanation = xi18nc("@info",
56                                     "Could not connect to UiServer: <message>%1</message>",
57                                     command.errorString().toHtmlEscaped());
58             m_proposedFix = xi18nc("@info",
59                                    "<para>Check that your firewall is not set to block local connections "
60                                    "(allow connections to <resource>localhost</resource> or <resource>127.0.0.1</resource>).</para>");
61         } else if (command.serverPid() != QCoreApplication::applicationPid()) {
62             m_passed = false;
63             m_error = i18n("multiple instances");
64             m_explanation = xi18nc("@info",
65                                     "It seems another <application>Kleopatra</application> is running (with process-id %1)",
66                                     command.serverPid());
67             m_proposedFix = xi18nc("@info",
68                                    "Quit any other running instances of <application>Kleopatra</application>.");
69         } else {
70             m_passed = true;
71         }
72 
73     }
74 
75 };
76 }
77 
makeUiServerConnectivitySelfTest()78 std::shared_ptr<SelfTest> Kleo::makeUiServerConnectivitySelfTest()
79 {
80      return std::shared_ptr<SelfTest>(new UiServerCheck);
81 }
82