• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KBlog Client Library

  • kblog
metaweblog.cpp
1 /*
2  This file is part of the kblog library.
3 
4  Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5  Copyright (c) 2006-2007 Christian Weilbach <christian_weilbach@web.de>
6  Copyright (c) 2007 Mike McQuaid <mike@mikemcquaid.com>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include "metaweblog.h"
25 #include "metaweblog_p.h"
26 #include "blogpost.h"
27 #include "blogmedia.h"
28 
29 #include <kxmlrpcclient/client.h>
30 #include <KDebug>
31 #include <KLocale>
32 #include <KDateTime>
33 #include <kstandarddirs.h>
34 
35 #include <QtCore/QFile>
36 #include <QtCore/QDataStream>
37 
38 using namespace KBlog;
39 
40 MetaWeblog::MetaWeblog( const KUrl &server, QObject *parent )
41  : Blogger1( server, *new MetaWeblogPrivate, parent )
42 {
43  kDebug();
44 }
45 
46 MetaWeblog::MetaWeblog( const KUrl &server, MetaWeblogPrivate &dd, QObject *parent )
47  : Blogger1( server, dd, parent )
48 {
49  kDebug();
50 }
51 
52 MetaWeblog::~MetaWeblog()
53 {
54  kDebug();
55 }
56 
57 QString MetaWeblog::interfaceName() const
58 {
59  return QLatin1String( "MetaWeblog" );
60 }
61 
62 void MetaWeblog::listCategories()
63 {
64  Q_D( 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)) );
71 }
72 
73 void MetaWeblog::createMedia( KBlog::BlogMedia *media )
74 {
75  Q_D( MetaWeblog );
76  if ( !media ) {
77  kError() << "MetaWeblog::createMedia: media is a null pointer";
78  emit error ( Other, i18n( "Media is a null pointer." ) );
79  return;
80  }
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  QList<QVariant> list;
87  map["name"] = media->name();
88  map["type"] = media->mimetype();
89  map["bits"] = media->data();
90  args << map;
91  d->mXmlRpcClient->call(
92  "metaWeblog.newMediaObject", args,
93  this, SLOT(slotCreateMedia(QList<QVariant>,QVariant)),
94  this, SLOT(slotError(int,QString,QVariant)),
95  QVariant( i ) );
96 
97 }
98 
99 MetaWeblogPrivate::MetaWeblogPrivate()
100 {
101  kDebug();
102  mCallMediaCounter=1;
103  mCatLoaded=false;
104 }
105 
106 MetaWeblogPrivate::~MetaWeblogPrivate()
107 {
108  kDebug();
109 }
110 
111 QList<QVariant> MetaWeblogPrivate::defaultArgs( const QString &id )
112 {
113  Q_Q( MetaWeblog );
114  QList<QVariant> args;
115  if ( !id.isEmpty() ) {
116  args << QVariant( id );
117  }
118  args << QVariant( q->username() )
119  << QVariant( q->password() );
120  return args;
121 }
122 
123 void MetaWeblogPrivate::loadCategories()
124 {
125  kDebug();
126 
127  if ( mCatLoaded ) {
128  return;
129  }
130  mCatLoaded = true;
131 
132  if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
133  kDebug() << "We need at least url, blogId and the username to create a unique filename.";
134  return;
135  }
136 
137  QString filename = "kblog/" + mUrl.host() + '_' + mBlogId + '_' + mUsername;
138  filename = KStandardDirs::locateLocal( "data", filename, true );
139 
140  QFile file( filename );
141  if ( !file.open( QIODevice::ReadOnly ) ) {
142  kDebug() << "Cannot open cached categories file: " << filename;
143  return;
144  }
145 
146  QDataStream stream( &file );
147  stream >> mCategoriesList;
148  file.close();
149 }
150 
151 void MetaWeblogPrivate::saveCategories()
152 {
153  kDebug();
154  if ( mUrl.isEmpty() || mBlogId.isEmpty() || mUsername.isEmpty() ) {
155  kDebug() << "We need at least url, blogId and the username to create a unique filename.";
156  return;
157  }
158 
159  QString filename = "kblog/" + mUrl.host() + '_' + mBlogId + '_' + mUsername;
160  filename = KStandardDirs::locateLocal( "data", filename, true );
161 
162  QFile file( filename );
163  if ( !file.open( QIODevice::WriteOnly ) ) {
164  kDebug() << "Cannot open cached categories file: " << filename;
165  return;
166  }
167 
168  QDataStream stream( &file );
169  stream << mCategoriesList;
170  file.close();
171 }
172 
173 
174 void MetaWeblogPrivate::slotListCategories( const QList<QVariant> &result,
175  const QVariant &id )
176 {
177  Q_Q( MetaWeblog );
178  Q_UNUSED( id );
179 
180  kDebug() << "MetaWeblogPrivate::slotListCategories";
181  kDebug() << "TOP:" << result[0].typeName();
182  if ( result[0].type() != QVariant::Map &&
183  result[0].type() != QVariant::List ) {
184  // include fix for not metaweblog standard compatible apis with
185  // array of structs instead of struct of structs, e.g. wordpress
186  kError() << "Could not list categories out of the result from the server.";
187  emit q->error( MetaWeblog::ParsingError,
188  i18n( "Could not list categories out of the result "
189  "from the server." ) );
190  } else {
191  if ( result[0].type() == QVariant::Map ) {
192  const QMap<QString, QVariant> serverMap = result[0].toMap();
193  const QList<QString> serverKeys = serverMap.keys();
194 
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 );
208  }
209  kDebug() << "Emitting listedCategories";
210  emit q->listedCategories( mCategoriesList );
211  }
212  }
213  if ( result[0].type() == QVariant::List ) {
214  // include fix for not metaweblog standard compatible apis with
215  // array of structs instead of struct of structs, e.g. wordpress
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 );
230  }
231  kDebug() << "Emitting listedCategories()";
232  emit q->listedCategories( mCategoriesList );
233  }
234  saveCategories();
235 }
236 
237 void MetaWeblogPrivate::slotCreateMedia( const QList<QVariant> &result,
238  const QVariant &id )
239 {
240  Q_Q( MetaWeblog );
241 
242  KBlog::BlogMedia *media = mCallMediaMap[ id.toInt() ];
243  mCallMediaMap.remove( id.toInt() );
244 
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.";
249  emit q->errorMedia( MetaWeblog::ParsingError,
250  i18n( "Could not read the result, not a map." ),
251  media );
252  return;
253  }
254  const QMap<QString, QVariant> resultStruct = result[0].toMap();
255  const QString url = resultStruct["url"].toString();
256  kDebug() << "MetaWeblog::slotCreateMedia url=" << url;
257 
258  if ( !url.isEmpty() ) {
259  media->setUrl( KUrl( url ) );
260  media->setStatus( BlogMedia::Created );
261  kDebug() << "Emitting createdMedia( url=" << url << ");";
262  emit q->createdMedia( media );
263  }
264 }
265 
266 bool MetaWeblogPrivate::readPostFromMap( BlogPost *post,
267  const QMap<QString, QVariant> &postInfo )
268 {
269  // FIXME: integrate error handling
270  kDebug() << "readPostFromMap()";
271  if ( !post ) {
272  return false;
273  }
274  QStringList mapkeys = postInfo.keys();
275  kDebug() << endl << "Keys:" << mapkeys.join( ", " );
276  kDebug() << endl;
277 
278  KDateTime dt =
279  KDateTime( postInfo["dateCreated"].toDateTime(), KDateTime::UTC );
280  if ( dt.isValid() && !dt.isNull() ) {
281  post->setCreationDateTime( dt.toLocalZone() );
282  }
283 
284  dt =
285  KDateTime( postInfo["lastModified"].toDateTime(), KDateTime::UTC );
286  if ( dt.isValid() && !dt.isNull() ) {
287  post->setModificationDateTime( dt.toLocalZone() );
288  }
289 
290  post->setPostId( postInfo["postid"].toString().isEmpty() ? postInfo["postId"].toString() :
291  postInfo["postid"].toString() );
292 
293  QString title( postInfo["title"].toString() );
294  QString description( postInfo["description"].toString() );
295  QStringList categories( postInfo["categories"].toStringList() );
296 
297  post->setTitle( title );
298  post->setContent( description );
299  if ( !categories.isEmpty() ) {
300  kDebug() << "Categories:" << categories;
301  post->setCategories( categories );
302  }
303  return true;
304 }
305 
306 bool MetaWeblogPrivate::readArgsFromPost( QList<QVariant> *args, const BlogPost &post )
307 {
308  if ( !args ) {
309  return false;
310  }
311  QMap<QString, QVariant> map;
312  map["categories"] = post.categories();
313  map["description"] = post.content();
314  map["title"] = post.title();
315  map["lastModified"] = post.modificationDateTime().dateTime().toUTC();
316  map["dateCreated"] = post.creationDateTime().dateTime().toUTC();
317  *args << map;
318  *args << QVariant( !post.isPrivate() );
319  return true;
320 }
321 
322 QString MetaWeblogPrivate::getCallFromFunction( FunctionToCall type )
323 {
324  switch ( 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();
330  }
331 }
332 #include "moc_metaweblog.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:24:42 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KBlog Client Library

Skip menu "KBlog Client Library"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal