Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

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

Extract code to grantleeprint. So now we can create unittest for it

parent 571fc448
......@@ -55,6 +55,7 @@ set(kaddressbook_printing_SRCS
printing/grantlee/contactgrantleeprintimobject.cpp
printing/grantlee/contactgrantleeprintgeoobject.cpp
printing/grantlee/contactgrantleeprintcryptoobject.cpp
printing/grantlee/grantleeprint.cpp
printing/stylepage.cpp
)
......
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grantleeprint.h"
#include "contactgrantleeprintobject.h"
#include "kaddressbookgrantlee/formatter/grantleecontactutils.h"
#include <grantlee/context.h>
#include <grantlee/engine.h>
#include <grantlee/templateloader.h>
using namespace KABPrinting;
GrantleePrint::GrantleePrint(const QString &themePath, QObject *parent)
: QObject(parent)
{
mEngine = new Grantlee::Engine;
mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
mTemplateLoader->setTemplateDirs( QStringList() << themePath );
mEngine->addTemplateLoader( mTemplateLoader );
mSelfcontainedTemplate = mEngine->loadByName( QLatin1String("theme.html") );
if ( mSelfcontainedTemplate->error() ) {
mErrorMessage = mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
}
GrantleePrint::~GrantleePrint()
{
delete mEngine;
}
QString GrantleePrint::contactsToHtml( const KABC::Addressee::List &contacts )
{
if (!mErrorMessage.isEmpty())
return mErrorMessage;
QVariantList contactsList;
QList<ContactGrantleePrintObject*> lst;
Q_FOREACH (const KABC::Addressee &address, contacts) {
ContactGrantleePrintObject *contactPrintObject = new ContactGrantleePrintObject(address);
lst.append(contactPrintObject);
contactsList << QVariant::fromValue(static_cast<QObject*>(contactPrintObject));
}
QVariantHash mapping;
QVariantHash contactI18n;
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "birthdayi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String("anniversaryi18n") );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "emailsi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "websitei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "blogUrli18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "addressBookNamei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "notei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "departmenti18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "Professioni18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "officei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "manageri18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "assistanti18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "spousei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "imAddressi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "latitudei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "longitudei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "organizationi18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "titlei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "nextcontacti18n" ));
mapping.insert( QLatin1String("contacti18n"), contactI18n );
Grantlee::Context context( mapping );
context.insert(QLatin1String("contacts"), contactsList);
const QString content = mSelfcontainedTemplate->render( &context );
qDeleteAll(lst);
return content;
}
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRANTLEEPRINT_H
#define GRANTLEEPRINT_H
#include <QObject>
#include <grantlee/templateloader.h>
#include <KABC/Addressee>
namespace Grantlee {
class Engine;
}
namespace KABPrinting {
class GrantleePrint : public QObject
{
Q_OBJECT
public:
explicit GrantleePrint(const QString &themePath, QObject *parent = 0);
~GrantleePrint();
QString contactsToHtml( const KABC::Addressee::List &contacts );
private:
QString mErrorMessage;
Grantlee::Engine *mEngine;
Grantlee::FileSystemTemplateLoader::Ptr mTemplateLoader;
Grantlee::Template mSelfcontainedTemplate;
};
}
#endif // GRANTLEEPRINT_H
......@@ -20,13 +20,7 @@
#include "printingwizard.h"
#include "printprogress.h"
#include "printstyle.h"
#include "contactgrantleeprintobject.h"
#include "kaddressbookgrantlee/formatter/grantleecontactutils.h"
#include <grantlee/context.h>
#include <grantlee/engine.h>
#include <grantlee/templateloader.h>
#include "grantleeprint.h"
#include <KABC/Addressee>
......@@ -41,72 +35,19 @@
using namespace KABPrinting;
QString GrantleePrintStyle::contactsToHtml( const KABC::Addressee::List &contacts )
{
if (!mErrorMessage.isEmpty())
return mErrorMessage;
QVariantList contactsList;
QList<ContactGrantleePrintObject*> lst;
Q_FOREACH (const KABC::Addressee &address, contacts) {
ContactGrantleePrintObject *contactPrintObject = new ContactGrantleePrintObject(address);
lst.append(contactPrintObject);
contactsList << QVariant::fromValue(static_cast<QObject*>(contactPrintObject));
}
QVariantHash mapping;
QVariantHash contactI18n;
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "birthdayi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String("anniversaryi18n") );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "emailsi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "websitei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "blogUrli18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "addressBookNamei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "notei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "departmenti18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "Professioni18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "officei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "manageri18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "assistanti18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "spousei18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "imAddressi18n" ) );
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "latitudei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "longitudei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "organizationi18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "titlei18n" ));
GrantleeContactUtils::insertVariableToQVariantHash( contactI18n, QLatin1String( "nextcontacti18n" ));
mapping.insert( QLatin1String("contacti18n"), contactI18n );
Grantlee::Context context( mapping );
context.insert(QLatin1String("contacts"), contactsList);
const QString content = mSelfcontainedTemplate->render( &context );
qDeleteAll(lst);
return content;
}
GrantleePrintStyle::GrantleePrintStyle( const QString &themePath, PrintingWizard *parent )
: PrintStyle( parent )
{
mEngine = new Grantlee::Engine;
mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
mGrantleePrint = new GrantleePrint(themePath, this);
QFile previewFile(QString(themePath + QDir::separator() + QLatin1String("preview.png")));
if (previewFile.exists()) {
setPreview( previewFile.fileName() );
}
mTemplateLoader->setTemplateDirs( QStringList() << themePath );
mEngine->addTemplateLoader( mTemplateLoader );
mSelfcontainedTemplate = mEngine->loadByName( QLatin1String("theme.html") );
if ( mSelfcontainedTemplate->error() ) {
mErrorMessage = mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
setPreferredSortOptions( ContactFields::FormattedName, Qt::AscendingOrder );
}
GrantleePrintStyle::~GrantleePrintStyle()
{
delete mEngine;
}
void GrantleePrintStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
......@@ -116,7 +57,7 @@ void GrantleePrintStyle::print( const KABC::Addressee::List &contacts, PrintProg
progress->addMessage( i18n( "Setting up document" ) );
const QString html = contactsToHtml( contacts );
const QString html = mGrantleePrint->contactsToHtml( contacts );
QTextDocument document;
document.setHtml( html );
......
......@@ -19,15 +19,12 @@
#define GRANTLEEPRINTSTYLE_H
#include "printstyle.h"
#include <grantlee/templateloader.h>
namespace Grantlee {
class Engine;
}
namespace KABPrinting {
class PrintProgress;
class GrantleePrint;
class GrantleePrintStyle : public PrintStyle
{
......@@ -39,11 +36,7 @@ public:
void print( const KABC::Addressee::List &, PrintProgress * );
private:
QString contactsToHtml( const KABC::Addressee::List &contacts );
QString mErrorMessage;
Grantlee::Engine *mEngine;
Grantlee::FileSystemTemplateLoader::Ptr mTemplateLoader;
Grantlee::Template mSelfcontainedTemplate;
GrantleePrint *mGrantleePrint;
};
class GrantleeStyleFactory : public PrintStyleFactory
......
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