38 #include <kstandarddirs.h>
39 #include <kmessagebox.h>
40 #include <klocalizedstring.h>
42 #include <kdatetime.h>
47 static const char POST_TITLE[] =
"Temporary-Post-Used-For-Style-Detection-Title-";
48 static const char POST_CONTENT[] =
"Temporary-Post-Used-For-Style-Detection-Content-";
56 KMessageBox::detailedError( mParent, i18n(
"Cannot fetch the selected blog style."),
65 QString url = QString::fromLatin1(
"blogilo/%1/" ).arg( blogid );
66 mCachePath = KStandardDirs::locateLocal(
"data", url ,
true );
67 generateRandomPostStrings();
68 mParent = qobject_cast<
QWidget* >( parent );
74 mTempPost->setTitle( mPostTitle );
75 mTempPost->setContent( mPostContent );
76 mTempPost->setPrivate(
false );
80 mTempPost->setCreationDateTime( KDateTime( QDate(2000, 1, 1), QTime(0, 0), KDateTime::UTC ) );
84 connect( b, SIGNAL(sigPostPublished(
int,
BilboPost*)),
this,
85 SLOT(slotTempPostPublished(
int,
BilboPost*)) );
86 connect( b, SIGNAL(sigError(QString)),
this, SLOT(slotError(QString)) );
100 const QString &content )
111 QString url = QString::fromLatin1(
"blogilo/%1/" ).arg( blogid );
112 url = KStandardDirs::locateLocal(
"data", url ,
true );
114 dest.addPath(QLatin1String(
"style.html"));
115 dest.setScheme(QLatin1String(
"file"));
117 if ( !dest.isValid() ) {
118 return QLatin1String(
"<html><body><h2 align='center'>") + title + QLatin1String(
"</h2><br>") + content + QLatin1String(
"</html>");
120 QFile file( dest.pathOrUrl() );
121 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
122 return QLatin1String(
"<html><body><h2 align='center'>") + title + QLatin1String(
"</h2><br>") + content + QLatin1String(
"</html>");
126 while ( !file.atEnd() ) {
127 QByteArray line = file.readLine();
128 buffer.append( QString::fromUtf8( line ) );
131 QRegExp typeRx ( QLatin1String(
"(TYPE[^>]+>)" ) );
132 buffer.remove( typeRx );
134 QRegExp titleRx( QString::fromLatin1(
"%1[\\d]*" ).arg( QLatin1String(
POST_TITLE) ) );
135 QRegExp contentRx( QString::fromLatin1(
"%1[\\d]*" ).arg( QLatin1String(
POST_CONTENT )) );
137 buffer.replace( titleRx, title );
138 buffer.replace( contentRx, content );
143 void StyleGetter::slotTempPostPublished(
int blogId,
BilboPost* post )
149 postUrl = post->link();
150 if ( postUrl.isEmpty() ) {
151 kDebug() <<
"link was empty";
153 postUrl = post->permaLink();
154 if ( postUrl.isEmpty() ) {
155 kDebug() <<
"permaLink was empty";
156 postUrl = KUrl(
DBMan::self()->blog(blogId)->blogUrl() );
163 KIO::StoredTransferJob *job = KIO::storedGet( postUrl, KIO::NoReload, KIO::HideProgressInfo );
164 connect( job, SIGNAL(result(KJob*)),
165 this, SLOT(slotHtmlCopied(KJob*)) );
169 void StyleGetter::slotHtmlCopied( KJob *job )
172 if ( job->error() ) {
173 KMessageBox::detailedError( mParent, i18n(
"Cannot get html file."),
174 job->errorString() );
175 sender()->deleteLater();
181 QByteArray httpData( qobject_cast<KIO::StoredTransferJob*>( job )->data() );
183 QString href( mTempPost->permaLink().url() );
184 int filenameOffset = href.lastIndexOf( QLatin1String(
"/") );
185 href = href.remove( filenameOffset + 1, 255 );
186 QString base( QLatin1String(
"<base href=\"")+href+QLatin1String(
"\"/>") );
188 QRegExp rxBase( QLatin1String(
"(<base\\shref=[^>]+>)") );
189 if ( rxBase.indexIn( QLatin1String(httpData) ) != -1 ) {
190 httpData.replace( rxBase.cap( 1 ).toLatin1(), base.toLatin1() );
193 int headOffset = httpData.indexOf(
"<head>" );
194 httpData.insert( headOffset + 6, base.toLatin1() );
197 QFile file( mCachePath + QLatin1String(
"style.html") );
199 if ( file.exists() ) {
202 if (!file.open( QIODevice::WriteOnly ) ) {
203 KMessageBox::error( mParent,
204 i18n(
"Cannot write data to file %1", file.fileName() ) );
207 if ( file.write( httpData ) == -1 ) {
208 KMessageBox::error( mParent,
209 i18n(
"Cannot write data to file %1", file.fileName() ) );
220 connect( b, SIGNAL(sigPostRemoved(
int,
BilboPost)),
this,
221 SLOT(slotTempPostRemoved(
int,
BilboPost)) );
225 void StyleGetter::slotTempPostRemoved(
int blog_id,
const BilboPost &post)
237 void StyleGetter::generateRandomPostStrings()
241 int postRandomNumber = rand();
242 mPostTitle = QString::fromLatin1(
"%1%2" ).arg( QLatin1String(
POST_TITLE) ).arg( postRandomNumber );
243 mPostContent = QString::fromLatin1(
"%1%2" ).arg( QLatin1String(
POST_CONTENT) ).arg( postRandomNumber );
246 void StyleGetter::slotError(
const QString & errMsg )
249 KMessageBox::detailedError( mParent, i18n(
"An error occurred in the latest transaction." ), errMsg );
253 #include "composer/stylegetter.moc"
void publishPost(BilboPost *post)
Use this to publish a post to server.
static DBMan * self()
Retrieve the instance of DataBase Manager.
Definition of a blog post! it's implemented to decrease dependency to KBlog :)
BilboBlog * blog(int blog_id)
QString as Title, and int as blog_id.
static QString styledHtml(const int blogid, const QString &title, const QString &content)
Looks for a file named style.html for the requested blog in blogilo data directory, then writes the file content into a buffer, inserts given post title and content in the buffer, and returns it.
void sigGetStyleProgress(int percent)
While the class is fetching blog style from the web, this signal shows the operation progress...
void removePost(BilboPost *post)
Remove an existing post from server.
void sigStyleFetched()
When StyleGetter finishes all jobs to get and save a blog style, this signal will be emmited...
static const char POST_TITLE[]
static const char POST_CONTENT[]
~StyleGetter()
StyleGetter destructor.
StyleGetter(const int blogid, QObject *parent)
Creates an instance of StyleGetter, to fetch the style of the requested blog.