File indexing completed on 2024-05-05 17:33:19
0001 /* 0002 * SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "CategoriesReader.h" 0008 #include "Category.h" 0009 #include "libdiscover_debug.h" 0010 #include <QCoreApplication> 0011 #include <QFile> 0012 #include <QStandardPaths> 0013 #include <QXmlStreamReader> 0014 0015 #include <DiscoverBackendsFactory.h> 0016 #include <resources/AbstractResourcesBackend.h> 0017 0018 QVector<Category *> CategoriesReader::loadCategoriesFile(AbstractResourcesBackend *backend) 0019 { 0020 QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, 0021 QStringLiteral("libdiscover/categories/") + backend->name() + QStringLiteral("-categories.xml")); 0022 if (path.isEmpty()) { 0023 auto cat = backend->category(); 0024 if (cat.isEmpty()) 0025 qCDebug(LIBDISCOVER_LOG) << "Couldn't find a category for " << backend->name(); 0026 0027 Category::sortCategories(cat); 0028 return cat; 0029 } 0030 return loadCategoriesPath(path); 0031 } 0032 0033 QVector<Category *> CategoriesReader::loadCategoriesPath(const QString &path) 0034 { 0035 QVector<Category *> ret; 0036 QFile menuFile(path); 0037 if (!menuFile.open(QIODevice::ReadOnly)) { 0038 qCWarning(LIBDISCOVER_LOG) << "couldn't open" << path; 0039 return ret; 0040 } 0041 0042 QXmlStreamReader xml(&menuFile); 0043 xml.readNextStartElement(); // We want to skip the first <Menu> overall 0044 0045 while (!xml.atEnd() && !xml.hasError()) { 0046 xml.readNext(); 0047 0048 if (xml.isStartElement() && xml.name() == QLatin1String("Menu")) { 0049 ret << new Category({path}, qApp); 0050 ret.last()->parseData(path, &xml); 0051 } 0052 } 0053 0054 if (xml.hasError()) { 0055 qCWarning(LIBDISCOVER_LOG) << "error while parsing the categories file:" << path << ':' << xml.lineNumber() << xml.errorString(); 0056 } 0057 0058 Category::sortCategories(ret); 0059 return ret; 0060 }