e599a51326d2708cfc7a252f48555557ca89aa11
[quassel.git] / src / client / coreaccount.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreaccount.h"
22
23 CoreAccount::CoreAccount(AccountId accountId)
24 {
25     _accountId = accountId;
26     _internal = false;
27     _port = 4242;
28     _storePassword = false;
29     _proxyType = QNetworkProxy::DefaultProxy;
30     _proxyPort = 8080;
31 }
32
33 void CoreAccount::setAccountId(AccountId id)
34 {
35     _accountId = id;
36 }
37
38 void CoreAccount::setAccountName(const QString& name)
39 {
40     _accountName = name;
41 }
42
43 void CoreAccount::setUuid(const QUuid& uuid)
44 {
45     _uuid = uuid;
46 }
47
48 void CoreAccount::setInternal(bool internal)
49 {
50     _internal = internal;
51 }
52
53 void CoreAccount::setUser(const QString& user)
54 {
55     _user = user;
56 }
57
58 void CoreAccount::setPassword(const QString& password)
59 {
60     _password = password;
61 }
62
63 void CoreAccount::setStorePassword(bool store)
64 {
65     _storePassword = store;
66 }
67
68 void CoreAccount::setHostName(const QString& hostname)
69 {
70     _hostName = hostname;
71 }
72
73 void CoreAccount::setPort(uint port)
74 {
75     _port = port;
76 }
77
78 void CoreAccount::setProxyType(QNetworkProxy::ProxyType type)
79 {
80     _proxyType = type;
81 }
82
83 void CoreAccount::setProxyUser(const QString& proxyUser)
84 {
85     _proxyUser = proxyUser;
86 }
87
88 void CoreAccount::setProxyPassword(const QString& proxyPassword)
89 {
90     _proxyPassword = proxyPassword;
91 }
92
93 void CoreAccount::setProxyHostName(const QString& proxyHostName)
94 {
95     _proxyHostName = proxyHostName;
96 }
97
98 void CoreAccount::setProxyPort(uint proxyPort)
99 {
100     _proxyPort = proxyPort;
101 }
102
103 QVariantMap CoreAccount::toVariantMap(bool forcePassword) const
104 {
105     QVariantMap v;
106     v["AccountId"] = accountId().toInt();  // can't use AccountId because then comparison fails
107     v["AccountName"] = accountName();
108     v["Uuid"] = uuid().toString();
109     v["Internal"] = isInternal();
110     v["User"] = user();
111     if (_storePassword || forcePassword)
112         v["Password"] = password();
113     else
114         v["Password"] = QString();
115     v["StorePassword"] = storePassword();
116     v["HostName"] = hostName();
117     v["Port"] = port();
118     v["ProxyType"] = proxyType();
119     v["ProxyUser"] = proxyUser();
120     v["ProxyPassword"] = proxyPassword();
121     v["ProxyHostName"] = proxyHostName();
122     v["ProxyPort"] = proxyPort();
123     return v;
124 }
125
126 void CoreAccount::fromVariantMap(const QVariantMap& v)
127 {
128     setAccountId((AccountId)v.value("AccountId").toInt());
129     setAccountName(v.value("AccountName").toString());
130     setUuid(QUuid(v.value("Uuid").toString()));
131     setInternal(v.value("Internal").toBool());
132     setUser(v.value("User").toString());
133     setPassword(v.value("Password").toString());
134     setStorePassword(v.value("StorePassword").toBool());
135     setHostName(v.value("HostName").toString());
136     setPort(v.value("Port").toUInt());
137     setProxyType((QNetworkProxy::ProxyType)v.value("ProxyType").toInt());
138     setProxyUser(v.value("ProxyUser").toString());
139     setProxyPassword(v.value("ProxyPassword").toString());
140     setProxyHostName(v.value("ProxyHostName").toString());
141     setProxyPort(v.value("ProxyPort").toUInt());
142
143     _storePassword = !password().isEmpty();
144 }
145
146 bool CoreAccount::operator==(const CoreAccount& other) const
147 {
148     return toVariantMap(true) == other.toVariantMap(true);
149 }
150
151 bool CoreAccount::operator!=(const CoreAccount& other) const
152 {
153     return !(*this == other);
154 }
155
156 QDebug operator<<(QDebug dbg, const CoreAccount& acc)
157 {
158     dbg.nospace() << qPrintable(QString("CoreAccount(AccountId:")) << acc.accountId()
159                   << qPrintable(QString(", AccountName:")) << acc.accountName()
160                   << qPrintable(QString(", Uuid:")) << acc.uuid()
161                   << qPrintable(QString(", Internal:")) << acc.isInternal()
162                   << qPrintable(QString(", User:")) << acc.user()
163                   << qPrintable(QString(", Password:")) << acc.password()
164                   << qPrintable(QString(", StorePassword:")) << acc.storePassword()
165                   << qPrintable(QString(", HostName:")) << acc.hostName()
166                   << qPrintable(QString(", Port:")) << acc.port()
167                   << qPrintable(QString(", ProxyType:")) << acc.proxyType()
168                   << qPrintable(QString(", ProxyUser:")) << acc.proxyUser()
169                   << qPrintable(QString(", ProxyPassword:")) << acc.proxyPassword()
170                   << qPrintable(QString(", ProxyHostName:")) << acc.proxyHostName()
171                   << qPrintable(QString(", ProxyPort:")) << acc.proxyPort();
172     return dbg.space();
173 }