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