Save/restore active bufferview
[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 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 void IrcConnectionWizard::finishClicked() {
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   } else {
74     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
75     Client::createIdentity(*identity);
76   }
77 }
78
79 void IrcConnectionWizard::identityReady(IdentityId id) {
80   disconnect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(identityReady(IdentityId)));
81   NetworkPage *networkPage = static_cast<NetworkPage *>(_networkPage);
82   NetworkInfo networkInfo = networkPage->networkInfo();
83   QStringList channels = networkPage->channelList();
84   networkInfo.identity = id;
85   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
86   Client::createNetwork(networkInfo, channels);
87 }
88
89 void IrcConnectionWizard::networkReady(NetworkId id) {
90   disconnect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(networkReady(NetworkId)));
91   const Network *net = Client::network(id);
92   Q_ASSERT(net);
93   net->requestConnect();
94   deleteLater();
95 }
96
97 // ==============================
98 //  Wizard Pages
99 // ==============================
100
101 // Identity Page
102 IdentityPage::IdentityPage(QWidget *parent)
103   : QWizardPage(parent),
104     _identityEditWidget(new IdentityEditWidget(this)),
105     _identity(0)
106 {
107   setTitle(tr("Setup Identity"));
108
109   if(Client::identityIds().isEmpty()) {
110     _identity = new CertIdentity(-1, this);
111     _identity->setToDefaults();
112     _identity->setIdentityName(tr("Default Identity"));
113   } else {
114     _identity = new CertIdentity(*Client::identity(Client::identityIds().first()), this);
115   }
116
117   _identityEditWidget->displayIdentity(_identity);
118   _identityEditWidget->showAdvanced(false);
119   QVBoxLayout *layout = new QVBoxLayout;
120   layout->addWidget(_identityEditWidget);
121   setLayout(layout);
122 }
123
124 CertIdentity *IdentityPage::identity() {
125   _identityEditWidget->saveToIdentity(_identity);
126   return _identity;
127 }
128
129
130 // Network Page
131 NetworkPage::NetworkPage(QWidget *parent)
132   : QWizardPage(parent),
133     _networkEditor(new SimpleNetworkEditor(this))
134 {
135
136   QStringList defaultNets = Network::presetNetworks(true);
137   if(!defaultNets.isEmpty()) {
138     NetworkInfo info = Network::networkInfoFromPreset(defaultNets[0]);
139     if(!info.networkName.isEmpty()) {
140       _networkInfo = info;
141       _channelList = Network::presetDefaultChannels(defaultNets[0]);
142     }
143   }
144
145   _networkEditor->displayNetworkInfo(_networkInfo);
146   _networkEditor->setDefaultChannels(_channelList);
147
148   setTitle(tr("Setup Network Connection"));
149
150   QVBoxLayout *layout = new QVBoxLayout;
151   layout->addWidget(_networkEditor);
152   setLayout(layout);
153 }
154
155 NetworkInfo NetworkPage::networkInfo() {
156   _networkEditor->saveToNetworkInfo(_networkInfo);
157   return _networkInfo;
158 }
159
160 QStringList NetworkPage::channelList() {
161   _channelList = _networkEditor->defaultChannels();
162   return _channelList;
163 }