Indicate whether a key is set for the buffer.
[quassel.git] / src / client / clientidentity.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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 "clientidentity.h"
22
23 #include "client.h"
24 #include "signalproxy.h"
25
26 INIT_SYNCABLE_OBJECT(CertIdentity)
27 CertIdentity::CertIdentity(IdentityId id, QObject *parent)
28     : Identity(id, parent)
29 #ifdef HAVE_SSL
30     ,  _certManager(0),
31     _isDirty(false)
32 #endif
33 {
34 }
35
36
37 CertIdentity::CertIdentity(const Identity &other, QObject *parent)
38     : Identity(other, parent)
39 #ifdef HAVE_SSL
40     , _certManager(0),
41     _isDirty(false)
42 #endif
43 {
44 }
45
46
47 CertIdentity::CertIdentity(const CertIdentity &other, QObject *parent)
48     : Identity(other, parent)
49 #ifdef HAVE_SSL
50     , _certManager(0),
51     _isDirty(other._isDirty),
52     _sslKey(other._sslKey),
53     _sslCert(other._sslCert)
54 #endif
55 {
56 }
57
58
59 #ifdef HAVE_SSL
60 void CertIdentity::enableEditSsl(bool enable)
61 {
62     if (!enable || _certManager)
63         return;
64
65     _certManager = new ClientCertManager(id(), this);
66     if (isValid()) { // this means we are not a newly created Identity but have a proper Id
67         Client::signalProxy()->synchronize(_certManager);
68         connect(_certManager, SIGNAL(updated()), this, SLOT(markClean()));
69         connect(_certManager, SIGNAL(initDone()), this, SLOT(markClean()));
70     }
71 }
72
73
74 void CertIdentity::setSslKey(const QSslKey &key)
75 {
76     if (key.toPem() == _sslKey.toPem())
77         return;
78     _sslKey = key;
79     _isDirty = true;
80 }
81
82
83 void CertIdentity::setSslCert(const QSslCertificate &cert)
84 {
85     if (cert.toPem() == _sslCert.toPem())
86         return;
87     _sslCert = cert;
88     _isDirty = true;
89 }
90
91
92 void CertIdentity::requestUpdateSslSettings()
93 {
94     if (!_certManager)
95         return;
96
97     _certManager->requestUpdate(_certManager->toVariantMap());
98 }
99
100
101 void CertIdentity::markClean()
102 {
103     _isDirty = false;
104     emit sslSettingsUpdated();
105 }
106
107
108 // ========================================
109 //  ClientCertManager
110 // ========================================
111 void ClientCertManager::setSslKey(const QByteArray &encoded)
112 {
113     QSslKey key(encoded, QSsl::Rsa);
114     if (key.isNull())
115         key = QSslKey(encoded, QSsl::Dsa);
116     _certIdentity->setSslKey(key);
117 }
118
119
120 void ClientCertManager::setSslCert(const QByteArray &encoded)
121 {
122     _certIdentity->setSslCert(QSslCertificate(encoded));
123 }
124
125
126 #endif // HAVE_SSL