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

KBlog Client Library

  • kblog
blogger1.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-2008 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 "blogger1.h"
25 #include "blogger1_p.h"
26 #include "blogpost.h"
27 
28 #include <kxmlrpcclient/client.h>
29 
30 #include <KDebug>
31 #include <KDateTime>
32 #include <KLocale>
33 
34 #include <QList>
35 
36 #include <QStringList>
37 
38 using namespace KBlog;
39 
40 Blogger1::Blogger1( const KUrl &server, QObject *parent )
41  : Blog( server, *new Blogger1Private, parent )
42 {
43  kDebug();
44  setUrl( server );
45 }
46 
47 Blogger1::Blogger1( const KUrl &server, Blogger1Private &dd, QObject *parent )
48  : Blog( server, dd, parent )
49 {
50  kDebug();
51  setUrl( server );
52 }
53 
54 Blogger1::~Blogger1()
55 {
56  kDebug();
57 }
58 
59 QString Blogger1::interfaceName() const
60 {
61  return QLatin1String( "Blogger 1.0" );
62 }
63 
64 void Blogger1::setUrl( const KUrl &server )
65 {
66  Q_D( Blogger1 );
67  Blog::setUrl( server );
68  delete d->mXmlRpcClient;
69  d->mXmlRpcClient = new KXmlRpc::Client( server );
70  d->mXmlRpcClient->setUserAgent( userAgent() );
71 }
72 
73 void Blogger1::fetchUserInfo()
74 {
75  Q_D( Blogger1 );
76  kDebug() << "Fetch user's info...";
77  QList<QVariant> args( d->blogger1Args() );
78  d->mXmlRpcClient->call(
79  "blogger.getUserInfo", args,
80  this, SLOT(slotFetchUserInfo(QList<QVariant>,QVariant)),
81  this, SLOT(slotError(int,QString,QVariant)) );
82 }
83 
84 void Blogger1::listBlogs()
85 {
86  Q_D( Blogger1 );
87  kDebug() << "Fetch List of Blogs...";
88  QList<QVariant> args( d->blogger1Args() );
89  d->mXmlRpcClient->call(
90  "blogger.getUsersBlogs", args,
91  this, SLOT(slotListBlogs(QList<QVariant>,QVariant)),
92  this, SLOT(slotError(int,QString,QVariant)) );
93 }
94 
95 void Blogger1::listRecentPosts( int number )
96 {
97  Q_D( Blogger1 );
98  kDebug() << "Fetching List of Posts...";
99  QList<QVariant> args( d->defaultArgs( blogId() ) );
100  args << QVariant( number );
101  d->mXmlRpcClient->call(
102  d->getCallFromFunction( Blogger1Private::GetRecentPosts ), args,
103  this, SLOT(slotListRecentPosts(QList<QVariant>,QVariant)),
104  this, SLOT(slotError(int,QString,QVariant)),
105  QVariant( number ) );
106 }
107 
108 void Blogger1::fetchPost( KBlog::BlogPost *post )
109 {
110  if ( !post ) {
111  kError() << "Blogger1::modifyPost: post is null pointer";
112  return;
113  }
114 
115  Q_D( Blogger1 );
116  kDebug() << "Fetching Post with url" << post->postId();
117  QList<QVariant> args( d->defaultArgs( post->postId() ) );
118  unsigned int i= d->mCallCounter++;
119  d->mCallMap[ i ] = post;
120  d->mXmlRpcClient->call(
121  d->getCallFromFunction( Blogger1Private::FetchPost ), args,
122  this, SLOT(slotFetchPost(QList<QVariant>,QVariant)),
123  this, SLOT(slotError(int,QString,QVariant)),
124  QVariant( i ) );
125 }
126 
127 void Blogger1::modifyPost( KBlog::BlogPost *post )
128 {
129  Q_D( Blogger1 );
130 
131  if ( !post ) {
132  kError() << "Blogger1::modifyPost: post is null pointer";
133  return;
134  }
135 
136  kDebug() << "Uploading Post with postId" << post->postId();
137  unsigned int i= d->mCallCounter++;
138  d->mCallMap[ i ] = post;
139  QList<QVariant> args( d->defaultArgs( post->postId() ) );
140  d->readArgsFromPost( &args, *post );
141  d->mXmlRpcClient->call(
142  d->getCallFromFunction( Blogger1Private::ModifyPost ), args,
143  this, SLOT(slotModifyPost(QList<QVariant>,QVariant)),
144  this, SLOT(slotError(int,QString,QVariant)),
145  QVariant( i ) );
146 }
147 
148 void Blogger1::createPost( KBlog::BlogPost *post )
149 {
150  Q_D( Blogger1 );
151 
152  if ( !post ) {
153  kError() << "Blogger1::createPost: post is null pointer";
154  return;
155  }
156 
157  unsigned int i= d->mCallCounter++;
158  d->mCallMap[ i ] = post;
159  kDebug() << "Creating new Post with blogid" << blogId();
160  QList<QVariant> args( d->defaultArgs( blogId() ) );
161  d->readArgsFromPost( &args, *post );
162  d->mXmlRpcClient->call(
163  d->getCallFromFunction( Blogger1Private::CreatePost ), args,
164  this, SLOT(slotCreatePost(QList<QVariant>,QVariant)),
165  this, SLOT(slotError(int,QString,QVariant)),
166  QVariant( i ) );
167 }
168 
169 void Blogger1::removePost( KBlog::BlogPost *post )
170 {
171  Q_D( Blogger1 );
172 
173  if ( !post ) {
174  kError() << "Blogger1::removePost: post is null pointer";
175  return;
176  }
177 
178  unsigned int i = d->mCallCounter++;
179  d->mCallMap[ i ] = post;
180  kDebug() << "Blogger1::removePost: postId=" << post->postId();
181  QList<QVariant> args( d->blogger1Args( post->postId() ) );
182  args << QVariant( true ); // Publish must be set to remove post.
183  d->mXmlRpcClient->call(
184  "blogger.deletePost", args,
185  this, SLOT(slotRemovePost(QList<QVariant>,QVariant)),
186  this, SLOT(slotError(int,QString,QVariant)),
187  QVariant( i ) );
188 }
189 
190 Blogger1Private::Blogger1Private() :
191 mXmlRpcClient(0)
192 {
193  kDebug();
194  mCallCounter = 1;
195 }
196 
197 Blogger1Private::~Blogger1Private()
198 {
199  kDebug();
200  delete mXmlRpcClient;
201 }
202 
203 QList<QVariant> Blogger1Private::defaultArgs( const QString &id )
204 {
205  kDebug();
206  Q_Q ( Blogger1 );
207  QList<QVariant> args;
208  args << QVariant( QString( "0123456789ABCDEF" ) );
209  if ( !id.isEmpty() ) {
210  args << QVariant( id );
211  }
212  args << QVariant( q->username() )
213  << QVariant( q->password() );
214  return args;
215 }
216 
217 // reimplemenet defaultArgs, since we may not use it virtually everywhere
218 QList<QVariant> Blogger1Private::blogger1Args( const QString &id )
219 {
220  kDebug();
221  Q_Q ( Blogger1 );
222  QList<QVariant> args;
223  args << QVariant( QString( "0123456789ABCDEF" ) );
224  if ( !id.isEmpty() ) {
225  args << QVariant( id );
226  }
227  args << QVariant( q->username() )
228  << QVariant( q->password() );
229  return args;
230 }
231 
232 void Blogger1Private::slotFetchUserInfo( const QList<QVariant> &result, const QVariant &id )
233 {
234  Q_Q( Blogger1 );
235  Q_UNUSED( id );
236 
237  kDebug();
238  kDebug() << "TOP:" << result[0].typeName();
239  QMap<QString,QString> userInfo;
240  if ( result[0].type() != QVariant::Map ) {
241  kError() << "Could not fetch user's info out of the result from the server,"
242  << "not a map.";
243  emit q->error( Blogger1::ParsingError,
244  i18n( "Could not fetch user's info out of the result "
245  "from the server, not a map." ) );
246  return;
247  }
248  const QMap<QString,QVariant> resultMap = result[0].toMap();
249  userInfo["nickname"]=resultMap["nickname"].toString();
250  userInfo["userid"]=resultMap["userid"].toString();
251  userInfo["url"]=resultMap["url"].toString();
252  userInfo["email"]=resultMap["email"].toString();
253  userInfo["lastname"]=resultMap["lastname"].toString();
254  userInfo["firstname"]=resultMap["firstname"].toString();
255 
256  emit q->fetchedUserInfo( userInfo );
257 }
258 
259 void Blogger1Private::slotListBlogs( const QList<QVariant> &result, const QVariant &id )
260 {
261  Q_Q( Blogger1 );
262  Q_UNUSED( id );
263 
264  kDebug();
265  kDebug() << "TOP:" << result[0].typeName();
266  QList<QMap<QString,QString> > blogsList;
267  if ( result[0].type() != QVariant::List ) {
268  kError() << "Could not fetch blogs out of the result from the server,"
269  << "not a list.";
270  emit q->error( Blogger1::ParsingError,
271  i18n( "Could not fetch blogs out of the result "
272  "from the server, not a list." ) );
273  return;
274  }
275  const QList<QVariant> posts = result[0].toList();
276  QList<QVariant>::ConstIterator it = posts.begin();
277  QList<QVariant>::ConstIterator end = posts.end();
278  for ( ; it != end; ++it ) {
279  kDebug() << "MIDDLE:" << ( *it ).typeName();
280  const QMap<QString, QVariant> postInfo = ( *it ).toMap();
281  QMap<QString,QString> blogInfo;
282  blogInfo[ "id" ] = postInfo["blogid"].toString();
283  blogInfo[ "url" ] = postInfo["url"].toString();
284  blogInfo[ "apiUrl" ] = postInfo["xmlrpc"].toString();
285  blogInfo[ "title" ] = postInfo["blogName"].toString();
286  kDebug() << "Blog information retrieved: ID =" << blogInfo["id"]
287  << ", Name =" << blogInfo["title"];
288  blogsList << blogInfo;
289  }
290  emit q->listedBlogs( blogsList );
291 }
292 
293 void Blogger1Private::slotListRecentPosts( const QList<QVariant> &result, const QVariant &id )
294 {
295  Q_Q( Blogger1 );
296  int count = id.toInt(); // not sure if needed, actually the API should
297 // not give more posts
298 
299  kDebug();
300  kDebug() << "TOP:" << result[0].typeName();
301 
302  QList <BlogPost> fetchedPostList;
303 
304  if ( result[0].type() != QVariant::List ) {
305  kError() << "Could not fetch list of posts out of the"
306  << "result from the server, not a list.";
307  emit q->error( Blogger1::ParsingError,
308  i18n( "Could not fetch list of posts out of the result "
309  "from the server, not a list." ) );
310  return;
311  }
312  const QList<QVariant> postReceived = result[0].toList();
313  QList<QVariant>::ConstIterator it = postReceived.begin();
314  QList<QVariant>::ConstIterator end = postReceived.end();
315  for ( ; it != end; ++it ) {
316  BlogPost post;
317  kDebug() << "MIDDLE:" << ( *it ).typeName();
318  const QMap<QString, QVariant> postInfo = ( *it ).toMap();
319  if ( readPostFromMap( &post, postInfo ) ) {
320  kDebug() << "Post with ID:"
321  << post.postId()
322  << "appended in fetchedPostList";
323  post.setStatus( BlogPost::Fetched );
324  fetchedPostList.append( post );
325  } else {
326  kError() << "readPostFromMap failed!";
327  emit q->error( Blogger1::ParsingError, i18n( "Could not read post." ) );
328  }
329  if ( --count == 0 ) {
330  break;
331  }
332  }
333  kDebug() << "Emitting listRecentPostsFinished()";
334  emit q->listedRecentPosts( fetchedPostList );
335 }
336 
337 void Blogger1Private::slotFetchPost( const QList<QVariant> &result, const QVariant &id )
338 {
339  Q_Q( Blogger1 );
340  kDebug();
341 
342  KBlog::BlogPost *post = mCallMap[ id.toInt() ];
343  mCallMap.remove( id.toInt() );
344 
345  //array of structs containing ISO.8601
346  // dateCreated, String userid, String postid, String content;
347  // TODO: Time zone for the dateCreated!
348  kDebug () << "TOP:" << result[0].typeName();
349  if ( result[0].type() == QVariant::Map &&
350  readPostFromMap( post, result[0].toMap() ) ) {
351  kDebug() << "Emitting fetchedPost()";
352  post->setStatus( KBlog::BlogPost::Fetched );
353  emit q->fetchedPost( post );
354  } else {
355  kError() << "Could not fetch post out of the result from the server.";
356  post->setError( i18n( "Could not fetch post out of the result from the server." ) );
357  post->setStatus( BlogPost::Error );
358  emit q->errorPost( Blogger1::ParsingError,
359  i18n( "Could not fetch post out of the result from the server." ), post );
360  }
361 }
362 
363 void Blogger1Private::slotCreatePost( const QList<QVariant> &result, const QVariant &id )
364 {
365  Q_Q( Blogger1 );
366  KBlog::BlogPost *post = mCallMap[ id.toInt() ];
367  mCallMap.remove( id.toInt() );
368 
369  kDebug();
370  //array of structs containing ISO.8601
371  // dateCreated, String userid, String postid, String content;
372  // TODO: Time zone for the dateCreated!
373  kDebug () << "TOP:" << result[0].typeName();
374  if ( result[0].type() != QVariant::String &&
375  result[0].type() != QVariant::Int ) {
376  kError() << "Could not read the postId, not a string or an integer.";
377  emit q->errorPost( Blogger1::ParsingError,
378  i18n( "Could not read the postId, not a string or an integer." ),
379  post );
380  return;
381  }
382  QString serverID;
383  if ( result[0].type() == QVariant::String ) {
384  serverID = result[0].toString();
385  }
386  if ( result[0].type() == QVariant::Int ) {
387  serverID = QString( "%1" ).arg( result[0].toInt() );
388  }
389  post->setPostId( serverID );
390  post->setStatus( KBlog::BlogPost::Created );
391  kDebug() << "emitting createdPost()"
392  << "for title: \"" << post->title()
393  << "\" server id: " << serverID;
394  emit q->createdPost( post );
395 }
396 
397 void Blogger1Private::slotModifyPost( const QList<QVariant> &result, const QVariant &id )
398 {
399  Q_Q( Blogger1 );
400  KBlog::BlogPost *post = mCallMap[ id.toInt() ];
401  mCallMap.remove( id.toInt() );
402 
403  kDebug();
404  //array of structs containing ISO.8601
405  // dateCreated, String userid, String postid, String content;
406  // TODO: Time zone for the dateCreated!
407  kDebug() << "TOP:" << result[0].typeName();
408  if ( result[0].type() != QVariant::Bool &&
409  result[0].type() != QVariant::Int ) {
410  kError() << "Could not read the result, not a boolean.";
411  emit q->errorPost( Blogger1::ParsingError,
412  i18n( "Could not read the result, not a boolean." ),
413  post );
414  return;
415  }
416  post->setStatus( KBlog::BlogPost::Modified );
417  kDebug() << "emitting modifiedPost() for title: \""
418  << post->title() << "\"";
419  emit q->modifiedPost( post );
420 }
421 
422 void Blogger1Private::slotRemovePost( const QList<QVariant> &result, const QVariant &id )
423 {
424  Q_Q( Blogger1 );
425  KBlog::BlogPost *post = mCallMap[ id.toInt() ];
426  mCallMap.remove( id.toInt() );
427 
428  kDebug() << "slotRemovePost";
429  //array of structs containing ISO.8601
430  // dateCreated, String userid, String postid, String content;
431  // TODO: Time zone for the dateCreated!
432  kDebug() << "TOP:" << result[0].typeName();
433  if ( result[0].type() != QVariant::Bool &&
434  result[0].type() != QVariant::Int ) {
435  kError() << "Could not read the result, not a boolean.";
436  emit q->errorPost( Blogger1::ParsingError,
437  i18n( "Could not read the result, not a boolean." ),
438  post );
439  return;
440  }
441  post->setStatus( KBlog::BlogPost::Removed );
442  kDebug() << "emitting removedPost()";
443  emit q->removedPost( post );
444 }
445 
446 void Blogger1Private::slotError( int number,
447  const QString &errorString,
448  const QVariant &id )
449 {
450  Q_Q( Blogger1 );
451  Q_UNUSED( number );
452  kDebug() << "An error occurred: " << errorString;
453  BlogPost *post = mCallMap[ id.toInt() ];
454 
455  if ( post )
456  emit q->errorPost( Blogger1::XmlRpc, errorString, post );
457  else
458  emit q->error( Blogger1::XmlRpc, errorString );
459 }
460 
461 bool Blogger1Private::readPostFromMap(
462  BlogPost *post, const QMap<QString, QVariant> &postInfo )
463 {
464  // FIXME: integrate error handling
465  if ( !post ) {
466  return false;
467  }
468  QStringList mapkeys = postInfo.keys();
469  kDebug() << endl << "Keys:" << mapkeys.join( ", " );
470  kDebug() << endl;
471 
472  KDateTime dt( postInfo["dateCreated"].toDateTime(), KDateTime::UTC );
473  if ( dt.isValid() && !dt.isNull() ) {
474  post->setCreationDateTime( dt.toLocalZone() );
475  }
476  dt = KDateTime ( postInfo["lastModified"].toDateTime(), KDateTime::UTC );
477  if ( dt.isValid() && !dt.isNull() ) {
478  post->setModificationDateTime( dt.toLocalZone() );
479  }
480  post->setPostId( postInfo["postid"].toString().isEmpty() ? postInfo["postId"].toString() :
481  postInfo["postid"].toString() );
482 
483  QString title( postInfo["title"].toString() );
484  QString description( postInfo["description"].toString() );
485  QString contents;
486  if ( postInfo["content"].type() == QVariant::ByteArray ) {
487  QByteArray tmpContent = postInfo["content"].toByteArray();
488  contents = QString::fromUtf8( tmpContent.data(), tmpContent.size() );
489  } else {
490  contents = postInfo["content"].toString();
491  }
492  QStringList category;
493 
494  // Check for hacked title/category support (e.g. in Wordpress)
495  QRegExp titleMatch = QRegExp( "<title>([^<]*)</title>" );
496  QRegExp categoryMatch = QRegExp( "<category>([^<]*)</category>" );
497  if ( contents.indexOf( titleMatch ) != -1 ) {
498  // Get the title value from the regular expression match
499  title = titleMatch.cap( 1 );
500  }
501  if ( contents.indexOf( categoryMatch ) != -1 ) {
502  // Get the category value from the regular expression match
503  category = categoryMatch.capturedTexts();
504  }
505  contents.remove( titleMatch );
506  contents.remove( categoryMatch );
507 
508  post->setTitle( title );
509  post->setContent( contents );
510  post->setCategories( category );
511  return true;
512 }
513 
514 bool Blogger1Private::readArgsFromPost( QList<QVariant> *args, const BlogPost &post )
515 {
516  if ( !args ) {
517  return false;
518  }
519  QStringList categories = post.categories();
520  QString content = "<title>" + post.title() + "</title>";
521  QStringList::const_iterator it;
522  for ( it = categories.constBegin(); it != categories.constEnd(); ++it ) {
523  content += "<category>" + *it + "</category>";
524  }
525  content += post.content();
526  *args << QVariant( content );
527  *args << QVariant( !post.isPrivate() );
528  return true;
529 }
530 
531 QString Blogger1Private::getCallFromFunction( FunctionToCall type )
532 {
533  switch ( type ) {
534  case GetRecentPosts: return "blogger.getRecentPosts";
535  case CreatePost: return "blogger.newPost";
536  case ModifyPost: return "blogger.editPost";
537  case FetchPost: return "blogger.getPost";
538  default: return QString();
539  }
540 }
541 
542 #include "moc_blogger1.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