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