1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include "coreconnectdlg.h"
23 #include "clientproxy.h"
26 #include "clientsettings.h"
28 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool /*doAutoConnect*/) : QDialog(parent) {
29 ui.setupUi(this); //qDebug() << "new dlg";
31 setAttribute(Qt::WA_DeleteOnClose);
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);
40 //ui.internalCore->hide();
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)));
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()));
58 ui.accountList->addItems(s.knownAccounts());
59 curacc = s.lastAccount();
60 if(!ui.accountList->count()) {
61 //if(doAutoConnect) reject();
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);
75 // FIXME We create a default account here that just connects to the internal core
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);
89 if(!curacc.isEmpty()) {
90 //if(doAutoConnect) { qDebug() << "auto";
92 // int idx = ui.accountList->findText(s.autoConnectAccount());
93 // if(idx < 0) reject();
95 // ui.accountList->setCurrentIndex(idx);
99 int idx = ui.accountList->findText(curacc);
100 ui.accountList->setCurrentIndex(idx);
106 CoreConnectDlg::~CoreConnectDlg() {
107 //qDebug() << "destroy";
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());
124 void CoreConnectDlg::accountChanged(const QString &text) {
126 if(!curacc.isEmpty()) {
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);
135 ui.autoConnect->setChecked(false);
136 if(!text.isEmpty()) { // empty text: just save stuff
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);
152 void CoreConnectDlg::autoConnectToggled(bool autoConnect) {
154 if(autoConnect) s.setAutoConnectAccount(curacc);
155 else s.setAutoConnectAccount("");
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);
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));
172 ui.accountList->addItem(accname);
173 ui.accountList->setCurrentIndex(ui.accountList->findText(accname));
174 setAccountEditEnabled(true);
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) {
185 s.removeAccount(acc);
187 ui.accountList->removeItem(ui.accountList->findText(acc));
188 if(!ui.accountList->count()) setAccountEditEnabled(false);
192 bool CoreConnectDlg::willDoInternalAutoConnect() {
194 if(Global::runMode != Global::Monolithic) return false;
195 if(ui.autoConnect->isChecked() && s.autoConnectAccount() == curacc && ui.internalCore->isChecked()) {
201 void CoreConnectDlg::doAutoConnect() {
203 if(s.autoConnectAccount() == curacc) {
208 void CoreConnectDlg::doConnect() {
209 accountChanged(); // save current account info
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!"));
218 ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
219 ui.connectionProgress->hide();
221 ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
222 conninfo["Host"] = ui.hostEdit->text();
223 conninfo["Port"] = ui.port->value();
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();
231 Client::instance()->connectToCore(conninfo);
232 } catch(Exception e) {
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));
242 void CoreConnectDlg::cancelConnect() {
243 ui.stackedWidget->setCurrentIndex(0);
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);
255 void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
256 //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
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());
269 void CoreConnectDlg::coreConnected() { /*
270 ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
271 ui.statusText->setText(tr("Synchronizing..."));
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();
280 initmsg["GUIProtocol"] = GUI_PROTOCOL;
281 // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
282 ui.connectionStatus->setText(tr("Connected to core."));
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);
295 void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
296 ui.connectionProgress->setMaximum(total);
297 ui.connectionProgress->setValue(partial);
298 //qDebug() << "progress:" << partial << total;
301 void CoreConnectDlg::recvCoreState(QVariant state) {
302 //ui.progressBar->hide();
307 QVariant CoreConnectDlg::getCoreState() {