File indexing completed on 2025-01-05 04:54:23

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "knoteprintobject.h"
0008 #include "attributes/notealarmattribute.h"
0009 #include "attributes/notedisplayattribute.h"
0010 #include "attributes/notelockattribute.h"
0011 
0012 #include <KMime/KMimeMessage>
0013 
0014 #include <QLocale>
0015 
0016 KNotePrintObject::KNotePrintObject(const Akonadi::Item &item, QObject *parent)
0017     : QObject(parent)
0018     , mItem(item)
0019 {
0020 }
0021 
0022 KNotePrintObject::~KNotePrintObject() = default;
0023 
0024 QString KNotePrintObject::description() const
0025 {
0026     auto noteMessage = mItem.payload<KMime::Message::Ptr>();
0027     if (noteMessage->contentType()->isHTMLText()) {
0028         return noteMessage->mainBodyPart()->decodedText();
0029     } else {
0030         return noteMessage->mainBodyPart()->decodedText().replace(QLatin1Char('\n'), QStringLiteral("<br>"));
0031     }
0032 }
0033 
0034 QString KNotePrintObject::name() const
0035 {
0036     auto noteMessage = mItem.payload<KMime::Message::Ptr>();
0037     const KMime::Headers::Subject *const subject = noteMessage ? noteMessage->subject(false) : nullptr;
0038     return subject ? subject->asUnicodeString() : QString();
0039 }
0040 
0041 QString KNotePrintObject::currentDateTime() const
0042 {
0043     const QDateTime now = QDateTime::currentDateTime();
0044     return QLocale().toString((now), QLocale::ShortFormat);
0045 }
0046 
0047 bool KNotePrintObject::hasAlarm() const
0048 {
0049     return mItem.hasAttribute<NoteShared::NoteAlarmAttribute>();
0050 }
0051 
0052 QString KNotePrintObject::alarm() const
0053 {
0054     const auto attr = mItem.attribute<NoteShared::NoteAlarmAttribute>();
0055     if (attr) {
0056         return QLocale().toString(attr->dateTime(), QLocale::LongFormat);
0057     }
0058     return {};
0059 }
0060 
0061 bool KNotePrintObject::isLock() const
0062 {
0063     return mItem.hasAttribute<NoteShared::NoteLockAttribute>();
0064 }
0065 
0066 QString KNotePrintObject::backgroundColorName() const
0067 {
0068     if (mItem.hasAttribute<NoteShared::NoteDisplayAttribute>()) {
0069         return mItem.attribute<NoteShared::NoteDisplayAttribute>()->backgroundColor().name();
0070     }
0071     return {};
0072 }
0073 
0074 #include "moc_knoteprintobject.cpp"