File indexing completed on 2024-03-03 15:08:48

0001 /*
0002     SPDX-FileCopyrightText: %{CURRENT_YEAR} %{AUTHOR} <%{EMAIL}>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "mydatasystem.h"
0008 
0009 QByteArray DataItem::data() const
0010 {
0011     // simulating content data fetched for the item
0012     return QByteArrayLiteral("Hello world\n");
0013 }
0014 
0015 
0016 MyDataSystem::MyDataSystem()
0017     // sample data simulating that in the exposed data system
0018     : m_toplevelGroup({
0019         { // subgroups
0020             { QStringLiteral("Subgroup"), {
0021                 {}, // subgroups
0022                 {   // items
0023                     { QStringLiteral("Item C") },
0024                     { QStringLiteral("Item D") },
0025                     { QStringLiteral("Item E") },
0026                 }
0027             } }
0028         },
0029         { // items
0030             { QStringLiteral("Item A") },
0031             { QStringLiteral("Item B") },
0032         }
0033     })
0034 {
0035 }
0036 
0037 const DataGroup* MyDataSystem::group(const QStringList& groupPath) const
0038 {
0039     const DataGroup* currentGroup = &m_toplevelGroup;
0040     for (const QString &group : groupPath) {
0041         auto it = currentGroup->subGroups.constFind(group);
0042         if (it == currentGroup->subGroups.constEnd()) {
0043             return nullptr;
0044         }
0045         currentGroup = &*it;
0046     }
0047     return currentGroup;
0048 }
0049 
0050 bool MyDataSystem::hasGroup(const QStringList& groupPath) const
0051 {
0052     return (group(groupPath) != nullptr);
0053 }
0054 
0055 QList<DataItem> MyDataSystem::items(const QStringList& groupPath) const
0056 {
0057     auto* group = this->group(groupPath);
0058     return group ? group->items : QList<DataItem>();
0059 }
0060 
0061 DataItem MyDataSystem::item(const QStringList& groupPath, const QString &itemName) const
0062 {
0063     auto* group = this->group(groupPath);
0064     if (group) {
0065         for (auto &item : group->items) {
0066             if (item.name == itemName) {
0067                 return item;
0068             }
0069         }
0070     }
0071     return DataItem();
0072 }
0073 
0074 QStringList MyDataSystem::subGroupNames(const QStringList& groupPath) const
0075 {
0076     auto* group = this->group(groupPath);
0077     return group ? group->subGroups.keys() : QStringList();
0078 }
0079