Pimcommon

annotationdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Thomas McGuire <[email protected]>
3  SPDX-FileCopyrightText: 2014-2022 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6 */
7 
8 #include "annotationdialog.h"
9 #include <KPIMTextEdit/PlainTextEditorWidget>
10 
11 #include <Akonadi/EntityAnnotationsAttribute>
12 #include <Akonadi/Item>
13 #include <Akonadi/ItemModifyJob>
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 #include <KSharedConfig>
17 #include <KStandardGuiItem>
18 
19 #include <QComboBox>
20 #include <QIcon>
21 
22 #include <KConfigGroup>
23 #include <QDialogButtonBox>
24 #include <QLabel>
25 #include <QPushButton>
26 #include <QVBoxLayout>
27 
28 using namespace PimCommon;
29 
30 class Q_DECL_HIDDEN AnnotationEditDialog::AnnotationEditDialogPrivate
31 {
32 public:
33  AnnotationEditDialogPrivate() = default;
34 
35  Akonadi::Item mItem;
36  KPIMTextEdit::PlainTextEditorWidget *mTextEdit = nullptr;
37  QComboBox *mNoteType = nullptr;
38  bool mHasAnnotation = false;
39 };
40 
42  : QDialog(parent)
43  , d(new AnnotationEditDialogPrivate)
44 {
45  d->mItem = item;
46  // check for correct key?
47  d->mHasAnnotation = item.hasAttribute<Akonadi::EntityAnnotationsAttribute>();
49  auto mainWidget = new QWidget(this);
50  auto mainLayout = new QVBoxLayout(this);
51  mainLayout->addWidget(mainWidget);
52  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
53  okButton->setDefault(true);
54  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
55  connect(buttonBox, &QDialogButtonBox::accepted, this, &AnnotationEditDialog::slotAccepted);
57 
58  if (d->mHasAnnotation) {
59  setWindowTitle(i18nc("@title:window", "Edit Note"));
60  auto user1Button = new QPushButton;
61  buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
62  user1Button->setText(i18nc("@action:button", "Delete Note"));
63  user1Button->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
64  connect(user1Button, &QPushButton::clicked, this, &AnnotationEditDialog::slotDeleteNote);
65  } else {
66  setWindowTitle(i18nc("@title:window", "Add Note"));
67  }
68 
69  buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
70 
71  auto label = new QLabel(i18n("Enter the text that should be stored as a note to the mail:"));
72  auto vbox = new QVBoxLayout(mainWidget);
73  vbox->setContentsMargins({});
74  d->mTextEdit = new KPIMTextEdit::PlainTextEditorWidget(this);
75  vbox->addWidget(label);
76  vbox->addWidget(d->mTextEdit);
77  d->mTextEdit->setFocus();
78 
79  auto hbox = new QHBoxLayout;
80  hbox->addStretch();
81  label = new QLabel(i18nc("@label:listbox", "Note type:"));
82  hbox->addWidget(label);
83  d->mNoteType = new QComboBox;
84  hbox->addWidget(d->mNoteType);
85  d->mNoteType->addItem(i18nc("@item:inlistbox", "Private note"), QByteArrayLiteral("/private/comment"));
86  d->mNoteType->addItem(i18nc("@item:inlistbox", "Shared note"), QByteArrayLiteral("/shared/comment"));
87 
88  vbox->addLayout(hbox);
89  if (d->mHasAnnotation && item.attribute<Akonadi::EntityAnnotationsAttribute>()) {
90  if (item.attribute<Akonadi::EntityAnnotationsAttribute>()->contains("/private/comment")) {
91  d->mNoteType->setCurrentIndex(d->mNoteType->findData(QStringLiteral("/private/comment")));
92  d->mTextEdit->setPlainText(item.attribute<Akonadi::EntityAnnotationsAttribute>()->value("/private/comment"));
93  } else {
94  d->mNoteType->setCurrentIndex(d->mNoteType->findData(QStringLiteral("/shared/comment")));
95  d->mTextEdit->setPlainText(item.attribute<Akonadi::EntityAnnotationsAttribute>()->value("/shared/comment"));
96  }
97  }
98  mainLayout->addWidget(buttonBox);
99  readConfig();
100 }
101 
103 {
104  writeConfig();
105 }
106 
107 void AnnotationEditDialog::slotAccepted()
108 {
109  bool textIsEmpty = d->mTextEdit->isEmpty();
110  if (!textIsEmpty) {
111  d->mItem.removeAttribute<Akonadi::EntityAnnotationsAttribute>();
112  auto annotation = d->mItem.attribute<Akonadi::EntityAnnotationsAttribute>(Akonadi::Item::AddIfMissing);
114  map.insert(d->mNoteType->itemData(d->mNoteType->currentIndex()).toByteArray(), d->mTextEdit->toPlainText().toUtf8());
115  annotation->setAnnotations(map);
116  d->mItem.addAttribute(annotation);
117  } else if (d->mHasAnnotation) {
118  d->mItem.removeAttribute<Akonadi::EntityAnnotationsAttribute>();
119  }
120  new Akonadi::ItemModifyJob(d->mItem);
121  accept();
122 }
123 
124 void AnnotationEditDialog::slotDeleteNote()
125 {
126  const int answer = KMessageBox::warningContinueCancel(this,
127  i18n("Do you really want to delete this note?"),
128  i18nc("@title:window", "Delete Note"),
130  if (answer == KMessageBox::Continue) {
131  d->mItem.removeAttribute<Akonadi::EntityAnnotationsAttribute>();
132  new Akonadi::ItemModifyJob(d->mItem);
133  accept();
134  }
135 }
136 
137 void AnnotationEditDialog::readConfig()
138 {
139  KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
140  KConfigGroup group(cfg, "AnnotationEditDialog");
141  QSize size = group.readEntry("Size", QSize());
142  if (!size.isEmpty()) {
143  resize(size);
144  }
145 }
146 
147 void AnnotationEditDialog::writeConfig()
148 {
149  KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
150  KConfigGroup group(cfg, "AnnotationEditDialog");
151  group.writeEntry("Size", size());
152 }
KGuiItem del()
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual void reject()
void setShortcut(const QKeySequence &key)
A dialog for editing annotations of an email.
folderdialogacltab.h
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void addStretch(int stretch)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
AnnotationEditDialog(const Akonadi::Item &item, QWidget *parent=nullptr)
Creates a new annotation edit dialog.
QString i18n(const char *text, const TYPE &arg...)
void setWindowTitle(const QString &)
~AnnotationEditDialog() override
Destroys the annotation edit dialog.
virtual void accept()
Key_Return
bool hasAttribute() const
void resize(int w, int h)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setDefault(bool)
void addItem(const QString &text, const QVariant &userData)
const T * attribute() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri May 27 2022 03:54:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.