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