Warning: That file was not part of the compilation database. It may have many parsing errors.

1/*
2 This file is part of the kblog library.
3
4 Copyright (c) 2007-2008 Mike McQuaid <mike@mikemcquaid.com>
5 Copyright (c) 2007 Christian Weilbach <christian_weilbach@web.de>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "livejournal.h"
24#include "livejournal_p.h"
25#include "blogpost.h"
26
27#include <kxmlrpcclient/client.h>
28
29#include <KDebug>
30#include <KLocalizedString>
31#include <KDateTime>
32
33using namespace KBlog;
34
35LiveJournal::LiveJournal( const KUrl &server, QObject *parent )
36 : Blog( server, *new LiveJournalPrivate, parent )
37{
38 setUrl( server );
39}
40
41LiveJournal::~LiveJournal()
42{
43}
44
45void LiveJournal::addFriend( const QString &username, int group,
46 const QColor &fgcolor, const QColor &bgcolor )
47{
48 // LJ.XMLRPC.editfriends
49 Q_D( LiveJournal ); // Enable d-pointer access to the LiveJournalPrivate object
50 unsigned int i = d->mCallCounter++; // Add one to the call counter and assign it
51 d->mCallMapAddFriend[ i ] = username; // Put the post in the map at location i
52 kDebug() << "LiveJournal::addFriend(): username: "
53 << username; // Send a message to the console to state which method we have entered.
54 QList<QVariant> args; // Create the argument list, in this case will just contain the map.
55 QMap<QString,QVariant> map( d->defaultArgs() ); // Create the initial map from the default arguments.
56 QList<QVariant> users;
57 QMap<QString,QVariant> user;
58 user.insert( "username", username );
59 user.insert( "group", group );
60 user.insert( "fgcolor", fgcolor );
61 user.insert( "bgcolor", bgcolor );
62 users << user;
63 map.insert( "add", users );
64 args << map;
65 d->mXmlRpcClient->call( "LJ.XMLRPC.editfriends", // The XML-RPC procedure to call.
66 args, // A list containing all the arguments to pass to the procedure.
67 this, // The object containing the slot to use on success.
68 SLOT(slotAddFriend(QList<QVariant>,QVariant)), // The slot to call on success.
69 this, // The object containing the slot to call on failure.
70 SLOT(slotError(int,QString,QVariant)), // The slot to call on failure
71 QVariant( i ) ); // The ID, as we haven't created a post, the location in the map.
72}
73
74void LiveJournal::assignFriendToCategory ( const QString &username, int category )
75{
76 Q_UNUSED( username );
77 Q_UNUSED( category );
78 //TODO
79 // LJ.XMLRPC.editfriendgroups
80}
81
82void LiveJournal::createPost( KBlog::BlogPost *post )
83{
84 Q_D( LiveJournal ); // Enable d-pointer access to the LiveJournalPrivate object
85 if ( !post ) { // Check if post has a valid memory address (>0)
86 kError() << "LiveJournal::createPost: post is null pointer"; // If it doesn't print an error to the console.
87 return; // If it does not, exit the method
88 }
89 unsigned int i = d->mCallCounter++; // Add one to the call counter and assign it
90 d->mCallMap[ i ] = post; // Put the post in the map at location i
91 kDebug() << "LiveJournal::createPost()"; // Send a message to the console to state which method we have entered.
92 QList<QVariant> args; // Create the argument list, in this case will just contain the map.
93 QMap<QString,QVariant> map( d->defaultArgs() ); // Create the initial map from the default arguments.
94 map.insert( "lineendings", "pc" ); // PC line endings
95 map.insert( "event", post->content() ); // Insert the post's content into the struct.
96 map.insert( "subject", post->title() ); // Insert the post's subject into the struct.
97 // TODO map.insert( "allowmask", post->categories() ); // We want to use the allowmask to use categories/tags
98 KDateTime date = post->creationDateTime(); // Get the date of the post's creation
99 int year = date.toString( "%Y" ).toInt(); // Get the year from the date using a format string and converting string to an integer
100 int month = date.toString( "%m" ).toInt(); // Get the month from the date using a format string and converting string to an integer
101 int day = date.toString( "%d" ).toInt(); // Get the day from the date using a format string and converting string to an integer
102 int hour = date.toString( "%H" ).toInt(); // Get the hour from the date using a format string and converting string to an integer
103 int minute = date.toString( "%M" ).toInt(); // Get the minute from the date using a format string and converting string to an integer
104 map.insert( "year", year ); // Insert the year into the struct.
105 map.insert( "mon", month ); // Insert the month into the struct.
106 map.insert( "day", day ); // Insert the day into the struct.
107 map.insert( "hour", hour ); // Insert the hour into the struct.
108 map.insert( "min", minute ); // Insert the minute into the struct.
109 args << map ; // Add the map to the arguments list.
110 d->mXmlRpcClient->call( "LJ.XMLRPC.postevent", // The XML-RPC procedure to call.
111 args, // A list containing all the arguments to pass to the procedure.
112 this, // The object containing the slot to use on success.
113 SLOT(slotCreatePost(QList<QVariant>,QVariant)), // The slot to call on success.
114 this, // The object containing the slot to call on failure.
115 SLOT(slotError(int,QString,QVariant)), // The slot to call on failure
116 QVariant( i ) ); // The ID, as we haven't created a post, the location in the map.
117}
118
119void LiveJournal::deleteFriend( const QString &username )
120{
121 Q_UNUSED( username );
122 //TODO
123 // LJ.XMLRPC.editfriends
124}
125
126void LiveJournal::fetchPost( KBlog::BlogPost *post )
127{
128 Q_UNUSED( post );
129 //TODO
130 // LJ.XMLRPC.getevents
131}
132
133QString LiveJournal::fullName() const
134{
135 return d_func()->mFullName;
136}
137
138QString LiveJournal::interfaceName() const
139{
140 return QLatin1String( "LiveJournal" );
141}
142
143void LiveJournal::fetchUserInfo()
144{
145 //TODO
146}
147
148void LiveJournal::listCategories()
149{
150 //TODO
151 // LJ.XMLRPC.getfriendgroups
152}
153
154void LiveJournal::listFriends()
155{
156 //TODO
157 // LJ.XMLRPC.getfriends and their groups
158}
159
160void LiveJournal::listFriendsOf()
161{
162 //TODO
163 // LJ.XMLRPC.friendof
164}
165
166void LiveJournal::listMoods()
167{
168 //TODO
169 // LJ.XMLRPC.login
170}
171
172void LiveJournal::listPictureKeywords()
173{
174 //TODO
175 // LJ.XMLRPC.login
176}
177
178void LiveJournal::listRecentPosts( int number )
179{
180 Q_UNUSED( number );
181 //TODO
182 // LJ.XMLRPC.getevents with lastn and howmany
183}
184
185void LiveJournal::modifyPost( KBlog::BlogPost *post )
186{
187 // LJ.XMLRPC.editevent
188 Q_D( LiveJournal ); // Enable d-pointer access to the LiveJournalPrivate object
189 if ( !post ) { // Check if post has a valid memory address (>0)
190 kError() << "LiveJournal::modifyPost: post is null pointer"; // If it doesn't print an error to the console.
191 return; // If it does not, exit the method
192 }
193 unsigned int i = d->mCallCounter++; // Add one to the call counter and assign it
194 d->mCallMap[ i ] = post; // Put the post in the map at location i
195 kDebug() << "LiveJournal::modifyPost()"; // Send a message to the console to state which method we have entered.
196 QList<QVariant> args; // Create the argument list, in this case will just contain the map.
197 QMap<QString,QVariant> map( d->defaultArgs() ); // Create the initial map from the default arguments.
198 map.insert( "lineendings", "pc" ); // PC line endings
199 map.insert( "event", post->content() ); // Insert the post's content into the struct.
200 map.insert( "subject", post->title() ); // Insert the post's subject into the struct.
201 // TODO map.insert( "allowmask", post->categories() ); // We want to use the allowmask to use categories/tags
202 KDateTime date = post->creationDateTime(); // Get the date of the post's creation
203 int year = date.toString( "%Y" ).toInt(); // Get the year from the date using a format string and converting string to an integer
204 int month = date.toString( "%m" ).toInt(); // Get the month from the date using a format string and converting string to an integer
205 int day = date.toString( "%d" ).toInt(); // Get the day from the date using a format string and converting string to an integer
206 int hour = date.toString( "%H" ).toInt(); // Get the hour from the date using a format string and converting string to an integer
207 int minute = date.toString( "%M" ).toInt(); // Get the minute from the date using a format string and converting string to an integer
208 map.insert( "year", year ); // Insert the year into the struct.
209 map.insert( "mon", month ); // Insert the month into the struct.
210 map.insert( "day", day ); // Insert the day into the struct.
211 map.insert( "hour", hour ); // Insert the hour into the struct.
212 map.insert( "min", minute ); // Insert the minute into the struct.
213 args << map ; // Add the map to the arguments list.
214 d->mXmlRpcClient->call( "LJ.XMLRPC.editevent", // The XML-RPC procedure to call.
215 args, // A list containing all the arguments to pass to the procedure.
216 this, // The object containing the slot to use on success.
217 SLOT(slotCreatePost(QList<QVariant>,QVariant)), // The slot to call on success.
218 this, // The object containing the slot to call on failure.
219 SLOT(slotError(int,QString,QVariant)), // The slot to call on failure
220 QVariant( i ) ); // The ID, as we haven't created a post, the location in the map.
221}
222
223void LiveJournal::removePost( KBlog::BlogPost *post )
224{
225 Q_D( LiveJournal ); // Enable d-pointer access to the LiveJournalPrivate object
226 kDebug() << "LiveJournal::removePost()"; // Send a message to the console to state which method we have entered.
227 QList<QVariant> args; // Create the argument list, in this case will just contain the map.
228 QMap<QString,QVariant> map( d->defaultArgs() ); // Create the initial map from the default arguments.
229 map.insert( "itemid", post->postId().toInt() ); // Insert the post's unique ID into the struct.
230 map.insert( "event", QString() ); // Insert no content into the struct to delete the post.
231 map.insert( "subject", post->title() ); // Insert the post's subject into the struct.
232 // TODO map.insert( "allowmask", post->categories() );
233 KDateTime date = post->creationDateTime(); // Get the date of the post's creation
234 int year = date.toString( "%Y" ).toInt(); // Get the year from the date using a format string and converting string to an integer
235 int month = date.toString( "%m" ).toInt(); // Get the month from the date using a format string and converting string to an integer
236 int day = date.toString( "%d" ).toInt(); // Get the day from the date using a format string and converting string to an integer
237 int hour = date.toString( "%H" ).toInt(); // Get the hour from the date using a format string and converting string to an integer
238 int minute = date.toString( "%M" ).toInt(); // Get the minute from the date using a format string and converting string to an integer
239 map.insert( "year", year ); // Insert the year into the struct.
240 map.insert( "mon", month ); // Insert the month into the struct.
241 map.insert( "day", day ); // Insert the day into the struct.
242 map.insert( "hour", hour ); // Insert the hour into the struct.
243 map.insert( "min", minute ); // Insert the minute into the struct.
244 args << QVariant( map ); // Add the map to the arguments list.
245 d->mXmlRpcClient->call( "LJ.XMLRPC.editevent", // The XML-RPC procedure to call.
246 args, // A list containing all the arguments to pass to the procedure.
247 this, // The object containing the slot to use on success.
248 SLOT(slotRemovePost(QList<QVariant>,QVariant)), // The slot to call on success.
249 this, // The object containing the slot to call on failure.
250 SLOT(slotError(int,QString,QVariant)) ); // The slot to call on failure.
251}
252
253void LiveJournal::setUrl( const KUrl &server )
254{
255 Q_D( LiveJournal );
256 Blog::setUrl( server );
257 delete d->mXmlRpcClient;
258 d->mXmlRpcClient = new KXmlRpc::Client( server );
259 d->mXmlRpcClient->setUserAgent( userAgent() );
260}
261
262QString LiveJournal::serverMessage() const
263{
264 //TODO
265 return d_func()->mServerMessage;
266}
267
268QString LiveJournal::userId() const
269{
270 //TODO
271 return d_func()->mUserId;
272}
273
274LiveJournalPrivate::LiveJournalPrivate() : mXmlRpcClient(0)
275{
276 mCallCounter = 1;
277}
278
279LiveJournalPrivate::~LiveJournalPrivate()
280{
281 delete mXmlRpcClient;
282}
283
284QMap<QString,QVariant> LiveJournalPrivate::defaultArgs()
285{
286 Q_Q( LiveJournal ); // Get access to the q object which allows access to LiveJournal.* from LiveJournalPrivate
287 QMap<QString,QVariant> args; // Create a map which is converted to a struct on the XML-RPC send.
288 args.insert( "username", q->username() ); // Add a username key with the username as it's value.
289 args.insert( "password", q->password() ); // Add a password key with the password as it's value.
290 args.insert( "ver", "1" ); // Add a version key indicating we support unicode.
291 return args; // return the QMap.
292}
293
294void LiveJournalPrivate::generateCookie( const GenerateCookieOptions &options )
295{
296 Q_UNUSED( options );
297 //TODO
298 // LJ.XMLRPC.sessiongenerate
299}
300
301void LiveJournalPrivate::expireCookie( const QString &cookie, bool expireAll )
302{
303 Q_UNUSED( cookie );
304 Q_UNUSED( expireAll );
305 //TODO
306 // LJ.XMLRPC.sessionexpire
307}
308
309bool LiveJournalPrivate::readPostFromMap( BlogPost *post, const QMap<QString, QVariant> &postInfo )
310{
311 Q_UNUSED( post );
312 Q_UNUSED( postInfo );
313 //TODO
314 return false;
315}
316
317void LiveJournalPrivate::slotAddFriend( const QList<QVariant> &result, const QVariant &id )
318{
319 Q_UNUSED( result );
320 Q_UNUSED( id );
321 //TODO
322}
323
324void LiveJournalPrivate::slotAssignFriendToCategory( const QList<QVariant> &result, const QVariant &id )
325{
326 Q_UNUSED( result );
327 Q_UNUSED( id );
328 //TODO
329}
330
331void LiveJournalPrivate::slotCreatePost( const QList<QVariant> &result, const QVariant &id )
332{
333 kDebug() << "LiveJournal::slotCreatePost: " << id; // Print method name and id to the console.
334 Q_Q( LiveJournal ); // Get access to the q object which allows access to LiveJournal.* from LiveJournalPrivate
335 KBlog::BlogPost *post = mCallMap[ id.toInt() ]; // Retrieve the post from the calling map
336 mCallMap.remove( id.toInt() ); // Remove the post as it is now owned by the signal catcher
337
338 // struct containing String anum, String itemid
339 kDebug () << "TOP:" << result[0].typeName(); // Print first return type to the console.
340 if ( result[0].type() != QVariant::Map ) { // Make sure the only return type is a struct.
341 kError() << "Could not fetch post's ID out of the result from the server,"
342 << "not a map."; // If not a struct, print error.
343 emit q->errorPost( LiveJournal::ParsingError,
344 i18n( "Could not read the post ID, result not a map." ), post ); // Emit an error signal if we can't get the post ID.
345 return;
346 }
347 QString itemid = result[0].value<QMap<QString,QVariant> >().value( "itemid" ).value<QString>(); // Get post ID from struct.
348 post->setPostId( itemid ); // Set the post ID to the anum value from the return struct.
349 post->setStatus( KBlog::BlogPost::Created ); // Set the post's status to indicate it has been successfully created.
350 kDebug() << "emitting createdPost()"
351 << "for" << itemid; // Notify emission to the console
352 emit q->createdPost( post ); // Emit the created post
353}
354
355void LiveJournalPrivate::slotDeleteFriend( const QList<QVariant> &result, const QVariant &id )
356{
357 Q_UNUSED( result );
358 Q_UNUSED( id );
359 //TODO
360}
361
362// void LiveJournalPrivate::slotExpireCookie(
363// const QList<QVariant> &result, const QVariant &id )
364// {
365// Q_UNUSED( result );
366// Q_UNUSED( id );
367// //TODO
368// }
369
370void LiveJournalPrivate::slotError( int number, const QString &errorString, const QVariant &id )
371{
372 Q_UNUSED( number );
373 Q_UNUSED( errorString );
374 kError() << "XML-RPC error for " << id;
375}
376
377void LiveJournalPrivate::slotFetchPost( const QList<QVariant> &result, const QVariant &id )
378{
379 Q_UNUSED( result );
380 Q_UNUSED( id );
381 //TODO
382}
383
384void LiveJournalPrivate::slotFetchUserInfo( const QList<QVariant> &result, const QVariant &id )
385{
386 Q_UNUSED( result );
387 Q_UNUSED( id );
388 //TODO
389}
390/*
391void LiveJournalPrivate::slotGenerateCookie( const QList<QVariant> &result, const QVariant &id )
392{
393 Q_UNUSED( result );
394 Q_UNUSED( id );
395 //TODO
396}*/
397
398void LiveJournalPrivate::slotListCategories( const QList<QVariant> &result, const QVariant &id )
399{
400 Q_UNUSED( result );
401 Q_UNUSED( id );
402 //TODO
403}
404
405void LiveJournalPrivate::slotListFriends( const QList<QVariant> &result, const QVariant &id )
406{
407 Q_UNUSED( result );
408 Q_UNUSED( id );
409 //TODO
410}
411
412void LiveJournalPrivate::slotListFriendsOf( const QList<QVariant> &result, const QVariant &id )
413{
414 Q_UNUSED( result );
415 Q_UNUSED( id );
416 //TODO
417}
418
419void LiveJournalPrivate::slotListMoods( const QList<QVariant> &result, const QVariant &id )
420{
421 Q_UNUSED( result );
422 Q_UNUSED( id );
423 //TODO
424}
425
426void LiveJournalPrivate::slotListPictureKeywords( const QList<QVariant> &result, const QVariant &id )
427{
428 Q_UNUSED( result );
429 Q_UNUSED( id );
430 //TODO
431}
432
433void LiveJournalPrivate::slotListRecentPosts( const QList<QVariant> &result, const QVariant &id )
434{
435 Q_UNUSED( result );
436 Q_UNUSED( id );
437 //TODO
438}
439
440void LiveJournalPrivate::slotModifyPost( const QList<QVariant> &result, const QVariant &id )
441{
442 kDebug() << "LiveJournal::slotModifyPost: " << id; // Print method name and id to the console.
443 Q_Q( LiveJournal ); // Get access to the q object which allows access to LiveJournal.* from LiveJournalPrivate
444 KBlog::BlogPost *post = mCallMap[ id.toInt() ]; // Retrieve the post from the calling map
445 mCallMap.remove( id.toInt() ); // Remove the post as it is now owned by the signal catcher
446
447 // struct containing String anum, String itemid
448 kDebug () << "TOP:" << result[0].typeName(); // Print first return type to the console.
449 if ( result[0].type() != QVariant::Map ) { // Make sure the only return type is a struct.
450 kError() << "Could not fetch post's ID out of the result from the server,"
451 << " not a map."; // If not a struct, print error.
452 emit q->errorPost( LiveJournal::ParsingError,
453 i18n( "Could not read the post ID, result not a map." ), post ); // Emit an error signal if we can't get the post ID.
454 return;
455 }
456 QString itemid = result[0].value<QMap<QString,QVariant> >().value( "itemid" ).value<QString>(); // Get post ID from struct.
457 post->setPostId( itemid ); // Set the post ID to the anum value from the return struct.
458 post->setStatus( KBlog::BlogPost::Created ); // Set the post's status to indicate it has been successfully created.
459 kDebug() << "emitting createdPost()"
460 << "for" << itemid; // Notify emission to the console
461 emit q->createdPost( post ); // Emit the created post
462}
463
464void LiveJournalPrivate::slotRemovePost( const QList<QVariant> &result,
465 const QVariant &id )
466{
467 kDebug() << "LiveJournal::slotCreatePost: " << id; // Print method name and id to the console.
468 Q_Q( LiveJournal ); // Get access to the q object which allows access to LiveJournal.* from LiveJournalPrivate
469 KBlog::BlogPost *post = mCallMap[ id.toInt() ]; // Retrieve the post from the calling map
470 mCallMap.remove( id.toInt() ); // Remove the post as it is now owned by the signal catcher
471
472 // struct containing String anum, String itemid
473 kDebug () << "TOP:" << result[0].typeName(); // Print first return type to the console.
474 if ( result[0].type() != QVariant::Map ) { // Make sure the only return type is a struct.
475 kError() << "Could not fetch post's ID out of the result from the server,"
476 << "not a map."; // If not a struct, print error.
477 emit q->errorPost( LiveJournal::ParsingError,
478 i18n( "Could not read the post ID, result not a map." ), post ); // Emit an error signal if we can't get the post ID.
479 return;
480 }
481 QString itemid = result[0].value<QMap<QString,QVariant> >().value( "itemid" ).value<QString>();
482 if ( itemid == post->postId() ) { // Check the post ID matches the anum value from the return struct.
483 post->setStatus( KBlog::BlogPost::Removed ); // Set the post's status to indicate it has been successfully removed.
484 kDebug() << "emitting createdPost()"
485 << "for" << itemid; // Notify emission to the console
486 emit q->removedPost( post ); // Emit the removed post
487 return;
488 }
489 kError() << "The returned post ID did not match the sent one."; // If not matching, print error.
490 emit q->errorPost( LiveJournal::ParsingError,
491 i18n( "The returned post ID did not match the sent one: " ), post ); // Emit an error signal if the post IDs don't match.
492}
493
494#include "moc_livejournal.cpp"
495

Warning: That file was not part of the compilation database. It may have many parsing errors.