File indexing completed on 2024-12-15 03:45:02
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "serverinfo.h" 0008 0009 #include <QSharedData> 0010 #include <QSettings> 0011 #include <QUrl> 0012 0013 using namespace KUserFeedback::Console; 0014 0015 namespace KUserFeedback { 0016 namespace Console { 0017 class ServerInfoData : public QSharedData 0018 { 0019 public: 0020 static QString groupName(const QString &name); 0021 0022 QString name; 0023 QUrl url; 0024 QString userName; 0025 QString password; 0026 }; 0027 0028 } 0029 } 0030 0031 QString ServerInfoData::groupName(const QString &name) 0032 { 0033 return QString::fromLatin1(QUrl::toPercentEncoding(name)); 0034 } 0035 0036 ServerInfo::ServerInfo() : d(new ServerInfoData) {} 0037 ServerInfo::ServerInfo(const ServerInfo&) = default; 0038 ServerInfo::~ServerInfo() = default; 0039 ServerInfo& ServerInfo::operator=(const ServerInfo&) = default; 0040 0041 bool ServerInfo::isValid() const 0042 { 0043 return d->url.isValid(); 0044 } 0045 0046 QString ServerInfo::name() const 0047 { 0048 // ### temporary 0049 if (d->name.isEmpty()) 0050 return d->url.toString(); 0051 return d->name; 0052 } 0053 0054 void ServerInfo::setName(const QString& name) 0055 { 0056 d->name = name; 0057 } 0058 0059 QUrl ServerInfo::url() const 0060 { 0061 return d->url; 0062 } 0063 0064 void ServerInfo::setUrl(const QUrl& url) 0065 { 0066 d->url = url; 0067 } 0068 0069 QString ServerInfo::userName() const 0070 { 0071 return d->userName; 0072 } 0073 0074 void ServerInfo::setUserName(const QString& userName) 0075 { 0076 d->userName = userName; 0077 } 0078 0079 QString ServerInfo::password() const 0080 { 0081 return d->password; 0082 } 0083 0084 void ServerInfo::setPassword(const QString& password) 0085 { 0086 d->password = password; 0087 } 0088 0089 void ServerInfo::save() const 0090 { 0091 QSettings settings(QStringLiteral("KDE"), QStringLiteral("UserFeedbackConsole.servers")); 0092 settings.beginGroup(ServerInfoData::groupName(name())); 0093 settings.setValue(QStringLiteral("name"), name()); 0094 settings.setValue(QStringLiteral("url"), url().toString()); 0095 settings.setValue(QStringLiteral("userName"), userName()); 0096 settings.setValue(QStringLiteral("password"), password()); // TODO 0097 settings.endGroup(); 0098 0099 auto allServers = settings.value(QStringLiteral("ServerNames")).toStringList(); 0100 if (!allServers.contains(name())) 0101 allServers.push_back(name()); 0102 settings.setValue(QStringLiteral("ServerNames"), allServers); 0103 } 0104 0105 ServerInfo ServerInfo::load(const QString &name) 0106 { 0107 QSettings settings(QStringLiteral("KDE"), QStringLiteral("UserFeedbackConsole.servers")); 0108 settings.beginGroup(ServerInfoData::groupName(name)); 0109 0110 ServerInfo info; 0111 info.setName(settings.value(QStringLiteral("name")).toString()); 0112 info.setUrl(QUrl(settings.value(QStringLiteral("url")).toString())); 0113 info.setUserName(settings.value(QStringLiteral("userName")).toString()); 0114 info.setPassword(settings.value(QStringLiteral("password")).toString()); 0115 return info; 0116 } 0117 0118 void ServerInfo::remove(const QString& name) 0119 { 0120 QSettings settings(QStringLiteral("KDE"), QStringLiteral("UserFeedbackConsole.servers")); 0121 settings.remove(ServerInfoData::groupName(name)); 0122 0123 auto allServers = settings.value(QStringLiteral("ServerNames")).toStringList(); 0124 allServers.removeAll(name); 0125 settings.setValue(QStringLiteral("ServerNames"), allServers); 0126 } 0127 0128 QStringList ServerInfo::allServerInfoNames() 0129 { 0130 QSettings settings(QStringLiteral("KDE"), QStringLiteral("UserFeedbackConsole.servers")); 0131 return settings.value(QStringLiteral("ServerNames")).toStringList(); 0132 }