minor fixes. now buffers can be removed via del/backspace from customviews
[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   
42   //widget = 0;
43   //qDebug() << "Available DB drivers: " << QSqlDatabase::drivers ();
44   setWindowTitle("Quassel IRC");
45   //setWindowTitle("Κυασελ Εγαρζη");
46   setWindowIcon(QIcon(":/qirc-icon.png"));
47   setWindowIconText("Quassel IRC");
48
49   //workspace = new QWorkspace(this);
50   //setCentralWidget(workspace);
51   statusBar()->showMessage(tr("Waiting for core..."));
52   
53 }
54
55 void MainWin::init() {
56   ui.bufferWidget->init();
57
58   show();
59   //syncToCore();
60   statusBar()->showMessage(tr("Ready."));
61   systray = new QSystemTrayIcon(this);
62   systray->setIcon(QIcon(":/qirc-icon.png"));
63   systray->show();
64
65   serverListDlg = new ServerListDlg(this);
66   serverListDlg->setVisible(serverListDlg->showOnStartup());
67
68   setupSettingsDlg();
69
70   setupMenus();
71   setupViews();
72
73   QSettings s;
74   s.beginGroup("Geometry");
75   //resize(s.value("MainWinSize", QSize(500, 400)).toSize());
76   //move(s.value("MainWinPos", QPoint(50, 50)).toPoint());
77   if(s.contains("MainWinState")) restoreState(s.value("MainWinState").toByteArray());
78   s.endGroup();
79
80   s.beginGroup("Buffers");
81   QString net = s.value("CurrentNetwork", "").toString();
82   QString buf = s.value("CurrentBuffer", "").toString();
83   s.endGroup();
84   /*
85   if(!net.isEmpty()) {
86     if(buffers.contains(net)) {
87       if(buffers[net].contains(buf)) {
88         showBuffer(net, buf);
89       } else {
90         showBuffer(net, "");
91       }
92     }
93   }
94   */
95 }
96
97 MainWin::~MainWin() {
98   //typedef QHash<QString, Buffer*> BufHash;
99   //foreach(BufHash h, buffers.values()) {
100   //  foreach(Buffer *b, h.values()) {
101   //    delete b;
102   //  }
103   //}
104   //foreach(Buffer *buf, buffers.values()) delete buf;
105 }
106
107 /* This is implemented in settingspages.cpp */
108 /*
109 void MainWin::setupSettingsDlg() {
110
111 }
112 */
113
114 void MainWin::setupMenus() {
115   connect(ui.actionNetworkList, SIGNAL(triggered()), this, SLOT(showServerList()));
116   connect(ui.actionEditIdentities, SIGNAL(triggered()), serverListDlg, SLOT(editIdentities()));
117   connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
118   //ui.actionSettingsDlg->setEnabled(false);
119   connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
120   // for debugging
121   connect(ui.actionImportBacklog, SIGNAL(triggered()), this, SLOT(importBacklog()));
122   connect(this, SIGNAL(importOldBacklog()), ClientProxy::instance(), SLOT(gsImportBacklog()));
123 }
124
125 void MainWin::setupViews() {
126   
127   BufferTreeModel *model = Client::bufferModel(); // FIXME Where is the delete for that? :p
128   connect(model, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
129   
130   addBufferView(tr("All Buffers"), model, BufferViewFilter::AllNets, QStringList());
131   addBufferView(tr("All Channels"), model, BufferViewFilter::AllNets|BufferViewFilter::NoQueries|BufferViewFilter::NoServers, QStringList());
132   addBufferView(tr("All Queries"), model, BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoServers, QStringList());
133   addBufferView(tr("All Networks"), model, BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoQueries, QStringList());
134   addBufferView(tr("Full Custom"), model, BufferViewFilter::FullCustom, QStringList());
135   
136   ui.menuViews->addSeparator();
137 }
138
139 void MainWin::addBufferView(const QString &viewname, QAbstractItemModel *model, const BufferViewFilter::Modes &mode, const QStringList &nets) {
140   QDockWidget *dock = new QDockWidget(viewname, this);
141   dock->setObjectName(QString("ViewDock-" + viewname)); // should be unique for mainwindow state!
142   dock->setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
143   //dock->setContentsMargins(4,4,4,4);
144
145   //create the view and initialize it's filter
146   BufferView *view = new BufferView(dock);
147   view->setFilteredModel(model, mode, nets);
148   dock->setWidget(view);
149   
150   addDockWidget(Qt::LeftDockWidgetArea, dock);
151   ui.menuViews->addAction(dock->toggleViewAction());
152   
153   netViews.append(dock);
154 }
155
156 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
157   return new ChatLine(msg);
158 }
159
160 void MainWin::showServerList() {
161 //  if(!serverListDlg) {
162 //    serverListDlg = new ServerListDlg(this);
163 //  }
164   serverListDlg->show();
165   serverListDlg->raise();
166 }
167
168 void MainWin::showSettingsDlg() {
169   settingsDlg->show();
170 }
171
172 void MainWin::closeEvent(QCloseEvent *event)
173 {
174   //if (userReallyWantsToQuit()) {
175     ui.bufferWidget->saveState();
176     QSettings s;
177     s.beginGroup("Geometry");
178     s.setValue("MainWinSize", size());
179     s.setValue("MainWinPos", pos());
180     s.setValue("MainWinState", saveState());
181     s.endGroup();
182     s.beginGroup("Buffers");
183     //s.setValue("CurrentNetwork", currentNetwork);
184     s.setValue("CurrentBuffer", currentBuffer);
185     s.endGroup();
186     delete systray;
187     event->accept();
188   //} else {
189     //event->ignore();
190   //}
191 }
192
193 void MainWin::showBuffer(BufferId id) {
194   showBuffer(Client::buffer(id));
195 }
196
197 void MainWin::showBuffer(Buffer *b) {
198   currentBuffer = b->bufferId().groupId();
199   //emit bufferSelected(b);
200   //qApp->processEvents();
201       
202   ui.bufferWidget->setBuffer(b);
203   //emit bufferSelected(b);
204 }
205
206 void MainWin::importBacklog() {
207   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
208                                 "<b>This will permanently delete the contents of your database!</b>",
209                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
210     emit importOldBacklog();
211   }
212 }