basic functionality of error handling ready
[situare] / src / ui / userinfo.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jukka Saastamoinen - jukka.saastamoinen@ixonos.com
6        Jussi Laitinen - jussi.laitinen@ixonos.com
7        Katri Kaikkonen - katri.kaikkonen@ixonos.com
8        Henri Lampela - henri.lampela@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 "common.h"
26 #include "imagebutton.h"
27 #include "../user/user.h"
28 #include "userinfo.h"
29
30 const int BACKGROUND_WIDTH = 240; ///< Width for item
31 const int BACKGROUND_TOP_HEIGHT = 16; ///< Height for item top
32 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
33 //const QColor COLOR_GRAY = QColor(152, 152, 152); ///< Gray color
34 //const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal); ///< Normal font
35 //const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);  ///< Small font
36 const int ICON_HEIGHT = 24; ///< Icon height
37 const int ICON_WIDTH = 24;  ///< Icon width
38 //const int IMAGE_HEIGHT = 60;    ///< Profile image height
39 //const int IMAGE_WIDTH = 60;     ///< Profile image width
40 const int MARGIN = 5; ///< Icon margin
41 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
42 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
43 const QString USER_UNSEND_MESSAGE = "User_unsend_message_content";
44 const QString USER_UNSEND_MESSAGE_PUBLISH_POLICITY = "User_unsend_message_publish";
45
46 /**
47 * @var LABEL_MAX_WIDTH
48 *
49 * @brief All label's maximum width
50 */
51 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
52
53 UserInfo::UserInfo(QWidget *parent)
54     : QWidget(parent),
55       m_expanded(false)
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
60     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
61     verticalLayout->setSpacing(0);
62     setLayout(verticalLayout);
63
64     QFormLayout *infoLayout = new QFormLayout();
65     infoLayout->setMargin(0);
66     infoLayout->setSpacing(0);
67
68     QHBoxLayout *buttonLayout = new QHBoxLayout();
69     buttonLayout->setMargin(0);
70     buttonLayout->setSpacing(0);
71
72     QLabel *envelopeLabel = new QLabel();
73     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
74     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
75     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76     QLabel *compassLabel = new QLabel();
77     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
78     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
79     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
80     QLabel *clockLabel = new QLabel();
81     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
82     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
83     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
84
85     m_findButton = new ImageButton();
86
87     m_nameLabel = new QLabel();
88     m_nameLabel->setWordWrap(true);
89
90     m_statusTextLabel = new QLabel();
91     m_statusTextLabel->setWordWrap(true);
92     m_locationLabel = new QLabel();
93     m_locationLabel->setWordWrap(true);
94     m_updatedLabel = new QLabel();
95     m_updatedLabel->setWordWrap(true);
96
97     m_messageDialog = new UpdateLocationDialog(this);
98     m_messageDialog->hide();
99     m_messageDialog->setWindowModality(Qt::NonModal);
100
101     ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
102                                                              ":/res/images/refresh_s.png");
103     ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
104                                                                    ":/res/images/send_position_s.png");
105
106     buttonLayout->addWidget(updateFriendsButton);
107     buttonLayout->addWidget(updateStatusMessageButton);
108
109     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
110     infoLayout->addRow(compassLabel, m_locationLabel);
111     infoLayout->addRow(clockLabel, m_updatedLabel);
112
113     verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
114     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
115     verticalLayout->addLayout(infoLayout);
116     verticalLayout->addLayout(buttonLayout);
117
118     connect(updateStatusMessageButton,SIGNAL(clicked()),
119             this,SLOT(messageUpdate()));
120
121     connect(updateFriendsButton,SIGNAL(clicked()),
122             this, SIGNAL(refreshUserData()));
123
124     connect(m_findButton, SIGNAL(clicked()),
125         this, SLOT(findButtonClicked()));
126
127     connect(this, SIGNAL(reverseGeoReady(QString)),
128             m_messageDialog, SLOT(setAddress(QString)));
129
130     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
131             this, SIGNAL(statusUpdate(QString,bool)));
132
133     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
134             this, SLOT(backupUpdateLocationDialogData(QString,bool)));
135
136     connect(this, SIGNAL(messageUpdatedToSituare()),
137             this, SLOT(clearUpdateLocationDialogData()));
138
139     setFixedWidth(BACKGROUND_WIDTH);
140
141     this->setFont(NOKIA_FONT_SMALL);
142     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
143     QPalette itemPalette = palette();
144     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
145     setPalette(itemPalette);
146     QPalette namePalette = m_nameLabel->palette();
147     namePalette.setColor(QPalette::Foreground, Qt::white);
148     m_nameLabel->setPalette(namePalette);
149
150     m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
151     m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
152     m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
153
154     restoreUnsendMessage();
155 }
156
157 UserInfo::~UserInfo()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     if (!m_backupMessage.isEmpty()) {
162         QSettings settings(DIRECTORY_NAME, FILE_NAME);
163         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
164         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, m_backupFacebookPublishPolicity);
165     }
166
167 }
168
169 void UserInfo::backupUpdateLocationDialogData(const QString &status, const bool &publish)
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     m_backupMessage = status;
174     m_backupFacebookPublishPolicity = publish;
175
176     m_messageUpdateVerified = false;
177 }
178
179 void UserInfo::clearUpdateLocationDialogData()
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     qDebug() << "Message Before Clearing: " << m_backupMessage;
184     m_backupMessage.clear();
185     qDebug() << "Message After Clearing: " << m_backupMessage;
186     m_backupFacebookPublishPolicity = false;
187 }
188
189 void UserInfo::restoreUnsendMessage()
190 {
191     QSettings settings(DIRECTORY_NAME, FILE_NAME);
192     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, "").toString();
193     m_backupFacebookPublishPolicity =
194             settings.value(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, false).toBool();
195
196 }
197
198 void UserInfo::setAddress(const QString &address)
199 {
200     qDebug() << __PRETTY_FUNCTION__;
201
202     m_locationLabel->setText(address);
203 }
204
205 void UserInfo::setCoordinates(const QPointF &coordinates)
206 {
207     qDebug() << __PRETTY_FUNCTION__;
208
209     m_coordinates = coordinates;
210 }
211
212 void UserInfo::setMessageText(const QString &text)
213 {
214     qDebug() << __PRETTY_FUNCTION__;
215
216     m_messageText = text;
217     setText(false);
218 }
219
220 void UserInfo::setProfileImage(const QPixmap &image)
221 {
222     qDebug() << __PRETTY_FUNCTION__;
223
224     m_findButton->setButtonIcon(image);
225 }
226
227 void UserInfo::setTime(const QString &time)
228 {
229     qDebug() << __PRETTY_FUNCTION__;
230
231     m_updatedLabel->setText(time);
232 }
233
234 void UserInfo::setUserName(const QString &name)
235 {
236     qDebug() << __PRETTY_FUNCTION__;
237
238     m_userName = name;
239     setText(false);
240 }
241
242 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
243 {
244     qDebug() << __PRETTY_FUNCTION__;
245
246     QFontMetrics labelMetrics = label->fontMetrics();
247
248     QString textParam = text;
249     int index = textParam.indexOf('\n');
250
251     if (index > 0) {
252         textParam.truncate(index);
253         textParam.append("...");
254     }
255
256    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
257 }
258
259 void UserInfo::setText(bool expanded)
260 {
261     qDebug() << __PRETTY_FUNCTION__;
262
263     if (expanded) {
264         m_nameLabel->setText(m_userName);
265         m_statusTextLabel->setText(m_messageText);
266     }
267     else {
268         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
269         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
270                                                LABEL_MAX_WIDTH));
271     }
272 }
273
274 void UserInfo::verifyMessageUpdateToServer(QString errorMessage)
275 {
276     qDebug() << __PRETTY_FUNCTION__;
277
278     Q_UNUSED(errorMessage);
279
280     if (!m_messageUpdateVerified) {
281         QString message = QObject::tr("Status update failed\n\nYour message is saved to textbox "
282                                       "until sending will succeed");
283
284         qDebug() << "m_messageText: " << m_messageText;
285         qDebug() << "m_backupMessage: " << m_backupMessage;
286
287         if (m_messageText != m_backupMessage && !m_backupMessage.isEmpty())
288             emit notificateUpdateFailing(message);
289     }
290
291     m_messageUpdateVerified = true;
292 }
293
294 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
295 {
296     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
297
298     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
299         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
300         if (m_expanded) {
301             setText(false);
302             m_expanded = false;
303         }
304         else {
305             setText(true);
306             m_expanded = true;
307         }
308     }
309 }
310
311 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
312 {
313     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
314
315     QPainter painter(this);
316
317     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
318     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
319                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
320     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
321                              BACKGROUND_BOTTOM_HEIGHT);
322
323     painter.drawPixmap(topRect, m_backgroundTopImage);
324     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
325     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
326 }
327
328 void UserInfo::mousePressEvent(QMouseEvent *event)
329 {
330     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
331
332     m_mousePosition = event->pos();
333 }
334
335 void UserInfo::findButtonClicked()
336 {
337     qDebug() << __PRETTY_FUNCTION__;
338
339     emit findUser(m_coordinates);
340 }
341
342 void UserInfo::messageUpdate()
343 {
344     qDebug() << __PRETTY_FUNCTION__;
345
346     emit requestReverseGeo();
347
348     m_messageDialog->show();
349     m_messageDialog->m_textEdit->setPlainText(m_backupMessage);
350     m_messageDialog->m_checkBox->setCheckState(Qt::Unchecked);
351
352     if (m_backupFacebookPublishPolicity == true)
353         m_messageDialog->m_checkBox->setCheckState(Qt::Checked);   
354
355     m_messageDialog->exec();
356 }