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

blogilo

  • sources
  • kde-4.12
  • kdepim
  • blogilo
  • src
dbman.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Blogilo, A KDE Blogging Client
3 
4  Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5  Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License or (at your option) version 3 or any later version
11  accepted by the membership of KDE e.V. (or its successor approved
12  by the membership of KDE e.V.), which shall act as a proxy
13  defined in Section 14 of version 3 of the license.
14 
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, see http://www.gnu.org/licenses/
23 */
24 //krazy:excludeall=crashy to skip false positives due to QSqlQuery.exec() usage
25 
26 #include "dbman.h"
27 #include "bilboblog.h"
28 #include "bilbopost.h"
29 
30 #include <kdebug.h>
31 #include <KDE/KLocale>
32 #include <kdatetime.h>
33 #include <kurl.h>
34 #include <kwallet.h>
35 #include <kio/deletejob.h>
36 #include <kmessagebox.h>
37 
38 #include <QSqlError>
39 #include <QSqlQuery>
40 #include <QFile>
41 #include <QSqlDatabase>
42 
43 class DBMan::Private
44 {
45 public:
46  Private()
47  : mWallet(0),
48  useWallet(false)
49  {
50  }
51  KWallet::Wallet* mWallet;
52  QString mLastErrorText;
53  bool useWallet;
54  QMap<int, BilboBlog*> mBlogList;
55  QSqlDatabase db;
56 };
57 
58 DBMan::DBMan()
59  : d(new Private)
60 {
61  d->mWallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
62  if ( d->mWallet ) {
63  d->useWallet = true;
64  if ( !d->mWallet->hasFolder( QLatin1String("blogilo") ) ) {
65  d->mWallet->createFolder( QLatin1String("blogilo") );
66  }
67  d->mWallet->setFolder( QLatin1String("blogilo") );
68  kDebug() << "Wallet successfully opened.";
69  } else {
70  d->useWallet = false;
71  kDebug() << "Could not use Wallet service, will use database to store passwords";
72  }
73 
74  if ( !QFile::exists( CONF_DB ) ) {
75  if ( !this->createDB() ) {
76  KMessageBox::detailedError( 0, i18n( "Cannot create database" ),
77  i18n( d->db.lastError().text().toUtf8().data() ) );
78  kDebug() << "Cannot create database, SQL error: " << d->db.lastError().text() << endl;
79  exit ( 1 );
80  }
81  } else if ( !connectDB() )
82  exit( 1 );
83 
84  reloadBlogList();
85 }
86 
87 DBMan::~DBMan()
88 {
89  kDebug();
90  d->db.close();
91  if(d->useWallet) {
92  d->mWallet->deleteLater();
93  d->mWallet = 0;
94  }
95  delete d;
96  mSelf = 0L;
97 }
98 
99 
100 QString DBMan::lastErrorText() const
101 {
102  return d->mLastErrorText;
103 }
104 
105 DBMan * DBMan::mSelf = 0L;
106 
107 DBMan * DBMan::self()
108 {
109  if ( !mSelf )
110  mSelf = new DBMan;
111  return mSelf;
112 }
113 
114 const QMap<int, BilboBlog*> & DBMan::blogList() const
115 {
116  return d->mBlogList;
117 }
118 
119 void DBMan::reloadBlogList()
120 {
121  d->mBlogList.clear();
122  QList<BilboBlog*> listBlogs = this->listBlogs();
123  const int count = listBlogs.count();
124  for ( int i = 0; i < count; ++i ) {
125  d->mBlogList [ listBlogs.at(i)->id() ] = listBlogs.at(i);
126  }
127 }
128 
129 bool DBMan::connectDB()
130 {
131  kDebug();
132  if( d->db.isOpen() )
133  return true;
134  d->db = QSqlDatabase::addDatabase( QLatin1String("QSQLITE") );
135  d->db.setDatabaseName( QString(CONF_DB) );
136 
137  if ( !d->db.open() ) {
138  KMessageBox::detailedError( 0, i18n( "Cannot connect to database" ),
139  i18n( d->db.lastError().text().toUtf8().data() ) );
140  kDebug() << "Cannot connect to database, SQL error: " << d->db.lastError().text();
141  return false;
142  }
143  return true;
144 }
145 
146 
153 bool DBMan::createDB()
154 {
155  kDebug();
156  bool ret = true;
157  if ( !connectDB() )
158  exit( 1 );
159 
160  QSqlQuery q;
162  if ( !q.exec( QLatin1String("CREATE TABLE blog (id INTEGER PRIMARY KEY, blogid TEXT, blog_url TEXT, username TEXT,\
163  password TEXT, style_url TEXT, api_type TEXT, title TEXT, direction TEXT,\
164  local_directory TEXT, icon_url TEXT)") ) ) {
165  ret = false;
166  d->mLastErrorText = q.lastError().text();
167  }
168 
170  if ( !q.exec( QLatin1String("CREATE TABLE post (id INTEGER PRIMARY KEY, postid TEXT NOT NULL, blog_id NUMERIC NOT NULL,\
171  author TEXT, slug TEXT, post_password TEXT, title TEXT, content TEXT, text_more TEXT,\
172  c_time TEXT, m_time TEXT, is_private NUMERIC, is_comment_allowed NUMERIC,\
173  is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
174  status NUMERIC, trackback_urls TEXT, UNIQUE(postid, blog_id));") ) ) {
175  ret = false;
176  d->mLastErrorText = q.lastError().text();
177  }
178 
180  if ( !q.exec( QLatin1String("CREATE TABLE comment (id INTEGER PRIMARY KEY, commentid TEXT NOT NULL, blog_id NUMERIC NOT NULL,\
181  postId TEXT, author_name TEXT, author_url TEXT, author_email TEXT, title TEXT, content TEXT,\
182  c_time TEXT, m_time TEXT, link TEXT, password TEXT,\
183  status NUMERIC, UNIQUE(commentid, blog_id));" ) ) ){
184  ret = false;
185  d->mLastErrorText = q.lastError().text();
186  }
187 
189  if ( !q.exec( QLatin1String("CREATE TABLE category (catid INTEGER PRIMARY KEY, name TEXT NOT NULL,\
190  description TEXT, htmlUrl TEXT, rssUrl TEXT, categoryId TEXT, parentId TEXT,\
191  blog_id NUMERIC NOT NULL, UNIQUE(name,blog_id));" ) ) ) {
192  ret = false;
193  d->mLastErrorText = q.lastError().text();
194  }
195 
197  if( !q.exec( QLatin1String("CREATE TABLE file (fileid INTEGER PRIMARY KEY, name TEXT, blog_id NUMERIC, is_uploaded NUMERIC,\
198  local_url TEXT, remote_url TEXT, mime_type TEXT);" ) ) ) {
199  ret = false;
200  d->mLastErrorText = q.lastError().text();
201  }
202 
204  if ( !q.exec( QLatin1String("CREATE TABLE post_cat (blogId TEXT NOT NULL, postId TEXT NOT NULL,\
205  categoryId TEXT NOT NULL, UNIQUE(blogId,postId,categoryId));" ) ) ) {
206  ret = false;
207  d->mLastErrorText = q.lastError().text();
208  }
209 
211  if ( !q.exec( QLatin1String("CREATE TABLE post_file (post_id INTEGER, file_id INTEGER);" ) ) ) {
212  ret = false;
213  d->mLastErrorText = q.lastError().text();
214  }
215 
217  if( !q.exec( QLatin1String("CREATE TABLE local_post (local_id INTEGER PRIMARY KEY, id INTEGER UNIQUE, postid TEXT, blog_id NUMERIC,\
218  author TEXT, slug TEXT, post_password TEXT, title TEXT, content TEXT, text_more TEXT,\
219  c_time TEXT, m_time TEXT, is_private NUMERIC, is_comment_allowed NUMERIC,\
220  is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
221  status NUMERIC);" ) ) ) {
222  ret = false;
223  d->mLastErrorText = q.lastError().text();
224  }
225 
227  if( !q.exec( QLatin1String("CREATE TABLE local_post_cat (local_id INT, categoryId TEXT);" ) ) ) {
228  ret = false;
229  d->mLastErrorText = q.lastError().text();
230  }
231 
233  if( !q.exec( QLatin1String("CREATE TABLE temp_post (local_id INTEGER PRIMARY KEY, id INTEGER UNIQUE, postid TEXT, blog_id NUMERIC,\
234  author TEXT, slug TEXT, post_password TEXT, title TEXT, content TEXT, text_more TEXT,\
235  c_time TEXT, m_time TEXT, is_private NUMERIC, is_comment_allowed NUMERIC,\
236  is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
237  status NUMERIC);" ) ) ){
238  ret = false;
239  d->mLastErrorText = q.lastError().text();
240  }
241 
243  if( !q.exec( QLatin1String("CREATE TABLE temp_post_cat (local_id INT, categoryId TEXT);" ) ) ) {
244  ret = false;
245  d->mLastErrorText = q.lastError().text();
246  }
247 
249  q.exec( QLatin1String("CREATE TRIGGER delete_post AFTER DELETE ON post\
250  BEGIN\
251  DELETE FROM post_cat WHERE post_cat.postId=OLD.postid;\
252  DELETE FROM post_file WHERE post_file.post_id=OLD.id;\
253  DELETE FROM comment WHERE comment.postId=OLD.postid;\
254  END" ) );
255  q.exec( QLatin1String("CREATE TRIGGER delete_blog AFTER DELETE ON blog \
256  BEGIN\
257  DELETE FROM category WHERE category.blog_id=OLD.id;\
258  DELETE FROM file WHERE file.blog_id=OLD.id;\
259  DELETE FROM post WHERE post.blog_id=OLD.id;\
260  DELETE FROM comment WHERE comment.blog_id=OLD.id;\
261  END" ) );
262  q.exec( QLatin1String("CREATE TRIGGER delete_temp_post AFTER DELETE ON temp_post \
263  BEGIN\
264  DELETE FROM temp_post_cat WHERE local_id=OLD.local_id;\
265  END" ));
266  q.exec( QLatin1String("CREATE TRIGGER delete_local_post AFTER DELETE ON local_post \
267  BEGIN\
268  DELETE FROM local_post_cat WHERE local_id=OLD.local_id;\
269  END" ));
270 
271  return ret;
272 }
273 
274 int DBMan::addBlog( const BilboBlog & blog )
275 {
276  QSqlQuery q;
277  if( d->useWallet ) {
278  q.prepare( QLatin1String("INSERT INTO blog (blogid, blog_url, username, style_url, api_type, title,\
279  direction, local_directory) VALUES(?, ?, ?, ?, ?, ?, ?, ?)" ) );
280  if ( d->mWallet && d->mWallet->writePassword( blog.url().url() + QLatin1Char('_') + blog.username(), blog.password() ) == 0 )
281  kDebug() << "Password stored to kde wallet";
282  else
283  return -1;
284  } else {
285  q.prepare( QLatin1String("INSERT INTO blog (password, blogid, blog_url, username, style_url, api_type, title,\
286  direction, local_directory) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)" ) );
287  q.addBindValue( blog.password() );
288  }
289  q.addBindValue( blog.blogid() );
290  q.addBindValue( blog.url().url() );
291  q.addBindValue( blog.username() );
292  q.addBindValue( blog.blogUrl() );
293  q.addBindValue( blog.api() );
294  q.addBindValue( blog.title() );
295  q.addBindValue( blog.direction() );
296  q.addBindValue( blog.localDirectory() );
297 
298  if ( q.exec() ) {
299  reloadBlogList();
300  return q.lastInsertId().toInt();
301  } else {
302  d->mLastErrorText = q.lastError().text();
303  return -1;
304  }
305 }
306 
307 bool DBMan::editBlog( const BilboBlog & blog )
308 {
309  QSqlQuery q;
310  if( d->useWallet ) {
311  q.prepare( QLatin1String("UPDATE blog SET blogid=?, blog_url=?, username=? , style_url=? , api_type=?, \
312  title=?, direction=?, local_directory=? WHERE id=?" ) );
313  if ( d->mWallet && d->mWallet->writePassword( blog.url().url() + QLatin1Char('_') + blog.username(), blog.password() ) == 0 )
314  kDebug() << "Password stored to kde wallet";
315  else
316  return false;
317  } else {
318  q.prepare( QLatin1String("UPDATE blog SET password=?, blogid=?, blog_url=?, username=? , style_url=? , api_type=?, \
319  title=?, direction=?, local_directory=? WHERE id=?" ));
320  q.addBindValue( blog.password() );
321  }
322  q.addBindValue( blog.blogid() );
323  q.addBindValue( blog.url().url() );
324  q.addBindValue( blog.username() );
325  q.addBindValue( blog.blogUrl() );
326  q.addBindValue( blog.api() );
327  q.addBindValue( blog.title() );
328  q.addBindValue( blog.direction() );
329  q.addBindValue( blog.localDirectory() );
330  q.addBindValue( blog.id() );
331 
332  bool res = q.exec();
333  if ( !res ) {
334  d->mLastErrorText = q.lastError().text();
335  kDebug() << q.lastError().text();
336  return res;
337  }
338  reloadBlogList();
339  return res;
340 }
341 
342 bool DBMan::removeBlog( int blog_id )
343 {
344  BilboBlog *tmp = d->mBlogList[ blog_id ];
345  if( d->useWallet ) {
346  if ( d->mWallet && d->mWallet->removeEntry( tmp->url().url() + QLatin1Char('_') + tmp->username() ) == 0 )
347  kDebug() << "Password removed to kde wallet";
348  }
349  QSqlQuery q;
350  q.prepare( QLatin1String("DELETE FROM blog WHERE id=?" ));
351  q.addBindValue( blog_id );
352  bool res = q.exec();
353  if ( !res ) {
354  d->mLastErrorText = q.lastError().text();
355  kDebug() << q.lastError().text();
356  return res;
357  }
358  QString path = KStandardDirs::locateLocal( "data", QString::fromLatin1( "blogilo/%1/" ).arg( blog_id ) , false );
359  KIO::del(KUrl(path), KIO::HideProgressInfo);
360  reloadBlogList();
361  return res;
362 }
363 
364 int DBMan::addPost( const BilboPost & post, int blog_id )
365 {
366  kDebug() << "Adding post with title: " << post.title() << " to Blog " << blog_id;
367  QSqlQuery q;
368  q.prepare( QLatin1String("INSERT OR REPLACE INTO post (postid, blog_id, author, title, content, text_more, c_time, m_time,\
369  is_private, is_comment_allowed, is_trackback_allowed, link, perma_link, summary, slug,\
370  tags, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ) );
371  q.addBindValue( post.postId() );
372  q.addBindValue( blog_id );
373  q.addBindValue( post.author() );
374  q.addBindValue( post.title() );
375  q.addBindValue( post.content() );
376  q.addBindValue( post.additionalContent() );
377  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
378  q.addBindValue( ( post.modificationDateTime().isNull() ? post.creationDateTime().toString( KDateTime::ISODate ) :
379  post.modificationDateTime().toString( KDateTime::ISODate )) );
380  q.addBindValue( post.isPrivate() );
381  q.addBindValue( post.isCommentAllowed() );
382  q.addBindValue( post.isTrackBackAllowed() );
383  q.addBindValue( post.link().url() );
384  q.addBindValue( post.permaLink().url() );
385  q.addBindValue( post.summary() );
386  q.addBindValue( post.slug() );
387  q.addBindValue( post.tags().join(QLatin1String(",")) );
388  q.addBindValue( post.status() );
389 
390  int ret;
391  if ( q.exec() ) {
392  ret = q.lastInsertId().toInt();
393 
395  QSqlQuery qd;
396  qd.prepare( QLatin1String("DELETE FROM post_cat WHERE postId=? AND blogId=(SELECT blogid FROM blog where id=?)" ));
397  qd.addBindValue(post.postId());
398  qd.addBindValue(blog_id);
399  if ( !qd.exec() ) {
400  d->mLastErrorText = qd.lastError().text();
401  kError() << "Cannot delete previous categories.";
402  }
403 
404  int cat_count = post.categories().count();
405  if( cat_count > 0 ) {
406 // kDebug()<< "Adding "<<cat_count<<" category to post.";
407  QSqlQuery q2;
408  q2.prepare( QLatin1String("INSERT OR REPLACE INTO post_cat (blogId, postId, categoryId)\
409  VALUES((SELECT blogid FROM blog where id=?), ?, \
410  (SELECT categoryId FROM category WHERE name = ? AND blog_id= ?))" ) );
411  for ( int i = 0; i < cat_count; ++i ) {
412  q2.addBindValue(blog_id);
413  q2.addBindValue(post.postId());
414  q2.addBindValue(post.categories()[i]);
415  q2.addBindValue(blog_id);
416  if ( !q2.exec() ) {
417  kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
418  d->mLastErrorText = q.lastError().text();
419  }
420  }
421  }
422  } else {
423  d->mLastErrorText = q.lastError().text();
424  kDebug() << "Cannot Add post to database!\n\tSQL Error: " << q.lastError().text();
425  ret = -1;
426  }
427 
428  return ret;
429 }
430 
431 bool DBMan::editPost( const BilboPost & post, int blog_id )
432 {
433  kDebug();
434  QSqlQuery q;
435  q.prepare( QLatin1String("UPDATE post SET author=?, title=?, content=?, text_more=?, c_time=?, m_time=?,\
436  is_private=?, is_comment_allowed=?, is_trackback_allowed=?, link=?, perma_link=?, summary=?,\
437  slug=?, tags=?, status=? WHERE postid=? AND blog_id=?" ) );
438  q.addBindValue( post.author() );
439  q.addBindValue( post.title() );
440  q.addBindValue( post.content() );
441  q.addBindValue( post.additionalContent() );
442  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
443  q.addBindValue( post.modificationDateTime().toString( KDateTime::ISODate ) );
444  q.addBindValue( post.isPrivate() );
445  q.addBindValue( post.isCommentAllowed() );
446  q.addBindValue( post.isTrackBackAllowed() );
447  q.addBindValue( post.link().url() );
448  q.addBindValue( post.permaLink().url() );
449  q.addBindValue( post.summary() );
450  q.addBindValue( post.slug() );
451  q.addBindValue( post.tags().join(QLatin1String(",")) );
452  q.addBindValue( post.status() );
453 
454  q.addBindValue( post.postId() );
455  q.addBindValue( blog_id );
456 
457  if ( !q.exec() ) {
458  d->mLastErrorText = q.lastError().text();
459  kDebug()<<"Modifying post failed, SQL ERROR: "<< d->mLastErrorText;
460  return false;
461  }
462 
464  QSqlQuery qd;
465  qd.prepare( QLatin1String("DELETE FROM post_cat WHERE postId=? AND blogId=(SELECT blogid FROM blog where id=?)" ));
466  qd.addBindValue(post.postId());
467  qd.addBindValue(blog_id);
468  if ( !qd.exec() ) {
469  d->mLastErrorText = qd.lastError().text();
470  kDebug() << "Cannot delete previous categories.";
471  }
472 
474 
475  int cat_count = post.categories().count();
476  if( cat_count > 0 ) {
477 // kDebug()<< "Adding "<<cat_count<<" category to post.";
478  QSqlQuery q2;
479  q2.prepare( QLatin1String("INSERT OR REPLACE INTO post_cat (blogId, postId, categoryId)\
480  VALUES((SELECT blogid FROM blog where id=?), ?, \
481  (SELECT categoryId FROM category WHERE name = ? AND blog_id= ?))" ) );
482  for ( int i = 0; i < cat_count; ++i ) {
483  q2.addBindValue(blog_id);
484  q2.addBindValue(post.postId());
485  q2.addBindValue(post.categories()[i]);
486  q2.addBindValue(blog_id);
487  if ( !q2.exec() ) {
488  d->mLastErrorText = q2.lastError().text();
489  kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
490  }
491  }
492  }
493 
494  return true;
495 }
496 
497 bool DBMan::removePost( int id )
498 {
499  QSqlQuery q;
500  q.prepare( QLatin1String("DELETE FROM post WHERE id=?" ));
501  q.addBindValue( id );
502  bool res = q.exec();
503  if ( !res ) {
504  d->mLastErrorText = q.lastError().text();
505  kDebug() << d->mLastErrorText;
506  }
507  return res;
508 }
509 
510 bool DBMan::removePost( int blog_id, const QString &postId)
511 {
512  QSqlQuery q;
513  q.prepare( QLatin1String("DELETE FROM post WHERE blog_id=? AND postId=?" ));
514  q.addBindValue( blog_id );
515  q.addBindValue( postId );
516  bool res = q.exec();
517  if ( !res ) {
518  d->mLastErrorText = q.lastError().text();
519  kDebug() << d->mLastErrorText;
520  }
521  return res;
522 }
523 
524 bool DBMan::clearPosts( int blog_id )
525 {
526  QSqlQuery q;
527  q.prepare( QLatin1String("DELETE FROM post WHERE blog_id=?") );
528  q.addBindValue( blog_id );
529  bool res = q.exec();
530  if ( !res ) {
531  d->mLastErrorText = q.lastError().text();
532  kDebug() << q.lastError().text();
533  }
534  return res;
535 }
536 
537 int DBMan::addCategory( const QString &name, const QString &description, const QString &htmlUrl,
538  const QString &rssUrl, const QString &categoryId, const QString &parentId, int blog_id )
539 {
540  QSqlQuery q;
541  q.prepare( QLatin1String("INSERT OR REPLACE INTO category (name, description, htmlUrl, rssUrl, categoryId, parentId, blog_id)\
542  VALUES(?, ?, ?, ?, ?, ?, ?)" ) );
543  q.addBindValue( name );
544  q.addBindValue( description );
545  q.addBindValue( htmlUrl );
546  q.addBindValue( rssUrl );
547  q.addBindValue( categoryId );
548  q.addBindValue( parentId );
549  q.addBindValue( blog_id );
550 
551  if ( q.exec() )
552  return q.lastInsertId().toInt();
553  else {
554  d->mLastErrorText = q.lastError().text();
555  return -1;
556  }
557 }
558 
559 bool DBMan::clearCategories( int blog_id )
560 {
561  QSqlQuery q;
562  q.prepare( QLatin1String("DELETE FROM category WHERE blog_id=?") );
563  q.addBindValue( blog_id );
564  bool res = q.exec();
565  if ( !res ) {
566  d->mLastErrorText = q.lastError().text();
567  kDebug() << q.lastError().text();
568  }
569  return res;
570 }
571 
572 int DBMan::addFile( const QString& name, int blog_id, bool isUploaded, const QString& localUrl, const QString& remoteUrl )
573 // int DBMan::addFile( const QString &name, int blog_id, bool isLocal, const QString &localUrl, const QString &remoteUrl )
574 {
575  QSqlQuery q;
576 // q.prepare("INSERT INTO file(name, blog_id, is_uploaded, local_url, remote_url) VALUES(?, ?, ?, ?, ?)");
577  q.prepare( QLatin1String("INSERT INTO file(name, blog_id, is_local, local_url, remote_url) VALUES(?, ?, ?, ?, ?)") );
578  q.addBindValue( name );
579  q.addBindValue( blog_id );
580  q.addBindValue( isUploaded );
581 // q.addBindValue( isLocal );
582  q.addBindValue( localUrl );
583  q.addBindValue( remoteUrl );
584 
585  if ( q.exec() )
586  return q.lastInsertId().toInt();
587  else {
588  d->mLastErrorText = q.lastError().text();
589  return -1;
590  }
591 }
592 
593 int DBMan::addFile(const BilboMedia & file)
594 {
595  QSqlQuery q;
596  q.prepare( QLatin1String("INSERT INTO file(name, blog_id, is_local, local_url, remote_url) VALUES(?, ?, ?, ?, ?)") );
597  q.addBindValue( file.name() );
598  q.addBindValue( file.blogId() );
599  q.addBindValue( file.isUploaded() );
600  q.addBindValue( file.localUrl() );
601  q.addBindValue( file.remoteUrl() );
602 
603  if ( q.exec() )
604  return q.lastInsertId().toInt();
605  else {
606  d->mLastErrorText = q.lastError().text();
607  return -1;
608  }
609 }
610 
611 bool DBMan::removeFile( int fileid )
612 {
613  QSqlQuery q;
614  q.prepare( QLatin1String("DELETE FROM file WHERE fileid=?") );
615  q.addBindValue( fileid );
616  bool res = q.exec();
617  if ( !res ) {
618  d->mLastErrorText = q.lastError().text();
619  kDebug() << q.lastError().text();
620  }
621  return res;
622 }
623 
624 bool DBMan::clearFiles( int blog_id )
625 {
626  QSqlQuery q;
627  q.prepare( QLatin1String("DELETE FROM file WHERE blog_id=?") );
628  q.addBindValue( blog_id );
629  bool res = q.exec();
630  if ( !res ) {
631  d->mLastErrorText = q.lastError().text();
632  kDebug() << q.lastError().text();
633  }
634  return res;
635 }
636 
637 int DBMan::saveLocalEntry( const BilboPost& post, int blog_id )
638 {
639  return saveTemp_LocalEntry(post, blog_id, Local);
640 }
641 
642 int DBMan::saveTempEntry( const BilboPost& post, int blog_id )
643 {
644  return saveTemp_LocalEntry(post, blog_id, Temp);
645 }
646 
647 int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPostState state )
648 {
649  kDebug();
650  QSqlQuery q;
651  BilboPost post = basePost;
652 // kDebug()<<"postId: "<<post.postId();
653  QString postTable, postCatTable;
654  if(state == Local) {
655  postTable = QLatin1String("local_post");
656  postCatTable = QLatin1String("local_post_cat");
657  } else {
658  postTable = QLatin1String("temp_post");
659  postCatTable = QLatin1String("temp_post_cat");
660  }
661  int localId = post.localId();
662 // if(post.status() == KBlog::BlogPost::New) {///Post is new!
663 // kDebug()<<"Post is new!";
664  if(post.localId() == -1){
666  kDebug()<<"Add new post to temp_post";
667  q.prepare( QLatin1String("INSERT OR REPLACE INTO ")+ postTable +QLatin1String(" (postid, blog_id,\
668  author, title, content, text_more, c_time, m_time, is_private, is_comment_allowed,\
669  is_trackback_allowed, link, perma_link, summary, slug, tags, status)\
670  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ));
671 // q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
672  q.addBindValue( post.postId() );
673  q.addBindValue( blog_id );
674  q.addBindValue( post.author() );
675  q.addBindValue( post.title() );
676  q.addBindValue( post.content() );
677  q.addBindValue( post.additionalContent() );
678  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
679  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
680  q.addBindValue( post.isPrivate() );
681  q.addBindValue( post.isCommentAllowed() );
682  q.addBindValue( post.isTrackBackAllowed() );
683  q.addBindValue( post.link().url() );
684  q.addBindValue( post.permaLink().url() );
685  q.addBindValue( post.summary() );
686  q.addBindValue( post.slug() );
687  q.addBindValue( post.tags().join(QLatin1String(",")) );
688  q.addBindValue( post.status() );
689 
690  if ( q.exec() ) {
691  localId = q.lastInsertId().toInt();
692  } else {
693  d->mLastErrorText = q.lastError().text();
694  kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
695  return -1;
696  }
697  } else {
699  kDebug()<<"Update post, with id!";
700  q.prepare( QLatin1String("UPDATE ")+ postTable +QLatin1String(" SET postid=?, blog_id=?,\
701  author=?, title=?, content=?, text_more=?, c_time=?, m_time=?, is_private=?, is_comment_allowed=?,\
702  is_trackback_allowed=?, link=?, perma_link=?, summary=?, slug=?, tags=?, status=?\
703  WHERE local_id=?" ));
704 // q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
705  q.addBindValue( post.postId() );
706  q.addBindValue( blog_id );
707  q.addBindValue( post.author() );
708  q.addBindValue( post.title() );
709  q.addBindValue( post.content() );
710  q.addBindValue( post.additionalContent() );
711  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
712  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
713  q.addBindValue( post.isPrivate() );
714  q.addBindValue( post.isCommentAllowed() );
715  q.addBindValue( post.isTrackBackAllowed() );
716  q.addBindValue( post.link().url() );
717  q.addBindValue( post.permaLink().url() );
718  q.addBindValue( post.summary() );
719  q.addBindValue( post.slug() );
720  q.addBindValue( post.tags().join(QLatin1String(",")) );
721  q.addBindValue( post.status() );
722  q.addBindValue( post.localId() );
723 
724  if ( !q.exec() ) {
725  d->mLastErrorText = q.lastError().text();
726  kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
727  return -1;
728  }
729  }
730  /*} else {///Post is already created at "Post" table and has a valid id, postId and blog_id. So, local_id is useless here
731  kDebug()<<"Post is already created at \"Post\" table and has a valid id, postId and blog_id. So, local_id is useless here";
732  q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (id, postid, blog_id, author, title,\
733  content, text_more, c_time, m_time, is_private,\
734  is_comment_allowed, is_trackback_allowed, link, perma_link, summary, slug, tags, status)\
735  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
736  q.addBindValue( post.id() );
737  q.addBindValue( post.postId() );
738  q.addBindValue( blog_id );
739  q.addBindValue( post.author() );
740  q.addBindValue( post.title() );
741  q.addBindValue( post.content() );
742  q.addBindValue( post.additionalContent() );
743  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
744  q.addBindValue( post.creationDateTime().toString( KDateTime::ISODate ) );
745  q.addBindValue( post.isPrivate() );
746  q.addBindValue( post.isCommentAllowed() );
747  q.addBindValue( post.isTrackBackAllowed() );
748  q.addBindValue( post.link().url() );
749  q.addBindValue( post.permaLink().url() );
750  q.addBindValue( post.summary() );
751  q.addBindValue( post.slug() );
752  q.addBindValue( post.tags().join(QString(',')) );
753  q.addBindValue( post.status() );
754 
755  if ( q.exec() ) {
756  postId = post.id();
757  localId = q.lastInsertId().toInt();
758  } else {
759  d->mLastErrorText = q.lastError().text();
760  kDebug() << "Cannot Add or Edit local post to database!\n\tSQL Error: " << q.lastError().text();
761  return -1;
762  }
763  }*/
764 
766  QSqlQuery qd;
767  qd.prepare( QLatin1String("DELETE FROM ") + postCatTable + QLatin1String(" WHERE local_id=?") );
768  qd.addBindValue( localId );
769  if ( !qd.exec() ) {
770  d->mLastErrorText = q.lastError().text();
771  kDebug() << "Cannot delete previouse categories.";
772  }
773 
776  int cat_count = post.categories().count();
777  if( cat_count > 0 ) {
778  //kDebug()<< "Adding "<<cat_count<<" category to post.";
779  QSqlQuery q2;
780  q2.prepare( QLatin1String("INSERT OR REPLACE INTO ") + postCatTable + QLatin1String(" (local_id, categoryId)\
781  VALUES(?, (SELECT categoryId FROM category WHERE name = ? AND blog_id= ?))" ) );
782  for ( int i = 0; i < cat_count; ++i ) {
783  q2.addBindValue(localId);
784  q2.addBindValue(post.categories()[i]);
785  q2.addBindValue(blog_id);
786  if ( !q2.exec() ) {
787  d->mLastErrorText = q.lastError().text();
788  kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
789  }
790 // kDebug()<<"category "<<post.categories()[i] <<" added.";
791  }
792  }
793  return localId;
794 }
795 
796 bool DBMan::removeLocalEntry( const BilboPost &post )
797 {
798  kDebug();
799  QSqlQuery q;
800  q.prepare( QLatin1String("DELETE FROM local_post WHERE local_id=?") );
801  q.addBindValue( post.localId() );
802  bool res = q.exec();
803  if ( !res ) {
804  d->mLastErrorText = q.lastError().text();
805  kDebug() << d->mLastErrorText;
806  }
807  return res;
808 }
809 
810 bool DBMan::removeLocalEntry( int local_id )
811 {
812  kDebug();
813  QSqlQuery q;
814  q.prepare( QLatin1String("DELETE FROM local_post WHERE local_id=?") );
815  q.addBindValue( local_id );
816  bool res = q.exec();
817  if ( !res ) {
818  d->mLastErrorText = q.lastError().text();
819  kDebug() << d->mLastErrorText;
820  }
821  return res;
822 }
823 
824 bool DBMan::removeTempEntry( const BilboPost &post )
825 {
826  kDebug();
827  QSqlQuery q;
828  q.prepare( QLatin1String("DELETE FROM temp_post WHERE local_id=?") );
829  q.addBindValue( post.localId() );
830  bool res = q.exec();
831  if ( !res ) {
832  d->mLastErrorText = q.lastError().text();
833  kDebug() << q.lastError().text();
834  }
835  kDebug()<<"Id: "<<post.id()<<"\tStatus: "<<post.status();
836  return res;
837 }
838 
839 bool DBMan::clearTempEntries()
840 {
841  kDebug();
842  QSqlQuery q;
843  bool res = q.exec( QLatin1String("DELETE FROM temp_post") );
844  if ( !res ) {
845  d->mLastErrorText = q.lastError().text();
846  kDebug() << q.lastError().text();
847  }
848  return res;
849 }
850 
851 BilboBlog *DBMan::blog(int blog_id)
852 {
853  return blogList().value(blog_id);
854 }
855 
856 QList< BilboBlog *> DBMan::listBlogs()
857 {
858  QList<BilboBlog *> list;
859  QSqlQuery q;
860  if (q.exec( QLatin1String("SELECT id, blogid, blog_url, username, style_url, api_type, title,\
861  direction, local_directory, password FROM blog") ) ) {
862  while ( q.next() ) {
863  BilboBlog *tmp = new BilboBlog;
864  tmp->setId( q.value( 0 ).toInt() );
865  tmp->setBlogId( q.value( 1 ).toString() );
866  tmp->setUrl( QUrl( q.value( 2 ).toString() ) );
867  tmp->setUsername( q.value( 3 ).toString() );
868  tmp->setBlogUrl( q.value( 4 ).toString() );
869  tmp->setApi(( BilboBlog::ApiType )q.value( 5 ).toInt() );
870  tmp->setTitle( q.value( 6 ).toString() );
871  tmp->setDirection(( Qt::LayoutDirection )q.value( 7 ).toInt() );
872  tmp->setLocalDirectory( q.value( 8 ).toString() );
873  if( d->useWallet ) {
874  QString buffer;
875  if ( d->mWallet && d->mWallet->readPassword( tmp->url().url() + QLatin1Char('_') + tmp->username() , buffer )
876  == 0 && !buffer.isEmpty() ) {
877  tmp->setPassword( buffer );
878  kDebug() << "Password loaded from kde wallet.";
879  }
880  } else {
881  tmp->setPassword( q.value( 9 ).toString() );
882  }
883  list.append( tmp );
884  }
885  } else {
886  d->mLastErrorText = q.lastError().text();
887  }
888  return list;
889 }
890 
891 QMap< QString, int > DBMan::listBlogsTitle()
892 {
893  QMap< QString, int > list;
894  QSqlQuery q;
895  if( q.exec( QLatin1String("SELECT title, id FROM blog") ) ) {
896  while ( q.next() ) {
897  list[q.value( 0 ).toString()] = q.value( 1 ).toInt();
898  }
899  } else {
900  d->mLastErrorText = q.lastError().text();
901  }
902  return list;
903 }
904 
905 QList< BilboPost* > DBMan::listPosts( int blog_id )
906 {
907  QList<BilboPost *> list;
908  QSqlQuery q;
909  q.prepare( QLatin1String("SELECT id, postid, author, title, content, c_time, m_time, is_private, is_comment_allowed,\
910  is_trackback_allowed, link, perma_link, summary, tags, status, text_more, slug\
911  FROM post WHERE blog_id = ? ORDER BY c_time DESC") );
912  q.addBindValue( blog_id );
913  if ( q.exec() ) {
914  while ( q.next() ) {
915  BilboPost *tmp = new BilboPost();
916  tmp->setId( q.value( 0 ).toInt() );
917  tmp->setAuthor( q.value( 2 ).toString() );
918  tmp->setPostId( q.value( 1 ).toString() );
919  tmp->setTitle( q.value( 3 ).toString() );
920  tmp->setContent( q.value( 4 ).toString() );
921  tmp->setCreationDateTime( KDateTime::fromString( q.value( 5 ).toString(), KDateTime::ISODate ) );
922  tmp->setModificationDateTime( KDateTime::fromString( q.value( 6 ).toString(), KDateTime::ISODate ) );
923  tmp->setPrivate( q.value( 7 ).toBool() );
924  tmp->setCommentAllowed( q.value( 8 ).toBool() );
925  tmp->setTrackBackAllowed( q.value( 9 ).toBool() );
926  tmp->setLink( KUrl( q.value( 10 ).toString() ) );
927  tmp->setPermaLink( KUrl( q.value( 11 ).toString() ) );
928  tmp->setSummary( q.value( 12 ).toString() );
929  tmp->setTags( q.value( 13 ).toString().split( QLatin1Char(','), QString::SkipEmptyParts ) );
930  tmp->setStatus(( KBlog::BlogPost::Status ) q.value( 14 ).toInt() );
931  tmp->setAdditionalContent( q.value( 15 ).toString() );
932  tmp->setSlug( q.value( 16 ).toString() );
933 
935  QList<Category> catList;
936  QSqlQuery q2;
937  q2.prepare( QLatin1String("SELECT category.name, category.description, category.htmlUrl, category.rssUrl,\
938  category.categoryId, category.parentId\
939  FROM category JOIN post_cat ON category.categoryId=post_cat.categoryId\
940  WHERE post_cat.postId = ? AND post_cat.blogId = (SELECT blogid FROM blog where id=?)" ) );
941  q2.addBindValue( tmp->postId() );
942  q2.addBindValue( blog_id );
943  if ( q2.exec() ) {
944  while ( q2.next() ) {
945  Category cat;
946  cat.blog_id = blog_id;
947  cat.name = q2.value( 0 ).toString();
948  cat.description = q2.value( 1 ).toString();
949  cat.htmlUrl = q2.value( 2 ).toString();
950  cat.rssUrl = q2.value( 3 ).toString();
951  cat.categoryId = q2.value( 4 ).toString();
952  cat.parentId = q2.value( 5 ).toString();
953  catList.append( cat );
954  }
955  } else {
956  d->mLastErrorText = q2.lastError().text();
957  }
958  tmp->setCategoryList( catList );
959  list.append( tmp );
960  }
961  } else {
962  d->mLastErrorText = q.lastError().text();
963  kDebug() << "Cannot get list of posts for blog with id " << blog_id << "error: "<< d->mLastErrorText;
964  }
965  return list;
966 }
967 
968 BilboPost DBMan::getPostInfo( int post_id )
969 {
970  QSqlQuery q;
971  BilboPost tmp;
972  q.prepare( QLatin1String("SELECT id, postid, author, title, content, c_time, m_time, is_private, is_comment_allowed,\
973  is_trackback_allowed, link, perma_link, summary, tags, status, blog_id, text_more, slug\
974  FROM post WHERE id = ?") );
975  q.addBindValue( post_id );
976  if ( q.exec() ) {
977  if ( q.next() ) {
978  tmp.setId( q.value( 0 ).toInt() );
979  tmp.setAuthor( q.value( 2 ).toString() );
980  tmp.setPostId( q.value( 1 ).toString() );
981  tmp.setTitle( q.value( 3 ).toString() );
982  tmp.setContent( q.value( 4 ).toString() );
983  tmp.setCreationDateTime( KDateTime::fromString( q.value( 5 ).toString(), KDateTime::ISODate ) );
984  tmp.setModificationDateTime( KDateTime::fromString( q.value( 6 ).toString(), KDateTime::ISODate ) );
985  tmp.setPrivate( q.value( 7 ).toBool() );
986  tmp.setCommentAllowed( q.value( 8 ).toBool() );
987  tmp.setTrackBackAllowed( q.value( 9 ).toBool() );
988  QUrl u( q.value( 10 ).toString() );
989  tmp.setLink( u );
990  QUrl pu( q.value( 11 ).toString() );
991  tmp.setPermaLink( pu );
992  tmp.setSummary( q.value( 12 ).toString() );
993  tmp.setTags( q.value( 13 ).toString().split( QLatin1Char(','), QString::SkipEmptyParts ) );
994  tmp.setStatus(( KBlog::BlogPost::Status ) q.value( 14 ).toInt() );
995  int blog_id = q.value( 15 ).toInt();
996  tmp.setAdditionalContent( q.value( 16 ).toString() );
997  tmp.setSlug( q.value( 17 ).toString() );
998 
1000  QList<Category> catList;
1001  QSqlQuery q2;
1002  q2.prepare( QLatin1String("SELECT category.name, category.description, category.htmlUrl, category.rssUrl,\
1003  category.categoryId, category.parentId, category.blog_id\
1004  FROM category JOIN post_cat ON category.categoryId=post_cat.categoryId\
1005  WHERE post_cat.postId = ? AND post_cat.blogId = (SELECT blogid FROM blog where id=?)") );
1006  q2.addBindValue( tmp.postId() );
1007  q2.addBindValue( blog_id );
1008  if ( q2.exec() ) {
1009  while ( q2.next() ) {
1010  Category cat;
1011  cat.blog_id = q2.value( 6 ).toInt();
1012  cat.name = q2.value( 0 ).toString();
1013  cat.description = q2.value( 1 ).toString();
1014  cat.htmlUrl = q2.value( 2 ).toString();
1015  cat.rssUrl = q2.value( 3 ).toString();
1016  cat.categoryId = q2.value( 4 ).toString();
1017  cat.parentId = q2.value( 5 ).toString();
1018  catList.append( cat );
1019  }
1020  } else {
1021  d->mLastErrorText = q2.lastError().text();
1022  }
1023  tmp.setCategoryList( catList );
1024  } else {
1025  d->mLastErrorText = i18n( "There is no post with the requested ID" );
1026  kDebug() << "There isn't any post with id: " << post_id;
1027  tmp.setStatus(KBlog::BlogPost::Error);
1028  }
1029  } else {
1030  d->mLastErrorText = q.lastError().text();
1031  kDebug() << "Cannot get post with id " << post_id;
1032  tmp.setStatus(KBlog::BlogPost::Error);
1033  }
1034 
1035  return tmp;
1036 }
1037 
1038 QMap< int, QString > DBMan::listPostsTitle( int blog_id )
1039 {
1040  QMap< int, QString >list;
1041  QSqlQuery q;
1042  q.prepare( QLatin1String("SELECT title, id FROM post WHERE blog_id = ? ORDER BY c_time DESC") );
1043  q.addBindValue( blog_id );
1044  if ( q.exec() ) {
1045  while ( q.next() ) {
1046  list.insert( q.value( 1 ).toInt(), q.value( 0 ).toString() );
1047  }
1048  } else {
1049  d->mLastErrorText = q.lastError().text();
1050  kDebug() << "Cannot get list of posts for blog with id " << blog_id;
1051  }
1052  return list;
1053 }
1054 
1055 QList<QVariantMap> DBMan::listPostsInfo( int blog_id )
1056 {
1057  QList<QVariantMap> list;
1058  QSqlQuery q;
1059  q.prepare( QLatin1String("SELECT title, id, c_time, is_private FROM post WHERE blog_id = ? ORDER BY c_time DESC") );
1060  q.addBindValue( blog_id );
1061  if ( q.exec() ) {
1062  while ( q.next() ) {
1063  QVariantMap entry;
1064  entry[ QLatin1String("title") ] = q.value( 0 ).toString();
1065  entry[ QLatin1String("id") ] = q.value( 1 ).toInt();
1066  entry[ QLatin1String("c_time") ] = KDateTime::fromString( q.value( 2 ).toString() ).dateTime();
1067  entry[ QLatin1String("is_private") ] = q.value( 3 ).toBool();
1068  list.append(entry);
1069  }
1070  } else {
1071  d->mLastErrorText = q.lastError().text();
1072  kDebug() << "Cannot get list of posts for blog with id " << blog_id;
1073  }
1074  return list;
1075 }
1076 
1077 QMap< QString, int > DBMan::listCategoriesName( int blog_id )
1078 {
1079  QMap< QString, int > list;
1080  QSqlQuery q;
1081  q.prepare( QLatin1String("SELECT name, catid FROM category WHERE blog_id = ?") );
1082  q.addBindValue( blog_id );
1083  if ( q.exec() ) {
1084  while ( q.next() ) {
1085  list[q.value( 0 ).toString()] = q.value( 1 ).toInt();
1086  }
1087  } else {
1088  d->mLastErrorText = q.lastError().text();
1089  kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1090  }
1091  return list;
1092 }
1093 
1094 QList< Category > DBMan::listCategories( int blog_id )
1095 {
1096  QList< Category > list;
1097  QSqlQuery q;
1098  q.prepare( QLatin1String("SELECT catid, name, description, htmlUrl, rssUrl, categoryId, parentId FROM category\
1099  WHERE blog_id = ?") );
1100  q.addBindValue( blog_id );
1101  if ( q.exec() ) {
1102  while ( q.next() ) {
1103  Category c;
1104  c.blog_id = blog_id;
1105  c.id = q.value( 0 ).toInt();
1106  c.name = q.value( 1 ).toString();
1107  c.description = q.value( 2 ).toString();
1108  c.htmlUrl = q.value( 3 ).toString();
1109  c.rssUrl = q.value( 4 ).toString();
1110  c.categoryId = q.value( 5 ).toString();
1111  c.parentId = q.value( 6 ).toString();
1112  list.append( c );
1113  }
1114  } else {
1115  d->mLastErrorText = q.lastError().text();
1116  kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1117  }
1118  return list;
1119 }
1120 
1121 QMap< QString, bool > DBMan::listCategoriesId( int blog_id )
1122 {
1123  QMap< QString, bool > list;
1124  QSqlQuery q;
1125  q.prepare( QLatin1String("SELECT categoryId FROM category\
1126  WHERE blog_id = ?") );
1127  q.addBindValue( blog_id );
1128  if ( q.exec() ) {
1129  while ( q.next() ) {
1130  list.insert( q.value( 0 ).toString(), false );
1131  }
1132  } else {
1133  d->mLastErrorText = q.lastError().text();
1134  kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1135  }
1136  return list;
1137 }
1138 
1139 QMap<BilboPost*, int> DBMan::listTempPosts()
1140 {
1141  QMap<BilboPost*, int> list;
1142  QSqlQuery q;
1143  q.prepare( QLatin1String("SELECT local_id, id, postid, blog_id, author, title, content, text_more, c_time,\
1144  m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link, summary, tags, status,\
1145  slug FROM temp_post ORDER BY m_time DESC") );
1146  if ( q.exec() ) {
1147  while ( q.next() ) {
1148  BilboPost *tmp = new BilboPost();
1149  tmp->setLocalId( q.value( 0 ).toInt() );
1150  tmp->setId( q.value( 1 ).toInt() );
1151  tmp->setPostId( q.value( 2 ).toString() );
1152  int blog_id = q.value( 3 ).toInt();
1153  tmp->setAuthor( q.value( 4 ).toString() );
1154  tmp->setTitle( q.value( 5 ).toString() );
1155  tmp->setContent( q.value( 6 ).toString() );
1156  tmp->setCreationDateTime( KDateTime::fromString( q.value( 8 ).toString(), KDateTime::ISODate ) );
1157  tmp->setModificationDateTime( KDateTime::fromString( q.value( 9 ).toString(), KDateTime::ISODate ) );
1158  tmp->setPrivate( q.value( 10 ).toBool() );
1159  tmp->setCommentAllowed( q.value( 11 ).toBool() );
1160  tmp->setTrackBackAllowed( q.value( 12 ).toBool() );
1161  tmp->setLink( KUrl( q.value( 13 ).toString() ) );
1162  tmp->setPermaLink( KUrl( q.value( 14 ).toString() ) );
1163  tmp->setSummary( q.value( 15 ).toString() );
1164  tmp->setTags( q.value( 16 ).toString().split( QLatin1Char(','), QString::SkipEmptyParts ) );
1165  tmp->setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
1166  tmp->setSlug( q.value( 18 ).toString() );
1167 
1169  QList<Category> catList;
1170  QSqlQuery q2;
1171  q2.prepare( QLatin1String("SELECT category.name, category.description, category.htmlUrl, category.rssUrl,\
1172  category.categoryId, category.parentId\
1173  FROM category JOIN temp_post_cat ON category.categoryId=temp_post_cat.categoryId\
1174  WHERE temp_post_cat.local_id = ?") );
1175  q2.addBindValue( tmp->localId() );
1176 // q2.addBindValue( blog_id );
1177  if ( q2.exec() ) {
1178  while ( q2.next() ) {
1179  Category cat;
1180  cat.blog_id = blog_id;
1181  cat.name = q2.value( 0 ).toString();
1182  cat.description = q2.value( 1 ).toString();
1183  cat.htmlUrl = q2.value( 2 ).toString();
1184  cat.rssUrl = q2.value( 3 ).toString();
1185  cat.categoryId = q2.value( 4 ).toString();
1186  cat.parentId = q2.value( 5 ).toString();
1187  catList.append( cat );
1188  }
1189  tmp->setCategoryList( catList );
1190  list.insert( tmp, blog_id);
1191  } else {
1192  d->mLastErrorText = q2.lastError().text();
1193  kDebug()<<"Cannot get categories list of a post. SQL Error: "<< q2.lastError().text();
1194  }
1195  }
1196  } else {
1197  d->mLastErrorText = q.lastError().text();
1198  kDebug() << "Cannot get list of temporary posts, SQL Error: "<< q.lastError().text();
1199  }
1200  return list;
1201 }
1202 
1203 QList<QVariantMap> DBMan::listLocalPosts()
1204 {
1205  kDebug();
1206  QList<QVariantMap> list;
1207  QSqlQuery q;
1208  q.prepare( QLatin1String("SELECT local_post.local_id, local_post.title, local_post.blog_id, blog.title\
1209  FROM local_post LEFT JOIN blog ON local_post.blog_id = blog.id ORDER BY m_time DESC") );
1210  if ( q.exec() ) {
1211  while ( q.next() ) {
1212  QVariantMap entry;
1213  entry[ QLatin1String("local_id") ] = q.value( 0 ).toInt();
1214  entry[ QLatin1String("post_title") ] = q.value( 1 ).toString();
1215  entry[ QLatin1String("blog_id") ] = q.value( 2 ).toInt();
1216  entry[ QLatin1String("blog_title") ] = q.value( 3 ).toString();
1217  list.append(entry);
1218  }
1219  } else {
1220  d->mLastErrorText = q.lastError().text();
1221  kDebug() << "Cannot get list of local posts. SQL Error: "<< q.lastError().text();
1222  }
1223  return list;
1224 }
1225 
1226 BilboPost DBMan::localPost(int local_id)
1227 {
1228  QSqlQuery q;
1229  BilboPost tmp;
1230  q.prepare( QLatin1String("SELECT id, local_id, postid, blog_id, author, title, content, text_more, c_time,\
1231  m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link, summary, tags, status,\
1232  slug FROM local_post WHERE local_id=?") );
1233  q.addBindValue(local_id);
1234  if ( q.exec() ) {
1235  if ( q.next() ) {
1236  tmp.setId( q.value( 0 ).toInt() );
1237  tmp.setLocalId( q.value( 1 ).toInt() );
1238  tmp.setPostId( q.value( 2 ).toString() );
1239  int blog_id = q.value( 3 ).toInt();
1240  tmp.setAuthor( q.value( 4 ).toString() );
1241  tmp.setTitle( q.value( 5 ).toString() );
1242  tmp.setContent( q.value( 6 ).toString() );
1243  tmp.setAdditionalContent( q.value( 7 ).toString() );
1244  tmp.setCreationDateTime( KDateTime::fromString( q.value( 8 ).toString(), KDateTime::ISODate ) );
1245  tmp.setModificationDateTime( KDateTime::fromString( q.value( 9 ).toString(), KDateTime::ISODate ) );
1246  tmp.setPrivate( q.value( 10 ).toBool() );
1247  tmp.setCommentAllowed( q.value( 11 ).toBool() );
1248  tmp.setTrackBackAllowed( q.value( 12 ).toBool() );
1249  tmp.setLink( KUrl( q.value( 13 ).toString() ) );
1250  tmp.setPermaLink( KUrl( q.value( 14 ).toString() ) );
1251  tmp.setSummary( q.value( 15 ).toString() );
1252  tmp.setTags( q.value( 16 ).toString().split( QLatin1Char(','), QString::SkipEmptyParts ) );
1253  tmp.setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
1254  tmp.setSlug( q.value( 18 ).toString() );
1255 
1257  QList<Category> catList;
1258  QSqlQuery q2;
1259  q2.prepare( QLatin1String("SELECT category.name, category.description, category.htmlUrl, category.rssUrl,\
1260  category.categoryId, category.parentId\
1261  FROM category JOIN local_post_cat ON category.categoryId=local_post_cat.categoryId\
1262  WHERE local_post_cat.local_id = ?") );
1263  q2.addBindValue( local_id );
1264  if ( q2.exec() ) {
1265  while ( q2.next() ) {
1266  Category cat;
1267  cat.blog_id = blog_id;
1268  cat.name = q2.value( 0 ).toString();
1269  cat.description = q2.value( 1 ).toString();
1270  cat.htmlUrl = q2.value( 2 ).toString();
1271  cat.rssUrl = q2.value( 3 ).toString();
1272  cat.categoryId = q2.value( 4 ).toString();
1273  cat.parentId = q2.value( 5 ).toString();
1274  catList.append( cat );
1275  }
1276  tmp.setCategoryList( catList );
1277  } else {
1278  d->mLastErrorText = q2.lastError().text();
1279  kDebug()<<"Cannot get categories list of local post. SQL Error: "<< q2.lastError().text();
1280  }
1281  } else {
1282  d->mLastErrorText = i18n( "There is no local post with the requested ID " );
1283  kDebug()<<"there isn't any local post with local_id "<<local_id;
1284  }
1285  } else {
1286  d->mLastErrorText = q.lastError().text();
1287  kDebug() << "Cannot get local post. SQL Error: "<< q.lastError().text();
1288  }
1289  return tmp;
1290 }
DBMan::listPosts
QList< BilboPost * > listPosts(int blog_id)
Definition: dbman.cpp:905
DBMan::clearCategories
bool clearCategories(int blog_id)
Definition: dbman.cpp:559
BilboPost::localId
int localId() const
Definition: bilbopost.cpp:97
BilboBlog::setApi
void setApi(const ApiType)
Definition: bilboblog.cpp:176
BilboPost::setAuthor
void setAuthor(const QString &)
Definition: bilbopost.cpp:92
BilboBlog::password
QString password() const
Definition: bilboblog.cpp:151
DBMan::removeLocalEntry
bool removeLocalEntry(const BilboPost &post)
Definition: dbman.cpp:796
Category::rssUrl
QString rssUrl
Definition: category.h:38
Category::description
QString description
Definition: category.h:36
DBMan::clearTempEntries
bool clearTempEntries()
Definition: dbman.cpp:839
DBMan::listCategoriesId
QMap< QString, bool > listCategoriesId(int blog_id)
Definition: dbman.cpp:1121
DBMan::lastErrorText
QString lastErrorText() const
Definition: dbman.cpp:100
BilboBlog::localDirectory
QString localDirectory() const
Definition: bilboblog.cpp:233
BilboPost::setLocalId
void setLocalId(const int)
Definition: bilbopost.cpp:102
BilboBlog::setTitle
void setTitle(const QString &)
Definition: bilboblog.cpp:166
DBMan::saveTempEntry
int saveTempEntry(const BilboPost &post, int blog_id)
Definition: dbman.cpp:642
DBMan::addFile
int addFile()
BilboBlog::title
QString title() const
Definition: bilboblog.cpp:161
DBMan::self
static DBMan * self()
Retrieve the instance of DataBase Manager.
Definition: dbman.cpp:107
BilboMedia::name
QString name() const
Definition: bilbomedia.cpp:125
BilboPost
Definition of a blog post! it's implemented to decrease dependency to KBlog :)
Definition: bilbopost.h:41
Category::htmlUrl
QString htmlUrl
Definition: category.h:37
DBMan::listLocalPosts
QList< QVariantMap > listLocalPosts()
Returns list of locally saved posts.
Definition: dbman.cpp:1203
BilboMedia::isUploaded
bool isUploaded() const
Definition: bilbomedia.cpp:85
DBMan::blog
BilboBlog * blog(int blog_id)
QString as Title, and int as blog_id.
Definition: dbman.cpp:851
bilbopost.h
Category::blog_id
int blog_id
Definition: category.h:42
BilboBlog::blogid
QString blogid() const
Definition: bilboblog.cpp:131
DBMan::saveLocalEntry
int saveLocalEntry(const BilboPost &post, int blog_id)
Definition: dbman.cpp:637
DBMan::listPostsTitle
QMap< int, QString > listPostsTitle(int blog_id)
Definition: dbman.cpp:1038
BilboBlog::setId
void setId(const int)
Definition: bilboblog.cpp:218
BilboBlog
Blog definition class!
Definition: bilboblog.h:40
Category::id
int id
Definition: category.h:41
BilboBlog::setDirection
void setDirection(const Qt::LayoutDirection)
Definition: bilboblog.cpp:228
DBMan::addCategory
int addCategory(const QString &name, const QString &description, const QString &htmlUrl, const QString &rssUrl, const QString &categoryId, const QString &parentId, int blog_id)
Category:
Definition: dbman.cpp:537
BilboBlog::setPassword
void setPassword(const QString &)
Definition: bilboblog.cpp:156
CONF_DB
#define CONF_DB
Definition: constants.h:39
BilboBlog::setUsername
void setUsername(const QString &)
Definition: bilboblog.cpp:146
BilboBlog::username
QString username() const
Definition: bilboblog.cpp:141
DBMan::DBMan
DBMan()
Definition: dbman.cpp:58
DBMan::listCategories
QList< Category > listCategories(int blog_id)
Definition: dbman.cpp:1094
DBMan::listBlogsTitle
QMap< QString, int > listBlogsTitle()
(BEGIN) Data retrieveing Functions:
Definition: dbman.cpp:891
DBMan::localPost
BilboPost localPost(int local_id)
Definition: dbman.cpp:1226
DBMan
DataBase Manager class.
Definition: dbman.h:45
DBMan::clearPosts
bool clearPosts(int blog_id)
Definition: dbman.cpp:524
BilboPost::author
QString author() const
Definition: bilbopost.cpp:77
BilboPost::setId
void setId(const int)
Definition: bilbopost.cpp:82
bilboblog.h
BilboMedia::blogId
int blogId() const
Definition: bilbomedia.cpp:64
BilboBlog::setUrl
void setUrl(const KUrl &)
Definition: bilboblog.cpp:126
DBMan::editPost
bool editPost(const BilboPost &post, int blog_id)
Definition: dbman.cpp:431
DBMan::blogList
const QMap< int, BilboBlog * > & blogList() const
Definition: dbman.cpp:114
BilboMedia::localUrl
KUrl localUrl() const
Definition: bilbomedia.cpp:95
DBMan::removeTempEntry
bool removeTempEntry(const BilboPost &post)
Definition: dbman.cpp:824
Category::categoryId
QString categoryId
Definition: category.h:39
DBMan::listTempPosts
QMap< BilboPost *, int > listTempPosts()
Returns list of temporary posts, e.g.
Definition: dbman.cpp:1139
DBMan::addBlog
int addBlog(const BilboBlog &blog)
END.
Definition: dbman.cpp:274
BilboMedia
Definition: bilbomedia.h:38
DBMan::clearFiles
bool clearFiles(int blog_id)
Definition: dbman.cpp:624
dbman.h
DBMan::addPost
int addPost(const BilboPost &post, int blog_id)
Post:
Definition: dbman.cpp:364
BilboBlog::direction
Qt::LayoutDirection direction() const
Definition: bilboblog.cpp:223
BilboBlog::id
int id() const
Definition: bilboblog.cpp:213
DBMan::listCategoriesName
QMap< QString, int > listCategoriesName(int blog_id)
Definition: dbman.cpp:1077
BilboPost::toString
QString toString() const
Definition: bilbopost.cpp:107
DBMan::editBlog
bool editBlog(const BilboBlog &blog)
Definition: dbman.cpp:307
DBMan::listPostsInfo
QList< QVariantMap > listPostsInfo(int blog_id)
QString as Title, and int as post_id.
Definition: dbman.cpp:1055
BilboBlog::setLocalDirectory
void setLocalDirectory(const QString &)
Definition: bilboblog.cpp:238
description
static const char description[]
Definition: main.cpp:33
DBMan::getPostInfo
BilboPost getPostInfo(int post_id)
Definition: dbman.cpp:968
BilboPost::id
int id() const
Definition: bilbopost.cpp:87
Category::name
QString name
Definition: category.h:35
BilboBlog::ApiType
ApiType
Definition: bilboblog.h:44
DBMan::removeBlog
bool removeBlog(int blog_id)
Definition: dbman.cpp:342
DBMan::removePost
bool removePost(int id)
Definition: dbman.cpp:497
BilboMedia::remoteUrl
KUrl remoteUrl() const
Definition: bilbomedia.cpp:105
BilboBlog::url
KUrl url() const
returns blog xmlrpc Url! For http://bilbo.wordpress.com : it's url() is http://bilbo.wordpress.com/xmlrpc.php and it's blogUrl() is http://bilbo.wordpress.com/
Definition: bilboblog.cpp:121
BilboBlog::blogUrl
QString blogUrl() const
return Blog Actual Url! For http://bilbo.wordpress.com : it's url() is http://bilbo.wordpress.com/xmlrpc.php and it's blogUrl() is http://bilbo.wordpress.com/
Definition: bilboblog.cpp:243
BilboBlog::api
ApiType api() const
Definition: bilboblog.cpp:171
DBMan::~DBMan
~DBMan()
Definition: dbman.cpp:87
BilboBlog::setBlogId
void setBlogId(const QString &)
Definition: bilboblog.cpp:136
DBMan::removeFile
bool removeFile(int fileid)
Definition: dbman.cpp:611
Category
Blog Category.
Definition: category.h:34
BilboBlog::setBlogUrl
void setBlogUrl(const QString &blogUrl)
Definition: bilboblog.cpp:251
BilboPost::setCategoryList
void setCategoryList(const QList< Category > &list)
Definition: bilbopost.cpp:141
Category::parentId
QString parentId
Definition: category.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:44 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

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