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
|
/*
This file is part of the KDE Project
SPDX-FileCopyrightText: 2009-2011 Sebastian Trueg <trueg@kde.org>
SPDX-FileCopyrightText: 2013-2014 Vishesh Handa <vhanda@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "metadatamover.h"
#include "database.h"
#include "termgenerator.h"
#include "transaction.h"
#include "baloodebug.h"
#include <QFile>
using namespace Baloo;
MetadataMover::MetadataMover(Database* db, QObject* parent)
: QObject(parent)
, m_db(db)
{
}
MetadataMover::~MetadataMover()
{
}
void MetadataMover::moveFileMetadata(const QString& from, const QString& to)
{
// qCDebug(BALOO) << from << to;
Q_ASSERT(!from.isEmpty() && from != QLatin1String("/"));
Q_ASSERT(!to.isEmpty() && to != QLatin1String("/"));
Transaction tr(m_db, Transaction::ReadWrite);
// We do NOT get deleted messages for overwritten files! Thus, we
// have to remove all metadata for overwritten files first.
removeMetadata(&tr, to);
// and finally update the old statements
updateMetadata(&tr, from, to);
tr.commit();
}
void MetadataMover::removeFileMetadata(const QString& file)
{
Q_ASSERT(!file.isEmpty() && file != QLatin1String("/"));
Transaction tr(m_db, Transaction::ReadWrite);
removeMetadata(&tr, file);
tr.commit();
}
void MetadataMover::removeMetadata(Transaction* tr, const QString& url)
{
Q_ASSERT(!url.isEmpty());
quint64 id = tr->documentId(QFile::encodeName(url));
if (!id) {
Q_EMIT fileRemoved(url);
return;
}
bool isDir = url.endsWith(QLatin1Char('/'));
if (!isDir) {
tr->removeDocument(id);
} else {
tr->removeRecursively(id);
}
Q_EMIT fileRemoved(url);
}
void MetadataMover::updateMetadata(Transaction* tr, const QString& from, const QString& to)
{
qCDebug(BALOO) << from << "->" << to;
Q_ASSERT(!from.isEmpty() && !to.isEmpty());
Q_ASSERT(from[from.size()-1] != QLatin1Char('/'));
Q_ASSERT(to[to.size()-1] != QLatin1Char('/'));
const QByteArray fromPath = QFile::encodeName(from);
quint64 id = tr->documentId(fromPath);
if (!id) {
qCDebug(BALOO) << "Document not (yet) known, signaling newFile" << to;
Q_EMIT movedWithoutData(to);
return;
}
const QByteArray toPath = QFile::encodeName(to);
auto lastSlash = toPath.lastIndexOf('/');
const QByteArray parentPath = toPath.left(lastSlash + 1);
quint64 parentId = tr->documentId(parentPath);
if (!parentId) {
qCDebug(BALOO) << "Parent directory not (yet) known, signaling newFile" << to;
Q_EMIT movedWithoutData(QFile::decodeName(parentPath));
return;
}
Document doc;
const QByteArray fileName = toPath.mid(lastSlash + 1);
TermGenerator tg(doc);
tg.indexFileNameText(QFile::decodeName(fileName));
doc.setId(id);
doc.setParentId(parentId);
doc.setUrl(toPath);
tr->replaceDocument(doc, DocumentUrl | FileNameTerms);
// Possible scenarios
// 1. file moves to the same device - id is preserved
// 2. file moves to a different device - id is not preserved
}
|