LibKEduVocDocument

sharedkvtmlfiles.cpp
1/*
2 * scan a group of KVTML documents to get information from them
3 * SPDX-FileCopyrightText: 2007 Jeremy Whiting <jpwhiting@kde.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6#include "sharedkvtmlfiles.h"
7
8#include "keduvocdocument.h"
9
10#include <KLocalizedString>
11
12#include <QDir>
13#include <QFile>
14#include <QStandardPaths>
15
16#include <QDebug>
17
18class SharedKvtmlFilesPrivate
19{
20public:
21 /** default constructor calls rescan*/
22 SharedKvtmlFilesPrivate()
23 {
24 rescan();
25 }
26
27 /** default destructor */
28 ~SharedKvtmlFilesPrivate()
29 {
30 }
31
32 /** scan the folder for documents, and record what is found */
33 void rescan();
34
35 /** list of all files found */
36 QStringList m_fileList;
37
38 /** list of all files titles found */
39 QStringList m_titleList;
40
41 /** list of file comments */
42 QStringList m_commentList;
43
44 /** map of files by language key */
45 QMap<QString, QStringList> m_filesByLang;
46};
47
48Q_GLOBAL_STATIC(SharedKvtmlFilesPrivate, sharedKvtmlFilesPrivate)
49
50void SharedKvtmlFilesPrivate::rescan()
51{
52 this->m_titleList.clear();
53 this->m_commentList.clear();
54 this->m_filesByLang.clear();
55 this->m_fileList.clear();
56
57 // Get all kvtml paths
58 QStringList nameFilter;
59 nameFilter.append(QStringLiteral("*.kvtml"));
61 for (const QString &path : dataPaths) {
62 qDebug() << "Checking path " << path << " for kvtml files";
64 for (const QString &locale : locales) {
65 const QStringList files = QDir(path + '/' + locale).entryList(nameFilter, QDir::Files);
66 for (const QString &filename : files) {
67 QString filePath = path + '/' + locale + '/' + filename;
68 this->m_fileList << filePath;
69 this->m_filesByLang[locale].append(filePath);
70 }
71 }
72 }
73
75 for (int i = 0; i < this->m_fileList.size(); ++i) {
76 // open the file
78
79 // add it's title to the title list
80 this->m_titleList.append(doc->title());
81
82 // add it's comment to the comment list
83 this->m_commentList.append(doc->documentComment());
84 }
85 delete doc;
86}
87
89{
90 sharedKvtmlFilesPrivate->rescan();
91}
92
94{
95 return sharedKvtmlFilesPrivate->m_filesByLang.keys();
96}
97
99{
100 // return files by language for given language if it's not empty
101 // otherwise return all filenames
102 return language.isEmpty() ? sharedKvtmlFilesPrivate->m_fileList : sharedKvtmlFilesPrivate->m_filesByLang.value(language);
103}
104
106{
107 QStringList retlist;
108
109 if (language.isEmpty()) {
110 retlist = sharedKvtmlFilesPrivate->m_titleList;
111 } else {
112 QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value(language);
113 for (int i = 0; i < filenames.size(); ++i) {
114 retlist.append(sharedKvtmlFilesPrivate->m_titleList[sharedKvtmlFilesPrivate->m_fileList.indexOf(filenames[i])]);
115 }
116 }
117
118 return retlist;
119}
120
122{
123 QStringList retlist;
124
125 if (language.isEmpty()) {
126 retlist = sharedKvtmlFilesPrivate->m_commentList;
127 } else {
128 QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value(language);
129 for (int i = 0; i < filenames.size(); ++i) {
130 retlist.append(sharedKvtmlFilesPrivate->m_commentList[sharedKvtmlFilesPrivate->m_fileList.indexOf(filenames[i])]);
131 }
132 }
133
134 return retlist;
135}
136
138{
139 QStringList nameFilter;
140 nameFilter.append(QStringLiteral("*.kvtml"));
142 QStringList unsortedFiles;
143 for (const QString &path : dataPaths) {
144 const QStringList files = QDir(path).entryList(nameFilter, QDir::Files | QDir::NoDotAndDotDot);
145 for (const QString &filename : files) {
146 unsortedFiles.append(path + '/' + filename);
147 }
148 }
149
150 KEduVocDocument doc;
151
152 while (!unsortedFiles.isEmpty()) {
153 QUrl fileUrl(QUrl::fromLocalFile(unsortedFiles.first()));
154 // find the file's locale
155 // open the file
156 doc.open(fileUrl);
157
158 if (doc.identifierCount() == 1) {
159 QString locale = doc.identifier(0).locale();
160
161 // make sure the locale sub-folder exists
162 QUrl pathUrl = QUrl(fileUrl);
163 pathUrl = QUrl(pathUrl.toString(QUrl::RemoveFilename) + '/' + locale);
164 QDir dir;
165 if (!dir.mkpath(pathUrl.toLocalFile())) {
166 // Unable to create destination path, so skip
167 continue;
168 }
169
170 pathUrl = QUrl(pathUrl.toString() + '/' + fileUrl.fileName());
171
172 // move the file into the locale sub-folder
173 bool moved = QFile(fileUrl.toLocalFile()).rename(pathUrl.toLocalFile());
174 if (!moved) {
175 qDebug() << "Unable to move " << fileUrl << " to " << pathUrl;
176 }
177 }
178
179 // take off the one we just did
180 unsortedFiles.removeFirst();
181 }
182
183 nameFilter = QStringList(QStringLiteral("*.txt"));
184 QStringList khangmanFiles;
185 for (const QString &path : dataPaths) {
186 const QStringList files = QDir(path).entryList(nameFilter, QDir::Files);
187 for (const QString &filename : files) {
188 khangmanFiles.append(path + '/' + filename);
189 }
190 }
191
192 // move khangman files into
193 while (!khangmanFiles.isEmpty()) {
194 QUrl fileUrl(QUrl::fromLocalFile(khangmanFiles.first()));
196 QUrl destUrl = QUrl::fromLocalFile(destDir.toString() + fileUrl.fileName());
197
198 QDir dir;
199 if (!dir.mkpath(destDir.toLocalFile())) {
200 // Unable to create destination path, so skip
201 continue;
202 }
203
204 // do this better with KStandardDirs stuff
205 bool worked = QFile(fileUrl.toLocalFile()).rename(destUrl.toLocalFile());
206 if (!worked) {
207 qDebug() << "Unable to move " << fileUrl << " to " << destUrl;
208 }
209 khangmanFiles.removeFirst();
210 }
211
212 rescan();
213}
The primary entry point to the hierarchy of objects describing vocabularies.
@ FileIgnoreLock
Ignore the file lock.
int identifierCount() const
QString documentComment() const
QString title() const
KEduVocIdentifier & identifier(int index)
Returns the identifier of translation index.
ErrorCode open(const QUrl &url, FileHandlingFlags flags=FileDefaultHandling)
Opens and locks a document file.
QString locale() const
The locale of the contents: en, de, es, ...
QString path(const QString &relativePath)
KEDUVOCDOCUMENT_EXPORT QStringList titles(const QString &language=QString())
get the list of document titles found of a given language
KEDUVOCDOCUMENT_EXPORT void sortDownloadedFiles()
sort files downloaded to kvtml top-level dir into locale sub-folders
KEDUVOCDOCUMENT_EXPORT QStringList languages()
get list of all languages found in any files
KEDUVOCDOCUMENT_EXPORT QStringList comments(const QString &language=QString())
get the list of document remarks found of a given language
KEDUVOCDOCUMENT_EXPORT QStringList fileNames(const QString &language=QString())
get list of filenames found of given language
KEDUVOCDOCUMENT_EXPORT void rescan()
rescan the shared kvtml locations
QStringList entryList(Filters filters, SortFlags sort) const const
bool rename(const QString &newName)
void append(QList< T > &&value)
void clear()
T & first()
bool isEmpty() const const
void removeFirst()
qsizetype size() const const
T value(qsizetype i) const const
void clear()
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
QString writableLocation(StandardLocation type)
bool isEmpty() const const
RemoveFilename
QString fileName(ComponentFormattingOptions options) const const
QUrl fromLocalFile(const QString &localFile)
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.