File indexing completed on 2024-12-15 04:51:47

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "notelistwidget.h"
0007 #include "attributes/notedisplayattribute.h"
0008 #include "noteutils.h"
0009 #include <KMime/KMimeMessage>
0010 
0011 using namespace NoteShared;
0012 
0013 NoteListWidget::NoteListWidget(QWidget *parent)
0014     : QListWidget(parent)
0015 {
0016     setSelectionMode(QAbstractItemView::ExtendedSelection);
0017 }
0018 
0019 NoteListWidget::~NoteListWidget() = default;
0020 
0021 void NoteListWidget::addNotes(const Akonadi::Item::List &notes)
0022 {
0023     for (const Akonadi::Item &note : notes) {
0024         if (mNotes.contains(note)) {
0025             continue;
0026         }
0027         createItem(note);
0028         mNotes.append(note);
0029     }
0030 }
0031 
0032 void NoteListWidget::removeNote(const Akonadi::Item &note)
0033 {
0034     for (int i = 0; i < count(); ++i) {
0035         if (item(i)->data(AkonadiId) == note.id()) {
0036             delete item(i);
0037             mNotes.removeAll(note);
0038             break;
0039         }
0040     }
0041 }
0042 
0043 void NoteListWidget::setNotes(const Akonadi::Item::List &notes)
0044 {
0045     clear();
0046     mNotes = notes;
0047     for (const Akonadi::Item &note : std::as_const(mNotes)) {
0048         createItem(note);
0049     }
0050 }
0051 
0052 void NoteListWidget::createItem(const Akonadi::Item &note)
0053 {
0054     auto noteMessage = note.payload<KMime::Message::Ptr>();
0055     if (!noteMessage) {
0056         return;
0057     }
0058     auto item = new QListWidgetItem(this);
0059 
0060     const KMime::Headers::Subject *const subject = noteMessage->subject(false);
0061     if (subject) {
0062         item->setText(subject->asUnicodeString());
0063     }
0064 
0065     NoteShared::NoteUtils noteUtils;
0066     item->setToolTip(noteUtils.createToolTip(note));
0067     item->setData(AkonadiId, note.id());
0068 }
0069 
0070 Akonadi::Item::List NoteListWidget::selectedNotes() const
0071 {
0072     Akonadi::Item::List lst;
0073     const auto lstItems = selectedItems();
0074     for (QListWidgetItem *item : lstItems) {
0075         const Akonadi::Item::Id akonadiId = item->data(AkonadiId).toLongLong();
0076         if (akonadiId != -1) {
0077             lst.append(Akonadi::Item(akonadiId));
0078         }
0079     }
0080     return lst;
0081 }
0082 
0083 Akonadi::Item::Id NoteListWidget::itemId(QListWidgetItem *item) const
0084 {
0085     if (item) {
0086         return item->data(AkonadiId).toLongLong();
0087     }
0088     return -1;
0089 }
0090 
0091 Akonadi::Item::Id NoteListWidget::currentItemId() const
0092 {
0093     QListWidgetItem *item = currentItem();
0094     return itemId(item);
0095 }
0096 
0097 #include "moc_notelistwidget.cpp"