00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "blogger1.h"
00025 #include "blogger1_p.h"
00026 #include "blogpost.h"
00027
00028 #include <kxmlrpcclient/client.h>
00029
00030 #include <KDebug>
00031 #include <KDateTime>
00032 #include <KLocale>
00033
00034 #include <QList>
00035
00036 #include <QStringList>
00037
00038 using namespace KBlog;
00039
00040 Blogger1::Blogger1( const KUrl &server, QObject *parent )
00041 : Blog( server, *new Blogger1Private, parent )
00042 {
00043 kDebug() << "Blogger1()";
00044 setUrl( server );
00045 }
00046
00047 Blogger1::Blogger1( const KUrl &server, Blogger1Private &dd, QObject *parent )
00048 : Blog( server, dd, parent )
00049 {
00050 kDebug() << "Blogger1()";
00051 setUrl( server );
00052 }
00053
00054 Blogger1::~Blogger1()
00055 {
00056 kDebug() << "~Blogger1()";
00057 }
00058
00059 QString Blogger1::interfaceName() const
00060 {
00061 return QLatin1String( "Blogger 1.0" );
00062 }
00063
00064 void Blogger1::setUrl( const KUrl &server )
00065 {
00066 Q_D( Blogger1 );
00067 Blog::setUrl( server );
00068 delete d->mXmlRpcClient;
00069 d->mXmlRpcClient = new KXmlRpc::Client( server );
00070 d->mXmlRpcClient->setUserAgent( userAgent() );
00071 }
00072
00073 void Blogger1::fetchUserInfo()
00074 {
00075 Q_D( Blogger1 );
00076 kDebug() << "Fetch user's info...";
00077 QList<QVariant> args( d->blogger1Args() );
00078 d->mXmlRpcClient->call(
00079 "blogger.getUserInfo", args,
00080 this, SLOT(slotFetchUserInfo(const QList<QVariant>&,const QVariant&)),
00081 this, SLOT(slotError(int,const QString&,const QVariant&)) );
00082 }
00083
00084 void Blogger1::listBlogs()
00085 {
00086 Q_D( Blogger1 );
00087 kDebug() << "Fetch List of Blogs...";
00088 QList<QVariant> args( d->blogger1Args() );
00089 d->mXmlRpcClient->call(
00090 "blogger.getUsersBlogs", args,
00091 this, SLOT(slotListBlogs(const QList<QVariant>&,const QVariant&)),
00092 this, SLOT(slotError(int,const QString&,const QVariant&)) );
00093 }
00094
00095 void Blogger1::listRecentPosts( int number )
00096 {
00097 Q_D( Blogger1 );
00098 kDebug() << "Fetching List of Posts...";
00099 QList<QVariant> args( d->defaultArgs( blogId() ) );
00100 args << QVariant( number );
00101 d->mXmlRpcClient->call(
00102 d->getCallFromFunction( Blogger1Private::GetRecentPosts ), args,
00103 this, SLOT(slotListRecentPosts(const QList<QVariant>&,const QVariant&)),
00104 this, SLOT(slotError(int,const QString&,const QVariant&)),
00105 QVariant( number ) );
00106 }
00107
00108 void Blogger1::fetchPost( KBlog::BlogPost *post )
00109 {
00110 if ( !post ) {
00111 kError() << "Blogger1::modifyPost: post is null pointer";
00112 return;
00113 }
00114
00115 Q_D( Blogger1 );
00116 kDebug() << "Fetching Post with url" << post->postId();
00117 QList<QVariant> args( d->defaultArgs( post->postId() ) );
00118 unsigned int i= d->mCallCounter++;
00119 d->mCallMap[ i ] = post;
00120 d->mXmlRpcClient->call(
00121 d->getCallFromFunction( Blogger1Private::FetchPost ), args,
00122 this, SLOT(slotFetchPost(const QList<QVariant>&,const QVariant&)),
00123 this, SLOT(slotError(int, const QString&,const QVariant&)),
00124 QVariant( i ) );
00125 }
00126
00127 void Blogger1::modifyPost( KBlog::BlogPost *post )
00128 {
00129 Q_D( Blogger1 );
00130
00131 if ( !post ) {
00132 kError() << "Blogger1::modifyPost: post is null pointer";
00133 return;
00134 }
00135
00136 kDebug() << "Uploading Post with postId" << post->postId();
00137 unsigned int i= d->mCallCounter++;
00138 d->mCallMap[ i ] = post;
00139 QList<QVariant> args( d->defaultArgs( post->postId() ) );
00140 d->readArgsFromPost( &args, *post );
00141 d->mXmlRpcClient->call(
00142 d->getCallFromFunction( Blogger1Private::ModifyPost ), args,
00143 this, SLOT(slotModifyPost(const QList<QVariant>&,const QVariant&)),
00144 this, SLOT(slotError(int,const QString&,const QVariant&)),
00145 QVariant( i ) );
00146 }
00147
00148 void Blogger1::createPost( KBlog::BlogPost *post )
00149 {
00150 Q_D( Blogger1 );
00151
00152 if ( !post ) {
00153 kError() << "Blogger1::createPost: post is null pointer";
00154 return;
00155 }
00156
00157 unsigned int i= d->mCallCounter++;
00158 d->mCallMap[ i ] = post;
00159 kDebug() << "Creating new Post with blogid" << blogId();
00160 QList<QVariant> args( d->defaultArgs( blogId() ) );
00161 d->readArgsFromPost( &args, *post );
00162 d->mXmlRpcClient->call(
00163 d->getCallFromFunction( Blogger1Private::CreatePost ), args,
00164 this, SLOT(slotCreatePost(const QList<QVariant>&,const QVariant&)),
00165 this, SLOT(slotError(int, const QString&,const QVariant&)),
00166 QVariant( i ) );
00167 }
00168
00169 void Blogger1::removePost( KBlog::BlogPost *post )
00170 {
00171 Q_D( Blogger1 );
00172
00173 if ( !post ) {
00174 kError() << "Blogger1::removePost: post is null pointer";
00175 return;
00176 }
00177
00178 unsigned int i = d->mCallCounter++;
00179 d->mCallMap[ i ] = post;
00180 kDebug() << "Blogger1::removePost: postId=" << post->postId();
00181 QList<QVariant> args( d->blogger1Args( post->postId() ) );
00182 args << QVariant( true );
00183 d->mXmlRpcClient->call(
00184 "blogger.deletePost", args,
00185 this, SLOT(slotRemovePost(const QList<QVariant>&,const QVariant&)),
00186 this, SLOT(slotError(int,const QString&,const QVariant&)),
00187 QVariant( i ) );
00188 }
00189
00190 Blogger1Private::Blogger1Private() :
00191 mXmlRpcClient(0)
00192 {
00193 mCallCounter = 1;
00194 }
00195
00196 Blogger1Private::~Blogger1Private()
00197 {
00198 kDebug() << "~Blogger1Private()";
00199 delete mXmlRpcClient;
00200 }
00201
00202 QList<QVariant> Blogger1Private::defaultArgs( const QString &id )
00203 {
00204 kDebug();
00205 Q_Q ( Blogger1 );
00206 QList<QVariant> args;
00207 args << QVariant( QString( "0123456789ABCDEF" ) );
00208 if( !id.isEmpty() ) {
00209 args << QVariant( id );
00210 }
00211 args << QVariant( q->username() )
00212 << QVariant( q->password() );
00213 return args;
00214 }
00215
00216
00217 QList<QVariant> Blogger1Private::blogger1Args( const QString &id )
00218 {
00219 kDebug();
00220 Q_Q ( Blogger1 );
00221 QList<QVariant> args;
00222 args << QVariant( QString( "0123456789ABCDEF" ) );
00223 if( !id.isEmpty() ) {
00224 args << QVariant( id );
00225 }
00226 args << QVariant( q->username() )
00227 << QVariant( q->password() );
00228 return args;
00229 }
00230
00231 void Blogger1Private::slotFetchUserInfo( const QList<QVariant> &result, const QVariant &id )
00232 {
00233 Q_Q( Blogger1 );
00234 Q_UNUSED( id );
00235
00236 kDebug() << "Blog::slotFetchUserInfo";
00237 kDebug() << "TOP:" << result[0].typeName();
00238 QMap<QString,QString> userInfo;
00239 if ( result[0].type() != QVariant::Map ) {
00240 kError() << "Could not fetch user's info out of the result from the server,"
00241 << "not a map.";
00242 emit q->error( Blogger1::ParsingError,
00243 i18n( "Could not fetch user's info out of the result "
00244 "from the server, not a map." ) );
00245 return;
00246 }
00247 const QMap<QString,QVariant> resultMap = result[0].toMap();
00248 userInfo["nickname"]=resultMap["nickname"].toString();
00249 userInfo["userid"]=resultMap["userid"].toString();
00250 userInfo["url"]=resultMap["url"].toString();
00251 userInfo["email"]=resultMap["email"].toString();
00252 userInfo["lastname"]=resultMap["lastname"].toString();
00253 userInfo["firstname"]=resultMap["firstname"].toString();
00254
00255 emit q->fetchedUserInfo( userInfo );
00256 }
00257
00258 void Blogger1Private::slotListBlogs( const QList<QVariant> &result, const QVariant &id )
00259 {
00260 Q_Q( Blogger1 );
00261 Q_UNUSED( id );
00262
00263 kDebug() << "Blog::slotListBlogs";
00264 kDebug() << "TOP:" << result[0].typeName();
00265 QList<QMap<QString,QString> > blogsList;
00266 if ( result[0].type() != QVariant::List ) {
00267 kError() << "Could not fetch blogs out of the result from the server,"
00268 << "not a list.";
00269 emit q->error( Blogger1::ParsingError,
00270 i18n( "Could not fetch blogs out of the result "
00271 "from the server, not a list." ) );
00272 return;
00273 }
00274 const QList<QVariant> posts = result[0].toList();
00275 QList<QVariant>::ConstIterator it = posts.begin();
00276 QList<QVariant>::ConstIterator end = posts.end();
00277 for ( ; it != end; ++it ) {
00278 kDebug() << "MIDDLE:" << ( *it ).typeName();
00279 const QMap<QString, QVariant> postInfo = ( *it ).toMap();
00280 QMap<QString,QString> blogInfo;
00281 blogInfo[ "id" ] = postInfo["blogid"].toString();
00282 blogInfo[ "name" ] = postInfo["blogName"].toString();
00283 kDebug() << "Blog information retrieved: ID =" << blogInfo["id"]
00284 << ", Name =" << blogInfo["name"];
00285 blogsList << blogInfo;
00286 }
00287 emit q->listedBlogs( blogsList );
00288 }
00289
00290 void Blogger1Private::slotListRecentPosts( const QList<QVariant> &result, const QVariant &id )
00291 {
00292 Q_Q( Blogger1 );
00293 int count = id.toInt();
00294
00295
00296 kDebug() << "Blog::slotListRecentPosts";
00297 kDebug() << "TOP:" << result[0].typeName();
00298
00299 QList <BlogPost> fetchedPostList;
00300
00301 if ( result[0].type() != QVariant::List ) {
00302 kError() << "Could not fetch list of posts out of the"
00303 << "result from the server, not a list.";
00304 emit q->error( Blogger1::ParsingError,
00305 i18n( "Could not fetch list of posts out of the result "
00306 "from the server, not a list." ) );
00307 return;
00308 }
00309 const QList<QVariant> postReceived = result[0].toList();
00310 QList<QVariant>::ConstIterator it = postReceived.begin();
00311 QList<QVariant>::ConstIterator end = postReceived.end();
00312 for ( ; it != end; ++it ) {
00313 BlogPost post;
00314 kDebug() << "MIDDLE:" << ( *it ).typeName();
00315 const QMap<QString, QVariant> postInfo = ( *it ).toMap();
00316 if ( readPostFromMap( &post, postInfo ) ) {
00317 kDebug() << "Post with ID:"
00318 << post.postId()
00319 << "appended in fetchedPostList";
00320 post.setStatus( BlogPost::Fetched );
00321 fetchedPostList.append( post );
00322 } else {
00323 kError() << "readPostFromMap failed!";
00324 emit q->error( Blogger1::ParsingError, i18n( "Could not read post." ) );
00325 }
00326 if ( --count == 0 ) {
00327 break;
00328 }
00329 }
00330 kDebug() << "Emitting listRecentPostsFinished()";
00331 emit q->listedRecentPosts( fetchedPostList );
00332 }
00333
00334 void Blogger1Private::slotFetchPost( const QList<QVariant> &result, const QVariant &id )
00335 {
00336 Q_Q( Blogger1 );
00337 kDebug() << "Blog::slotFetchPost";
00338
00339 KBlog::BlogPost *post = mCallMap[ id.toInt() ];
00340 mCallMap.remove( id.toInt() );
00341
00342
00343
00344
00345 kDebug () << "TOP:" << result[0].typeName();
00346 if ( result[0].type() == QVariant::Map && readPostFromMap( post, result[0].toMap() ) ) {
00347 kDebug() << "Emitting fetchedPost()";
00348 post->setStatus( KBlog::BlogPost::Fetched );
00349 emit q->fetchedPost( post );
00350 } else {
00351 kError() << "Could not fetch post out of the result from the server.";
00352 post->setError( i18n( "Could not fetch post out of the result from the server." ) );
00353 post->setStatus( BlogPost::Error );
00354 emit q->errorPost( Blogger1::ParsingError,
00355 i18n( "Could not fetch post out of the result from the server." ), post );
00356 }
00357 }
00358
00359 void Blogger1Private::slotCreatePost( const QList<QVariant> &result, const QVariant &id )
00360 {
00361 Q_Q( Blogger1 );
00362 KBlog::BlogPost *post = mCallMap[ id.toInt() ];
00363 mCallMap.remove( id.toInt() );
00364
00365 kDebug() << "Blog::slotCreatePost";
00366
00367
00368
00369 kDebug () << "TOP:" << result[0].typeName();
00370 if ( result[0].type() != QVariant::String && result[0].type() != QVariant::Int ) {
00371 kError() << "Could not read the postId, not a string or an integer.";
00372 emit q->errorPost( Blogger1::ParsingError,
00373 i18n( "Could not read the postId, not a string or an integer." ),
00374 post );
00375 return;
00376 }
00377 QString serverID;
00378 if ( result[0].type() == QVariant::String ) {
00379 serverID = result[0].toString();
00380 }
00381 if ( result[0].type() == QVariant::Int ) {
00382 serverID = QString( "%1" ).arg( result[0].toInt() );
00383 }
00384 post->setPostId( serverID );
00385 post->setStatus( KBlog::BlogPost::Created );
00386 kDebug() << "emitting createdPost()"
00387 << "for title: \"" << post->title()
00388 << "\" server id: " << serverID;
00389 emit q->createdPost( post );
00390 }
00391
00392 void Blogger1Private::slotModifyPost( const QList<QVariant> &result, const QVariant &id )
00393 {
00394 Q_Q( Blogger1 );
00395 KBlog::BlogPost *post = mCallMap[ id.toInt() ];
00396 mCallMap.remove( id.toInt() );
00397
00398 kDebug() << "Blog::slotModifyPost";
00399
00400
00401
00402 kDebug() << "TOP:" << result[0].typeName();
00403 if ( result[0].type() != QVariant::Bool ) {
00404 kError() << "Could not read the result, not a boolean.";
00405 emit q->errorPost( Blogger1::ParsingError,
00406 i18n( "Could not read the result, not a boolean." ),
00407 post );
00408 return;
00409 }
00410 post->setStatus( KBlog::BlogPost::Modified );
00411 kDebug() << "emitting modifiedPost() for title: \""
00412 << post->title() << "\"";
00413 emit q->modifiedPost( post );
00414 }
00415
00416 void Blogger1Private::slotRemovePost( const QList<QVariant> &result, const QVariant &id )
00417 {
00418 Q_Q( Blogger1 );
00419 KBlog::BlogPost *post = mCallMap[ id.toInt() ];
00420 mCallMap.remove( id.toInt() );
00421
00422 kDebug() << "Blog::slotRemovePost";
00423
00424
00425
00426 kDebug() << "TOP:" << result[0].typeName();
00427 if ( result[0].type() != QVariant::Bool ) {
00428 kError() << "Could not read the result, not a boolean.";
00429 emit q->errorPost( Blogger1::ParsingError,
00430 i18n( "Could not read the result, not a boolean." ),
00431 post );
00432 return;
00433 }
00434 post->setStatus( KBlog::BlogPost::Removed );
00435 kDebug() << "emitting removedPost()";
00436 emit q->removedPost( post );
00437 }
00438
00439 void Blogger1Private::slotError( int number,
00440 const QString &errorString,
00441 const QVariant &id )
00442 {
00443 Q_Q( Blogger1 );
00444 Q_UNUSED( number );
00445 BlogPost *post = mCallMap[ id.toInt() ];
00446
00447 emit q->errorPost( Blogger1::XmlRpc, errorString, post );
00448 }
00449
00450 bool Blogger1Private::readPostFromMap(
00451 BlogPost *post, const QMap<QString, QVariant> &postInfo )
00452 {
00453
00454 if ( !post ) {
00455 return false;
00456 }
00457 QStringList mapkeys = postInfo.keys();
00458 kDebug() << endl << "Keys:" << mapkeys.join( ", " );
00459 kDebug() << endl;
00460
00461 KDateTime dt( postInfo["dateCreated"].toDateTime(), KDateTime::UTC );
00462 if ( dt.isValid() && !dt.isNull() ) {
00463 post->setCreationDateTime( dt );
00464 }
00465 dt = KDateTime ( postInfo["lastModified"].toDateTime(), KDateTime::UTC );
00466 if ( dt.isValid() && !dt.isNull() ) {
00467 post->setModificationDateTime( dt );
00468 }
00469 post->setPostId( postInfo["postid"].toString() );
00470
00471 QString title( postInfo["title"].toString() );
00472 QString description( postInfo["description"].toString() );
00473 QString contents( postInfo["content"].toString() );
00474 QStringList category;
00475
00476
00477 QRegExp titleMatch = QRegExp( "<title>([^<]*)</title>" );
00478 QRegExp categoryMatch = QRegExp( "<category>([^<]*)</category>" );
00479 contents.remove( titleMatch );
00480 if ( titleMatch.numCaptures() > 0 ) {
00481
00482 title = titleMatch.cap( 1 );
00483 }
00484 contents.remove( categoryMatch );
00485 if ( categoryMatch.numCaptures() > 0 ) {
00486
00487 category = categoryMatch.capturedTexts();
00488 }
00489
00490 post->setTitle( title );
00491 post->setContent( contents );
00492 post->setCategories( category );
00493 return true;
00494 }
00495
00496 bool Blogger1Private::readArgsFromPost( QList<QVariant> *args, const BlogPost &post )
00497 {
00498 if ( !args ) {
00499 return false;
00500 }
00501 QStringList categories = post.categories();
00502 QString content = "<title>" + post.title() + "</title>";
00503 QStringList::const_iterator it;
00504 for ( it = categories.constBegin(); it != categories.constEnd(); ++it ) {
00505 content += "<category>" + *it + "</category>";
00506 }
00507 content += post.content();
00508 *args << QVariant( content );
00509 *args << QVariant( !post.isPrivate() );
00510 return true;
00511 }
00512
00513 QString Blogger1Private::getCallFromFunction( FunctionToCall type )
00514 {
00515 switch ( type ) {
00516 case GetRecentPosts: return "blogger.getRecentPosts";
00517 case CreatePost: return "blogger.newPost";
00518 case ModifyPost: return "blogger.editPost";
00519 case FetchPost: return "blogger.getPost";
00520 default: return QString();
00521 }
00522 }
00523
00524 #include "blogger1.moc"