5b1961434430f57d6b9a9a2a01862ff73fa8ad4d
[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("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   connect(this, SIGNAL(importOldBacklog()), ClientProxy::instance(), SLOT(gsImportBacklog()));
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.actionDisconnectCore->setEnabled(true);
153   ui.actionNetworkList->setEnabled(true);
154   ui.bufferWidget->show();
155 }
156
157 void MainWin::disconnectedFromCore() {
158   ui.menuViews->setEnabled(false);
159   ui.menuCore->setEnabled(false);
160   ui.actionDisconnectCore->setEnabled(false);
161   ui.actionNetworkList->setEnabled(false);
162   ui.bufferWidget->hide();
163   ui.actionConnectCore->setEnabled(true);
164   //qDebug() << "mainwin disconnected";
165 }
166
167 AbstractUiMsg *MainWin::layoutMsg(const Message &msg) {
168   return new ChatLine(msg);
169 }
170
171 void MainWin::showCoreConnectionDlg(bool autoConnect) {
172   coreConnectDlg = new CoreConnectDlg(this, autoConnect);
173   connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
174   coreConnectDlg->setModal(true);
175   if(!autoConnect || !coreConnectDlg->willDoInternalAutoConnect())
176     coreConnectDlg->show(); // avoid flicker and show dlg only if we do remote connect, which needs a progress bar
177   if(autoConnect) coreConnectDlg->doAutoConnect();
178 }
179
180 void MainWin::coreConnectionDlgFinished(int /*code*/) {
181
182   coreConnectDlg->close();
183 }
184
185
186 void MainWin::showServerList() {
187 //  if(!serverListDlg) {
188 //    serverListDlg = new ServerListDlg(this);
189 //  }
190   serverListDlg->show();
191   serverListDlg->raise();
192 }
193
194 void MainWin::showSettingsDlg() {
195   settingsDlg->show();
196 }
197
198 void MainWin::closeEvent(QCloseEvent *event)
199 {
200   //if (userReallyWantsToQuit()) {
201     ui.bufferWidget->saveState();
202     QSettings s;
203     s.beginGroup("Geometry");
204     s.setValue("MainWinSize", size());
205     s.setValue("MainWinPos", pos());
206     s.setValue("MainWinState", saveState());
207     s.endGroup();
208     s.beginGroup("Buffers");
209     //s.setValue("CurrentNetwork", currentNetwork);
210     s.setValue("CurrentBuffer", currentBuffer);
211     s.endGroup();
212     delete systray;
213     event->accept();
214   //} else {
215     //event->ignore();
216   //}
217 }
218
219 void MainWin::showBuffer(BufferId id) {
220   showBuffer(Client::buffer(id));
221 }
222
223 void MainWin::showBuffer(Buffer *b) {
224   currentBuffer = b->bufferId().groupId();
225   //emit bufferSelected(b);
226   //qApp->processEvents();
227       
228   ui.bufferWidget->setBuffer(b);
229   //emit bufferSelected(b);
230 }
231
232 void MainWin::importBacklog() {
233   if(QMessageBox::warning(this, "Import old backlog?", "Do you want to import your old file-based backlog into new the backlog database?<br>"
234                                 "<b>This will permanently delete the contents of your database!</b>",
235                                 QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) {
236     emit importOldBacklog();
237   }
238 }