Mailcommon

snippetsmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3  SPDX-FileContributor: Tobias Koenig <[email protected]>
4 
5  SPDX-FileCopyrightText: 2019-2023 Laurent Montel <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "snippetsmodel.h"
11 
12 #include <KConfigGroup>
13 #include <KLocalizedString>
14 #include <KMessageBox>
15 #include <KSharedConfig>
16 #include <QDataStream>
17 #include <QIODevice>
18 #include <QMimeData>
19 #include <QStringList>
20 
21 using namespace MailCommon;
22 
23 class MailCommon::SnippetItem
24 {
25 public:
26  explicit SnippetItem(bool isGroup = false, SnippetItem *parent = nullptr);
27  ~SnippetItem();
28 
29  Q_REQUIRED_RESULT bool isGroup() const;
30 
31  void setName(const QString &name);
32  Q_REQUIRED_RESULT QString name() const;
33 
34  void setText(const QString &text);
35  Q_REQUIRED_RESULT QString text() const;
36 
37  void setKeyword(const QString &text);
38  Q_REQUIRED_RESULT QString keyword() const;
39 
40  void setKeySequence(const QString &sequence);
41  Q_REQUIRED_RESULT QString keySequence() const;
42 
43  void appendChild(SnippetItem *child);
44  void removeChild(SnippetItem *child);
45  Q_REQUIRED_RESULT SnippetItem *child(int row) const;
46  Q_REQUIRED_RESULT int childCount() const;
47  Q_REQUIRED_RESULT int row() const;
48  Q_REQUIRED_RESULT SnippetItem *parent() const;
49 
50  Q_REQUIRED_RESULT QString subject() const;
51  void setSubject(const QString &subject);
52 
53  Q_REQUIRED_RESULT QString to() const;
54  void setTo(const QString &to);
55 
56  Q_REQUIRED_RESULT QString cc() const;
57  void setCc(const QString &cc);
58 
59  Q_REQUIRED_RESULT QString bcc() const;
60  void setBcc(const QString &bcc);
61 
62  Q_REQUIRED_RESULT QString attachment() const;
63  void setAttachment(const QString &attachment);
64 
65 private:
66  QList<SnippetItem *> mChildItems;
67  SnippetItem *mParentItem = nullptr;
68 
69  bool mIsGroup = false;
70  QString mName;
71  QString mText;
72  QString mKeySequence;
73  QString mKeyword;
74  QString mSubject;
75  QString mTo;
76  QString mCc;
77  QString mBcc;
78  QString mAttachment;
79 };
80 
81 SnippetItem::SnippetItem(bool isGroup, SnippetItem *parent)
82  : mParentItem(parent)
83  , mIsGroup(isGroup)
84 {
85 }
86 
87 SnippetItem::~SnippetItem()
88 {
89  qDeleteAll(mChildItems);
90  mChildItems.clear();
91 }
92 
93 bool SnippetItem::isGroup() const
94 {
95  return mIsGroup;
96 }
97 
98 void SnippetItem::setName(const QString &name)
99 {
100  mName = name;
101 }
102 
103 QString SnippetItem::name() const
104 {
105  return mName;
106 }
107 
108 void SnippetItem::setText(const QString &text)
109 {
110  mText = text;
111 }
112 
113 QString SnippetItem::text() const
114 {
115  return mText;
116 }
117 
118 void SnippetItem::setKeyword(const QString &text)
119 {
120  mKeyword = text;
121 }
122 
123 QString SnippetItem::keyword() const
124 {
125  return mKeyword;
126 }
127 
128 void SnippetItem::setKeySequence(const QString &sequence)
129 {
130  mKeySequence = sequence;
131 }
132 
133 QString SnippetItem::keySequence() const
134 {
135  return mKeySequence;
136 }
137 
138 void SnippetItem::appendChild(SnippetItem *item)
139 {
140  mChildItems.append(item);
141 }
142 
143 void SnippetItem::removeChild(SnippetItem *item)
144 {
145  mChildItems.removeAll(item);
146  delete item;
147 }
148 
149 SnippetItem *SnippetItem::child(int row) const
150 {
151  return mChildItems.value(row);
152 }
153 
154 int SnippetItem::childCount() const
155 {
156  return mChildItems.count();
157 }
158 
159 SnippetItem *SnippetItem::parent() const
160 {
161  return mParentItem;
162 }
163 
164 QString SnippetItem::subject() const
165 {
166  return mSubject;
167 }
168 
169 void SnippetItem::setSubject(const QString &subject)
170 {
171  mSubject = subject;
172 }
173 
174 QString SnippetItem::to() const
175 {
176  return mTo;
177 }
178 
179 void SnippetItem::setTo(const QString &to)
180 {
181  mTo = to;
182 }
183 
184 QString SnippetItem::cc() const
185 {
186  return mCc;
187 }
188 
189 void SnippetItem::setCc(const QString &cc)
190 {
191  mCc = cc;
192 }
193 
194 QString SnippetItem::bcc() const
195 {
196  return mBcc;
197 }
198 
199 void SnippetItem::setBcc(const QString &bcc)
200 {
201  mBcc = bcc;
202 }
203 
204 QString SnippetItem::attachment() const
205 {
206  return mAttachment;
207 }
208 
209 void SnippetItem::setAttachment(const QString &attachment)
210 {
211  mAttachment = attachment;
212 }
213 
214 int SnippetItem::row() const
215 {
216  if (mParentItem) {
217  return mParentItem->mChildItems.indexOf(const_cast<SnippetItem *>(this));
218  }
219 
220  return 0;
221 }
222 
223 SnippetsModel *SnippetsModel::instance()
224 {
225  static SnippetsModel s_self;
226  return &s_self;
227 }
228 
229 SnippetsModel::SnippetsModel(QObject *parent)
230  : QAbstractItemModel(parent)
231 {
232  mRootItem = new SnippetItem(true);
233  load();
234 }
235 
236 SnippetsModel::~SnippetsModel()
237 {
238  delete mRootItem;
239 }
240 
241 int SnippetsModel::columnCount(const QModelIndex &) const
242 {
243  return 1;
244 }
245 
246 bool SnippetsModel::setData(const QModelIndex &index, const QVariant &value, int role)
247 {
248  if (!index.isValid()) {
249  return false;
250  }
251 
252  auto item = static_cast<SnippetItem *>(index.internalPointer());
253  Q_ASSERT(item);
254 
255  switch (role) {
256  case NameRole:
257  item->setName(value.toString());
258  Q_EMIT dataChanged(index, index);
259  return true;
260  case TextRole:
261  item->setText(value.toString());
262  Q_EMIT dataChanged(index, index);
263  return true;
264  case KeySequenceRole:
265  item->setKeySequence(value.toString());
266  Q_EMIT dataChanged(index, index);
267  return true;
268  case KeywordRole:
269  item->setKeyword(value.toString());
270  Q_EMIT dataChanged(index, index);
271  return true;
272  case SubjectRole:
273  item->setSubject(value.toString());
274  Q_EMIT dataChanged(index, index);
275  return true;
276  case ToRole:
277  item->setTo(value.toString());
278  Q_EMIT dataChanged(index, index);
279  return true;
280  case CcRole:
281  item->setCc(value.toString());
282  Q_EMIT dataChanged(index, index);
283  return true;
284  case BccRole:
285  item->setBcc(value.toString());
286  Q_EMIT dataChanged(index, index);
287  return true;
288  case AttachmentRole:
289  item->setAttachment(value.toString());
290  Q_EMIT dataChanged(index, index);
291  return true;
292  }
293 
294  return false;
295 }
296 
297 QVariant SnippetsModel::data(const QModelIndex &index, int role) const
298 {
299  if (!index.isValid()) {
300  return {};
301  }
302 
303  auto item = static_cast<SnippetItem *>(index.internalPointer());
304 
305  switch (role) {
306  case Qt::DisplayRole:
307  return item->name();
308  case IsGroupRole:
309  return item->isGroup();
310  case NameRole:
311  return item->name();
312  case TextRole:
313  return item->text();
314  case KeySequenceRole:
315  return item->keySequence();
316  case KeywordRole:
317  return item->keyword();
318  case SubjectRole:
319  return item->subject();
320  case ToRole:
321  return item->to();
322  case CcRole:
323  return item->cc();
324  case BccRole:
325  return item->bcc();
326  case AttachmentRole:
327  return item->attachment();
328  }
329 
330  return {};
331 }
332 
333 Qt::ItemFlags SnippetsModel::flags(const QModelIndex &index) const
334 {
335  Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
336 
337  if (index.isValid()) {
338  const SnippetItem *item = static_cast<SnippetItem *>(index.internalPointer());
339  if (!item->isGroup()) {
340  return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
341  }
342  }
343 
344  return Qt::ItemIsDropEnabled | defaultFlags;
345 }
346 
347 QModelIndex SnippetsModel::index(int row, int column, const QModelIndex &parent) const
348 {
349  if (!hasIndex(row, column, parent)) {
350  return {};
351  }
352 
353  SnippetItem *parentItem = nullptr;
354 
355  if (!parent.isValid()) {
356  parentItem = mRootItem;
357  } else {
358  parentItem = static_cast<SnippetItem *>(parent.internalPointer());
359  }
360 
361  SnippetItem *childItem = parentItem->child(row);
362  if (childItem) {
363  return createIndex(row, column, childItem);
364  } else {
365  return {};
366  }
367 }
368 
370 {
371  if (!index.isValid()) {
372  return {};
373  }
374 
375  auto childItem = static_cast<SnippetItem *>(index.internalPointer());
376  SnippetItem *parentItem = childItem->parent();
377 
378  if (parentItem == mRootItem) {
379  return {};
380  }
381 
382  return createIndex(parentItem->row(), 0, parentItem);
383 }
384 
385 int SnippetsModel::rowCount(const QModelIndex &parent) const
386 {
387  SnippetItem *parentItem = nullptr;
388  if (parent.column() > 0) {
389  return 0;
390  }
391 
392  if (!parent.isValid()) {
393  parentItem = mRootItem;
394  } else {
395  parentItem = static_cast<SnippetItem *>(parent.internalPointer());
396  }
397 
398  return parentItem->childCount();
399 }
400 
401 bool SnippetsModel::insertRows(int row, int count, const QModelIndex &parent)
402 {
403  SnippetItem *parentItem = nullptr;
404 
405  if (!parent.isValid()) {
406  parentItem = mRootItem;
407  } else {
408  parentItem = static_cast<SnippetItem *>(parent.internalPointer());
409  }
410 
411  beginInsertRows(parent, row, row + count - 1);
412  for (int i = 0; i < count; ++i) {
413  auto snippet = new SnippetItem(!parent.isValid(), parentItem);
414  parentItem->appendChild(snippet);
415  }
416  endInsertRows();
417 
418  return true;
419 }
420 
421 bool SnippetsModel::removeRows(int row, int count, const QModelIndex &parent)
422 {
423  SnippetItem *parentItem = nullptr;
424 
425  if (!parent.isValid()) {
426  parentItem = mRootItem;
427  } else {
428  parentItem = static_cast<SnippetItem *>(parent.internalPointer());
429  }
430 
431  beginRemoveRows(parent, row, row + count - 1);
432  for (int i = 0; i < count; ++i) {
433  parentItem->removeChild(parentItem->child(row));
434  }
435  endRemoveRows();
436 
437  return true;
438 }
439 
440 QStringList SnippetsModel::mimeTypes() const
441 {
442  return QStringList() << QStringLiteral("text/x-kmail-textsnippet") << QStringLiteral("text/plain");
443 }
444 
445 QMimeData *SnippetsModel::mimeData(const QModelIndexList &indexes) const
446 {
447  if (indexes.isEmpty()) {
448  return nullptr;
449  }
450 
451  const QModelIndex index = indexes.first();
452 
453  auto item = static_cast<SnippetItem *>(index.internalPointer());
454  if (item->isGroup()) {
455  return nullptr;
456  }
457 
458  auto mimeData = new QMimeData();
459 
460  QByteArray encodedData;
461  QDataStream stream(&encodedData, QIODevice::WriteOnly);
462  stream << index.parent().internalId() << item->name() << item->text() << item->keySequence() << item->keyword() << item->subject() << item->to()
463  << item->cc() << item->bcc() << item->attachment();
464 
465  mimeData->setData(QStringLiteral("text/x-kmail-textsnippet"), encodedData);
466  mimeData->setText(item->text());
467 
468  return mimeData;
469 }
470 
471 bool SnippetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
472 {
473  Q_UNUSED(row)
474 
475  if (action == Qt::IgnoreAction) {
476  return true;
477  }
478 
479  if (data->hasFormat(QStringLiteral("text/x-kmail-textsnippet"))) {
480  if (!parent.isValid()) {
481  return false;
482  }
483 
484  if (column > 1) {
485  return false;
486  }
487 
488  auto item = static_cast<SnippetItem *>(parent.internalPointer());
489 
490  QByteArray encodedData = data->data(QStringLiteral("text/x-kmail-textsnippet"));
491  QDataStream stream(&encodedData, QIODevice::ReadOnly);
492 
493  quintptr id;
494  QString name;
495  QString text;
497  QString keyword;
498  QString subject;
499  QString to;
500  QString cc;
501  QString bcc;
502  QString attachment;
503  stream >> id >> name >> text >> keySequence >> keyword >> subject >> to >> cc >> bcc >> attachment;
504  if (parent.internalId() == id) {
505  return false;
506  }
507  if (item->isGroup()) {
508  insertRow(rowCount(parent), parent);
509 
510  const QModelIndex idx = index(rowCount(parent) - 1, 0, parent);
511 
512  setData(idx, name, SnippetsModel::NameRole);
513  setData(idx, text, SnippetsModel::TextRole);
514  setData(idx, keySequence, SnippetsModel::KeySequenceRole);
515  setData(idx, keyword, SnippetsModel::KeywordRole);
516  setData(idx, subject, SnippetsModel::SubjectRole);
517  setData(idx, to, SnippetsModel::ToRole);
518  setData(idx, cc, SnippetsModel::CcRole);
519  setData(idx, bcc, SnippetsModel::BccRole);
520  setData(idx, attachment, SnippetsModel::AttachmentRole);
521  Q_EMIT dndDone();
522  return true;
523  } else {
524  if (KMessageBox::ButtonCode::PrimaryAction
526  i18n("Do you want to update snippet?"),
527  i18n("Update snippet"),
528  KGuiItem(i18n("Update")),
530  item->setText(text);
531  item->setSubject(subject);
532  item->setTo(to);
533  item->setCc(cc);
534  item->setBcc(bcc);
535  return true;
536  }
537  return false;
538  }
539  } else if (data->hasFormat(QStringLiteral("text/plain"))) {
540  if (column > 1) {
541  return false;
542  }
543  const QString encodedData = QString::fromUtf8(data->data(QStringLiteral("text/plain")));
544  if (!parent.isValid()) {
545  Q_EMIT addNewDndSnippset(encodedData);
546  return false;
547  }
548  auto item = static_cast<SnippetItem *>(parent.internalPointer());
549 
550  if (item->isGroup()) {
551  Q_EMIT addNewDndSnippset(encodedData);
552  } else {
553  if (KMessageBox::ButtonCode::PrimaryAction
555  i18n("Do you want to update snippet?"),
556  i18n("Update snippet"),
557  KGuiItem(i18n("Update")),
559  item->setText(encodedData);
560  }
561  }
562  return false;
563  }
564  return false;
565 }
566 
567 Qt::DropActions SnippetsModel::supportedDropActions() const
568 {
570 }
571 
572 QModelIndex SnippetsModel::createGroup(const QString &groupName)
573 {
574  insertRow(rowCount(), QModelIndex());
575  const QModelIndex groupIndex = index(rowCount() - 1, 0, QModelIndex());
576  setData(groupIndex, groupName, SnippetsModel::NameRole);
577  return groupIndex;
578 }
579 
580 void SnippetsModel::load(const QString &filename)
581 {
582  const KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
583 
584  const KConfigGroup snippetPartGroup = config->group("SnippetPart");
585 
586  const int groupCount = snippetPartGroup.readEntry("snippetGroupCount", 0);
587 
588  for (int i = 0; i < groupCount; ++i) {
589  const KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
590 
591  const QString groupName = group.readEntry("Name");
592 
593  // create group
594  const QModelIndex groupIndex = createGroup(groupName);
595 
596  const int snippetCount = group.readEntry("snippetCount", 0);
597  for (int j = 0; j < snippetCount; ++j) {
598  const QString snippetName = group.readEntry(QStringLiteral("snippetName_%1").arg(j), QString());
599 
600  const QString snippetText = group.readEntry(QStringLiteral("snippetText_%1").arg(j), QString());
601 
602  const QString snippetKeySequence = group.readEntry(QStringLiteral("snippetKeySequence_%1").arg(j), QString());
603 
604  const QString snippetKeyword = group.readEntry(QStringLiteral("snippetKeyword_%1").arg(j), QString());
605 
606  const QString snippetSubject = group.readEntry(QStringLiteral("snippetSubject_%1").arg(j), QString());
607 
608  const QString to = group.readEntry(QStringLiteral("snippetTo_%1").arg(j), QString());
609 
610  const QString cc = group.readEntry(QStringLiteral("snippetCc_%1").arg(j), QString());
611 
612  const QString bcc = group.readEntry(QStringLiteral("snippetBcc_%1").arg(j), QString());
613 
614  const QString attachment = group.readEntry(QStringLiteral("snippetAttachment_%1").arg(j), QString());
615  createSnippet(groupIndex, snippetName, snippetText, snippetKeySequence, snippetKeyword, snippetSubject, to, cc, bcc, attachment);
616  }
617  }
618 
619  const KConfigGroup group = config->group("SavedVariablesPart");
620  const int variablesCount = group.readEntry("variablesCount", 0);
621 
622  for (int i = 0; i < variablesCount; ++i) {
623  const QString variableKey = group.readEntry(QStringLiteral("variableName_%1").arg(i), QString());
624 
625  const QString variableValue = group.readEntry(QStringLiteral("variableValue_%1").arg(i), QString());
626 
627  mSavedVariables.insert(variableKey, variableValue);
628  }
629 }
630 
631 void SnippetsModel::createSnippet(const QModelIndex &groupIndex,
632  const QString &snippetName,
633  const QString &snippetText,
634  const QString &snippetKeySequence,
635  const QString &snippetKeyword,
636  const QString &snippetSubject,
637  const QString &to,
638  const QString &cc,
639  const QString &bcc,
640  const QString &attachment)
641 {
642  insertRow(rowCount(groupIndex), groupIndex);
643  const QModelIndex modelIndex = index(rowCount(groupIndex) - 1, 0, groupIndex);
644 
645  setData(modelIndex, snippetName, SnippetsModel::NameRole);
646  setData(modelIndex, snippetText, SnippetsModel::TextRole);
647  setData(modelIndex, snippetKeySequence, SnippetsModel::KeySequenceRole);
648  setData(modelIndex, snippetKeyword, SnippetsModel::KeywordRole);
649  setData(modelIndex, snippetSubject, SnippetsModel::SubjectRole);
650 
651  setData(modelIndex, to, SnippetsModel::ToRole);
652  setData(modelIndex, cc, SnippetsModel::CcRole);
653  setData(modelIndex, bcc, SnippetsModel::BccRole);
654  setData(modelIndex, attachment, SnippetsModel::AttachmentRole);
655 
656  Q_EMIT updateActionCollection(QString(), snippetName, QKeySequence::fromString(snippetKeySequence), snippetText, snippetSubject, to, cc, bcc, attachment);
657 }
658 
659 void SnippetsModel::setSavedVariables(const QMap<QString, QString> &savedVariables)
660 {
661  mSavedVariables = savedVariables;
662 }
663 
664 QList<SnippetsInfo> SnippetsModel::snippetsInfo() const
665 {
666  QList<SnippetsInfo> infos;
667  const int groupCount = rowCount();
668 
669  for (int i = 0; i < groupCount; ++i) {
670  const QModelIndex groupIndex = index(i, 0, QModelIndex());
671  const int snippetCount = rowCount(groupIndex);
672  for (int j = 0; j < snippetCount; ++j) {
673  SnippetsInfo info;
674  const QModelIndex modelIndex = index(j, 0, groupIndex);
675 
676  const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
677  if (!snippetName.isEmpty()) {
678  const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
679  const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
680  const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
681  const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
682  const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
683  const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
684  const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
685  const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
686  info.text = snippetText;
687  info.newName = snippetName;
688  info.keyword = snippetKeyword;
689  info.keySequence = QKeySequence::fromString(snippetKeySequence);
690  info.subject = snippetSubject;
691  info.to = snippetTo;
692  info.cc = snippetCc;
693  info.bcc = snippetBcc;
694  info.attachment = snippetAttachment;
695  infos.append(info);
696  }
697  }
698  }
699  return infos;
700 }
701 
702 QMap<QString, QString> SnippetsModel::savedVariables() const
703 {
704  return mSavedVariables;
705 }
706 
707 void SnippetsModel::save(const QString &filename)
708 {
709  KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
710 
711  // clear everything
712  const QStringList lst = config->groupList();
713  for (const QString &group : lst) {
714  config->deleteGroup(group);
715  }
716 
717  // write number of snippet groups
718  KConfigGroup group = config->group("SnippetPart");
719 
720  const int groupCount = rowCount();
721  group.writeEntry("snippetGroupCount", groupCount);
722 
723  for (int i = 0; i < groupCount; ++i) {
724  const QModelIndex groupIndex = index(i, 0, QModelIndex());
725  const QString groupName = groupIndex.data(SnippetsModel::NameRole).toString();
726 
727  KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
728  group.writeEntry("Name", groupName);
729 
730  const int snippetCount = rowCount(groupIndex);
731 
732  group.writeEntry("snippetCount", snippetCount);
733  for (int j = 0; j < snippetCount; ++j) {
734  const QModelIndex modelIndex = index(j, 0, groupIndex);
735 
736  const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
737  if (!snippetName.isEmpty()) {
738  const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
739  const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
740  const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
741  const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
742  const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
743  const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
744  const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
745  const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
746 
747  group.writeEntry(QStringLiteral("snippetName_%1").arg(j), snippetName);
748  if (!snippetText.isEmpty()) {
749  group.writeEntry(QStringLiteral("snippetText_%1").arg(j), snippetText);
750  }
751  if (!snippetKeySequence.isEmpty()) {
752  group.writeEntry(QStringLiteral("snippetKeySequence_%1").arg(j), snippetKeySequence);
753  }
754  if (!snippetKeyword.isEmpty()) {
755  group.writeEntry(QStringLiteral("snippetKeyword_%1").arg(j), snippetKeyword);
756  }
757  if (!snippetSubject.isEmpty()) {
758  group.writeEntry(QStringLiteral("snippetSubject_%1").arg(j), snippetSubject);
759  }
760  if (!snippetTo.isEmpty()) {
761  group.writeEntry(QStringLiteral("snippetTo_%1").arg(j), snippetTo);
762  }
763  if (!snippetCc.isEmpty()) {
764  group.writeEntry(QStringLiteral("snippetCc_%1").arg(j), snippetCc);
765  }
766  if (!snippetBcc.isEmpty()) {
767  group.writeEntry(QStringLiteral("snippetBcc_%1").arg(j), snippetBcc);
768  }
769  if (!snippetAttachment.isEmpty()) {
770  group.writeEntry(QStringLiteral("snippetAttachment_%1").arg(j), snippetAttachment);
771  }
772  }
773  }
774  }
775 
776  {
777  KConfigGroup group = config->group("SavedVariablesPart");
778 
779  const int variablesCount = mSavedVariables.count();
780  group.writeEntry("variablesCount", variablesCount);
781 
782  int counter = 0;
783  QMap<QString, QString>::const_iterator it = mSavedVariables.cbegin();
784  const QMap<QString, QString>::const_iterator itEnd = mSavedVariables.cend();
785  for (; it != itEnd; ++it) {
786  group.writeEntry(QStringLiteral("variableName_%1").arg(counter), it.key());
787  group.writeEntry(QStringLiteral("variableValue_%1").arg(counter), it.value());
788  counter++;
789  }
790  }
791  config->sync();
792 }
793 
794 #include "moc_snippetsmodel.cpp"
void append(const T &value)
QByteArray data(const QString &mimeType) const const
quintptr internalId() const const
@ IsGroupRole
Returns whether the index represents a group.
Definition: snippetsmodel.h:43
QString readEntry(const char *key, const char *aDefault=nullptr) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
DisplayRole
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QString fromUtf8(const char *str, int size)
void setData(const QString &mimeType, const QByteArray &data)
void * internalPointer() const const
Q_EMITQ_EMIT
const T value(const Key &key, const T &defaultValue) const const
@ NameRole
The name of a snippet or group.
Definition: snippetsmodel.h:44
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
bool insertRow(int row, const QModelIndex &parent)
@ BccRole
The Cc of a snippet.
Definition: snippetsmodel.h:51
@ KeySequenceRole
The key sequence to activate a snippet.
Definition: snippetsmodel.h:46
void keySequence(QWindow *window, const QKeySequence &keySequence)
QMap::const_iterator cbegin() const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void beginInsertRows(const QModelIndex &parent, int first, int last)
KGuiItem cancel()
QMap::iterator insert(const Key &key, const T &value)
QVariant data(int role) const const
QModelIndex createIndex(int row, int column, void *ptr) const const
typedef ItemFlags
QString i18n(const char *text, const TYPE &arg...)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
QMap::const_iterator cend() const const
bool isEmpty() const const
int count(const Key &key) const const
@ KeywordRole
The keyword which will replace by snippet.
Definition: snippetsmodel.h:47
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
virtual bool hasFormat(const QString &mimeType) const const
KSharedConfigPtr config()
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
bool isValid() const const
const Key key(const T &value, const Key &defaultKey) const const
QKeySequence fromString(const QString &str, QKeySequence::SequenceFormat format)
void setText(const QString &text)
DropAction
int count() const const
@ ToRole
The To of a snippet.
Definition: snippetsmodel.h:49
@ AttachmentRole
The Attachment of a snippet.
Definition: snippetsmodel.h:52
const char * name(StandardAction id)
@ TextRole
The text of a snippet.
Definition: snippetsmodel.h:45
The SnippetsModel class.
Definition: snippetsmodel.h:38
QModelIndex parent() const const
@ CcRole
The Cc of a snippet.
Definition: snippetsmodel.h:50
@ SubjectRole
The subject of a snippet.
Definition: snippetsmodel.h:48
The SnippetsInfo struct.
Definition: snippetsmodel.h:22
QObject * parent() const const
QString & append(QChar ch)
The filter dialog.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Oct 1 2023 04:00:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.