9992496ca7d5f40ea4daef478f1e055df64ebd3e
[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     QVariantMap 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     QVariantMap 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   QVariantMap 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   QVariantMap conninfo;
214   ui.stackedWidget->setCurrentIndex(1);
215   if(ui.internalCore->isChecked()) {
216     // FIXME
217     coreConnectionError(tr("Can't connect to internal core at the moment [serious breakage due to switch to dynamic signals]. Please check back later."));
218     return;
219     if(Global::runMode != Global::Monolithic) {
220       coreConnectionError(tr("Can't connect to internal core, since we are running as a standalone GUI!"));
221       return;
222     }
223     ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
224     ui.connectionProgress->hide();
225   } else {
226     ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
227     conninfo["Host"] = ui.hostEdit->text();
228     conninfo["Port"] = ui.port->value();
229   }
230   conninfo["User"] = ui.userEdit->text();
231   conninfo["Password"] = ui.passwdEdit->text();
232   ui.profileLabel->hide(); ui.guiProfile->hide();
233   ui.newGuiProfile->hide(); ui.alwaysUseProfile->hide();
234   ui.connectionProgress->show();
235   try {
236     Client::instance()->connectToCore(conninfo);
237   } catch(Exception e) {
238     QString msg;
239     //if(!e.msg().isEmpty()) msg = tr("<br>%1").arg(e.msg()); // FIXME throw more detailed (vulgo: any) error msg
240     coreConnectionError(tr("Invalid user or password. Pleasy try again.%1").arg(msg));
241     //QMessageBox::warning(this, tr("Unknown account"), tr("Invalid user or password. Pleasy try again.%1").arg(msg));
242     //cancelConnect();
243     return;
244   }
245 }
246
247 void CoreConnectDlg::cancelConnect() {
248   ui.stackedWidget->setCurrentIndex(0);
249 }
250
251 void CoreConnectDlg::setStartState() { /*
252   ui.hostName->show(); ui.hostPort->show(); ui.hostLabel->show(); ui.portLabel->show();
253   ui.statusText->setText(tr("Connect to Quassel Core running on:"));
254   ui.buttonBox->button(QDialogButtonBox::Ok)->show();
255   ui.hostName->setEnabled(true); ui.hostPort->setEnabled(true);
256   ui.hostName->setSelection(0, ui.hostName->text().length()); */
257   ui.stackedWidget->setCurrentIndex(0);
258 }
259
260 void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
261   //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
262 }
263
264 void CoreConnectDlg::hostSelected() { /*
265   ui.hostName->hide(); ui.hostPort->hide(); ui.hostLabel->hide(); ui.portLabel->hide();
266   ui.statusText->setText(tr("Connecting to %1:%2" ).arg(ui.hostName->text()).arg(ui.hostPort->value()));
267   ui.buttonBox->button(QDialogButtonBox::Ok)->hide();
268   connect(ClientProxy::instance(), SIGNAL(coreConnected()), this, SLOT(coreConnected()));
269   connect(ClientProxy::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
270   Client::instance()->connectToCore(ui.hostName->text(), ui.hostPort->value());
271 */
272 }
273
274 void CoreConnectDlg::coreConnected() { /*
275   ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
276   ui.statusText->setText(tr("Synchronizing..."));
277   QSettings s;
278   s.setValue("GUI/CoreHost", ui.hostName->text());
279   s.setValue("GUI/CorePort", ui.hostPort->value());
280   s.setValue("GUI/CoreAutoConnect", ui.autoConnect->isChecked());
281   connect(ClientProxy::instance(), SIGNAL(recvPartialItem(quint32, quint32)), this, SLOT(updateProgressBar(quint32, quint32)));
282   connect(ClientProxy::instance(), SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(QVariant)));
283   ui.progressBar->show();
284   QVariantMap initmsg;
285   initmsg["GUIProtocol"] = GUI_PROTOCOL;
286   // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
287   ui.connectionStatus->setText(tr("Connected to core."));
288   accept();
289 }
290
291 void CoreConnectDlg::coreConnectionError(QString err) {
292   ui.stackedWidget->setCurrentIndex(0);
293   show(); // just in case we started hidden
294   QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Retry);
295   //disconnect(ClientProxy::instance(), 0, this, 0); FIXME?
296   //ui.autoConnect->setChecked(false);
297   setStartState();
298 }
299
300 void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
301   ui.connectionProgress->setMaximum(total);
302   ui.connectionProgress->setValue(partial);
303   //qDebug() << "progress:" << partial << total;
304 }
305
306 void CoreConnectDlg::recvCoreState(QVariant state) {
307   //ui.progressBar->hide();
308   coreState = state;
309   accept();
310 }
311
312 QVariant CoreConnectDlg::getCoreState() {
313   return coreState;
314 }