Added manager null check.
[situare] / src / ui / friendlistpanel.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Henri Lampela - henri.lampela@ixonos.com
7         Pekka Nissinen - pekka.nissinen@ixonos.com
8         Sami Rämö - sami.ramo@ixonos.com
9
10     Situare is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     version 2 as published by the Free Software Foundation.
13
14     Situare is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with Situare; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22     USA.
23 */
24
25 #include <QApplication>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QPushButton>
30
31 #include "coordinates/geocoordinate.h"
32 #include "friendlistitem.h"
33 #include "friendlistitemdelegate.h"
34 #include "friendlistview.h"
35 #include "panelcommon.h"
36 #include "user/user.h"
37
38 #include "friendlistpanel.h"
39
40 FriendListPanel::FriendListPanel(QWidget *parent)
41     : QWidget(parent),
42       m_mainWindowIsTopmost(false),
43       m_somePanelIsOpen(false)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
48     const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
49     const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
50     const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
51
52     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
53     friendListPanelLayout->setMargin(0);
54     friendListPanelLayout->setSpacing(0);
55     setLayout(friendListPanelLayout);
56
57     QHBoxLayout *filterLayout = new QHBoxLayout();
58     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, FRIENDPANEL_FILTER_MARGIN_TOP,
59                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, FRIENDPANEL_FILTER_MARGIN_BOTTOM);
60
61     m_friendListHeaderWidget = new QWidget();
62     m_friendListHeaderWidget->setLayout(filterLayout);
63     m_friendListHeaderWidget->setAutoFillBackground(true);
64
65     m_routeButton = new QPushButton(tr("Route to friend"));
66     m_routeButton->setDisabled(true);
67
68     m_showContactButton = new QPushButton(tr("Show contact"));
69     m_showContactButton->setDisabled(true);
70
71     QPalette labelPalette = m_friendListHeaderWidget->palette();
72     labelPalette.setColor(QPalette::Background, Qt::black);
73
74     m_friendListHeaderWidget->setPalette(labelPalette);
75     m_friendListHeaderWidget->hide();
76     m_friendListLabel = new QLabel(this);
77     m_clearFilterButton = new QPushButton(tr("Show all"));
78
79     filterLayout->addWidget(m_friendListLabel);
80     filterLayout->addWidget(m_clearFilterButton);
81
82     m_friendListView = new FriendListView(this);
83     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
84
85     QVBoxLayout *listViewLayout = new QVBoxLayout;
86     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
87                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
88     listViewLayout->addWidget(m_friendListView);
89
90     friendListPanelLayout->addWidget(m_showContactButton);
91     friendListPanelLayout->addWidget(m_routeButton);
92     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
93     friendListPanelLayout->addLayout(listViewLayout);
94
95     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
96             this, SIGNAL(findFriend(GeoCoordinate)));
97
98     connect(m_clearFilterButton, SIGNAL(clicked()),
99             this, SLOT(clearFiltering()));
100
101     connect(m_routeButton, SIGNAL(clicked()),
102             this, SLOT(routeToSelectedFriend()));
103
104     connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
105             this, SLOT(setSelectionButtonsDisabled()));
106
107     /// @todo remove old filterLayout when new panel are merged
108
109     //////////////////////////////////////////////////////////////////////////
110     // NOTE! Do not mix the new filtering layout below with the old one above
111     //////////////////////////////////////////////////////////////////////////
112
113     // filtering layout
114     QHBoxLayout *filteringLayout = new QHBoxLayout();
115     friendListPanelLayout->addLayout(filteringLayout);
116
117     // line edit for filtering
118     m_filterField = new QLineEdit;
119     filteringLayout->addWidget(m_filterField);
120
121     connect(m_filterField, SIGNAL(returnPressed()),
122             this, SLOT(clearTextFiltering()));
123
124     connect(m_filterField, SIGNAL(textChanged(QString)),
125             this, SLOT(filterTextChanged(QString)));
126
127     // button for clearing the filtering
128     m_filterClearButton = new QPushButton();
129     filteringLayout->addWidget(m_filterClearButton);
130     m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
131
132     connect(m_filterClearButton, SIGNAL(clicked()),
133             this, SLOT(clearTextFiltering()));
134
135     connect(qApp, SIGNAL(topmostWindowChanged(bool)),
136             this, SLOT(topmostWindowChanged(bool)));
137
138     connect(m_showContactButton, SIGNAL(clicked()),
139             this, SLOT(requestSelectedFriendContactDialog()));
140 }
141
142 void FriendListPanel::anyPanelClosed()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     m_somePanelIsOpen = false;
147     updateKeyboardGrabbing();
148
149     clearFiltering();
150
151     m_friendListView->clearItemSelection();
152     setSelectionButtonsDisabled();
153 }
154
155 void FriendListPanel::anyPanelOpened()
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     m_somePanelIsOpen = true;
160     updateKeyboardGrabbing();
161 }
162
163 void FriendListPanel::clearFiltering()
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     m_friendListHeaderWidget->hide();
168     m_friendListView->clearFilter();
169     clearTextFiltering();
170 }
171
172 void FriendListPanel::clearTextFiltering()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     // clearing the filtering text field does cause also hiding the filtering layout
177     m_filterField->clear();
178 }
179
180 void FriendListPanel::filterTextChanged(const QString &text)
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     if (m_filterField->isHidden() && !text.isEmpty())
185         setFilteringLayoutVisibility(true);
186     else if (m_filterField->isVisible() && text.isEmpty())
187         setFilteringLayoutVisibility(false);
188
189     m_friendListView->filter(text);
190 }
191
192 void FriendListPanel::friendImageReady(User *user)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
197
198     if (item)
199         item->setAvatarImage(user->profileImage());
200 }
201
202 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
203 {
204     qDebug() << __PRETTY_FUNCTION__;
205
206     QStringList newUserIDs;
207
208     foreach (User *user, friendList) {
209         FriendListItem *item = 0;
210         if (!m_friendListView->contains(user->userId())) {
211             item = new FriendListItem();
212             item->setUserData(user);
213             m_friendListView->addListItem(user->userId(), item);
214         } else {
215             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
216                     user->userId()));
217
218             if (item) {
219                 item->setUserData(user);
220                 m_friendListView->addListItemToView(item);
221             }
222         }
223
224         newUserIDs.append(user->userId());
225     }
226
227     m_friendListView->clearUnused(newUserIDs);
228 }
229
230 void FriendListPanel::hideEvent(QHideEvent *event)
231 {
232     qDebug() << __PRETTY_FUNCTION__;
233
234     QWidget::hideEvent(event);
235     updateKeyboardGrabbing();
236     clearFiltering();
237
238     m_friendListView->clearItemSelection();
239     setSelectionButtonsDisabled();
240 }
241
242 void FriendListPanel::requestSelectedFriendContactDialog()
243 {
244     qDebug() << __PRETTY_FUNCTION__;
245
246     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
247
248     if (item) {
249         QString facebookId = item->facebookId();
250         if (!facebookId.isEmpty())
251             emit requestContactDialog(facebookId);
252     }
253 }
254
255 void FriendListPanel::routeToSelectedFriend()
256 {
257     qDebug() << __PRETTY_FUNCTION__;
258
259     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
260
261     if (item)
262         emit routeToFriend(item->coordinates());
263 }
264
265 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
266 {
267     qDebug() << __PRETTY_FUNCTION__;
268
269     m_filterField->setVisible(visible);
270     m_filterClearButton->setVisible(visible);
271 }
272
273 void FriendListPanel::setSelectionButtonsDisabled()
274 {
275     qDebug() << __PRETTY_FUNCTION__;
276
277     bool isAnyItemSelected = m_friendListView->selectedItems().isEmpty();
278
279     m_routeButton->setDisabled(isAnyItemSelected);
280     m_showContactButton->setDisabled(isAnyItemSelected);
281 }
282
283 void FriendListPanel::updateKeyboardGrabbing()
284 {
285     qDebug() << __PRETTY_FUNCTION__;
286
287     if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
288         if (QWidget::keyboardGrabber() == m_filterField)
289             m_filterField->releaseKeyboard();
290     } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
291         if (QWidget::keyboardGrabber() != m_filterField)
292             m_filterField->grabKeyboard();
293     }
294 }
295
296 void FriendListPanel::showEvent(QShowEvent *event)
297 {
298     qDebug() << __PRETTY_FUNCTION__;
299
300     QWidget::showEvent(event);
301     updateKeyboardGrabbing();
302     setFilteringLayoutVisibility(false);
303 }
304
305 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
306 {
307     qDebug() << __PRETTY_FUNCTION__;
308
309     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
310
311     m_friendListHeaderWidget->show();
312     m_friendListView->filter(userIDs);
313
314     clearTextFiltering();
315
316     emit showPanelRequested(this);
317 }
318
319 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
320 {
321     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
322
323     m_mainWindowIsTopmost = mainWindowIsTopmost;
324     updateKeyboardGrabbing();
325 }