2bc5cf749e8550b3ce2f1ecb2b0aca9d0639384c
[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 "coreconnectdlg.h"
27 #include "serverlist.h"
28 #include "settingsdlg.h"
29 //#include "settingspage.h"
30 #include "signalproxy.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   Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferId, QVariant, QVariant)));
45   ui.bufferWidget->init();
46
47   show();
48
49   //QVariantMap connInfo;
50   //connInfo["User"] = "Default";
51   //connInfo["Password"] = "password";
52   //connectToCore(connInfo);
53
54   statusBar()->showMessage(tr("Not connected to core."));
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   disconnectedFromCore();  // Disable menus and stuff
80   showCoreConnectionDlg(true); // autoconnect if appropriate
81   //ui.actionConnectCore->activate(QAction::Trigger);
82 }
83
84 MainWin::~MainWin() {
85   //typedef QHash<QString, Buffer*> BufHash;
86   //foreach(BufHash h, buffers.values()) {
87   //  foreach(Buffer *b, h.values()) {
88   //    delete b;
89   //  }
90   //}
91   //foreach(Buffer *buf, buffers.values()) delete buf;
92 }
93
94 /* This is implemented in settingspages.cpp */
95 /*
96 void MainWin::setupSettingsDlg() {
97
98 }
99 */
100
101 void MainWin::setupMenus() {
102   connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
103   connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
104   connect(ui.actionNetworkList, SIGNAL(triggered()), this, SLOT(showServerList()));
105   connect(ui.actionEditIdentities, SIGNAL(triggered()), serverListDlg, SLOT(editIdentities()));
106   connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
107   ui.actionSettingsDlg->setEnabled(false);
108   connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
109   // for debugging
110   connect(ui.actionImportBacklog, SIGNAL(triggered()), this, SLOT(importBacklog()));
111   Client::signalProxy()->attachSignal(this, SIGNAL(importOldBacklog()));
112 }
113
114 void MainWin::setupViews() {
115   
116   BufferTreeModel *model = Client::bufferModel();
117   connect(model, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
118
119   addBufferView(tr("All Buffers"), model, BufferViewFilter::AllNets, QStringList());
120   addBufferView(tr("All Channels"), model, BufferViewFilter::AllNets|BufferViewFilter::NoQueries|BufferViewFilter::NoServers, QStringList());
121   addBufferView(tr("All Queries"), model, BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoServers, QStringList());
122   addBufferView(tr("All Networks"), model, BufferViewFilter::AllNets|BufferViewFilter::NoChannels|BufferViewFilter::NoQueries, QStringList());
123   addBufferView(tr("Full Custom"), model, BufferViewFilter::FullCustom, QStringList());
124   
125   ui.menuViews->addSeparator();
126 }
127
128 void MainWin::addBufferView(const QString &viewname, QAbstractItemModel *model, const BufferViewFilter::Modes &mode, const QStringList &nets) {
129   QDockWidget *dock = new QDockWidget(viewname, this);
130   dock->setObjectName(QString("ViewDock-" + viewname)); // should be unique for mainwindow state!
131   dock->setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
132   //dock->setContentsMargins(4,4,4,4);
133
134   //create the view and initialize it's filter
135   BufferView *view = new BufferView(dock);
136   view->setFilteredModel(model, mode, nets);
137   dock->setWidget(view);
138   
139   addDockWidget(Qt::LeftDockWidgetArea, dock);
140   ui.menuViews->addAction(dock->toggleViewAction());
141
142   netViews.append(dock);
143 }
144
145 void MainWin::connectedToCore() {
146   foreach(BufferId id, Client::allBufferIds()) {
147     emit requestBacklog(id, 100, -1);
148   }
149
150   ui.menuViews->setEnabled(true);
151   ui.menuCore->setEnabled(true);
152   ui.actionConnectCore->setEnabled(false);
153   ui.actionDisconnectCore->setEnabled(true);
154   ui.actionNetworkList->setEnabled(true);
155   ui.bufferWidget->show();
156 }
157
158 void MainWin::disconnectedFromCore() {
159   ui.menuViews->setEnabled(false);
160   ui.menuCore->setEnabled(false);
161   ui.actionDisconnectCore->setEnabled(false);
162   ui.actionNetworkList->setEnabled(false);
163   ui.bufferWidget->hide();
164   ui.actionConnectCore->setEnabled(true);
165   //qDebug() << "mainwin disconnected";
166 }
167
168 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
169   return new ChatLine(msg);
170 }
171
172 void MainWin::showCoreConnectionDlg(bool autoConnect) {
173   coreConnectDlg = new CoreConnectDlg(this, autoConnect);
174   connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
175   coreConnectDlg->setModal(true);
176   if(!autoConnect || !coreConnectDlg->willDoInternalAutoConnect())
177     coreConnectDlg->show(); // avoid flicker and show dlg only if we do remote connect, which needs a progress bar
178   if(autoConnect) coreConnectDlg->doAutoConnect();
179 }
180
181 void MainWin::coreConnectionDlgFinished(int /*code*/) {
182
183   coreConnectDlg->close();
184 }
185
186
187 void MainWin::showServerList() {
188 //  if(!serverListDlg) {
189 //    serverListDlg = new ServerListDlg(this);
190 //  }
191   serverListDlg->show();
192   serverListDlg->raise();
193 }
194
195 void MainWin::showSettingsDlg() {
196   settingsDlg->show();
197 }
198
199 void MainWin::closeEvent(QCloseEvent *event)
200 {
201   //if (userReallyWantsToQuit()) {
202     ui.bufferWidget->saveState();
203     QSettings s;
204     s.beginGroup("Geometry");
205     s.setValue("MainWinSize", size());
206     s.setValue("MainWinPos", pos());
207     s.setValue("MainWinState", saveState());
208     s.endGroup();
209     s.beginGroup("Buffers");
210     //s.setValue("CurrentNetwork", currentNetwork);
211     s.setValue("CurrentBuffer", currentBuffer);
212     s.endGroup();
213     delete systray;
214     event->accept();
215   //} else {
216     //event->ignore();
217   //}
218 }
219
220 void MainWin::showBuffer(BufferId id) {
221   showBuffer(Client::buffer(id));
222 }
223
224 void MainWin::showBuffer(Buffer *b) {
225   currentBuffer = b->bufferId().groupId();
226   //emit bufferSelected(b);
227   //qApp->processEvents();
228       
229   ui.bufferWidget->setBuffer(b);
230   //emit bufferSelected(b);
231 }
232
233 void MainWin::importBacklog() {
234   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
235                                 "<b>This will permanently delete the contents of your database!</b>",
236                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
237     emit importOldBacklog();
238   }
239 }