File indexing completed on 2024-04-28 05:26:51

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2009, 2010, 2011 Dario Andres Rodriguez <andresbajotierra@gmail.com>
0003 // SPDX-FileCopyrightText: 2019-2022 Harald Sitter <sitter@kde.org>
0004 
0005 #include "platformmodel.h"
0006 
0007 #include <QMetaEnum>
0008 
0009 #include <KLocalizedString>
0010 
0011 #include "bugzillaintegration/bugzillalib.h"
0012 #include "bugzillaintegration/libbugzilla/clients/bugfieldclient.h"
0013 #include "drkonqi.h"
0014 #include "drkonqi_debug.h"
0015 #include "systeminformation.h"
0016 
0017 void PlatformModel::setManager(BugzillaManager *manager)
0018 {
0019     if (m_manager) {
0020         m_manager->disconnect(this);
0021     }
0022     m_manager = manager;
0023 
0024     beginResetModel();
0025     m_list.clear();
0026     m_list << QStringLiteral("unspecified"); // in case the loading fails, always offer unspecified.
0027     endResetModel();
0028 
0029     Bugzilla::BugFieldClient client;
0030     auto job = client.getField(QStringLiteral("rep_platform"));
0031     connect(job, &KJob::finished, this, [this, client](KJob *job) {
0032         try {
0033             Bugzilla::BugField::Ptr field = client.getField(job);
0034             if (!field) {
0035                 // This is a bit flimsy but only acts as save guard.
0036                 // Ideally this code path is never hit.
0037                 throw Bugzilla::RuntimeException(i18nc("@info/status error", "Failed to get platform list"));
0038             }
0039 
0040             beginResetModel();
0041             m_list.clear();
0042             const QList<Bugzilla::BugFieldValue *> values = field->values();
0043             for (const Bugzilla::BugFieldValue *value : values) {
0044                 m_list << value->name();
0045             }
0046             endResetModel();
0047             Q_EMIT detectedPlatformRowChanged();
0048         } catch (Bugzilla::Exception &e) {
0049             qCWarning(DRKONQI_LOG) << e.whatString();
0050             m_error = e.whatString();
0051             Q_EMIT errorChanged();
0052         }
0053     });
0054 
0055     Q_EMIT managerChanged();
0056 }
0057 
0058 int PlatformModel::detectedPlatformRow()
0059 {
0060     const QString detectedPlatform = DrKonqi::systemInformation()->bugzillaPlatform();
0061     int index = m_list.indexOf(detectedPlatform);
0062 
0063     if (index < 0) { // failed to restore value
0064         index = m_list.indexOf(QStringLiteral("unspecified"));
0065     }
0066     if (index < 0) { // also failed to find unspecified... shouldn't happen
0067         index = 0;
0068     }
0069 
0070     return index;
0071 }
0072 
0073 int PlatformModel::rowCount(const QModelIndex &parent) const
0074 {
0075     Q_UNUSED(parent);
0076     return m_list.size();
0077 }
0078 
0079 QVariant PlatformModel::data(const QModelIndex &index, int intRole) const
0080 {
0081     if (!index.isValid()) {
0082         return {};
0083     }
0084 
0085     if (intRole <= Qt::UserRole) {
0086         switch (static_cast<Qt::ItemDataRole>(intRole)) {
0087         case Qt::DisplayRole:
0088             return m_list.at(index.row());
0089         default:
0090             return {};
0091         }
0092     }
0093 
0094     switch (static_cast<Role>(intRole)) {
0095     case Role::Name:
0096         return m_list.at(index.row());
0097     }
0098 
0099     return {};
0100 }
0101 
0102 QHash<int, QByteArray> PlatformModel::roleNames() const
0103 {
0104     static QHash<int, QByteArray> roles;
0105     if (!roles.isEmpty()) {
0106         return roles;
0107     }
0108 
0109     const QMetaEnum roleEnum = QMetaEnum::fromType<Role>();
0110     for (int i = 0; i < roleEnum.keyCount(); ++i) {
0111         const int value = roleEnum.value(i);
0112         Q_ASSERT(value != -1);
0113         roles[static_cast<int>(value)] = QByteArray("ROLE_") + roleEnum.valueToKey(value);
0114     }
0115     return roles;
0116 }
0117 
0118 #include "moc_platformmodel.cpp"