~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to blogilo/src/dbman.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    This file is part of Blogilo, A KDE Blogging Client
3
3
 
4
 
    Copyright (C) 2008-2009 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5
 
    Copyright (C) 2008-2009 Golnaz Nilieh <g382nilieh@gmail.com>
 
4
    Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
 
5
    Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
6
6
 
7
7
    This program is free software; you can redistribute it and/or
8
8
    modify it under the terms of the GNU General Public License as
36
36
#include <QSqlError>
37
37
#include <QSqlQuery>
38
38
#include <QFile>
 
39
#include <QSqlDatabase>
 
40
 
 
41
class DBMan::Private
 
42
{
 
43
public:
 
44
    KWallet::Wallet* mWallet;
 
45
    QString mLastErrorText;
 
46
    bool useWallet;
 
47
    QMap<int, BilboBlog*> mBlogList;
 
48
    QSqlDatabase db;
 
49
};
39
50
 
40
51
DBMan::DBMan()
 
52
: d(new Private)
41
53
{
42
54
    kDebug();
43
 
    mWallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
44
 
    if ( mWallet ) {
45
 
        useWallet = true;
46
 
        if ( !mWallet->setFolder( "blogilo" ) ) {
47
 
            mWallet->createFolder( "blogilo" );
48
 
            mWallet->setFolder( "blogilo" );
 
55
    d->mWallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
 
56
    if ( d->mWallet ) {
 
57
        d->useWallet = true;
 
58
        if ( !d->mWallet->setFolder( "blogilo" ) ) {
 
59
            d->mWallet->createFolder( "blogilo" );
 
60
            d->mWallet->setFolder( "blogilo" );
49
61
        }
50
62
        kDebug() << "Wallet successfully opened.";
51
63
    } else {
52
 
        useWallet = false;
 
64
        d->useWallet = false;
53
65
        kDebug() << "Could not use Wallet service, will use database to store passwords";
54
66
    }
55
67
 
56
68
    if ( !QFile::exists( CONF_DB ) ) {
57
69
        if ( !this->createDB() ) {
58
70
            KMessageBox::detailedError( 0, i18n( "Cannot create database" ),
59
 
                                        i18n( db.lastError().text().toUtf8().data() ) );
60
 
            kDebug() << "Cannot create database, SQL error: " << db.lastError().text() << endl;
 
71
                                        i18n( d->db.lastError().text().toUtf8().data() ) );
 
72
            kDebug() << "Cannot create database, SQL error: " << d->db.lastError().text() << endl;
61
73
            exit ( 1 );
62
74
        }
63
75
    } else if ( !connectDB() )
68
80
 
69
81
QString DBMan::lastErrorText()
70
82
{
71
 
    return mLastErrorText;
 
83
    return d->mLastErrorText;
72
84
}
73
85
 
74
86
DBMan * DBMan::mSelf = 0L;
82
94
 
83
95
const QMap<int, BilboBlog*> & DBMan::blogList() const
84
96
{
85
 
    return mBlogList;
 
97
    return d->mBlogList;
86
98
}
87
99
 
88
100
void DBMan::reloadBlogList()
89
101
{
90
 
    mBlogList.clear();
 
102
    d->mBlogList.clear();
91
103
    QList<BilboBlog*> listBlogs = this->listBlogs();
92
104
    int count = listBlogs.count();
93
105
    for ( int i = 0; i < count; ++i ) {
94
 
        mBlogList [ listBlogs[i]->id() ] = listBlogs[i];
 
106
        d->mBlogList [ listBlogs[i]->id() ] = listBlogs[i];
95
107
    }
96
108
}
97
109
 
98
110
bool DBMan::connectDB()
99
111
{
100
112
    kDebug();
101
 
    if( db.isOpen() )
 
113
    if( d->db.isOpen() )
102
114
        return true;
103
 
    db = QSqlDatabase::addDatabase( "QSQLITE" );
104
 
    db.setDatabaseName( CONF_DB );
 
115
    d->db = QSqlDatabase::addDatabase( "QSQLITE" );
 
116
    d->db.setDatabaseName( CONF_DB );
105
117
 
106
 
    if ( !db.open() ) {
 
118
    if ( !d->db.open() ) {
107
119
        KMessageBox::detailedError( 0, i18n( "Cannot connect to database" ),
108
 
                                    i18n( db.lastError().text().toUtf8().data() ) );
109
 
        kDebug() << "Cannot connect to database, SQL error: " << db.lastError().text();
 
120
                                    i18n( d->db.lastError().text().toUtf8().data() ) );
 
121
        kDebug() << "Cannot connect to database, SQL error: " << d->db.lastError().text();
110
122
        return false;
111
123
    }
112
124
    return true;
115
127
DBMan::~DBMan()
116
128
{
117
129
    kDebug();
118
 
    db.close();
 
130
    d->db.close();
 
131
    delete d;
119
132
    mSelf = 0L;
120
133
}
121
134
 
138
151
                  password TEXT, style_url TEXT, api_type TEXT, title TEXT, direction TEXT,\
139
152
                  local_directory TEXT, icon_url TEXT)" ) ) {
140
153
        ret = false;
141
 
        mLastErrorText = q.lastError().text();
 
154
        d->mLastErrorText = q.lastError().text();
142
155
    }
143
156
 
144
157
    ///posts table!
148
161
                  is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
149
162
                  status NUMERIC, trackback_urls TEXT, UNIQUE(postid, blog_id));" ) ) {
