Baloo

metadatamover.cpp
1/*
2 This file is part of the KDE Project
3 SPDX-FileCopyrightText: 2009-2011 Sebastian Trueg <trueg@kde.org>
4 SPDX-FileCopyrightText: 2013-2014 Vishesh Handa <vhanda@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "metadatamover.h"
10#include "database.h"
11#include "termgenerator.h"
12#include "transaction.h"
13#include "baloodebug.h"
14
15#include <QFile>
16
17using namespace Baloo;
18
19MetadataMover::MetadataMover(Database* db, QObject* parent)
20 : QObject(parent)
21 , m_db(db)
22{
23}
24
25MetadataMover::~MetadataMover()
26{
27}
28
29void MetadataMover::moveFileMetadata(const QString& from, const QString& to)
30{
31// qCDebug(BALOO) << from << to;
32 Q_ASSERT(!from.isEmpty() && from != QLatin1String("/"));
33 Q_ASSERT(!to.isEmpty() && to != QLatin1String("/"));
34
35 Transaction tr(m_db, Transaction::ReadWrite);
36
37 // We do NOT get deleted messages for overwritten files! Thus, we
38 // have to remove all metadata for overwritten files first.
39 removeMetadata(&tr, to);
40
41 // and finally update the old statements
42 updateMetadata(&tr, from, to);
43
44 tr.commit();
45}
46
47void MetadataMover::removeFileMetadata(const QString& file)
48{
49 Q_ASSERT(!file.isEmpty() && file != QLatin1String("/"));
50
51 Transaction tr(m_db, Transaction::ReadWrite);
52 removeMetadata(&tr, file);
53 tr.commit();
54}
55
56void MetadataMover::removeMetadata(Transaction* tr, const QString& url)
57{
58 Q_ASSERT(!url.isEmpty());
59
60 quint64 id = tr->documentId(QFile::encodeName(url));
61 if (!id) {
62 Q_EMIT fileRemoved(url);
63 return;
64 }
65
66 bool isDir = url.endsWith(QLatin1Char('/'));
67 if (!isDir) {
68 tr->removeDocument(id);
69 } else {
70 tr->removeRecursively(id);
71 }
72
73 Q_EMIT fileRemoved(url);
74}
75
76void MetadataMover::updateMetadata(Transaction* tr, const QString& from, const QString& to)
77{
78 qCDebug(BALOO) << from << "->" << to;
79 Q_ASSERT(!from.isEmpty() && !to.isEmpty());
80 Q_ASSERT(from[from.size()-1] != QLatin1Char('/'));
81 Q_ASSERT(to[to.size()-1] != QLatin1Char('/'));
82
83 const QByteArray fromPath = QFile::encodeName(from);
84 quint64 id = tr->documentId(fromPath);
85 if (!id) {
86 qCDebug(BALOO) << "Document not (yet) known, signaling newFile" << to;
87 Q_EMIT movedWithoutData(to);
88 return;
89 }
90
92 auto lastSlash = toPath.lastIndexOf('/');
93 const QByteArray parentPath = toPath.left(lastSlash + 1);
94
95 quint64 parentId = tr->documentId(parentPath);
96 if (!parentId) {
97 qCDebug(BALOO) << "Parent directory not (yet) known, signaling newFile" << to;
98 Q_EMIT movedWithoutData(QFile::decodeName(parentPath));
99 return;
100 }
101
102 Document doc;
103
104 const QByteArray fileName = toPath.mid(lastSlash + 1);
105 TermGenerator tg(doc);
106 tg.indexFileNameText(QFile::decodeName(fileName));
107
108 doc.setId(id);
109 doc.setParentId(parentId);
110 doc.setUrl(toPath);
111 tr->replaceDocument(doc, DocumentUrl | FileNameTerms);
112
113 // Possible scenarios
114 // 1. file moves to the same device - id is preserved
115 // 2. file moves to a different device - id is not preserved
116}
117
118#include "moc_metadatamover.cpp"
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
QString decodeName(const QByteArray &localFileName)
QByteArray encodeName(const QString &fileName)
qsizetype lastIndexOf(const AT &value, qsizetype from) const const
QList< T > mid(qsizetype pos, qsizetype length) const const
Q_EMITQ_EMIT
QString tr(const char *sourceText, const char *disambiguation, int n)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.