keepassxc

Форк
0
/
KeeShare.cpp 
254 строки · 7.4 Кб
1
/*
2
 *  Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "KeeShare.h"
19
#include "core/CustomData.h"
20
#include "core/Database.h"
21
#include "core/Group.h"
22
#include "core/Metadata.h"
23
#include "gui/DatabaseIcons.h"
24
#include "keeshare/ShareObserver.h"
25

26
namespace
27
{
28
    static const QString KeeShare_Reference("KeeShare/Reference");
29
}
30

31
KeeShare* KeeShare::m_instance = nullptr;
32

33
KeeShare* KeeShare::instance()
34
{
35
    if (!m_instance) {
36
        qFatal("Race condition: instance wanted before it was initialized, this is a bug.");
37
    }
38

39
    return m_instance;
40
}
41

42
KeeShare::KeeShare(QObject* parent)
43
    : QObject(parent)
44
{
45
    connect(config(), &Config::changed, this, &KeeShare::handleSettingsChanged);
46
}
47

48
void KeeShare::init(QObject* parent)
49
{
50
    Q_ASSERT(!m_instance);
51
    m_instance = new KeeShare(parent);
52
}
53

54
KeeShareSettings::Own KeeShare::own()
55
{
56
    // Read existing own certificate or generate a new one if none available
57
    auto own = KeeShareSettings::Own::deserialize(config()->get(Config::KeeShare_Own).toString());
58
    if (own.key.isNull()) {
59
        own = KeeShareSettings::Own::generate();
60
        setOwn(own);
61
    }
62
    return own;
63
}
64

65
KeeShareSettings::Active KeeShare::active()
66
{
67
    return KeeShareSettings::Active::deserialize(config()->get(Config::KeeShare_Active).toString());
68
}
69

70
void KeeShare::setActive(const KeeShareSettings::Active& active)
71
{
72
    config()->set(Config::KeeShare_Active, KeeShareSettings::Active::serialize(active));
73
}
74

75
void KeeShare::setOwn(const KeeShareSettings::Own& own)
76
{
77
    config()->set(Config::KeeShare_Own, KeeShareSettings::Own::serialize(own));
78
}
79

80
bool KeeShare::isShared(const Group* group)
81
{
82
    return group && group->customData()->contains(KeeShare_Reference);
83
}
84

85
KeeShareSettings::Reference KeeShare::referenceOf(const Group* group)
86
{
87
    static const KeeShareSettings::Reference s_emptyReference;
88
    const CustomData* customData = group->customData();
89
    if (!customData->contains(KeeShare_Reference)) {
90
        return s_emptyReference;
91
    }
92
    const auto encoded = customData->value(KeeShare_Reference);
93
    const auto serialized = QString::fromUtf8(QByteArray::fromBase64(encoded.toLatin1()));
94
    KeeShareSettings::Reference reference = KeeShareSettings::Reference::deserialize(serialized);
95
    if (reference.isNull()) {
96
        qWarning("Invalid sharing reference detected - sharing disabled");
97
        return s_emptyReference;
98
    }
99
    return reference;
100
}
101

102
void KeeShare::setReferenceTo(Group* group, const KeeShareSettings::Reference& reference)
103
{
104
    CustomData* customData = group->customData();
105
    if (reference.isNull()) {
106
        customData->remove(KeeShare_Reference);
107
        return;
108
    }
109
    const auto serialized = KeeShareSettings::Reference::serialize(reference);
110
    customData->set(KeeShare_Reference, serialized.toUtf8().toBase64());
111
}
112

113
bool KeeShare::isEnabled(const Group* group)
114
{
115
    const auto reference = KeeShare::referenceOf(group);
116
    const auto active = KeeShare::active();
117
    return (reference.isImporting() && active.in) || (reference.isExporting() && active.out);
118
}
119

120
const Group* KeeShare::resolveSharedGroup(const Group* group)
121
{
122
    while (group && group != group->database()->rootGroup()) {
123
        if (isShared(group)) {
124
            return group;
125
        }
126
        group = group->parentGroup();
127
    }
128

129
    return nullptr;
130
}
131

132
QString KeeShare::sharingLabel(const Group* group)
133
{
134
    auto* share = resolveSharedGroup(group);
135
    if (!share) {
136
        return {};
137
    }
138

139
    const auto reference = referenceOf(share);
140
    if (!reference.isValid()) {
141
        return tr("Invalid sharing reference");
142
    }
143
    QStringList messages;
144
    switch (reference.type) {
145
    case KeeShareSettings::Inactive:
146
        messages << tr("Inactive share %1").arg(reference.path);
147
        break;
148
    case KeeShareSettings::ImportFrom:
149
        messages << tr("Imported from %1").arg(reference.path);
150
        break;
151
    case KeeShareSettings::ExportTo:
152
        messages << tr("Exported to %1").arg(reference.path);
153
        break;
154
    case KeeShareSettings::SynchronizeWith:
155
        messages << tr("Synchronized with %1").arg(reference.path);
156
        break;
157
    }
158
    const auto active = KeeShare::active();
159
    if (reference.isImporting() && !active.in) {
160
        messages << tr("Import is disabled in settings");
161
    }
162
    if (reference.isExporting() && !active.out) {
163
        messages << tr("Export is disabled in settings");
164
    }
165
    return messages.join("\n");
166
}
167

168
QPixmap KeeShare::indicatorBadge(const Group* group, QPixmap pixmap)
169
{
170
    if (!isShared(group)) {
171
        return pixmap;
172
    }
173

174
    if (isEnabled(group)) {
175
        return databaseIcons()->applyBadge(pixmap, DatabaseIcons::Badges::ShareActive);
176
    }
177
    return databaseIcons()->applyBadge(pixmap, DatabaseIcons::Badges::ShareInactive);
178
}
179

180
QString KeeShare::referenceTypeLabel(const KeeShareSettings::Reference& reference)
181
{
182
    switch (reference.type) {
183
    case KeeShareSettings::Inactive:
184
        return tr("Inactive share");
185
    case KeeShareSettings::ImportFrom:
186
        return tr("Imported from");
187
    case KeeShareSettings::ExportTo:
188
        return tr("Exported to");
189
    case KeeShareSettings::SynchronizeWith:
190
        return tr("Synchronized with");
191
    }
192
    return "";
193
}
194

195
QString KeeShare::indicatorSuffix(const Group* group, const QString& text)
196
{
197
    // we not adjust the display name for now - it's just an alternative to the icon
198
    Q_UNUSED(group);
199
    return text;
200
}
201

202
void KeeShare::connectDatabase(QSharedPointer<Database> newDb, QSharedPointer<Database> oldDb)
203
{
204
    if (oldDb && m_observersByDatabase.contains(oldDb->uuid())) {
205
        QPointer<ShareObserver> observer = m_observersByDatabase.take(oldDb->uuid());
206
        if (observer) {
207
            delete observer;
208
        }
209
    }
210

211
    if (newDb && !m_observersByDatabase.contains(newDb->uuid())) {
212
        QPointer<ShareObserver> observer(new ShareObserver(newDb, this));
213
        m_observersByDatabase[newDb->uuid()] = observer;
214
        connect(observer.data(),
215
                SIGNAL(sharingMessage(QString, MessageWidget::MessageType)),
216
                SIGNAL(sharingMessage(QString, MessageWidget::MessageType)));
217
    }
218
}
219

220
const QString KeeShare::signedContainerFileType()
221
{
222
    static const QString filetype("kdbx.share");
223
    return filetype;
224
}
225

226
const QString KeeShare::unsignedContainerFileType()
227
{
228
    static const QString filetype("kdbx");
229
    return filetype;
230
}
231

232
bool KeeShare::isContainerType(const QFileInfo& fileInfo, const QString type)
233
{
234
    return fileInfo.fileName().endsWith(type, Qt::CaseInsensitive);
235
}
236

237
void KeeShare::handleSettingsChanged(Config::ConfigKey key)
238
{
239
    if (key == Config::KeeShare_Active) {
240
        emit activeChanged();
241
    }
242
}
243

244
const QString KeeShare::signatureFileName()
245
{
246
    static const QString fileName("container.share.signature");
247
    return fileName;
248
}
249

250
const QString KeeShare::containerFileName()
251
{
252
    static const QString fileName("container.share.kdbx");
253
    return fileName;
254
}
255

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.