150
163
        ret = false;
151
 
        mLastErrorText = q.lastError().text();
 
164
        d->mLastErrorText = q.lastError().text();
152
165
    }
153
166
 
154
167
    ///comments table!
157
170
        c_time TEXT, m_time TEXT, link TEXT, password TEXT,\
158
171
        status NUMERIC, UNIQUE(commentid, blog_id));" ) ) {
159
172
        ret = false;
160
 
        mLastErrorText = q.lastError().text();
 
173
        d->mLastErrorText = q.lastError().text();
161
174
    }
162
175
 
163
176
    ///categories table!
165
178
                  description TEXT, htmlUrl TEXT, rssUrl TEXT, categoryId TEXT, parentId TEXT,\
166
179
                  blog_id NUMERIC NOT NULL, UNIQUE(name,blog_id));" ) ) {
167
180
        ret = false;
168
 
        mLastErrorText = q.lastError().text();
 
181
        d->mLastErrorText = q.lastError().text();
169
182
    }
170
183
 
171
184
    ///files table
172
185
    if( !q.exec( "CREATE TABLE file (fileid INTEGER PRIMARY KEY, name TEXT, blog_id NUMERIC, is_uploaded NUMERIC,\
173
186
        local_url TEXT, remote_url TEXT, mime_type TEXT);" ) ) {
174
187
        ret = false;
175
 
        mLastErrorText = q.lastError().text();
 
188
        d->mLastErrorText = q.lastError().text();
176
189
    }
177
190
 
178
191
    ///connection bethween posts and categories
179
192
    if ( !q.exec( "CREATE TABLE post_cat (blogId TEXT NOT NULL, postId TEXT NOT NULL,\
180
193
        categoryId TEXT NOT NULL, UNIQUE(blogId,postId,categoryId));" ) ) {
181
194
        ret = false;
182
 
        mLastErrorText = q.lastError().text();
 
195
        d->mLastErrorText = q.lastError().text();
183
196
    }
184
197
 
185
198
    ///connection bethween posts and media files
186
199
    if ( !q.exec( "CREATE TABLE post_file (post_id INTEGER, file_id INTEGER);" ) ) {
187
200
        ret = false;
188
 
        mLastErrorText = q.lastError().text();
 
201
        d->mLastErrorText = q.lastError().text();
189
202
    }
190
203
 
191
204
    ///local posts table
195
208
        is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
196
209
        status NUMERIC);" ) ) {
197
210
        ret = false;
198
 
        mLastErrorText = q.lastError().text();
 
211
        d->mLastErrorText = q.lastError().text();
199
212
    }
200
213
 
201
214
    ///Connection between local_posts and categories
202
215
    if( !q.exec( "CREATE TABLE local_post_cat (local_id INT, categoryId TEXT);" ) ) {
203
216
        ret = false;
204
 
        mLastErrorText = q.lastError().text();
 
217
        d->mLastErrorText = q.lastError().text();
205
218
    }
206
219
 
207
220
    ///temporary posts table
211
224
                 is_trackback_allowed NUMERIC, link TEXT, perma_link TEXT, summary TEXT, tags TEXT,\
212
225
                 status NUMERIC);" ) ) {
213
226
        ret = false;
214
 
        mLastErrorText = q.lastError().text();
 
227
        d->mLastErrorText = q.lastError().text();
215
228
    }
216
229
 
217
230
    ///Connection between temp_posts and categories
