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