File indexing completed on 2024-05-05 05:52:19
0001 /* This file is part of the Kate project. 0002 * Based on the snippet plugin from KDevelop 4. 0003 * 0004 * SPDX-FileCopyrightText: 2007 Robert Gruber <rgruber@users.sourceforge.net> 0005 * SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de> 0006 * SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org> 0007 * 0008 * SPDX-License-Identifier: LGPL-2.0-or-later 0009 */ 0010 0011 #include "snippetstore.h" 0012 0013 #include "katesnippetglobal.h" 0014 #include "snippetrepository.h" 0015 0016 #include <QDir> 0017 #include <QStandardPaths> 0018 0019 #include <KSharedConfig> 0020 0021 #include <ktexteditor/editor.h> 0022 0023 Q_DECLARE_METATYPE(KSharedConfig::Ptr) 0024 0025 SnippetStore *SnippetStore::m_self = nullptr; 0026 0027 SnippetStore::SnippetStore() 0028 { 0029 m_self = this; 0030 0031 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, // 0032 QStringLiteral("ktexteditor_snippets/data"), 0033 QStandardPaths::LocateDirectory) 0034 << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("ktexteditor_snippets/ghns"), QStandardPaths::LocateDirectory); 0035 0036 QStringList files; 0037 for (const QString &dir : dirs) { 0038 const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.xml")); 0039 for (const QString &file : fileNames) { 0040 files.append(dir + QLatin1Char('/') + file); 0041 } 0042 } 0043 0044 for (const QString &file : qAsConst(files)) { 0045 SnippetRepository *repo = new SnippetRepository(file); 0046 appendRow(repo); 0047 } 0048 } 0049 0050 SnippetStore::~SnippetStore() 0051 { 0052 invisibleRootItem()->removeRows(0, invisibleRootItem()->rowCount()); 0053 m_self = nullptr; 0054 } 0055 0056 void SnippetStore::init() 0057 { 0058 Q_ASSERT(!SnippetStore::self()); 0059 new SnippetStore(); 0060 } 0061 0062 SnippetStore *SnippetStore::self() 0063 { 0064 return m_self; 0065 } 0066 0067 Qt::ItemFlags SnippetStore::flags(const QModelIndex &index) const 0068 { 0069 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; 0070 if (!index.parent().isValid()) { 0071 flags |= Qt::ItemIsUserCheckable; 0072 } 0073 return flags; 0074 } 0075 0076 KConfigGroup SnippetStore::getConfig() 0077 { 0078 return KSharedConfig::openConfig()->group(QStringLiteral("Snippets")); 0079 } 0080 0081 bool SnippetStore::setData(const QModelIndex &index, const QVariant &value, int role) 0082 { 0083 if (role == Qt::EditRole && value.toString().isEmpty()) { 0084 // don't allow empty names 0085 return false; 0086 } 0087 if (value == data(index, role)) { 0088 // if unchanged, avoid saving 0089 return true; 0090 } 0091 const bool success = QStandardItemModel::setData(index, value, role); 0092 if (!success || role != Qt::EditRole) { 0093 return success; 0094 } 0095 0096 // when we edited something, save the repository 0097 0098 QStandardItem *repoItem = nullptr; 0099 if (index.parent().isValid()) { 0100 repoItem = itemFromIndex(index.parent()); 0101 } else { 0102 repoItem = itemFromIndex(index); 0103 } 0104 0105 SnippetRepository *repo = SnippetRepository::fromItem(repoItem); 0106 if (repo) { 0107 repo->save(); 0108 } 0109 return true; 0110 } 0111 0112 SnippetRepository *SnippetStore::repositoryForFile(const QString &file) 0113 { 0114 for (int i = 0; i < rowCount(); ++i) { 0115 if (auto *repo = SnippetRepository::fromItem(item(i))) { 0116 if (repo->file() == file) { 0117 return repo; 0118 } 0119 } 0120 } 0121 return nullptr; 0122 } 0123 0124 #include "moc_snippetstore.cpp"