218
231
    if( !q.exec( "CREATE TABLE temp_post_cat (local_id INT, categoryId TEXT);" ) ) {
219
232
        ret = false;
220
 
        mLastErrorText = q.lastError().text();
 
233
        d->mLastErrorText = q.lastError().text();
221
234
    }
222
235
 
223
236
    ///delete related information on DB, On removing a post or a blog
249
262
int DBMan::addBlog( const BilboBlog & blog )
250
263
{
251
264
    QSqlQuery q;
252
 
    if( useWallet ) {
 
265
    if( d->useWallet ) {
253
266
        q.prepare( "INSERT INTO blog (blogid, blog_url, username, style_url, api_type, title,\
254
267
               direction, local_directory) VALUES(?, ?, ?, ?, ?, ?, ?, ?)" );
255
 
        if ( mWallet && mWallet->writePassword( blog.url().url() + '_' + blog.username(), blog.password() ) == 0 )
 
268
        if ( d->mWallet && d->mWallet->writePassword( blog.url().url() + '_' + blog.username(), blog.password() ) == 0 )
256
269
            kDebug() << "Password stored to kde wallet";
257
270
        else
258
271
            return -1;
274
287
        reloadBlogList();
275
288
        return q.lastInsertId().toInt();
276
289
    } else {
277
 
        mLastErrorText = q.lastError().text();
 
290
        d->mLastErrorText = q.lastError().text();
278
291
        return -1;
279
292
    }
280
293
}
282
295
bool DBMan::editBlog( const BilboBlog & blog )
283
296
{
284
297
    QSqlQuery q;
285
 
    if( useWallet ) {
 
298
    if( d->useWallet ) {
286
299
        q.prepare( "UPDATE blog SET blogid=?, blog_url=?, username=? , style_url=? , api_type=?, \
287
300
                   title=?, direction=?, local_directory=? WHERE id=?" );
288
 
        if ( mWallet && mWallet->writePassword( blog.url().url() + '_' + blog.username(), blog.password() ) == 0 )
 
301
        if ( d->mWallet && d->mWallet->writePassword( blog.url().url() + '_' + blog.username(), blog.password() ) == 0 )
289
302
            kDebug() << "Password stored to kde wallet";
290
303
        else
291
304
            return false;
306
319
 
307
320
    bool res = q.exec();
308
321
    if ( !res ) {
309
 
        mLastErrorText = q.lastError().text();
 
322
        d->mLastErrorText = q.lastError().text();
310
323
        kDebug() << q.lastError().text();
311
324
        return res;
312
325
    }
316
329
 
317
330
bool DBMan::removeBlog( int blog_id )
318
331
{
319
 
    BilboBlog *tmp = mBlogList[ blog_id ];
320
 
    if( useWallet ) {
321
 
        if ( mWallet && mWallet->removeEntry( tmp->url().url() + '_' + tmp->username() ) == 0 )
 
332
    BilboBlog *tmp = d->mBlogList[ blog_id ];
 
333
    if( d->useWallet ) {
 
334
        if ( d->mWallet && d->mWallet->removeEntry( tmp->url().url() + '_' + tmp->username() ) == 0 )
322
335
            kDebug() << "Password removed to kde wallet";
323
336
    }
324
337
    QSqlQuery q;
326
339
    q.addBindValue( blog_id );
327
340
    bool res = q.exec();
328
341
    if ( !res ) {
329
 
        mLastErrorText = q.lastError().text();
 
342
        d->mLastErrorText = q.lastError().text();
330
343
        kDebug() << q.lastError().text();
331
344
        return res;
332
345
    }
372
385
        qd.addBindValue(post.postId());
373
386
        qd.addBindValue(blog_id);
374
387
        if ( !qd.exec() ) {
375
 
            mLastErrorText = qd.lastError().text();
 
388
            d->mLastErrorText = qd.lastError().text();
376
389
            kError() << "Cannot delete previous categories.";
377
390
        }
378
391
 
390
403
                q2.addBindValue(blog_id);
391
404
                if ( !q2.exec() ) {
392
405
                    kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
393
 
                    mLastErrorText = q.lastError().text();
 
406
                    d->mLastErrorText = q.lastError().text();
394
407
                }
395
408
            }
396
409
        }
397
410
    } else {
398
 
        mLastErrorText = q.lastError().text();
 
411
        d->mLastErrorText = q.lastError().text();
399
412
        kDebug() << "Cannot Add post to database!\n\tSQL Error: " << q.lastError().text();
400
413
        ret = -1;
401
414
    }
430
443
    q.addBindValue( blog_id );
431
444
 
432
445
    if ( !q.exec() ) {
433
 
        mLastErrorText = q.lastError().text();
434
 
        kDebug()<<"Modifying post failed, SQL ERROR: "<< mLastErrorText;
 
446
        d->mLastErrorText = q.lastError().text();
 
447
        kDebug()<<"Modifying post failed, SQL ERROR: "<< d->mLastErrorText;
435
448
        return false;
436
449
    }
437
450
 
441
454
    qd.addBindValue(post.postId());
442
455
    qd.addBindValue(blog_id);
443
456
    if ( !qd.exec() ) {
444
 
        mLastErrorText = qd.lastError().text();
 
457
        d->mLastErrorText = qd.lastError().text();
445
458
        kDebug() << "Cannot delete previous categories.";
446
459
    }
447
460
 
460
473
            q2.addBindValue(post.categories()[i]);
461
474
            q2.addBindValue(blog_id);
462
475
            if ( !q2.exec() ) {
463
 
                mLastErrorText = q2.lastError().text();
 
476
                d->mLastErrorText = q2.lastError().text();
464
477
                kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
465
478
            }
466
479
        }
476
489
    q.addBindValue( id );
477
490
    bool res = q.exec();
478
491
    if ( !res ) {
479
 
        mLastErrorText = q.lastError().text();
480
 
        kDebug() << mLastErrorText;
 
492
        d->mLastErrorText = q.lastError().text();
 
493
        kDebug() << d->mLastErrorText;
481
494
    }
482
495
    return res;
483
496
}
490
503
    q.addBindValue( postId );
491
504
    bool res = q.exec();
492
505
    if ( !res ) {
493
 
        mLastErrorText = q.lastError().text();
494
 
        kDebug() << mLastErrorText;
 
506
        d->mLastErrorText = q.lastError().text();
 
507
        kDebug() << d->mLastErrorText;
495
508
    }
496
509
    return res;
497
510
}
503
516
    q.addBindValue( blog_id );
504
517
    bool res = q.exec();
505
518
    if ( !res ) {
506
 
        mLastErrorText = q.lastError().text();
 
519
        d->mLastErrorText = q.lastError().text();
507
520
        kDebug() << q.lastError().text();
508
521
    }
509
522
    return res;
526
539
    if ( q.exec() )
527
540
        return q.lastInsertId().toInt();
528
541
    else {
529
 
        mLastErrorText = q.lastError().text();
 
542
        d->mLastErrorText = q.lastError().text();
530
543
        return -1;
531
544
    }
532
545
}
538
551
    q.addBindValue( blog_id );
