File indexing completed on 2024-05-05 05:21:06

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "notesagentnotedialog.h"
0007 #include "attributes/notedisplayattribute.h"
0008 #include "notesagent_debug.h"
0009 #include <Akonadi/ItemFetchJob>
0010 #include <Akonadi/ItemFetchScope>
0011 #include <TextCustomEditor/RichTextEditor>
0012 #include <TextCustomEditor/RichTextEditorWidget>
0013 
0014 #include <KSharedConfig>
0015 
0016 #include <KMime/KMimeMessage>
0017 
0018 #include <QIcon>
0019 
0020 #include <KConfigGroup>
0021 #include <KWindowConfig>
0022 #include <QDialogButtonBox>
0023 #include <QLineEdit>
0024 #include <QPushButton>
0025 #include <QVBoxLayout>
0026 #include <QWindow>
0027 
0028 namespace
0029 {
0030 static const char myNotesAgentNoteDialogName[] = "NotesAgentNoteDialog";
0031 }
0032 NotesAgentNoteDialog::NotesAgentNoteDialog(QWidget *parent)
0033     : QDialog(parent)
0034     , mNote(new TextCustomEditor::RichTextEditorWidget(this))
0035     , mSubject(new QLineEdit(this))
0036 {
0037     auto mainLayout = new QVBoxLayout(this);
0038     setAttribute(Qt::WA_DeleteOnClose);
0039     setWindowIcon(QIcon::fromTheme(QStringLiteral("knotes")));
0040 
0041     mSubject->setReadOnly(true);
0042     mainLayout->addWidget(mSubject);
0043 
0044     mNote->setReadOnly(true);
0045 
0046     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0047     connect(buttonBox, &QDialogButtonBox::rejected, this, &NotesAgentNoteDialog::reject);
0048 
0049     mainLayout->addWidget(mNote);
0050     mainLayout->addWidget(buttonBox);
0051     readConfig();
0052 }
0053 
0054 NotesAgentNoteDialog::~NotesAgentNoteDialog()
0055 {
0056     writeConfig();
0057 }
0058 
0059 void NotesAgentNoteDialog::setNoteId(Akonadi::Item::Id id)
0060 {
0061     Akonadi::Item item(id);
0062     auto job = new Akonadi::ItemFetchJob(item, this);
0063     job->fetchScope().fetchFullPayload(true);
0064     job->fetchScope().fetchAttribute<NoteShared::NoteDisplayAttribute>();
0065     connect(job, &Akonadi::ItemFetchJob::result, this, &NotesAgentNoteDialog::slotFetchItem);
0066 }
0067 
0068 void NotesAgentNoteDialog::slotFetchItem(KJob *job)
0069 {
0070     if (job->error()) {
0071         qCDebug(NOTESAGENT_LOG) << "fetch item failed " << job->errorString();
0072         return;
0073     }
0074     auto itemFetchJob = static_cast<Akonadi::ItemFetchJob *>(job);
0075     const Akonadi::Item::List lstItem = itemFetchJob->items();
0076     if (!lstItem.isEmpty()) {
0077         const Akonadi::Item item = lstItem.first();
0078         auto noteMessage = item.payload<KMime::Message::Ptr>();
0079         if (noteMessage) {
0080             const KMime::Headers::Subject *const subject = noteMessage->subject(false);
0081             if (subject) {
0082                 mSubject->setText(subject->asUnicodeString());
0083             }
0084             if (noteMessage->contentType()->isHTMLText()) {
0085                 mNote->setAcceptRichText(true);
0086                 mNote->setHtml(noteMessage->mainBodyPart()->decodedText());
0087             } else {
0088                 mNote->setAcceptRichText(false);
0089                 mNote->setPlainText(noteMessage->mainBodyPart()->decodedText());
0090             }
0091         }
0092         if (item.hasAttribute<NoteShared::NoteDisplayAttribute>()) {
0093             const auto attr = item.attribute<NoteShared::NoteDisplayAttribute>();
0094             if (attr) {
0095                 mNote->editor()->setTextColor(attr->backgroundColor());
0096                 // TODO add background color.
0097             }
0098         }
0099     }
0100 }
0101 
0102 void NotesAgentNoteDialog::readConfig()
0103 {
0104     create(); // ensure a window is created
0105     windowHandle()->resize(QSize(300, 200));
0106     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myNotesAgentNoteDialogName));
0107     KWindowConfig::restoreWindowSize(windowHandle(), group);
0108     resize(windowHandle()->size()); // workaround for QTBUG-40584
0109 }
0110 
0111 void NotesAgentNoteDialog::writeConfig()
0112 {
0113     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myNotesAgentNoteDialogName));
0114     KWindowConfig::saveWindowSize(windowHandle(), group);
0115     group.sync();
0116 }
0117 
0118 #include "moc_notesagentnotedialog.cpp"