0ef57c18dac415be87fbb4a98e3193254c4769d2
[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);
132   registerBufferViewDock(all);
133   
134   BufferViewDock *allchans = new BufferViewDock(model, tr("All Channels"), BufferViewFilter::AllNets|BufferViewFilter::NoQueries|BufferViewFilter::NoServers);
135   registerBufferViewDock(allchans);
136   
137   BufferViewDock *allqrys = new BufferViewDock(model, tr("All Queries"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoServers);
138   registerBufferViewDock(allqrys);
139
140   
141   BufferViewDock *allnets = new BufferViewDock(model, tr("All Networks"), BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoQueries);
142   registerBufferViewDock(allnets);
143
144
145   ui.menuViews->addSeparator();
146 }
147
148 void MainWin::registerBufferViewDock(BufferViewDock *dock) {
149   addDockWidget(Qt::LeftDockWidgetArea, dock);
150   dock->setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
151   netViews.append(dock);
152   ui.menuViews->addAction(dock->toggleViewAction());
153   
154   /*
155   connect(this, SIGNAL(bufferSelected(Buffer *)), view, SLOT(selectBuffer(Buffer *)));
156   connect(this, SIGNAL(bufferDestroyed(Buffer *)), view, SLOT(bufferDestroyed(Buffer *)));
157   connect(view, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
158   view->setBuffers(buffers.values());
159    */
160 }
161
162 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
163   return new ChatLine(msg);
164 }
165
166 void MainWin::showServerList() {
167 //  if(!serverListDlg) {
168 //    serverListDlg = new ServerListDlg(this);
169 //  }
170   serverListDlg->show();
171   serverListDlg->raise();
172 }
173
174 void MainWin::showSettingsDlg() {
175   settingsDlg->show();
176 }
177
178 void MainWin::closeEvent(QCloseEvent *event)
179 {
180   //if (userReallyWantsToQuit()) {
181     ui.bufferWidget->saveState();
182     QSettings s;
183     s.beginGroup("Geometry");
184     s.setValue("MainWinSize", size());
185     s.setValue("MainWinPos", pos());
186     s.setValue("MainWinState", saveState());
187     s.endGroup();
188     s.beginGroup("Buffers");
189     //s.setValue("CurrentNetwork", currentNetwork);
190     s.setValue("CurrentBuffer", currentBuffer);
191     s.endGroup();
192     delete systray;
193     event->accept();
194   //} else {
195     //event->ignore();
196   //}
197 }
198
199 void MainWin::showBuffer(BufferId id) {
200   showBuffer(Client::buffer(id));
201 }
202
203 void MainWin::showBuffer(Buffer *b) {
204   currentBuffer = b->bufferId().groupId();
205   //emit bufferSelected(b);
206   //qApp->processEvents();
207   ui.bufferWidget->setBuffer(b);
208   //emit bufferSelected(b);
209 }
210
211 void MainWin::importBacklog() {
212   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
213                                 "<b>This will permanently delete the contents of your database!</b>",
214                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
215     emit importOldBacklog();
216   }
217 }