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