539
552
    bool res = q.exec();
540
553
    if ( !res ) {
541
 
        mLastErrorText = q.lastError().text();
 
554
        d->mLastErrorText = q.lastError().text();
542
555
        kDebug() << q.lastError().text();
543
556
    }
544
557
    return res;
560
573
    if ( q.exec() )
561
574
        return q.lastInsertId().toInt();
562
575
    else {
563
 
        mLastErrorText = q.lastError().text();
 
576
        d->mLastErrorText = q.lastError().text();
564
577
        return -1;
565
578
    }
566
579
}
578
591
    if ( q.exec() )
579
592
        return q.lastInsertId().toInt();
580
593
    else {
581
 
        mLastErrorText = q.lastError().text();
 
594
        d->mLastErrorText = q.lastError().text();
582
595
        return -1;
583
596
    }
584
597
}
590
603
    q.addBindValue( fileid );
591
604
    bool res = q.exec();
592
605
    if ( !res ) {
593
 
        mLastErrorText = q.lastError().text();
 
606
        d->mLastErrorText = q.lastError().text();
594
607
        kDebug() << q.lastError().text();
595
608
    }
596
609
    return res;
603
616
    q.addBindValue( blog_id );
604
617
    bool res = q.exec();
605
618
    if ( !res ) {
606
 
        mLastErrorText = q.lastError().text();
 
619
        d->mLastErrorText = q.lastError().text();
607
620
        kDebug() << q.lastError().text();
608
621
    }
609
622
    return res;
633
646
        postTable = "temp_post";
634
647
        postCatTable = "temp_post_cat";
635
648
    }
636
 
    int postId = -1, localId=-1;
