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 "metaweblog.h"
00025 #include "metaweblog_p.h"
00026 #include "blogpost.h"
00027 #include "blogmedia.h"
00028
00029 #include <kxmlrpcclient/client.h>
00030 #include <KDebug>
00031 #include <KLocale>
00032 #include <KDateTime>
00033
00034 using namespace KBlog;
00035
00036 MetaWeblog::MetaWeblog( const KUrl &server, QObject *parent )
00037 : Blogger1( server, *new MetaWeblogPrivate, parent )
00038 {
00039 kDebug(5323) << "MetaWeblog()";
00040 }
00041
00042 MetaWeblog::MetaWeblog( const KUrl &server, MetaWeblogPrivate &dd, QObject *parent )
00043 : Blogger1( server, dd, parent )
00044 {
00045 kDebug(5323) << "MetaWeblog()";
00046 }
00047
00048 MetaWeblog::~MetaWeblog()
00049 {
00050 kDebug(5323) << "~MetaWeblog()";
00051 }
00052
00053 QString MetaWeblog::interfaceName() const
00054 {
00055 return QLatin1String( "MetaWeblog" );
00056 }
00057
00058 void MetaWeblog::listCategories()
00059 {
00060 Q_D( MetaWeblog );
00061 kDebug(5323) << "Fetching List of Categories...";
00062 QList<QVariant> args( d->defaultArgs( blogId() ) );
00063 d->mXmlRpcClient->call(
00064 "metaWeblog.getCategories", args,
00065 this, SLOT(slotListCategories(const QList<QVariant>&, const QVariant&)),
00066 this, SLOT(slotError(int, const QString&, const QVariant&)) );
00067 }
00068
00069 void MetaWeblog::createMedia( KBlog::BlogMedia *media )
00070 {
00071 Q_D( MetaWeblog );
00072 if ( !media ) {
00073 kError(5323) << "MetaWeblog::createMedia: media is a null pointer";
00074 emit error ( Other, i18n( "Media is a null pointer." ) );
00075 return;
00076 }
00077 unsigned int i = d->mCallMediaCounter++;
00078 d->mCallMediaMap[ i ] = media;
00079 kDebug(5323) << "MetaWeblog::createMedia: name="<< media->name();
00080 QList<QVariant> args( d->defaultArgs( blogId() ) );
00081 QMap<QString, QVariant> map;
00082 QList<QVariant> list;
00083 map["name"] = media->name();
00084 map["type"] = media->mimetype();
00085 map["bits"] = media->data();
00086 args << map;
00087 d->mXmlRpcClient->call(
00088 "metaWeblog.newMediaObject", args,
00089 this, SLOT(slotCreateMedia(const QList<QVariant>&,const QVariant&)),
00090 this, SLOT(slotError(int,const QString&,const QVariant&)),
00091 QVariant( i ) );
00092
00093 }
00094
00095 MetaWeblogPrivate::MetaWeblogPrivate()
00096 {
00097 mCallMediaCounter=1;
00098 }
00099
00100 MetaWeblogPrivate::~MetaWeblogPrivate()
00101 {
00102 kDebug(5323) << "~MetaWeblogPrivate()";
00103 }
00104
00105 QList<QVariant> MetaWeblogPrivate::defaultArgs( const QString &id )
00106 {
00107 Q_Q( MetaWeblog );
00108 QList<QVariant> args;
00109 if( !id.isEmpty() ) {
00110 args << QVariant( id );
00111 }
00112 args << QVariant( q->username() )
00113 << QVariant( q->password() );
00114 return args;
00115 }
00116
00117 void MetaWeblogPrivate::slotListCategories( const QList<QVariant> &result,
00118 const QVariant &id )
00119 {
00120 Q_Q( MetaWeblog );
00121 Q_UNUSED( id );
00122
00123 QList<QMap<QString,QString> > categoriesList;
00124
00125 kDebug(5323) << "MetaWeblogPrivate::slotListCategories";
00126 kDebug(5323) << "TOP:" << result[0].typeName();
00127 if ( result[0].type() != QVariant::Map &&
00128 result[0].type() != QVariant::List ) {
00129
00130
00131 kError(5323) << "Could not list categories out of the result from the server.";
00132 emit q->error( MetaWeblog::ParsingError,
00133 i18n( "Could not list categories out of the result "
00134 "from the server." ) );
00135 } else {
00136 if ( result[0].type() == QVariant::Map ) {
00137 const QMap<QString, QVariant> serverMap = result[0].toMap();
00138 const QList<QString> serverKeys = serverMap.keys();
00139
00140 QList<QString>::ConstIterator it = serverKeys.begin();
00141 QList<QString>::ConstIterator end = serverKeys.end();
00142 for ( ; it != end; ++it ) {
00143 kDebug(5323) << "MIDDLE:" << ( *it );
00144 QMap<QString,QString> category;
00145 const QMap<QString, QVariant> serverCategory = serverMap[*it].toMap();
00146 category["name"]= ( *it );
00147 category["description"] = serverCategory[ "description" ].toString();
00148 category["htmlUrl"] = serverCategory[ "htmlUrl" ].toString();
00149 category["rssUrl"] = serverCategory[ "rssUrl" ].toString();
00150 categoriesList.append( category );
00151 }
00152 emit q->listedCategories( categoriesList );
00153 kDebug(5323) << "Emitting listedCategories";
00154 }
00155 }
00156 if ( result[0].type() == QVariant::List ) {
00157
00158
00159 const QList<QVariant> serverList = result[0].toList();
00160 QList<QVariant>::ConstIterator it = serverList.begin();
00161 QList<QVariant>::ConstIterator end = serverList.end();
00162 for ( ; it != end; ++it ) {
00163 kDebug(5323) << "MIDDLE:" << ( *it ).typeName();
00164 QMap<QString,QString> category;
00165 const QMap<QString, QVariant> serverCategory = ( *it ).toMap();
00166 category[ "name" ] = serverCategory["categoryName"].toString();
00167 category["description"] = serverCategory[ "description" ].toString();
00168 category["htmlUrl"] = serverCategory[ "htmlUrl" ].toString();
00169 category["rssUrl"] = serverCategory[ "rssUrl" ].toString();
00170 categoriesList.append( category );
00171 }
00172 kDebug(5323) << "Emitting listedCategories()";
00173 emit q->listedCategories( categoriesList );
00174 }
00175 }
00176
00177 void MetaWeblogPrivate::slotCreateMedia( const QList<QVariant> &result,
00178 const QVariant &id )
00179 {
00180 Q_Q( MetaWeblog );
00181
00182 KBlog::BlogMedia *media = mCallMediaMap[ id.toInt() ];
00183 mCallMediaMap.remove( id.toInt() );
00184
00185 kDebug(5323) << "MetaWeblogPrivate::slotCreateMedia, no error!";
00186 kDebug(5323) << "TOP:" << result[0].typeName();
00187 if ( result[0].type() != 8 ) {
00188 kError(5323) << "Could not read the result, not a map.";
00189 emit q->errorMedia( MetaWeblog::ParsingError,
00190 i18n( "Could not read the result, not a map." ),
00191 media );
00192 } else {
00193 const QMap<QString, QVariant> resultStruct = result[0].toMap();
00194 const QString url = resultStruct["url"].toString();
00195 kDebug(5323) << "MetaWeblog::slotCreateMedia url=" << url;
00196
00197 if ( !url.isEmpty() ) {
00198 media->setUrl( KUrl( url ) );
00199 media->setStatus( BlogMedia::Created );
00200 emit q->createdMedia( media );
00201 kDebug(5323) << "Emitting createdMedia( url=" << url << ");";
00202 }
00203 }
00204 }
00205
00206 bool MetaWeblogPrivate::readPostFromMap( BlogPost *post,
00207 const QMap<QString, QVariant> &postInfo )
00208 {
00209
00210 kDebug(5323) << "readPostFromMap()";
00211 if ( !post ) {
00212 return false;
00213 }
00214 QStringList mapkeys = postInfo.keys();
00215 kDebug(5323) << endl << "Keys:" << mapkeys.join( ", " );
00216 kDebug(5323) << endl;
00217
00218 KDateTime dt =
00219 KDateTime( postInfo["dateCreated"].toDateTime(), KDateTime::UTC );
00220 if ( dt.isValid() && !dt.isNull() ) {
00221 post->setCreationDateTime( dt );
00222 }
00223
00224 dt =
00225 KDateTime( postInfo["lastModified"].toDateTime(), KDateTime::UTC );
00226 if ( dt.isValid() && !dt.isNull() ) {
00227 post->setModificationDateTime( dt );
00228 }
00229
00230 post->setPostId( postInfo["postid"].toString() );
00231
00232 QString title( postInfo["title"].toString() );
00233 QString description( postInfo["description"].toString() );
00234 QStringList categories( postInfo["categories"].toStringList() );
00235
00236 post->setTitle( title );
00237 post->setContent( description );
00238 if ( !categories.isEmpty() ){
00239 kDebug(5323) << "Categories:" << categories;
00240 post->setCategories( categories );
00241 }
00242 return true;
00243 }
00244
00245 bool MetaWeblogPrivate::readArgsFromPost( QList<QVariant> *args, const BlogPost &post )
00246 {
00247 if ( !args ) {
00248 return false;
00249 }
00250 QMap<QString, QVariant> map;
00251 map["categories"] = post.categories();
00252 map["description"] = post.content();
00253 map["title"] = post.title();
00254 map["lastModified"] = post.modificationDateTime().toUtc().dateTime();
00255 map["dateCreated"] = post.creationDateTime().toUtc().dateTime();
00256 *args << map;
00257 *args << QVariant( !post.isPrivate() );
00258 return true;
00259 }
00260
00261 QString MetaWeblogPrivate::getCallFromFunction( FunctionToCall type )
00262 {
00263 switch ( type ) {
00264 case GetRecentPosts: return "metaWeblog.getRecentPosts";
00265 case CreatePost: return "metaWeblog.newPost";
00266 case ModifyPost: return "metaWeblog.editPost";
00267 case FetchPost: return "metaWeblog.getPost";
00268 default: return QString();
00269 }
00270 }
00271 #include "metaweblog.moc"