Baloo

metadatamover.cpp
1 /*
2  This file is part of the KDE Project
3  SPDX-FileCopyrightText: 2009-2011 Sebastian Trueg <[email protected]>
4  SPDX-FileCopyrightText: 2013-2014 Vishesh Handa <[email protected]>
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 
17 using namespace Baloo;
18 
19 MetadataMover::MetadataMover(Database* db, QObject* parent)
20  : QObject(parent)
21  , m_db(db)
22 {
23 }
24 
25 MetadataMover::~MetadataMover()
26 {
27 }
28 
29 void 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 
47 void 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 
56 void 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 
76 void 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 
91  const QByteArray toPath = QFile::encodeName(to);
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"
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
int size() const const
QByteArray encodeName(const QString &fileName)
int lastIndexOf(char ch, int from) const const
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QByteArray mid(int pos, int len) const const
bool isEmpty() const const
QByteArray left(int len) const const
A document represents an indexed file to be stored in the Baloo engine.
Definition: document.h:30
QString decodeName(const QByteArray &localFileName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.