some minor tweaks
[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 "chatline.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   ui.bufferWidget->init();
55
56   show();
57   //syncToCore();
58   statusBar()->showMessage(tr("Ready."));
59   systray = new QSystemTrayIcon(this);
60   systray->setIcon(QIcon(":/qirc-icon.png"));
61   systray->show();
62
63   serverListDlg = new ServerListDlg(this);
64   serverListDlg->setVisible(serverListDlg->showOnStartup());
65
66   setupSettingsDlg();
67
68   setupMenus();
69   setupViews();
70
71   QSettings s;
72   s.beginGroup("Geometry");
73   //resize(s.value("MainWinSize", QSize(500, 400)).toSize());
74   //move(s.value("MainWinPos", QPoint(50, 50)).toPoint());
75   if(s.contains("MainWinState")) restoreState(s.value("MainWinState").toByteArray());
76   s.endGroup();
77
78   s.beginGroup("Buffers");
79   QString net = s.value("CurrentNetwork", "").toString();
80   QString buf = s.value("CurrentBuffer", "").toString();
81   s.endGroup();
82   /*
83   if(!net.isEmpty()) {
84     if(buffers.contains(net)) {
85       if(buffers[net].contains(buf)) {
86         showBuffer(net, buf);
87       } else {
88         showBuffer(net, "");
89       }
90     }
91   }
92   */
93 }
94
95 MainWin::~MainWin() {
96   //typedef QHash<QString, Buffer*> BufHash;
97   //foreach(BufHash h, buffers.values()) {
98   //  foreach(Buffer *b, h.values()) {
99   //    delete b;
100   //  }
101   //}
102   //foreach(Buffer *buf, buffers.values()) delete buf;
103 }
104
105 /* This is implemented in settingspages.cpp */
106 /*
107 void MainWin::setupSettingsDlg() {
108
109 }
110 */
111
112 void MainWin::setupMenus() {
113   connect(ui.actionNetworkList, SIGNAL(triggered()), this, SLOT(showServerList()));
114   connect(ui.actionEditIdentities, SIGNAL(triggered()), serverListDlg, SLOT(editIdentities()));
115   connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
116   //ui.actionSettingsDlg->setEnabled(false);
117   connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
118   // for debugging
119   connect(ui.actionImportBacklog, SIGNAL(triggered()), this, SLOT(importBacklog()));
120   connect(this, SIGNAL(importOldBacklog()), ClientProxy::instance(), SLOT(gsImportBacklog()));
121 }
122
123 void MainWin::setupViews() {
124   
125   BufferTreeModel *model = Client::bufferModel(); // FIXME Where is the delete for that? :p
126   connect(model, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
127   //connect(this, SIGNAL(bufferSelected(Buffer *)), model, SLOT(selectBuffer(Buffer *)));
128   //connect(this, SIGNAL(bufferUpdated(Buffer *)), model, SLOT(bufferUpdated(Buffer *)));
129   //connect(this, SIGNAL(bufferActivity(Buffer::ActivityLevel, Buffer *)), model, SLOT(bufferActivity(Buffer::ActivityLevel, Buffer *)));
130   
131   BufferViewDock *all = new BufferViewDock(model, tr("All Buffers"), BufferViewFilter::AllNets, QStringList(), this);
132   registerBufferViewDock(all);
133   
134   BufferViewDock *allchans = new BufferViewDock(model, tr("All Channels"), BufferViewFilter::AllNets|BufferViewFilter::NoQueries|BufferViewFilter::NoServers, QStringList(), this);
135   registerBufferViewDock(allchans);
136   
137   BufferViewDock *allqrys = new BufferViewDock(model, tr("All Queries"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoServers, QStringList(), this);
138   registerBufferViewDock(allqrys);
139
140   
141   BufferViewDock *allnets = new BufferViewDock(model, tr("All Networks"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoQueries, QStringList(), this);
142   registerBufferViewDock(allnets);
143
144   BufferViewDock *fullCustom = new BufferViewDock(model, tr("Full Custom"), BufferViewFilter::FullCustom|BufferViewFilter::SomeNets, QStringList(), this);
145   registerBufferViewDock(fullCustom);
146
147   ui.menuViews->addSeparator();
148 }
149
150 void MainWin::registerBufferViewDock(BufferViewDock *dock) {
151   addDockWidget(Qt::LeftDockWidgetArea, dock);
152   dock->setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
153   netViews.append(dock);
154   ui.menuViews->addAction(dock->toggleViewAction());
155   
156   /*
157   connect(this, SIGNAL(bufferSelected(Buffer *)), view, SLOT(selectBuffer(Buffer *)));
158   connect(this, SIGNAL(bufferDestroyed(Buffer *)), view, SLOT(bufferDestroyed(Buffer *)));
159   connect(view, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
160   view->setBuffers(buffers.values());
161    */
162 }
163
164 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
165   return new ChatLine(msg);
166 }
167
168 void MainWin::showServerList() {
169 //  if(!serverListDlg) {
170 //    serverListDlg = new ServerListDlg(this);
171 //  }
172   serverListDlg->show();
173   serverListDlg->raise();
174 }
175
176 void MainWin::showSettingsDlg() {
177   settingsDlg->show();
178 }
179
180 void MainWin::closeEvent(QCloseEvent *event)
181 {
182   //if (userReallyWantsToQuit()) {
183     ui.bufferWidget->saveState();
184     QSettings s;
185     s.beginGroup("Geometry");
186     s.setValue("MainWinSize", size());
187     s.setValue("MainWinPos", pos());
188     s.setValue("MainWinState", saveState());
189     s.endGroup();
190     s.beginGroup("Buffers");
191     //s.setValue("CurrentNetwork", currentNetwork);
192     s.setValue("CurrentBuffer", currentBuffer);
193     s.endGroup();
194     delete systray;
195     event->accept();
196   //} else {
197     //event->ignore();
198   //}
199 }
200
201 void MainWin::showBuffer(BufferId id) {
202   showBuffer(Client::buffer(id));
203 }
204
205 void MainWin::showBuffer(Buffer *b) {
206   currentBuffer = b->bufferId().groupId();
207   //emit bufferSelected(b);
208   //qApp->processEvents();
209   ui.bufferWidget->setBuffer(b);
210   //emit bufferSelected(b);
211 }
212
213 void MainWin::importBacklog() {
214   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
215                                 "<b>This will permanently delete the contents of your database!</b>",
216                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
217     emit importOldBacklog();
218   }
219 }