• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

blogilo

  • sources
  • kde-4.14
  • kdepim
  • blogilo
  • src
  • composer
stylegetter.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Blogilo, A KDE Blogging Client
3 
4  It is a modified version of "weblogstylegetter.h" from
5  KBlogger project. it has been modified for use in Blogilo, at
6  February 2009.
7 
8  Copyright (C) 2007-2008-2008 by Christian Weilbach <christian_weilbach@web.de>
9  Copyright (C) 2007-2008 Antonio Aloisio <gnuton@gnuton.org>
10  Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
11  Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
12 
13  This program is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of
16  the License or (at your option) version 3 or any later version
17  accepted by the membership of KDE e.V. (or its successor approved
18  by the membership of KDE e.V.), which shall act as a proxy
19  defined in Section 14 of version 3 of the license.
20 
21 
22  This program is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  GNU General Public License for more details.
26 
27  You should have received a copy of the GNU General Public License
28  along with this program; if not, see http://www.gnu.org/licenses/
29 */
30 
31 #include "stylegetter.h"
32 #include "bilbopost.h"
33 #include "bilboblog.h"
34 #include "backend.h"
35 #include "dbman.h"
36 
37 #include <kio/job.h>
38 #include <kstandarddirs.h>
39 #include <kmessagebox.h>
40 #include <klocalizedstring.h>
41 #include <kdebug.h>
42 #include <kdatetime.h>
43 
44 #include <QFile>
45 
46 
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-";
49 
50 StyleGetter::StyleGetter( const int blogid, QObject *parent )
51  : QObject( parent )
52 {
53  kDebug();
54  BilboBlog *tempBlog = DBMan::self()->blog( blogid );
55  if ( tempBlog->isError() ) {
56  KMessageBox::detailedError( mParent, i18n( "Cannot fetch the selected blog style."),
57  DBMan::self()->lastErrorText() );
58  return;
59  }
60  // sets cachePath to ~/.kde4/share/apps/bilbo/blog_host_name/
61 // QString blogDir = DBMan::self()->getBlogInfo( blogid ).url().host();
62 // QString blogDir = tempBlog.url().host();
63 // kDebug() << blogDir;
64 // mCachePath = KStandardDirs::locateLocal( "data", "bilbo/" + blogDir + '/' , true );
65  QString url = QString::fromLatin1("blogilo/%1/" ).arg( blogid );
66  mCachePath = KStandardDirs::locateLocal( "data", url , true );
67  generateRandomPostStrings();
68  mParent = qobject_cast< QWidget* >( parent );
69  Q_ASSERT( mParent );
70 
71  // create temp post
72 
73  mTempPost = new BilboPost();
74  mTempPost->setTitle( mPostTitle );
75  mTempPost->setContent( mPostContent );
76  mTempPost->setPrivate( false );
77 
78  if ( ( tempBlog->api() == BilboBlog::MOVABLETYPE_API ) ||
79  ( tempBlog->api() == BilboBlog::WORDPRESSBUGGY_API ) ) {
80  mTempPost->setCreationDateTime( KDateTime( QDate(2000, 1, 1), QTime(0, 0), KDateTime::UTC ) );
81  }
82 
83  b = new Backend( blogid );
84  connect( b, SIGNAL(sigPostPublished(int,BilboPost*)), this,
85  SLOT(slotTempPostPublished(int,BilboPost*)) );
86  connect( b, SIGNAL(sigError(QString)), this, SLOT(slotError(QString)) );
87 
88  Q_EMIT sigGetStyleProgress( 10 );
89 
90  b->publishPost( mTempPost );
91 }
92 
93 StyleGetter::~StyleGetter()
94 {
95  kDebug();
96 }
97 
98 QString StyleGetter::styledHtml( const int blogid,
99  const QString &title,
100  const QString &content )
101 {
102  kDebug();
103 // BilboBlog tempBlog = DBMan::self()->getBlogInfo( blogid );
104 // if ( tempBlog.isError() ) {
105 // kDebug() << DBMan::self()->lastErrorText();
106 // return "<html><body><b>" + title + "</b><br>" + content + "</html>";
107 // }
108 //
109 // QString blogDir = tempBlog.url().host();
110  //QString url = QString( "bilbo/%1/" ).arg( blogid );
111  QString url = QString::fromLatin1("blogilo/%1/" ).arg( blogid );
112  url = KStandardDirs::locateLocal( "data", url , true );
113  KUrl dest( url );
114  dest.addPath(QLatin1String("style.html"));
115  dest.setScheme(QLatin1String("file"));
116 
117  if ( !dest.isValid() ) {
118  return QLatin1String("<html><body><h2 align='center'>") + title + QLatin1String("</h2><br>") + content + QLatin1String("</html>");
119  }
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>");
123  }
124 
125  QString buffer;
126  while ( !file.atEnd() ) {
127  QByteArray line = file.readLine();
128  buffer.append( QString::fromUtf8( line ) );
129  }
130 
131  QRegExp typeRx ( QLatin1String( "(TYPE[^>]+>)" ) );
132  buffer.remove( typeRx );
133 
134  QRegExp titleRx( QString::fromLatin1( "%1[\\d]*" ).arg( QLatin1String(POST_TITLE) ) );
135  QRegExp contentRx( QString::fromLatin1( "%1[\\d]*" ).arg( QLatin1String(POST_CONTENT )) );
136 
137  buffer.replace( titleRx, title );
138  buffer.replace( contentRx, content );
139 
140  return buffer;
141 }
142 
143 void StyleGetter::slotTempPostPublished( int blogId, BilboPost* post )
144 {
145  kDebug();
146 
147  KUrl postUrl;
148 // postUrl = post->permaLink();
149  postUrl = post->link();
150  if ( postUrl.isEmpty() ) {
151  kDebug() << "link was empty";
152 // postUrl = post->link();
153  postUrl = post->permaLink();
154  if ( postUrl.isEmpty() ) {
155  kDebug() << "permaLink was empty";
156  postUrl = KUrl( DBMan::self()->blog(blogId)->blogUrl() );
157  }
158  }
159 
160  Q_EMIT sigGetStyleProgress( 30 );
161 
162  mTempPost = post;
163  KIO::StoredTransferJob *job = KIO::storedGet( postUrl, KIO::NoReload, KIO::HideProgressInfo );
164  connect( job, SIGNAL(result(KJob*)),
165  this, SLOT(slotHtmlCopied(KJob*)) );
166 
167 }
168 
169 void StyleGetter::slotHtmlCopied( KJob *job )
170 {
171  kDebug();
172  if ( job->error() ) {
173  KMessageBox::detailedError( mParent, i18n( "Cannot get html file."),
174  job->errorString() );
175  sender()->deleteLater();
176  return;
177  }
178 
179  Q_EMIT sigGetStyleProgress( 50 );
180 
181  QByteArray httpData( qobject_cast<KIO::StoredTransferJob*>( job )->data() );
182 
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("\"/>") );
187 
188  QRegExp rxBase( QLatin1String("(<base\\shref=[^>]+>)") );
189  if ( rxBase.indexIn( QLatin1String(httpData) ) != -1 ) {
190  httpData.replace( rxBase.cap( 1 ).toLatin1(), base.toLatin1() );
191  }
192  else {
193  int headOffset = httpData.indexOf( "<head>" );
194  httpData.insert( headOffset + 6, base.toLatin1() );
195  }
196 
197  QFile file( mCachePath + QLatin1String("style.html") );
198 // Q_ASSERT( dest.isValid() );
199  if ( file.exists() ) {
200  file.remove();
201  }
202  if (!file.open( QIODevice::WriteOnly ) ) {
203  KMessageBox::error( mParent,
204  i18n( "Cannot write data to file %1", file.fileName() ) );
205  return;
206  }
207  if ( file.write( httpData ) == -1 ) {
208  KMessageBox::error( mParent,
209  i18n( "Cannot write data to file %1", file.fileName() ) );
210 
211  file.close();
212  return;
213  }
214  file.close();
215  Q_EMIT sigGetStyleProgress( 70 );
216 // Q_EMIT sigStyleFetched();
217 
218 
219  //Remove temp post from the server.
220  connect( b, SIGNAL(sigPostRemoved(int,BilboPost)), this,
221  SLOT(slotTempPostRemoved(int,BilboPost)) );
222  b->removePost( mTempPost );
223 }
224 
225 void StyleGetter::slotTempPostRemoved( int blog_id, const BilboPost &post)
226 {
227  Q_UNUSED( blog_id );
228  Q_UNUSED( post );
229 
230  delete mTempPost;
231  b->deleteLater();
232 
233  Q_EMIT sigGetStyleProgress( 100 );
234  Q_EMIT sigStyleFetched();
235 }
236 
237 void StyleGetter::generateRandomPostStrings()
238 {
239  kDebug();
240  srand( time( 0 ) );
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 );
244 }
245 
246 void StyleGetter::slotError( const QString & errMsg )
247 {
248  kDebug();
249  KMessageBox::detailedError( mParent, i18n( "An error occurred in the latest transaction." ), errMsg );
250  b->deleteLater();
251 }
252 
QWidget
QString::append
QString & append(QChar ch)
QByteArray
QObject::sender
QObject * sender() const
Backend::publishPost
void publishPost(BilboPost *post)
Use this to publish a post to server.
Definition: backend.cpp:159
BilboBlog::isError
bool isError() const
Definition: bilboblog.cpp:115
DBMan::self
static DBMan * self()
Retrieve the instance of DataBase Manager.
Definition: dbman.cpp:117
QString::remove
QString & remove(int position, int n)
BilboPost
Definition of a blog post! it's implemented to decrease dependency to KBlog :)
Definition: bilbopost.h:41
QTime
DBMan::blog
BilboBlog * blog(int blog_id)
QString as Title, and int as blog_id.
Definition: dbman.cpp:1011
BilboBlog::WORDPRESSBUGGY_API
Definition: bilboblog.h:45
QFile
bilbopost.h
StyleGetter::styledHtml
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.
Definition: stylegetter.cpp:98
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QRegExp
BilboBlog
Blog definition class!
Definition: bilboblog.h:40
QString::fromUtf8
QString fromUtf8(const char *str, int size)
StyleGetter::sigGetStyleProgress
void sigGetStyleProgress(int percent)
While the class is fetching blog style from the web, this signal shows the operation progress...
Backend::removePost
void removePost(BilboPost *post)
Remove an existing post from server.
Definition: backend.cpp:311
QObject
StyleGetter::sigStyleFetched
void sigStyleFetched()
When StyleGetter finishes all jobs to get and save a blog style, this signal will be emmited...
QDate
POST_TITLE
static const char POST_TITLE[]
Definition: stylegetter.cpp:47
QObject::deleteLater
void deleteLater()
QString
bilboblog.h
POST_CONTENT
static const char POST_CONTENT[]
Definition: stylegetter.cpp:48
Backend
Engine of application.
Definition: backend.h:45
dbman.h
QString::replace
QString & replace(int position, int n, QChar after)
QLatin1String
QString::fromLatin1
QString fromLatin1(const char *str, int size)
StyleGetter::~StyleGetter
~StyleGetter()
StyleGetter destructor.
Definition: stylegetter.cpp:93
stylegetter.h
backend.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
StyleGetter::StyleGetter
StyleGetter(const int blogid, QObject *parent)
Creates an instance of StyleGetter, to fetch the style of the requested blog.
Definition: stylegetter.cpp:50
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
BilboBlog::MOVABLETYPE_API
Definition: bilboblog.h:45
BilboBlog::api
ApiType api() const
Definition: bilboblog.cpp:175
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:16 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

blogilo

Skip menu "blogilo"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



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