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