modernize: Reformat ALL the source... again!
[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 CoreIdentity::CoreIdentity(IdentityId id, QObject* parent)
26     : Identity(id, parent)
27 #ifdef HAVE_SSL
28     , _certManager(*this)
29 #endif
30 {
31 #ifdef HAVE_SSL
32     connect(this, &Identity::idSet, &_certManager, &CoreCertManager::setId);
33     connect(&_certManager, &SyncableObject::updated, this, &SyncableObject::updated);
34 #endif
35 }
36
37 CoreIdentity::CoreIdentity(const Identity& other, QObject* parent)
38     : Identity(other, parent)
39 #ifdef HAVE_SSL
40     , _certManager(*this)
41 #endif
42 {
43 #ifdef HAVE_SSL
44     connect(this, &Identity::idSet, &_certManager, &CoreCertManager::setId);
45     connect(&_certManager, &SyncableObject::updated, this, &SyncableObject::updated);
46 #endif
47 }
48
49 CoreIdentity::CoreIdentity(const CoreIdentity& other, QObject* parent)
50     : Identity(other, parent)
51 #ifdef HAVE_SSL
52     , _sslKey(other._sslKey)
53     , _sslCert(other._sslCert)
54     , _certManager(*this)
55 #endif
56 {
57 #ifdef HAVE_SSL
58     connect(this, &Identity::idSet, &_certManager, &CoreCertManager::setId);
59     connect(&_certManager, &SyncableObject::updated, this, &SyncableObject::updated);
60 #endif
61 }
62
63 void CoreIdentity::synchronize(SignalProxy* proxy)
64 {
65     proxy->synchronize(this);
66 #ifdef HAVE_SSL
67     proxy->synchronize(&_certManager);
68 #endif
69 }
70
71 #ifdef HAVE_SSL
72 void CoreIdentity::setSslKey(const QByteArray& encoded)
73 {
74     QSslKey key(encoded, QSsl::Rsa);
75     if (key.isNull())
76         key = QSslKey(encoded, QSsl::Ec);
77     if (key.isNull())
78         key = QSslKey(encoded, QSsl::Dsa);
79     setSslKey(key);
80 }
81
82 void CoreIdentity::setSslCert(const QByteArray& encoded)
83 {
84     setSslCert(QSslCertificate(encoded));
85 }
86
87 #endif
88
89 CoreIdentity& CoreIdentity::operator=(const CoreIdentity& identity)
90 {
91     Identity::operator=(identity);
92 #ifdef HAVE_SSL
93     _sslKey = identity._sslKey;
94     _sslCert = identity._sslCert;
95 #endif
96     return *this;
97 }
98
99 #ifdef HAVE_SSL
100 // ========================================
101 //  CoreCertManager
102 // ========================================
103
104 CoreCertManager::CoreCertManager(CoreIdentity& identity)
105     : CertManager(identity.id())
106     , identity(identity)
107 {
108     setAllowClientUpdates(true);
109 }
110
111 void CoreCertManager::setId(IdentityId id)
112 {
113     renameObject(QString::number(id.toInt()));
114 }
115
116 void CoreCertManager::setSslKey(const QByteArray& encoded)
117 {
118     identity.setSslKey(encoded);
119     CertManager::setSslKey(encoded);
120 }
121
122 void CoreCertManager::setSslCert(const QByteArray& encoded)
123 {
124     identity.setSslCert(encoded);
125     CertManager::setSslCert(encoded);
126 }
127
128 #endif  // HAVE_SSL