File indexing completed on 2024-05-12 16:28:05
0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "preferences.h" 0005 #include "identity.h" 0006 #include "utils/utils.h" 0007 0008 #include <QHttpMultiPart> 0009 0010 Preferences::Preferences(AbstractAccount *account) 0011 : QObject(account) 0012 , m_account(account) 0013 { 0014 connect(account, &AbstractAccount::authenticated, this, [this, account]() { 0015 account->get(account->apiUrl(QStringLiteral("/api/v1/preferences")), true, this, [this](QNetworkReply *reply) { 0016 const auto obj = QJsonDocument::fromJson(reply->readAll()).object(); 0017 0018 if (auto defaultLanguage = obj[QStringLiteral("posting:default:language")]; !defaultLanguage.isNull()) { 0019 m_defaultLanguage = defaultLanguage.toString(); 0020 } 0021 0022 m_defaultSensitive = obj[QStringLiteral("posting:default:sensitive")].toBool(); 0023 m_defaultVisibility = stringToVisibility(obj[QStringLiteral("posting:default:visibility")].toString()); 0024 m_extendSpoiler = obj[QStringLiteral("reading:expand:spoilers")].toBool(); 0025 m_extendMedia = obj[QStringLiteral("reading:expand:media")].toString(); 0026 Q_EMIT defaultVisibilityChanged(); 0027 Q_EMIT defaultSensitiveChanged(); 0028 Q_EMIT defaultLanguageChanged(); 0029 Q_EMIT extendMediaChanged(); 0030 Q_EMIT extendSpoilerChanged(); 0031 }); 0032 }); 0033 } 0034 0035 Post::Visibility Preferences::defaultVisibility() const 0036 { 0037 return m_defaultVisibility; 0038 } 0039 0040 void Preferences::setDefaultVisibility(Post::Visibility visibility) 0041 { 0042 if (visibility == m_defaultVisibility) { 0043 return; 0044 } 0045 0046 m_defaultVisibility = visibility; 0047 Q_EMIT defaultVisibilityChanged(); 0048 0049 setPreferencesField(QStringLiteral("source[privacy]"), visibilityToString(visibility)); 0050 } 0051 0052 bool Preferences::defaultSensitive() const 0053 { 0054 return m_defaultSensitive; 0055 } 0056 0057 void Preferences::setDefaultSensitive(bool sensitive) 0058 { 0059 if (sensitive == m_defaultSensitive) { 0060 return; 0061 } 0062 0063 m_defaultSensitive = sensitive; 0064 Q_EMIT defaultSensitiveChanged(); 0065 0066 setPreferencesField(QStringLiteral("source[sensitive]"), sensitive ? QStringLiteral("true") : QStringLiteral("false")); 0067 } 0068 0069 QString Preferences::defaultLanguage() const 0070 { 0071 if (!m_defaultLanguage.isEmpty()) { 0072 return m_defaultLanguage; 0073 } else { 0074 return "en"; 0075 } 0076 } 0077 0078 void Preferences::setDefaultLanguage(QString language) 0079 { 0080 if (language == m_defaultLanguage) { 0081 return; 0082 } 0083 0084 m_defaultLanguage = language; 0085 Q_EMIT defaultLanguageChanged(); 0086 0087 setPreferencesField(QStringLiteral("source[language]"), language); 0088 } 0089 0090 QString Preferences::extendMedia() const 0091 { 0092 return m_extendMedia; 0093 } 0094 0095 bool Preferences::extendSpoiler() const 0096 { 0097 return m_extendSpoiler; 0098 } 0099 0100 void Preferences::setPreferencesField(QString name, QString value) 0101 { 0102 auto multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); 0103 0104 QHttpPart preferencesPart; 0105 preferencesPart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(name)); 0106 preferencesPart.setBody(value.toUtf8()); 0107 multiPart->append(preferencesPart); 0108 0109 m_account->patch(m_account->apiUrl("/api/v1/accounts/update_credentials"), multiPart, true, this, [=](QNetworkReply *) {}); 0110 }