2
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
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.
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.
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/>.
18
#include "EntryAttachments.h"
20
#include "config-keepassx.h"
21
#include "core/Global.h"
22
#include "crypto/Random.h"
24
#include <QDesktopServices>
26
#include <QProcessEnvironment>
28
#include <QTemporaryFile>
31
EntryAttachments::EntryAttachments(QObject* parent)
32
: ModifiableObject(parent)
36
EntryAttachments::~EntryAttachments()
41
QList<QString> EntryAttachments::keys() const
43
return m_attachments.keys();
46
bool EntryAttachments::hasKey(const QString& key) const
48
return m_attachments.contains(key);
51
QSet<QByteArray> EntryAttachments::values() const
53
return asConst(m_attachments).values().toSet();
56
QByteArray EntryAttachments::value(const QString& key) const
58
return m_attachments.value(key);
61
void EntryAttachments::set(const QString& key, const QByteArray& value)
63
bool shouldEmitModified = false;
64
bool addAttachment = !m_attachments.contains(key);
67
emit aboutToBeAdded(key);
70
if (addAttachment || m_attachments.value(key) != value) {
71
m_attachments.insert(key, value);
72
shouldEmitModified = true;
78
emit keyModified(key);
81
if (shouldEmitModified) {
86
void EntryAttachments::remove(const QString& key)
88
if (!m_attachments.contains(key)) {
89
Q_ASSERT_X(false, "EntryAttachments::remove", qPrintable(QString("Can't find attachment for key %1").arg(key)));
93
emit aboutToBeRemoved(key);
95
m_attachments.remove(key);
97
if (m_openedAttachments.contains(key)) {
98
disconnectAndEraseExternalFile(m_openedAttachments.value(key));
105
void EntryAttachments::remove(const QStringList& keys)
107
if (keys.isEmpty()) {
111
bool emitStatus = modifiedSignalEnabled();
112
setEmitModified(false);
114
bool isModified = false;
115
for (const QString& key : keys) {
116
isModified |= m_attachments.contains(key);
120
setEmitModified(emitStatus);
127
void EntryAttachments::rename(const QString& key, const QString& newKey)
129
const QByteArray val = value(key);
134
bool EntryAttachments::isEmpty() const
136
return m_attachments.isEmpty();
139
void EntryAttachments::clear()
141
if (m_attachments.isEmpty()) {
145
emit aboutToBeReset();
147
m_attachments.clear();
149
const auto externalPath = m_openedAttachments.values();
150
for (auto& path : externalPath) {
151
disconnectAndEraseExternalFile(path);
158
void EntryAttachments::disconnectAndEraseExternalFile(const QString& path)
160
if (m_openedAttachmentsInverse.contains(path)) {
161
m_attachmentFileWatchers.value(path)->stop();
162
m_attachmentFileWatchers.remove(path);
164
m_openedAttachments.remove(m_openedAttachmentsInverse.value(path));
165
m_openedAttachmentsInverse.remove(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));
179
void EntryAttachments::copyDataFrom(const EntryAttachments* other)
181
if (*this != *other) {
182
emit aboutToBeReset();
184
// Reset all externally opened files
185
const auto externalPath = m_openedAttachments.values();
186
for (auto& path : externalPath) {
187
disconnectAndEraseExternalFile(path);
190
m_attachments = other->m_attachments;
197
bool EntryAttachments::operator==(const EntryAttachments& other) const
199
return m_attachments == other.m_attachments;
202
bool EntryAttachments::operator!=(const EntryAttachments& other) const
204
return m_attachments != other.m_attachments;
207
int EntryAttachments::attachmentsSize() const
210
for (auto it = m_attachments.constBegin(); it != m_attachments.constEnd(); ++it) {
211
size += it.key().toUtf8().size() + it.value().size();
216
bool EntryAttachments::openAttachment(const QString& key, QString* errorMessage)
218
if (!m_openedAttachments.contains(key)) {
219
const QByteArray attachmentData = value(key);
220
auto ext = key.contains(".") ? "." + key.split(".").last() : "";
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);
230
const QString tmpFileTemplate = QDir::temp().absoluteFilePath(QString("XXXXXXXXXXXX").append(ext));
233
QTemporaryFile tmpFile(tmpFileTemplate);
235
const bool saveOk = tmpFile.open() && tmpFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)
236
&& tmpFile.write(attachmentData) == attachmentData.size() && tmpFile.flush();
238
if (!saveOk && errorMessage) {
239
*errorMessage = QString("%1 - %2").arg(key, tmpFile.errorString());
244
tmpFile.setAutoRemove(false);
245
m_openedAttachments.insert(key, tmpFile.fileName());
246
m_openedAttachmentsInverse.insert(tmpFile.fileName(), key);
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);
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);
263
void EntryAttachments::attachmentFileModified(const QString& path)
265
auto it = m_openedAttachmentsInverse.find(path);
266
if (it != m_openedAttachmentsInverse.end()) {
267
emit valueModifiedExternally(it.value(), path);