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() << "MetaWeblog()";
00040 }
00041
00042 MetaWeblog::MetaWeblog( const KUrl &server, MetaWeblogPrivate &dd, QObject *parent )
00043 : Blogger1( server, dd, parent )
00044 {
00045 kDebug() << "MetaWeblog()";
00046 }
00047
00048 MetaWeblog::~MetaWeblog()
00049 {
00050 kDebug() << "~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() << "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() << "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() << "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() << "~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() << "MetaWeblogPrivate::slotListCategories";
00126 kDebug() << "TOP:" << result[0].typeName();
00127 if ( result[0].type() != QVariant::Map &&
00128 result[0].type() != QVariant::List ) {
00129
00130
00131 kError() << "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() << "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 category["categoryId"] = serverCategory[ "categoryId" ].toString();
00151 category["parentId"] = serverCategory[ "parentId" ].toString();
00152 categoriesList.append( category );
00153 }
00154 kDebug() << "Emitting listedCategories";
00155 emit q->listedCategories( categoriesList );
00156 }
00157 }
00158 if ( result[0].type() == QVariant::List ) {
00159
00160
00161 const QList<QVariant> serverList = result[0].toList();
00162 QList<QVariant>::ConstIterator it = serverList.begin();
00163 QList<QVariant>::ConstIterator end = serverList.end();
00164 for ( ; it != end; ++it ) {
00165 kDebug() << "MIDDLE:" << ( *it ).typeName();
00166 QMap<QString,QString> category;
00167 const QMap<QString, QVariant> serverCategory = ( *it ).toMap();
00168 category[ "name" ] = serverCategory["categoryName"].toString();
00169 category["description"] = serverCategory[ "description" ].toString();
00170 category["htmlUrl"] = serverCategory[ "htmlUrl" ].toString();
00171 category["rssUrl"] = serverCategory[ "rssUrl" ].toString();
00172 category["categoryId"] = serverCategory[ "categoryId" ].toString();
00173 category["parentId"] = serverCategory[ "parentId" ].toString();
00174 categoriesList.append( category );
00175 }
00176 kDebug() << "Emitting listedCategories()";
00177 emit q->listedCategories( categoriesList );
00178 }
00179 }
00180
00181 void MetaWeblogPrivate::slotCreateMedia( const QList<QVariant> &result,
00182 const QVariant &id )
00183 {
00184 Q_Q( MetaWeblog );
00185
00186 KBlog::BlogMedia *media = mCallMediaMap[ id.toInt() ];
00187 mCallMediaMap.remove( id.toInt() );
00188
00189 kDebug() << "MetaWeblogPrivate::slotCreateMedia, no error!";
00190 kDebug() << "TOP:" << result[0].typeName();
00191 if ( result[0].type() != 8 ) {
00192 kError() << "Could not read the result, not a map.";
00193 emit q->errorMedia( MetaWeblog::ParsingError,
00194 i18n( "Could not read the result, not a map." ),
00195 media );
00196 return;
00197 }
00198 const QMap<QString, QVariant> resultStruct = result[0].toMap();
00199 const QString url = resultStruct["url"].toString();
00200 kDebug() << "MetaWeblog::slotCreateMedia url=" << url;
00201
00202 if ( !url.isEmpty() ) {
00203 media->setUrl( KUrl( url ) );
00204 media->setStatus( BlogMedia::Created );
00205 kDebug() << "Emitting createdMedia( url=" << url << ");";
00206 emit q->createdMedia( media );
00207 }
00208 }
00209
00210 bool MetaWeblogPrivate::readPostFromMap( BlogPost *post,
00211 const QMap<QString, QVariant> &postInfo )
00212 {
00213
00214 kDebug() << "readPostFromMap()";
00215 if ( !post ) {
00216 return false;
00217 }
00218 QStringList mapkeys = postInfo.keys();
00219 kDebug() << endl << "Keys:" << mapkeys.join( ", " );
00220 kDebug() << endl;
00221
00222 KDateTime dt =
00223 KDateTime( postInfo["dateCreated"].toDateTime(), KDateTime::UTC );
00224 if ( dt.isValid() && !dt.isNull() ) {
00225 post->setCreationDateTime( dt );
00226 }
00227
00228 dt =
00229 KDateTime( postInfo["lastModified"].toDateTime(), KDateTime::UTC );
00230 if ( dt.isValid() && !dt.isNull() ) {
00231 post->setModificationDateTime( dt );
00232 }
00233
00234 post->setPostId( postInfo["postid"].toString() );
00235
00236 QString title( postInfo["title"].toString() );
00237 QString description( postInfo["description"].toString() );
00238 QStringList categories( postInfo["categories"].toStringList() );
00239
00240 post->setTitle( title );
00241 post->setContent( description );
00242 if ( !categories.isEmpty() ){
00243 kDebug() << "Categories:" << categories;
00244 post->setCategories( categories );
00245 }
00246 return true;
00247 }
00248
00249 bool MetaWeblogPrivate::readArgsFromPost( QList<QVariant> *args, const BlogPost &post )
00250 {
00251 if ( !args ) {
00252 return false;
00253 }
00254 QMap<QString, QVariant> map;
00255 map["categories"] = post.categories();
00256 map["description"] = post.content();
00257 map["title"] = post.title();
00258 map["lastModified"] = post.modificationDateTime().toUtc().dateTime();
00259 map["dateCreated"] = post.creationDateTime().toUtc().dateTime();
00260 *args << map;
00261 *args << QVariant( !post.isPrivate() );
00262 return true;
00263 }
00264
00265 QString MetaWeblogPrivate::getCallFromFunction( FunctionToCall type )
00266 {
00267 switch ( type ) {
00268 case GetRecentPosts: return "metaWeblog.getRecentPosts";
00269 case CreatePost: return "metaWeblog.newPost";
00270 case ModifyPost: return "metaWeblog.editPost";
00271 case FetchPost: return "metaWeblog.getPost";
00272 default: return QString();
00273 }
00274 }
00275 #include "metaweblog.moc"