Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FdoSecretsPlugin.cpp « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96d76e345ad97ab3d8266aa43b3e7723c098b03d (plain)
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
/*
 *  Copyright (C) 2018 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "FdoSecretsPlugin.h"

#include "fdosecrets/FdoSecretsSettings.h"
#include "fdosecrets/objects/DBusTypes.h"
#include "fdosecrets/objects/Service.h"
#include "fdosecrets/widgets/SettingsWidgetFdoSecrets.h"

#include "gui/DatabaseTabWidget.h"

#include <QFile>

#include <utility>

using FdoSecrets::Service;

// TODO: Only used for testing. Need to split service functions away from settings page.
QPointer<FdoSecretsPlugin> g_fdoSecretsPlugin;

FdoSecretsPlugin::FdoSecretsPlugin(DatabaseTabWidget* tabWidget)
    : m_dbTabs(tabWidget)
{
    g_fdoSecretsPlugin = this;
    FdoSecrets::registerDBusTypes();
}

FdoSecretsPlugin* FdoSecretsPlugin::getPlugin()
{
    return g_fdoSecretsPlugin;
}

QWidget* FdoSecretsPlugin::createWidget()
{
    return new SettingsWidgetFdoSecrets(this);
}

void FdoSecretsPlugin::loadSettings(QWidget* widget)
{
    qobject_cast<SettingsWidgetFdoSecrets*>(widget)->loadSettings();
}

void FdoSecretsPlugin::saveSettings(QWidget* widget)
{
    qobject_cast<SettingsWidgetFdoSecrets*>(widget)->saveSettings();
    updateServiceState();
}

void FdoSecretsPlugin::updateServiceState()
{
    if (FdoSecrets::settings()->isEnabled()) {
        if (!m_secretService && m_dbTabs) {
            m_secretService.reset(new Service(this, m_dbTabs));
            connect(m_secretService.data(), &Service::error, this, [this](const QString& msg) {
                emit error(tr("<b>Fdo Secret Service:</b> %1").arg(msg));
            });
            if (!m_secretService->initialize()) {
                m_secretService.reset();
                FdoSecrets::settings()->setEnabled(false);
                return;
            }
            emit secretServiceStarted();
        }
    } else {
        if (m_secretService) {
            m_secretService.reset();
            emit secretServiceStopped();
        }
    }
}

Service* FdoSecretsPlugin::serviceInstance() const
{
    return m_secretService.data();
}

DatabaseTabWidget* FdoSecretsPlugin::dbTabs() const
{
    return m_dbTabs;
}

void FdoSecretsPlugin::emitRequestSwitchToDatabases()
{
    emit requestSwitchToDatabases();
}

void FdoSecretsPlugin::emitRequestShowNotification(const QString& msg, const QString& title)
{
    if (!FdoSecrets::settings()->showNotification()) {
        return;
    }
    emit requestShowNotification(msg, title, 10000);
}

QString FdoSecretsPlugin::reportExistingService() const
{
    auto pidStr = tr("Unknown", "Unknown PID");
    auto exeStr = tr("Unknown", "Unknown executable path");

    // try get pid
    auto pid = QDBusConnection::sessionBus().interface()->servicePid(DBUS_SERVICE_SECRET);
    if (pid.isValid()) {
        pidStr = QString::number(pid.value());

        // try get the first part of the cmdline, which usually is the executable name/path
        QFile proc(QStringLiteral("/proc/%1/cmdline").arg(pid.value()));
        if (proc.open(QFile::ReadOnly)) {
            auto parts = proc.readAll().split('\0');
            if (parts.length() >= 1) {
                exeStr = QString::fromLocal8Bit(parts[0]).trimmed();
            }
        }
    }
    auto otherService = tr("<i>PID: %1, Executable: %2</i>", "<i>PID: 1234, Executable: /path/to/exe</i>")
                            .arg(pidStr, exeStr.toHtmlEscaped());
    return tr("Another secret service is running (%1).<br/>"
              "Please stop/remove it before re-enabling the Secret Service Integration.")
        .arg(otherService);
}