Fix buffer preselection on reconnect
[quassel.git] / src / qtui / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 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 <QDialogButtonBox>
22 #include <QVBoxLayout>
23
24 #include "coreconnectdlg.h"
25
26 #include "iconloader.h"
27 #include "clientsettings.h"
28 #include "coreaccountsettingspage.h"
29
30 CoreConnectDlg::CoreConnectDlg(QWidget *parent) : QDialog(parent) {
31   _settingsPage = new CoreAccountSettingsPage(this);
32   _settingsPage->setStandAlone(true);
33   _settingsPage->load();
34
35   CoreAccountSettings s;
36   AccountId lastAccount = s.lastAccount();
37   if(lastAccount.isValid())
38     _settingsPage->setSelectedAccount(lastAccount);
39
40   setWindowTitle(tr("Connect to Core"));
41   setWindowIcon(SmallIcon("network-disconnect"));
42
43   QVBoxLayout *layout = new QVBoxLayout(this);
44   layout->addWidget(_settingsPage);
45
46   QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
47   buttonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
48   layout->addWidget(buttonBox);
49
50   connect(_settingsPage, SIGNAL(connectToCore(AccountId)), SLOT(accept()));
51   connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
52   connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
53 }
54
55 AccountId CoreConnectDlg::selectedAccount() const {
56   return _settingsPage->selectedAccount();
57 }
58
59 void CoreConnectDlg::accept() {
60   _settingsPage->save();
61   QDialog::accept();
62 }
63
64 /******** CoreConnectAuthDlg ****************************************************************/
65
66 CoreConnectAuthDlg::CoreConnectAuthDlg(CoreAccount *account, QWidget *parent)
67   : QDialog(parent),
68   _account(account)
69 {
70   ui.setupUi(this);
71
72   connect(ui.user, SIGNAL(textChanged(QString)), SLOT(setButtonStates()));
73   connect(ui.password, SIGNAL(textChanged(QString)), SLOT(setButtonStates()));
74
75   ui.label->setText(tr("Please enter your credentials for %1:").arg(account->accountName()));
76   ui.user->setText(account->user());
77   ui.password->setText(account->password());
78   ui.rememberPasswd->setChecked(account->storePassword());
79
80   if(ui.user->text().isEmpty())
81     ui.user->setFocus();
82   else
83     ui.password->setFocus();
84 }
85
86 void CoreConnectAuthDlg::accept() {
87   _account->setUser(ui.user->text());
88   _account->setPassword(ui.password->text());
89   _account->setStorePassword(ui.rememberPasswd->isChecked());
90
91   QDialog::accept();
92 }
93
94 void CoreConnectAuthDlg::setButtonStates() {
95   bool valid = !(ui.user->text().isEmpty() || ui.password->text().isEmpty());
96   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
97 }