637
 
    if(post.status() == KBlog::BlogPost::New) {///Post is new!
638
 
        if(post.id() == -1){
 
649
    int localId = post.localId();
 
650
//    if(post.status() == KBlog::BlogPost::New) {///Post is new!
 
651
//         kDebug()<<"Post is new!";
 
652
        if(post.localId() == -1){
639
653
            ///Add new post to temp_post
640
 
            q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (postid, blog_id, author, title, content,\
641
 
            text_more, c_time, m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link,\
642
 
            summary, slug, tags, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
 
654
            kDebug()<<"Add new post to temp_post";
 
655
            q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (postid, blog_id,\
 
656
            author, title, content, text_more, c_time, m_time, is_private, is_comment_allowed,\
 
657
            is_trackback_allowed, link, perma_link, summary, slug, tags, status)\
 
658
            VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
 
659
//             q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
643
660
            q.addBindValue( post.postId() );
644
661
            q.addBindValue( blog_id );
645
662
            q.addBindValue( post.author() );
659
676
            q.addBindValue( post.status() );
660
677
 
661
678
            if ( q.exec() ) {
662
 
                localId = postId = q.lastInsertId().toInt();
 
679
                localId = q.lastInsertId().toInt();
663
680
            } else {
664
 
                mLastErrorText = q.lastError().text();
 
681
                d->mLastErrorText = q.lastError().text();
665
682
                kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
666
683
                return -1;
667
684
            }
668
685
        } else {
669
686
            ///Update post, with id!
670
 
            q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (local_id, postid, blog_id, author, title,\
671
 
            content, text_more, c_time, m_time, is_private, is_comment_allowed, is_trackback_allowed, link,\
672
 
            perma_link, summary, slug, tags, status)\
673
 
            VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
674
 
            q.addBindValue( post.id() );
 
687
            kDebug()<<"Update post, with id!";
 
688
            q.prepare( "UPDATE "+ postTable +" SET postid=?, blog_id=?,\
 
689
            author=?, title=?, content=?, text_more=?, c_time=?, m_time=?, is_private=?, is_comment_allowed=?,\
 
690
            is_trackback_allowed=?, link=?, perma_link=?, summary=?, slug=?, tags=?, status=?\
 
691
            WHERE local_id=?" );
 
692
//             q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
675
693
            q.addBindValue( post.postId() );
676
694
            q.addBindValue( blog_id );
677
695
            q.addBindValue( post.author() );
689
707
            q.addBindValue( post.slug() );
690
708
            q.addBindValue( post.tags().join(QString(',')) );
691
709
            q.addBindValue( post.status() );
 
710
            q.addBindValue( post.localId() );
692
711
 
693
 
            if ( q.exec() ) {
694
 
                localId = postId = q.lastInsertId().toInt();
695
 
            } else {
696
 
                mLastErrorText = q.lastError().text();
 
712
            if ( !q.exec() ) {
 
713
                d->mLastErrorText = q.lastError().text();
697
714
                kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
698
715
                return -1;
699
716
            }
700
717
        }
701
 
    } else {///Post is already created at "Post" table and has a valid id, postId and blog_id. So, local_id is useless here
 
718
    /*} else {///Post is already created at "Post" table and has a valid id, postId and blog_id. So, local_id is useless here
 
719
        kDebug()<<"Post is already created at \"Post\" table and has a valid id, postId and blog_id. So, local_id is useless here";
702
720
        q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (id, postid, blog_id, author, title,\
703
721
        content, text_more, c_time, m_time, is_private,\
704
722
        is_comment_allowed, is_trackback_allowed, link, perma_link, summary, slug, tags, status)\
726
744
            postId = post.id();
727
745
            localId = q.lastInsertId().toInt();
728
746
        } else {
729
 
            mLastErrorText = q.lastError().text();
 
747
            d->mLastErrorText = q.lastError().text();
730
748
            kDebug() << "Cannot Add or Edit local post to database!\n\tSQL Error: " << q.lastError().text();
731
749
            return -1;
732
750
        }
733
 
    }
 
751
    }*/
734
752
 
735
753
    ///Delete previouse Categories:
736
754
    QSqlQuery qd;
737
755
    qd.prepare( "DELETE FROM " + postCatTable + " WHERE local_id=?" );
738
756
    qd.addBindValue( localId );
739
757
    if ( !qd.exec() ) {
740
 
        mLastErrorText = q.lastError().text();
 
758
        d->mLastErrorText = q.lastError().text();
741
759
        kDebug() << "Cannot delete previouse categories.";
742
760
    }
743
761
 
754
772
            q2.addBindValue(post.categories()[i]);
755
773
            q2.addBindValue(blog_id);
756
774
            if ( !q2.exec() ) {
757
 
                mLastErrorText = q.lastError().text();
 
775
                d->mLastErrorText = q.lastError().text();
758
776
                kDebug() << "Cannot add one of categories to Post, SQL Error: " << q2.lastError().text();
759
777
            }
760
778
//             kDebug()<<"category "<<post.categories()[i] <<" added.";
761
779
        }
762
780
    }
763
 
    return postId;
 
781
    return localId;
764
782
}
765
783
 
766
784
bool DBMan::removeLocalEntry( const BilboPost &post )
767
785
{
768
786
    kDebug();
769
787
    QSqlQuery q;
770
 
    if(post.status() == KBlog::BlogPost::New) {
771
 
        q.prepare( "DELETE FROM local_post WHERE local_id=?" );
772
 
    } else {
773
 
        q.prepare( "DELETE FROM local_post WHERE id=?" );
774
 
    }
775
 
    q.addBindValue( post.id() );
 
788
    q.prepare( "DELETE FROM local_post WHERE local_id=?" );
 
789
    q.addBindValue( post.localId() );
776
790
    bool res = q.exec();
777
791
    if ( !res ) {
778
 
        mLastErrorText = q.lastError().text();
779
 
        kDebug() << mLastErrorText;
 
792
        d->mLastErrorText = q.lastError().text();
 
793
        kDebug() << d->mLastErrorText;
780
794
    }
781
795
    return res;
782
796
}
789
803
    q.addBindValue( local_id );
790
804
    bool res = q.exec();
791
805
    if ( !res ) {
792
 
        mLastErrorText = q.lastError().text();
793
 
        kDebug() << mLastErrorText;
 
806
        d->mLastErrorText = q.lastError().text();
 
807
        kDebug() << d->mLastErrorText;
794
808
    }
795
809
    return res;
796
810
}
799
813
{
800
814
    kDebug();
801
815
    QSqlQuery q;
802
 
    if(post.status() == KBlog::BlogPost::New) {
803
 
        q.prepare( "DELETE FROM temp_post WHERE local_id=?" );
804
 
    } else {
805
 
        q.prepare( "DELETE FROM temp_post WHERE id=?" );
806
 
    }
807
 
    q.addBindValue( post.id() );
 
816
    q.prepare( "DELETE FROM temp_post WHERE local_id=?" );
 
817
    q.addBindValue( post.localId() );
808
818
    bool res = q.exec();
809
819
    if ( !res ) {
810
 
        mLastErrorText = q.lastError().text();
 
820
        d->mLastErrorText = q.lastError().text();
811
821
        kDebug() << q.lastError().text();
812
822
    }
813
823
    kDebug()<<"Id: "<<post.id()<<"\tStatus: "<<post.status();
820
830
    QSqlQuery q;
821
831
    bool res = q.exec( "DELETE FROM temp_post" );
822
832
    if ( !res ) {
823
 
        mLastErrorText = q.lastError().text();
 
833
        d->mLastErrorText = q.lastError().text();
824
834
        kDebug() << q.lastError().text();
825
835
    }
826
836
    return res;
827
837
}
828
838
 
829
 
const BilboBlog &DBMan::blog(int blog_id)
 
839
BilboBlog *DBMan::blog(int blog_id)
830
840
{
831
 
    return *blogList().value(blog_id);
 
841
    return blogList().value(blog_id);
832
842
}
833
843
 
834
844
QList< BilboBlog *> DBMan::listBlogs()
848
858
            tmp->setTitle( q.value( 6 ).toString() );
849
859
            tmp->setDirection(( Qt::LayoutDirection )q.value( 7 ).toInt() );
850
860
            tmp->setLocalDirectory( q.value( 8 ).toString() );
851
 
            if( useWallet ) {
 
861
            if( d->useWallet ) {
852
862
                QString buffer;
853
 
                if ( mWallet && mWallet->readPassword( tmp->url().url() + '_' + tmp->username() , buffer )
 
863
                if ( d->mWallet && d->mWallet->readPassword( tmp->url().url() + '_' + tmp->username() , buffer )
854
864
                    == 0 && !buffer.isEmpty() ) {
855
865
                    tmp->setPassword( buffer );
856
866
                    kDebug() << "Password loaded from kde wallet.";
861
871
            list.append( tmp );
862
872
        }
863
873
    } else {
864
 
        mLastErrorText = q.lastError().text();
 
874
        d->mLastErrorText = q.lastError().text();
865
875
    }
866
876
    return list;
867
877
}
875
885
            list[q.value( 0 ).toString()] = q.value( 1 ).toInt();
876
886
        }
877
887
    } else {
878
 
        mLastErrorText = q.lastError().text();
 
888
        d->mLastErrorText = q.lastError().text();
879
889
    }
