Two hours of bughunting ending between keyboard and chair. Or something like that...
[quassel.git] / gui / mainwin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 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 <QtCore>
23 #include <QSqlDatabase>
24
25 #include "gui.h"
26 #include "util.h"
27 #include "global.h"
28 #include "message.h"
29 #include "guiproxy.h"
30
31 #include "mainwin.h"
32 #include "buffer.h"
33 #include "bufferviewwidget.h"
34 #include "serverlist.h"
35 #include "coreconnectdlg.h"
36 #include "settingsdlg.h"
37 #include "settingspages.h"
38
39 MainWin::MainWin() : QMainWindow() {
40   ui.setupUi(this);
41   //widget = 0;
42   //qDebug() << "Available DB drivers: " << QSqlDatabase::drivers ();
43   setWindowTitle("Quassel IRC");
44   //setWindowTitle("Κυασελ Εγαρζη");
45   setWindowIcon(QIcon(":/qirc-icon.png"));
46   setWindowIconText("Quassel IRC");
47
48   //workspace = new QWorkspace(this);
49   //setCentralWidget(workspace);
50   statusBar()->showMessage(tr("Waiting for core..."));
51 }
52
53 void MainWin::init() {
54 /*
55   connect(guiProxy, SIGNAL(csServerState(QString, QVariant)), this, SLOT(recvNetworkState(QString, QVariant)));
56   connect(guiProxy, SIGNAL(csServerConnected(QString)), this, SLOT(networkConnected(QString)));
57   connect(guiProxy, SIGNAL(csServerDisconnected(QString)), this, SLOT(networkDisconnected(QString)));
58   connect(guiProxy, SIGNAL(csDisplayMsg(Message)), this, SLOT(recvMessage(Message)));
59   connect(guiProxy, SIGNAL(csDisplayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
60   connect(guiProxy, SIGNAL(csTopicSet(QString, QString, QString)), this, SLOT(setTopic(QString, QString, QString)));
61   connect(guiProxy, SIGNAL(csNickAdded(QString, QString, VarMap)), this, SLOT(addNick(QString, QString, VarMap)));
62   connect(guiProxy, SIGNAL(csNickRemoved(QString, QString)), this, SLOT(removeNick(QString, QString)));
63   connect(guiProxy, SIGNAL(csNickRenamed(QString, QString, QString)), this, SLOT(renameNick(QString, QString, QString)));
64   connect(guiProxy, SIGNAL(csNickUpdated(QString, QString, VarMap)), this, SLOT(updateNick(QString, QString, VarMap)));
65   connect(guiProxy, SIGNAL(csOwnNickSet(QString, QString)), this, SLOT(setOwnNick(QString, QString)));
66   connect(guiProxy, SIGNAL(csBacklogData(BufferId, QList<QVariant>, bool)), this, SLOT(recvBacklogData(BufferId, QList<QVariant>, bool)));
67   connect(guiProxy, SIGNAL(csUpdateBufferId(BufferId)), this, SLOT(updateBufferId(BufferId)));
68   connect(this, SIGNAL(sendInput(BufferId, QString)), guiProxy, SLOT(gsUserInput(BufferId, QString)));
69   connect(this, SIGNAL(requestBacklog(BufferId, QVariant, QVariant)), guiProxy, SLOT(gsRequestBacklog(BufferId, QVariant, QVariant)));
70
71   //layoutThread = new LayoutThread();
72   //layoutThread->start();
73   //while(!layoutThread->isRunning()) {};
74 */
75   ui.bufferWidget->init();
76
77   show();
78   //syncToCore();
79   statusBar()->showMessage(tr("Ready."));
80   systray = new QSystemTrayIcon(this);
81   systray->setIcon(QIcon(":/qirc-icon.png"));
82   systray->show();
83
84   serverListDlg = new ServerListDlg(this);
85   serverListDlg->setVisible(serverListDlg->showOnStartup());
86
87   setupSettingsDlg();
88
89   setupMenus();
90   setupViews();
91
92   QSettings s;
93   s.beginGroup("Geometry");
94   //resize(s.value("MainWinSize", QSize(500, 400)).toSize());
95   //move(s.value("MainWinPos", QPoint(50, 50)).toPoint());
96   if(s.contains("MainWinState")) restoreState(s.value("MainWinState").toByteArray());
97   s.endGroup();
98
99   s.beginGroup("Buffers");
100   QString net = s.value("CurrentNetwork", "").toString();
101   QString buf = s.value("CurrentBuffer", "").toString();
102   s.endGroup();
103   /*
104   if(!net.isEmpty()) {
105     if(buffers.contains(net)) {
106       if(buffers[net].contains(buf)) {
107         showBuffer(net, buf);
108       } else {
109         showBuffer(net, "");
110       }
111     }
112   }
113   */
114 }
115
116 MainWin::~MainWin() {
117   //typedef QHash<QString, Buffer*> BufHash;
118   //foreach(BufHash h, buffers.values()) {
119   //  foreach(Buffer *b, h.values()) {
120   //    delete b;
121   //  }
122   //}
123   //foreach(Buffer *buf, buffers.values()) delete buf;
124 }
125
126 /* This is implemented in settingspages.cpp */
127 /*
128 void MainWin::setupSettingsDlg() {
129
130 }
131 */
132
133 void MainWin::setupMenus() {
134   connect(ui.actionNetworkList, SIGNAL(triggered()), this, SLOT(showServerList()));
135   connect(ui.actionEditIdentities, SIGNAL(triggered()), serverListDlg, SLOT(editIdentities()));
136   connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
137   //ui.actionSettingsDlg->setEnabled(false);
138   connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
139   // for debugging
140   connect(ui.actionImportBacklog, SIGNAL(triggered()), this, SLOT(importBacklog()));
141   connect(this, SIGNAL(importOldBacklog()), ClientProxy::instance(), SLOT(gsImportBacklog()));
142 }
143
144 void MainWin::setupViews() {
145   
146   BufferTreeModel *model = Client::bufferModel(); // FIXME Where is the delete for that? :p
147   connect(model, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
148   //connect(this, SIGNAL(bufferSelected(Buffer *)), model, SLOT(selectBuffer(Buffer *)));
149   //connect(this, SIGNAL(bufferUpdated(Buffer *)), model, SLOT(bufferUpdated(Buffer *)));
150   //connect(this, SIGNAL(bufferActivity(Buffer::ActivityLevel, Buffer *)), model, SLOT(bufferActivity(Buffer::ActivityLevel, Buffer *)));
151   
152   BufferViewDock *all = new BufferViewDock(model, tr("All Buffers"), BufferViewFilter::AllNets);
153   registerBufferViewDock(all);
154   
155   BufferViewDock *allchans = new BufferViewDock(model, tr("All Channels"), BufferViewFilter::AllNets|BufferViewFilter::NoQueries|BufferViewFilter::NoServers);
156   registerBufferViewDock(allchans);
157   
158   BufferViewDock *allqrys = new BufferViewDock(model, tr("All Queries"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoServers);
159   registerBufferViewDock(allqrys);
160
161   
162   BufferViewDock *allnets = new BufferViewDock(model, tr("All Networks"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoQueries);
163   registerBufferViewDock(allnets);
164
165
166   ui.menuViews->addSeparator();
167 }
168
169 void MainWin::registerBufferViewDock(BufferViewDock *dock) {
170   addDockWidget(Qt::LeftDockWidgetArea, dock);
171   dock->setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
172   netViews.append(dock);
173   ui.menuViews->addAction(dock->toggleViewAction());
174   
175   /*
176   connect(this, SIGNAL(bufferSelected(Buffer *)), view, SLOT(selectBuffer(Buffer *)));
177   connect(this, SIGNAL(bufferDestroyed(Buffer *)), view, SLOT(bufferDestroyed(Buffer *)));
178   connect(view, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
179   view->setBuffers(buffers.values());
180    */
181 }
182
183 void MainWin::showServerList() {
184 //  if(!serverListDlg) {
185 //    serverListDlg = new ServerListDlg(this);
186 //  }
187   serverListDlg->show();
188   serverListDlg->raise();
189 }
190
191 void MainWin::showSettingsDlg() {
192   settingsDlg->show();
193 }
194
195 void MainWin::closeEvent(QCloseEvent *event)
196 {
197   //if (userReallyWantsToQuit()) {
198     ui.bufferWidget->saveState();
199     QSettings s;
200     s.beginGroup("Geometry");
201     s.setValue("MainWinSize", size());
202     s.setValue("MainWinPos", pos());
203     s.setValue("MainWinState", saveState());
204     s.endGroup();
205     s.beginGroup("Buffers");
206     //s.setValue("CurrentNetwork", currentNetwork);
207     s.setValue("CurrentBuffer", currentBuffer);
208     s.endGroup();
209     delete systray;
210     event->accept();
211   //} else {
212     //event->ignore();
213   //}
214 }
215
216 void MainWin::showBuffer(BufferId id) {
217   showBuffer(Client::buffer(id));
218 }
219
220 void MainWin::showBuffer(Buffer *b) {
221   currentBuffer = b->bufferId().groupId();
222   //emit bufferSelected(b);
223   //qApp->processEvents();
224   ui.bufferWidget->setBuffer(b);
225   //emit bufferSelected(b); // FIXME do we need this?
226 }
227
228 void MainWin::importBacklog() {
229   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
230                                 "<b>This will permanently delete the contents of your database!</b>",
231                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
232     emit importOldBacklog();
233   }
234 }