Mailcommon

snippetsmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3 SPDX-FileContributor: Tobias Koenig <tokoe@kdab.com>
4
5 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
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
21using namespace MailCommon;
22
23class MailCommon::SnippetItem
24{
25public:
26 explicit SnippetItem(bool isGroup = false, SnippetItem *parent = nullptr);
27 ~SnippetItem();
28
29 [[nodiscard]] bool isGroup() const;
30
31 void setName(const QString &name);
32 [[nodiscard]] QString name() const;
33
34 void setText(const QString &text);
35 [[nodiscard]] QString text() const;
36
37 void setKeyword(const QString &text);
38 [[nodiscard]] QString keyword() const;
39
40 void setKeySequence(const QString &sequence);
41 [[nodiscard]] QString keySequence() const;
42
43 void insertChild(int row, SnippetItem *child);
44 void removeChild(SnippetItem *child);
45 [[nodiscard]] SnippetItem *child(int row) const;
46 [[nodiscard]] int childCount() const;
47 [[nodiscard]] int row() const;
48 [[nodiscard]] SnippetItem *parent() const;
49
50 [[nodiscard]] QString subject() const;
51 void setSubject(const QString &subject);
52
53 [[nodiscard]] QString to() const;
54 void setTo(const QString &to);
55
56 [[nodiscard]] QString cc() const;
57 void setCc(const QString &cc);
58
59 [[nodiscard]] QString bcc() const;
60 void setBcc(const QString &bcc);
61
62 [[nodiscard]] QString attachment() const;
63 void setAttachment(const QString &attachment);
64
65private:
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
81SnippetItem::SnippetItem(bool isGroup, SnippetItem *parent)
82 : mParentItem(parent)
83 , mIsGroup(isGroup)
84{
85}
86
87SnippetItem::~SnippetItem()
88{
89 qDeleteAll(mChildItems);
90 mChildItems.clear();
91}
92
93bool SnippetItem::isGroup() const
94{
95 return mIsGroup;
96}
97
98void SnippetItem::setName(const QString &name)
99{
100 mName = name;
101}
102
103QString SnippetItem::name() const
104{
105 return mName;
106}
107
108void SnippetItem::setText(const QString &text)
109{
110 mText = text;
111}
112
113QString SnippetItem::text() const
114{
115 return mText;
116}
117
118void SnippetItem::setKeyword(const QString &text)
119{
120 mKeyword = text;
121}
122
123QString SnippetItem::keyword() const
124{
125 return mKeyword;
126}
127
128void SnippetItem::setKeySequence(const QString &sequence)
129{
130 mKeySequence = sequence;
131}
132
133QString SnippetItem::keySequence() const
134{
135 return mKeySequence;
136}
137
138void SnippetItem::insertChild(int row, SnippetItem *item)
139{
140 mChildItems.insert(row, item);
141}
142
143void SnippetItem::removeChild(SnippetItem *item)
144{
145 mChildItems.removeAll(item);
146 delete item;
147}
148
149SnippetItem *SnippetItem::child(int row) const
150{
151 return mChildItems.value(row);
152}
153
154int SnippetItem::childCount() const
155{
156 return mChildItems.count();
157}
158
159SnippetItem *SnippetItem::parent() const
160{
161 return mParentItem;
162}
163
164QString SnippetItem::subject() const
165{
166 return mSubject;
167}
168
169void SnippetItem::setSubject(const QString &subject)
170{
171 mSubject = subject;
172}
173
174QString SnippetItem::to() const
175{
176 return mTo;
177}
178
179void SnippetItem::setTo(const QString &to)
180{
181 mTo = to;
182}
183
184QString SnippetItem::cc() const
185{
186 return mCc;
187}
188
189void SnippetItem::setCc(const QString &cc)
190{
191 mCc = cc;
192}
193
194QString SnippetItem::bcc() const
195{
196 return mBcc;
197}
198
199void SnippetItem::setBcc(const QString &bcc)
200{
201 mBcc = bcc;
202}
203
204QString SnippetItem::attachment() const
205{
206 return mAttachment;
207}
208
209void SnippetItem::setAttachment(const QString &attachment)
210{
211 mAttachment = attachment;
212}
213
214int SnippetItem::row() const
215{
216 if (mParentItem) {
217 return mParentItem->mChildItems.indexOf(const_cast<SnippetItem *>(this));
218 }
219
220 return 0;
221}
222
223SnippetsModel *SnippetsModel::instance()
224{
225 static SnippetsModel s_self;
226 return &s_self;
227}
228
229SnippetsModel::SnippetsModel(QObject *parent)
230 : QAbstractItemModel(parent)
231{
232 mRootItem = new SnippetItem(true);
233 load();
234}
235
236SnippetsModel::~SnippetsModel()
237{
238 delete mRootItem;
239}
240
241int SnippetsModel::columnCount(const QModelIndex &) const
242{
243 return 1;
244}
245
246bool 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
297QVariant 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
333Qt::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
347QModelIndex 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
385int 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
401bool 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->insertChild(row, snippet);
415 }
417
418 return true;
419}
420
421bool 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 }
436
437 return true;
438}
439
440QStringList SnippetsModel::mimeTypes() const
441{
442 return QStringList() << QStringLiteral("text/x-kmail-textsnippet") << QStringLiteral("text/plain");
443}
444
445QMimeData *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
471bool SnippetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
472{
473 if (action == Qt::IgnoreAction) {
474 return true;
475 }
476
477 if (data->hasFormat(QStringLiteral("text/x-kmail-textsnippet"))) {
478 if (!parent.isValid()) {
479 return false;
480 }
481
482 if (column > 1) {
483 return false;
484 }
485 if (row == -1) {
486 row = rowCount(parent);
487 }
488
489 auto item = static_cast<SnippetItem *>(parent.internalPointer());
490
491 QByteArray encodedData = data->data(QStringLiteral("text/x-kmail-textsnippet"));
492 QDataStream stream(&encodedData, QIODevice::ReadOnly);
493
494 quintptr id;
496 QString text;
498 QString keyword;
499 QString subject;
500 QString to;
501 QString cc;
502 QString bcc;
503 QString attachment;
504 stream >> id >> name >> text >> keySequence >> keyword >> subject >> to >> cc >> bcc >> attachment;
505 if (item->isGroup()) {
506 insertRow(row, parent);
507
508 const QModelIndex idx = index(row, 0, parent);
509
510 setData(idx, name, SnippetsModel::NameRole);
511 setData(idx, text, SnippetsModel::TextRole);
512 setData(idx, keySequence, SnippetsModel::KeySequenceRole);
513 setData(idx, keyword, SnippetsModel::KeywordRole);
514 setData(idx, subject, SnippetsModel::SubjectRole);
515 setData(idx, to, SnippetsModel::ToRole);
516 setData(idx, cc, SnippetsModel::CcRole);
517 setData(idx, bcc, SnippetsModel::BccRole);
518 setData(idx, attachment, SnippetsModel::AttachmentRole);
519 Q_EMIT dndDone();
520 return true;
521 } else {
522 if (KMessageBox::ButtonCode::PrimaryAction
524 i18n("Do you want to update snippet?"),
525 i18nc("@title:window", "Update snippet"),
526 KGuiItem(i18nc("@action:button", "Update")),
528 item->setText(text);
529 item->setSubject(subject);
530 item->setTo(to);
531 item->setCc(cc);
532 item->setBcc(bcc);
533 return true;
534 }
535 return false;
536 }
537 } else if (data->hasFormat(QStringLiteral("text/plain"))) {
538 if (column > 1) {
539 return false;
540 }
541 const QString encodedData = QString::fromUtf8(data->data(QStringLiteral("text/plain")));
542 if (!parent.isValid()) {
543 Q_EMIT addNewDndSnippset(encodedData);
544 return false;
545 }
546 auto item = static_cast<SnippetItem *>(parent.internalPointer());
547
548 if (item->isGroup()) {
549 Q_EMIT addNewDndSnippset(encodedData);
550 } else {
551 if (KMessageBox::ButtonCode::PrimaryAction
553 i18n("Do you want to update snippet?"),
554 i18nc("@title:window", "Update snippet"),
555 KGuiItem(i18nc("@action:button", "Update")),
557 item->setText(encodedData);
558 }
559 }
560 return false;
561 }
562 return false;
563}
564
565Qt::DropActions SnippetsModel::supportedDropActions() const
566{
568}
569
570QModelIndex SnippetsModel::createGroup(const QString &groupName)
571{
572 insertRow(rowCount(), QModelIndex());
573 const QModelIndex groupIndex = index(rowCount() - 1, 0, QModelIndex());
574 setData(groupIndex, groupName, SnippetsModel::NameRole);
575 return groupIndex;
576}
577
578void SnippetsModel::load(const QString &filename)
579{
580 const KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
581
582 const KConfigGroup snippetPartGroup = config->group("SnippetPart");
583
584 const int groupCount = snippetPartGroup.readEntry("snippetGroupCount", 0);
585
586 for (int i = 0; i < groupCount; ++i) {
587 const KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
588
589 const QString groupName = group.readEntry("Name");
590
591 // create group
592 const QModelIndex groupIndex = createGroup(groupName);
593
594 const int snippetCount = group.readEntry("snippetCount", 0);
595 for (int j = 0; j < snippetCount; ++j) {
596 const QString snippetName = group.readEntry(QStringLiteral("snippetName_%1").arg(j), QString());
597
598 const QString snippetText = group.readEntry(QStringLiteral("snippetText_%1").arg(j), QString());
599
600 const QString snippetKeySequence = group.readEntry(QStringLiteral("snippetKeySequence_%1").arg(j), QString());
601
602 const QString snippetKeyword = group.readEntry(QStringLiteral("snippetKeyword_%1").arg(j), QString());
603
604 const QString snippetSubject = group.readEntry(QStringLiteral("snippetSubject_%1").arg(j), QString());
605
606 const QString to = group.readEntry(QStringLiteral("snippetTo_%1").arg(j), QString());
607
608 const QString cc = group.readEntry(QStringLiteral("snippetCc_%1").arg(j), QString());
609
610 const QString bcc = group.readEntry(QStringLiteral("snippetBcc_%1").arg(j), QString());
611
612 const QString attachment = group.readEntry(QStringLiteral("snippetAttachment_%1").arg(j), QString());
613 createSnippet(groupIndex, snippetName, snippetText, snippetKeySequence, snippetKeyword, snippetSubject, to, cc, bcc, attachment);
614 }
615 }
616
617 const KConfigGroup group = config->group("SavedVariablesPart");
618 const int variablesCount = group.readEntry("variablesCount", 0);
619
620 for (int i = 0; i < variablesCount; ++i) {
621 const QString variableKey = group.readEntry(QStringLiteral("variableName_%1").arg(i), QString());
622
623 const QString variableValue = group.readEntry(QStringLiteral("variableValue_%1").arg(i), QString());
624
625 mSavedVariables.insert(variableKey, variableValue);
626 }
627}
628
629void SnippetsModel::createSnippet(const QModelIndex &groupIndex,
630 const QString &snippetName,
631 const QString &snippetText,
632 const QString &snippetKeySequence,
633 const QString &snippetKeyword,
634 const QString &snippetSubject,
635 const QString &to,
636 const QString &cc,
637 const QString &bcc,
638 const QString &attachment)
639{
640 insertRow(rowCount(groupIndex), groupIndex);
641 const QModelIndex modelIndex = index(rowCount(groupIndex) - 1, 0, groupIndex);
642
643 setData(modelIndex, snippetName, SnippetsModel::NameRole);
644 setData(modelIndex, snippetText, SnippetsModel::TextRole);
645 setData(modelIndex, snippetKeySequence, SnippetsModel::KeySequenceRole);
646 setData(modelIndex, snippetKeyword, SnippetsModel::KeywordRole);
647 setData(modelIndex, snippetSubject, SnippetsModel::SubjectRole);
648
649 setData(modelIndex, to, SnippetsModel::ToRole);
650 setData(modelIndex, cc, SnippetsModel::CcRole);
651 setData(modelIndex, bcc, SnippetsModel::BccRole);
652 setData(modelIndex, attachment, SnippetsModel::AttachmentRole);
653
654 Q_EMIT updateActionCollection(QString(), snippetName, QKeySequence::fromString(snippetKeySequence), snippetText, snippetSubject, to, cc, bcc, attachment);
655}
656
657void SnippetsModel::setSavedVariables(const QMap<QString, QString> &savedVariables)
658{
659 mSavedVariables = savedVariables;
660}
661
662QList<SnippetsInfo> SnippetsModel::snippetsInfo() const
663{
665 const int groupCount = rowCount();
666
667 for (int i = 0; i < groupCount; ++i) {
668 const QModelIndex groupIndex = index(i, 0, QModelIndex());
669 const int snippetCount = rowCount(groupIndex);
670 for (int j = 0; j < snippetCount; ++j) {
671 SnippetsInfo info;
672 const QModelIndex modelIndex = index(j, 0, groupIndex);
673
674 const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
675 if (!snippetName.isEmpty()) {
676 const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
677 const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
678 const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
679 const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
680 const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
681 const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
682 const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
683 const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
684 info.text = snippetText;
685 info.newName = snippetName;
686 info.keyword = snippetKeyword;
687 info.keySequence = QKeySequence::fromString(snippetKeySequence);
688 info.subject = snippetSubject;
689 info.to = snippetTo;
690 info.cc = snippetCc;
691 info.bcc = snippetBcc;
692 info.attachment = snippetAttachment;
693 infos.append(info);
694 }
695 }
696 }
697 return infos;
698}
699
700QMap<QString, QString> SnippetsModel::savedVariables() const
701{
702 return mSavedVariables;
703}
704
705void SnippetsModel::save(const QString &filename)
706{
707 KSharedConfig::Ptr config = KSharedConfig::openConfig(filename.isEmpty() ? QStringLiteral("kmailsnippetrc") : filename, KConfig::NoGlobals);
708
709 // clear everything
710 const QStringList lst = config->groupList();
711 for (const QString &group : lst) {
712 config->deleteGroup(group);
713 }
714
715 // write number of snippet groups
716 KConfigGroup group = config->group("SnippetPart");
717
718 const int groupCount = rowCount();
719 group.writeEntry("snippetGroupCount", groupCount);
720
721 for (int i = 0; i < groupCount; ++i) {
722 const QModelIndex groupIndex = index(i, 0, QModelIndex());
723 const QString groupName = groupIndex.data(SnippetsModel::NameRole).toString();
724
725 KConfigGroup group = config->group(QStringLiteral("SnippetGroup_%1").arg(i));
726 group.writeEntry("Name", groupName);
727
728 const int snippetCount = rowCount(groupIndex);
729
730 group.writeEntry("snippetCount", snippetCount);
731 for (int j = 0; j < snippetCount; ++j) {
732 const QModelIndex modelIndex = index(j, 0, groupIndex);
733
734 const QString snippetName = modelIndex.data(SnippetsModel::NameRole).toString();
735 if (!snippetName.isEmpty()) {
736 const QString snippetText = modelIndex.data(SnippetsModel::TextRole).toString();
737 const QString snippetKeySequence = modelIndex.data(SnippetsModel::KeySequenceRole).toString();
738 const QString snippetKeyword = modelIndex.data(SnippetsModel::KeywordRole).toString();
739 const QString snippetSubject = modelIndex.data(SnippetsModel::SubjectRole).toString();
740 const QString snippetTo = modelIndex.data(SnippetsModel::ToRole).toString();
741 const QString snippetCc = modelIndex.data(SnippetsModel::CcRole).toString();
742 const QString snippetBcc = modelIndex.data(SnippetsModel::BccRole).toString();
743 const QString snippetAttachment = modelIndex.data(SnippetsModel::AttachmentRole).toString();
744
745 group.writeEntry(QStringLiteral("snippetName_%1").arg(j), snippetName);
746 if (!snippetText.isEmpty()) {
747 group.writeEntry(QStringLiteral("snippetText_%1").arg(j), snippetText);
748 }
749 if (!snippetKeySequence.isEmpty()) {
750 group.writeEntry(QStringLiteral("snippetKeySequence_%1").arg(j), snippetKeySequence);
751 }
752 if (!snippetKeyword.isEmpty()) {
753 group.writeEntry(QStringLiteral("snippetKeyword_%1").arg(j), snippetKeyword);
754 }
755 if (!snippetSubject.isEmpty()) {
756 group.writeEntry(QStringLiteral("snippetSubject_%1").arg(j), snippetSubject);
757 }
758 if (!snippetTo.isEmpty()) {
759 group.writeEntry(QStringLiteral("snippetTo_%1").arg(j), snippetTo);
760 }
761 if (!snippetCc.isEmpty()) {
762 group.writeEntry(QStringLiteral("snippetCc_%1").arg(j), snippetCc);
763 }
764 if (!snippetBcc.isEmpty()) {
765 group.writeEntry(QStringLiteral("snippetBcc_%1").arg(j), snippetBcc);
766 }
767 if (!snippetAttachment.isEmpty()) {
768 group.writeEntry(QStringLiteral("snippetAttachment_%1").arg(j), snippetAttachment);
769 }
770 }
771 }
772 }
773
774 {
775 KConfigGroup group = config->group("SavedVariablesPart");
776
777 const int variablesCount = mSavedVariables.count();
778 group.writeEntry("variablesCount", variablesCount);
779
780 int counter = 0;
781 QMap<QString, QString>::const_iterator it = mSavedVariables.cbegin();
782 const QMap<QString, QString>::const_iterator itEnd = mSavedVariables.cend();
783 for (; it != itEnd; ++it) {
784 group.writeEntry(QStringLiteral("variableName_%1").arg(counter), it.key());
785 group.writeEntry(QStringLiteral("variableValue_%1").arg(counter), it.value());
786 counter++;
787 }
788 }
789 config->sync();
790}
791
792#include "moc_snippetsmodel.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The SnippetsModel class.
@ AttachmentRole
The Attachment of a snippet.
@ KeywordRole
The keyword which will replace by snippet.
@ CcRole
The Cc of a snippet.
@ SubjectRole
The subject of a snippet.
@ BccRole
The Cc of a snippet.
@ KeySequenceRole
The key sequence to activate a snippet.
@ IsGroupRole
Returns whether the index represents a group.
@ ToRole
The To of a snippet.
@ NameRole
The name of a snippet or group.
@ TextRole
The text of a snippet.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString name(GameStandardAction id)
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem cancel()
The filter dialog.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
bool insertRow(int row, const QModelIndex &parent)
QKeySequence fromString(const QString &str, SequenceFormat format)
void append(QList< T > &&value)
void clear()
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
iterator insert(const_iterator before, parameter_type value)
qsizetype removeAll(const AT &t)
T value(qsizetype i) const const
const_iterator cbegin() const const
const_iterator cend() const const
size_type count() const const
iterator insert(const Key &key, const T &value)
QByteArray data(const QString &mimeType) const const
virtual bool hasFormat(const QString &mimeType) const const
void setData(const QString &mimeType, const QByteArray &data)
void setText(const QString &text)
QVariant data(int role) const const
quintptr internalId() const const
void * internalPointer() const const
bool isValid() const const
QModelIndex parent() const const
Q_EMITQ_EMIT
QObject * parent() const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
DropAction
DisplayRole
typedef ItemFlags
void keySequence(QWidget *widget, const QKeySequence &keySequence)
QString toString() const const
The SnippetsInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:58:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.