1 | /* |
2 | This file is part of the kblog library. |
3 | |
4 | Copyright (c) 2006-2007 Christian Weilbach <christian_weilbach@web.de> |
5 | Copyright (c) 2007 Mike McQuaid <mike@mikemcquaid.com> |
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 "blogpost.h" |
24 | #include "blogpost_p.h" |
25 | |
26 | #include "blog.h" |
27 | |
28 | #include <KDateTime> |
29 | #include <KUrl> |
30 | #include <kcal/journal.h> |
31 | |
32 | #include <QStringList> |
33 | |
34 | namespace KBlog { |
35 | |
36 | BlogPost::BlogPost( const KBlog::BlogPost &post ) |
37 | : d_ptr( new BlogPostPrivate ) |
38 | { |
39 | d_ptr->q_ptr = this; |
40 | d_ptr->mPrivate = post.isPrivate(); |
41 | d_ptr->mPostId = post.postId(); |
42 | d_ptr->mTitle = post.title(); |
43 | d_ptr->mContent = post.content(); |
44 | d_ptr->mAdditionalContent = post.additionalContent(); |
45 | d_ptr->mWpSlug = post.slug(); |
46 | d_ptr->mCategories = post.categories(); |
47 | d_ptr->mTags = post.tags(); |
48 | d_ptr->mMood = post.mood(); |
49 | d_ptr->mPermaLink = post.permaLink(); |
50 | d_ptr->mSummary = post.summary(); |
51 | d_ptr->mLink = post.link(); |
52 | d_ptr->mMusic = post.music(); |
53 | d_ptr->mTrackBackAllowed = post.isTrackBackAllowed(); |
54 | d_ptr->mCommentAllowed = post.isCommentAllowed(); |
55 | d_ptr->mError = post.error(); |
56 | d_ptr->mJournalId = post.journalId(); |
57 | d_ptr->mStatus = post.status(); |
58 | d_ptr->mCreationDateTime = post.creationDateTime(); |
59 | d_ptr->mModificationDateTime = post.modificationDateTime(); |
60 | } |
61 | |
62 | BlogPost::BlogPost( const QString &postId ) |
63 | : d_ptr( new BlogPostPrivate ) |
64 | { |
65 | d_ptr->q_ptr = this; |
66 | d_ptr->mPrivate = false; |
67 | d_ptr->mPostId = postId; |
68 | d_ptr->mStatus = New; |
69 | } |
70 | |
71 | BlogPost::BlogPost( const KCal::Journal &journal ) |
72 | : d_ptr( new BlogPostPrivate ) |
73 | { |
74 | d_ptr->q_ptr = this; |
75 | d_ptr->mPrivate = false; |
76 | d_ptr->mPostId = journal.customProperty( "KBLOG" , "ID" ); |
77 | d_ptr->mJournalId = journal.uid(); |
78 | d_ptr->mStatus = New; |
79 | d_ptr->mTitle = journal.summary(); |
80 | if ( journal.descriptionIsRich() ) { |
81 | d_ptr->mContent = d_ptr->cleanRichText( journal.description() ); |
82 | } else { |
83 | d_ptr->mContent = journal.description(); |
84 | } |
85 | d_ptr->mCategories = journal.categories(); |
86 | d_ptr->mCreationDateTime = journal.dtStart(); |
87 | } |
88 | |
89 | // BlogPost::BlogPost( const KCal::Journal &journal, BlogPostPrivate &dd ) |
90 | // : d_ptr( &dd ) |
91 | // { |
92 | // d_ptr->q_ptr = this; |
93 | // d_ptr->mPrivate = false; |
94 | // d_ptr->mPostId = journal.customProperty( "KBLOG", "ID" ); |
95 | // d_ptr->mJournalId = journal.uid(); |
96 | // d_ptr->mStatus = New; |
97 | // d_ptr->mTitle = journal.summary(); |
98 | // d_ptr->mContent = journal.description(); |
99 | // d_ptr->mCategories = journal.categories(); |
100 | // d_ptr->mCreationDateTime = journal.dtStart(); |
101 | // } |
102 | |
103 | BlogPost::~BlogPost() |
104 | { |
105 | delete d_ptr; |
106 | } |
107 | |
108 | KCal::Journal *BlogPost::journal( const Blog &blog ) const |
109 | { |
110 | QString url = blog.url().url(); |
111 | QString username = blog.username(); |
112 | QString blogId = blog.blogId(); |
113 | // Generate unique ID. Should be unique enough... |
114 | QString id = QLatin1String("kblog-" ) + url + QLatin1Char('-') + blogId + QLatin1Char('-') + username + |
115 | QLatin1Char('-') + d_ptr->mPostId; |
116 | KCal::Journal *journal = new KCal::Journal(); |
117 | journal->setUid( id ); |
118 | journal->setSummary( d_ptr->mTitle ); |
119 | journal->setCategories( d_ptr->mCategories ); |
120 | journal->setDescription( d_ptr->mContent, true ); |
121 | journal->setDtStart( d_ptr->mCreationDateTime ); |
122 | journal->setCustomProperty( "KBLOG" , "URL" , url ); |
123 | journal->setCustomProperty( "KBLOG" , "USER" , blog.username() ); |
124 | journal->setCustomProperty( "KBLOG" , "BLOG" , blogId ); |
125 | journal->setCustomProperty( "KBLOG" , "ID" , d_ptr->mPostId ); |
126 | return journal; |
127 | } |
128 | |
129 | QString BlogPost::journalId() const |
130 | { |
131 | return d_ptr->mJournalId; |
132 | } |
133 | |
134 | bool BlogPost::isPrivate() const |
135 | { |
136 | return d_ptr->mPrivate; |
137 | } |
138 | |
139 | void BlogPost::setPrivate( bool privatePost ) |
140 | { |
141 | d_ptr->mPrivate = privatePost; |
142 | } |
143 | |
144 | QString BlogPost::postId() const |
145 | { |
146 | return d_ptr->mPostId; |
147 | } |
148 | |
149 | void BlogPost::setPostId( const QString &postId ) |
150 | { |
151 | d_ptr->mPostId = postId; |
152 | } |
153 | |
154 | QString BlogPost::title() const |
155 | { |
156 | return d_ptr->mTitle; |
157 | } |
158 | |
159 | void BlogPost::setTitle( const QString &title ) |
160 | { |
161 | d_ptr->mTitle = title; |
162 | } |
163 | |
164 | QString BlogPost::content() const |
165 | { |
166 | return d_ptr->mContent; |
167 | } |
168 | |
169 | void BlogPost::setContent( const QString &content ) |
170 | { |
171 | d_ptr->mContent = content; |
172 | } |
173 | |
174 | // QString BlogPost::abbreviatedContent() const |
175 | // { |
176 | // //TODO |
177 | // return 0; |
178 | // } |
179 | // |
180 | // void BlogPost::setAbbreviatedContent( const QString &abbreviatedContent ) |
181 | // { |
182 | // Q_UNUSED( abbreviatedContent ); |
183 | // //TODO |
184 | // } |
185 | |
186 | QString BlogPost::additionalContent() const |
187 | { |
188 | return d_ptr->mAdditionalContent; |
189 | } |
190 | |
191 | void BlogPost::setAdditionalContent( const QString &additionalContent ) |
192 | { |
193 | d_ptr->mAdditionalContent = additionalContent; |
194 | } |
195 | |
196 | QString BlogPost::slug() const |
197 | { |
198 | return d_ptr->mWpSlug; |
199 | } |
200 | |
201 | void BlogPost::setSlug( const QString &slug ) |
202 | { |
203 | d_ptr->mWpSlug = slug; |
204 | } |
205 | |
206 | KUrl BlogPost::link() const |
207 | { |
208 | return d_ptr->mLink; |
209 | } |
210 | |
211 | void BlogPost::setLink( const KUrl &link ) const |
212 | { |
213 | d_ptr->mLink = link; |
214 | } |
215 | |
216 | KUrl BlogPost::permaLink() const |
217 | { |
218 | return d_ptr->mPermaLink; |
219 | } |
220 | |
221 | void BlogPost::setPermaLink( const KUrl &permalink ) const |
222 | { |
223 | d_ptr->mPermaLink = permalink; |
224 | } |
225 | |
226 | bool BlogPost::() const |
227 | { |
228 | return d_ptr->mCommentAllowed; |
229 | } |
230 | |
231 | void BlogPost::( bool ) |
232 | { |
233 | d_ptr->mCommentAllowed = commentAllowed; |
234 | } |
235 | |
236 | bool BlogPost::isTrackBackAllowed() const |
237 | { |
238 | return d_ptr->mCommentAllowed; |
239 | } |
240 | |
241 | void BlogPost::setTrackBackAllowed ( bool allowTrackBacks ) |
242 | { |
243 | d_ptr->mTrackBackAllowed = allowTrackBacks; |
244 | } |
245 | |
246 | QString BlogPost::summary() const |
247 | { |
248 | return d_ptr->mSummary; |
249 | } |
250 | |
251 | void BlogPost::setSummary( const QString &summary ) |
252 | { |
253 | d_ptr->mSummary = summary; |
254 | } |
255 | |
256 | QStringList BlogPost::tags() const |
257 | { |
258 | return d_ptr->mTags; |
259 | } |
260 | |
261 | void BlogPost::setTags( const QStringList &tags ) |
262 | { |
263 | d_ptr->mTags = tags; |
264 | } |
265 | |
266 | // QList<KUrl> BlogPost::trackBackUrls() const |
267 | // { |
268 | // //TODO |
269 | // return QList<KUrl>(); |
270 | // } |
271 | // |
272 | // void BlogPost::setTrackBackUrls( const QList<KUrl> &trackBackUrls ) |
273 | // { |
274 | // Q_UNUSED( trackBackUrls ); |
275 | // //TODO |
276 | // } |
277 | |
278 | QString BlogPost::mood() const |
279 | { |
280 | return d_ptr->mMood; |
281 | } |
282 | |
283 | void BlogPost::setMood( const QString &mood ) |
284 | { |
285 | d_ptr->mMood = mood; |
286 | } |
287 | |
288 | QString BlogPost::music() const |
289 | { |
290 | return d_ptr->mMusic; |
291 | } |
292 | |
293 | void BlogPost::setMusic( const QString &music ) |
294 | { |
295 | d_ptr->mMusic = music; |
296 | } |
297 | |
298 | QStringList BlogPost::categories() const |
299 | { |
300 | return d_ptr->mCategories; |
301 | } |
302 | |
303 | void BlogPost::setCategories( const QStringList &categories ) |
304 | { |
305 | d_ptr->mCategories = categories; |
306 | } |
307 | |
308 | KDateTime BlogPost::creationDateTime() const |
309 | { |
310 | return d_ptr->mCreationDateTime; |
311 | } |
312 | |
313 | void BlogPost::setCreationDateTime( const KDateTime &datetime ) |
314 | { |
315 | d_ptr->mCreationDateTime = datetime; |
316 | } |
317 | |
318 | KDateTime BlogPost::modificationDateTime() const |
319 | { |
320 | return d_ptr->mModificationDateTime; |
321 | } |
322 | |
323 | void BlogPost::setModificationDateTime( const KDateTime &datetime ) |
324 | { |
325 | d_ptr->mModificationDateTime = datetime; |
326 | } |
327 | |
328 | BlogPost::Status BlogPost::status() const |
329 | { |
330 | return d_ptr->mStatus; |
331 | } |
332 | |
333 | void BlogPost::setStatus( BlogPost::Status status ) |
334 | { |
335 | d_ptr->mStatus = status; |
336 | } |
337 | |
338 | QString BlogPost::error() const |
339 | { |
340 | return d_ptr->mError; |
341 | } |
342 | |
343 | void BlogPost::setError( const QString &error ) |
344 | { |
345 | d_ptr->mError = error; |
346 | } |
347 | |
348 | BlogPost &BlogPost::operator=( const BlogPost &other ) |
349 | { |
350 | BlogPost copy( other ); |
351 | swap( copy ); |
352 | return *this; |
353 | } |
354 | |
355 | QString BlogPostPrivate::cleanRichText( QString richText ) const |
356 | { |
357 | QRegExp getBodyContents( QLatin1String("<body[^>]*>(.*)</body>" ) ); |
358 | if ( getBodyContents.indexIn( richText ) ) { |
359 | // Get anything inside but excluding the body tags |
360 | richText = getBodyContents.cap( 1 ); |
361 | // Get rid of any whitespace |
362 | richText.remove( QRegExp( QLatin1String("^\\s+" ) ) ); |
363 | } |
364 | // Get rid of styled paragraphs |
365 | richText.replace( QRegExp( QLatin1String("<p style=\"[^\"]*\">" )), QLatin1String("<p>" ) ); |
366 | |
367 | // If we're left with empty content then return a clean empty string |
368 | if ( richText == QLatin1String("<p></p>" ) ) { |
369 | richText.clear(); |
370 | } |
371 | |
372 | return richText; |
373 | } |
374 | |
375 | } // namespace KBlog |
376 | |