keepassxc

Форк
0
/
EntryAttachments.cpp 
269 строк · 7.1 Кб
1
/*
2
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
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 "EntryAttachments.h"
19

20
#include "config-keepassx.h"
21
#include "core/Global.h"
22
#include "crypto/Random.h"
23

24
#include <QDesktopServices>
25
#include <QDir>
26
#include <QProcessEnvironment>
27
#include <QSet>
28
#include <QTemporaryFile>
29
#include <QUrl>
30

31
EntryAttachments::EntryAttachments(QObject* parent)
32
    : ModifiableObject(parent)
33
{
34
}
35

36
EntryAttachments::~EntryAttachments()
37
{
38
    clear();
39
}
40

41
QList<QString> EntryAttachments::keys() const
42
{
43
    return m_attachments.keys();
44
}
45

46
bool EntryAttachments::hasKey(const QString& key) const
47
{
48
    return m_attachments.contains(key);
49
}
50

51
QSet<QByteArray> EntryAttachments::values() const
52
{
53
    return asConst(m_attachments).values().toSet();
54
}
55

56
QByteArray EntryAttachments::value(const QString& key) const
57
{
58
    return m_attachments.value(key);
59
}
60

61
void EntryAttachments::set(const QString& key, const QByteArray& value)
62
{
63
    bool shouldEmitModified = false;
64
    bool addAttachment = !m_attachments.contains(key);
65

66
    if (addAttachment) {
67
        emit aboutToBeAdded(key);
68
    }
69

70
    if (addAttachment || m_attachments.value(key) != value) {
71
        m_attachments.insert(key, value);
72
        shouldEmitModified = true;
73
    }
74

75
    if (addAttachment) {
76
        emit added(key);
77
    } else {
78
        emit keyModified(key);
79
    }
80

81
    if (shouldEmitModified) {
82
        emitModified();
83
    }
84
}
85

86
void EntryAttachments::remove(const QString& key)
87
{
88
    if (!m_attachments.contains(key)) {
89
        Q_ASSERT_X(false, "EntryAttachments::remove", qPrintable(QString("Can't find attachment for key %1").arg(key)));
90
        return;
91
    }
92

93
    emit aboutToBeRemoved(key);
94

95
    m_attachments.remove(key);
96

97
    if (m_openedAttachments.contains(key)) {
98
        disconnectAndEraseExternalFile(m_openedAttachments.value(key));
99
    }
100

101
    emit removed(key);
102
    emitModified();
103
}
104

105
void EntryAttachments::remove(const QStringList& keys)
106
{
107
    if (keys.isEmpty()) {
108
        return;
109
    }
110

111
    bool emitStatus = modifiedSignalEnabled();
112
    setEmitModified(false);
113

114
    bool isModified = false;
115
    for (const QString& key : keys) {
116
        isModified |= m_attachments.contains(key);
117
        remove(key);
118
    }
119

120
    setEmitModified(emitStatus);
121

122
    if (isModified) {
123
        emitModified();
124
    }
125
}
126

127
void EntryAttachments::rename(const QString& key, const QString& newKey)
128
{
129
    const QByteArray val = value(key);
130
    remove(key);
131
    set(newKey, val);
132
}
133

134
bool EntryAttachments::isEmpty() const
135
{
136
    return m_attachments.isEmpty();
137
}
138

139
void EntryAttachments::clear()
140
{
141
    if (m_attachments.isEmpty()) {
142
        return;
143
    }
144

145
    emit aboutToBeReset();
146

147
    m_attachments.clear();
148

149
    const auto externalPath = m_openedAttachments.values();
150
    for (auto& path : externalPath) {
151
        disconnectAndEraseExternalFile(path);
152
    }
153

154
    emit reset();
155
    emitModified();
156
}
157

158
void EntryAttachments::disconnectAndEraseExternalFile(const QString& path)
159
{
160
    if (m_openedAttachmentsInverse.contains(path)) {
161
        m_attachmentFileWatchers.value(path)->stop();
162
        m_attachmentFileWatchers.remove(path);
163

164
        m_openedAttachments.remove(m_openedAttachmentsInverse.value(path));
165
        m_openedAttachmentsInverse.remove(path);
166
    }
167

168
    QFile f(path);
169
    if (f.open(QFile::ReadWrite)) {
170
        qint64 blocks = f.size() / 128 + 1;
171
        for (qint64 i = 0; i < blocks; ++i) {
172
            f.write(randomGen()->randomArray(128));
173
        }
174
        f.close();
175
    }
176
    f.remove();
177
}
178

179
void EntryAttachments::copyDataFrom(const EntryAttachments* other)
180
{
181
    if (*this != *other) {
182
        emit aboutToBeReset();
183

184
        // Reset all externally opened files
185
        const auto externalPath = m_openedAttachments.values();
186
        for (auto& path : externalPath) {
187
            disconnectAndEraseExternalFile(path);
188
        }
189

190
        m_attachments = other->m_attachments;
191

192
        emit reset();
193
        emitModified();
194
    }
195
}
196

197
bool EntryAttachments::operator==(const EntryAttachments& other) const
198
{
199
    return m_attachments == other.m_attachments;
200
}
201

202
bool EntryAttachments::operator!=(const EntryAttachments& other) const
203
{
204
    return m_attachments != other.m_attachments;
205
}
206

207
int EntryAttachments::attachmentsSize() const
208
{
209
    int size = 0;
210
    for (auto it = m_attachments.constBegin(); it != m_attachments.constEnd(); ++it) {
211
        size += it.key().toUtf8().size() + it.value().size();
212
    }
213
    return size;
214
}
215

216
bool EntryAttachments::openAttachment(const QString& key, QString* errorMessage)
217
{
218
    if (!m_openedAttachments.contains(key)) {
219
        const QByteArray attachmentData = value(key);
220
        auto ext = key.contains(".") ? "." + key.split(".").last() : "";
221

222
#if defined(KEEPASSXC_DIST_SNAP)
223
        const QString tmpFileTemplate =
224
            QString("%1/XXXXXXXXXXXX%2").arg(QProcessEnvironment::systemEnvironment().value("SNAP_USER_DATA"), ext);
225
#elif defined(KEEPASSXC_DIST_FLATPAK)
226
        const QString tmpFileTemplate =
227
            QString("%1/app/%2/XXXXXX.%3")
228
                .arg(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation), "org.keepassxc.KeePassXC", ext);
229
#else
230
        const QString tmpFileTemplate = QDir::temp().absoluteFilePath(QString("XXXXXXXXXXXX").append(ext));
231
#endif
232

233
        QTemporaryFile tmpFile(tmpFileTemplate);
234

235
        const bool saveOk = tmpFile.open() && tmpFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)
236
                            && tmpFile.write(attachmentData) == attachmentData.size() && tmpFile.flush();
237

238
        if (!saveOk && errorMessage) {
239
            *errorMessage = QString("%1 - %2").arg(key, tmpFile.errorString());
240
            return false;
241
        }
242

243
        tmpFile.close();
244
        tmpFile.setAutoRemove(false);
245
        m_openedAttachments.insert(key, tmpFile.fileName());
246
        m_openedAttachmentsInverse.insert(tmpFile.fileName(), key);
247

248
        auto watcher = QSharedPointer<FileWatcher>::create();
249
        watcher->start(tmpFile.fileName(), 5);
250
        connect(watcher.data(), &FileWatcher::fileChanged, this, &EntryAttachments::attachmentFileModified);
251
        m_attachmentFileWatchers.insert(tmpFile.fileName(), watcher);
252
    }
253

254
    const bool openOk = QDesktopServices::openUrl(QUrl::fromLocalFile(m_openedAttachments.value(key)));
255
    if (!openOk && errorMessage) {
256
        *errorMessage = tr("Cannot open file \"%1\"").arg(key);
257
        return false;
258
    }
259

260
    return true;
261
}
262

263
void EntryAttachments::attachmentFileModified(const QString& path)
264
{
265
    auto it = m_openedAttachmentsInverse.find(path);
266
    if (it != m_openedAttachmentsInverse.end()) {
267
        emit valueModifiedExternally(it.value(), path);
268
    }
269
}
270

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

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

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

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