1
/* This file is part of the Kate project.
2
* Based on the snippet plugin from KDevelop 4.
4
* Copyright (C) 2007 Robert Gruber <rgruber@users.sourceforge.net>
5
* Copyright (C) 2010 Milian Wolff <mail@milianw.de>
6
* Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Library General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Library General Public License for more details.
18
* You should have received a copy of the GNU Library General Public License
19
* along with this library; see the file COPYING.LIB. If not, write to
20
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
* Boston, MA 02110-1301, USA.
24
#include "snippetstore.h"
26
#include "katesnippetglobal.h"
27
#include "snippetrepository.h"
29
#include <KStandardDirs>
32
#include <ktexteditor/editor.h>
33
#include <ktexteditor/templateinterface2.h>
35
SnippetStore* SnippetStore::m_self = 0;
37
SnippetStore::SnippetStore(KateSnippetGlobal* plugin)
38
: m_plugin(plugin), m_scriptregistrar(0)
42
const QStringList list = KGlobal::dirs()->findAllResources("data",
43
"ktexteditor_snippets/data/*.xml", KStandardDirs::NoDuplicates)
44
<< KGlobal::dirs()->findAllResources("data",
45
"ktexteditor_snippets/ghns/*.xml", KStandardDirs::NoDuplicates);
47
foreach(const QString& file, list ) {
48
SnippetRepository* repo = new SnippetRepository(file);
52
m_scriptregistrar = KateGlobal::self();
55
SnippetStore::~SnippetStore()
57
invisibleRootItem()->removeRows( 0, invisibleRootItem()->rowCount() );
61
void SnippetStore::init(KateSnippetGlobal* plugin)
63
Q_ASSERT(!SnippetStore::self());
64
new SnippetStore(plugin);
67
SnippetStore* SnippetStore::self()
72
Qt::ItemFlags SnippetStore::flags(const QModelIndex & index) const
74
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
75
if ( !index.parent().isValid() ) {
76
flags |= Qt::ItemIsUserCheckable;
81
KConfigGroup SnippetStore::getConfig()
84
* use KTextEditor::Editor session config object
86
return KateGlobal::self()->sessionConfig()->group("Snippets");
89
bool SnippetStore::setData(const QModelIndex& index, const QVariant& value, int role)
91
if ( role == Qt::EditRole && value.toString().isEmpty() ) {
92
// don't allow empty names
95
const bool success = QStandardItemModel::setData(index, value, role);
96
if ( !success || role != Qt::EditRole ) {
100
// when we edited something, save the repository
102
QStandardItem* repoItem = 0;
103
if ( index.parent().isValid() ) {
104
repoItem = itemFromIndex(index.parent());
106
repoItem = itemFromIndex(index);
109
SnippetRepository* repo = dynamic_cast<SnippetRepository*>(repoItem);
116
SnippetRepository* SnippetStore::repositoryForFile(const QString& file)
118
for ( int i = 0; i < rowCount(); ++i ) {
119
if ( SnippetRepository* repo = dynamic_cast<SnippetRepository*>(item(i)) ) {
120
if ( repo->file() == file ) {
128
void SnippetStore::unregisterScript(KTextEditor::TemplateScript* script)
130
if ( m_scriptregistrar ) {
131
m_scriptregistrar->unregisterTemplateScript(script);
135
KTextEditor::TemplateScript* SnippetStore::registerScript(const QString& script)
137
if ( m_scriptregistrar ) {
138
return m_scriptregistrar->registerTemplateScript(this, script);
144
#include "snippetstore.moc"