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