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 QLatin1String(
"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;
86 map[QLatin1String(
"name")] = media->
name();
87 map[QLatin1String(
"type")] = media->
mimetype();
88 map[QLatin1String(
"bits")] = media->
data();
90 d->mXmlRpcClient->call(
91 QLatin1String(
"metaWeblog.newMediaObject"), args,
92 this, SLOT(slotCreateMedia(QList<QVariant>,QVariant)),
93 this, SLOT(slotError(
int,QString,QVariant)),
98 MetaWeblogPrivate::MetaWeblogPrivate()
105 MetaWeblogPrivate::~MetaWeblogPrivate()
110 QList<QVariant> MetaWeblogPrivate::defaultArgs(
const QString &
id )
113 QList<QVariant> args;
114 if ( !
id.isEmpty() ) {
115 args << QVariant(
id );
117 args << QVariant( q->username() )
118 << QVariant( q->password() );
122 void MetaWeblogPrivate::loadCategories()
131 if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
132 kDebug() <<
"We need at least url, blogId and the username to create a unique filename.";
136 QString filename = QLatin1String(
"kblog/") + mUrl.host() + QLatin1Char(
'_') + mBlogId + QLatin1Char(
'_') + mUsername;
137 filename = KStandardDirs::locateLocal(
"data", filename,
true );
139 QFile file( filename );
140 if ( !file.open( QIODevice::ReadOnly ) ) {
141 kDebug() <<
"Cannot open cached categories file: " << filename;
145 QDataStream stream( &file );
146 stream >> mCategoriesList;
150 void MetaWeblogPrivate::saveCategories()
153 if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
154 kDebug() <<
"We need at least url, blogId and the username to create a unique filename.";
158 QString filename = QLatin1String(
"kblog/") + mUrl.host() + QLatin1Char(
'_') + mBlogId + QLatin1Char(
'_') + mUsername;
159 filename = KStandardDirs::locateLocal(
"data", filename,
true );
161 QFile file( filename );
162 if ( !file.open( QIODevice::WriteOnly ) ) {
163 kDebug() <<
"Cannot open cached categories file: " << filename;
167 QDataStream stream( &file );
168 stream << mCategoriesList;
172 void MetaWeblogPrivate::slotListCategories(
const QList<QVariant> &result,
178 kDebug() <<
"MetaWeblogPrivate::slotListCategories";
179 kDebug() <<
"TOP:" << result[0].typeName();
180 if ( result[0].type() != QVariant::Map &&
181 result[0].type() != QVariant::List ) {
184 kError() <<
"Could not list categories out of the result from the server.";
186 i18n(
"Could not list categories out of the result "
187 "from the server." ) );
189 if ( result[0].type() == QVariant::Map ) {
190 const QMap<QString, QVariant> serverMap = result[0].toMap();
191 const QList<QString> serverKeys = serverMap.keys();
193 QList<QString>::ConstIterator it = serverKeys.begin();
194 QList<QString>::ConstIterator end = serverKeys.end();
195 for ( ; it != end; ++it ) {
196 kDebug() <<
"MIDDLE:" << ( *it );
197 QMap<QString,QString> category;
198 const QMap<QString, QVariant> serverCategory = serverMap[*it].toMap();
199 category[QLatin1String(
"name")]= ( *it );
200 category[QLatin1String(
"description")] = serverCategory[ QLatin1String(
"description") ].toString();
201 category[QLatin1String(
"htmlUrl")] = serverCategory[ QLatin1String(
"htmlUrl") ].toString();
202 category[QLatin1String(
"rssUrl")] = serverCategory[ QLatin1String(
"rssUrl") ].toString();
203 category[QLatin1String(
"categoryId")] = serverCategory[ QLatin1String(
"categoryId") ].toString();
204 category[QLatin1String(
"parentId")] = serverCategory[ QLatin1String(
"parentId") ].toString();
205 mCategoriesList.append( category );
207 kDebug() <<
"Emitting listedCategories";
208 emit q->listedCategories( mCategoriesList );
211 if ( result[0].type() == QVariant::List ) {
214 const QList<QVariant> serverList = result[0].toList();
215 QList<QVariant>::ConstIterator it = serverList.begin();
216 QList<QVariant>::ConstIterator end = serverList.end();
217 for ( ; it != end; ++it ) {
218 kDebug() <<
"MIDDLE:" << ( *it ).typeName();
219 QMap<QString,QString> category;
220 const QMap<QString, QVariant> serverCategory = ( *it ).toMap();
221 category[ QLatin1String(
"name") ] = serverCategory[QLatin1String(
"categoryName")].toString();
222 category[QLatin1String(
"description")] = serverCategory[ QLatin1String(
"description") ].toString();
223 category[QLatin1String(
"htmlUrl")] = serverCategory[ QLatin1String(
"htmlUrl") ].toString();
224 category[QLatin1String(
"rssUrl")] = serverCategory[ QLatin1String(
"rssUrl") ].toString();
225 category[QLatin1String(
"categoryId")] = serverCategory[ QLatin1String(
"categoryId") ].toString();
226 category[QLatin1String(
"parentId")] = serverCategory[ QLatin1String(
"parentId") ].toString();
227 mCategoriesList.append( category );
229 kDebug() <<
"Emitting listedCategories()";
230 emit q->listedCategories( mCategoriesList );
235 void MetaWeblogPrivate::slotCreateMedia(
const QList<QVariant> &result,
241 mCallMediaMap.remove(
id.toInt() );
243 kDebug() <<
"MetaWeblogPrivate::slotCreateMedia, no error!";
244 kDebug() <<
"TOP:" << result[0].typeName();
245 if ( result[0].type() != 8 ) {
246 kError() <<
"Could not read the result, not a map.";
248 i18n(
"Could not read the result, not a map." ),
252 const QMap<QString, QVariant> resultStruct = result[0].toMap();
253 const QString url = resultStruct[QLatin1String(
"url")].toString();
254 kDebug() <<
"MetaWeblog::slotCreateMedia url=" << url;
256 if ( !url.isEmpty() ) {
257 media->
setUrl( KUrl( url ) );
259 kDebug() <<
"Emitting createdMedia( url=" << url <<
");";
260 emit q->createdMedia( media );
264 bool MetaWeblogPrivate::readPostFromMap(
BlogPost *post,
265 const QMap<QString, QVariant> &postInfo )
268 kDebug() <<
"readPostFromMap()";
272 QStringList mapkeys = postInfo.keys();
273 kDebug() << endl <<
"Keys:" << mapkeys.join( QLatin1String(
", ") );
277 KDateTime( postInfo[QLatin1String(
"dateCreated")].toDateTime(), KDateTime::UTC );
278 if ( dt.isValid() && !dt.isNull() ) {
283 KDateTime( postInfo[QLatin1String(
"lastModified")].toDateTime(), KDateTime::UTC );
284 if ( dt.isValid() && !dt.isNull() ) {
288 post->
setPostId( postInfo[QLatin1String(
"postid")].toString().isEmpty() ? postInfo[QLatin1String(
"postId")].toString() :
289 postInfo[QLatin1String(
"postid")].toString() );
291 QString title( postInfo[QLatin1String(
"title")].toString() );
292 QString description( postInfo[QLatin1String(
"description")].toString() );
293 QStringList categories( postInfo[QLatin1String(
"categories")].toStringList() );
297 if ( !categories.isEmpty() ) {
298 kDebug() <<
"Categories:" << categories;
304 bool MetaWeblogPrivate::readArgsFromPost( QList<QVariant> *args,
const BlogPost &post )
309 QMap<QString, QVariant> map;
310 map[QLatin1String(
"categories")] = post.
categories();
311 map[QLatin1String(
"description")] = post.
content();
312 map[QLatin1String(
"title")] = post.
title();
314 map[QLatin1String(
"dateCreated")] = post.
creationDateTime().dateTime().toUTC();
320 QString MetaWeblogPrivate::getCallFromFunction( FunctionToCall type )
323 case GetRecentPosts:
return QLatin1String(
"metaWeblog.getRecentPosts");
324 case CreatePost:
return QLatin1String(
"metaWeblog.newPost");
325 case ModifyPost:
return QLatin1String(
"metaWeblog.editPost");
326 case FetchPost:
return QLatin1String(
"metaWeblog.getPost");
327 default:
return QString();
330 #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.