24
by Martin Klapetek
Rehaul of the details view |
1 |
/*
|
34.1.13
by Martin Klapetek
License header fixes |
2 |
Copyright (C) 2013 Martin Klapetek <mklapetek@kde.org>
|
24
by Martin Klapetek
Rehaul of the details view |
3 |
|
4 |
This library is free software; you can redistribute it and/or
|
|
5 |
modify it under the terms of the GNU Lesser General Public
|
|
6 |
License as published by the Free Software Foundation; either
|
|
7 |
version 2.1 of the License, or (at your option) any later version.
|
|
8 |
||
9 |
This library is distributed in the hope that it will be useful,
|
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 |
Lesser General Public License for more details.
|
|
13 |
||
14 |
You should have received a copy of the GNU Lesser General Public
|
|
15 |
License along with this library; if not, write to the Free Software
|
|
16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 |
*/
|
|
18 |
||
19 |
||
20 |
#include "facebook-connector.h" |
|
21 |
#include <libkfbapi/postslistjob.h> |
|
22 |
||
23 |
#include <KDebug> |
|
24 |
#include <KPixmapSequenceWidget> |
|
25 |
#include <KPixmapSequence> |
|
26 |
#include <KLocalizedString> |
|
27 |
#include <KLocale> |
|
28 |
#include <KIcon> |
|
29 |
#include <KStandardDirs> |
|
30 |
#include <KConfigGroup> |
|
34.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
31 |
#include <KLocalizedString> |
24
by Martin Klapetek
Rehaul of the details view |
32 |
|
33 |
#include <QVBoxLayout> |
|
34 |
#include <QGridLayout> |
|
35 |
#include <QLabel> |
|
36 |
#include <QDir> |
|
37 |
||
34.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
38 |
#include <kpeople/persondata.h> |
39 |
||
24
by Martin Klapetek
Rehaul of the details view |
40 |
FacebookConnector::FacebookConnector(QWidget *parent) |
34.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
41 |
: AbstractPersonDetailsWidget(parent) |
24
by Martin Klapetek
Rehaul of the details view |
42 |
{
|
34.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
43 |
setTitle(i18n("Facebook")); |
34.1.7
by Martin Klapetek
Fix Facebook widget |
44 |
setIcon(KIcon("facebookresource")); |
34.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
45 |
|
29
by David Edmundson
Fix crashes when there is no Facebook access token |
46 |
QDir d(KGlobal::dirs()->saveLocation("config", QString(), false)); |
47 |
||
48 |
QStringList akonadiFacebookConfigFileNames = d.entryList(QStringList() << "akonadi_facebook_resource*", QDir::Files); |
|
49 |
if (! akonadiFacebookConfigFileNames.isEmpty()) { |
|
50 |
KConfig akonadiFacebookConfig(d.entryList(QStringList() << "akonadi_facebook_resource*", QDir::Files).last()); |
|
51 |
KConfigGroup afGroup = akonadiFacebookConfig.group("Authentication"); |
|
52 |
||
53 |
m_accessToken = afGroup.readEntry("AccessToken"); |
|
54 |
}
|
|
34.1.7
by Martin Klapetek
Fix Facebook widget |
55 |
|
56 |
QVBoxLayout *layout = new QVBoxLayout(this); |
|
57 |
layout->setContentsMargins(0,0,0,0); |
|
58 |
setLayout(layout); |
|
24
by Martin Klapetek
Rehaul of the details view |
59 |
|
60 |
QFont f; |
|
61 |
f.setPixelSize(16); |
|
62 |
m_lastPostTitle = new QLabel(this); |
|
63 |
m_lastPostTitle->setFont(f); |
|
64 |
||
65 |
m_post = new QLabel(this); |
|
66 |
m_post->setWordWrap(true); |
|
67 |
m_post->hide(); |
|
68 |
||
69 |
m_busyWidget = new KPixmapSequenceWidget(this); |
|
70 |
//apparently KPixmapSequence has only few sizes, 22 is one of them
|
|
71 |
m_busyWidget->setSequence(KPixmapSequence("process-working", 22)); |
|
72 |
m_busyWidget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
|
73 |
m_busyWidget->hide(); |
|
74 |
||
75 |
m_profileLink = new QLabel(this); |
|
76 |
m_profileLink->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByMouse); |
|
77 |
||
34.1.7
by Martin Klapetek
Fix Facebook widget |
78 |
layout->addWidget(m_lastPostTitle); |
79 |
layout->addWidget(m_busyWidget); |
|
80 |
layout->addWidget(m_post); |
|
81 |
layout->addWidget(m_profileLink); |
|
24
by Martin Klapetek
Rehaul of the details view |
82 |
}
|
83 |
||
37
by Martin Klapetek
Fix build |
84 |
void FacebookConnector::setPerson(PersonData *person) |
30.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
85 |
{
|
86 |
QString userId; |
|
87 |
||
88 |
userId = person->contactId(); |
|
89 |
||
90 |
//also search IM accounts for anything Facebook related
|
|
91 |
QStringList imAccounts = person->imAccounts(); |
|
38
by Martin Klapetek
Extract Facebook ID from Telepathy contacts |
92 |
for (int i = 0; i < imAccounts.size(); i += 3) { |
30.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
93 |
if (imAccounts[i] == "facebook") { |
38
by Martin Klapetek
Extract Facebook ID from Telepathy contacts |
94 |
//the facebook id from telepathy looks like this
|
95 |
// -1581495501@chat.facebook.com
|
|
96 |
//so we need to remove the "-" at the beginning
|
|
97 |
//and everything after "@", which is 19 chars
|
|
98 |
userId = imAccounts[i+2].mid(1, imAccounts[i+2].length() - 19); |
|
30.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
99 |
}
|
100 |
}
|
|
101 |
||
102 |
if (!userId.isEmpty()) { |
|
103 |
setActive(true); |
|
104 |
setUserId(userId); |
|
105 |
} else { |
|
30.1.5
by David Edmundson
Fix Facebook widget |
106 |
setActive(false); |
30.1.4
by David Edmundson
Port FacebookConnector to AbstractPersonDetailsWidget |
107 |
}
|
108 |
}
|
|
109 |
||
24
by Martin Klapetek
Rehaul of the details view |
110 |
void FacebookConnector::setUserId(const QString &userId) |
111 |
{
|
|
29
by David Edmundson
Fix crashes when there is no Facebook access token |
112 |
if (m_accessToken.isEmpty()) { |
113 |
return; |
|
114 |
}
|
|
24
by Martin Klapetek
Rehaul of the details view |
115 |
if (userId == m_userId) { |
116 |
return; |
|
117 |
}
|
|
118 |
||
119 |
m_userId = userId; |
|
120 |
||
121 |
if (!m_runningJob.isNull()) { |
|
122 |
m_runningJob->kill(); |
|
123 |
}
|
|
124 |
||
125 |
m_busyWidget->show(); |
|
126 |
m_post->show(); |
|
127 |
m_post->setText(i18n("Fetching last Facebook post...")); |
|
128 |
m_profileLink->setText("<a href=\"https://facebook.com/" + userId + "\">Open Facebook profile...</a>"); |
|
129 |
||
130 |
KFbAPI::PostsListJob *job = new KFbAPI::PostsListJob(userId, m_accessToken, this); |
|
131 |
job->setProperty("userId", userId); |
|
132 |
connect(job, SIGNAL(result(KJob*)), this, SLOT(lastPostFetched(KJob*))); |
|
133 |
job->start(); |
|
134 |
||
135 |
m_runningJob = job; |
|
136 |
}
|
|
137 |
||
138 |
void FacebookConnector::lastPostFetched(KJob *job) |
|
139 |
{
|
|
140 |
m_busyWidget->hide(); |
|
141 |
KFbAPI::PostsListJob *postsJob = qobject_cast<KFbAPI::PostsListJob*>(job); |
|
142 |
||
32
by Martin Klapetek
Check the facebook job for errors |
143 |
if (postsJob->error() == KFbAPI::FacebookJob::AuthenticationProblem) { |
144 |
m_lastPostTitle->setText(i18n("Authentication problem")); |
|
145 |
m_post->setText(i18n("Cannot authenticate with your access token")); |
|
146 |
return; |
|
33
by Martin Klapetek
Handle other facebook errors as well |
147 |
} else if (postsJob->error() == KJob::UserDefinedError) { |
148 |
m_lastPostTitle->setText(i18n("There was an error")); |
|
149 |
m_post->setText(postsJob->errorText()); |
|
32
by Martin Klapetek
Check the facebook job for errors |
150 |
}
|
151 |
||
24
by Martin Klapetek
Rehaul of the details view |
152 |
QString userId = postsJob->property("userId").toString(); |
153 |
QString postMessage; |
|
154 |
QString postDate; |
|
155 |
||
156 |
Q_FOREACH(KFbAPI::PostInfo post, postsJob->posts()) { |
|
157 |
if (!post.message().isEmpty()) { |
|
158 |
postMessage = post.message(); |
|
159 |
postDate = KGlobal::locale()->formatDateTime(post.createdTime(), KLocale::FancyLongDate); |
|
160 |
break; |
|
161 |
}
|
|
162 |
}
|
|
163 |
||
164 |
kDebug() << postMessage << postDate; |
|
165 |
||
166 |
if (postMessage.isEmpty()) { |
|
167 |
postMessage = QString("(empty)"); |
|
168 |
}
|
|
169 |
m_post->setText(postMessage); |
|
170 |
m_lastPostTitle->setText(QString("Last Post (from %1)").arg(postDate)); |
|
171 |
}
|
|
172 |
||
173 |
void FacebookConnector::clear() |
|
174 |
{
|
|
175 |
m_userId = QString(); |
|
176 |
m_busyWidget->hide(); |
|
177 |
m_post->hide(); |
|
178 |
}
|