Finishing the renaming of the BufferTreeView, since SVN doesn't allow
[quassel.git] / src / qtui / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC 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) version 3.                                           *
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 "client.h"
24 #include "clientsettings.h"
25 #include "configwizard.h"
26 #include "global.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   connect(Client::instance(), SIGNAL(showConfigWizard(const QVariantMap &)), this, SLOT(showConfigWizard(const QVariantMap &)));
60
61   AccountSettings s;
62   QString lastacc = s.lastAccount();
63   ui.accountList->addItems(s.knownAccounts());
64   if(!ui.accountList->count()) {
65     //if(doAutoConnect) reject();
66
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(DEFAULT_PORT);
75       ui.internalCore->setChecked(false);
76       setAccountEditEnabled(true);
77     }
78     /*
79     // FIXME We create a default account here that just connects to the internal core
80     curacc = "Default";
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);
91     */
92   } else {
93     if(!lastacc.isEmpty()) {
94       //if(doAutoConnect) { qDebug() << "auto";
95       //  AccountSettings s;
96       //  int idx = ui.accountList->findText(s.autoConnectAccount());
97       //  if(idx < 0) reject();
98       //  else {
99       //    ui.accountList->setCurrentIndex(idx);
100       //    doConnect();
101       //  }
102       //} else {
103         int idx = ui.accountList->findText(lastacc);
104         ui.accountList->setCurrentIndex(idx);
105       //}
106     }
107   }
108 }
109
110 CoreConnectDlg::~CoreConnectDlg() {
111   //qDebug() << "destroy";
112 }
113
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());
126 }
127
128 void CoreConnectDlg::accountChanged(const QString &text) {
129   AccountSettings s;
130   if(!curacc.isEmpty()) {
131     QVariantMap oldAcc;
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);
138   }
139   ui.autoConnect->setChecked(false);
140   if(!text.isEmpty()) { // empty text: just save stuff
141     curacc = text;
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);
153   }
154 }
155
156 void CoreConnectDlg::autoConnectToggled(bool autoConnect) {
157   AccountSettings s;
158   if(autoConnect) s.setAutoConnectAccount(curacc);
159   else s.setAutoConnectAccount("");
160 }
161
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);
165   return res;
166 }
167
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));
173     return;
174   }
175   QVariantMap defdata;
176   ui.accountList->addItem(accname);
177   ui.accountList->setCurrentIndex(ui.accountList->findText(accname));
178   setAccountEditEnabled(true);
179 }
180
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) {
188     AccountSettings s;
189     s.removeAccount(acc);
190     curacc = "";
191     ui.accountList->removeItem(ui.accountList->findText(acc));
192     if(!ui.accountList->count()) setAccountEditEnabled(false);
193   }
194 }
195
196 bool CoreConnectDlg::willDoInternalAutoConnect() {
197   AccountSettings s;
198   if(Global::runMode != Global::Monolithic) return false;
199   if(ui.autoConnect->isChecked() && s.autoConnectAccount() == curacc && ui.internalCore->isChecked()) {
200     return true;
201   }
202   return false;
203 }
204
205 void CoreConnectDlg::doAutoConnect() {
206   AccountSettings s;
207   if(s.autoConnectAccount() == curacc) {
208     doConnect();
209   }
210 }
211
212 void CoreConnectDlg::doConnect() {
213   accountChanged(); // save current account info
214   QVariantMap conninfo;
215   ui.stackedWidget->setCurrentIndex(1);
216   if(ui.internalCore->isChecked()) {
217     // FIXME
218     coreConnectionError(tr("Can't connect to internal core at the moment [serious breakage due to switch to dynamic signals]. Please check back later."));
219     return;
220     if(Global::runMode != Global::Monolithic) {
221       coreConnectionError(tr("Can't connect to internal core, since we are running as a standalone GUI!"));
222       return;
223     }
224     ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
225     ui.connectionProgress->hide();
226   } else {
227     ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
228     conninfo["Host"] = ui.hostEdit->text();
229     conninfo["Port"] = ui.port->value();
230   }
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();
236   try {
237     Client::instance()->connectToCore(conninfo);
238   } catch(Exception e) {
239     QString msg;
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));
243     //cancelConnect();
244     return;
245   }
246 }
247
248 void CoreConnectDlg::cancelConnect() {
249   ui.stackedWidget->setCurrentIndex(0);
250 }
251
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);
259 }
260
261 void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
262   //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
263 }
264
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());
272 */
273 }
274
275 void CoreConnectDlg::coreConnected() { /*
276   ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
277   ui.statusText->setText(tr("Synchronizing..."));
278   QSettings s;
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();
285   QVariantMap initmsg;
286   initmsg["GUIProtocol"] = GUI_PROTOCOL;
287   // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
288   ui.connectionStatus->setText(tr("Connected to core."));
289   accept();
290 }
291
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);
298   setStartState();
299 }
300
301 void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
302   ui.connectionProgress->setMaximum(total);
303   ui.connectionProgress->setValue(partial);
304   //qDebug() << "progress:" << partial << total;
305 }
306
307 void CoreConnectDlg::recvCoreState(QVariant state) {
308   //ui.progressBar->hide();
309   coreState = state;
310   accept();
311 }
312
313 QVariant CoreConnectDlg::getCoreState() {
314   return coreState;
315 }
316
317 void CoreConnectDlg::showConfigWizard(const QVariantMap &coredata) {
318   QStringList storageProviders = coredata["StorageProviders"].toStringList();
319   ConfigWizard *wizard = new ConfigWizard(storageProviders, this);
320   wizard->exec();
321   QVariantMap reply;
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());
327   reply["Type"] = sp;
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();
334   }
335   Client::instance()->setCoreConfiguration(reply);
336 }