42 #include <libkdepim/widgets/spellchecklineedit.h>
46 #include <klocalizedstring.h>
47 #include <klineedit.h>
48 #include <KMessageBox>
49 #include <ktexteditor/view.h>
50 #include <ktexteditor/document.h>
54 #include <QProgressBar>
58 #include <QHBoxLayout>
62 class PostEntry::Private
65 QPointer<QProgressBar> progress;
66 QGridLayout *gridLayout;
67 QHBoxLayout *horizontalLayout;
69 KPIM::SpellCheckLineEdit *txtTitle;
72 int mCurrentPostBlogId;
73 QMap <QString, BilboMedia*> mMediaList;
75 int mNumOfFilesToBeUploaded;
76 bool isUploadingMediaFilesFailed;
78 bool isPostContentModified;
87 KTextEditor::View *htmlEditor;
94 :
QFrame( parent ), d(new Private)
98 connect( d->htmlEditor->document(), SIGNAL(
textChanged(KTextEditor::Document*)),
100 layout()->addWidget( d->tabWidget );
101 d->mTimer =
new QTimer(
this);
103 connect( d->mTimer, SIGNAL(timeout()),
this, SLOT(
saveTemporary()) );
105 d->mCurrentPostBlogId = -1;
106 d->mNumOfFilesToBeUploaded = 0;
107 d->isPostContentModified =
false;
126 void PostEntry::createUi()
129 d->tabVisual =
new QWidget( d->tabWidget );
130 d->tabHtml =
new QWidget( d->tabWidget );
131 d->tabPreview =
new QWidget( d->tabWidget );
132 d->tabWidget->addTab( d->tabVisual, i18nc(
"Software",
"Visual Editor" ) );
133 d->tabWidget->addTab( d->tabHtml, i18nc(
"Software",
"Html Editor" ) );
134 d->tabWidget->addTab( d->tabPreview, i18nc(
"preview of the edited post",
"Post Preview" ) );
135 connect( d->tabWidget, SIGNAL(currentChanged(
int)),
this, SLOT(
slotSyncEditors(
int)) );
141 QVBoxLayout *vLayout =
new QVBoxLayout( d->tabVisual );
142 vLayout->addWidget( d->wysiwygEditor );
146 QGridLayout *hLayout =
new QGridLayout( d->tabHtml );
147 hLayout->addWidget( d->htmlEditor );
151 QGridLayout *gLayout =
new QGridLayout( d->tabPreview );
152 gLayout->addWidget( d->previewer );
157 d->tabWidget->setCurrentIndex( 0 );
159 d->gridLayout =
new QGridLayout(
this );
161 d->horizontalLayout =
new QHBoxLayout();
162 d->horizontalLayout->setSizeConstraint( QLayout::SetDefaultConstraint );
164 d->labelTitle =
new QLabel(
this );
165 d->labelTitle->setText( i18nc(
"noun, the post title",
"Title:" ) );
166 d->horizontalLayout->addWidget( d->labelTitle );
168 d->txtTitle =
new KPIM::SpellCheckLineEdit(
this, QLatin1String(
"blogilorc" ) );
169 d->horizontalLayout->addWidget( d->txtTitle );
170 d->labelTitle->setBuddy( d->txtTitle );
175 d->gridLayout->addLayout( d->horizontalLayout, 0, 0, 1, 1 );
180 switch(d->tabWidget->currentIndex()) {
182 d->wysiwygEditor->editor()->startEditing();
185 d->htmlEditor->setFocus();
193 if ( d->prev_index == 2 ) {
194 d->previewer->stop();
195 d->prev_index = index;
198 d->wysiwygEditor->editor()->setHtmlContent(d->htmlEditor->document()->text());
199 d->wysiwygEditor->editor()->setFocus();
200 d->wysiwygEditor->editor()->startEditing();
201 }
else if ( index == 1 ) {
202 if ( d->prev_index == 2 ) {
203 d->previewer->stop();
204 d->prev_index = index;
207 d->htmlEditor->document()->setText( d->wysiwygEditor->editor()->htmlContent() );
208 d->htmlEditor->setFocus();
210 if ( d->prev_index == 1 ) {
211 d->wysiwygEditor->editor()->setHtmlContent(d->htmlEditor->document()->text());
213 d->htmlEditor->document()->setText( d->wysiwygEditor->editor()->htmlContent() );
215 d->previewer->setHtml( d->txtTitle->toPlainText(), d->htmlEditor->document()->text() );
217 d->prev_index = index;
222 if ( d->tabWidget->currentIndex() == 2 ) {
223 d->previewer->setHtml( d->txtTitle->toPlainText(), d->htmlEditor->document()->text() );
229 if ( d->tabWidget->currentIndex() == 1 ) {
230 d->wysiwygEditor->editor()->setHtmlContent( d->htmlEditor->document()->text() );
232 d->htmlEditor->document()->setText( d->wysiwygEditor->editor()->htmlContent() );
234 return d->htmlEditor->document()->text();
239 return d->wysiwygEditor->editor()->plainTextContent();
244 d->wysiwygEditor->editor()->setHtmlContent(content);
245 d->htmlEditor->document()->setText( content );
250 const QString titleText(d->txtTitle->toPlainText());
251 d->mCurrentPost.setTitle(titleText);
257 return d->mCurrentPost.title();
262 d->txtTitle->setPlainText( title );
263 d->mCurrentPost.setTitle( title );
269 if (additionalContent.isEmpty()) {
272 body = content + QLatin1String(
"<hr/><!--split-->") + additionalContent;
273 d->mCurrentPost.setAdditionalContent(QString());
278 d->mCurrentPost.setContent( body );
280 d->isPostContentModified =
false;
287 return d->mCurrentPostBlogId;
292 d->mCurrentPostBlogId = blog_id;
293 if ( blog_id != -1 &&
DBMan::self()->blogList().contains( blog_id ) ) {
298 void PostEntry::setCurrentPostFromEditor()
300 if ( d->isPostContentModified ) {
303 d->mCurrentPost.setContent( str );
304 d->isPostContentModified =
false;
311 setCurrentPostFromEditor();
312 return &d->mCurrentPost;
317 d->mCurrentPost = post;
318 kDebug()<<
"local_id: "<<d->mCurrentPost.localId();
319 this->
setPostBody( d->mCurrentPost.content(), d->mCurrentPost.additionalContent() );
325 return d->txtTitle->layoutDirection();
331 d->tabWidget->setLayoutDirection( direction );
332 d->txtTitle->setLayoutDirection( direction );
337 return d->wysiwygEditor->editor()->getLocalImages();
342 d->wysiwygEditor->editor()->replaceImageSrc(src, dest);
347 bool localBackend =
false;
351 backend =
new Backend( d->mCurrentPostBlogId,
this );
354 if ( !lImages.isEmpty() ) {
356 QList<BilboMedia*>::iterator it = lImages.begin();
357 QList<BilboMedia*>::iterator endIt = lImages.end();
358 for ( ; it != endIt; ++it ) {
365 const QString err = i18n(
"Uploading the media file %1 failed.\n%2",
371 uploader->deleteLater();
376 backend->deleteLater();
382 const QString err = i18n(
"An error occurred in the last transaction.\n%1", errMsg );
385 sender()->deleteLater();
390 setCurrentPostFromEditor();
391 if ( d->mCurrentPost.content().isEmpty() || d->mCurrentPost.title().isEmpty() ) {
392 if ( KMessageBox::warningContinueCancel(
this,
393 i18n(
"Your post title or body is empty.\nAre you sure you want to submit this post?" )
394 ) == KMessageBox::Cancel )
398 if (d->mCurrentPost.status() == BilboPost::New)
400 QPointer<SendToBlogDialog> dia =
new SendToBlogDialog( isNew, d->mCurrentPost.isPrivate(),
this);
401 dia->setAttribute(Qt::WA_DeleteOnClose,
false);
402 if ( dia->exec() == KDialog::Accepted ) {
403 this->setCursor( Qt::BusyCursor );
404 d->mCurrentPost.setProperties( postData );
405 d->mCurrentPostBlogId = blogId;
408 if (dia->isPrivate()) {
409 msgType = i18nc(
"Post status, e.g Draft or Published Post",
"draft");
410 d->mCurrentPost.setPrivate(
true);
412 msgType = i18nc(
"Post status, e.g Draft or Published Post",
"post");
413 d->mCurrentPost.setPrivate(
false);
418 statusMsg = i18n(
"Submitting new %1...", msgType);
421 statusMsg = i18n(
"Modifying %1...", msgType);
422 d->isNewPost =
false;
427 connect( b, SIGNAL(sigError(QString)),
this, SLOT(
slotError(QString)) );
429 kDebug()<<
"Uploading";
431 connect( b, SIGNAL(sigPostPublished(
int,
BilboPost*)),
446 kDebug() <<
"BlogId: " << blog_id <<
"Post Id on server: " << post->postId();
450 if ( d->mCurrentPost.isPrivate() ) {
451 msg = i18n(
"Draft with title \"%1\" saved successfully.", post->title() );
452 }
else if (d->mCurrentPost.status() == BilboPost::Modified){
453 msg = i18n(
"Post with title \"%1\" modified successfully.", post->title() );
455 msg = i18n(
"Post with title \"%1\" published successfully.", post->title() );
461 sender()->deleteLater();
466 if ( !d->progress ) {
467 d->progress =
new QProgressBar(
this );
468 layout()->addWidget( d->progress );
469 d->progress->setRange( 0, 0 );
476 this->layout()->removeWidget( d->progress );
477 d->progress->deleteLater();
485 if ( KMessageBox::warningYesNo(
this, i18n(
"The current post content is empty, are you sure you want to save an empty post?")) == KMessageBox::No )
490 KMessageBox::detailedSorry(
this, i18n(
"Saving post locally failed."),
DBMan::self()->lastErrorText());
493 d->mCurrentPost.setLocalId( resId );
496 kDebug()<<
"Locally saved";
501 if ( d->isPostContentModified ) {
504 d->mCurrentPost.setLocalId( res );
506 kDebug()<<
"Temporary saved";
519 d->isPostContentModified =
true;
522 #include "postentry.moc"
void showStatusMessage(const QString &message, bool isPermanent)
To show a message on statusBar.
static HtmlEditor * self()
void postTitleChanged(const QString &title)
emitted when title of this entry changed.
QString lastErrorText() const
void postPublishingDone(bool isError, const QString &customMessage)
This signal emitted when a post manipulation job e.g.
void replaceImageSrc(const QString &src, const QString &dest)
void slotSyncEditors(int index)
void submitPost(int blogId, const BilboPost &postData)
void publishPost(BilboPost *post)
Use this to publish a post to server.
QString htmlContent() const
Returns the editor current text in html format Synchronizes HtmlEditor and editor tabs...
void setHtmlContent(const QString &content)
Sets the given string as the HtmlEditor and VisualEditor content.
int saveTempEntry(const BilboPost &post, int blog_id)
static DBMan * self()
Retrieve the instance of DataBase Manager.
Qt::LayoutDirection defaultLayoutDirection() const
Definition of a blog post! it's implemented to decrease dependency to KBlog :)
QString errorMessage() const
int saveLocalEntry(const BilboPost &post, int blog_id)
void setPostTitle(const QString &title)
void slotSetPostPreview()
QString plainTextContent() const
void slotError(const QString &errMsg)
void setDefaultLayoutDirection(Qt::LayoutDirection direction)
void textChanged()
This signal is emitted when the content of VisualEditor or HtmlEditor changes.
void modifyPost(BilboPost *post)
Modify an existing post.
KTextEditor::View * createView(QWidget *parent)
bool removeTempEntry(const BilboPost &post)
void slotPostPublished(int blog_id, BilboPost *post)
void postSavedTemporary()
This signal is emitted when the post is saved temporarily!
bool uploadMedia(Backend *backend, BilboMedia *media)
Synchronous Media file uploader!
int currentPostBlogId() const
bool uploadMediaFiles(Backend *backend=0)
Will Upload media files not uploaded yet, and return true on success and false on failure...
PostEntry(QWidget *parent)
void setPostBody(const QString &content, const QString &additionalContent=QString())
static int autosaveInterval()
Get autosaveInterval.
BilboPost * currentPost()
QList< BilboMedia * > localImages() const
void setCurrentPost(const BilboPost &post)
Implements a simple browser widget for use in blogilo Post Preview.
QString postTitle() const
void setCurrentPostBlogId(int blog_id)