File indexing completed on 2024-04-14 03:49:46

0001 /*
0002     This file is part of the KDE Project
0003     SPDX-FileCopyrightText: 2009-2011 Sebastian Trueg <trueg@kde.org>
0004     SPDX-FileCopyrightText: 2013-2014 Vishesh Handa <vhanda@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "metadatamover.h"
0010 #include "database.h"
0011 #include "termgenerator.h"
0012 #include "transaction.h"
0013 #include "baloodebug.h"
0014 
0015 #include <QFile>
0016 
0017 using namespace Baloo;
0018 
0019 MetadataMover::MetadataMover(Database* db, QObject* parent)
0020     : QObject(parent)
0021     , m_db(db)
0022 {
0023 }
0024 
0025 MetadataMover::~MetadataMover()
0026 {
0027 }
0028 
0029 void MetadataMover::moveFileMetadata(const QString& from, const QString& to)
0030 {
0031 //    qCDebug(BALOO) << from << to;
0032     Q_ASSERT(!from.isEmpty() && from != QLatin1String("/"));
0033     Q_ASSERT(!to.isEmpty() && to != QLatin1String("/"));
0034 
0035     Transaction tr(m_db, Transaction::ReadWrite);
0036 
0037     // We do NOT get deleted messages for overwritten files! Thus, we
0038     // have to remove all metadata for overwritten files first.
0039     removeMetadata(&tr, to);
0040 
0041     // and finally update the old statements
0042     updateMetadata(&tr, from, to);
0043 
0044     tr.commit();
0045 }
0046 
0047 void MetadataMover::removeFileMetadata(const QString& file)
0048 {
0049     Q_ASSERT(!file.isEmpty() && file != QLatin1String("/"));
0050 
0051     Transaction tr(m_db, Transaction::ReadWrite);
0052     removeMetadata(&tr, file);
0053     tr.commit();
0054 }
0055 
0056 void MetadataMover::removeMetadata(Transaction* tr, const QString& url)
0057 {
0058     Q_ASSERT(!url.isEmpty());
0059 
0060     quint64 id = tr->documentId(QFile::encodeName(url));
0061     if (!id) {
0062         Q_EMIT fileRemoved(url);
0063         return;
0064     }
0065 
0066     bool isDir = url.endsWith(QLatin1Char('/'));
0067     if (!isDir) {
0068         tr->removeDocument(id);
0069     } else {
0070         tr->removeRecursively(id);
0071     }
0072 
0073     Q_EMIT fileRemoved(url);
0074 }
0075 
0076 void MetadataMover::updateMetadata(Transaction* tr, const QString& from, const QString& to)
0077 {
0078     qCDebug(BALOO) << from << "->" << to;
0079     Q_ASSERT(!from.isEmpty() && !to.isEmpty());
0080     Q_ASSERT(from[from.size()-1] != QLatin1Char('/'));
0081     Q_ASSERT(to[to.size()-1] != QLatin1Char('/'));
0082 
0083     const QByteArray fromPath = QFile::encodeName(from);
0084     quint64 id = tr->documentId(fromPath);
0085     if (!id) {
0086         qCDebug(BALOO) << "Document not (yet) known, signaling newFile" << to;
0087         Q_EMIT movedWithoutData(to);
0088         return;
0089     }
0090 
0091     const QByteArray toPath = QFile::encodeName(to);
0092     auto lastSlash = toPath.lastIndexOf('/');
0093     const QByteArray parentPath = toPath.left(lastSlash + 1);
0094 
0095     quint64 parentId = tr->documentId(parentPath);
0096     if (!parentId) {
0097         qCDebug(BALOO) << "Parent directory not (yet) known, signaling newFile" << to;
0098         Q_EMIT movedWithoutData(QFile::decodeName(parentPath));
0099         return;
0100     }
0101 
0102     Document doc;
0103 
0104     const QByteArray fileName = toPath.mid(lastSlash + 1);
0105     TermGenerator tg(doc);
0106     tg.indexFileNameText(QFile::decodeName(fileName));
0107 
0108     doc.setId(id);
0109     doc.setParentId(parentId);
0110     doc.setUrl(toPath);
0111     tr->replaceDocument(doc, DocumentUrl | FileNameTerms);
0112 
0113     // Possible scenarios
0114     // 1. file moves to the same device - id is preserved
0115     // 2. file moves to a different device - id is not preserved
0116 }
0117 
0118 #include "moc_metadatamover.cpp"