33 #include <kblog/blogger1.h>
34 #include <kblog/gdata.h>
35 #include <kblog/metaweblog.h>
36 #include <kblog/movabletype.h>
37 #include <kblog/wordpressbuggy.h>
38 #include <kblog/blogmedia.h>
40 #include <KDE/KLocale>
42 #include <kio/netaccess.h>
45 const QRegExp
splitRX(QLatin1String(
"((<hr/?>)?<!--split-->)"));
47 class Backend::Private
51 :kBlog(0), bBlog(0), categoryListNotSet(false)
55 QList<Category> mCreatePostCategories;
56 QMap<QString, KBlog::BlogPost *> mSetPostCategoriesMap;
57 QMap<KBlog::BlogPost *, BilboPost::Status> mSubmitPostStatusMap;
58 QMap<KBlog::BlogMedia *, BilboMedia *> mPublishMediaMap;
59 bool categoryListNotSet;
63 :
QObject( parent ), d(new Private)
65 kDebug() <<
"with blog id: " << blog_id;
67 d->kBlog = d->bBlog->blogBackend();
69 connect( d->kBlog, SIGNAL(
error(KBlog::Blog::ErrorType,QString)),
70 this, SLOT(
error(KBlog::Blog::ErrorType,QString)) );
71 connect( d->kBlog, SIGNAL(errorPost(KBlog::Blog::ErrorType,QString,KBlog::BlogPost*)),
72 this, SLOT(
error(KBlog::Blog::ErrorType,QString)) );
73 connect( d->kBlog, SIGNAL( errorComment( KBlog::Blog::ErrorType,
const QString &, KBlog::BlogPost*,
74 KBlog::BlogComment* ) ),
75 this, SLOT(
error(KBlog::Blog::ErrorType,QString)) );
76 connect( d->kBlog, SIGNAL(errorMedia(KBlog::Blog::ErrorType,QString,KBlog::BlogMedia*)),
77 this, SLOT(
error(KBlog::Blog::ErrorType,QString)) );
88 kDebug() <<
"Blog Id: " << d->bBlog->id();
92 KBlog::MetaWeblog *tmp =
static_cast<KBlog::MetaWeblog*
>( d->kBlog );
93 connect( tmp, SIGNAL(listedCategories(QList<QMap<QString,QString> >)),
95 tmp->listCategories();
97 error( KBlog::Blog::NotSupported, i18n(
"Blog API doesn't support getting Category list." ) );
103 kDebug() <<
"Blog Id: " << d->bBlog->id();
106 const int categoriesCount(categories.count());
107 for (
int i = 0; i < categoriesCount; ++i ) {
108 const QMap<QString, QString> &category = categories.at( i );
110 const QString name = category.value( QLatin1String(
"name"), QString() );
111 const QString
description = category.value( QLatin1String(
"description"), QString() );
112 const QString htmlUrl = category.value( QLatin1String(
"htmlUrl"), QString() );
113 const QString rssUrl = category.value( QLatin1String(
"rssUrl"), QString() );
114 QString categoryId = category.value( QLatin1String(
"categoryId"), QString() );
115 const QString parentId = category.value( QLatin1String(
"parentId"), QString() );
117 if(categoryId.isEmpty()) {
118 categoryId = QString::number(i);
123 kDebug() <<
"Emitting sigCategoryListFetched...";
129 kDebug() <<
"Blog Id: " << d->bBlog->id();
130 connect( d->kBlog, SIGNAL(listedRecentPosts(QList<KBlog::BlogPost>)),
132 d->kBlog->listRecentPosts( count );
137 kDebug() <<
"Blog Id: " << d->bBlog->id();
140 const int postCount(posts.count());
141 for (
int i = 0; i < postCount; ++i ) {
144 tempPost.setContent( tempPost.content().replace( QLatin1Char(
'\n'), QLatin1String(
"<br/>") ) );
145 tempPost.setAdditionalContent( tempPost.additionalContent().replace( QLatin1Char(
'\n'), QLatin1String(
"<br/>") ) );
149 kDebug() <<
"Emitting sigEntriesListFetched ...";
155 kDebug() <<
"Blog Id: " << d->bBlog->id();
158 QString poweredStr = QLatin1String(
"<p>=-=-=-=-=<br/>"
159 "<i>Powered by <b><a href='http://blogilo.gnufolks.org/'>Blogilo</a></b></i></p>");
160 post->setContent(post->content() + poweredStr);
163 connect( d->kBlog, SIGNAL(createdPost(KBlog::BlogPost*)),
165 d->kBlog->createPost( post );
170 kDebug() <<
"Blog Id: " << d->bBlog->id();
171 if ( post->status() == KBlog::BlogPost::Error ) {
172 kDebug() <<
"Publishing/Modifying Failed";
173 const QString tmp( i18n(
"Publishing/Modifying post failed: %1", post->error() ) );
174 kDebug() <<
"Emitting sigError...";
178 kDebug()<<
"isPrivate: "<<post->isPrivate();
184 d->mSubmitPostStatusMap[ post ] = post->status();
185 connect( d->kBlog, SIGNAL(fetchedPost(KBlog::BlogPost*)),
187 d->kBlog->fetchPost( post );
192 kDebug() <<
"Blog Id: " << d->bBlog->id();
194 switch ( d->bBlog->api() ) {
197 kDebug() <<
"The Blogger1 and GData API type doesn't support uploading Media files.";
198 tmp = i18n(
"Uploading media failed: Your Blog API does not support uploading media objects.");
199 kDebug() <<
"Emitting sigError...";
205 KBlog::BlogMedia *m =
new KBlog::BlogMedia() ;
206 KBlog::MetaWeblog *MWBlog = qobject_cast<KBlog::MetaWeblog*>( d->kBlog );
208 m->setMimetype( media->
mimeType() );
211 KIO::TransferJob *job = KIO::get( media->
localUrl(), KIO::Reload, KIO::HideProgressInfo);
212 if( !KIO::NetAccess::synchronousRun(job, 0, &data) ){
213 kError()<<
"Job error: " << job->errorString();
214 tmp = i18n(
"Uploading media failed: Cannot read the media file, please check if it exists. Path: %1", media->
localUrl().pathOrUrl() );
215 kDebug() <<
"Emitting sigError...";
219 if ( data.count() == 0 ) {
220 kError() <<
"Cannot read the media file, please check if it exists.";
221 tmp = i18n(
"Uploading media failed: Cannot read the media file, please check if it exists. Path: %1", media->
localUrl().pathOrUrl() );
222 kDebug() <<
"Emitting sigError...";
229 m->setName( media->
name() );
231 media->
setCheckSum( qChecksum( data.data(), data.count() ) );
234 kError() <<
"Media file checksum is zero";
235 tmp = i18n(
"Uploading media failed: Media file checksum is zero, please check file path. Path: %1",
237 kDebug() <<
"Emitting sigError...";
244 kError() <<
"MWBlog is NULL: casting has not worked, this should NEVER happen, has the gui allowed using GDATA?";
245 tmp = i18n(
"INTERNAL ERROR: MWBlog is NULL: casting has not worked, this should NEVER happen." );
246 kDebug() <<
"Emitting sigError...";
251 d->mPublishMediaMap[ m ] = media;
252 connect( MWBlog, SIGNAL(createdMedia(KBlog::BlogMedia*)),
this, SLOT(
mediaUploaded(KBlog::BlogMedia*)) );
253 connect( MWBlog, SIGNAL(errorMedia(KBlog::Blog::ErrorType,QString,KBlog::BlogMedia*)),
254 this, SLOT(
slotMediaError(KBlog::Blog::ErrorType,QString,KBlog::BlogMedia*)) );
255 MWBlog->createMedia( m );
258 kError() <<
"Api type isn't set correctly!";
259 tmp = i18n(
"API type is not set correctly." );
265 kDebug() <<
"Blog Id: " << d->bBlog->id() <<
"Media: "<<media->url();
267 kError()<<
"ERROR! Media returned from KBlog is NULL!";
270 BilboMedia * m = d->mPublishMediaMap.value( media );
272 kError()<<
"ERROR! Media returned from KBlog doesn't exist on the Map! Url is:"
276 d->mPublishMediaMap.remove( media );
277 if ( media->status() == KBlog::BlogMedia::Error ) {
278 kError() <<
"Upload error! with this message: " << media->error();
279 const QString tmp( i18n(
"Uploading media failed: %1", media->error() ) );
280 kDebug() <<
"Emitting sigMediaError ...";
284 quint16 newChecksum = qChecksum( media->data().data(), media->data().count() );
285 if ( newChecksum != m->
checksum() ) {
286 kError() <<
"Check sum error: checksum of sent file: " << m->
checksum() <<
287 " Checksum of received file: " << newChecksum <<
"Error: " << media->error() << endl;
288 const QString tmp( i18n(
"Uploading media failed: Checksum error. Returned error: %1",
290 kDebug() <<
"Emitting sigMediaError ...";
294 m->
setRemoteUrl( QUrl( media->url().url() ).toString() );
296 kDebug() <<
"Emitting sigMediaUploaded...";
302 kDebug() <<
"Blog Id: " << d->bBlog->id();
305 connect( d->kBlog, SIGNAL(modifiedPost(KBlog::BlogPost*)),
307 d->kBlog->modifyPost( post );
312 kDebug() <<
"Blog Id: " << d->bBlog->id();
315 connect( d->kBlog, SIGNAL(removedPost(KBlog::BlogPost*)),
317 d->kBlog->removePost( post );
323 kDebug()<<
"post returned from server is NULL";
335 connect( d->kBlog, SIGNAL(fetchedPost(KBlog::BlogPost*)),
337 d->kBlog->fetchPost( post );
348 kDebug() <<
"Blog Id: " << d->bBlog->id();
349 QString errType = errorTypeToString( type );
350 errType += errorMessage;
352 kDebug() <<
"Emitting sigError";
357 KBlog::BlogMedia * media )
360 QString errType = errorTypeToString( type );
361 errType += errorMessage;
363 kDebug() <<
"Emitting sigMediaError ...";
364 emit
sigMediaError( errorMessage, d->mPublishMediaMap[ media ] );
365 d->mPublishMediaMap.remove( media );
368 QString Backend::errorTypeToString( KBlog::Blog::ErrorType type )
372 case KBlog::Blog::XmlRpc:
373 errType = i18n(
"Server (XMLRPC) error: " );
375 case KBlog::Blog::Atom:
376 errType = i18n(
"Server (Atom) error: " );
378 case KBlog::Blog::ParsingError:
379 errType = i18n(
"Parsing error: " );
381 case KBlog::Blog::AuthenticationError:
382 errType = i18n(
"Authentication error: " );
384 case KBlog::Blog::NotSupported:
385 errType = i18n(
"Not supported error: " );
388 errType = i18n(
"Unknown error: " );
396 kError()<<
"ERROR: post is NULL ";
397 Q_EMIT
sigError( i18n(
"post is NULL") );
400 kDebug()<<
"isPrivate: "<<post->isPrivate();
403 if( d->mSubmitPostStatusMap[ post ] == KBlog::BlogPost::Modified) {
408 d->mSubmitPostStatusMap.remove(post);
409 if ( post_id != -1 ) {
410 pp->setPrivate( post->isPrivate() );
411 pp->
setId( post_id );
412 kDebug() <<
"Emitting sigPostPublished ...";
420 KBlog::BlogPost* Backend::preparePost( KBlog::BlogPost* post )
422 QString content = post->content();
425 int found = content.indexOf(QLatin1String(
"<pre>"), i, Qt::CaseInsensitive);
426 while ( found != -1 )
428 html1 += content.mid( i, found-i).remove(QLatin1Char(
'\n'));
430 found = content.indexOf(QLatin1String(
"</pre>"), i, Qt::CaseInsensitive);
432 html1 += content.mid( i, found+5-i);
434 found = content.indexOf(QLatin1String(
"<pre>"), i, Qt::CaseInsensitive);
436 html1 += content.mid( i, content.length()-i );
441 html1 += content.mid( i, content.length()-i).
remove(QLatin1Char(
'\n'));
442 post->setContent( html1 );
444 content = post->additionalContent();
445 QString html2 = QString();
447 found = content.indexOf(QLatin1String(
"<pre>"), i, Qt::CaseInsensitive);
448 while ( found != -1 )
450 html2 += content.mid( i, found-i).remove(QLatin1Char(
'\n'));
452 found = content.indexOf(QLatin1String(
"</pre>"), i, Qt::CaseInsensitive);
454 html2 += content.mid( i, found+5-i);
456 found = content.indexOf(QLatin1String(
"<pre>"), i, Qt::CaseInsensitive);
458 html2 += content.mid( i, content.length()-i );
463 html2 += content.mid( i, content.length()-i).
remove(QLatin1Char(
'\n'));
464 post->setAdditionalContent( html2 );
472 QStringList content = post->content().split(
splitRX);
473 if( content.count() == 2 ) {
474 post->setContent(content.at(0));
475 post->setAdditionalContent( content.at(1) );
485 #include "backend.moc"
bool clearCategories(int blog_id)
void postPublished(KBlog::BlogPost *post)
void slotPostFetched(KBlog::BlogPost *post)
QString lastErrorText() const
void publishPost(BilboPost *post)
Use this to publish a post to server.
void sigCategoryListFetched(int blog_id)
emit when a categoriesListed() Done and Categories added to DB
static DBMan * self()
Retrieve the instance of DataBase Manager.
Definition of a blog post! it's implemented to decrease dependency to KBlog :)
void sigError(const QString &errorMessage)
this signal emitted when an error occurred on current transaction.
void sigMediaError(const QString &errorMessage, BilboMedia *media)
BilboBlog * blog(int blog_id)
QString as Title, and int as blog_id.
void error(KBlog::Blog::ErrorType type, const QString &errorMessage)
void fetchPost(BilboPost *post)
Fetch a blog post from the server with a specific ID.
void slotPostRemoved(KBlog::BlogPost *post)
void sigEntriesListFetched(int blog_id)
emit when a entriesListed() Done and Entries added to DB
int addCategory(const QString &name, const QString &description, const QString &htmlUrl, const QString &rssUrl, const QString &categoryId, const QString &parentId, int blog_id)
Category:
void removePost(BilboPost *post)
Remove an existing post from server.
const QRegExp splitRX(QLatin1String("((<hr/?>)?<!--split-->)"))
void sigPostFetched(BilboPost *post)
static bool addPoweredBy()
Get addPoweredBy.
void mediaUploaded(KBlog::BlogMedia *media)
void modifyPost(BilboPost *post)
Modify an existing post.
void getEntriesListFromServer(int count)
retrieve latest posts from server
bool editPost(const BilboPost &post, int blog_id)
void uploadMedia(BilboMedia *media)
Upload a new Media object e.g.
int addPost(const BilboPost &post, int blog_id)
Post:
void slotMediaError(KBlog::Blog::ErrorType type, const QString &errorMessage, KBlog::BlogMedia *media)
static bool changeNToBreak()
Get changeNToBreak.
static const char description[]
void getCategoryListFromServer()
Request to Fetch categories list from server.
void savePostInDbAndEmitResult(KBlog::BlogPost *post)
This function is called after a post published fine, to insert it to DB and emit sigPostPublished.
void sigMediaUploaded(BilboMedia *media)
This signal is emitted when a media has been uploaded to the server.
void sigPostRemoved(int blog_id, const BilboPost &post)
this signal is emitted when a post removed successfully.
void entriesListed(const QList< KBlog::BlogPost > &posts)
Backend(int blog_id, QObject *parent=0)
void sigPostPublished(int blog_id, BilboPost *post)
This signal is emitted when a post published/modified and added/edited to Database.
void categoriesListed(const QList< QMap< QString, QString > > &categories)