880
890
    return list;
881
891
}
931
941
                    catList.append( cat );
932
942
                }
933
943
            } else {
934
 
                mLastErrorText = q2.lastError().text();
 
944
                d->mLastErrorText = q2.lastError().text();
935
945
            }
936
946
            tmp->setCategoryList( catList );
937
947
            list.append( tmp );
938
948
        }
939
949
    } else {
940
 
        mLastErrorText = q.lastError().text();
941
 
        kDebug() << "Cannot get list of posts for blog with id " << blog_id << "error: "<< mLastErrorText;
 
950
        d->mLastErrorText = q.lastError().text();
 
951
        kDebug() << "Cannot get list of posts for blog with id " << blog_id << "error: "<< d->mLastErrorText;
942
952
    }
943
953
    return list;
944
954
}
996
1006
                    catList.append( cat );
997
1007
                }
998
1008
            } else {
999
 
                mLastErrorText = q2.lastError().text();
 
1009
                d->mLastErrorText = q2.lastError().text();
1000
1010
            }
1001
1011
            tmp.setCategoryList( catList );
1002
1012
        } else {
1003
 
            mLastErrorText = i18n( "There is no post with the requested ID" );
 
1013
            d->mLastErrorText = i18n( "There is no post with the requested ID" );
1004
1014
            kDebug() << "There isn't any post with id: " << post_id;
1005
1015
            tmp.setStatus(KBlog::BlogPost::Error);
1006
1016
        }
