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