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