25 #include "metaweblog_p.h"
27 #include "blogmedia.h"
29 #include <kxmlrpcclient/client.h>
33 #include <kstandarddirs.h>
35 #include <QtCore/QFile>
36 #include <QtCore/QDataStream>
38 using namespace KBlog;
41 :
Blogger1( server, *new MetaWeblogPrivate, parent )
59 return QLatin1String(
"MetaWeblog" );
65 kDebug() <<
"Fetching List of Categories...";
66 QList<QVariant> args( d->defaultArgs(
blogId() ) );
67 d->mXmlRpcClient->call(
68 "metaWeblog.getCategories", args,
69 this, SLOT(slotListCategories(QList<QVariant>,QVariant)),
70 this, SLOT(slotError(
int,QString,QVariant)) );
77 kError() <<
"MetaWeblog::createMedia: media is a null pointer";
78 emit
error (
Other, i18n(
"Media is a null pointer." ) );
81 unsigned int i = d->mCallMediaCounter++;
82 d->mCallMediaMap[ i ] = media;
83 kDebug() <<
"MetaWeblog::createMedia: name=" << media->
name();
84 QList<QVariant> args( d->defaultArgs(
blogId() ) );
85 QMap<QString, QVariant> map;
87 map[
"name"] = media->
name();
89 map[
"bits"] = media->
data();
91 d->mXmlRpcClient->call(
92 "metaWeblog.newMediaObject", args,
93 this, SLOT(slotCreateMedia(QList<QVariant>,QVariant)),
94 this, SLOT(slotError(
int,QString,QVariant)),
99 MetaWeblogPrivate::MetaWeblogPrivate()
106 MetaWeblogPrivate::~MetaWeblogPrivate()
111 QList<QVariant> MetaWeblogPrivate::defaultArgs(
const QString &
id )
114 QList<QVariant> args;
115 if ( !
id.isEmpty() ) {
116 args << QVariant(
id );
118 args << QVariant( q->username() )
119 << QVariant( q->password() );
123 void MetaWeblogPrivate::loadCategories()
132 if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
133 kDebug() <<
"We need at least url, blogId and the username to create a unique filename.";
137 QString filename =
"kblog/" + mUrl.host() +
'_' + mBlogId +
'_' + mUsername;
138 filename = KStandardDirs::locateLocal(
"data", filename,
true );
140 QFile file( filename );
141 if ( !file.open( QIODevice::ReadOnly ) ) {
142 kDebug() <<
"Cannot open cached categories file: " << filename;
146 QDataStream stream( &file );
147 stream >> mCategoriesList;
151 void MetaWeblogPrivate::saveCategories()
154 if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
155 kDebug() <<
"We need at least url, blogId and the username to create a unique filename.";
159 QString filename =
"kblog/" + mUrl.host() +
'_' + mBlogId +
'_' + mUsername;
160 filename = KStandardDirs::locateLocal(
"data", filename,
true );
162 QFile file( filename );
163 if ( !file.open( QIODevice::WriteOnly ) ) {
164 kDebug() <<
"Cannot open cached categories file: " << filename;
168 QDataStream stream( &file );
169 stream << mCategoriesList;
174 void MetaWeblogPrivate::slotListCategories(
const QList<QVariant> &result,
180 kDebug() <<
"MetaWeblogPrivate::slotListCategories";
181 kDebug() <<
"TOP:" << result[0].typeName();
182 if ( result[0].type() != QVariant::Map &&
183 result[0].type() != QVariant::List ) {
186 kError() <<
"Could not list categories out of the result from the server.";
188 i18n(
"Could not list categories out of the result "
189 "from the server." ) );
191 if ( result[0].type() == QVariant::Map ) {
192 const QMap<QString, QVariant> serverMap = result[0].toMap();
193 const QList<QString> serverKeys = serverMap.keys();
195 QList<QString>::ConstIterator it = serverKeys.begin();
196 QList<QString>::ConstIterator end = serverKeys.end();
197 for ( ; it != end; ++it ) {
198 kDebug() <<
"MIDDLE:" << ( *it );
199 QMap<QString,QString> category;
200 const QMap<QString, QVariant> serverCategory = serverMap[*it].toMap();
201 category[
"name"]= ( *it );
202 category[
"description"] = serverCategory[
"description" ].toString();
203 category[
"htmlUrl"] = serverCategory[
"htmlUrl" ].toString();
204 category[
"rssUrl"] = serverCategory[
"rssUrl" ].toString();
205 category[
"categoryId"] = serverCategory[
"categoryId" ].toString();
206 category[
"parentId"] = serverCategory[
"parentId" ].toString();
207 mCategoriesList.append( category );
209 kDebug() <<
"Emitting listedCategories";
210 emit q->listedCategories( mCategoriesList );
213 if ( result[0].type() == QVariant::List ) {
216 const QList<QVariant> serverList = result[0].toList();
217 QList<QVariant>::ConstIterator it = serverList.begin();
218 QList<QVariant>::ConstIterator end = serverList.end();
219 for ( ; it != end; ++it ) {
220 kDebug() <<
"MIDDLE:" << ( *it ).typeName();
221 QMap<QString,QString> category;
222 const QMap<QString, QVariant> serverCategory = ( *it ).toMap();
223 category[
"name" ] = serverCategory[
"categoryName"].toString();
224 category[
"description"] = serverCategory[
"description" ].toString();
225 category[
"htmlUrl"] = serverCategory[
"htmlUrl" ].toString();
226 category[
"rssUrl"] = serverCategory[
"rssUrl" ].toString();
227 category[
"categoryId"] = serverCategory[
"categoryId" ].toString();
228 category[
"parentId"] = serverCategory[
"parentId" ].toString();
229 mCategoriesList.append( category );
231 kDebug() <<
"Emitting listedCategories()";
232 emit q->listedCategories( mCategoriesList );
237 void MetaWeblogPrivate::slotCreateMedia(
const QList<QVariant> &result,
243 mCallMediaMap.remove(
id.toInt() );
245 kDebug() <<
"MetaWeblogPrivate::slotCreateMedia, no error!";
246 kDebug() <<
"TOP:" << result[0].typeName();
247 if ( result[0].type() != 8 ) {
248 kError() <<
"Could not read the result, not a map.";
250 i18n(
"Could not read the result, not a map." ),
254 const QMap<QString, QVariant> resultStruct = result[0].toMap();
255 const QString url = resultStruct[
"url"].toString();
256 kDebug() <<
"MetaWeblog::slotCreateMedia url=" << url;
258 if ( !url.isEmpty() ) {
259 media->
setUrl( KUrl( url ) );
261 kDebug() <<
"Emitting createdMedia( url=" << url <<
");";
262 emit q->createdMedia( media );
266 bool MetaWeblogPrivate::readPostFromMap(
BlogPost *post,
267 const QMap<QString, QVariant> &postInfo )
270 kDebug() <<
"readPostFromMap()";
274 QStringList mapkeys = postInfo.keys();
275 kDebug() << endl <<
"Keys:" << mapkeys.join(
", " );
279 KDateTime( postInfo[
"dateCreated"].toDateTime(), KDateTime::UTC );
280 if ( dt.isValid() && !dt.isNull() ) {
285 KDateTime( postInfo[
"lastModified"].toDateTime(), KDateTime::UTC );
286 if ( dt.isValid() && !dt.isNull() ) {
290 post->
setPostId( postInfo[
"postid"].toString().isEmpty() ? postInfo[
"postId"].toString() :
291 postInfo[
"postid"].toString() );
293 QString title( postInfo[
"title"].toString() );
294 QString description( postInfo[
"description"].toString() );
295 QStringList categories( postInfo[
"categories"].toStringList() );
299 if ( !categories.isEmpty() ) {
300 kDebug() <<
"Categories:" << categories;
306 bool MetaWeblogPrivate::readArgsFromPost( QList<QVariant> *args,
const BlogPost &post )
311 QMap<QString, QVariant> map;
313 map[
"description"] = post.
content();
314 map[
"title"] = post.
title();
322 QString MetaWeblogPrivate::getCallFromFunction( FunctionToCall type )
325 case GetRecentPosts:
return "metaWeblog.getRecentPosts";
326 case CreatePost:
return "metaWeblog.newPost";
327 case ModifyPost:
return "metaWeblog.editPost";
328 case FetchPost:
return "metaWeblog.getPost";
329 default:
return QString();
332 #include "moc_metaweblog.cpp"