[kde-doc-english] [trojita] src/Gui: GUI: control text/plain vs. text/html preference via PartLoadingOptions
Jan Kundrát
jkt at flaska.net
Mon Aug 19 14:54:01 UTC 2013
Git commit c43046361b75db4780a65f755d9222638a030161 by Jan Kundrát.
Committed on 19/08/2013 at 12:04.
Pushed by jkt into branch 'master'.
GUI: control text/plain vs. text/html preference via PartLoadingOptions
The work on the Kontact plugin requires a different way of accessing the user's
preferences because the QSettings's default constructor no longer works as
expected. Towards that end, Pali is working on having the QSettings access
abstracted away, and this patch makes it possible to avoid passing the
QSettings* around.
REVIEW: 112155
M +6 -1 src/Gui/MessageView.cpp
M +12 -10 src/Gui/PartWidget.cpp
M +8 -4 src/Gui/PartWidget.h
M +7 -12 src/Gui/PartWidgetFactory.cpp
M +3 -2 src/Gui/PartWidgetFactory.h
http://commits.kde.org/trojita/c43046361b75db4780a65f755d9222638a030161
diff --git a/src/Gui/MessageView.cpp b/src/Gui/MessageView.cpp
index b729a7e..660e384 100644
--- a/src/Gui/MessageView.cpp
+++ b/src/Gui/MessageView.cpp
@@ -26,6 +26,7 @@
#include <QMenu>
#include <QMessageBox>
#include <QProgressBar>
+#include <QSettings>
#include <QTimer>
#include <QUrl>
#include <QVBoxLayout>
@@ -46,6 +47,7 @@
#include "TagListWidget.h"
#include "UserAgentWebPage.h"
#include "Window.h"
+#include "Common/SettingsNames.h"
#include "Composer/SubjectMangling.h"
#include "Imap/Model/MailboxTree.h"
#include "Imap/Model/MsgListModel.h"
@@ -227,7 +229,10 @@ void MessageView::setMessage(const QModelIndex &index)
m_loadingItemCount = 0;
m_progress->hide();
- viewer = factory->create(rootPartIndex, 0);
+ PartWidgetFactory::PartLoadingOptions loadingMode;
+ if (QSettings().value(Common::SettingsNames::guiPreferPlaintextRendering, QVariant(true)).toBool())
+ loadingMode |= PartWidgetFactory::PART_PREFER_PLAINTEXT_OVER_HTML;
+ viewer = factory->create(rootPartIndex, 0, loadingMode);
viewer->setParent(this);
layout->addWidget(viewer);
viewer->show();
diff --git a/src/Gui/PartWidget.cpp b/src/Gui/PartWidget.cpp
index fc71906..3580b8e 100644
--- a/src/Gui/PartWidget.cpp
+++ b/src/Gui/PartWidget.cpp
@@ -49,11 +49,13 @@ QString quoteMeHelper(const QObjectList &children)
MultipartAlternativeWidget::MultipartAlternativeWidget(QWidget *parent,
PartWidgetFactory *factory, const QModelIndex &partIndex,
- const int recursionDepth, const QString &preferredMimeType):
+ const int recursionDepth, const PartWidgetFactory::PartLoadingOptions options):
QTabWidget(parent)
{
setContentsMargins(0,0,0,0);
int preferredIndex = -1;
+ const QString preferredMimeType = options & PartWidgetFactory::PART_PREFER_PLAINTEXT_OVER_HTML ?
+ QLatin1String("text/plain") : QLatin1String("text/html");
// First loop iteration is used to find out what MIME type to show
for (int i = 0; i < partIndex.model()->rowCount(partIndex); ++i) {
QModelIndex anotherPart = partIndex.child(i, 0);
@@ -75,8 +77,8 @@ MultipartAlternativeWidget::MultipartAlternativeWidget(QWidget *parent,
// I can live with that.
QWidget *item = factory->create(anotherPart, recursionDepth + 1,
i == preferredIndex ?
- PartWidgetFactory::PartLoadingOptions() :
- PartWidgetFactory::PartLoadingOptions() | PartWidgetFactory::PART_IS_HIDDEN);
+ options :
+ options | PartWidgetFactory::PART_IS_HIDDEN);
QString mimeType = anotherPart.data(Imap::Mailbox::RolePartMimeType).toString();
addTab(item, mimeType);
}
@@ -115,7 +117,7 @@ bool MultipartAlternativeWidget::eventFilter(QObject *o, QEvent *e)
MultipartSignedWidget::MultipartSignedWidget(QWidget *parent,
PartWidgetFactory *factory, const QModelIndex &partIndex,
- const int recursionDepth):
+ const int recursionDepth, const PartWidgetFactory::PartLoadingOptions options):
QGroupBox(tr("Signed Message"), parent)
{
setFlat(true);
@@ -127,7 +129,7 @@ MultipartSignedWidget::MultipartSignedWidget(QWidget *parent,
setTitle(tr("Malformed multipart/signed message: only one nested part"));
QModelIndex anotherPart = partIndex.child(0, 0);
Q_ASSERT(anotherPart.isValid()); // guaranteed by the MVC
- layout->addWidget(factory->create(anotherPart, recursionDepth + 1));
+ layout->addWidget(factory->create(anotherPart, recursionDepth + 1, options));
} else if (childrenCount != 2) {
QLabel *lbl = new QLabel(tr("Malformed multipart/signed message: %1 nested parts").arg(QString::number(childrenCount)), this);
layout->addWidget(lbl);
@@ -136,7 +138,7 @@ MultipartSignedWidget::MultipartSignedWidget(QWidget *parent,
Q_ASSERT(childrenCount == 2); // from the if logic; FIXME: refactor
QModelIndex anotherPart = partIndex.child(0, 0);
Q_ASSERT(anotherPart.isValid()); // guaranteed by the MVC
- layout->addWidget(factory->create(anotherPart, recursionDepth + 1));
+ layout->addWidget(factory->create(anotherPart, recursionDepth + 1, options));
}
}
@@ -147,7 +149,7 @@ QString MultipartSignedWidget::quoteMe() const
GenericMultipartWidget::GenericMultipartWidget(QWidget *parent,
PartWidgetFactory *factory, const QModelIndex &partIndex,
- int recursionDepth):
+ int recursionDepth, const PartWidgetFactory::PartLoadingOptions options):
QWidget(parent)
{
setContentsMargins(0, 0, 0, 0);
@@ -158,7 +160,7 @@ GenericMultipartWidget::GenericMultipartWidget(QWidget *parent,
using namespace Imap::Mailbox;
QModelIndex anotherPart = partIndex.child(i, 0);
Q_ASSERT(anotherPart.isValid()); // guaranteed by the MVC
- QWidget *res = factory->create(anotherPart, recursionDepth + 1);
+ QWidget *res = factory->create(anotherPart, recursionDepth + 1, options);
layout->addWidget(res);
}
}
@@ -170,7 +172,7 @@ QString GenericMultipartWidget::quoteMe() const
Message822Widget::Message822Widget(QWidget *parent,
PartWidgetFactory *factory, const QModelIndex &partIndex,
- int recursionDepth):
+ int recursionDepth, const PartWidgetFactory::PartLoadingOptions options):
QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
@@ -182,7 +184,7 @@ Message822Widget::Message822Widget(QWidget *parent,
using namespace Imap::Mailbox;
QModelIndex anotherPart = partIndex.child(i, 0);
Q_ASSERT(anotherPart.isValid()); // guaranteed by the MVC
- QWidget *res = factory->create(anotherPart, recursionDepth + 1);
+ QWidget *res = factory->create(anotherPart, recursionDepth + 1, options);
layout->addWidget(res);
}
}
diff --git a/src/Gui/PartWidget.h b/src/Gui/PartWidget.h
index e8fda9d..7aa8406 100644
--- a/src/Gui/PartWidget.h
+++ b/src/Gui/PartWidget.h
@@ -26,6 +26,7 @@
#include <QTabWidget>
#include "AbstractPartWidget.h"
+#include "PartWidgetFactory.h"
class QModelIndex;
@@ -40,7 +41,7 @@ class MultipartAlternativeWidget: public QTabWidget, public AbstractPartWidget
Q_OBJECT
public:
MultipartAlternativeWidget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex,
- const int recursionDepth, const QString &preferredMimeType);
+ const int recursionDepth, const PartWidgetFactory::PartLoadingOptions options);
virtual QString quoteMe() const;
virtual void reloadContents();
protected:
@@ -52,7 +53,8 @@ class MultipartSignedWidget: public QGroupBox, public AbstractPartWidget
{
Q_OBJECT
public:
- MultipartSignedWidget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth);
+ MultipartSignedWidget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth,
+ const PartWidgetFactory::PartLoadingOptions loadingOptions);
virtual QString quoteMe() const;
virtual void reloadContents();
};
@@ -62,7 +64,8 @@ class GenericMultipartWidget: public QWidget, public AbstractPartWidget
{
Q_OBJECT
public:
- GenericMultipartWidget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth);
+ GenericMultipartWidget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth,
+ const PartWidgetFactory::PartLoadingOptions loadingOptions);
virtual QString quoteMe() const;
virtual void reloadContents();
};
@@ -72,7 +75,8 @@ class Message822Widget: public QWidget, public AbstractPartWidget
{
Q_OBJECT
public:
- Message822Widget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth);
+ Message822Widget(QWidget *parent, PartWidgetFactory *factory, const QModelIndex &partIndex, const int recursionDepth,
+ const PartWidgetFactory::PartLoadingOptions loadingOptions);
virtual QString quoteMe() const;
virtual void reloadContents();
};
diff --git a/src/Gui/PartWidgetFactory.cpp b/src/Gui/PartWidgetFactory.cpp
index a3a1b23..8a1b867 100644
--- a/src/Gui/PartWidgetFactory.cpp
+++ b/src/Gui/PartWidgetFactory.cpp
@@ -148,17 +148,12 @@ QWidget *PartWidgetFactory::create(const QModelIndex &partIndex, int recursionDe
}
// Now we know for sure that it's not supposed to be wrapped in an AttachmentView, cool.
- bool userPrefersPlaintext = QSettings().value(Common::SettingsNames::guiPreferPlaintextRendering, QVariant(true)).toBool();
-
if (mimeType.startsWith(QLatin1String("multipart/"))) {
// it's a compound part
if (mimeType == QLatin1String("multipart/alternative")) {
- return new MultipartAlternativeWidget(0, this, partIndex, recursionDepth,
- userPrefersPlaintext ?
- QLatin1String("text/plain") :
- QLatin1String("text/html"));
+ return new MultipartAlternativeWidget(0, this, partIndex, recursionDepth, loadingMode);
} else if (mimeType == QLatin1String("multipart/signed")) {
- return new MultipartSignedWidget(0, this, partIndex, recursionDepth);
+ return new MultipartSignedWidget(0, this, partIndex, recursionDepth, loadingMode);
} else if (mimeType == QLatin1String("multipart/related")) {
// The purpose of this section is to find a text/html e-mail, along with its associated body parts, and hide
// everything else than the HTML widget.
@@ -196,24 +191,24 @@ QWidget *PartWidgetFactory::create(const QModelIndex &partIndex, int recursionDe
if (mainPartIndex.isValid()) {
if (mainPartIndex.data(RolePartMimeType).toString() == QLatin1String("text/html")) {
- return PartWidgetFactory::create(mainPartIndex, recursionDepth+1);
+ return PartWidgetFactory::create(mainPartIndex, recursionDepth+1, loadingMode);
} else {
// Sorry, but anything else than text/html is by definition suspicious here. Better than picking some random
// choice, let's just show everything.
- return new GenericMultipartWidget(0, this, partIndex, recursionDepth);
+ return new GenericMultipartWidget(0, this, partIndex, recursionDepth, loadingMode);
}
} else {
// The RFC2387's wording is clear that in absence of an explicit START argument, the first part is the starting one.
// On the other hand, I've seen real-world messages whose first part is some utter garbage (an image sent as
// application/octet-stream, for example) and some *other* part is an HTML text. In that case (and if we somehow
// failed to pick the HTML part by a heuristic), it's better to show everything.
- return new GenericMultipartWidget(0, this, partIndex, recursionDepth);
+ return new GenericMultipartWidget(0, this, partIndex, recursionDepth, loadingMode);
}
} else {
- return new GenericMultipartWidget(0, this, partIndex, recursionDepth);
+ return new GenericMultipartWidget(0, this, partIndex, recursionDepth, loadingMode);
}
} else if (mimeType == QLatin1String("message/rfc822")) {
- return new Message822Widget(0, this, partIndex, recursionDepth);
+ return new Message822Widget(0, this, partIndex, recursionDepth, loadingMode);
} else {
part->fetchFromCache(model);
diff --git a/src/Gui/PartWidgetFactory.h b/src/Gui/PartWidgetFactory.h
index 261a98d..dc41d9c 100644
--- a/src/Gui/PartWidgetFactory.h
+++ b/src/Gui/PartWidgetFactory.h
@@ -43,12 +43,13 @@ public:
PART_IGNORE_DISPOSITION_ATTACHMENT = 1 << 0, /**< @short Don't wrap the part in an AttachmentView */
PART_IGNORE_CLICKTHROUGH = 1 << 1, /**< @short Ignore any heuristics which lead to wrapping in an LoadablePartWidget with a clickthrough */
PART_IGNORE_LOAD_ON_SHOW = 1 << 2, /**< @short Ignore wrapping in a LoadablePartWidget set up to load on first show event */
- PART_IS_HIDDEN = 1 << 3 /**< @short Request wrapping this part in a LoadablePartWidget */
+ PART_IS_HIDDEN = 1 << 3, /**< @short Request wrapping this part in a LoadablePartWidget */
+ PART_PREFER_PLAINTEXT_OVER_HTML = 1 << 4 /**< @short The user's preferences indicate that a text/plain part shall be shown instead of a text/html if available */
} PartLoadingFlag;
typedef QFlags<PartLoadingFlag> PartLoadingOptions;
PartWidgetFactory(Imap::Network::MsgPartNetAccessManager *manager, MessageView *messageView);
- QWidget *create(const QModelIndex &partIndex, int recursionDepth, const PartLoadingOptions loadingMode = PartLoadingOptions());
+ QWidget *create(const QModelIndex &partIndex, int recursionDepth, const PartLoadingOptions loadingMode);
MessageView *messageView() const;
private:
More information about the kde-doc-english
mailing list