diff --git a/microblogs/pumpio/pumpiomicroblog.cpp b/microblogs/pumpio/pumpiomicroblog.cpp index 461323a2..7adb75c1 100644 --- a/microblogs/pumpio/pumpiomicroblog.cpp +++ b/microblogs/pumpio/pumpiomicroblog.cpp @@ -1,290 +1,279 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #include "pumpiomicroblog.h" #include #include #include #include #include #include "accountmanager.h" #include "application.h" #include "pumpioaccount.h" #include "pumpioeditaccountwidget.h" #include "pumpiopostwidget.h" K_PLUGIN_FACTORY( MyPluginFactory, registerPlugin < PumpIOMicroBlog > (); ) K_EXPORT_PLUGIN( MyPluginFactory( "choqok_pumpio" ) ) PumpIOMicroBlog::PumpIOMicroBlog(QObject* parent, const QVariantList& args): MicroBlog(MyPluginFactory::componentData(), parent) { setServiceName("Pump.io"); setServiceHomepageUrl("http://pump.io"); QStringList timelineNames; - timelineNames << "Activity" << "Inbox"; + timelineNames << "Activity"; setTimelineNames(timelineNames); setTimelinesInfo(); } PumpIOMicroBlog::~PumpIOMicroBlog() { } ChoqokEditAccountWidget* PumpIOMicroBlog::createEditAccountWidget(Choqok::Account* account, QWidget* parent) { PumpIOAccount *acc = qobject_cast(account); if (acc || !account) { return new PumpIOEditAccountWidget(this, acc, parent); } else { kDebug() << "Account passed here was not a valid PumpIOAccount!"; return 0; } } Choqok::Account* PumpIOMicroBlog::createNewAccount(const QString& alias) { PumpIOAccount *acc = qobject_cast( Choqok::AccountManager::self()->findAccount(alias) ); if (!acc) { return new PumpIOAccount(this, alias); } else { kDebug() << "Cannot create a new PumpIOAccount!"; return 0; } } void PumpIOMicroBlog::setTimelinesInfo() { Choqok::TimelineInfo *t = new Choqok::TimelineInfo; t->name = i18nc("Timeline Name", "Activity"); t->description = i18nc("Timeline description", "You and your friends"); t->icon = "user-home"; m_timelinesInfos["Activity"] = t; m_timelinesPaths["Activity"] = "/api/user/%1/inbox/major"; - - t = new Choqok::TimelineInfo; - t->name = i18nc("Timeline Name", "Inbox"); - t->description = i18nc("Timeline description", "Your incoming private messages"); - t->icon = "mail-folder-inbox"; - m_timelinesInfos["Inbox"] = t; - m_timelinesPaths["Inbox"] = "/api/user/%1/inbox/major"; } void PumpIOMicroBlog::updateTimelines(Choqok::Account* theAccount) { PumpIOAccount *acc = qobject_cast(theAccount); if (acc) { Q_FOREACH (const QString timeline, acc->timelineNames()) { KUrl url(acc->host()); url.addPath(m_timelinesPaths[timeline].arg(acc->username())); KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::Reload, KIO::HideProgressInfo); if (!job) { kDebug() << "Cannot create an http GET request!"; continue; } const QString lastActivityID; QOAuth::ParamMap oAuthParams; oAuthParams.insert("count", "20"); oAuthParams.insert("since", m_timelinesLatestIds[theAccount][timeline].toLocal8Bit()); QByteArray authorization = acc->oAuth()->createParametersString(url.url(), QOAuth::GET, acc->token().toLocal8Bit(), acc->tokenSecret().toLocal8Bit(), QOAuth::HMAC_SHA1, oAuthParams, QOAuth::ParseForHeaderArguments); job->addMetaData("customHTTPHeader", "Authorization: " + authorization); m_timelinesRequests[job] = timeline; m_accountJobs[job] = acc; connect(job, SIGNAL(result(KJob*)), this, SLOT(slotUpdateTimeline(KJob*))); job->start(); } } else { kDebug() << "Error updating timelines"; } } Choqok::TimelineInfo* PumpIOMicroBlog::timelineInfo(const QString& timelineName) { return m_timelinesInfos.value(timelineName); } void PumpIOMicroBlog::slotUpdateTimeline(KJob* job) { Choqok::Account *account = m_accountJobs.take(job); if (job->error()) { kDebug() << "Job Error: " << job->errorString(); emit error(account, Choqok::MicroBlog::CommunicationError, i18n("An error occurred when fetching the timeline")); return; } KIO::StoredTransferJob* j = qobject_cast(job); QList list = readTimeline(account, j->data()); emit timelineDataReceived(account, m_timelinesRequests.take(job), list); } void PumpIOMicroBlog::saveTimeline(Choqok::Account* account, const QString& timelineName, const QList< Choqok::UI::PostWidget* >& timeline) { QString fileName = Choqok::AccountManager::generatePostBackupFileName(account->alias(), timelineName); KConfig postsBackup("choqok/" + fileName, KConfig::NoGlobals, "data"); ///Clear previous data: QStringList prevList = postsBackup.groupList(); int c = prevList.count(); if ( c > 0 ) { for ( int i = 0; i < c; ++i ) { postsBackup.deleteGroup(prevList[i]); } } QList< Choqok::UI::PostWidget *>::const_iterator it, endIt = timeline.constEnd(); for ( it = timeline.constBegin(); it != endIt; ++it ) { const Choqok::Post *post = ((*it)->currentPost()); KConfigGroup grp( &postsBackup, post->creationDateTime.toString() ); grp.writeEntry( "creationDateTime", post->creationDateTime ); grp.writeEntry( "postId", post->postId.toString() ); grp.writeEntry( "link", post->link ); grp.writeEntry( "content", post->content ); grp.writeEntry( "source", post->source ); grp.writeEntry( "favorited", post->isFavorited ); grp.writeEntry( "authorId", post->author.userId.toString() ); grp.writeEntry( "authorRealName", post->author.realName ); grp.writeEntry( "authorUserName", post->author.userName ); grp.writeEntry( "authorLocation" , post->author.location ); grp.writeEntry( "authorDescription" , post->author.description ); grp.writeEntry( "authorProfileImageUrl", post->author.profileImageUrl ); grp.writeEntry( "authorHomePageUrl" , post->author.homePageUrl ); grp.writeEntry( "type" , post->type ); grp.writeEntry( "isRead" , post->isRead ); grp.writeEntry( "conversationId", post->conversationId.toString() ); } postsBackup.sync(); } QList< Choqok::Post* > PumpIOMicroBlog::loadTimeline(Choqok::Account* account, const QString& timelineName) { QList< Choqok::Post* > list; QString fileName = Choqok::AccountManager::generatePostBackupFileName(account->alias(), timelineName); KConfig postsBackup( "choqok/" + fileName, KConfig::NoGlobals, "data" ); QStringList tmpList = postsBackup.groupList(); // don't load old archives if (tmpList.isEmpty() || !(QDateTime::fromString(tmpList.first()).isValid()) ) return list; QList groupList; foreach(const QString &str, tmpList) groupList.append(QDateTime::fromString(str) ); qSort(groupList); int count = groupList.count(); if( count ) { Choqok::Post *st = 0; for ( int i = 0; i < count; ++i ) { st = new Choqok::Post; KConfigGroup grp( &postsBackup, groupList[i].toString() ); st->creationDateTime = grp.readEntry( "creationDateTime", QDateTime::currentDateTime() ); st->postId = grp.readEntry( "postId", QString() ); st->link = grp.readEntry( "link", QString() ); st->content = grp.readEntry( "content", QString() ); st->source = grp.readEntry( "source", QString() ); st->isFavorited = grp.readEntry( "favorited", false ); st->author.userId = grp.readEntry( "authorId", QString() ); st->author.userName = grp.readEntry( "authorUserName", QString() ); st->author.realName = grp.readEntry( "authorRealName", QString() ); st->author.location = grp.readEntry("authorLocation", QString()); st->author.description = grp.readEntry( "authorDescription" , QString() ); st->author.profileImageUrl = grp.readEntry( "authorProfileImageUrl", QString() ); st->author.homePageUrl = grp.readEntry("authorHomePageUrl", QString()); st->type = grp.readEntry("type", QString()); st->isRead = grp.readEntry("isRead", true); st->conversationId = grp.readEntry("conversationId", QString()); list.append( st ); } m_timelinesLatestIds[account][timelineName] = st->postId; } return list; } QList< Choqok::Post* > PumpIOMicroBlog::readTimeline(Choqok::Account* theAccount, const QByteArray& buffer) { QList posts; QJson::Parser parser; QVariantList list = parser.parse(buffer).toMap().take("items").toList(); Q_FOREACH (const QVariant element, list) { - Choqok::Post* post = new Choqok::Post(); - readPost(theAccount, element.toMap(), post); - if (!post->postId.isNull()) { - posts.prepend(post); + if (!element.toMap().take("object").toMap().take("deleted").toString().isEmpty()) { + // Skip deleted posts + continue; } + posts.prepend(readPost(theAccount, element.toMap(), new Choqok::Post)); } return posts; } Choqok::Post* PumpIOMicroBlog::readPost(Choqok::Account* theAccount, const QVariantMap& var, Choqok::Post* post) { const QVariantMap object = var["object"].toMap(); - if (!object["deleted"].toString().isEmpty()) { - // Skip deleted posts - return 0; - } QTextDocument content; content.setHtml(object["content"].toString()); if (object["displayName"].toString().isEmpty()) { post->content = content.toPlainText(); } else { post->content = object["displayName"].toString() + '\n' + content.toPlainText(); } if (!object["fullImage"].toMap().isEmpty()) { const QString imagePath = object["fullImage"].toMap().take("url").toString(); if (!imagePath.isEmpty()) { post->content += '\n' + imagePath; } } post->creationDateTime = QDateTime::fromString(var["published"].toString(), "yyyy-MM-dd'T'hh:mm:ss'Z'"); post->link = object["url"].toString(); post->type = object["objectType"].toString(); const QVariantMap actor = var["actor"].toMap(); post->author.userId = actor["id"].toString(); post->author.userName = actor["preferredUsername"].toString(); post->author.realName = actor["displayName"].toString(); post->author.homePageUrl = actor["url"].toString(); post->author.location = actor["location"].toMap().take("displayName").toString(); post->author.description = actor["summary"].toString(); post->author.profileImageUrl = actor["image"].toMap().take("url").toString(); post->source = var["generator"].toMap().take("displayName").toString(); post->postId = var["id"].toString(); return post; } Choqok::UI::PostWidget* PumpIOMicroBlog::createPostWidget(Choqok::Account* account, Choqok::Post* post, QWidget* parent) { return new PumpIOPostWidget(account, post, parent); } QString PumpIOMicroBlog::profileUrl(Choqok::Account* account, const QString& username) const { return username; }