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