Commit 154d5129 authored by Mehrdad Momeny's avatar Mehrdad Momeny

problem in Save/Load temporary posts on exit/start fixed.

svn path=/trunk/KDE/kdepim/blogilo/; revision=1196936
parent 19f96d8d
......@@ -32,6 +32,7 @@ class BilboPostPrivate
public:
QString mAuthor;
int mId;///id in DB
int localId;
// Position mPosition;
bool mModifyTimeStamp;///Just for toolbox entry!
QList<Category> mCategoryList;
......@@ -48,6 +49,7 @@ BilboPost::BilboPost()
this->setTrackBackAllowed( true );
d_ptr->mModifyTimeStamp = false;
this->setId( -1 );
this->setLocalId( -1 );
this->setStatus( KBlog::BlogPost::New );
}
......@@ -56,6 +58,7 @@ BilboPost::BilboPost( const KBlog::BlogPost &post )
{
kDebug()<<"KBlog::BlogPost";
d_ptr->mId = -1;
d_ptr->localId = -1;
d_ptr->mModifyTimeStamp = false;
}
......@@ -66,6 +69,7 @@ BilboPost::BilboPost( const BilboPost &post )
this->setAuthor( post.author() );
this->setModifyTimeStamp( post.isModifyTimeStamp() );
this->setId( post.id() );
setLocalId( post.localId() );
this->setCategoryList( post.categoryList() );
}
......@@ -94,33 +98,16 @@ void BilboPost::setAuthor( const QString &author )
{
d_ptr->mAuthor = author;
}
/*
KBlog::BlogPost * BilboPost::toKBlogPost()
{
KBlog::BlogPost *pp = new KBlog::BlogPost( QString() );
pp->setStatus( this->status() );
pp->setSummary( this->summary() );
pp->setTags( this->tags() );
pp->setTitle( this->title() );
pp->setTrackBackAllowed( this->isTrackBackAllowed() );
pp->setCategories( this->categories() );
pp->setCommentAllowed( this->isCommentAllowed() );
pp->setContent( this->content() );
pp->setPrivate( this->isPrivate() );
pp->setCreationDateTime( this->creationDateTime() );
pp->setError( this->error() );
pp->setModificationDateTime( this->modificationDateTime() );
pp->setMood( this->mood() );
pp->setMusic( this->music() );
pp->setPostId( this->postId() );
pp->setLink( this->link() );
pp->setPermaLink( this->permaLink() );
pp->setStatus( this->status() );
pp->setAdditionalContent( this->additionalContent() );
pp->setSlug( this->slug() );
return pp;
}*/
int BilboPost::localId() const
{
return d_ptr->localId;
}
void BilboPost::setLocalId(const int localId)
{
d_ptr->localId = localId;
}
QString BilboPost::toString() const
{
......
......@@ -55,6 +55,10 @@ public:
int id() const;
void setId( const int );
//To be used for local_post and temp_post tabels
int localId() const;
void setLocalId( const int );
// KBlog::BlogPost * toKBlogPost();
QString toString() const;
......
......@@ -646,15 +646,17 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
postTable = "temp_post";
postCatTable = "temp_post_cat";
}
int postId = -1, localId=-1;
if(post.status() == KBlog::BlogPost::New) {///Post is new!
kDebug()<<"Post is new!";
if(post.id() == -1){
int localId = post.localId();
// if(post.status() == KBlog::BlogPost::New) {///Post is new!
// kDebug()<<"Post is new!";
if(post.localId() == -1){
///Add new post to temp_post
kDebug()<<"Add new post to temp_post";
q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (postid, blog_id, author, title, content,\
text_more, c_time, m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link,\
summary, slug, tags, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (postid, blog_id,\
author, title, content, text_more, c_time, m_time, is_private, is_comment_allowed,\
is_trackback_allowed, link, perma_link, summary, slug, tags, status)\
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
// q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
q.addBindValue( post.postId() );
q.addBindValue( blog_id );
q.addBindValue( post.author() );
......@@ -674,7 +676,7 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
q.addBindValue( post.status() );
if ( q.exec() ) {
localId = postId = q.lastInsertId().toInt();
localId = q.lastInsertId().toInt();
} else {
d->mLastErrorText = q.lastError().text();
kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
......@@ -683,11 +685,11 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
} else {
///Update post, with id!
kDebug()<<"Update post, with id!";
q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (local_id, postid, blog_id, author, title,\
content, text_more, c_time, m_time, is_private, is_comment_allowed, is_trackback_allowed, link,\
perma_link, summary, slug, tags, status)\
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
q.addBindValue( post.id() );
q.prepare( "UPDATE "+ postTable +" SET postid=?, blog_id=?,\
author=?, title=?, content=?, text_more=?, c_time=?, m_time=?, is_private=?, is_comment_allowed=?,\
is_trackback_allowed=?, link=?, perma_link=?, summary=?, slug=?, tags=?, status=?\
WHERE local_id=?" );
// q.addBindValue( post.id() == -1 ? QVariant(QVariant::Int) : post.id() );
q.addBindValue( post.postId() );
q.addBindValue( blog_id );
q.addBindValue( post.author() );
......@@ -705,16 +707,15 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
q.addBindValue( post.slug() );
q.addBindValue( post.tags().join(QString(',')) );
q.addBindValue( post.status() );
q.addBindValue( post.localId() );
if ( q.exec() ) {
localId = postId = q.lastInsertId().toInt();
} else {
if ( !q.exec() ) {
d->mLastErrorText = q.lastError().text();
kDebug() << "Cannot Add new local post to database!\n\tSQL Error: " << q.lastError().text();
return -1;
}
}
} else {///Post is already created at "Post" table and has a valid id, postId and blog_id. So, local_id is useless here
/*} else {///Post is already created at "Post" table and has a valid id, postId and blog_id. So, local_id is useless here
kDebug()<<"Post is already created at \"Post\" table and has a valid id, postId and blog_id. So, local_id is useless here";
q.prepare( "INSERT OR REPLACE INTO "+ postTable +" (id, postid, blog_id, author, title,\
content, text_more, c_time, m_time, is_private,\
......@@ -747,7 +748,7 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
kDebug() << "Cannot Add or Edit local post to database!\n\tSQL Error: " << q.lastError().text();
return -1;
}
}
}*/
///Delete previouse Categories:
QSqlQuery qd;
......@@ -777,19 +778,15 @@ int DBMan::saveTemp_LocalEntry( const BilboPost& basePost, int blog_id, LocalPos
// kDebug()<<"category "<<post.categories()[i] <<" added.";
}
}
return postId;
return localId;
}
bool DBMan::removeLocalEntry( const BilboPost &post )
{
kDebug();
QSqlQuery q;
if(post.status() == KBlog::BlogPost::New) {
q.prepare( "DELETE FROM local_post WHERE local_id=?" );
} else {
q.prepare( "DELETE FROM local_post WHERE id=?" );
}
q.addBindValue( post.id() );
q.prepare( "DELETE FROM local_post WHERE local_id=?" );
q.addBindValue( post.localId() );
bool res = q.exec();
if ( !res ) {
d->mLastErrorText = q.lastError().text();
......@@ -816,12 +813,8 @@ bool DBMan::removeTempEntry( const BilboPost &post )
{
kDebug()<<post.toString();
QSqlQuery q;
if(post.status() == KBlog::BlogPost::New) {
q.prepare( "DELETE FROM temp_post WHERE local_id=?" );
} else {
q.prepare( "DELETE FROM temp_post WHERE id=?" );
}
q.addBindValue( post.id() );
q.prepare( "DELETE FROM temp_post WHERE local_id=?" );
q.addBindValue( post.localId() );
bool res = q.exec();
if ( !res ) {
d->mLastErrorText = q.lastError().text();
......@@ -1135,14 +1128,14 @@ QMap<BilboPost*, int> DBMan::listTempPosts()
{
QMap<BilboPost*, int> list;
QSqlQuery q;
q.prepare( "SELECT id, local_id, postid, blog_id, author, title, content, text_more, c_time,\
q.prepare( "SELECT local_id, id, postid, blog_id, author, title, content, text_more, c_time,\
m_time, is_private, is_comment_allowed, is_trackback_allowed, link, perma_link, summary, tags, status,\
slug FROM temp_post ORDER BY m_time DESC" );
if ( q.exec() ) {
while ( q.next() ) {
BilboPost *tmp = new BilboPost();
int id = q.value( 0 ).toInt();
int local_id = q.value( 1 ).toInt();
tmp->setLocalId( q.value( 0 ).toInt() );
tmp->setId( q.value( 1 ).toInt() );
tmp->setPostId( q.value( 2 ).toString() );
int blog_id = q.value( 3 ).toInt();
tmp->setAuthor( q.value( 4 ).toString() );
......@@ -1160,11 +1153,6 @@ QMap<BilboPost*, int> DBMan::listTempPosts()
tmp->setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
tmp->setSlug( q.value( 18 ).toString() );
if(tmp->status() == KBlog::BlogPost::New){
tmp->setId(local_id);
} else {
tmp->setId(id);
}
///get Category list:
QList<Category> catList;
QSqlQuery q2;
......@@ -1172,7 +1160,7 @@ QMap<BilboPost*, int> DBMan::listTempPosts()
category.categoryId, category.parentId\
FROM category JOIN temp_post_cat ON category.categoryId=temp_post_cat.categoryId\
WHERE temp_post_cat.local_id = ?" );
q2.addBindValue( local_id );
q2.addBindValue( tmp->localId() );
// q2.addBindValue( blog_id );
if ( q2.exec() ) {
while ( q2.next() ) {
......@@ -1233,7 +1221,8 @@ BilboPost DBMan::localPost(int local_id)
q.addBindValue(local_id);
if ( q.exec() ) {
if ( q.next() ) {
int id = q.value( 0 ).toInt();
tmp.setId( q.value( 0 ).toInt() );
tmp.setLocalId( q.value( 1 ).toInt() );
tmp.setPostId( q.value( 2 ).toString() );
int blog_id = q.value( 3 ).toInt();
tmp.setAuthor( q.value( 4 ).toString() );
......@@ -1252,11 +1241,6 @@ BilboPost DBMan::localPost(int local_id)
tmp.setStatus(( KBlog::BlogPost::Status ) q.value( 17 ).toInt() );
tmp.setSlug( q.value( 18 ).toString() );
if(tmp.status() == KBlog::BlogPost::New){
tmp.setId(local_id);
} else {
tmp.setId(id);
}
///get Category list:
QList<Category> catList;
QSqlQuery q2;
......
......@@ -130,16 +130,26 @@ MainWindow::MainWindow()
MainWindow::~MainWindow()
{
kDebug();
int count = tabPosts->count();
for(int i =0; i<count; ++i)
qobject_cast<PostEntry*>(tabPosts->widget(i))->aboutToQuit();
}
bool MainWindow::queryExit()
{
kDebug();
writeConfigs();
qApp->quit();
if( !DBMan::self()->clearTempEntries() )
kDebug()<<"Could not erase temp_post table: "<< DBMan::self()->lastErrorText();
int count = tabPosts->count();
toolbox->getFieldsValue(activePost->currentPost());
for(int i =0; i<count; ++i) {
PostEntry* pst = qobject_cast<PostEntry*>(tabPosts->widget(i));
DBMan::self()->saveTempEntry(*pst->currentPost(), pst->currentPostBlogId());
}
return true;
}
void MainWindow::setupActions()
{
KStandardAction::quit( this, SLOT( close() ), actionCollection() );
KStandardAction::quit( qApp, SLOT( quit() ), actionCollection() );
KStandardAction::preferences( this, SLOT( optionsPreferences() ), actionCollection() );
......@@ -418,7 +428,7 @@ void MainWindow::slotActivePostChanged( int index )
if (( prevActivePost != 0 ) && ( index != previousActivePostIndex ) ) {
prevPostBlogId = prevActivePost->currentPostBlogId();
toolbox->getFieldsValue( *prevActivePost->currentPost() );
toolbox->getFieldsValue( prevActivePost->currentPost() );
prevActivePost->setCurrentPostBlogId( mCurrentBlogId );
}
......@@ -447,10 +457,9 @@ void MainWindow::slotPublishPost()
kDebug() << "There isn't any post";
return;
}
BilboPost post = *activePost->currentPost();
toolbox->getFieldsValue( post );
toolbox->getFieldsValue( activePost->currentPost() );
// post.setPrivate( false );
activePost->submitPost( mCurrentBlogId, post );
activePost->submitPost( mCurrentBlogId, *activePost->currentPost() );
}
void MainWindow::slotRemovePostEntry( PostEntry *widget )
......@@ -493,7 +502,7 @@ void MainWindow::slotSavePostLocally()
{
kDebug();
if(activePost && tabPosts->count() > 0) {
toolbox->getFieldsValue(*activePost->currentPost());
toolbox->getFieldsValue(activePost->currentPost());
activePost->saveLocally();
toolbox->reloadLocalPosts();
}
......
......@@ -68,7 +68,6 @@ protected slots:
void slotToggleToolboxVisible( bool isVisible );
void slotToolboxVisibilityChanged( bool isVisible );
void slotActivePostChanged( int tabIndex );
void slotNewPostOpened( BilboPost &newPost, int blog_id );
void slotPublishPost();
......@@ -104,6 +103,7 @@ private slots:
protected:
void keyPressEvent( QKeyEvent * event );
virtual bool queryExit();
private:
void setCurrentBlog( int blog_id );
......@@ -130,6 +130,5 @@ private:
KSelectAction *blogs;
int &mCurrentBlogId;
// KTextEditor::Editor *mHtmlEditor;
};
#endif
......@@ -108,12 +108,6 @@ PostEntry::PostEntry( QWidget *parent )
connect( this, SIGNAL(textChanged()), this, SLOT(slotPostModified()) );
}
void PostEntry::aboutToQuit()
{
kDebug();
saveTemporary(true);
}
void PostEntry::settingsChanged()
{
kDebug();
......@@ -308,8 +302,8 @@ BilboPost* PostEntry::currentPost()
void PostEntry::setCurrentPost( const BilboPost &post )
{
kDebug();
d->mCurrentPost = BilboPost( post );
// kDebug()<<"postId: "<<mCurrentPost.postId();
d->mCurrentPost = post;
kDebug()<<"local_id: "<<d->mCurrentPost.localId();
this->setPostBody( d->mCurrentPost.content(), d->mCurrentPost.additionalContent() );
this->setPostTitle( d->mCurrentPost.title() );
}
......@@ -487,18 +481,23 @@ void PostEntry::saveLocally()
are you sure you want to save an empty post?")) == KMessageBox::No )
return;
}
d->mCurrentPost.setId( DBMan::self()->saveLocalEntry( *currentPost(), d->mCurrentPostBlogId ) );
d->mCurrentPost.setLocalId( DBMan::self()->saveLocalEntry( *currentPost(), d->mCurrentPostBlogId ) );
emit postSavedLocally();
emit showStatusMessage(i18n( "Post saved locally." ), false);
kDebug()<<"Locally saved";
}
void PostEntry::saveTemporary( bool force )
void PostEntry::saveTemporary()
{
if( d->isPostContentModified || ( !plainTextContent().isEmpty() && force ) ) {
d->mCurrentPost.setId( DBMan::self()->saveTempEntry( *currentPost(), d->mCurrentPostBlogId) );
emit postSavedTemporary();
kDebug()<<"Temporary saved";
if( d->isPostContentModified ) {
int res = DBMan::self()->saveTempEntry( *currentPost(), d->mCurrentPostBlogId);
if(res != -1) {
d->mCurrentPost.setLocalId( res );
emit postSavedTemporary();
kDebug()<<"Temporary saved";
} else {
kDebug()<<"Saving temporary failed: "<< DBMan::self()->lastErrorText();
}
}
}
......
......@@ -73,8 +73,6 @@ public:
void saveLocally();
void aboutToQuit();
Q_SIGNALS:
/**
* emitted when title of this entry changed.
......@@ -124,7 +122,7 @@ protected Q_SLOTS:
void slotPostPublished( int blog_id, BilboPost *post );
void slotTitleChanged( const QString& title );
void deleteProgressBar();
void saveTemporary( bool force=false );
void saveTemporary();
void slotPostModified();
/*!
......
......@@ -292,36 +292,36 @@ void Toolbox::clearCatList()
d->listCategoryCheckBoxes.clear();
}
void Toolbox::getFieldsValue( BilboPost &currentPost )
void Toolbox::getFieldsValue( BilboPost* currentPost )
{
kDebug();
currentPost.setCategoryList( this->selectedCategories() );
currentPost.setTags( this->currentTags() );
currentPost.setModifyTimeStamp( this->chkOptionsTime->isChecked() );
if ( currentPost.status() == KBlog::BlogPost::New ) {
currentPost->setCategoryList( this->selectedCategories() );
currentPost->setTags( this->currentTags() );
currentPost->setModifyTimeStamp( this->chkOptionsTime->isChecked() );
if ( currentPost->status() == KBlog::BlogPost::New ) {
if ( chkOptionsTime->isChecked() ) {
currentPost.setModificationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost.setCreationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost->setModificationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost->setCreationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
} else {
currentPost.setCreationDateTime( KDateTime::currentLocalDateTime() );
currentPost.setModificationDateTime( KDateTime::currentLocalDateTime() );
currentPost->setCreationDateTime( KDateTime::currentLocalDateTime() );
currentPost->setModificationDateTime( KDateTime::currentLocalDateTime() );
}
} else {
currentPost.setCreationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost.setModificationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost->setCreationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
currentPost->setModificationDateTime( KDateTime( optionsDate->date(), optionsTime->time() ) );
}
if( currentPost.creationDateTime().isUtc() || currentPost.modificationDateTime().isUtc() ){
if( currentPost->creationDateTime().isUtc() || currentPost->modificationDateTime().isUtc() ){
kDebug()<<"creationDateTime was UTC!";
currentPost.setCreationDateTime( KDateTime( currentPost.creationDateTime().dateTime(),
currentPost->setCreationDateTime( KDateTime( currentPost->creationDateTime().dateTime(),
KDateTime::LocalZone ) );
currentPost.setModificationDateTime( KDateTime( currentPost.modificationDateTime().dateTime(),
currentPost->setModificationDateTime( KDateTime( currentPost->modificationDateTime().dateTime(),
KDateTime::LocalZone ) );
}
currentPost.setSlug( txtSlug->text() );
currentPost.setPrivate(( comboOptionsStatus->currentIndex() == 1 ) ? true : false );
currentPost.setCommentAllowed( chkOptionsComments->isChecked() );
currentPost.setTrackBackAllowed( chkOptionsTrackback->isChecked() );
currentPost.setSummary( txtSummary->toPlainText() );
currentPost->setSlug( txtSlug->text() );
currentPost->setPrivate(( comboOptionsStatus->currentIndex() == 1 ) ? true : false );
currentPost->setCommentAllowed( chkOptionsComments->isChecked() );
currentPost->setTrackBackAllowed( chkOptionsTrackback->isChecked() );
currentPost->setSummary( txtSummary->toPlainText() );
}
void Toolbox::setFieldsValue( BilboPost* post )
......
......@@ -50,7 +50,7 @@ public:
* Will set current state of toolbox (Current post) properties on input pointer!
* @param currentPost input and output of this Function.
*/
void getFieldsValue( BilboPost &currentPost );
void getFieldsValue( BilboPost* currentPost );
void setFieldsValue( BilboPost* post = 0 );
void setCurrentBlogId( int blog_id );
void setCurrentPage( int index );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment