25 #include "movabletype_p.h"
28 #include <kxmlrpcclient/client.h>
35 #include <QtCore/QStringList>
37 using namespace KBlog;
40 :
MetaWeblog( server, *new MovableTypePrivate, parent )
59 return QLatin1String(
"Movable Type" );
66 QList<QVariant> args( d->defaultArgs(
blogId() ) );
67 args << QVariant( number );
68 d->mXmlRpcClient->call(
69 "metaWeblog.getRecentPosts", args,
70 this, SLOT(slotListRecentPosts(QList<QVariant>,QVariant)),
71 this, SLOT(slotError(
int,QString,QVariant)),
80 args << QVariant( post->
postId() );
81 unsigned int i = d->mCallCounter++;
82 d->mCallMap[ i ] = post;
83 d->mXmlRpcClient->call(
84 "mt.getTrackbackPings", args,
85 this, SLOT(slotListTrackbackPings(QList<QVariant>,QVariant)),
86 this, SLOT(slotError(
int,QString,QVariant)),
95 if ( d->mCategoriesList.isEmpty() && post->
categories( ).count() ) {
96 d->mFetchPostCache << post;
97 if ( d->mFetchPostCache.count() ) {
104 ,
this, SLOT(slotTriggerFetchPost()) );
121 if(d->mCategoriesList.isEmpty()&&!post->
categories().isEmpty()){
122 kDebug() <<
"No categories in the cache yet. Have to fetch them first.";
123 d->mCreatePostCache << post;
125 this,SLOT(slotTriggerCreatePost()));
133 if ( d->mSilentCreationList.contains( post ) ) {
134 kDebug()<<
"Post already in mSilentCreationList, this *should* never happen!";
136 d->mSilentCreationList << post;
156 if(d->mCategoriesList.isEmpty() && !post->
categories().isEmpty()){
157 kDebug() <<
"No categories in the cache yet. Have to fetch them first.";
158 d->mModifyPostCache << post;
160 this,SLOT(slotTriggerModifyPost()));
168 void MovableTypePrivate::slotTriggerCreatePost()
173 q->disconnect(q,SIGNAL(listedCategories(QList<QMap<QString,QString> >)),
174 q,SLOT(slotTriggerCreatePost()));
176 QList<BlogPost*>::Iterator it = mCreatePostCache.begin();
177 QList<BlogPost*>::Iterator end = mCreatePostCache.end();
178 for ( ; it!=end; it++ ) {
179 q->createPost( *it );
181 mCreatePostCache.clear();
184 void MovableTypePrivate::slotTriggerModifyPost()
189 q->disconnect(q,SIGNAL(listedCategories(QList<QMap<QString,QString> >)),
190 q,SLOT(slotTriggerModifyPost()));
192 QList<BlogPost*>::Iterator it = mModifyPostCache.begin();
193 QList<BlogPost*>::Iterator end = mModifyPostCache.end();
194 for ( ; it!=end; it++ ) {
195 q->modifyPost( *it );
197 mModifyPostCache.clear();
200 void MovableTypePrivate::slotTriggerFetchPost()
205 q->disconnect( q,SIGNAL(listedCategories(QList<QMap<QString,QString> >)),
206 q,SLOT(slotTriggerFetchPost()) );
207 QList<BlogPost*>::Iterator it = mFetchPostCache.begin();
208 QList<BlogPost*>::Iterator end = mFetchPostCache.end();
209 for ( ; it!=end; it++ ) {
212 mFetchPostCache.clear();
216 MovableTypePrivate::MovableTypePrivate()
221 MovableTypePrivate::~MovableTypePrivate()
226 void MovableTypePrivate::slotCreatePost(
const QList<QVariant> &result,
const QVariant &
id )
232 mCallMap.remove(
id.toInt() );
237 kDebug () <<
"TOP:" << result[0].typeName();
238 if ( result[0].type() != QVariant::String && result[0].type() != QVariant::Int ) {
239 kError() <<
"Could not read the postId, not a string or an integer.";
241 i18n(
"Could not read the postId, not a string or an integer." ),
246 if ( result[0].type() == QVariant::String ) {
247 serverID = result[0].toString();
249 if ( result[0].type() == QVariant::Int ) {
250 serverID = QString(
"%1" ).arg( result[0].toInt() );
253 if ( mSilentCreationList.contains( post ) )
256 setPostCategories( post, !post->
isPrivate() );
258 kDebug() <<
"emitting createdPost()"
259 <<
"for title: \"" << post->
title()
260 <<
"\" server id: " << serverID;
262 emit q->createdPost( post );
266 void MovableTypePrivate::slotFetchPost(
const QList<QVariant> &result,
const QVariant &
id )
272 mCallMap.remove(
id.toInt() );
276 kDebug () <<
"TOP:" << result[0].typeName();
277 if ( result[0].type() == QVariant::Map && readPostFromMap( post, result[0].toMap() ) ) {
279 kError() <<
"Could not fetch post out of the result from the server.";
280 post->
setError( i18n(
"Could not fetch post out of the result from the server." ) );
283 i18n(
"Could not fetch post out of the result from the server." ), post );
286 QList<QVariant> args( defaultArgs( post->
postId() ) );
287 unsigned int i= mCallCounter++;
288 mCallMap[ i ] = post;
290 "mt.getPostCategories", args,
291 q, SLOT(slotGetPostCategories(QList<QVariant>,QVariant)),
292 q, SLOT(slotError(
int,QString,QVariant)),
295 kDebug() <<
"Emitting fetchedPost()";
297 emit q->fetchedPost( post );
301 void MovableTypePrivate::slotModifyPost(
const QList<QVariant> &result,
const QVariant &
id )
307 mCallMap.remove(
id.toInt() );
311 kDebug() <<
"TOP:" << result[0].typeName();
312 if ( result[0].type() != QVariant::Bool && result[0].type() != QVariant::Int ) {
313 kError() <<
"Could not read the result, not a boolean.";
315 i18n(
"Could not read the result, not a boolean." ),
319 if ( mSilentCreationList.contains( post ) ) {
321 mSilentCreationList.removeOne( post );
322 emit q->createdPost( post );
325 setPostCategories( post,
false );
330 void MovableTypePrivate::setPostCategories(
BlogPost *post,
bool publishAfterCategories )
335 unsigned int i = mCallCounter++;
336 mCallMap[ i ] = post;
337 mPublishAfterCategories[ i ] = publishAfterCategories;
338 QList<QVariant> catList;
339 QList<QVariant> args( defaultArgs( post->
postId() ) );
343 for(
int j=0; j<categories.count(); j++ ){
344 for(
int k=0; k<mCategoriesList.count(); k++ ){
345 if(mCategoriesList[k][
"name"]==categories[j]){
346 kDebug() <<
"Matched category with name: " << categories[ j ] <<
" and id: " << mCategoriesList[ k ][
"categoryId" ];
347 QMap<QString,QVariant> category;
350 category[
"categoryId"]=mCategoriesList[k][
"categoryId"].toInt();
351 catList<<QVariant( category );
354 if(k==mCategoriesList.count()){
355 kDebug() <<
"Couldn't find categoryId for: " << categories[j];
359 args<<QVariant( catList );
362 "mt.setPostCategories", args,
363 q, SLOT(slotSetPostCategories(QList<QVariant>,QVariant)),
364 q, SLOT(slotError(
int,QString,QVariant)),
368 void MovableTypePrivate::slotGetPostCategories(
const QList<QVariant>& result,
const QVariant&
id)
377 if ( result[ 0 ].type() != QVariant::List ) {
378 kError() <<
"Could not read the result, not a list. Category fetching failed! We will still emit fetched post now.";
380 i18n(
"Could not read the result - is not a list. Category fetching failed." ), post );
383 emit q->fetchedPost( post );
385 QList<QVariant> categoryList = result[ 0 ].toList();
386 QList<QString> newCatList;
387 QList<QVariant>::ConstIterator it = categoryList.constBegin();
388 QList<QVariant>::ConstIterator end = categoryList.constEnd();
389 for ( ;it!=end;it++ ) {
390 newCatList << ( *it ).toMap()[
"categoryName" ].toString();
392 kDebug()<<
"categories list: " << newCatList;
395 emit q->fetchedPost( post );
399 void MovableTypePrivate::slotSetPostCategories(
const QList<QVariant>& result,
const QVariant&
id)
406 bool publish = mPublishAfterCategories[ i ];
408 mPublishAfterCategories.remove(i);
410 if ( result[0].type() != QVariant::Bool ) {
411 kError() <<
"Could not read the result, not a boolean. Category setting failed! We will still publish if now if necessary. ";
413 i18n(
"Could not read the result - is not a boolean value. Category setting failed. Will still publish now if necessary." ),
420 q->modifyPost( post );
425 if ( mSilentCreationList.contains( post ) ) {
426 kDebug() <<
"emitting createdPost() for title: \""
427 << post->
title() <<
"\"";
429 mSilentCreationList.removeOne( post );
430 emit q->createdPost( post );
432 kDebug() <<
"emitting modifiedPost() for title: \""
433 << post->
title() <<
"\"";
435 emit q->modifiedPost( post );
440 QList<QVariant> MovableTypePrivate::defaultArgs(
const QString &
id )
443 QList<QVariant> args;
444 if( !
id.isEmpty() ) {
445 args << QVariant(
id );
447 args << QVariant( q->username() )
448 << QVariant( q->password() );
452 bool MovableTypePrivate::readPostFromMap(
BlogPost *post,
const QMap<QString, QVariant> &postInfo )
456 kDebug() <<
"readPostFromMap()";
460 QStringList mapkeys = postInfo.keys();
461 kDebug() << endl <<
"Keys:" << mapkeys.join(
", " );
465 KDateTime( postInfo[
"dateCreated"].toDateTime(), KDateTime::UTC );
466 if ( dt.isValid() && !dt.isNull() ) {
471 KDateTime( postInfo[
"lastModified"].toDateTime(), KDateTime::UTC );
472 if ( dt.isValid() && !dt.isNull() ) {
476 post->
setPostId( postInfo[
"postid"].toString().isEmpty() ? postInfo[
"postId"].toString() :
477 postInfo[
"postid"].toString() );
479 QString title( postInfo[
"title"].toString() );
480 QString description( postInfo[
"description"].toString() );
481 QStringList categoryIdList = postInfo[
"categories"].toStringList();
482 QStringList categories;
485 for (
int i=0; i<categoryIdList.count(); i++ ) {
486 for (
int k=0; k<mCategoriesList.count(); k++ ) {
487 if ( mCategoriesList[ k ][
"name" ]==categoryIdList[ i ] ){
488 categories << mCategoriesList[ k ][
"name" ];
489 }
else if ( mCategoriesList[ k ][
"categoryId" ]==categoryIdList[ i ]) {
490 categories << mCategoriesList[ k ][
"name" ];
497 post->
setSlug( postInfo[
"wp_slug"].toString() );
503 post->
setSummary( postInfo[
"mt_excerpt"].toString() );
504 post->
setTags( postInfo[
"mt_keywords"].toStringList() );
505 post->
setLink( postInfo[
"link"].toString() );
507 QString postStatus = postInfo[
"post_status"].toString();
508 if( postStatus !=
"publish" && !postStatus.isEmpty() ){
516 if ( !categories.isEmpty() ){
517 kDebug() <<
"Categories:" << categories;
523 void MovableTypePrivate::slotListTrackBackPings(
524 const QList<QVariant> &result,
const QVariant &
id )
527 kDebug() <<
"slotTrackbackPings()";
528 BlogPost *post = mCallMap[
id.toInt() ];
529 mCallMap.remove(
id.toInt() );
530 QList<QMap<QString,QString> > trackBackList;
531 if ( result[0].type() != QVariant::List ) {
532 kError() <<
"Could not fetch list of trackback pings out of the"
533 <<
"result from the server.";
535 i18n(
"Could not fetch list of trackback pings out of the "
536 "result from the server." ) );
539 const QList<QVariant> trackBackReceived = result[0].toList();
540 QList<QVariant>::ConstIterator it = trackBackReceived.begin();
541 QList<QVariant>::ConstIterator end = trackBackReceived.end();
542 for ( ; it != end; ++it ) {
543 QMap<QString,QString> tping;
544 kDebug() <<
"MIDDLE:" << ( *it ).typeName();
545 const QMap<QString, QVariant> trackBackInfo = ( *it ).toMap();
546 tping[
"title" ] = trackBackInfo[
"pingTitle"].toString();
547 tping[
"url" ] = trackBackInfo[
"pingURL"].toString();
548 tping[
"ip" ] = trackBackInfo[
"pingIP"].toString();
549 trackBackList << tping;
551 kDebug() <<
"Emitting listedTrackBackPings()";
552 emit q->listedTrackBackPings( post, trackBackList );
555 bool MovableTypePrivate::readArgsFromPost( QList<QVariant> *args,
const BlogPost &post )
563 QMap<QString, QVariant> map;
565 map[
"description"] = post.
content();
568 map[
"title"] = post.
title();
572 map[
"mt_excerpt"] = post.
summary();
573 map[
"mt_keywords"] = post.
tags().join(
",");
580 #include "movabletype.moc"