25 #include "metaweblog_p.h"
27 #include "blogmedia.h"
29 #include <kxmlrpcclient/client.h>
31 #include <KLocalizedString>
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"
QString title() const
Returns the title.
MetaWeblog(const KUrl &server, QObject *parent=0)
Create an object for MetaWeblog.
QString interfaceName() const
Returns the of the inherited object.
void error(KBlog::Blog::ErrorType type, const QString &errorMessage)
This signal is emitted when an error occurs with XML parsing or a structural problem.
KDateTime creationDateTime() const
Returns the creation date time.
virtual void listCategories()
List the categories of the blog.
A class that represents a media object on the server.
void setStatus(Status status)
Set the status.
A class that can be used for access to MetaWeblog blogs.
QString mimetype() const
Returns the mimetype.
void setTitle(const QString &title)
Sets the title.
virtual void createMedia(KBlog::BlogMedia *media)
Create a new media object, e.g.
void setPostId(const QString &postId)
Sets the post id value.
void setCreationDateTime(const KDateTime &datetime)
Sets the creation time.
void setUrl(const KUrl &url)
Sets the url of the server side object.
void setContent(const QString &content)
Sets the content.
KDateTime modificationDateTime() const
Returns the modification date time.
void setModificationDateTime(const KDateTime &datetime)
Sets the modification time.
QString blogId() const
Returns the unique ID for the specific blog on the server.
This file is part of the for accessing Blog Servers and defines the MetaWeblog class.
QString content() const
Returns the content.
virtual ~MetaWeblog()
Destroy the object.
A class that represents a blog post on the server.
A class that can be used for access to Blogger 1.0 blogs.
Status of a media object successfully created on the server.
void setCategories(const QStringList &categories)
Sets the categories.
QString name() const
Returns the wished name.
QStringList categories() const
Returns the categories.
QByteArray data() const
Returns the data of the file.
bool isPrivate() const
Returns if the post is published or not.
Any other miscellaneous error.