Checking in current work on settings dialogs. We will move away from SettingsInterface
[quassel.git] / src / qtgui / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 <QtGui>
22 #include "coreconnectdlg.h"
23 #include "clientproxy.h"
24 #include "global.h"
25 #include "client.h"
26 #include "clientsettings.h"
27
28 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool /*doAutoConnect*/) : QDialog(parent) {
29   ui.setupUi(this); //qDebug() << "new dlg";
30
31   setAttribute(Qt::WA_DeleteOnClose);
32
33   coreState = 0;
34   /* We show ui.internalCore in any case, because we might want to run as monolithic client anyway at another time
35   if(Global::runMode == Global::Monolithic) {
36     connect(ui.internalCore, SIGNAL(toggled(bool)), ui.hostEdit, SLOT(setDisabled(bool)));
37     connect(ui.internalCore, SIGNAL(toggled(bool)), ui.port, SLOT(setDisabled(bool)));
38     ui.internalCore->setChecked(true);
39   } else {
40     //ui.internalCore->hide();
41   }
42   */
43   connect(ui.newAccount, SIGNAL(clicked()), this, SLOT(createAccount()));
44   connect(ui.delAccount, SIGNAL(clicked()), this, SLOT(removeAccount()));
45   connect(ui.buttonBox1, SIGNAL(accepted()), this, SLOT(doConnect()));
46   connect(ui.hostEdit, SIGNAL(textChanged(const QString &)), this, SLOT(checkInputValid()));
47   connect(ui.userEdit, SIGNAL(textChanged(const QString &)), this, SLOT(checkInputValid()));
48   connect(ui.internalCore, SIGNAL(toggled(bool)), this, SLOT(checkInputValid()));
49   connect(ui.internalCore, SIGNAL(toggled(bool)), ui.hostEdit, SLOT(setDisabled(bool)));
50   connect(ui.internalCore, SIGNAL(toggled(bool)), ui.port, SLOT(setDisabled(bool)));
51   connect(ui.accountList, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(accountChanged(const QString &)));
52   connect(ui.autoConnect, SIGNAL(clicked(bool)), this, SLOT(autoConnectToggled(bool)));
53
54   connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
55   connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
56   connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
57   connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
58
59   AccountSettings s;
60   ui.accountList->addItems(s.knownAccounts());
61   curacc = s.lastAccount();
62   if(!ui.accountList->count()) {
63     //if(doAutoConnect) reject();
64     
65     setAccountEditEnabled(false);
66     QString newacc = QInputDialog::getText(this, tr("Create Account"), tr(
67                                            "In order to connect to a Quassel Core, you need to create an account.<br>"
68                                            "Please enter a name for this account now:"), QLineEdit::Normal, tr("Default"));
69     if(!newacc.isEmpty()) {
70       ui.accountList->addItem(newacc);
71       ui.hostEdit->setText("localhost");
72       ui.port->setValue(DEFAULT_PORT);
73       ui.internalCore->setChecked(false);
74       setAccountEditEnabled(true);
75     }
76     /*
77     // FIXME We create a default account here that just connects to the internal core
78     curacc = "Default";
79     ui.accountList->addItem("Default");
80     ui.internalCore->setChecked(true);
81     ui.userEdit->setText("Default");
82     ui.passwdEdit->setText("password");
83     ui.rememberPasswd->setChecked(true);
84     accountChanged(curacc);
85     ui.passwdEdit->setText("password");
86     ui.accountList->setCurrentIndex(0);
87     ui.autoConnect->setChecked(true);
88     autoConnectToggled(true);
89     */
90   } else {
91     if(!curacc.isEmpty()) {
92       //if(doAutoConnect) { qDebug() << "auto";
93       //  AccountSettings s;
94       //  int idx = ui.accountList->findText(s.autoConnectAccount());
95       //  if(idx < 0) reject();
96       //  else {
97       //    ui.accountList->setCurrentIndex(idx);
98       //    doConnect();
99       //  }
100       //} else {
101         int idx = ui.accountList->findText(curacc);
102         ui.accountList->setCurrentIndex(idx);
103       //}
104     }
105   }
106 }
107
108 CoreConnectDlg::~CoreConnectDlg() {
109   //qDebug() << "destroy";
110 }
111
112 void CoreConnectDlg::setAccountEditEnabled(bool en) {
113   ui.accountList->setEnabled(en);
114   ui.hostEdit->setEnabled(en && !ui.internalCore->isChecked());
115   ui.userEdit->setEnabled(en);
116   ui.passwdEdit->setEnabled(en);
117   ui.port->setEnabled(en && !ui.internalCore->isChecked());
118   ui.editAccount->setEnabled(en);
119   ui.delAccount->setEnabled(en);
120   ui.internalCore->setEnabled(en);
121   ui.rememberPasswd->setEnabled(en);
122   ui.autoConnect->setEnabled(en);
123   ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(en && checkInputValid());
124 }
125
126 void CoreConnectDlg::accountChanged(const QString &text) {
127   AccountSettings s;
128   if(!curacc.isEmpty()) {
129     VarMap oldAcc;
130     oldAcc["User"] = ui.userEdit->text();
131     oldAcc["Host"] = ui.hostEdit->text();
132     oldAcc["Port"] = ui.port->value();
133     oldAcc["InternalCore"] = ui.internalCore->isChecked();
134     if(ui.rememberPasswd->isChecked()) oldAcc["Password"] = ui.passwdEdit->text();
135     s.setValue(curacc, "AccountData", oldAcc);
136   }
137   ui.autoConnect->setChecked(false);
138   if(!text.isEmpty()) { // empty text: just save stuff
139     curacc = text;
140     s.setLastAccount(curacc);
141     VarMap newAcc = s.value(curacc, "AccountData").toMap();
142     ui.userEdit->setText(newAcc["User"].toString());
143     ui.hostEdit->setText(newAcc["Host"].toString());
144     ui.port->setValue(newAcc["Port"].toInt());
145     ui.internalCore->setChecked(newAcc["InternalCore"].toBool());
146     if(newAcc.contains("Password")) {
147       ui.passwdEdit->setText(newAcc["Password"].toString());
148       ui.rememberPasswd->setChecked(true);
149     } else ui.rememberPasswd->setChecked(false);
150     if(s.autoConnectAccount() == curacc) ui.autoConnect->setChecked(true);
151   }
152 }
153
154 void CoreConnectDlg::autoConnectToggled(bool autoConnect) {
155   AccountSettings s;
156   if(autoConnect) s.setAutoConnectAccount(curacc);
157   else s.setAutoConnectAccount("");
158 }
159
160 bool CoreConnectDlg::checkInputValid() {
161   bool res = (ui.internalCore->isChecked() || ui.hostEdit->text().count()) && ui.userEdit->text().count();
162   ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(res);
163   return res;
164 }
165
166 void CoreConnectDlg::createAccount() {
167   QString accname = QInputDialog::getText(this, tr("Create Account"), tr("Please enter a name for the new account:"));
168   if(accname.isEmpty()) return;
169   if(ui.accountList->findText(accname) >= 0) {
170     QMessageBox::warning(this, tr("Account name already exists!"), tr("An account named '%1' already exists, and account names must be unique!").arg(accname));
171     return;
172   }
173   VarMap defdata;
174   ui.accountList->addItem(accname);
175   ui.accountList->setCurrentIndex(ui.accountList->findText(accname));
176   setAccountEditEnabled(true);
177 }
178
179 void CoreConnectDlg::removeAccount() {
180   QString acc = ui.accountList->currentText();
181   int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
182                                                        "Note that this only affects your local account settings and will not remove "
183                                                        "any data from the core.").arg(acc),
184                              QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
185   if(res == QMessageBox::Yes) {
186     AccountSettings s;
187     s.removeAccount(acc);
188     curacc = "";
189     ui.accountList->removeItem(ui.accountList->findText(acc));
190     if(!ui.accountList->count()) setAccountEditEnabled(false);
191   }
192 }
193
194 bool CoreConnectDlg::willDoInternalAutoConnect() {
195   AccountSettings s;
196   if(Global::runMode != Global::Monolithic) return false;
197   if(ui.autoConnect->isChecked() && s.autoConnectAccount() == curacc && ui.internalCore->isChecked()) {
198     return true;
199   }
200   return false;
201 }
202
203 void CoreConnectDlg::doAutoConnect() {
204   AccountSettings s;
205   if(s.autoConnectAccount() == curacc) {
206     doConnect();
207   }
208 }
209
210 void CoreConnectDlg::doConnect() {
211   accountChanged(); // save current account info
212
213   VarMap conninfo;
214   ui.stackedWidget->setCurrentIndex(1);
215   if(ui.internalCore->isChecked()) {
216     if(Global::runMode != Global::Monolithic) {
217       coreConnectionError(tr("Can't connect to internal core, since we are running as a standalone GUI!"));
218       return;
219     }
220     ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
221     ui.connectionProgress->hide();
222   } else {
223     ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
224     conninfo["Host"] = ui.hostEdit->text();
225     conninfo["Port"] = ui.port->value();
226   }
227   conninfo["User"] = ui.userEdit->text();
228   conninfo["Password"] = ui.passwdEdit->text();
229   ui.profileLabel->hide(); ui.guiProfile->hide();
230   ui.newGuiProfile->hide(); ui.alwaysUseProfile->hide();
231   ui.connectionProgress->show();
232   try {
233     Client::instance()->connectToCore(conninfo);
234   } catch(Exception e) {
235     QString msg;
236     //if(!e.msg().isEmpty()) msg = tr("<br>%1").arg(e.msg()); // FIXME throw more detailed (vulgo: any) error msg
237     coreConnectionError(tr("Invalid user or password. Pleasy try again.%1").arg(msg));
238     //QMessageBox::warning(this, tr("Unknown account"), tr("Invalid user or password. Pleasy try again.%1").arg(msg));
239     //cancelConnect();
240     return;
241   }
242 }
243
244 void CoreConnectDlg::cancelConnect() {
245   ui.stackedWidget->setCurrentIndex(0);
246 }
247
248 void CoreConnectDlg::setStartState() { /*
249   ui.hostName->show(); ui.hostPort->show(); ui.hostLabel->show(); ui.portLabel->show();
250   ui.statusText->setText(tr("Connect to Quassel Core running on:"));
251   ui.buttonBox->button(QDialogButtonBox::Ok)->show();
252   ui.hostName->setEnabled(true); ui.hostPort->setEnabled(true);
253   ui.hostName->setSelection(0, ui.hostName->text().length()); */
254   ui.stackedWidget->setCurrentIndex(0);
255 }
256
257 void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
258   //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
259 }
260
261 void CoreConnectDlg::hostSelected() { /*
262   ui.hostName->hide(); ui.hostPort->hide(); ui.hostLabel->hide(); ui.portLabel->hide();
263   ui.statusText->setText(tr("Connecting to %1:%2" ).arg(ui.hostName->text()).arg(ui.hostPort->value()));
264   ui.buttonBox->button(QDialogButtonBox::Ok)->hide();
265   connect(ClientProxy::instance(), SIGNAL(coreConnected()), this, SLOT(coreConnected()));
266   connect(ClientProxy::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
267   Client::instance()->connectToCore(ui.hostName->text(), ui.hostPort->value());
268 */
269 }
270
271 void CoreConnectDlg::coreConnected() { /*
272   ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
273   ui.statusText->setText(tr("Synchronizing..."));
274   QSettings s;
275   s.setValue("GUI/CoreHost", ui.hostName->text());
276   s.setValue("GUI/CorePort", ui.hostPort->value());
277   s.setValue("GUI/CoreAutoConnect", ui.autoConnect->isChecked());
278   connect(ClientProxy::instance(), SIGNAL(recvPartialItem(quint32, quint32)), this, SLOT(updateProgressBar(quint32, quint32)));
279   connect(ClientProxy::instance(), SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(QVariant)));
280   ui.progressBar->show();
281   VarMap initmsg;
282   initmsg["GUIProtocol"] = GUI_PROTOCOL;
283   // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
284   ui.connectionStatus->setText(tr("Connected to core."));
285   accept();
286 }
287
288 void CoreConnectDlg::coreConnectionError(QString err) {
289   ui.stackedWidget->setCurrentIndex(0);
290   show(); // just in case we started hidden
291   QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Retry);
292   disconnect(ClientProxy::instance(), 0, this, 0);
293   //ui.autoConnect->setChecked(false);
294   setStartState();
295 }
296
297 void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
298   ui.connectionProgress->setMaximum(total);
299   ui.connectionProgress->setValue(partial);
300   //qDebug() << "progress:" << partial << total;
301 }
302
303 void CoreConnectDlg::recvCoreState(QVariant state) {
304   //ui.progressBar->hide();
305   coreState = state;
306   accept();
307 }
308
309 QVariant CoreConnectDlg::getCoreState() {
310   return coreState;
311 }