Wupps, fixed an oooopsie with the migration code :)
[quassel.git] / src / qtui / nicklistwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
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) version 3.                                           *
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 "nicklistwidget.h"
22
23 #include "nickview.h"
24 #include "client.h"
25 #include "networkmodel.h"
26 #include "buffermodel.h"
27 #include "nickviewfilter.h"
28 #include "qtuisettings.h"
29
30 #include <QAction>
31 #include <QDebug>
32 #include <QEvent>
33 #include <QAbstractButton>
34
35 NickListWidget::NickListWidget(QWidget *parent)
36   : AbstractItemView(parent)
37 {
38   ui.setupUi(this);
39 }
40
41 QDockWidget *NickListWidget::dock() const {
42   QDockWidget *dock = qobject_cast<QDockWidget *>(parent());
43   if(dock)
44     return dock;
45   else
46     return 0;
47 }
48
49 void NickListWidget::showWidget(bool visible) {
50   if(!selectionModel())
51     return;
52
53   QModelIndex currentIndex = selectionModel()->currentIndex();
54   if(currentIndex.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
55     QDockWidget *dock_ = dock();
56     if(!dock_)
57       return;
58
59     if(visible)
60       dock_->show();
61     else
62       dock_->close();
63   }
64 }
65
66 void NickListWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
67   BufferInfo::Type bufferType = (BufferInfo::Type)current.data(NetworkModel::BufferTypeRole).toInt();
68   BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
69   BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
70
71   if(bufferType != BufferInfo::ChannelBuffer) {
72     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
73     return;
74   }
75
76   // See NickListDock::NickListDock() below
77 //   if(bufferType != BufferInfo::ChannelBuffer) {
78 //     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
79 //     QDockWidget *dock_ = dock();
80 //     if(dock_) {
81 //       dock_->close();
82 //     }
83 //     return;
84 //   } else {
85 //     QDockWidget *dock_ = dock();
86 //     if(dock_ && dock_->toggleViewAction()->isChecked()) {
87 //       dock_->show();
88 //     }
89 //   }
90
91   if(newBufferId == oldBufferId)
92     return;
93
94   if(nickViews.contains(newBufferId)) {
95     ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
96   } else {
97     NickView *view = new NickView(this);
98     NickViewFilter *filter = new NickViewFilter(newBufferId, Client::networkModel());
99     view->setModel(filter);
100     QModelIndex source_current = Client::bufferModel()->mapToSource(current);
101     view->setRootIndex(filter->mapFromSource(source_current));
102     nickViews[newBufferId] = view;
103     ui.stackedWidget->addWidget(view);
104     ui.stackedWidget->setCurrentWidget(view);
105   }
106 }
107
108 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
109   Q_ASSERT(model());
110   if(!parent.isValid()) {
111     // ok this means that whole networks are about to be removed
112     // we can't determine which buffers are affect, so we hope that all nets are removed
113     // this is the most common case (for example disconnecting from the core or terminating the clint)
114     NickView *nickView;
115     QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
116     while(iter != nickViews.end()) {
117       nickView = *iter;
118       iter = nickViews.erase(iter);
119       ui.stackedWidget->removeWidget(nickView);
120       QAbstractItemModel *model = nickView->model();
121       nickView->setModel(0);
122       if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
123         filter->setSourceModel(0);
124       model->deleteLater();
125       nickView->deleteLater();
126     }
127   } else {
128     // check if there are explicitly buffers removed
129     for(int i = start; i <= end; i++) {
130       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
131       if(!variant.isValid())
132         continue;
133
134       BufferId bufferId = qVariantValue<BufferId>(variant);
135       removeBuffer(bufferId);
136     }
137   }
138 }
139
140 void NickListWidget::removeBuffer(BufferId bufferId) {
141   if(!nickViews.contains(bufferId))
142     return;
143
144   NickView *view = nickViews.take(bufferId);
145   ui.stackedWidget->removeWidget(view);
146   QAbstractItemModel *model = view->model();
147   view->setModel(0);
148   if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
149     filter->setSourceModel(0);
150   model->deleteLater();
151   view->deleteLater();
152 }
153
154 QSize NickListWidget::sizeHint() const {
155   QWidget *currentWidget = ui.stackedWidget->currentWidget();
156   if(!currentWidget || currentWidget == ui.emptyPage)
157     return QSize(100, height());
158   else
159     return currentWidget->sizeHint();
160 }
161
162
163 // ==============================
164 //  NickList Dock
165 // ==============================
166 NickListDock::NickListDock(const QString &title, QWidget *parent)
167   : QDockWidget(title, parent)
168 {
169   // THIS STUFF IS NEEDED FOR NICKLIST AUTOHIDE... 
170   // AS THIS BRINGS LOTS OF FUCKUPS WITH IT IT'S DEACTIVATED FOR NOW...
171   
172 //   QAction *toggleView = toggleViewAction();
173 //   disconnect(toggleView, SIGNAL(triggered(bool)), this, 0);
174 //   toggleView->setChecked(QtUiSettings().value("ShowNickList", QVariant(true)).toBool());
175
176 //   // reconnecting the closebuttons clicked signal to the action
177 //   foreach(QAbstractButton *button, findChildren<QAbstractButton *>()) {
178 //     if(disconnect(button, SIGNAL(clicked()), this, SLOT(close())))
179 //       connect(button, SIGNAL(clicked()), toggleView, SLOT(trigger()));
180 //   }
181 }
182
183 // NickListDock::~NickListDock() {
184 //   QtUiSettings().setValue("ShowNickList", toggleViewAction()->isChecked());
185 // }
186
187 // bool NickListDock::event(QEvent *event) {
188 //   switch (event->type()) {
189 //   case QEvent::Hide:
190 //   case QEvent::Show:
191 //     emit visibilityChanged(event->type() == QEvent::Show);
192 //     return QWidget::event(event);
193 //   default:
194 //     return QDockWidget::event(event);
195 //   }
196 // }