still continuing separation of gui and data and file reorganisation:
[quassel.git] / src / qtgui / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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
27 CoreConnectDlg::CoreConnectDlg(QWidget *parent) : QDialog(parent) {
28   ui.setupUi(this);
29   ui.progressBar->hide();
30   coreState = 0;
31   QSettings s;
32   connect(ui.hostName, SIGNAL(textChanged(QString)), this, SLOT(hostEditChanged(QString)));
33   connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(hostSelected()));
34
35   ui.hostName->setText(s.value("GUI/CoreHost", "localhost").toString());
36   ui.hostName->setSelection(0, ui.hostName->text().length());
37   ui.hostPort->setValue(s.value("GUI/CorePort", 4242).toInt());
38   ui.autoConnect->setChecked(s.value("GUI/CoreAutoConnect", true).toBool());
39   if(s.value("GUI/CoreAutoConnect").toBool()) {
40     hostSelected();
41   }
42 }
43
44 void CoreConnectDlg::setStartState() {
45   ui.hostName->show(); ui.hostPort->show(); ui.hostLabel->show(); ui.portLabel->show();
46   ui.statusText->setText(tr("Connect to Quassel Core running on:"));
47   ui.buttonBox->button(QDialogButtonBox::Ok)->show();
48   ui.hostName->setEnabled(true); ui.hostPort->setEnabled(true);
49   ui.hostName->setSelection(0, ui.hostName->text().length());
50 }
51
52 void CoreConnectDlg::hostEditChanged(QString txt) {
53   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
54 }
55
56 void CoreConnectDlg::hostSelected() {
57   ui.hostName->hide(); ui.hostPort->hide(); ui.hostLabel->hide(); ui.portLabel->hide();
58   ui.statusText->setText(tr("Connecting to %1:%2" ).arg(ui.hostName->text()).arg(ui.hostPort->value()));
59   ui.buttonBox->button(QDialogButtonBox::Ok)->hide();
60   connect(ClientProxy::instance(), SIGNAL(coreConnected()), this, SLOT(coreConnected()));
61   connect(ClientProxy::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
62   Client::instance()->connectToCore(ui.hostName->text(), ui.hostPort->value());
63
64 }
65
66 void CoreConnectDlg::coreConnected() {
67   ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
68   ui.statusText->setText(tr("Synchronizing..."));
69   QSettings s;
70   s.setValue("GUI/CoreHost", ui.hostName->text());
71   s.setValue("GUI/CorePort", ui.hostPort->value());
72   s.setValue("GUI/CoreAutoConnect", ui.autoConnect->isChecked());
73   connect(ClientProxy::instance(), SIGNAL(recvPartialItem(quint32, quint32)), this, SLOT(updateProgressBar(quint32, quint32)));
74   connect(ClientProxy::instance(), SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(QVariant)));
75   ui.progressBar->show();
76   VarMap initmsg;
77   initmsg["GUIProtocol"] = GUI_PROTOCOL;
78   // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg));
79 }
80
81 void CoreConnectDlg::coreConnectionError(QString err) {
82   QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Retry);
83   disconnect(ClientProxy::instance(), 0, this, 0);
84   ui.autoConnect->setChecked(false);
85   setStartState();
86 }
87
88 void CoreConnectDlg::updateProgressBar(quint32 recv, quint32 avail) {
89   ui.progressBar->setMaximum(avail);
90   ui.progressBar->setValue(recv);
91 }
92
93 void CoreConnectDlg::recvCoreState(QVariant state) {
94   ui.progressBar->hide();
95   coreState = state;
96   accept();
97 }
98
99 QVariant CoreConnectDlg::getCoreState() {
100   return coreState;
101 }