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-2022 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  QVector<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::Yes
525  == KMessageBox::questionYesNo(nullptr, i18n("Do you want to update snippet?"), i18n("Update snippet"), KGuiItem(i18n("Update")))) {
526  item->setText(text);
527  item->setSubject(subject);
528  item->setTo(to);
529  item->setCc(cc);
530  item->setBcc(bcc);
531  return true;
532  }
533  return false;
534  }
535  } else if (data->hasFormat(QStringLiteral("text/plain"))) {
536  if (column > 1) {
537  return false;
538  }
539  const QString encodedData = QString::fromUtf8(data->data(QStringLiteral("text/plain")));
540  if (!parent.isValid()) {
541  Q_EMIT addNewDndSnippset(encodedData);
542  return false;
543  }
544  auto item = static_cast<SnippetItem *>(parent.internalPointer());
545 
546  if (item->isGroup()) {
547  Q_EMIT addNewDndSnippset(encodedData);
548  } else {
549  if (KMessageBox::Yes == KMessageBox::questionYesNo(nullptr, i18n("Do you want to update snippet?"), i18n("Update snippet"))) {
550  item->setText(encodedData);
551  }
552  }
553  return false;
554  }
555  return false;
556 }
557 
558 Qt::DropActions SnippetsModel::supportedDropActions() const
559 {
561 }
562 
563 QModelIndex SnippetsModel::createGroup(const QString &groupName)
564 {
565  insertRow(rowCount(), QModelIndex());
566  const QModelIndex groupIndex = index(rowCount() - 1, 0, QModelIndex());
567  setData(groupIndex, groupName, SnippetsModel::NameRole);
568  return groupIndex;
569 }
570 
571 void SnippetsModel::load(const QString &filename)
572 {
573  const KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
574 
575  const KConfigGroup snippetPartGroup = config->group("SnippetPart");
576 
577  const int groupCount = snippetPartGroup.readEntry("snippetGroupCount", 0);
578 
579  for (int i = 0; i < groupCount; ++i) {
580  const KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
581 
582  const QString groupName = group.readEntry("Name");
583 
584  // create group
585  const QModelIndex groupIndex = createGroup(groupName);
586 
587  const int snippetCount = group.readEntry("snippetCount", 0);
588  for (int j = 0; j < snippetCount; ++j) {
589  const QString snippetName = group.readEntry(QStringLiteral("snippetName_%1").arg(j), QString());
590 
591  const QString snippetText = group.readEntry(QStringLiteral("snippetText_%1").arg(j), QString());
592 
593  const QString snippetKeySequence = group.readEntry(QStringLiteral("snippetKeySequence_%1").arg(j), QString());
594 
595  const QString snippetKeyword = group.readEntry(QStringLiteral("snippetKeyword_%1").arg(j), QString());
596 
597  const QString snippetSubject = group.readEntry(QStringLiteral("snippetSubject_%1").arg(j), QString());
598 
599  const QString to = group.readEntry(QStringLiteral("snippetTo_%1").arg(j), QString());
600 
601  const QString cc = group.readEntry(QStringLiteral("snippetCc_%1").arg(j), QString());
602 
603  const QString bcc = group.readEntry(QStringLiteral("snippetBcc_%1").arg(j), QString());
604 
605  const QString attachment = group.readEntry(QStringLiteral("snippetAttachment_%1").arg(j), QString());
606  createSnippet(groupIndex, snippetName, snippetText, snippetKeySequence, snippetKeyword, snippetSubject, to, cc, bcc, attachment);
607  }
608  }
609 
610  const KConfigGroup group = config->group("SavedVariablesPart");
611  const int variablesCount = group.readEntry("variablesCount", 0);
612 
613  for (int i = 0; i < variablesCount; ++i) {
614  const QString variableKey = group.readEntry(QStringLiteral("variableName_%1").arg(i), QString());
615 
616  const QString variableValue = group.readEntry(QStringLiteral("variableValue_%1").arg(i), QString());
617 
618  mSavedVariables.insert(variableKey, variableValue);
619  }
620 }
621 
622 void SnippetsModel::createSnippet(const QModelIndex &groupIndex,
623  const QString &snippetName,
624  const QString &snippetText,
625  const QString &snippetKeySequence,
626  const QString &snippetKeyword,
627  const QString &snippetSubject,
628  const QString &to,
629  const QString &cc,
630  const QString &bcc,
631  const QString &attachment)
632 {
633  insertRow(rowCount(groupIndex), groupIndex);
634  const QModelIndex modelIndex = index(rowCount(groupIndex) - 1, 0, groupIndex);
635 
636  setData(modelIndex, snippetName, SnippetsModel::NameRole);
637  setData(modelIndex, snippetText, SnippetsModel::TextRole);
638  setData(modelIndex, snippetKeySequence, SnippetsModel::KeySequenceRole);
639  setData(modelIndex, snippetKeyword, SnippetsModel::KeywordRole);
640  setData(modelIndex, snippetSubject, SnippetsModel::SubjectRole);
641 
642  setData(modelIndex, to, SnippetsModel::ToRole);
643  setData(modelIndex, cc, SnippetsModel::CcRole);
644  setData(modelIndex, bcc, SnippetsModel::BccRole);
645  setData(modelIndex, attachment, SnippetsModel::AttachmentRole);
646 
647  Q_EMIT updateActionCollection(QString(), snippetName, QKeySequence::fromString(snippetKeySequence), snippetText, snippetSubject, to, cc, bcc, attachment);
648 }
649 
650 void SnippetsModel::setSavedVariables(const QMap<QString, QString> &savedVariables)
651 {
652  mSavedVariables = savedVariables;
653 }
654 
655 QVector<SnippetsInfo> SnippetsModel::snippetsInfo() const
656 {
657  QVector<SnippetsInfo> infos;
658  const int groupCount = rowCount();
659 
660  for (int i = 0; i < groupCount; ++i) {
661  const QModelIndex groupIndex = index(i, 0, QModelIndex());
662  const int snippetCount = rowCount(groupIndex);
663  for (int j = 0; j < snippetCount; ++j) {
664  SnippetsInfo info;
665  const QModelIndex modelIndex = index(j, 0, groupIndex);
666 
667  const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
668  if (!snippetName.isEmpty()) {
669  const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
670  const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
671  const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
672  const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
673  const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
674  const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
675  const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
676  const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
677  info.text = snippetText;
678  info.newName = snippetName;
679  info.keyword = snippetKeyword;
680  info.keySequence = QKeySequence::fromString(snippetKeySequence);
681  info.subject = snippetSubject;
682  info.to = snippetTo;
683  info.cc = snippetCc;
684  info.bcc = snippetBcc;
685  info.attachment = snippetAttachment;
686  infos.append(info);
687  }
688  }
689  }
690  return infos;
691 }
692 
693 QMap<QString, QString> SnippetsModel::savedVariables() const
694 {
695  return mSavedVariables;
696 }
697 
698 void SnippetsModel::save(const QString &filename)
699 {
700  KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
701 
702  // clear everything
703  const QStringList lst = config->groupList();
704  for (const QString &group : lst) {
705  config->deleteGroup(group);
706  }
707 
708  // write number of snippet groups
709  KConfigGroup group = config->group("SnippetPart");
710 
711  const int groupCount = rowCount();
712  group.writeEntry("snippetGroupCount", groupCount);
713 
714  for (int i = 0; i < groupCount; ++i) {
715  const QModelIndex groupIndex = index(i, 0, QModelIndex());
716  const QString groupName = groupIndex.data(SnippetsModel::NameRole).toString();
717 
718  KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
719  group.writeEntry("Name", groupName);
720 
721  const int snippetCount = rowCount(groupIndex);
722 
723  group.writeEntry("snippetCount", snippetCount);
724  for (int j = 0; j < snippetCount; ++j) {
725  const QModelIndex modelIndex = index(j, 0, groupIndex);
726 
727  const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
728  if (!snippetName.isEmpty()) {
729  const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
730  const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
731  const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
732  const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
733  const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
734  const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
735  const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
736  const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
737 
738  group.writeEntry(QStringLiteral("snippetName_%1").arg(j), snippetName);
739  if (!snippetText.isEmpty()) {
740  group.writeEntry(QStringLiteral("snippetText_%1").arg(j), snippetText);
741  }
742  if (!snippetKeySequence.isEmpty()) {
743  group.writeEntry(QStringLiteral("snippetKeySequence_%1").arg(j), snippetKeySequence);
744  }
745  if (!snippetKeyword.isEmpty()) {
746  group.writeEntry(QStringLiteral("snippetKeyword_%1").arg(j), snippetKeyword);
747  }
748  if (!snippetSubject.isEmpty()) {
749  group.writeEntry(QStringLiteral("snippetSubject_%1").arg(j), snippetSubject);
750  }
751  if (!snippetTo.isEmpty()) {
752  group.writeEntry(QStringLiteral("snippetTo_%1").arg(j), snippetTo);
753  }
754  if (!snippetCc.isEmpty()) {
755  group.writeEntry(QStringLiteral("snippetCc_%1").arg(j), snippetCc);
756  }
757  if (!snippetBcc.isEmpty()) {
758  group.writeEntry(QStringLiteral("snippetBcc_%1").arg(j), snippetBcc);
759  }
760  if (!snippetAttachment.isEmpty()) {
761  group.writeEntry(QStringLiteral("snippetAttachment_%1").arg(j), snippetAttachment);
762  }
763  }
764  }
765  }
766 
767  {
768  KConfigGroup group = config->group("SavedVariablesPart");
769 
770  const int variablesCount = mSavedVariables.count();
771  group.writeEntry("variablesCount", variablesCount);
772 
773  int counter = 0;
774  QMap<QString, QString>::const_iterator it = mSavedVariables.cbegin();
775  const QMap<QString, QString>::const_iterator itEnd = mSavedVariables.cend();
776  for (; it != itEnd; ++it) {
777  group.writeEntry(QStringLiteral("variableName_%1").arg(counter), it.key());
778  group.writeEntry(QStringLiteral("variableValue_%1").arg(counter), it.value());
779  counter++;
780  }
781  }
782  config->sync();
783 }
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
void append(const T &value)
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)
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()
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
QString name(StandardShortcut 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
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
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-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.