f7a66f09f382160c63cfb0363f1a9a8495a68207
[quassel.git] / src / qtui / ircconnectionwizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
36   _introductionPage = createIntroductionPage(this);
37   _identityPage = new IdentityPage(this);
38   _networkPage = new NetworkPage(this);
39
40
41   addPage(_introductionPage);
42   addPage(_identityPage);
43   addPage(_networkPage);
44
45   setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
46
47   setOptions(options() | (QWizard::WizardOptions)(QWizard::NoDefaultButton | QWizard::CancelButtonOnLeft));
48   setOption(QWizard::NoCancelButton, false);
49
50   connect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(finishClicked()));
51   setButtonText(QWizard::FinishButton, tr("Save && Connect"));
52 }
53
54 QWizardPage *IrcConnectionWizard::createIntroductionPage(QWidget *parent) {
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 setting up your default identity and your IRC network connection. "
59                                          "You can abort this wizard in any step, if you want to setup your identity and IRC connection in more detail. "
60                                          "This can be done in the settings."), 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 void IrcConnectionWizard::finishClicked() {
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   } else {
75     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
76     Client::createIdentity(*identity);
77   }
78 }
79
80 void IrcConnectionWizard::identityReady(IdentityId id) {
81   disconnect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
82   NetworkPage *networkPage = static_cast<NetworkPage *>(_networkPage);
83   NetworkInfo networkInfo = networkPage->networkInfo();
84   QStringList channels = networkPage->channelList();
85   networkInfo.identity = id;
86   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
87   Client::createNetwork(networkInfo, channels);
88 }
89
90 void IrcConnectionWizard::networkReady(NetworkId id) {
91   disconnect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
92   const Network *net = Client::network(id);
93   Q_ASSERT(net);
94   net->requestConnect();
95   deleteLater();
96 }
97
98 // ==============================
99 //  Wizard Pages
100 // ==============================
101
102 // Identity Page
103 IdentityPage::IdentityPage(QWidget *parent)
104   : QWizardPage(parent),
105     _identityEditWidget(new IdentityEditWidget(this)),
106     _identity(0)
107 {
108   setTitle(tr("Setup Identity"));
109
110   if(Client::identityIds().isEmpty()) {
111     _identity = new CertIdentity(-1, this);
112     _identity->setToDefaults();
113     _identity->setIdentityName(tr("Default Identity"));
114   } else {
115     _identity = new CertIdentity(*Client::identity(Client::identityIds().first()), this);
116   }
117
118   _identityEditWidget->displayIdentity(_identity);
119   _identityEditWidget->showAdvanced(false);
120   QVBoxLayout *layout = new QVBoxLayout;
121   layout->addWidget(_identityEditWidget);
122   setLayout(layout);
123 }
124
125 CertIdentity *IdentityPage::identity() {
126   _identityEditWidget->saveToIdentity(_identity);
127   return _identity;
128 }
129
130
131 // Network Page
132 NetworkPage::NetworkPage(QWidget *parent)
133   : QWizardPage(parent),
134     _networkEditor(new SimpleNetworkEditor(this))
135 {
136
137   QStringList defaultNets = Network::presetNetworks(true);
138   if(!defaultNets.isEmpty()) {
139     NetworkInfo info = Network::networkInfoFromPreset(defaultNets[0]);
140     if(!info.networkName.isEmpty()) {
141       _networkInfo = info;
142       _channelList = Network::presetDefaultChannels(defaultNets[0]);
143     }
144   }
145
146   _networkEditor->displayNetworkInfo(_networkInfo);
147   _networkEditor->setDefaultChannels(_channelList);
148
149   setTitle(tr("Setup Network Connection"));
150
151   QVBoxLayout *layout = new QVBoxLayout;
152   layout->addWidget(_networkEditor);
153   setLayout(layout);
154 }
155
156 NetworkInfo NetworkPage::networkInfo() {
157   _networkEditor->saveToNetworkInfo(_networkInfo);
158   return _networkInfo;
159 }
160
161 QStringList NetworkPage::channelList() {
162   _channelList = _networkEditor->defaultChannels();
163   return _channelList;
164 }