1007
1017
    } else {
1008
 
        mLastErrorText = q.lastError().text();
 
1018
        d->mLastErrorText = q.lastError().text();
1009
1019
        kDebug() << "Cannot get post with id " << post_id;
1010
1020
        tmp.setStatus(KBlog::BlogPost::Error);
1011
1021
    }
1024
1034
            list.insert( q.value( 1 ).toInt(), q.value( 0 ).toString() );
1025
1035
        }
1026
1036
    } else {
1027
 
        mLastErrorText = q.lastError().text();
 
1037
        d->mLastErrorText = q.lastError().text();
1028
1038
        kDebug() << "Cannot get list of posts for blog with id " << blog_id;
1029
1039
    }
1030
1040
    return list;
1046
1056
            list.append(entry);
1047
1057
        }
1048
1058
    } else {
1049
 
        mLastErrorText = q.lastError().text();
 
1059
        d->mLastErrorText = q.lastError().text();
1050
1060
        kDebug() << "Cannot get list of posts for blog with id " << blog_id;
1051
1061
    }
1052
1062
    return list;
1063
1073
            list[q.value( 0 ).toString()] = q.value( 1 ).toInt();
1064
1074
        }
1065
1075
    } else {
1066
 
        mLastErrorText = q.lastError().text();
 
1076
        d->mLastErrorText = q.lastError().text();
1067
1077
        kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1068
1078
    }
1069
1079
    return list;
1090
1100
            list.append( c );
1091
1101
        }
1092
1102
    } else {
1093
 
        mLastErrorText = q.lastError().text();
 
1103
        d->mLastErrorText = q.lastError().text();
1094
1104
        kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1095
1105
    }
1096
1106
    return list;
1108
1118
            list.insert( q.value( 0 ).toString(), false );
1109
1119
        }
1110
1120
    } else {
1111
 
        mLastErrorText = q.lastError().text();
 
1121
        d->mLastErrorText = q.lastError().text();
1112
1122
        kDebug() << "Cannot get list of categories for blog with id " << blog_id;
1113
1123
    }
1114
1124
    return list;
