More small tweaks to fix scrollbar behavior. This really sucks.
[quassel.git] / main / main_gui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 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 <iostream>
22
23 #include <QtGui>
24 #include <QApplication>
25
26 #include "global.h"
27 #include "guiproxy.h"
28 #include "coreconnectdlg.h"
29 #include "util.h"
30
31 #include "mainwin.h"
32
33 int main(int argc, char **argv) {
34   QApplication app(argc, argv);
35   QApplication::setOrganizationDomain("quassel-irc.org");
36   QApplication::setApplicationName("Quassel IRC");
37   QApplication::setOrganizationName("The Quassel Team");
38
39   Global::runMode = Global::GUIOnly;
40   Global::quasselDir = QDir::homePath() + "/.quassel";
41
42   global = new Global();
43   guiProxy = new GUIProxy();
44
45   MainWin mainWin;
46   int exitCode = app.exec();
47   delete guiProxy;
48   delete global;
49 }
50
51 void MainWin::syncToCore() {
52   Q_ASSERT(!global->getData("CoreReady").toBool());
53   // ok, we are running as standalone GUI
54   coreConnectDlg = new CoreConnectDlg(this);
55   if(coreConnectDlg->exec() != QDialog::Accepted) {
56     //qApp->quit();
57     exit(1);
58   }
59   VarMap state = coreConnectDlg->getCoreState().toMap();
60   delete coreConnectDlg;
61   VarMap data = state["CoreData"].toMap();
62   QString key;
63   foreach(key, data.keys()) {
64     global->updateData(key, data[key]);
65   }
66   if(!global->getData("CoreReady").toBool()) {
67     QMessageBox::critical(this, tr("Fatal Error"), tr("<b>Could not synchronize with Quassel Core!</b><br>Quassel GUI will be aborted."), QMessageBox::Abort);
68     //qApp->quit();
69     exit(1);
70   }
71   foreach(QString net, state["CoreBackLog"].toMap().keys()) {
72     QByteArray logbuf = state["CoreBackLog"].toMap()[net].toByteArray();
73     QDataStream in(&logbuf, QIODevice::ReadOnly); in.setVersion(QDataStream::Qt_4_2);
74     while(!in.atEnd()) {
75       Message msg; in >> msg;
76       coreBackLog[net].append(msg);
77     }
78     qDebug() << net << coreBackLog[net].count();
79   }
80 }
81
82 GUIProxy::GUIProxy() {
83   if(guiProxy) qFatal("Trying to instantiate more than one CoreProxy object!");
84
85   blockSize = 0;
86
87   connect(&socket, SIGNAL(readyRead()), this, SLOT(serverHasData()));
88   connect(&socket, SIGNAL(connected()), this, SIGNAL(coreConnected()));
89   connect(&socket, SIGNAL(disconnected()), this, SIGNAL(coreDisconnected()));
90   connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverError(QAbstractSocket::SocketError)));
91
92   connect(global, SIGNAL(dataPutLocally(QString)), this, SLOT(updateCoreData(QString)));
93   connect(this, SIGNAL(csUpdateGlobalData(QString, QVariant)), global, SLOT(updateData(QString, QVariant)));
94
95 }
96
97 void GUIProxy::connectToCore(QString host, quint16 port) {
98   socket.connectToHost(host, port);
99 }
100
101 void GUIProxy::disconnectFromCore() {
102   socket.close();
103 }
104
105 void GUIProxy::serverError(QAbstractSocket::SocketError) {
106   emit coreConnectionError(socket.errorString());
107   //qFatal(QString("Connection error: %1").arg(socket.errorString()).toAscii());
108 }
109
110 void GUIProxy::serverHasData() {
111   QVariant item;
112   while(readDataFromDevice(&socket, blockSize, item)) {
113     emit recvPartialItem(1,1);
114     QList<QVariant> sigdata = item.toList();
115     Q_ASSERT(sigdata.size() == 4);
116     recv((CoreSignal)sigdata[0].toInt(), sigdata[1], sigdata[2], sigdata[3]);
117     blockSize = 0;
118   }
119   if(blockSize > 0) {
120     emit recvPartialItem(socket.bytesAvailable(), blockSize);
121   }
122 }
123
124 void GUIProxy::send(GUISignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
125   QList<QVariant> sigdata;
126   sigdata.append(sig); sigdata.append(arg1); sigdata.append(arg2); sigdata.append(arg3);
127   //qDebug() << "Sending signal: " << sigdata;
128   writeDataToDevice(&socket, QVariant(sigdata));
129 }
130
131 void GUIProxy::updateCoreData(QString key) {
132   QVariant data = global->getData(key);
133   send(GS_UPDATE_GLOBAL_DATA, key, data);
134 }