android: Fix XML form filter.
[qpwmc.git] / pwmdRemoteHost.cpp
blobe8984bfe4c116acf3d86574e397124f566d87dde
1 /*
2 Copyright (C) 2012-2023 Ben Kibbey <bjk@luxsci.net>
4 This file is part of qpwmc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 USA
21 #include "pwmdRemoteHost.h"
22 #include <libpwmd.h>
24 PwmdRemoteHost::PwmdRemoteHost (QString name)
26 _name = name;
27 _type = PWMD_SOCKET_LOCAL;
28 _hostname = _sshUsername = QString ();
29 _port = 0;
30 _ipProtocol = 0;
31 _tlsVerify = false;
32 _tlsPriority = QString ();
33 _socketTimeout = 0;
34 _connectTimeout = 0;
35 _sshAgent = false;
36 #ifdef Q_OS_ANDROID
37 _clientCertificateAlias = QString ();
38 #endif
41 bool
42 PwmdRemoteHost::operator == (const PwmdRemoteHost &other) const
44 if (this->_name == other._name
45 #ifdef Q_OS_ANDROID
46 && this->_clientCertificateAlias == other._clientCertificateAlias
47 #endif
48 && this->_type == other._type
49 && this->_hostname == other._hostname
50 && this->_port == other._port
51 && this->_ipProtocol == other._ipProtocol
52 && this->_tlsVerify == other._tlsVerify
53 && this->_tlsPriority == other._tlsPriority
54 && this->_socketTimeout == other._socketTimeout
55 && this->_connectTimeout == other._connectTimeout
56 && this->_sshAgent == other._sshAgent
57 && this->_socketArgs == other._socketArgs)
58 return true;
60 return false;
63 bool
64 PwmdRemoteHost::operator != (const PwmdRemoteHost &other) const
66 return !(*this == other);
69 PwmdRemoteHost::~PwmdRemoteHost ()
73 QString
74 PwmdRemoteHost::name ()
76 return _name;
79 void
80 PwmdRemoteHost::setName (QString str)
82 _name = str;
85 QString
86 PwmdRemoteHost::hostname ()
88 return _hostname;
91 void
92 PwmdRemoteHost::setHostname (QString h)
94 _hostname = h;
97 int
98 PwmdRemoteHost::type ()
100 return _type;
103 void
104 PwmdRemoteHost::setType (int n)
106 _type = n;
110 PwmdRemoteHost::port ()
112 return _port;
115 void
116 PwmdRemoteHost::setPort (int n)
118 _port = n;
122 PwmdRemoteHost::ipProtocol ()
124 return _ipProtocol;
127 void
128 PwmdRemoteHost::setIpProtocol (int n)
130 _ipProtocol = n;
133 QStringList
134 PwmdRemoteHost::socketArgs ()
136 return _socketArgs;
139 void
140 PwmdRemoteHost::setSocketArgs (QStringList list)
142 _socketArgs = list;
145 QString
146 PwmdRemoteHost::tlsPriority ()
148 return _tlsPriority;
151 void
152 PwmdRemoteHost::setTlsPriority (QString s)
154 _tlsPriority = s;
157 bool
158 PwmdRemoteHost::tlsVerify ()
160 return _tlsVerify;
163 void
164 PwmdRemoteHost::setTlsVerify (bool b)
166 _tlsVerify = b;
170 PwmdRemoteHost::socketTimeout ()
172 return _socketTimeout;
175 void
176 PwmdRemoteHost::setSocketTimeout (int n)
178 _socketTimeout = n;
182 PwmdRemoteHost::connectTimeout ()
184 return _connectTimeout;
187 void
188 PwmdRemoteHost::setConnectTimeout (int n)
190 _connectTimeout = n;
193 QString
194 PwmdRemoteHost::sshUsername ()
196 return _sshUsername;
199 void
200 PwmdRemoteHost::setSshUsername (QString s)
202 _sshUsername = s;
205 bool
206 PwmdRemoteHost::sshAgent ()
208 return _sshAgent;
211 void
212 PwmdRemoteHost::setSshAgent (bool b)
214 _sshAgent = b;
217 #ifdef Q_OS_ANDROID
218 QString
219 PwmdRemoteHost::clientCertificateAlias ()
221 return _clientCertificateAlias;
224 void
225 PwmdRemoteHost::setClientCertificateAlias (QString s)
227 _clientCertificateAlias = s;
229 #endif
231 bool
232 PwmdRemoteHost::fillRemoteHost (const QString &name, PwmdRemoteHost &data)
234 QSettings cfg ("qpwmc");
235 int size = cfg.beginReadArray ("remoteHosts");
237 for (int i = 0; i < size; ++i)
239 cfg.setArrayIndex (i);
241 if (cfg.value ("name").toString () != name)
242 continue;
244 data = PwmdRemoteHost (cfg.value ("name").toString ());
245 data.setType (cfg.value ("type").toInt ());
246 data.setHostname (cfg.value ("hostname").toString ());
247 data.setPort (cfg.value ("port").toInt ());
248 data.setIpProtocol (cfg.value ("ipProtocol").toInt ());
249 data.setSocketArgs (cfg.value ("socketArgs").toStringList ());
250 data.setTlsVerify (cfg.value ("tlsVerify").toBool ());
251 data.setTlsPriority (cfg.value ("tlsPriority").toString ());
252 data.setConnectTimeout (cfg.value ("connectTimeout").toInt ());
253 data.setSocketTimeout (cfg.value ("socketTimeout").toInt ());
254 data.setSshAgent (cfg.value ("sshAgent").toBool ());
255 data.setSshUsername (cfg.value ("sshUsername").toString ());
256 #ifdef Q_OS_ANDROID
257 data.setClientCertificateAlias (cfg.value ("clientCertificateAlias").toString ());
258 #endif
259 return true;
262 cfg.endArray ();
263 return false;
266 QString
267 PwmdRemoteHost::socketUrl (PwmdRemoteHost &host)
269 QString s = QString ();
271 if (host.type () == PWMD_SOCKET_SSH)
272 s.append ("ssh");
273 else
274 s.append ("tls");
276 if (host.ipProtocol () != 0 && host.ipProtocol () == 1)
277 s.append ("4");
278 else if (host.ipProtocol () != 0)
279 s.append ("6");
281 s.append ("://");
283 if (host.type () == PWMD_SOCKET_SSH)
285 s.append (host.sshUsername ().isEmpty ()? "" : host.sshUsername ());
286 s.append ("@");
289 if (host.ipProtocol () != 0)
290 s.append ('[');
292 s.append (host.hostname ());
294 if (host.ipProtocol () != 0)
295 s.append (']');
297 s.append (QString (":%1").arg (host.port ()));
298 return s;