1118
1128
{
1119
1129
    QMap<BilboPost*, int> list;
1120
1130
    QSqlQuery q;
1121
 
    q.prepare( "SELECT id, local_id, postid, blog_id, author, title, content, text_more, c_time,\
 
1131
    q.prepare( "SELECT local_id, id, postid, blog_id, author, title, content, text_more, c_time,\
1122
1132
    m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link, summary, tags, status,\
1123
1133
    slug FROM temp_post ORDER BY m_time DESC" );
1124
1134
    if ( q.exec() ) {
1125
1135
        while ( q.next() ) {
1126
1136
            BilboPost *tmp = new BilboPost();
1127
 
            int id = q.value( 0 ).toInt();
1128
 
            int local_id = q.value( 1 ).toInt();
 
1137
            tmp->setLocalId( q.value( 0 ).toInt() );
 
1138
            tmp->setId( q.value( 1 ).toInt() );
1129
1139
            tmp->setPostId( q.value( 2 ).toString() );
1130
1140
            int blog_id = q.value( 3 ).toInt();
1131
1141
            tmp->setAuthor( q.value( 4 ).toString() );
1143
1153
            tmp->setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
1144
1154
            tmp->setSlug( q.value( 18 ).toString() );
1145
1155
 
1146
 
            if(tmp->status() == KBlog::BlogPost::New){
1147
 
                tmp->setId(local_id);
1148
 
            } else {
1149
 
                tmp->setId(id);
1150
 
            }
1151
1156
            ///get Category list:
1152
1157
            QList<Category> catList;
1153
1158
            QSqlQuery q2;
1155
1160
            category.categoryId, category.parentId\
1156
1161
            FROM category JOIN temp_post_cat ON category.categoryId=temp_post_cat.categoryId\
1157
1162
            WHERE temp_post_cat.local_id = ?" );
1158
 
            q2.addBindValue( local_id );
 
1163
            q2.addBindValue( tmp->localId() );
1159
1164
//             q2.addBindValue( blog_id );
1160
1165
            if ( q2.exec() ) {
1161
1166
                while ( q2.next() ) {
1172
1177
                tmp->setCategoryList( catList );
1173
1178
                list.insert( tmp, blog_id);
1174
1179
            } else {
1175
 
                mLastErrorText = q2.lastError().text();
 
1180
                d->mLastErrorText = q2.lastError().text();
1176
1181
                kDebug()<<"Cannot get categories list of a post. SQL Error: "<< q2.lastError().text();
1177
1182
            }
1178
1183
        }
1179
1184
    } else {
1180
 
        mLastErrorText = q.lastError().text();
 
1185
        d->mLastErrorText = q.lastError().text();
1181
1186
        kDebug() << "Cannot get list of temporary posts, SQL Error: "<< q.lastError().text();
1182
1187
    }
1183
1188
    return list;
1200
1205
            list.append(entry);
1201
1206
        }
1202
1207
    } else {
1203
 
        mLastErrorText = q.lastError().text();
 
1208
        d->mLastErrorText = q.lastError().text();
1204
1209
        kDebug() << "Cannot get list of local posts. SQL Error: "<< q.lastError().text();
1205
1210
    }
1206
1211
    return list;
1216
1221
    q.addBindValue(local_id);
1217
1222
    if ( q.exec() ) {
1218
1223
        if ( q.next() ) {
1219
 
            int id = q.value( 0 ).toInt();
 
1224
            tmp.setId( q.value( 0 ).toInt() );
 
1225
            tmp.setLocalId( q.value( 1 ).toInt() );
1220
1226
            tmp.setPostId( q.value( 2 ).toString() );
1221
1227
            int blog_id = q.value( 3 ).toInt();
1222
1228
            tmp.setAuthor( q.value( 4 ).toString() );
1235
1241
            tmp.setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
1236
1242
            tmp.setSlug( q.value( 18 ).toString() );
1237
1243
 
1238
 
            if(tmp.status() == KBlog::BlogPost::New){
1239
 
                tmp.setId(local_id);
1240
 
            } else {
1241
 
                tmp.setId(id);
1242
 
            }
1243
1244
            ///get Category list:
1244
1245
            QList<Category> catList;
1245
1246
            QSqlQuery q2;
1262
1263
                }
1263
1264
                tmp.setCategoryList( catList );
1264
1265
            } else {
1265
 
                mLastErrorText = q2.lastError().text();
 
1266
                d->mLastErrorText = q2.lastError().text();
1266
1267
                kDebug()<<"Cannot get categories list of local post. SQL Error: "<< q2.lastError().text();
1267
1268
            }
1268
1269
        } else {
1269
 
            mLastErrorText = i18n( "There is no local post with the requested ID " );
 
1270
            d->mLastErrorText = i18n( "There is no local post with the requested ID " );
1270
1271
            kDebug()<<"there isn't any local post with local_id "<<local_id;
1271
1272
        }
1272
1273
    } else {
1273
 
        mLastErrorText = q.lastError().text();
 
1274
        d->mLastErrorText = q.lastError().text();
1274
1275
        kDebug() << "Cannot get local post. SQL Error: "<< q.lastError().text();
1275
1276
    }
1276
1277
    return tmp;