File indexing completed on 2023-11-26 08:17:55

0001 /*
0002    SPDX-FileCopyrightText: 2017-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ruqolaserverconfig.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QRegularExpression>
0013 #include <QStringList>
0014 
0015 RuqolaServerConfig::RuqolaServerConfig() = default;
0016 
0017 QString RuqolaServerConfig::uniqueId() const
0018 {
0019     return mUniqueId;
0020 }
0021 
0022 void RuqolaServerConfig::setUniqueId(const QString &uniqueId)
0023 {
0024     mUniqueId = uniqueId;
0025 }
0026 
0027 QString RuqolaServerConfig::jitsiMeetUrl() const
0028 {
0029     return mJitsiMeetUrl;
0030 }
0031 
0032 void RuqolaServerConfig::setJitsiMeetUrl(const QString &jitsiMeetUrl)
0033 {
0034     mJitsiMeetUrl = jitsiMeetUrl;
0035 }
0036 
0037 QString RuqolaServerConfig::jitsiMeetPrefix() const
0038 {
0039     return mJitsiMeetPrefix;
0040 }
0041 
0042 void RuqolaServerConfig::setJitsiMeetPrefix(const QString &jitsiMeetPrefix)
0043 {
0044     mJitsiMeetPrefix = jitsiMeetPrefix;
0045 }
0046 
0047 QString RuqolaServerConfig::fileUploadStorageType() const
0048 {
0049     return mFileUploadStorageType;
0050 }
0051 
0052 void RuqolaServerConfig::setFileUploadStorageType(const QString &type)
0053 {
0054     mFileUploadStorageType = type;
0055 }
0056 
0057 void RuqolaServerConfig::setBlockDeletingMessageInMinutes(int minutes)
0058 {
0059     mBlockDeletingMessageInMinutes = minutes;
0060 }
0061 
0062 void RuqolaServerConfig::setBlockEditingMessageInMinutes(int minutes)
0063 {
0064     mBlockEditingMessageInMinutes = minutes;
0065 }
0066 
0067 int RuqolaServerConfig::blockEditingMessageInMinutes() const
0068 {
0069     return mBlockEditingMessageInMinutes;
0070 }
0071 
0072 bool RuqolaServerConfig::needAdaptNewSubscriptionRC60() const
0073 {
0074     return mNeedAdaptNewSubscriptionRC60;
0075 }
0076 
0077 bool RuqolaServerConfig::hasAtLeastVersion(int major, int minor, int patch) const
0078 {
0079     //    qDebug() << " major " << major << " mServerVersionMajor " << mServerVersionMajor << " (major <= mServerVersionMajor) " << (major <=
0080     //    mServerVersionMajor) <<
0081     //                " minor " << minor << " mServerVersionMinor  " << mServerVersionMinor << " (minor <= mServerVersionMinor) " << (minor <=
0082     //                mServerVersionMinor) << " patch " << patch << " mServerVersionPatch " << mServerVersionPatch << " (patch <= mServerVersionPatch) " <<
0083     //                (patch <= mServerVersionPatch);
0084     if (mServerVersionMajor > major) {
0085         return true;
0086     }
0087     return (major <= mServerVersionMajor) && (minor <= mServerVersionMinor) && (patch <= mServerVersionPatch);
0088 }
0089 
0090 void RuqolaServerConfig::setServerVersion(const QString &version)
0091 {
0092     mServerVersionStr = version;
0093     const QStringList lst = version.split(QLatin1Char('.'));
0094     // 0.70.0-rc.1 has 4 "."
0095     if (lst.count() >= 3) {
0096         bool ok;
0097         int value = lst.at(0).toInt(&ok);
0098         if (ok) {
0099             mServerVersionMajor = value;
0100         }
0101         value = lst.at(1).toInt(&ok);
0102         if (ok) {
0103             mServerVersionMinor = value;
0104         }
0105         value = lst.at(2).toInt(&ok);
0106         if (ok) {
0107             mServerVersionPatch = value;
0108         } else { // Perhaps it has "rc"/"beta" etc.
0109             mServerVersionPatch = 0;
0110         }
0111     } else if (lst.count() == 2) { // As "4.0"
0112         bool ok;
0113         int value = lst.at(0).toInt(&ok);
0114         if (ok) {
0115             mServerVersionMajor = value;
0116         }
0117         value = lst.at(1).toInt(&ok);
0118         if (ok) {
0119             mServerVersionMinor = value;
0120         }
0121         mServerVersionPatch = 0;
0122     }
0123     adaptToServerVersion();
0124 }
0125 
0126 QString RuqolaServerConfig::serverVersion() const
0127 {
0128     return mServerVersionStr;
0129 }
0130 
0131 bool RuqolaServerConfig::ruqolaHasSupportForOauthType(AuthenticationManager::OauthType type) const
0132 {
0133     return mRuqolaOauthTypes & type;
0134 }
0135 
0136 bool RuqolaServerConfig::canShowOauthService(AuthenticationManager::OauthType type) const
0137 {
0138     return serverHasSupportForOauthType(type) && ruqolaHasSupportForOauthType(type);
0139 }
0140 
0141 void RuqolaServerConfig::addRuqolaAuthenticationSupport(AuthenticationManager::OauthType type)
0142 {
0143     mRuqolaOauthTypes |= type;
0144 }
0145 
0146 bool RuqolaServerConfig::serverHasSupportForOauthType(AuthenticationManager::OauthType type) const
0147 {
0148     return mServerOauthTypes & type;
0149 }
0150 
0151 void RuqolaServerConfig::addOauthService(const QString &service)
0152 {
0153     const QString serviceLower = service.toLower();
0154     if (serviceLower.endsWith(QLatin1String("twitter"))) {
0155         mServerOauthTypes |= AuthenticationManager::OauthType::Twitter;
0156     } else if (serviceLower.endsWith(QLatin1String("facebook"))) {
0157         mServerOauthTypes |= AuthenticationManager::OauthType::FaceBook;
0158     } else if (serviceLower.endsWith(QLatin1String("github"))) {
0159         mServerOauthTypes |= AuthenticationManager::OauthType::GitHub;
0160     } else if (serviceLower.endsWith(QLatin1String("gitlab"))) {
0161         mServerOauthTypes |= AuthenticationManager::OauthType::GitLab;
0162     } else if (serviceLower.endsWith(QLatin1String("google"))) {
0163         mServerOauthTypes |= AuthenticationManager::OauthType::Google;
0164     } else if (serviceLower.endsWith(QLatin1String("linkedin"))) {
0165         mServerOauthTypes |= AuthenticationManager::OauthType::Linkedin;
0166     } else if (serviceLower.endsWith(QLatin1String("wordpress"))) {
0167         mServerOauthTypes |= AuthenticationManager::OauthType::Wordpress;
0168     } else if (serviceLower.endsWith(QLatin1String("_oauth_proxy_host"))) {
0169         // Hide warning as it's not a service
0170         qCDebug(RUQOLA_LOG) << "_OAuth_Proxy_host found ";
0171     } else if (serviceLower.endsWith(QLatin1String("_oauth_meteor"))) {
0172         qCDebug(RUQOLA_LOG) << "Accounts_OAuth_Meteor found ";
0173     } else {
0174         qCDebug(RUQOLA_LOG) << "Unknown service type: " << service;
0175     }
0176 }
0177 
0178 void RuqolaServerConfig::adaptToServerVersion()
0179 {
0180     mNeedAdaptNewSubscriptionRC60 = (mServerVersionMajor >= 1) || ((mServerVersionMajor == 0) && (mServerVersionMinor >= 60));
0181 }
0182 
0183 bool RuqolaServerConfig::messageAllowConvertLongMessagesToAttachment() const
0184 {
0185     return mMessageAllowConvertLongMessagesToAttachment;
0186 }
0187 
0188 void RuqolaServerConfig::setMessageAllowConvertLongMessagesToAttachment(bool newMessageAllowConvertLongMessagesToAttachment)
0189 {
0190     mMessageAllowConvertLongMessagesToAttachment = newMessageAllowConvertLongMessagesToAttachment;
0191 }
0192 
0193 void RuqolaServerConfig::privateSettingsUpdated(const QJsonArray &updateArray)
0194 {
0195     if (updateArray.count() == 2) {
0196         const QString updateArrayInfo{updateArray.at(0).toString()};
0197         if (updateArrayInfo == QLatin1String("updated")) {
0198             loadSettings(updateArray.at(1).toObject());
0199             qDebug() << "Update settings " << *this;
0200         } else {
0201             qCWarning(RUQOLA_LOG) << "UpdateArray info unknown: " << updateArrayInfo;
0202         }
0203     } else {
0204         qCWarning(RUQOLA_LOG) << "Error in privateSettingsUpdated " << updateArray;
0205     }
0206 }
0207 
0208 int RuqolaServerConfig::messageMaximumAllowedSize() const
0209 {
0210     return mMessageMaximumAllowedSize;
0211 }
0212 
0213 void RuqolaServerConfig::setMessageMaximumAllowedSize(int newMessageMaximumAllowedSize)
0214 {
0215     mMessageMaximumAllowedSize = newMessageMaximumAllowedSize;
0216 }
0217 
0218 const QString &RuqolaServerConfig::userNameValidation() const
0219 {
0220     return mUserNameValidation;
0221 }
0222 
0223 const QString &RuqolaServerConfig::channelNameValidation() const
0224 {
0225     return mChannelNameValidation;
0226 }
0227 
0228 int RuqolaServerConfig::loginExpiration() const
0229 {
0230     return mLoginExpiration;
0231 }
0232 
0233 void RuqolaServerConfig::setChannelNameValidation(const QString &str)
0234 {
0235     mChannelNameValidation = str;
0236 }
0237 
0238 void RuqolaServerConfig::setUserNameValidation(const QString &str)
0239 {
0240     mUserNameValidation = str;
0241 }
0242 
0243 void RuqolaServerConfig::setLoginExpiration(int loginExpiration)
0244 {
0245     mLoginExpiration = loginExpiration;
0246 }
0247 
0248 RuqolaServerConfig::ServerConfigFeatureTypes RuqolaServerConfig::serverConfigFeatureTypes() const
0249 {
0250     return mServerConfigFeatureTypes;
0251 }
0252 
0253 void RuqolaServerConfig::setServerConfigFeatureTypes(ServerConfigFeatureTypes serverConfigFeatureTypes)
0254 {
0255     mServerConfigFeatureTypes = serverConfigFeatureTypes;
0256 }
0257 
0258 void RuqolaServerConfig::setAllowRegistrationFrom(const QString &registrationFromValue)
0259 {
0260     if (registrationFromValue == QLatin1String("Public")) {
0261         mServerConfigFeatureTypes |= ServerConfigFeatureType::RegistrationFromEnabled;
0262     } else if (registrationFromValue == QLatin1String("Disabled")) {
0263         // Nothing => disabled
0264         ;
0265     } else if (registrationFromValue == QLatin1String("Secret URL")) {
0266         qCWarning(RUQOLA_LOG) << " Registration Secret Url not implemented";
0267     }
0268 }
0269 
0270 qint64 RuqolaServerConfig::fileMaxFileSize() const
0271 {
0272     return mFileMaxFileSize;
0273 }
0274 
0275 void RuqolaServerConfig::setFileMaxFileSize(qint64 fileMaxFileSize)
0276 {
0277     mFileMaxFileSize = fileMaxFileSize;
0278 }
0279 
0280 int RuqolaServerConfig::blockDeletingMessageInMinutes() const
0281 {
0282     return mBlockDeletingMessageInMinutes;
0283 }
0284 
0285 QString RuqolaServerConfig::autoTranslateGoogleKey() const
0286 {
0287     return mAutoTranslateGoogleKey;
0288 }
0289 
0290 void RuqolaServerConfig::setAutoTranslateGoogleKey(const QString &autoTranslateGoogleKey)
0291 {
0292     mAutoTranslateGoogleKey = autoTranslateGoogleKey;
0293 }
0294 
0295 int RuqolaServerConfig::serverVersionPatch() const
0296 {
0297     return mServerVersionPatch;
0298 }
0299 
0300 int RuqolaServerConfig::serverVersionMinor() const
0301 {
0302     return mServerVersionMinor;
0303 }
0304 
0305 int RuqolaServerConfig::serverVersionMajor() const
0306 {
0307     return mServerVersionMajor;
0308 }
0309 
0310 QString RuqolaServerConfig::serverVersionStr() const
0311 {
0312     return mServerVersionStr;
0313 }
0314 
0315 QString RuqolaServerConfig::siteName() const
0316 {
0317     return mSiteName;
0318 }
0319 
0320 void RuqolaServerConfig::setSiteName(const QString &siteName)
0321 {
0322     mSiteName = siteName;
0323 }
0324 
0325 QString RuqolaServerConfig::siteUrl() const
0326 {
0327     return mSiteUrl;
0328 }
0329 
0330 void RuqolaServerConfig::setSiteUrl(const QString &siteUrl)
0331 {
0332     mSiteUrl = siteUrl;
0333 }
0334 
0335 AuthenticationManager::OauthTypes RuqolaServerConfig::ruqolaOauthTypes() const
0336 {
0337     return mRuqolaOauthTypes;
0338 }
0339 
0340 AuthenticationManager::OauthTypes RuqolaServerConfig::serverOauthTypes() const
0341 {
0342     return mServerOauthTypes;
0343 }
0344 
0345 RuqolaServerConfig::ConfigWithDefaultValue RuqolaServerConfig::logoUrl() const
0346 {
0347     return mLogoUrl;
0348 }
0349 
0350 void RuqolaServerConfig::setLogoUrl(const RuqolaServerConfig::ConfigWithDefaultValue &url)
0351 {
0352     mLogoUrl = url;
0353 }
0354 
0355 RuqolaServerConfig::ConfigWithDefaultValue RuqolaServerConfig::faviconUrl() const
0356 {
0357     return mFaviconUrl;
0358 }
0359 
0360 void RuqolaServerConfig::setFaviconUrl(const RuqolaServerConfig::ConfigWithDefaultValue &url)
0361 {
0362     mFaviconUrl = url;
0363 }
0364 
0365 QDebug operator<<(QDebug d, const RuqolaServerConfig::ConfigWithDefaultValue &t)
0366 {
0367     d << " Value " << t.url;
0368     d << " Default Value " << t.defaultUrl;
0369     return d;
0370 }
0371 
0372 QDebug operator<<(QDebug d, const RuqolaServerConfig &t)
0373 {
0374     d << "mUniqueId  " << t.uniqueId();
0375     d << "mJitsiMeetUrl " << t.jitsiMeetUrl();
0376     d << "mJitsiMeetPrefix " << t.jitsiMeetPrefix();
0377     d << "mFileUploadStorageType " << t.fileUploadStorageType();
0378     d << "mSiteUrl " << t.siteUrl();
0379     d << "mSiteName " << t.siteName();
0380     d << "mServerOauthTypes " << t.serverOauthTypes();
0381     d << "mRuqolaOauthTypes " << t.ruqolaOauthTypes();
0382     d << "mBlockEditingMessageInMinutes " << t.blockEditingMessageInMinutes();
0383     d << "mNeedAdaptNewSubscriptionRC60 " << t.needAdaptNewSubscriptionRC60();
0384     d << "mServerVersionMajor " << t.serverVersionMajor() << " mServerVersionMinor " << t.serverVersionMinor() << " mServerVersionPatch "
0385       << t.serverVersionPatch();
0386     d << "mLogoUrl " << t.logoUrl();
0387     d << "mFaviconUrl " << t.faviconUrl();
0388     d << "mLoginExpiration " << t.loginExpiration();
0389     d << "mChannelNameValidation " << t.channelNameValidation();
0390     d << "mUserNameValidation " << t.userNameValidation();
0391     d << "mMessageMaximumAllowedSize " << t.messageMaximumAllowedSize();
0392     d << "mMessageAllowConvertLongMessagesToAttachment " << t.messageAllowConvertLongMessagesToAttachment();
0393     d << "mUIUseRealName " << t.useRealName();
0394     d << "mAccountsAllowInvisibleStatusOption" << t.accountsAllowInvisibleStatusOption();
0395     d << "mUserDataDownloadEnabled " << t.userDataDownloadEnabled();
0396     d << "mMediaBlackList " << t.mediaBlackList();
0397     d << "mMediaWhiteList " << t.mediaWhiteList();
0398     return d;
0399 }
0400 
0401 void RuqolaServerConfig::assignSettingValue(bool value, ServerConfigFeatureType type)
0402 {
0403     if (value) {
0404         mServerConfigFeatureTypes |= type;
0405     } else {
0406         mServerConfigFeatureTypes &= ~type;
0407     }
0408 }
0409 
0410 RuqolaServerConfig::ConfigWithDefaultValue RuqolaServerConfig::parseConfigWithDefaultValue(const QJsonObject &o)
0411 {
0412     RuqolaServerConfig::ConfigWithDefaultValue value;
0413     value.defaultUrl = o[QLatin1String("defaultUrl")].toString();
0414     value.url = o[QLatin1String("url")].toString();
0415     return value;
0416 }
0417 
0418 void RuqolaServerConfig::loadSettings(const QJsonObject &currentConfObject)
0419 {
0420     // qDebug() << " currentConfObject " << currentConfObject;
0421     const QString id = currentConfObject[QLatin1String("_id")].toString();
0422     const QVariant value = currentConfObject[QLatin1String("value")].toVariant();
0423     static const QRegularExpression regExp(QStringLiteral("^Accounts_OAuth_\\w+"));
0424     if (id == QLatin1String("uniqueID")) {
0425         setUniqueId(value.toString());
0426     } else if (id == QLatin1String("Jitsi_Enabled")) {
0427         assignSettingValue(value.toBool(), ServerConfigFeatureType::JitsiEnabled);
0428     } else if (id == QLatin1String("Jitsi_Enable_Teams")) {
0429         assignSettingValue(value.toBool(), ServerConfigFeatureType::JitsiEnabledTeams);
0430     } else if (id == QLatin1String("Jitsi_Enable_Channels")) {
0431         assignSettingValue(value.toBool(), ServerConfigFeatureType::JitsiEnabledChannels);
0432     } else if (id == QLatin1String("Jitsi_Domain")) {
0433         setJitsiMeetUrl(value.toString());
0434     } else if (id == QLatin1String("Jitsi_URL_Room_Prefix")) {
0435         setJitsiMeetPrefix(value.toString());
0436     } else if (id == QLatin1String("FileUpload_Storage_Type")) {
0437         setFileUploadStorageType(value.toString());
0438     } else if (id == QLatin1String("Message_AllowEditing")) {
0439         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowEditingMessage);
0440     } else if (id == QLatin1String("Message_AllowEditing_BlockEditInMinutes")) {
0441         setBlockEditingMessageInMinutes(value.toInt());
0442     } else if (id == QLatin1String("Message_AllowDeleting_BlockDeleteInMinutes")) {
0443         setBlockDeletingMessageInMinutes(value.toInt());
0444     } else if (id == QLatin1String("OTR_Enable")) {
0445         assignSettingValue(value.toBool(), ServerConfigFeatureType::OtrEnabled);
0446     } else if (id.contains(regExp)) {
0447         if (value.toBool()) {
0448             addOauthService(id);
0449         }
0450     } else if (id == QLatin1String("Site_Url")) {
0451         setSiteUrl(value.toString());
0452     } else if (id == QLatin1String("Site_Name")) {
0453         setSiteName(value.toString());
0454     } else if (id == QLatin1String("E2E_Enable")) {
0455         assignSettingValue(value.toBool(), ServerConfigFeatureType::EncryptionEnabled);
0456     } else if (id == QLatin1String("Message_AllowPinning")) {
0457         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowMessagePinning);
0458     } else if (id == QLatin1String("Message_AllowStarring")) {
0459         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowMessageStarring);
0460     } else if (id == QLatin1String("Message_AllowDeleting")) {
0461         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowMessageDeleting);
0462     } else if (id == QLatin1String("Threads_enabled")) {
0463         assignSettingValue(value.toBool(), ServerConfigFeatureType::ThreadsEnabled);
0464     } else if (id == QLatin1String("Discussion_enabled")) {
0465         assignSettingValue(value.toBool(), ServerConfigFeatureType::DiscussionEnabled);
0466     } else if (id == QLatin1String("AutoTranslate_Enabled")) {
0467         assignSettingValue(value.toBool(), ServerConfigFeatureType::AutoTranslateEnabled);
0468     } else if (id == QLatin1String("AutoTranslate_GoogleAPIKey")) {
0469         setAutoTranslateGoogleKey(value.toString());
0470     } else if (id == QLatin1String("FileUpload_Enabled")) {
0471         assignSettingValue(value.toBool(), ServerConfigFeatureType::UploadFileEnabled);
0472     } else if (id == QLatin1String("FileUpload_MaxFileSize")) {
0473         setFileMaxFileSize(value.toULongLong());
0474     } else if (id == QLatin1String("Broadcasting_enabled")) {
0475         assignSettingValue(value.toBool(), ServerConfigFeatureType::BroadCastEnabled);
0476     } else if (id == QLatin1String("Message_VideoRecorderEnabled")) {
0477         assignSettingValue(value.toBool(), ServerConfigFeatureType::VideoRecorderEnabled);
0478     } else if (id == QLatin1String("Message_AudioRecorderEnabled")) {
0479         assignSettingValue(value.toBool(), ServerConfigFeatureType::AudioRecorderEnabled);
0480     } else if (id == QLatin1String("Assets_logo")) {
0481         setLogoUrl(parseConfigWithDefaultValue(value.toJsonObject()));
0482     } else if (id == QLatin1String("Assets_favicon")) {
0483         setFaviconUrl(parseConfigWithDefaultValue(value.toJsonObject()));
0484     } else if (id == QLatin1String("Accounts_AllowDeleteOwnAccount")) {
0485         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowDeleteOwnAccount);
0486     } else if (id == QLatin1String("Accounts_RegistrationForm")) {
0487         setAllowRegistrationFrom(value.toString());
0488     } else if (id == QLatin1String("Accounts_PasswordReset")) {
0489         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowPasswordReset);
0490     } else if (id == QLatin1String("Accounts_AllowEmailChange")) {
0491         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowEmailChange);
0492     } else if (id == QLatin1String("Accounts_AllowPasswordChange")) {
0493         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowPasswordChange);
0494     } else if (id == QLatin1String("Accounts_AllowUsernameChange")) {
0495         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowUsernameChange);
0496     } else if (id == QLatin1String("Accounts_AllowUserProfileChange")) {
0497         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowUserProfileChange);
0498     } else if (id == QLatin1String("Accounts_AllowUserAvatarChange")) {
0499         assignSettingValue(value.toBool(), ServerConfigFeatureType::AllowUserAvatarChange);
0500     } else if (id == QLatin1String("LDAP_Enable")) {
0501         assignSettingValue(value.toBool(), ServerConfigFeatureType::LdapEnabled);
0502     } else if (id == QLatin1String("Accounts_LoginExpiration")) {
0503         setLoginExpiration(value.toInt());
0504     } else if (id == QLatin1String("Accounts_TwoFactorAuthentication_Enabled")) {
0505         assignSettingValue(value.toBool(), ServerConfigFeatureType::TwoFactorAuthenticationEnabled);
0506     } else if (id == QLatin1String("Accounts_TwoFactorAuthentication_By_Email_Enabled")) {
0507         assignSettingValue(value.toBool(), ServerConfigFeatureType::TwoFactorAuthenticationByEmailEnabled);
0508     } else if (id == QLatin1String("Accounts_TwoFactorAuthentication_By_TOTP_Enabled")) {
0509         assignSettingValue(value.toBool(), ServerConfigFeatureType::TwoFactorAuthenticationByTOTPEnabled);
0510     } else if (id == QLatin1String("Accounts_TwoFactorAuthentication_Enforce_Password_Fallback")) {
0511         assignSettingValue(value.toBool(), ServerConfigFeatureType::TwoFactorAuthenticationEnforcePasswordFallback);
0512     } else if (id == QLatin1String("UTF8_Channel_Names_Validation")) {
0513         setChannelNameValidation(value.toString());
0514     } else if (id == QLatin1String("UTF8_User_Names_Validation")) {
0515         setUserNameValidation(value.toString());
0516     } else if (id == QLatin1String("Message_MaxAllowedSize")) {
0517         setMessageMaximumAllowedSize(value.toInt());
0518     } else if (id == QLatin1String("Message_AllowConvertLongMessagesToAttachment")) {
0519         setMessageAllowConvertLongMessagesToAttachment(value.toBool());
0520     } else if (id == QLatin1String("UI_Use_Real_Name")) {
0521         setUseRealName(value.toBool());
0522     } else if (id == QLatin1String("Accounts_AllowInvisibleStatusOption")) {
0523         setAccountsAllowInvisibleStatusOption(value.toBool());
0524     } else if (id == QLatin1String("UserData_EnableDownload")) {
0525         setUserDataDownloadEnabled(value.toBool());
0526     } else if (id == QLatin1String("Device_Management_Enable_Login_Emails")) {
0527         setDeviceManagementEnableLoginEmails(value.toBool());
0528     } else if (id == QLatin1String("Device_Management_Allow_Login_Email_preference")) {
0529         setDeviceManagementAllowLoginEmailpreference(value.toBool());
0530     } else if (id == QLatin1String("Message_GroupingPeriod")) {
0531         setMessageGroupingPeriod(value.toInt());
0532     } else if (id == QLatin1String("DirectMesssage_maxUsers")) {
0533         setDirectMessageMaximumUser(value.toInt());
0534     } else if (id == QLatin1String("Message_QuoteChainLimit")) {
0535         setMessageQuoteChainLimit(value.toInt());
0536     } else if (id == QLatin1String("Accounts_AllowUserStatusMessageChange")) {
0537         setAllowCustomStatusMessage(value.toBool());
0538     } else if (id == QLatin1String("FileUpload_MediaTypeWhiteList")) {
0539         setMediaWhiteList(value.toString().split(QLatin1Char(','), Qt::SkipEmptyParts));
0540     } else if (id == QLatin1String("FileUpload_MediaTypeBlackList")) {
0541         setMediaBlackList(value.toString().split(QLatin1Char(','), Qt::SkipEmptyParts));
0542     } else {
0543         qCDebug(RUQOLA_LOG) << "Other public settings id " << id << value;
0544     }
0545 }
0546 
0547 QStringList RuqolaServerConfig::mediaBlackList() const
0548 {
0549     return mMediaBlackList;
0550 }
0551 
0552 void RuqolaServerConfig::setMediaBlackList(const QStringList &newMediaBlackList)
0553 {
0554     mMediaBlackList = newMediaBlackList;
0555 }
0556 
0557 QJsonObject RuqolaServerConfig::createJsonObject(const QString &identifier, const QString &value)
0558 {
0559     QJsonObject v;
0560     v[QLatin1String("_id")] = identifier;
0561     v[QLatin1String("value")] = value;
0562     return v;
0563 }
0564 
0565 QJsonObject RuqolaServerConfig::createJsonObject(const QString &identifier, const RuqolaServerConfig::ConfigWithDefaultValue &value)
0566 {
0567     QJsonObject v;
0568     v[QLatin1String("_id")] = identifier;
0569     QJsonObject customUrl;
0570     customUrl[QLatin1String("defaultUrl")] = value.defaultUrl;
0571     customUrl[QLatin1String("url")] = value.url;
0572     v[QLatin1String("value")] = customUrl;
0573     return v;
0574 }
0575 
0576 QJsonObject RuqolaServerConfig::createJsonObject(const QString &identifier, qint64 value)
0577 {
0578     QJsonObject v;
0579     v[QLatin1String("_id")] = identifier;
0580     v[QLatin1String("value")] = static_cast<qint64>(value);
0581     return v;
0582 }
0583 
0584 QJsonObject RuqolaServerConfig::createJsonObject(const QString &identifier, bool value)
0585 {
0586     QJsonObject v;
0587     v[QLatin1String("_id")] = identifier;
0588     v[QLatin1String("value")] = value;
0589     return v;
0590 }
0591 
0592 QJsonObject RuqolaServerConfig::createJsonObject(const QString &identifier, int value)
0593 {
0594     QJsonObject v;
0595     v[QLatin1String("_id")] = identifier;
0596     v[QLatin1String("value")] = value;
0597     return v;
0598 }
0599 
0600 QByteArray RuqolaServerConfig::serialize(bool toBinary)
0601 {
0602     QJsonObject o;
0603     QJsonArray array;
0604     array.append(createJsonObject(QStringLiteral("uniqueID"), mUniqueId));
0605     array.append(createJsonObject(QStringLiteral("Jitsi_Enabled"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::JitsiEnabled)));
0606     array.append(
0607         createJsonObject(QStringLiteral("Jitsi_Enable_Teams"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::JitsiEnabledTeams)));
0608     array.append(createJsonObject(QStringLiteral("Jitsi_Enable_Channels"),
0609                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::JitsiEnabledChannels)));
0610     array.append(createJsonObject(QStringLiteral("Jitsi_Domain"), jitsiMeetUrl()));
0611     array.append(createJsonObject(QStringLiteral("Jitsi_URL_Room_Prefix"), jitsiMeetPrefix()));
0612     array.append(createJsonObject(QStringLiteral("FileUpload_Storage_Type"), fileUploadStorageType()));
0613     array.append(
0614         createJsonObject(QStringLiteral("Message_AllowEditing"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowEditingMessage)));
0615     array.append(createJsonObject(QStringLiteral("Message_AllowEditing_BlockEditInMinutes"), blockEditingMessageInMinutes()));
0616     array.append(createJsonObject(QStringLiteral("Message_AllowDeleting_BlockDeleteInMinutes"), blockDeletingMessageInMinutes()));
0617     array.append(createJsonObject(QStringLiteral("OTR_Enable"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::OtrEnabled)));
0618     array.append(createJsonObject(QStringLiteral("Site_Url"), mSiteUrl));
0619     array.append(createJsonObject(QStringLiteral("Site_Name"), siteName()));
0620     array.append(createJsonObject(QStringLiteral("E2E_Enable"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::EncryptionEnabled)));
0621     array.append(
0622         createJsonObject(QStringLiteral("Message_AllowPinning"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowMessagePinning)));
0623     array.append(createJsonObject(QStringLiteral("Message_AllowStarring"),
0624                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowMessageStarring)));
0625     array.append(createJsonObject(QStringLiteral("Message_AllowDeleting"),
0626                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowMessageDeleting)));
0627     array.append(createJsonObject(QStringLiteral("Threads_enabled"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::ThreadsEnabled)));
0628     array.append(
0629         createJsonObject(QStringLiteral("Discussion_enabled"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::DiscussionEnabled)));
0630     array.append(createJsonObject(QStringLiteral("AutoTranslate_Enabled"),
0631                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AutoTranslateEnabled)));
0632     array.append(
0633         createJsonObject(QStringLiteral("FileUpload_Enabled"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::UploadFileEnabled)));
0634     array.append(createJsonObject(QStringLiteral("AutoTranslate_GoogleAPIKey"), autoTranslateGoogleKey()));
0635     array.append(
0636         createJsonObject(QStringLiteral("Broadcasting_enabled"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::BroadCastEnabled)));
0637     array.append(createJsonObject(QStringLiteral("Message_VideoRecorderEnabled"),
0638                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::VideoRecorderEnabled)));
0639     array.append(createJsonObject(QStringLiteral("Message_AudioRecorderEnabled"),
0640                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AudioRecorderEnabled)));
0641     array.append(createJsonObject(QStringLiteral("Accounts_AllowDeleteOwnAccount"),
0642                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowDeleteOwnAccount)));
0643     array.append(createJsonObject(QStringLiteral("Accounts_PasswordReset"),
0644                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowPasswordReset)));
0645     array.append(createJsonObject(QStringLiteral("Accounts_AllowEmailChange"),
0646                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowEmailChange)));
0647     array.append(createJsonObject(QStringLiteral("Accounts_AllowPasswordChange"),
0648                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowPasswordChange)));
0649     array.append(createJsonObject(QStringLiteral("Accounts_AllowUsernameChange"),
0650                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowUsernameChange)));
0651 
0652     array.append(createJsonObject(QStringLiteral("Accounts_AllowUserProfileChange"),
0653                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowUserProfileChange)));
0654     array.append(createJsonObject(QStringLiteral("Accounts_AllowUserAvatarChange"),
0655                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::AllowUserAvatarChange)));
0656     array.append(createJsonObject(QStringLiteral("LDAP_Enable"), static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::LdapEnabled)));
0657     array.append(createJsonObject(QStringLiteral("Accounts_TwoFactorAuthentication_Enabled"),
0658                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::TwoFactorAuthenticationEnabled)));
0659     array.append(createJsonObject(QStringLiteral("Accounts_TwoFactorAuthentication_By_Email_Enabled"),
0660                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::TwoFactorAuthenticationByEmailEnabled)));
0661     array.append(createJsonObject(QStringLiteral("Accounts_TwoFactorAuthentication_By_TOTP_Enabled"),
0662                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::TwoFactorAuthenticationByTOTPEnabled)));
0663     array.append(createJsonObject(QStringLiteral("Accounts_TwoFactorAuthentication_Enforce_Password_Fallback"),
0664                                   static_cast<bool>(serverConfigFeatureTypes() & ServerConfigFeatureType::TwoFactorAuthenticationEnforcePasswordFallback)));
0665     array.append(createJsonObject(QStringLiteral("Assets_logo"), logoUrl()));
0666     array.append(createJsonObject(QStringLiteral("Assets_favicon"), faviconUrl()));
0667     array.append(createJsonObject(QStringLiteral("Accounts_LoginExpiration"), loginExpiration()));
0668     array.append(createJsonObject(QStringLiteral("UTF8_Channel_Names_Validation"), channelNameValidation()));
0669     array.append(createJsonObject(QStringLiteral("UTF8_User_Names_Validation"), userNameValidation()));
0670     array.append(createJsonObject(QStringLiteral("Message_MaxAllowedSize"), messageMaximumAllowedSize()));
0671     array.append(createJsonObject(QStringLiteral("Message_AllowConvertLongMessagesToAttachment"), messageAllowConvertLongMessagesToAttachment()));
0672     array.append(createJsonObject(QStringLiteral("UI_Use_Real_Name"), useRealName()));
0673     array.append(createJsonObject(QStringLiteral("Accounts_AllowInvisibleStatusOption"), accountsAllowInvisibleStatusOption()));
0674     array.append(createJsonObject(QStringLiteral("UserData_EnableDownload"), userDataDownloadEnabled()));
0675     array.append(createJsonObject(QStringLiteral("Device_Management_Enable_Login_Emails"), deviceManagementEnableLoginEmails()));
0676     array.append(createJsonObject(QStringLiteral("Device_Management_Allow_Login_Email_preference"), deviceManagementAllowLoginEmailpreference()));
0677     array.append(createJsonObject(QStringLiteral("Message_GroupingPeriod"), messageGroupingPeriod()));
0678     array.append(createJsonObject(QStringLiteral("DirectMesssage_maxUsers"), directMessageMaximumUser()));
0679     array.append(createJsonObject(QStringLiteral("Message_QuoteChainLimit"), messageQuoteChainLimit()));
0680     array.append(createJsonObject(QStringLiteral("Accounts_AllowUserStatusMessageChange"), allowCustomStatusMessage()));
0681     array.append(createJsonObject(QStringLiteral("FileUpload_MediaTypeWhiteList"), mMediaWhiteList.join(QLatin1Char(','))));
0682     array.append(createJsonObject(QStringLiteral("FileUpload_MediaTypeBlackList"), mMediaBlackList.join(QLatin1Char(','))));
0683     array.append(createJsonObject(QStringLiteral("FileUpload_MaxFileSize"), mFileMaxFileSize));
0684 
0685     o[QLatin1String("result")] = array;
0686 #if 0
0687 } else if (id.contains(regExp)) {
0688     if (value.toBool()) {
0689         addOauthService(id);
0690     }
0691 } else if (id == QLatin1String("Accounts_RegistrationForm")) {
0692     setAllowRegistrationFrom(value.toString());
0693 
0694 #endif
0695     if (toBinary) {
0696         return QCborValue::fromJsonValue(o).toCbor();
0697     }
0698     QJsonDocument d;
0699     d.setObject(o);
0700     return d.toJson(QJsonDocument::Indented);
0701 }
0702 
0703 void RuqolaServerConfig::deserialize(const QJsonObject &obj)
0704 {
0705     QJsonArray configs = obj.value(QLatin1String("result")).toArray();
0706     mServerConfigFeatureTypes = ServerConfigFeatureType::None;
0707     for (QJsonValueRef currentConfig : configs) {
0708         const QJsonObject currentConfObject = currentConfig.toObject();
0709         loadSettings(currentConfObject);
0710     }
0711 }
0712 
0713 QStringList RuqolaServerConfig::mediaWhiteList() const
0714 {
0715     return mMediaWhiteList;
0716 }
0717 
0718 void RuqolaServerConfig::setMediaWhiteList(const QStringList &newMediaWhiteList)
0719 {
0720     mMediaWhiteList = newMediaWhiteList;
0721 }
0722 
0723 bool RuqolaServerConfig::allowCustomStatusMessage() const
0724 {
0725     return mAllowCustomStatusMessage;
0726 }
0727 
0728 void RuqolaServerConfig::setAllowCustomStatusMessage(bool newAllowCustomStatusMessage)
0729 {
0730     mAllowCustomStatusMessage = newAllowCustomStatusMessage;
0731 }
0732 
0733 int RuqolaServerConfig::messageQuoteChainLimit() const
0734 {
0735     return mMessageQuoteChainLimit;
0736 }
0737 
0738 void RuqolaServerConfig::setMessageQuoteChainLimit(int newMessageQuoteChainLimit)
0739 {
0740     mMessageQuoteChainLimit = newMessageQuoteChainLimit;
0741 }
0742 
0743 int RuqolaServerConfig::directMessageMaximumUser() const
0744 {
0745     return mDirectMessageMaximumUser;
0746 }
0747 
0748 void RuqolaServerConfig::setDirectMessageMaximumUser(int newDirectMessageMaximumUser)
0749 {
0750     mDirectMessageMaximumUser = newDirectMessageMaximumUser;
0751 }
0752 
0753 int RuqolaServerConfig::messageGroupingPeriod() const
0754 {
0755     return mMessageGroupingPeriod;
0756 }
0757 
0758 void RuqolaServerConfig::setMessageGroupingPeriod(int newMessageGroupingPeriod)
0759 {
0760     mMessageGroupingPeriod = newMessageGroupingPeriod;
0761 }
0762 
0763 bool RuqolaServerConfig::deviceManagementAllowLoginEmailpreference() const
0764 {
0765     return mDeviceManagementAllowLoginEmailpreference;
0766 }
0767 
0768 void RuqolaServerConfig::setDeviceManagementAllowLoginEmailpreference(bool newDeviceManagementAllowLoginEmailpreference)
0769 {
0770     mDeviceManagementAllowLoginEmailpreference = newDeviceManagementAllowLoginEmailpreference;
0771 }
0772 
0773 bool RuqolaServerConfig::deviceManagementEnableLoginEmails() const
0774 {
0775     return mDeviceManagementEnableLoginEmails;
0776 }
0777 
0778 void RuqolaServerConfig::setDeviceManagementEnableLoginEmails(bool newDeviceManagementEnableLoginEmails)
0779 {
0780     mDeviceManagementEnableLoginEmails = newDeviceManagementEnableLoginEmails;
0781 }
0782 
0783 bool RuqolaServerConfig::userDataDownloadEnabled() const
0784 {
0785     return mUserDataDownloadEnabled;
0786 }
0787 
0788 void RuqolaServerConfig::setUserDataDownloadEnabled(bool newUserDataDownloadEnabled)
0789 {
0790     mUserDataDownloadEnabled = newUserDataDownloadEnabled;
0791 }
0792 
0793 bool RuqolaServerConfig::accountsAllowInvisibleStatusOption() const
0794 {
0795     return mAccountsAllowInvisibleStatusOption;
0796 }
0797 
0798 void RuqolaServerConfig::setAccountsAllowInvisibleStatusOption(bool newAccountsAllowInvisibleStatusOption)
0799 {
0800     mAccountsAllowInvisibleStatusOption = newAccountsAllowInvisibleStatusOption;
0801 }
0802 
0803 bool RuqolaServerConfig::hasEnterpriseSupport() const
0804 {
0805     return mHasEnterpriseSupport;
0806 }
0807 
0808 void RuqolaServerConfig::setHasEnterpriseSupport(bool newHasEnterpriseSupport)
0809 {
0810     mHasEnterpriseSupport = newHasEnterpriseSupport;
0811 }
0812 
0813 bool RuqolaServerConfig::useRealName() const
0814 {
0815     return mUIUseRealName;
0816 }
0817 
0818 void RuqolaServerConfig::setUseRealName(bool newUIUseRealName)
0819 {
0820     mUIUseRealName = newUIUseRealName;
0821 }
0822 
0823 void RuqolaServerConfig::parsePublicSettings(const QJsonObject &obj, bool update)
0824 {
0825     // qDebug() << " void RuqolaServerConfig::parsePublicSettings(const QJsonObject &obj)" << obj;
0826     QJsonArray configs = obj.value(QLatin1String("result")).toArray();
0827     if (!update) {
0828         mServerConfigFeatureTypes = ServerConfigFeatureType::None;
0829     }
0830     for (QJsonValueRef currentConfig : configs) {
0831         const QJsonObject currentConfObject = currentConfig.toObject();
0832         loadSettings(currentConfObject);
0833     }
0834 }
0835 
0836 bool RuqolaServerConfig::ConfigWithDefaultValue::operator==(const RuqolaServerConfig::ConfigWithDefaultValue &other) const
0837 {
0838     return other.url == url && other.defaultUrl == defaultUrl;
0839 }
0840 
0841 bool RuqolaServerConfig::operator==(const RuqolaServerConfig &other) const
0842 {
0843     return mUniqueId == other.mUniqueId && mJitsiMeetUrl == other.mJitsiMeetUrl && mJitsiMeetPrefix == other.mJitsiMeetPrefix
0844         && mFileUploadStorageType == other.mFileUploadStorageType && mSiteUrl == other.mSiteUrl && mSiteName == other.mSiteName
0845         && mServerVersionStr == other.mServerVersionStr && mAutoTranslateGoogleKey == other.mAutoTranslateGoogleKey
0846         && mChannelNameValidation == other.mChannelNameValidation && mUserNameValidation == other.mUserNameValidation
0847         && mServerOauthTypes == other.mServerOauthTypes && mRuqolaOauthTypes == other.mRuqolaOauthTypes
0848         && mBlockEditingMessageInMinutes == other.mBlockEditingMessageInMinutes && mBlockDeletingMessageInMinutes == other.mBlockDeletingMessageInMinutes
0849         && mServerVersionMajor == other.mServerVersionMajor && mServerVersionMinor == other.mServerVersionMinor
0850         && mServerVersionPatch == other.mServerVersionPatch && mFileMaxFileSize == other.mFileMaxFileSize
0851         && mNeedAdaptNewSubscriptionRC60 == other.mNeedAdaptNewSubscriptionRC60
0852         && mMessageAllowConvertLongMessagesToAttachment == other.mMessageAllowConvertLongMessagesToAttachment && mUIUseRealName == other.mUIUseRealName
0853         && mServerConfigFeatureTypes == other.mServerConfigFeatureTypes && mMediaWhiteList == other.mMediaWhiteList && mMediaBlackList == other.mMediaBlackList
0854         && mLogoUrl == other.mLogoUrl && mFaviconUrl == other.mFaviconUrl && mLoginExpiration == other.mLoginExpiration
0855         && mMessageMaximumAllowedSize == other.mMessageMaximumAllowedSize && mMessageGroupingPeriod == other.mMessageGroupingPeriod
0856         && mDirectMessageMaximumUser == other.mDirectMessageMaximumUser && mMessageQuoteChainLimit == other.mMessageQuoteChainLimit
0857         && mHasEnterpriseSupport == other.mHasEnterpriseSupport && mAccountsAllowInvisibleStatusOption == other.mAccountsAllowInvisibleStatusOption
0858         && mUserDataDownloadEnabled == other.mUserDataDownloadEnabled && mDeviceManagementEnableLoginEmails == other.mDeviceManagementEnableLoginEmails
0859         && mDeviceManagementAllowLoginEmailpreference == other.mDeviceManagementAllowLoginEmailpreference
0860         && mAllowCustomStatusMessage == other.mAllowCustomStatusMessage;
0861 }
0862 
0863 void RuqolaServerConfig::loadAccountSettingsFromLocalDataBase(const QByteArray &ba)
0864 {
0865     const QJsonDocument doc = QJsonDocument::fromJson(ba);
0866     const QJsonObject newObj = doc.object();
0867     deserialize(newObj);
0868 }
0869 
0870 #include "moc_ruqolaserverconfig.cpp"