File indexing completed on 2024-05-12 05:13:28

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "contactprintthemepreview.h"
0008 #include "contactprintthemeeditor_debug.h"
0009 #include "contactprintthemeeditorutil.h"
0010 #include <Akonadi/GrantleePrint>
0011 #include <KConfigGroup>
0012 #include <KContacts/VCardConverter>
0013 #include <KSharedConfig>
0014 #include <QHBoxLayout>
0015 #include <QWebEngineView>
0016 
0017 ContactPrintThemePreview::ContactPrintThemePreview(const QString &projectDirectory, QWidget *parent)
0018     : GrantleeThemeEditor::PreviewWidget(parent)
0019     , mThemePath(projectDirectory)
0020 {
0021     auto hbox = new QHBoxLayout(this);
0022     hbox->setContentsMargins(0, 0, 0, 0);
0023     mViewer = new QWebEngineView(this);
0024     mViewer->setContextMenuPolicy(Qt::NoContextMenu);
0025     hbox->addWidget(mViewer);
0026     loadConfig();
0027 }
0028 
0029 ContactPrintThemePreview::~ContactPrintThemePreview() = default;
0030 
0031 void ContactPrintThemePreview::updateViewer()
0032 {
0033     KContacts::AddresseeList lst;
0034     lst << mContact;
0035     KAddressBookGrantlee::GrantleePrint grantleePrint(mThemePath);
0036     grantleePrint.setApplicationDomain("kaddressbook");
0037     const QString html = grantleePrint.contactsToHtml(lst);
0038     mViewer->setHtml(html);
0039 }
0040 
0041 void ContactPrintThemePreview::createScreenShot(const QStringList &fileName)
0042 {
0043     Q_UNUSED(fileName)
0044     qCWarning(CONTACTPRINTTHEMEEDITOR_LOG) << "Create screenshot not implemented yet";
0045 }
0046 
0047 void ContactPrintThemePreview::setThemePath(const QString &projectDirectory, const QString &mainPageFileName)
0048 {
0049     Q_UNUSED(mainPageFileName)
0050     mThemePath = projectDirectory;
0051 }
0052 
0053 void ContactPrintThemePreview::loadConfig()
0054 {
0055     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0056 
0057     if (config->hasGroup(QStringLiteral("Global"))) {
0058         KConfigGroup group = config->group(QStringLiteral("Global"));
0059         ContactPrintThemeEditorutil contactEditorUtil;
0060         const QString defaultContact = group.readEntry("defaultContact", contactEditorUtil.defaultContact());
0061         if (!defaultContact.isEmpty()) {
0062             KContacts::VCardConverter converter;
0063             mContact = converter.parseVCard(defaultContact.toUtf8());
0064         } else {
0065             mContact = KContacts::Addressee();
0066         }
0067     } else {
0068         ContactPrintThemeEditorutil contactEditorUtil;
0069         if (!contactEditorUtil.defaultContact().isEmpty()) {
0070             KContacts::VCardConverter converter;
0071             mContact = converter.parseVCard(contactEditorUtil.defaultContact().toUtf8());
0072         } else {
0073             mContact = KContacts::Addressee();
0074         }
0075     }
0076 }
0077 
0078 #include "moc_contactprintthemepreview.cpp"