File indexing completed on 2024-12-08 07:33:46

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 <Quotient/connection.h>
0007 
0008 #include <QDebug>
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KSharedConfig>
0013 
0014 #include "neochatconnection.h"
0015 
0016 ServerListModel::ServerListModel(QObject *parent)
0017     : QAbstractListModel(parent)
0018 {
0019 }
0020 
0021 QVariant ServerListModel::data(const QModelIndex &index, int role) const
0022 {
0023     if (!index.isValid()) {
0024         return {};
0025     }
0026 
0027     if (index.row() >= m_servers.count()) {
0028         qDebug() << "ServerListModel, something's wrong: index.row() >= m_notificationRules.count()";
0029         return {};
0030     }
0031 
0032     if (role == UrlRole) {
0033         return m_servers.at(index.row()).url;
0034     }
0035 
0036     if (role == IsHomeServerRole) {
0037         return m_servers.at(index.row()).isHomeServer;
0038     }
0039 
0040     if (role == IsAddServerDelegateRole) {
0041         return m_servers.at(index.row()).isAddServerDelegate;
0042     }
0043 
0044     if (role == IsDeletableRole) {
0045         return m_servers.at(index.row()).isDeletable;
0046     }
0047 
0048     return {};
0049 }
0050 
0051 int ServerListModel::rowCount(const QModelIndex &parent) const
0052 {
0053     Q_UNUSED(parent)
0054 
0055     return m_servers.count();
0056 }
0057 
0058 void ServerListModel::checkServer(const QString &url)
0059 {
0060     const auto stateConfig = KSharedConfig::openStateConfig();
0061     const KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
0062 
0063     if (!serverGroup.hasKey(url)) {
0064         if (Quotient::isJobPending(m_checkServerJob)) {
0065             m_checkServerJob->abandon();
0066         }
0067 
0068         m_checkServerJob = m_connection->callApi<Quotient::QueryPublicRoomsJob>(url, 1);
0069         connect(m_checkServerJob, &Quotient::BaseJob::success, this, [this, url] {
0070             Q_EMIT serverCheckComplete(url, true);
0071         });
0072     }
0073 }
0074 
0075 void ServerListModel::addServer(const QString &url)
0076 {
0077     const auto stateConfig = KSharedConfig::openStateConfig();
0078     KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
0079 
0080     if (!serverGroup.hasKey(url)) {
0081         Server newServer = Server{
0082             url,
0083             false,
0084             false,
0085             true,
0086         };
0087 
0088         beginInsertRows(QModelIndex(), m_servers.count() - 1, m_servers.count() - 1);
0089         m_servers.insert(rowCount() - 1, newServer);
0090         endInsertRows();
0091     }
0092 
0093     serverGroup.writeEntry(url, url);
0094     stateConfig->sync();
0095 }
0096 
0097 void ServerListModel::removeServerAtIndex(int row)
0098 {
0099     const auto stateConfig = KSharedConfig::openStateConfig();
0100     KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
0101 
0102     serverGroup.deleteEntry(data(index(row), UrlRole).toString());
0103 
0104     beginRemoveRows(QModelIndex(), row, row);
0105     m_servers.removeAt(row);
0106     endRemoveRows();
0107 
0108     stateConfig->sync();
0109 }
0110 
0111 QHash<int, QByteArray> ServerListModel::roleNames() const
0112 {
0113     return {
0114         {UrlRole, QByteArrayLiteral("url")},
0115         {IsHomeServerRole, QByteArrayLiteral("isHomeServer")},
0116         {IsAddServerDelegateRole, QByteArrayLiteral("isAddServerDelegate")},
0117         {IsDeletableRole, QByteArrayLiteral("isDeletable")},
0118     };
0119 }
0120 
0121 NeoChatConnection *ServerListModel::connection() const
0122 {
0123     return m_connection;
0124 }
0125 
0126 void ServerListModel::setConnection(NeoChatConnection *connection)
0127 {
0128     if (m_connection == connection) {
0129         return;
0130     }
0131     m_connection = connection;
0132     Q_EMIT connectionChanged();
0133 
0134     initialize();
0135 }
0136 
0137 void ServerListModel::initialize()
0138 {
0139     if (m_connection == nullptr) {
0140         return;
0141     }
0142 
0143     beginResetModel();
0144     const auto stateConfig = KSharedConfig::openStateConfig();
0145     const KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
0146 
0147     QString domain = m_connection->domain();
0148 
0149     // Add the user's homeserver
0150     m_servers.append(Server{
0151         domain,
0152         true,
0153         false,
0154         false,
0155     });
0156     // Add matrix.org
0157     m_servers.append(Server{
0158         QStringLiteral("matrix.org"),
0159         false,
0160         false,
0161         false,
0162     });
0163     // Add each of the saved custom servers
0164     for (const auto &i : serverGroup.keyList()) {
0165         m_servers.append(Server{
0166             serverGroup.readEntry(i, QString()),
0167             false,
0168             false,
0169             true,
0170         });
0171     }
0172     // Add add server delegate entry
0173     m_servers.append(Server{
0174         QString(),
0175         false,
0176         true,
0177         false,
0178     });
0179     beginResetModel();
0180 }
0181 
0182 #include "moc_serverlistmodel.cpp"