File indexing completed on 2024-05-12 16:28:08
0001 // SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com> 0002 // SPDX-License-Identifier: GPL-2.0-or-later OR GPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0003 0004 #include "networkcontroller.h" 0005 0006 #include "account/abstractaccount.h" 0007 #include "account/accountmanager.h" 0008 #include "config.h" 0009 #include "tokodon_http_debug.h" 0010 0011 #include <QNetworkProxy> 0012 #include <QUrlQuery> 0013 0014 NetworkController::NetworkController(QObject *parent) 0015 : QObject(parent) 0016 { 0017 setApplicationProxy(); 0018 0019 connect(&AccountManager::instance(), &AccountManager::accountsReady, this, [=] { 0020 m_accountsReady = true; 0021 openLink(); 0022 }); 0023 } 0024 0025 NetworkController &NetworkController::instance() 0026 { 0027 static NetworkController _instance; 0028 return _instance; 0029 } 0030 0031 void NetworkController::setApplicationProxy() 0032 { 0033 Config *cfg = Config::self(); 0034 QNetworkProxy proxy; 0035 0036 // type match to ProxyType from config.kcfg 0037 switch (cfg->proxyType()) { 0038 case 1: // HTTP 0039 proxy.setType(QNetworkProxy::HttpProxy); 0040 proxy.setHostName(cfg->proxyHost()); 0041 proxy.setPort(cfg->proxyPort()); 0042 proxy.setUser(cfg->proxyUser()); 0043 proxy.setPassword(cfg->proxyPassword()); 0044 break; 0045 case 2: // SOCKS 5 0046 proxy.setType(QNetworkProxy::Socks5Proxy); 0047 proxy.setHostName(cfg->proxyHost()); 0048 proxy.setPort(cfg->proxyPort()); 0049 proxy.setUser(cfg->proxyUser()); 0050 proxy.setPassword(cfg->proxyPassword()); 0051 break; 0052 case 0: // System Default 0053 default: 0054 // do nothing 0055 break; 0056 } 0057 0058 QNetworkProxy::setApplicationProxy(proxy); 0059 0060 AccountManager::instance().reloadAccounts(); 0061 } 0062 0063 void NetworkController::openWebApLink(QString url) 0064 { 0065 if (url.startsWith("web+ap")) { 0066 url = url.replace(QRegularExpression("(web\\+ap)+:\\/\\/"), "https://"); 0067 } 0068 0069 m_requestedLink = url; 0070 0071 if (m_accountsReady) { 0072 openLink(); 0073 } 0074 } 0075 0076 void NetworkController::openLink() 0077 { 0078 if (m_requestedLink.isEmpty()) 0079 return; 0080 0081 auto account = AccountManager::instance().selectedAccount(); 0082 0083 auto url = account->apiUrl("/api/v2/search"); 0084 url.setQuery({ 0085 {"q", m_requestedLink}, 0086 {"resolve", "true"}, 0087 {"limit", "1"}, 0088 }); 0089 account->get(url, true, &AccountManager::instance(), [=](QNetworkReply *reply) { 0090 const auto searchResult = QJsonDocument::fromJson(reply->readAll()).object(); 0091 const auto statuses = searchResult[QStringLiteral("statuses")].toArray(); 0092 const auto accounts = searchResult[QStringLiteral("accounts")].toArray(); 0093 0094 if (statuses.isEmpty()) { 0095 qCDebug(TOKODON_HTTP) << "Failed to find any statuses!"; 0096 } else { 0097 const auto status = statuses[0].toObject(); 0098 0099 Q_EMIT NetworkController::instance().openPost(status["id"].toString()); 0100 } 0101 0102 if (accounts.isEmpty()) { 0103 qCDebug(TOKODON_HTTP) << "Failed to find any accounts!"; 0104 } else { 0105 const auto account = accounts[0].toObject(); 0106 0107 Q_EMIT NetworkController::instance().openAccount(account["id"].toString()); 0108 } 0109 0110 m_requestedLink.clear(); 0111 }); 0112 }