modernize: Prefer default member init over ctor init
[quassel.git] / src / qtui / ircconnectionwizard.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 "ircconnectionwizard.h"
22
23 #include "client.h"
24 #include "identityeditwidget.h"
25 #include "simplenetworkeditor.h"
26 #include "presetnetworks.h"
27
28 #include <QVBoxLayout>
29
30 IrcConnectionWizard::IrcConnectionWizard(QWidget *parent, Qt::WindowFlags flags)
31     : QWizard(parent, flags)
32 {
33     _introductionPage = createIntroductionPage(this);
34     _identityPage = new IdentityPage(this);
35     _networkPage = new NetworkPage(this);
36
37     addPage(_introductionPage);
38     addPage(_identityPage);
39     addPage(_networkPage);
40
41     setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
42
43     setOptions(options() | (QWizard::WizardOptions)(QWizard::NoDefaultButton | QWizard::CancelButtonOnLeft));
44     setOption(QWizard::NoCancelButton, false);
45
46     connect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(finishClicked()));
47     setButtonText(QWizard::FinishButton, tr("Save && Connect"));
48 }
49
50
51 QWizardPage *IrcConnectionWizard::createIntroductionPage(QWidget *parent)
52 {
53     QWizardPage *page = new QWizardPage(parent);
54     page->setTitle(QObject::tr("Welcome to Quassel IRC"));
55
56     QLabel *label = new QLabel(QObject::tr("This wizard will help you to set up your default identity and your IRC network connection.<br>"
57                                            "This only covers basic settings. You can cancel this wizard any time and use the settings dialog for more detailed changes."), page);
58     label->setWordWrap(true);
59
60     QVBoxLayout *layout = new QVBoxLayout;
61     layout->addWidget(label);
62     page->setLayout(layout);
63     return page;
64 }
65
66
67 void IrcConnectionWizard::finishClicked()
68 {
69     CertIdentity *identity = static_cast<IdentityPage *>(_identityPage)->identity();
70     if (identity->id().isValid()) {
71         Client::updateIdentity(identity->id(), identity->toVariantMap());
72         identityReady(identity->id());
73     }
74     else {
75         connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
76         Client::createIdentity(*identity);
77     }
78 }
79
80
81 void IrcConnectionWizard::identityReady(IdentityId id)
82 {
83     disconnect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
84     NetworkPage *networkPage = static_cast<NetworkPage *>(_networkPage);
85     NetworkInfo networkInfo = networkPage->networkInfo();
86     QStringList channels = networkPage->channelList();
87     networkInfo.identity = id;
88     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
89     Client::createNetwork(networkInfo, channels);
90 }
91
92
93 void IrcConnectionWizard::networkReady(NetworkId id)
94 {
95     disconnect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
96     const Network *net = Client::network(id);
97     Q_ASSERT(net);
98     net->requestConnect();
99     deleteLater();
100 }
101
102
103 // ==============================
104 //  Wizard Pages
105 // ==============================
106
107 // Identity Page
108 IdentityPage::IdentityPage(QWidget *parent)
109     : QWizardPage(parent),
110     _identityEditWidget(new IdentityEditWidget(this))
111 {
112     setTitle(tr("Setup Identity"));
113
114     if (Client::identityIds().isEmpty()) {
115         _identity = new CertIdentity(-1, this);
116         _identity->setToDefaults();
117         _identity->setIdentityName(tr("Default Identity"));
118     }
119     else {
120         _identity = new CertIdentity(*Client::identity(Client::identityIds().first()), this);
121     }
122
123     _identityEditWidget->displayIdentity(_identity);
124     _identityEditWidget->showAdvanced(false);
125     QVBoxLayout *layout = new QVBoxLayout;
126     layout->addWidget(_identityEditWidget);
127     setLayout(layout);
128 }
129
130
131 CertIdentity *IdentityPage::identity()
132 {
133     _identityEditWidget->saveToIdentity(_identity);
134     return _identity;
135 }
136
137
138 // Network Page
139 NetworkPage::NetworkPage(QWidget *parent)
140     : QWizardPage(parent),
141     _networkEditor(new SimpleNetworkEditor(this))
142 {
143     QStringList defaultNets = PresetNetworks::names(true);
144     if (!defaultNets.isEmpty()) {
145         NetworkInfo info = PresetNetworks::networkInfo(defaultNets[0]);
146         if (!info.networkName.isEmpty()) {
147             _networkInfo = info;
148             _channelList = PresetNetworks::defaultChannels(defaultNets[0]);
149         }
150     }
151
152     _networkEditor->displayNetworkInfo(_networkInfo);
153     _networkEditor->setDefaultChannels(_channelList);
154
155     setTitle(tr("Setup Network Connection"));
156
157     QVBoxLayout *layout = new QVBoxLayout;
158     layout->addWidget(_networkEditor);
159     setLayout(layout);
160 }
161
162
163 NetworkInfo NetworkPage::networkInfo()
164 {
165     _networkEditor->saveToNetworkInfo(_networkInfo);
166     return _networkInfo;
167 }
168
169
170 QStringList NetworkPage::channelList()
171 {
172     _channelList = _networkEditor->defaultChannels();
173     return _channelList;
174 }