File indexing completed on 2024-04-21 16:12:19
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_debug.h" 0014 #include "systeminformation.h" 0015 0016 void PlatformModel::setManager(BugzillaManager *manager) 0017 { 0018 if (m_manager) { 0019 m_manager->disconnect(this); 0020 } 0021 m_manager = manager; 0022 0023 beginResetModel(); 0024 m_list.clear(); 0025 m_list << QStringLiteral("unspecified"); // in case the loading fails, always offer unspecified. 0026 endResetModel(); 0027 0028 Bugzilla::BugFieldClient client; 0029 auto job = client.getField(QStringLiteral("rep_platform")); 0030 connect(job, &KJob::finished, this, [this, client](KJob *job) { 0031 try { 0032 Bugzilla::BugField::Ptr field = client.getField(job); 0033 if (!field) { 0034 // This is a bit flimsy but only acts as save guard. 0035 // Ideally this code path is never hit. 0036 throw Bugzilla::RuntimeException(i18nc("@info/status error", "Failed to get platform list")); 0037 } 0038 0039 beginResetModel(); 0040 m_list.clear(); 0041 const QList<Bugzilla::BugFieldValue *> values = field->values(); 0042 for (const Bugzilla::BugFieldValue *value : values) { 0043 m_list << value->name(); 0044 } 0045 endResetModel(); 0046 Q_EMIT detectedPlatformRowChanged(); 0047 } catch (Bugzilla::Exception &e) { 0048 qCWarning(DRKONQI_LOG) << e.whatString(); 0049 m_error = e.whatString(); 0050 Q_EMIT errorChanged(); 0051 } 0052 }); 0053 0054 Q_EMIT managerChanged(); 0055 } 0056 0057 int PlatformModel::detectedPlatformRow() 0058 { 0059 const QString detectedPlatform = DrKonqi::systemInformation()->bugzillaPlatform(); 0060 int index = m_list.indexOf(detectedPlatform); 0061 0062 if (index < 0) { // failed to restore value 0063 index = m_list.indexOf(QStringLiteral("unspecified")); 0064 } 0065 if (index < 0) { // also failed to find unspecified... shouldn't happen 0066 index = 0; 0067 } 0068 0069 return index; 0070 } 0071 0072 int PlatformModel::rowCount(const QModelIndex &parent) const 0073 { 0074 Q_UNUSED(parent); 0075 return m_list.size(); 0076 } 0077 0078 QVariant PlatformModel::data(const QModelIndex &index, int intRole) const 0079 { 0080 if (!index.isValid()) { 0081 return {}; 0082 } 0083 0084 if (intRole <= Qt::UserRole) { 0085 switch (static_cast<Qt::ItemDataRole>(intRole)) { 0086 case Qt::DisplayRole: 0087 return m_list.at(index.row()); 0088 default: 0089 return {}; 0090 } 0091 } 0092 0093 switch (static_cast<Role>(intRole)) { 0094 case Role::Name: 0095 return m_list.at(index.row()); 0096 } 0097 0098 return {}; 0099 } 0100 0101 QHash<int, QByteArray> PlatformModel::roleNames() const 0102 { 0103 static QHash<int, QByteArray> roles; 0104 if (!roles.isEmpty()) { 0105 return roles; 0106 } 0107 0108 const QMetaEnum roleEnum = QMetaEnum::fromType<Role>(); 0109 for (int i = 0; i < roleEnum.keyCount(); ++i) { 0110 const int value = roleEnum.value(i); 0111 Q_ASSERT(value != -1); 0112 roles[static_cast<int>(value)] = QByteArray("ROLE_") + roleEnum.valueToKey(value); 0113 } 0114 return roles; 0115 }