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