File indexing completed on 2024-05-12 12:54:44
0001 // SPDX-FileCopyrightText: 2022 James Graham <james.h.graham@protonmail.com> 0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0003 0004 #include "serverlistmodel.h" 0005 0006 #include "controller.h" 0007 0008 #include <Quotient/connection.h> 0009 0010 #include <QDebug> 0011 0012 #include <KConfig> 0013 #include <KConfigGroup> 0014 0015 ServerListModel::ServerListModel(QObject *parent) 0016 : QAbstractListModel(parent) 0017 { 0018 KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); 0019 KConfigGroup serverGroup(&dataResource, "Servers"); 0020 0021 QString domain = Controller::instance().activeConnection()->domain(); 0022 0023 // Add the user's homeserver 0024 m_servers.append(Server{ 0025 domain, 0026 true, 0027 false, 0028 false, 0029 }); 0030 // Add matrix.org 0031 m_servers.append(Server{ 0032 QStringLiteral("matrix.org"), 0033 false, 0034 false, 0035 false, 0036 }); 0037 // Add each of the saved custom servers 0038 for (const auto &i : serverGroup.keyList()) { 0039 m_servers.append(Server{ 0040 serverGroup.readEntry(i, QString()), 0041 false, 0042 false, 0043 true, 0044 }); 0045 } 0046 // Add add server delegate entry 0047 m_servers.append(Server{ 0048 QString(), 0049 false, 0050 true, 0051 false, 0052 }); 0053 } 0054 0055 QVariant ServerListModel::data(const QModelIndex &index, int role) const 0056 { 0057 if (!index.isValid()) { 0058 return {}; 0059 } 0060 0061 if (index.row() >= m_servers.count()) { 0062 qDebug() << "ServerListModel, something's wrong: index.row() >= m_notificationRules.count()"; 0063 return {}; 0064 } 0065 0066 if (role == UrlRole) { 0067 return m_servers.at(index.row()).url; 0068 } 0069 0070 if (role == IsHomeServerRole) { 0071 return m_servers.at(index.row()).isHomeServer; 0072 } 0073 0074 if (role == IsAddServerDelegateRole) { 0075 return m_servers.at(index.row()).isAddServerDelegate; 0076 } 0077 0078 if (role == IsDeletableRole) { 0079 return m_servers.at(index.row()).isDeletable; 0080 } 0081 0082 return {}; 0083 } 0084 0085 int ServerListModel::rowCount(const QModelIndex &parent) const 0086 { 0087 Q_UNUSED(parent) 0088 0089 return m_servers.count(); 0090 } 0091 0092 void ServerListModel::checkServer(const QString &url) 0093 { 0094 KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); 0095 KConfigGroup serverGroup(&dataResource, "Servers"); 0096 0097 if (!serverGroup.hasKey(url)) { 0098 if (Quotient::isJobPending(m_checkServerJob)) { 0099 m_checkServerJob->abandon(); 0100 } 0101 0102 m_checkServerJob = Controller::instance().activeConnection()->callApi<Quotient::QueryPublicRoomsJob>(url, 1); 0103 connect(m_checkServerJob, &Quotient::BaseJob::success, this, [this, url] { 0104 Q_EMIT serverCheckComplete(url, true); 0105 }); 0106 } 0107 } 0108 0109 void ServerListModel::addServer(const QString &url) 0110 { 0111 KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); 0112 KConfigGroup serverGroup(&dataResource, "Servers"); 0113 0114 if (!serverGroup.hasKey(url)) { 0115 Server newServer = Server{ 0116 url, 0117 false, 0118 false, 0119 true, 0120 }; 0121 0122 beginInsertRows(QModelIndex(), m_servers.count() - 1, m_servers.count() - 1); 0123 m_servers.insert(rowCount() - 1, newServer); 0124 endInsertRows(); 0125 } 0126 0127 serverGroup.writeEntry(url, url); 0128 } 0129 0130 void ServerListModel::removeServerAtIndex(int row) 0131 { 0132 KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); 0133 KConfigGroup serverGroup(&dataResource, "Servers"); 0134 serverGroup.deleteEntry(data(index(row), UrlRole).toString()); 0135 0136 beginRemoveRows(QModelIndex(), row, row); 0137 m_servers.removeAt(row); 0138 endRemoveRows(); 0139 } 0140 0141 QHash<int, QByteArray> ServerListModel::roleNames() const 0142 { 0143 return { 0144 {UrlRole, QByteArrayLiteral("url")}, 0145 {IsHomeServerRole, QByteArrayLiteral("isHomeServer")}, 0146 {IsAddServerDelegateRole, QByteArrayLiteral("isAddServerDelegate")}, 0147 {IsDeletableRole, QByteArrayLiteral("isDeletable")}, 0148 }; 0149 } 0150 0151 #include "moc_serverlistmodel.cpp"