1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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"
24 #include "clientsettings.h"
25 #include "configwizard.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.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)));
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()));
59 connect(Client::instance(), SIGNAL(showConfigWizard(const QVariantMap &)), this, SLOT(showConfigWizard(const QVariantMap &)));
62 QString lastacc = s.lastAccount();
63 ui.accountList->addItems(s.knownAccounts());
64 if(!ui.accountList->count()) {
65 //if(doAutoConnect) reject();
67 setAccountEditEnabled(false);
68 QString newacc = QInputDialog::getText(this, tr("Create Account"), tr(
69 "In order to connect to a Quassel Core, you need to create an account.<br>"
70 "Please enter a name for this account now:"), QLineEdit::Normal, tr("Default"));
71 if(!newacc.isEmpty()) {
72 ui.accountList->addItem(newacc);
73 ui.hostEdit->setText("localhost");
74 ui.port->setValue(Global::defaultPort);
75 ui.internalCore->setChecked(false);
76 setAccountEditEnabled(true);
79 // FIXME We create a default account here that just connects to the internal core
81 ui.accountList->addItem("Default");
82 ui.internalCore->setChecked(true);
83 ui.userEdit->setText("Default");
84 ui.passwdEdit->setText("password");
85 ui.rememberPasswd->setChecked(true);
86 accountChanged(curacc);
87 ui.passwdEdit->setText("password");
88 ui.accountList->setCurrentIndex(0);
89 ui.autoConnect->setChecked(true);
90 autoConnectToggled(true);
93 if(!lastacc.isEmpty()) {
94 //if(doAutoConnect) { qDebug() << "auto";
96 // int idx = ui.accountList->findText(s.autoConnectAccount());
97 // if(idx < 0) reject();
99 // ui.accountList->setCurrentIndex(idx);
103 int idx = ui.accountList->findText(lastacc);
104 ui.accountList->setCurrentIndex(idx);
110 CoreConnectDlg::~CoreConnectDlg() {
111 //qDebug() << "destroy";
114 void CoreConnectDlg::setAccountEditEnabled(bool en) {
115 ui.accountList->setEnabled(en);
116 ui.hostEdit->setEnabled(en && !ui.internalCore->isChecked());
117 ui.userEdit->setEnabled(en);
118 ui.passwdEdit->setEnabled(en);
119 ui.port->setEnabled(en && !ui.internalCore->isChecked());
120 ui.editAccount->setEnabled(en);
121 ui.delAccount->setEnabled(en);
122 ui.internalCore->setEnabled(en);
123 ui.rememberPasswd->setEnabled(en);
124 ui.autoConnect->setEnabled(en);
125 ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(en && checkInputValid());
128 void CoreConnectDlg::accountChanged(const QString &text) {
130 if(!curacc.isEmpty()) {
132 oldAcc["User"] = ui.userEdit->text();
133 oldAcc["Host"] = ui.hostEdit->text();
134 oldAcc["Port"] = ui.port->value();
135 oldAcc["InternalCore"] = ui.internalCore->isChecked();
136 if(ui.rememberPasswd->isChecked()) oldAcc["Password"] = ui.passwdEdit->text();
137 s.setValue(curacc, "AccountData", oldAcc);
139 ui.autoConnect->setChecked(false);
140 if(!text.isEmpty()) { // empty text: just save stuff
142 s.setLastAccount(curacc);
143 QVariantMap newAcc = s.value(curacc, "AccountData").toMap();
144 ui.userEdit->setText(newAcc["User"].toString());
145 ui.hostEdit->setText(newAcc["Host"].toString());
146 ui.port->setValue(newAcc["Port"].toInt());
147 ui.internalCore->setChecked(newAcc["InternalCore"].toBool());
148 if(newAcc.contains("Password")) {
149 ui.passwdEdit->setText(newAcc["Password"].toString());
150 ui.rememberPasswd->setChecked(true);
151 } else ui.rememberPasswd->setChecked(false);
152 if(s.autoConnectAccount() == curacc) ui.autoConnect->setChecked(true);
156 void CoreConnectDlg::autoConnectToggled(bool autoConnect) {
158 if(autoConnect) s.setAutoConnectAccount(curacc);
159 else s.setAutoConnectAccount("");
162 bool CoreConnectDlg::checkInputValid() {
163 bool res = (ui.internalCore->isChecked() || ui.hostEdit->text().count()) && ui.userEdit->text().count();
164 ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(res);
168 void CoreConnectDlg::createAccount() {
169 QString accname = QInputDialog::getText(this, tr("Create Account"), tr("Please enter a name for the new account:"));
170 if(accname.isEmpty()) return;
171 if(ui.accountList->findText(accname) >= 0) {
172 QMessageBox::warning(this, tr("Account name already exists!"), tr("An account named '%1' already exists, and account names must be unique!").arg(accname));
176 ui.accountList->addItem(accname);
177 ui.accountList->setCurrentIndex(ui.accountList->findText(accname));
178 setAccountEditEnabled(true);
181 void CoreConnectDlg::removeAccount() {
182 QString acc = ui.accountList->currentText();
183 int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
184 "Note that this only affects your local account settings and will not remove "
185 "any data from the core.").arg(acc),
186 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
187 if(res == QMessageBox::Yes) {
189 s.removeAccount(acc);
191 ui.accountList->removeItem(ui.accountList->findText(acc));
192 if(!ui.accountList->count()) setAccountEditEnabled(false);
196 bool CoreConnectDlg::willDoInternalAutoConnect() {
198 if(Global::runMode != Global::Monolithic) return false;
199 if(ui.autoConnect->isChecked() && s.autoConnectAccount() == curacc && ui.internalCore->isChecked()) {
205 void CoreConnectDlg::doAutoConnect() {
207 if(s.autoConnectAccount() == curacc) {
212 void CoreConnectDlg::doConnect() {
213 accountChanged(); // save current account info
214 QVariantMap conninfo;
215 ui.stackedWidget->setCurrentIndex(1);
216 if(ui.internalCore->isChecked()) {
218 coreConnectionError(tr("Can't connect to internal core at the moment [serious breakage due to switch to dynamic signals]. Please check back later."));
220 if(Global::runMode != Global::Monolithic) {
221 coreConnectionError(tr("Can't connect to internal core, since we are running as a standalone GUI!"));
224 ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
225 ui.connectionProgress->hide();
227 ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
228 conninfo["Host"] = ui.hostEdit->text();
229 conninfo["Port"] = ui.port->value();
231 conninfo["User"] = ui.userEdit->text();
232 conninfo["Password"] = ui.passwdEdit->text();
233 ui.profileLabel->hide(); ui.guiProfile->hide();
234 ui.newGuiProfile->hide(); ui.alwaysUseProfile->hide();
235 ui.connectionProgress->show();
237 Client::instance()->connectToCore(conninfo);
238 } catch(Exception e) {
240 //if(!e.msg().isEmpty()) msg = tr("<br>%1").arg(e.msg()); // FIXME throw more detailed (vulgo: any) error msg
241 coreConnectionError(tr("Invalid user or password. Pleasy try again.%1").arg(msg));
242 //QMessageBox::warning(this, tr("Unknown account"), tr("Invalid user or password. Pleasy try again.%1").arg(msg));
248 void CoreConnectDlg::cancelConnect() {
249 ui.stackedWidget->setCurrentIndex(0);
252 void CoreConnectDlg::setStartState() { /*
253 ui.hostName->show(); ui.hostPort->show(); ui.hostLabel->show(); ui.portLabel->show();
254 ui.statusText->setText(tr("Connect to Quassel Core running on:"));
255 ui.buttonBox->button(QDialogButtonBox::Ok)->show();
256 ui.hostName->setEnabled(true); ui.hostPort->setEnabled(true);
257 ui.hostName->setSelection(0, ui.hostName->text().length()); */
258 ui.stackedWidget->setCurrentIndex(0);
261 void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
262 //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
265 void CoreConnectDlg::hostSelected() { /*
266 ui.hostName->hide(); ui.hostPort->hide(); ui.hostLabel->hide(); ui.portLabel->hide();
267 ui.statusText->setText(tr("Connecting to %1:%2" ).arg(ui.hostName->text()).arg(ui.hostPort->value()));
268 ui.buttonBox->button(QDialogButtonBox::Ok)->hide();
269 connect(ClientProxy::instance(), SIGNAL(coreConnected()), this, SLOT(coreConnected()));
270 connect(ClientProxy::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
271 Client::instance()->connectToCore(ui.hostName->text(), ui.hostPort->value());
275 void CoreConnectDlg::coreConnected() { /*
276 ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
277 ui.statusText->setText(tr("Synchronizing..."));
279 s.setValue("GUI/CoreHost", ui.hostName->text());
280 s.setValue("GUI/CorePort", ui.hostPort->value());
281 s.setValue("GUI/CoreAutoConnect", ui.autoConnect->isChecked());
282 connect(ClientProxy::instance(), SIGNAL(recvPartialItem(quint32, quint32)), this, SLOT(updateProgressBar(quint32, quint32)));
283 connect(ClientProxy::instance(), SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(QVariant)));
284 ui.progressBar->show();
286 initmsg["GUIProtocol"] = GUI_PROTOCOL;
287 // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
288 ui.connectionStatus->setText(tr("Connected to core."));
292 void CoreConnectDlg::coreConnectionError(QString err) {
293 ui.stackedWidget->setCurrentIndex(0);
294 show(); // just in case we started hidden
295 QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Retry);
296 //disconnect(ClientProxy::instance(), 0, this, 0); FIXME?
297 //ui.autoConnect->setChecked(false);
301 void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
302 ui.connectionProgress->setMaximum(total);
303 ui.connectionProgress->setValue(partial);
304 //qDebug() << "progress:" << partial << total;
307 void CoreConnectDlg::recvCoreState(QVariant state) {
308 //ui.progressBar->hide();
313 QVariant CoreConnectDlg::getCoreState() {
317 void CoreConnectDlg::showConfigWizard(const QVariantMap &coredata) {
318 QStringList storageProviders = coredata["StorageProviders"].toStringList();
319 ConfigWizard *wizard = new ConfigWizard(storageProviders, this);
322 reply["GuiProtocol"] = GUI_PROTOCOL;
323 reply["HasSettings"] = true;
324 reply["User"] = wizard->field("adminuser.name").toString();
325 reply["Password"] = wizard->field("adminuser.password").toString();
326 QString sp = storageProviders.value(wizard->field("storage.provider").toInt());
328 if (sp.compare("Sqlite", Qt::CaseInsensitive)) {
329 reply["Host"] = wizard->field("storage.host").toString();
330 reply["Port"] = wizard->field("storage.port").toString();
331 reply["Database"] = wizard->field("storage.database").toString();
332 reply["User"] = wizard->field("storage.user").toString();
333 reply["Password"] = wizard->field("storage.password").toString();
335 Client::instance()->setCoreConfiguration(reply);