840edbddb0144ba6f8277ce4765afe8cfc268f17
[quassel.git] / src / core / coreidentity.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "coreidentity.h"
22
23 #include "signalproxy.h"
24
25 INIT_SYNCABLE_OBJECT(CoreIdentity)
26 CoreIdentity::CoreIdentity(IdentityId id, QObject *parent)
27     : Identity(id, parent)
28 #ifdef HAVE_SSL
29     , _certManager(*this)
30 #endif
31 {
32 #ifdef HAVE_SSL
33     connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
34     connect(&_certManager, SIGNAL(updated()), this, SIGNAL(updated()));
35 #endif
36 }
37
38
39 CoreIdentity::CoreIdentity(const Identity &other, QObject *parent)
40     : Identity(other, parent)
41 #ifdef HAVE_SSL
42     , _certManager(*this)
43 #endif
44 {
45 #ifdef HAVE_SSL
46     connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
47     connect(&_certManager, SIGNAL(updated()), this, SIGNAL(updated()));
48 #endif
49 }
50
51
52 CoreIdentity::CoreIdentity(const CoreIdentity &other, QObject *parent)
53     : Identity(other, parent)
54 #ifdef HAVE_SSL
55     , _sslKey(other._sslKey),
56     _sslCert(other._sslCert),
57     _certManager(*this)
58 #endif
59 {
60 #ifdef HAVE_SSL
61     connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
62     connect(&_certManager, SIGNAL(updated()), this, SIGNAL(updated()));
63 #endif
64 }
65
66
67 void CoreIdentity::synchronize(SignalProxy *proxy)
68 {
69     proxy->synchronize(this);
70 #ifdef HAVE_SSL
71     proxy->synchronize(&_certManager);
72 #endif
73 }
74
75
76 #ifdef HAVE_SSL
77 void CoreIdentity::setSslKey(const QByteArray &encoded)
78 {
79     QSslKey key(encoded, QSsl::Rsa);
80     if (key.isNull())
81         key = QSslKey(encoded, QSsl::Ec);
82     if (key.isNull())
83         key = QSslKey(encoded, QSsl::Dsa);
84     setSslKey(key);
85 }
86
87
88 void CoreIdentity::setSslCert(const QByteArray &encoded)
89 {
90     setSslCert(QSslCertificate(encoded));
91 }
92
93
94 #endif
95
96 CoreIdentity &CoreIdentity::operator=(const CoreIdentity &identity)
97 {
98     Identity::operator=(identity);
99 #ifdef HAVE_SSL
100     _sslKey = identity._sslKey;
101     _sslCert = identity._sslCert;
102 #endif
103     return *this;
104 }
105
106
107 #ifdef HAVE_SSL
108 // ========================================
109 //  CoreCertManager
110 // ========================================
111 INIT_SYNCABLE_OBJECT(CoreCertManager)
112 CoreCertManager::CoreCertManager(CoreIdentity &identity)
113     : CertManager(identity.id()),
114     identity(identity)
115 {
116     setAllowClientUpdates(true);
117 }
118
119
120 void CoreCertManager::setId(IdentityId id)
121 {
122     renameObject(QString::number(id.toInt()));
123 }
124
125
126 void CoreCertManager::setSslKey(const QByteArray &encoded)
127 {
128     identity.setSslKey(encoded);
129     CertManager::setSslKey(encoded);
130 }
131
132
133 void CoreCertManager::setSslCert(const QByteArray &encoded)
134 {
135     identity.setSslCert(encoded);
136     CertManager::setSslCert(encoded);
137 }
138
139
140 #endif //HAVE_SSL