From a7c0ff95aba382b0e2390157c24e5691237e473d Mon Sep 17 00:00:00 2001 From: Montel Laurent Date: Wed, 30 Nov 2011 15:13:14 +0100 Subject: [PATCH] Reduce duplicate code --- kmcomposereditor.cpp | 81 +-------------------------- kmcomposewin.cpp | 127 ++++++++++++++++++++++++++++++------------- kmcomposewin.h | 4 +- 3 files changed, 95 insertions(+), 117 deletions(-) diff --git a/kmcomposereditor.cpp b/kmcomposereditor.cpp index 75c3f1e9b..5a35b9ddb 100644 --- a/kmcomposereditor.cpp +++ b/kmcomposereditor.cpp @@ -128,88 +128,13 @@ bool KMComposerEditor::canInsertFromMimeData( const QMimeData *source ) const void KMComposerEditor::insertFromMimeData( const QMimeData *source ) { - // If this is a PNG image, either add it as an attachment or as an inline image - if ( source->hasImage() && source->hasFormat( "image/png" ) ) { - // Get the image data before showing the dialog, since that processes events which can delete - // the QMimeData object behind our back - const QByteArray imageData = source->data( "image/png" ); - - if ( textMode() == KRichTextEdit::Rich && isEnableImageActions() ) { - QImage image = qvariant_cast( source->imageData() ); - QFileInfo fi( source->text() ); - - KMenu menu; - const QAction *addAsInlineImageAction = menu.addAction( i18n("Add as &Inline Image") ); - /*const QAction *addAsAttachmentAction = */menu.addAction( i18n("Add as &Attachment") ); - const QAction *selectedAction = menu.exec( QCursor::pos() ); - if ( selectedAction == addAsInlineImageAction ) { - // Let the textedit from kdepimlibs handle inline images - insertImage( image, fi ); - return; - } else if( !selectedAction ) { - return; - } - // else fall through - } - m_composerWin->addImageAsAttachement(imageData); - return; - } - - if ( source->hasFormat( "text/x-kmail-textsnippet" ) ) { emit insertSnippet(); return; } - - // If this is a URL list, add those files as attachments or text - const KUrl::List urlList = KUrl::List::fromMimeData( source ); - if ( !urlList.isEmpty() ) { - //Search if it's message items. - Akonadi::Item::List items; - bool allLocalURLs = true; - - foreach ( const KUrl &url, urlList ) { - if ( !url.isLocalFile() ) { - allLocalURLs = false; - } - const Akonadi::Item item = Akonadi::Item::fromUrl( url ); - if ( item.isValid() ) { - items << item; - } - } - - if ( items.isEmpty() ) { - if ( allLocalURLs ) { - foreach( const KUrl &url, urlList ) { - m_composerWin->addAttachment( url, "" ); - } - } else { - KMenu p; - const QAction *addAsTextAction = p.addAction( i18np("Add URL into Message &Text", "Add URLs into Message &Text", urlList.size() ) ); - const QAction *addAsAttachmentAction = p.addAction( i18np("Add File as &Attachment", "Add Files as &Attachment", urlList.size() ) ); - const QAction *selectedAction = p.exec( QCursor::pos() ); - - if ( selectedAction == addAsTextAction ) { - foreach( const KUrl &url, urlList ) { - textCursor().insertText(url.url() + '\n'); - } - } else if ( selectedAction == addAsAttachmentAction ) { - foreach( const KUrl &url, urlList ) { - m_composerWin->addAttachment( url, "" ); - } - } - } - return; - } else { - Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this ); - itemFetchJob->fetchScope().fetchFullPayload( true ); - itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); - connect( itemFetchJob, SIGNAL(result(KJob*)), m_composerWin, SLOT(slotFetchJob(KJob*)) ); - return; - } - } - - KPIMTextEdit::TextEdit::insertFromMimeData( source ); + + if ( !m_composerWin->insertFromMimeData( source, false ) ) + KPIMTextEdit::TextEdit::insertFromMimeData( source ); } #include "kmcomposereditor.moc" diff --git a/kmcomposewin.cpp b/kmcomposewin.cpp index 69e0b6863..90e19a711 100644 --- a/kmcomposewin.cpp +++ b/kmcomposewin.cpp @@ -2091,61 +2091,114 @@ QString KMComposeWin::smartQuote( const QString & msg ) return MessageCore::StringUtil::smartQuote( msg, MessageComposer::MessageComposerSettings::self()->lineWrapWidth() ); } -void KMComposeWin::slotPasteAsAttachment() + +bool KMComposeWin::insertFromMimeData( const QMimeData *source, bool forceAttachment ) { - const QMimeData *mimeData = QApplication::clipboard()->mimeData(); + // If this is a PNG image, either add it as an attachment or as an inline image + if ( source->hasImage() && source->hasFormat( "image/png" ) ) { + // Get the image data before showing the dialog, since that processes events which can delete + // the QMimeData object behind our back + const QByteArray imageData = source->data( "image/png" ); + if ( !forceAttachment ) { + if ( mComposerBase->editor()->textMode() == KRichTextEdit::Rich && mComposerBase->editor()->isEnableImageActions() ) { + QImage image = qvariant_cast( source->imageData() ); + QFileInfo fi( source->text() ); + + KMenu menu; + const QAction *addAsInlineImageAction = menu.addAction( i18n("Add as &Inline Image") ); + /*const QAction *addAsAttachmentAction = */menu.addAction( i18n("Add as &Attachment") ); + const QAction *selectedAction = menu.exec( QCursor::pos() ); + if ( selectedAction == addAsInlineImageAction ) { + // Let the textedit from kdepimlibs handle inline images + mComposerBase->editor()->insertImage( image, fi ); + return true; + } else if( !selectedAction ) { + return true; + } + // else fall through + } + } + // Ok, when we reached this point, the user wants to add the image as an attachment. + // Ask for the filename first. + bool ok; + const QString attName = + KInputDialog::getText( "KMail", i18n( "Name of the attachment:" ), QString(), &ok, this ); + if ( !ok ) { + return true; + } + addAttachment( attName, KMime::Headers::CEbase64, QString(), imageData, "image/png" ); + return true; + } - if( mimeData->hasUrls() ) { - // If the clipboard contains a list of URL, attach each file. - const KUrl::List urls = KUrl::List::fromMimeData( mimeData ); + // If this is a URL list, add those files as attachments or text + const KUrl::List urlList = KUrl::List::fromMimeData( source ); + if ( !urlList.isEmpty() ) { + //Search if it's message items. Akonadi::Item::List items; - foreach( const KUrl &url, urls ) { + bool allLocalURLs = true; + + foreach ( const KUrl &url, urlList ) { + if ( !url.isLocalFile() ) { + allLocalURLs = false; + } const Akonadi::Item item = Akonadi::Item::fromUrl( url ); if ( item.isValid() ) { items << item; } - if ( items.isEmpty() ) - mComposerBase->attachmentController()->addAttachment( url ); - else { - Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this ); - itemFetchJob->fetchScope().fetchFullPayload( true ); - itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); - connect( itemFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotFetchJob(KJob*)) ); - return; + } + + if ( items.isEmpty() ) { + if ( allLocalURLs || forceAttachment ) { + foreach( const KUrl &url, urlList ) { + addAttachment( url, "" ); + } + } else { + KMenu p; + const QAction *addAsTextAction = p.addAction( i18np("Add URL into Message &Text", "Add URLs into Message &Text", urlList.size() ) ); + const QAction *addAsAttachmentAction = p.addAction( i18np("Add File as &Attachment", "Add Files as &Attachment", urlList.size() ) ); + const QAction *selectedAction = p.exec( QCursor::pos() ); + + if ( selectedAction == addAsTextAction ) { + foreach( const KUrl &url, urlList ) { + mComposerBase->editor()->textCursor().insertText(url.url() + '\n'); + } + } else if ( selectedAction == addAsAttachmentAction ) { + foreach( const KUrl &url, urlList ) { + addAttachment( url, "" ); + } + } } + return true; + } else { + Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this ); + itemFetchJob->fetchScope().fetchFullPayload( true ); + itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); + connect( itemFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotFetchJob(KJob*)) ); + return true; } - } else if( mimeData->hasText() ) { + } + return false; +} + +void KMComposeWin::slotPasteAsAttachment() +{ + const QMimeData *mimeData = QApplication::clipboard()->mimeData(); + if ( insertFromMimeData( mimeData, true ) ) + return; + if( mimeData->hasText() ) { bool ok; const QString attName = KInputDialog::getText( - i18n( "Insert clipboard text as attachment" ), - i18n( "Name of the attachment:" ), - QString(), &ok, this ); + i18n( "Insert clipboard text as attachment" ), + i18n( "Name of the attachment:" ), + QString(), &ok, this ); if( ok ) { mComposerBase->addAttachment( attName, attName, "utf-8", QApplication::clipboard()->text().toUtf8(), "text/plain" ); } - } else if ( mimeData->hasImage() && mimeData->hasFormat( "image/png" ) ) { - // Get the image data before showing the dialog, since that processes events which can delete - // the QMimeData object behind our back - const QByteArray imageData = mimeData->data( "image/png" ); - addImageAsAttachement( imageData ); - } - -} - -void KMComposeWin::addImageAsAttachement(const QByteArray & imageData) -{ - // Ok, when we reached this point, the user wants to add the image as an attachment. - // Ask for the filename first. - bool ok; - const QString attName = - KInputDialog::getText( "KMail", i18n( "Name of the attachment:" ), QString(), &ok, this ); - if ( !ok ) { return; } - - addAttachment( attName, KMime::Headers::CEbase64, QString(), imageData, "image/png" ); } + void KMComposeWin::slotFetchJob(KJob*job) { if ( job->error() ) { diff --git a/kmcomposewin.h b/kmcomposewin.h index 03edcffeb..9a4e129ea 100644 --- a/kmcomposewin.h +++ b/kmcomposewin.h @@ -222,8 +222,8 @@ class KMComposeWin : public KMail::Composer * message to a known recipient. */ void setFocusToSubject(); - - void addImageAsAttachement(const QByteArray & imageData); + + bool insertFromMimeData( const QMimeData *source, bool forceAttachment = false ); private: /**