diff --git a/kblog/wordpressbuggy.cpp b/kblog/wordpressbuggy.cpp index a7a749415..648160ad7 100644 --- a/kblog/wordpressbuggy.cpp +++ b/kblog/wordpressbuggy.cpp @@ -1,371 +1,371 @@ /* This file is part of the kblog library. Copyright (c) 2006-2007 Christian Weilbach This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "wordpressbuggy.h" #include "wordpressbuggy_p.h" #include "blogpost.h" #include #include #include #include #include #include using namespace KBlog; WordpressBuggy::WordpressBuggy( const KUrl &server, QObject *parent ) : MovableType( server, *new WordpressBuggyPrivate, parent ) { kDebug() << "WordpressBuggy()"; } WordpressBuggy::WordpressBuggy( const KUrl &server, WordpressBuggyPrivate &dd, QObject *parent ) : MovableType( server, dd, parent ) { kDebug() << "WordpressBuggy()"; } WordpressBuggy::~WordpressBuggy() { kDebug() << "~WordpressBuggy()"; } void WordpressBuggy::createPost( KBlog::BlogPost *post ) { kDebug() << "createPost()"; Q_D( WordpressBuggy ); if ( !post ) { kError() << "WordpressBuggy::createPost: post is a null pointer"; emit error ( Other, i18n( "Post is a null pointer." ) ); return; } kDebug() << "Creating new Post with blogId" << blogId(); QString xmlMarkup = ""; xmlMarkup += ""; xmlMarkup += "metaWeblog.newPost"; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += "description"; xmlMarkup += "content()+"]]>"; xmlMarkup += ""; xmlMarkup += "title"; xmlMarkup += "title()+"]]>"; xmlMarkup += ""; QList catList = post->categories(); if ( !catList.empty() ){ xmlMarkup += "categories"; - xmlMarkup += ""; + xmlMarkup += ""; QList::ConstIterator it = catList.constBegin(); QList::ConstIterator end = catList.constEnd(); for ( ; it != end; ++it ){ - xmlMarkup += ""; + xmlMarkup += ""; } - xmlMarkup += ""; + xmlMarkup += ""; xmlMarkup += ""; } xmlMarkup += "dateCreated"; xmlMarkup += "" + post->creationDateTime().toUtc().dateTime().toString( "yyyyMMddThh:mm:ss" ) + ""; xmlMarkup += ""; xmlMarkup += "mt_allow_comments"; xmlMarkup += QString( "%1" ).arg( (int)post->isCommentAllowed() ); xmlMarkup += ""; xmlMarkup += "mt_allow_pings"; xmlMarkup += QString( "%1" ).arg( (int)post->isTrackBackAllowed() ); xmlMarkup += ""; xmlMarkup += "mt_excerpt"; xmlMarkup += "summary() + "]]>"; xmlMarkup += ""; xmlMarkup += "mt_keywords"; xmlMarkup += "tags().join(",") + "]]>"; xmlMarkup += ""; xmlMarkup += "" + QString( "%1" ).arg( (int)(!post->isPrivate() ) ) + ""; xmlMarkup += ""; QByteArray postData; QDataStream stream( &postData, QIODevice::WriteOnly ); stream.writeRawData( xmlMarkup.toUtf8(), xmlMarkup.toUtf8().length() ); KIO::TransferJob *job = KIO::http_post( url(), postData, KIO::HideProgressInfo ); d->mCreatePostMap[ job ] = post; if ( !job ) { kWarning() << "Failed to create job for: " << url().url(); } job->addMetaData( "customHTTPHeader", "X-hacker: Shame on you Wordpress, " + QString() + "you took another 4 hours of my life to work around the stupid dateTime bug." ); job->addMetaData( "content-type", "Content-Type: text/xml; charset=utf-8" ); job->addMetaData( "ConnectTimeout", "50" ); job->addMetaData( "UserAgent", userAgent() ); connect( job, SIGNAL(data(KIO::Job *,const QByteArray &)), this, SLOT(slotCreatePostData(KIO::Job *,const QByteArray &)) ); connect( job, SIGNAL(result(KJob *)), this, SLOT(slotCreatePost(KJob *)) ); } void WordpressBuggy::modifyPost( KBlog::BlogPost *post ) { kDebug() << "modifyPost()"; Q_D( WordpressBuggy ); if ( !post ) { kError() << "WordpressBuggy::modifyPost: post is a null pointer"; emit error ( Other, i18n( "Post is a null pointer." ) ); return; } kDebug() << "Uploading Post with postId" << post->postId(); QString xmlMarkup = ""; xmlMarkup += ""; xmlMarkup += "metaWeblog.editPost"; xmlMarkup += ""; xmlMarkup += "postId()+"]]>"; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += ""; xmlMarkup += "description"; xmlMarkup += "content()+"]]>"; xmlMarkup += ""; xmlMarkup += "title"; xmlMarkup += "title()+"]]>"; xmlMarkup += ""; QList catList = post->categories(); if ( !catList.empty() ){ xmlMarkup += "categories"; - xmlMarkup += ""; + xmlMarkup += ""; QList::ConstIterator it = catList.constBegin(); QList::ConstIterator end = catList.constEnd(); for ( ; it != end; ++it ){ - xmlMarkup += ""; + xmlMarkup += ""; } - xmlMarkup += ""; + xmlMarkup += ""; xmlMarkup += ""; } xmlMarkup += "lastModified"; xmlMarkup += "" + post->modificationDateTime().toUtc().dateTime().toString( "yyyyMMddThh:mm:ss" ) + ""; xmlMarkup += ""; xmlMarkup += "dateCreated"; xmlMarkup += "" + post->creationDateTime().toUtc().dateTime().toString( "yyyyMMddThh:mm:ss" ) + ""; xmlMarkup += ""; xmlMarkup += "mt_allow_comments"; xmlMarkup += QString( "%1" ).arg( (int)post->isCommentAllowed() ); xmlMarkup += ""; xmlMarkup += "mt_allow_pings"; xmlMarkup += QString( "%1" ).arg( (int)post->isTrackBackAllowed() ); xmlMarkup += ""; xmlMarkup += "mt_excerpt"; xmlMarkup += "summary() + "]]>"; xmlMarkup += ""; xmlMarkup += "mt_keywords"; xmlMarkup += "tags().join( "," ) + "]]>"; xmlMarkup += ""; xmlMarkup += "" + QString( "%1" ).arg( (int)( !post->isPrivate() ) ) + ""; xmlMarkup += ""; QByteArray postData; QDataStream stream( &postData, QIODevice::WriteOnly ); stream.writeRawData( xmlMarkup.toUtf8(), xmlMarkup.toUtf8().length() ); KIO::TransferJob *job = KIO::http_post( url(), postData, KIO::HideProgressInfo ); d->mModifyPostMap[ job ] = post; if ( !job ) { kWarning() << "Failed to create job for: " << url().url(); } job->addMetaData( "customHTTPHeader", "X-hacker: Shame on you Wordpress, " + QString() + "you took another 4 hours of my life to work around the stupid dateTime bug." ); job->addMetaData( "content-type", "Content-Type: text/xml; charset=utf-8" ); job->addMetaData( "ConnectTimeout", "50" ); job->addMetaData( "UserAgent", userAgent() ); connect( job, SIGNAL(data(KIO::Job*,const QByteArray&)), this, SLOT(slotModifyPostData(KIO::Job*,const QByteArray&)) ); connect( job, SIGNAL(result(KJob*)), this, SLOT(slotModifyPost(KJob*)) ); } QString WordpressBuggy::interfaceName() const { return QLatin1String( "Movable Type" ); } WordpressBuggyPrivate::WordpressBuggyPrivate() { } WordpressBuggyPrivate::~WordpressBuggyPrivate() { kDebug() << "~WordpressBuggyPrivate()"; } QList WordpressBuggyPrivate::defaultArgs( const QString &id ) { Q_Q( WordpressBuggy ); QList args; if ( !id.isEmpty() ) { args << QVariant( id ); } args << QVariant( q->username() ) << QVariant( q->password() ); return args; } void WordpressBuggyPrivate::slotCreatePostData( KIO::Job *job, const QByteArray &data ) { kDebug() << "slotCreatePostData()"; unsigned int oldSize = mCreatePostBuffer[ job ].size(); mCreatePostBuffer[ job ].resize( oldSize + data.size() ); memcpy( mCreatePostBuffer[ job ].data() + oldSize, data.data(), data.size() ); } void WordpressBuggyPrivate::slotCreatePost( KJob *job ) { kDebug() << "slotCreatePost()"; const QString data = QString::fromUtf8( mCreatePostBuffer[ job ].data(), mCreatePostBuffer[ job ].size() ); mCreatePostBuffer[ job ].resize( 0 ); Q_Q( WordpressBuggy ); KBlog::BlogPost *post = mCreatePostMap[ job ]; mCreatePostMap.remove( job ); if ( job->error() != 0 ) { kError() << "slotCreatePost error:" << job->errorString(); emit q->errorPost( WordpressBuggy::Atom, job->errorString(), post ); return; } QRegExp rxError( "faultString" ); if ( rxError.indexIn( data ) != -1 ){ rxError = QRegExp( "(.+)" ); if ( rxError.indexIn( data ) != -1 ) { kDebug() << "RegExp of faultString failed."; } kDebug() << rxError.cap(1); emit q->errorPost( WordpressBuggy::XmlRpc, rxError.cap(1), post ); return; } QRegExp rxId( "(.+)" ); if ( rxId.indexIn( data ) == -1 ){ kError() << "Could not regexp the id out of the result:" << data; emit q->errorPost( WordpressBuggy::XmlRpc, i18n( "Could not regexp the id out of the result." ), post ); return; } kDebug() << "QRegExp rx( \"(.+)\" ) matches" << rxId.cap( 1 ); post->setPostId( rxId.cap( 1 ) ); post->setStatus( BlogPost::Created ); kDebug() << "Emitting createdPost()"; emit q->createdPost( post ); } void WordpressBuggyPrivate::slotModifyPostData( KIO::Job *job, const QByteArray &data ) { kDebug() << "slotModifyPostData()"; unsigned int oldSize = mModifyPostBuffer[ job ].size(); mModifyPostBuffer[ job ].resize( oldSize + data.size() ); memcpy( mModifyPostBuffer[ job ].data() + oldSize, data.data(), data.size() ); } void WordpressBuggyPrivate::slotModifyPost( KJob *job ) { kDebug() << "slotModifyPost()"; const QString data = QString::fromUtf8( mModifyPostBuffer[ job ].data(), mModifyPostBuffer[ job ].size() ); mModifyPostBuffer[ job ].resize( 0 ); KBlog::BlogPost *post = mModifyPostMap[ job ]; mModifyPostMap.remove( job ); Q_Q( WordpressBuggy ); if ( job->error() != 0 ) { kError() << "slotModifyPost error:" << job->errorString(); emit q->errorPost( WordpressBuggy::Atom, job->errorString(), post ); return; } QRegExp rxError( "faultString" ); if ( rxError.indexIn( data ) != -1 ){ rxError = QRegExp( "(.+)" ); if ( rxError.indexIn( data ) != -1 ) { kDebug() << "RegExp of faultString failed."; } kDebug() << rxError.cap(1); emit q->errorPost( WordpressBuggy::XmlRpc, rxError.cap(1), post ); return; } QRegExp rxId( "(.+)" ); if ( rxId.indexIn( data ) == -1 ) { kError() << "Could not regexp the id out of the result:" << data; emit q->errorPost( WordpressBuggy::XmlRpc, i18n( "Could not regexp the id out of the result." ), post ); return; } kDebug() << "QRegExp rx( \"(.+)\" ) matches" << rxId.cap( 1 ); if ( rxId.cap( 1 ).toInt() == 1 ) { kDebug() << "Post successfully updated."; post->setStatus( BlogPost::Modified ); emit q->modifiedPost( post ); } } #include "wordpressbuggy.moc"