1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/* This file is part of the KDE libraries
Copyright (C) 2010 Teo Mrnjavac <teo@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kaboutapplicationpersonmodel_p.h"
#ifdef HAVE_ATTICA
#include <attica/person.h>
#endif //HAVE_ATTICA
#include <kdebug.h>
#include <kaboutdata.h>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
namespace KDEPrivate
{
KAboutApplicationPersonModel::KAboutApplicationPersonModel( const QList< KAboutPerson > &personList,
const QString &providerUrl,
QObject *parent )
: QAbstractListModel( parent )
, m_personList( personList )
, m_hasAvatarPixmaps( false )
, m_providerUrl( providerUrl )
{
if( m_providerUrl.isEmpty() )
m_providerUrl = QString( "https://api.opendesktop.org/v1/" );
bool hasOcsUsernames = false;
for( QList< KAboutPerson >::const_iterator it = personList.begin(); it != personList.end(); ++it )
{
KAboutPerson person = *it;
if( !person.ocsUsername().isEmpty() )
hasOcsUsernames = true;
KAboutApplicationPersonProfile profile =
KAboutApplicationPersonProfile( person.name(),
person.task(),
person.emailAddress(),
person.ocsUsername() );
profile.setHomepage( person.webAddress() );
m_profileList.append( profile );
}
m_ocsLinkIcons.insert( KAboutApplicationPersonProfileOcsLink::Other, KIcon( "applications-internet").pixmap( 16 ) );
m_ocsLinkIcons.insert( KAboutApplicationPersonProfileOcsLink::Blog, KIcon( "applications-internet" ).pixmap( 16 ) );
m_ocsLinkIcons.insert( KAboutApplicationPersonProfileOcsLink::Homepage, KIcon( "applications-internet" ).pixmap( 16 ) );
#ifdef HAVE_ATTICA
connect( &m_providerManager, SIGNAL(defaultProvidersLoaded()),
SLOT(onProvidersLoaded()) );
if( hasOcsUsernames )
m_providerManager.loadDefaultProviders();
#endif //HAVE_ATTICA
}
int KAboutApplicationPersonModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return m_personList.count();
}
QVariant KAboutApplicationPersonModel::data( const QModelIndex &index, int role ) const
{
if( !index.isValid() ) {
kWarning()<<"ERROR: invalid index";
return QVariant();
}
if( index.row() >= rowCount() ) {
kWarning()<<"ERROR: index out of bounds";
return QVariant();
}
if( role == Qt::DisplayRole ) {
// kDebug() << "Spitting data for name " << m_profileList.at( index.row() ).name();
QVariant var;
var.setValue( m_profileList.at( index.row() ) );
return var;
}
else {
return QVariant();
}
}
Qt::ItemFlags KAboutApplicationPersonModel::flags( const QModelIndex &index ) const
{
if( index.isValid() )
return Qt::ItemIsEnabled;
return QAbstractListModel::flags( index ) | Qt::ItemIsEditable;
}
void KAboutApplicationPersonModel::onProvidersLoaded() //SLOT
{
#ifdef HAVE_ATTICA
if( !m_providerManager.providers().isEmpty() ) {
m_provider = m_providerManager.providerByUrl( QUrl( m_providerUrl ) );
if( !m_provider.isValid() ) {
kDebug() << "OCS Provider error: could not find opendesktop.org provider.";
return;
}
m_providerName = m_provider.name();
int i = 0;
for( QList< KAboutApplicationPersonProfile >::const_iterator it = m_profileList.constBegin();
it != m_profileList.constEnd(); ++it ) {
KAboutApplicationPersonProfile profile = *it;
if( !profile.ocsUsername().isEmpty() ) {
Attica::ItemJob< Attica::Person > *job = m_provider.requestPerson( profile.ocsUsername() );
connect( job, SIGNAL(finished(Attica::BaseJob*)),
this, SLOT(onPersonJobFinished(Attica::BaseJob*)) );
job->setProperty( "personProfile", i );
job->start();
}
++i;
}
}
#endif //HAVE_ATTICA
}
void KAboutApplicationPersonModel::onPersonJobFinished( Attica::BaseJob *job ) //SLOT
{
#ifndef HAVE_ATTICA
Q_UNUSED( job )
#endif //HAVE_ATTICA
#ifdef HAVE_ATTICA
Attica::ItemJob< Attica::Person > *personJob =
static_cast< Attica::ItemJob< Attica::Person > * >( job );
if( personJob->metadata().error() == Attica::Metadata::NoError ) {
Attica::Person p = personJob->result();
int personProfileListIndex = personJob->property( "personProfile" ).toInt();
KAboutApplicationPersonProfile profile = m_profileList.value( personProfileListIndex );
//Let's set up OCS links...
QList< KAboutApplicationPersonProfileOcsLink > ocsLinks;
for( int i = 2; i <= 10; ++i ) { //OCS supports 10 total homepages as of 2/oct/2009
//This starts at 2 because the first homepage is
//just "homepage" in the OCS API and is exposed by
//Attica simply through Person::homepage()
QString atticaType = p.extendedAttribute( QString( "homepagetype%1" ).arg( i ) );
QString url = p.extendedAttribute( QString( "homepage%1" ).arg( i ) );
if( url.isEmpty() )
continue;
KAboutApplicationPersonProfileOcsLink::Type type =
KAboutApplicationPersonProfileOcsLink::typeFromAttica( atticaType );
ocsLinks.append( KAboutApplicationPersonProfileOcsLink( type, KUrl( url ) ) );
if( !m_ocsLinkIcons.contains( type ) && !m_ocsLinkIconUrls.contains( type ) ) {
m_ocsLinkIconUrls.insert( type, p.extendedAttribute( QString( "homepageicon%1" ).arg( i ) ) );
}
}
if( profile.homepage().isEmpty() ) {
if( !p.homepage().isEmpty() )
profile.setHomepage( p.homepage() );
else {
if( !ocsLinks.isEmpty() ) {
QList< KAboutApplicationPersonProfileOcsLink >::iterator toUse = ocsLinks.begin();
for( QList< KAboutApplicationPersonProfileOcsLink >::iterator it = ocsLinks.begin();
it != ocsLinks.end(); ++it ) {
KAboutApplicationPersonProfileOcsLink link = *it;
if( link.type() == KAboutApplicationPersonProfileOcsLink::Blog ||
link.type() == KAboutApplicationPersonProfileOcsLink::Homepage ||
link.type() == KAboutApplicationPersonProfileOcsLink::Other ) {
toUse = it;
break;
}
}
profile.setHomepage( toUse->url().url() );
ocsLinks.erase( toUse );
}
}
}
else
{
KAboutApplicationPersonProfileOcsLink::Type type =
KAboutApplicationPersonProfileOcsLink::typeFromAttica( p.extendedAttribute( "homepagetype" ) );
ocsLinks.insert( 0, KAboutApplicationPersonProfileOcsLink( type, KUrl( p.homepage() ) ) ); //we prepend the main homepage
if( !m_ocsLinkIcons.contains( type ) && !m_ocsLinkIconUrls.contains( type ) ) {
m_ocsLinkIconUrls.insert( type, p.extendedAttribute( "homepageicon" ) );
}
}
profile.setOcsLinks( ocsLinks );
if( !( p.city().isEmpty() && p.country().isEmpty() ) ) {
if( !p.city().isEmpty() )
profile.setLocation( i18nc( "City, Country", "%1, %2", p.city(), p.country() ) );
else
profile.setLocation( p.country() );
}
profile.setOcsProfileUrl( p.extendedAttribute( "profilepage" ) );
m_profileList.replace( personProfileListIndex, profile );
if( p.avatarUrl().isEmpty() ) {
emit dataChanged( index( personProfileListIndex ), index( personProfileListIndex ) );
fetchOcsLinkIcons( personProfileListIndex );
}
else {
//TODO: Create a PixmapFromUrlJob in Attica which would use KIO::get if available
// and QNAM otherwise. - Teo 30/10/2010
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
connect( manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(onAvatarJobFinished(QNetworkReply*)) );
manager->get( QNetworkRequest( p.avatarUrl() ) );
manager->setProperty( "personProfile", personProfileListIndex );
}
}
else
kDebug() << "Could not fetch OCS person info.";
#endif //HAVE_ATTICA
}
void KAboutApplicationPersonModel::onAvatarJobFinished( QNetworkReply *reply ) //SLOT
{
#ifndef HAVE_ATTICA
Q_UNUSED( reply )
#endif //HAVE_ATTICA
#ifdef HAVE_ATTICA
QNetworkAccessManager *manager = reply->manager();
int personProfileListIndex = manager->property( "personProfile" ).toInt();
if( reply->error() != QNetworkReply::NoError ) {
kDebug() << "Could not fetch OCS person avatar.";
emit dataChanged( index( personProfileListIndex ), index( personProfileListIndex ) );
return;
}
QByteArray data = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData( data );
KAboutApplicationPersonProfile profile = m_profileList.value( personProfileListIndex );
if( !pixmap.isNull() ) {
profile.setAvatar( pixmap );
m_hasAvatarPixmaps = true;
}
m_profileList.replace( personProfileListIndex, profile );
reply->deleteLater();
fetchOcsLinkIcons( personProfileListIndex );
#endif //HAVE_ATTICA
}
void KAboutApplicationPersonModel::fetchOcsLinkIcons( int personProfileListIndex )
{
KAboutApplicationPersonProfile profile = m_profileList.value( personProfileListIndex );
QList< KAboutApplicationPersonProfileOcsLink > ocsLinks = profile.ocsLinks();
KAboutApplicationPersonIconsJob *job =
new KAboutApplicationPersonIconsJob( this, personProfileListIndex );
connect( job, SIGNAL(finished(KAboutApplicationPersonIconsJob*)),
this, SLOT(onOcsLinksJobFinished(KAboutApplicationPersonIconsJob*)) );
job->start();
}
void KAboutApplicationPersonModel::onOcsLinksJobFinished( KAboutApplicationPersonIconsJob *job ) //SLOT
{
int personProfileListIndex = job->personProfileListIndex();
KAboutApplicationPersonProfile profile = m_profileList.value( personProfileListIndex );
profile.setOcsLinks( job->ocsLinks() );
m_profileList.replace( personProfileListIndex, profile );
emit dataChanged( index( personProfileListIndex ), index( personProfileListIndex ) );
emit layoutChanged();
}
KAboutApplicationPersonProfileOcsLink::Type KAboutApplicationPersonProfileOcsLink::typeFromAttica( const QString &atticaType )
{
int index = -1;
for( int i = 0; i < NUM_ATTICA_LINK_TYPES; ++i ) {
if( atticaType == QString( s_personOcsLinkAtticaTypes[i] ) ) {
index = i;
break;
}
}
if( index == -1 )
return Other;
else
return static_cast< Type >( index );
}
QString KAboutApplicationPersonProfileOcsLink::prettyType() const
{
//This can't be static like the other lists because of i18n
switch( m_type ) {
case Other:
return i18nc( "A generic social network or homepage link of an unlisted type.", "Other" );
case Blog:
return i18nc( "A type of link.", "Blog" );
case Delicious:
return "Delicious";
case Digg:
return "Digg";
case Facebook:
return "Facebook";
case Homepage:
return i18nc( "A type of link.", "Homepage" );
case Identica:
return "Identi.ca";
case LibreFm:
return "Libre.fm";
case LinkedIn:
return "LinkedIn";
case MySpace:
return "MySpace";
case Reddit:
return "Reddit";
case StackOverflow:
return "Stack Overflow";
case Twitter:
return "Twitter";
case Wikipedia:
return "Wikipedia";
case Xing:
return "Xing";
case YouTube:
return "YouTube";
case NUM_ATTICA_LINK_TYPES: // silence compiler warning
break;
}
return QString();
}
KAboutApplicationPersonIconsJob::KAboutApplicationPersonIconsJob( KAboutApplicationPersonModel *model,
int personProfileListIndex )
: QObject( model )
, m_personProfileListIndex( personProfileListIndex )
, m_model( model )
{
m_manager = new QNetworkAccessManager( this );
connect( m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(onJobFinished(QNetworkReply*)) );
m_ocsLinks = model->m_profileList.value( personProfileListIndex ).ocsLinks();
}
void KAboutApplicationPersonIconsJob::start()
{
getIcons( 0 );
}
void KAboutApplicationPersonIconsJob::getIcons( int i )
{
for( QList< KAboutApplicationPersonProfileOcsLink >::iterator it = m_ocsLinks.begin() + i;
it != m_ocsLinks.end(); ++it ) {
if( m_model->m_ocsLinkIcons.contains( it->type() ) ) {
it->setIcon( m_model->m_ocsLinkIcons.value( it->type() ) );
}
else if( m_model->m_ocsLinkIconUrls.contains( it->type() ) )
{
QNetworkReply *reply =
m_manager->get( QNetworkRequest( QUrl( m_model->m_ocsLinkIconUrls.value( it->type() ) ) ) );
reply->setProperty( "linkIndex", i );
return;
}
++i;
}
emit finished( this );
}
void KAboutApplicationPersonIconsJob::onJobFinished( QNetworkReply *reply ) //SLOT
{
int i = reply->property( "linkIndex" ).toInt();
KAboutApplicationPersonProfileOcsLink::Type type = m_ocsLinks.at( i ).type();
if( reply->error() != QNetworkReply::NoError ) {
kDebug() << "Could not fetch OCS link icon.";
reply->deleteLater();
getIcons( i + 1 );
return;
}
QByteArray data = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData( data );
if( !pixmap.isNull() && !m_model->m_ocsLinkIcons.contains( type ) ) {
m_model->m_ocsLinkIcons.insert( type, pixmap );
}
reply->deleteLater();
getIcons( i );
}
} //namespace KDEPrivate
#include "kaboutapplicationpersonmodel_p.moc"
|