~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to part/snippet/snippetstore.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the Kate project.
2
 
 *  Based on the snippet plugin from KDevelop 4.
3
 
 *
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>
7
 
 *
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.
12
 
 *
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.
17
 
 *
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.
22
 
 */
23
 
 
24
 
#include "snippetstore.h"
25
 
 
26
 
#include "katesnippetglobal.h"
27
 
#include "snippetrepository.h"
28
 
 
29
 
#include <KStandardDirs>
30
 
#include <KDebug>
31
 
 
32
 
#include <ktexteditor/editor.h>
33
 
#include <ktexteditor/templateinterface2.h>
34
 
 
35
 
SnippetStore* SnippetStore::m_self = 0;
36
 
 
37
 
SnippetStore::SnippetStore(KateSnippetGlobal* plugin)
38
 
    : m_plugin(plugin), m_scriptregistrar(0)
39
 
{
40
 
    m_self = this;
41
 
 
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);
46
 
 
47
 
    foreach(const QString& file, list ) {
48
 
        SnippetRepository* repo = new SnippetRepository(file);
49
 
        appendRow(repo);
50
 
    }
51
 
 
52
 
    m_scriptregistrar = KateGlobal::self();
53
 
}
54
 
 
55
 
SnippetStore::~SnippetStore()
56
 
{
57
 
    invisibleRootItem()->removeRows( 0, invisibleRootItem()->rowCount() );
58
 
    m_self = 0;
59
 
}
60
 
 
61
 
void SnippetStore::init(KateSnippetGlobal* plugin)
62
 
{
63
 
    Q_ASSERT(!SnippetStore::self());
64
 
    new SnippetStore(plugin);
65
 
}
66
 
 
67
 
SnippetStore* SnippetStore::self()
68
 
{
69
 
    return m_self;
70
 
}
71
 
 
72
 
Qt::ItemFlags SnippetStore::flags(const QModelIndex & index) const
73
 
{
74
 
    Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
75
 
    if ( !index.parent().isValid() ) {
76
 
        flags |= Qt::ItemIsUserCheckable;
77
 
    }
78
 
    return flags;
79
 
}
80
 
 
81
 
KConfigGroup SnippetStore::getConfig()
82
 
{
83
 
  /**
84
 
   * use KTextEditor::Editor session config object
85
 
   */
86
 
  return KateGlobal::self()->sessionConfig()->group("Snippets");
87
 
}
88
 
 
89
 
bool SnippetStore::setData(const QModelIndex& index, const QVariant& value, int role)
90
 
{
91
 
    if ( role == Qt::EditRole && value.toString().isEmpty() ) {
92
 
        // don't allow empty names
93
 
        return false;
94
 
    }
95
 
    const bool success = QStandardItemModel::setData(index, value, role);
96
 
    if ( !success || role != Qt::EditRole ) {
97
 
        return success;
98
 
    }
99
 
 
100
 
    // when we edited something, save the repository
101
 
 
102
 
    QStandardItem* repoItem = 0;
103
 
    if ( index.parent().isValid() ) {
104
 
        repoItem = itemFromIndex(index.parent());
105
 
    } else {
106
 
        repoItem = itemFromIndex(index);
107
 
    }
108
 
 
109
 
    SnippetRepository* repo = dynamic_cast<SnippetRepository*>(repoItem);
110
 
    if ( repo ) {
111
 
        repo->save();
112
 
    }
113
 
    return true;
114
 
}
115
 
 
116
 
SnippetRepository* SnippetStore::repositoryForFile(const QString& file)
117
 
{
118
 
    for ( int i = 0; i < rowCount(); ++i ) {
119
 
        if ( SnippetRepository* repo = dynamic_cast<SnippetRepository*>(item(i)) ) {
120
 
            if ( repo->file() == file ) {
121
 
                return repo;
122
 
            }
123
 
        }
124
 
    }
125
 
    return 0;
126
 
}
127
 
 
128
 
void SnippetStore::unregisterScript(KTextEditor::TemplateScript* script)
129
 
{
130
 
    if ( m_scriptregistrar ) {
131
 
        m_scriptregistrar->unregisterTemplateScript(script);
132
 
    }
133
 
}
134
 
 
135
 
KTextEditor::TemplateScript* SnippetStore::registerScript(const QString& script)
136
 
{
137
 
    if ( m_scriptregistrar ) {
138
 
        return m_scriptregistrar->registerTemplateScript(this, script);
139
 
    }
140
 
    return 0;
141
 
}
142
 
 
143
 
 
144
 
#include "snippetstore.moc"