Commit b69a9796 authored by Laurent Montel's avatar Laurent Montel 😁

Start to implement show plaintext when an email is only html

parent 54d16e36
......@@ -88,6 +88,7 @@ set(libmessageviewer_viewer_SRCS
viewer/messagedisplayformatattribute.cpp
viewer/urlhandlermanager.cpp
viewer/mimeparttreeview.cpp
viewer/converthtmltoplaintext.cpp
viewer/memento/cryptobodypartmemento.cpp
viewer/memento/decryptverifybodypartmemento.cpp
viewer/memento/verifydetachedbodypartmemento.cpp
......@@ -215,6 +216,7 @@ target_link_libraries(messageviewer messagecore kdepim kpgp kleo
grantleetheme
kaddressbookgrantlee
incidenceeditorsng
${Grantlee_GUI_LIBRARIES}
)
include_directories(
......
......@@ -48,6 +48,11 @@ kde4_add_unit_test( messageviewer_attachmentdirstest ${messageviewer_attachmentd
target_link_libraries( messageviewer_attachmentdirstest ${QT_QTTEST_LIBRARY} ${KDE4_KDEUI_LIBS})
set( messageviewer_converthtmltoplaintext_SRCS converthtmltoplaintexttest.cpp ../viewer/converthtmltoplaintext.cpp)
kde4_add_unit_test( messageviewer_converthtmltoplaintext ${messageviewer_converthtmltoplaintext_SRCS})
target_link_libraries( messageviewer_converthtmltoplaintext ${QT_QTTEST_LIBRARY} ${KDE4_KDEUI_LIBS} ${Grantlee_GUI_LIBRARIES})
########### viewertest_gui ###############
......
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "converthtmltoplaintexttest.h"
#include "../viewer/converthtmltoplaintext.h"
#include <qtest_kde.h>
ConvertHtmlToPlainTextTest::ConvertHtmlToPlainTextTest(QObject *parent)
: QObject(parent)
{
}
ConvertHtmlToPlainTextTest::~ConvertHtmlToPlainTextTest()
{
}
void ConvertHtmlToPlainTextTest::shouldHaveDefaultValue()
{
MessageViewer::ConvertHtmlToPlainText convert;
QVERIFY(convert.htmlString().isEmpty());
}
void ConvertHtmlToPlainTextTest::shouldReturnEmptyStringIfInputTextIsEmpty()
{
MessageViewer::ConvertHtmlToPlainText convert;
convert.setHtmlString(QString());
QVERIFY(convert.generatePlainText().isEmpty());
}
void ConvertHtmlToPlainTextTest::shouldReturnNotEmptyStringIfInputTextIsNotEmpty()
{
MessageViewer::ConvertHtmlToPlainText convert;
convert.setHtmlString(QLatin1String("foo bla"));
QVERIFY(!convert.generatePlainText().isEmpty());
}
QTEST_KDEMAIN(ConvertHtmlToPlainTextTest, NoGUI)
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CONVERTHTMLTOPLAINTEXTTEST_H
#define CONVERTHTMLTOPLAINTEXTTEST_H
#include <QObject>
class ConvertHtmlToPlainTextTest : public QObject
{
Q_OBJECT
public:
explicit ConvertHtmlToPlainTextTest(QObject *parent = 0);
~ConvertHtmlToPlainTextTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldReturnEmptyStringIfInputTextIsEmpty();
void shouldReturnNotEmptyStringIfInputTextIsNotEmpty();
};
#endif // CONVERTHTMLTOPLAINTEXTTEST_H
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "converthtmltoplaintext.h"
#include <grantlee/plaintextmarkupbuilder.h>
#include <QTextDocument>
using namespace MessageViewer;
ConvertHtmlToPlainText::ConvertHtmlToPlainText()
{
}
ConvertHtmlToPlainText::~ConvertHtmlToPlainText()
{
}
void ConvertHtmlToPlainText::setHtmlString(const QString &htmlString)
{
mHtmlString = htmlString;
}
QString ConvertHtmlToPlainText::generatePlainText()
{
if (mHtmlString.isEmpty())
return QString();
Grantlee::PlainTextMarkupBuilder *pb = new Grantlee::PlainTextMarkupBuilder();
Grantlee::MarkupDirector *pmd = new Grantlee::MarkupDirector( pb );
QTextDocument *doc = new QTextDocument(mHtmlString);
pmd->processDocument( doc );
QString plainText = pb->getResult();
delete doc;
delete pmd;
delete pb;
toCleanPlainText(plainText);
return plainText;
}
QString ConvertHtmlToPlainText::htmlString() const
{
return mHtmlString;
}
//Duplicate from kpimtextedit/textedit.h
void ConvertHtmlToPlainText::toCleanPlainText(QString &text)
{
// Remove line separators. Normal \n chars are still there, so no linebreaks get lost here
text.remove( QChar::LineSeparator );
// Get rid of embedded images, see QTextImageFormat documentation:
// "Inline images are represented by an object replacement character (0xFFFC in Unicode) "
text.remove( 0xFFFC );
// In plaintext mode, each space is non-breaking.
text.replace( QChar::Nbsp, QChar::fromLatin1( ' ' ) );
}
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CONVERTHTMLTOPLAINTEXT_H
#define CONVERTHTMLTOPLAINTEXT_H
#include <QString>
namespace MessageViewer {
class ConvertHtmlToPlainText
{
public:
ConvertHtmlToPlainText();
~ConvertHtmlToPlainText();
QString generatePlainText();
QString htmlString() const;
void setHtmlString(const QString &htmlString);
private:
void toCleanPlainText(QString &text);
QString mHtmlString;
};
}
#endif // CONVERTHTMLTOPLAINTEXT_H
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