• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • mode
katemodemanager.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2001-2010 Christoph Cullmann <cullmann@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 //BEGIN Includes
22 #include "katemodemanager.h"
23 #include "katewildcardmatcher.h"
24 
25 #include "katedocument.h"
26 #include "kateconfig.h"
27 #include "kateview.h"
28 #include "kateglobal.h"
29 #include "katesyntaxmanager.h"
30 #include "katesyntaxdocument.h"
31 
32 #include "ui_filetypeconfigwidget.h"
33 
34 #include <kconfig.h>
35 #include <kmimetype.h>
36 #include <kmimetypechooser.h>
37 #include <kdebug.h>
38 #include <kiconloader.h>
39 #include <knuminput.h>
40 #include <klocale.h>
41 #include <kmenu.h>
42 
43 #include <QtCore/QRegExp>
44 #include <QtGui/QCheckBox>
45 #include <QtGui/QComboBox>
46 #include <QtGui/QGroupBox>
47 
48 #include <QtGui/QLabel>
49 #include <QtGui/QLayout>
50 #include <QtGui/QPushButton>
51 #include <QtGui/QToolButton>
52 #include <kvbox.h>
53 
54 #define KATE_FT_HOWMANY 1024
55 //END Includes
56 
57 KateModeManager::KateModeManager ()
58 {
59  update ();
60 }
61 
62 KateModeManager::~KateModeManager ()
63 {
64  qDeleteAll (m_types);
65 }
66 
67 bool compareKateFileType(const KateFileType* const left, const KateFileType* const right)
68 {
69  int comparison = left->section.compare(right->section, Qt::CaseInsensitive);
70  if (comparison == 0) {
71  comparison = left->name.compare(right->name, Qt::CaseInsensitive);
72  }
73  return comparison < 0;
74 }
75 
76 //
77 // read the types from config file and update the internal list
78 //
79 void KateModeManager::update ()
80 {
81  KConfig config ("katemoderc", KConfig::NoGlobals);
82 
83  QStringList g (config.groupList());
84 
85  qDeleteAll (m_types);
86  m_types.clear ();
87  m_name2Type.clear ();
88  for (int z=0; z < g.count(); z++)
89  {
90  KConfigGroup cg(&config, g[z]);
91 
92  KateFileType *type = new KateFileType ();
93  type->number = z;
94  type->name = g[z];
95  type->section = cg.readEntry ("Section");
96  type->wildcards = cg.readXdgListEntry ("Wildcards");
97  type->mimetypes = cg.readXdgListEntry ("Mimetypes");
98  type->priority = cg.readEntry ("Priority", 0);
99  type->varLine = cg.readEntry ("Variables");
100  type->indenter = cg.readEntry ("Indenter");
101 
102  type->hl = cg.readEntry ("Highlighting");
103 
104  // only for generated types...
105  type->hlGenerated = cg.readEntry ("Highlighting Generated", false);
106  type->version = cg.readEntry ("Highlighting Version");
107 
108  // insert into the list + hash...
109  m_types.append(type);
110  m_name2Type.insert (type->name, type);
111  }
112 
113  // try if the hl stuff is up to date...
114  const KateSyntaxModeList &modes = KateHlManager::self()->syntaxDocument()->modeList();
115  for (int i = 0; i < modes.size(); ++i)
116  {
117  KateFileType *type = 0;
118  bool newType = false;
119  if (m_name2Type.contains (modes[i]->name))
120  type = m_name2Type[modes[i]->name];
121  else
122  {
123  newType = true;
124  type = new KateFileType ();
125  type->name = modes[i]->name;
126  type->priority = 0;
127  m_types.append (type);
128  m_name2Type.insert (type->name, type);
129  }
130 
131  if (newType || type->version != modes[i]->version)
132  {
133  type->name = modes[i]->name;
134  type->section = modes[i]->section;
135  type->wildcards = modes[i]->extension.split (';', QString::SkipEmptyParts);
136  type->mimetypes = modes[i]->mimetype.split (';', QString::SkipEmptyParts);
137  type->priority = modes[i]->priority.toInt();
138  type->version = modes[i]->version;
139  type->indenter = modes[i]->indenter;
140  type->hl = modes[i]->name;
141  type->hlGenerated = true;
142  }
143  }
144 
145  // sort the list...
146  qSort(m_types.begin(), m_types.end(), compareKateFileType);
147 
148  // add the none type...
149  KateFileType *t = new KateFileType ();
150 
151  // DO NOT TRANSLATE THIS, DONE LATER, marked by hlGenerated
152  t->name = "Normal";
153  t->hl = "None";
154  t->hlGenerated = true;
155 
156  m_types.prepend (t);
157 }
158 
159 //
160 // save the given list to config file + update
161 //
162 void KateModeManager::save (const QList<KateFileType *>& v)
163 {
164  KConfig katerc("katemoderc", KConfig::NoGlobals);
165 
166  QStringList newg;
167  foreach (const KateFileType *type, v)
168  {
169  KConfigGroup config(&katerc, type->name);
170 
171  config.writeEntry ("Section", type->section);
172  config.writeXdgListEntry ("Wildcards", type->wildcards);
173  config.writeXdgListEntry ("Mimetypes", type->mimetypes);
174  config.writeEntry ("Priority", type->priority);
175  config.writeEntry ("Indenter", type->indenter);
176 
177  QString varLine = type->varLine;
178  if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
179  varLine.prepend ("kate: ");
180 
181  config.writeEntry ("Variables", varLine);
182 
183  config.writeEntry ("Highlighting", type->hl);
184 
185  // only for generated types...
186  config.writeEntry ("Highlighting Generated", type->hlGenerated);
187  config.writeEntry ("Highlighting Version", type->version);
188 
189  newg << type->name;
190  }
191 
192  foreach (const QString &groupName, katerc.groupList())
193  {
194  if (newg.indexOf (groupName) == -1)
195  {
196  katerc.deleteGroup (groupName);
197  }
198  }
199 
200  katerc.sync ();
201 
202  update ();
203 }
204 
205 QString KateModeManager::fileType (KateDocument *doc, const QString &fileToReadFrom)
206 {
207  kDebug(13020);
208  if (!doc)
209  return "";
210 
211  if (m_types.isEmpty())
212  return "";
213 
214  QString fileName = doc->url().prettyUrl();
215  int length = doc->url().prettyUrl().length();
216 
217  QString result;
218 
219  // Try wildcards
220  if ( ! fileName.isEmpty() )
221  {
222  static const QStringList commonSuffixes = QString(".orig;.new;~;.bak;.BAK").split (';');
223 
224  if (!(result = wildcardsFind(fileName)).isEmpty())
225  return result;
226 
227  QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
228  if (fileName.endsWith(backupSuffix)) {
229  if (!(result = wildcardsFind(fileName.left(length - backupSuffix.length()))).isEmpty())
230  return result;
231  }
232 
233  for (QStringList::ConstIterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
234  if (*it != backupSuffix && fileName.endsWith(*it)) {
235  if (!(result = wildcardsFind(fileName.left(length - (*it).length()))).isEmpty())
236  return result;
237  }
238  }
239  }
240 
241  // Try content-based mimetype
242  KMimeType::Ptr mt;
243  if (!fileToReadFrom.isEmpty()) {
244  int accuracy = 0;
245  mt = KMimeType::findByFileContent(fileToReadFrom, &accuracy);
246  if (!mt)
247  mt = KMimeType::defaultMimeTypePtr();
248  } else {
249  mt = doc->mimeTypeForContent();
250  }
251 
252  QList<KateFileType*> types;
253 
254  foreach (KateFileType *type, m_types)
255  {
256  if (type->mimetypes.indexOf (mt->name()) > -1)
257  types.append (type);
258  }
259 
260  if ( !types.isEmpty() )
261  {
262  int pri = -1;
263  QString name;
264 
265  foreach (KateFileType *type, types)
266  {
267  if (type->priority > pri)
268  {
269  pri = type->priority;
270  name = type->name;
271  }
272  }
273 
274  return name;
275  }
276 
277 
278  return "";
279 }
280 
281 QString KateModeManager::wildcardsFind (const QString &fileName)
282 {
283  KateFileType * match = NULL;
284  int minPrio = -1;
285  foreach (KateFileType *type, m_types)
286  {
287  if (type->priority <= minPrio) {
288  continue;
289  }
290 
291  foreach (const QString &wildcard, type->wildcards)
292  {
293  if (KateWildcardMatcher::exactMatch(fileName, wildcard)) {
294  match = type;
295  minPrio = type->priority;
296  break;
297  }
298  }
299  }
300 
301  return (match == NULL) ? "" : match->name;
302 }
303 
304 const KateFileType& KateModeManager::fileType(const QString &name) const
305 {
306  for (int i = 0; i < m_types.size(); ++i)
307  if (m_types[i]->name == name)
308  return *m_types[i];
309 
310  static KateFileType notype;
311  return notype;
312 }
313 
314 // kate: space-indent on; indent-width 2; replace-tabs on;
QList::clear
void clear()
KateHlManager::syntaxDocument
KateSyntaxDocument * syntaxDocument()
Definition: katesyntaxmanager.h:58
kateview.h
QHash::insert
iterator insert(const Key &key, const T &value)
KateFileType::section
QString section
Definition: katemodemanager.h:39
KateWildcardMatcher::exactMatch
bool exactMatch(const QString &candidate, const QString &wildcard, int candidatePosFromRight, int wildcardPosFromRight, bool caseSensitive=true)
Definition: katewildcardmatcher.cpp:29
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString::prepend
QString & prepend(QChar ch)
KateFileType::hl
QString hl
Definition: katemodemanager.h:44
KateModeManager::~KateModeManager
~KateModeManager()
Definition: katemodemanager.cpp:62
katedocument.h
KateModeManager::save
void save(const QList< KateFileType * > &v)
Definition: katemodemanager.cpp:162
katewildcardmatcher.h
QList::size
int size() const
QRegExp
QList::append
void append(const T &value)
katesyntaxmanager.h
KateDocumentConfig::backupSuffix
const QString & backupSuffix() const
Definition: kateconfig.cpp:1008
KateFileType::wildcards
QStringList wildcards
Definition: katemodemanager.h:40
KateFileType::mimetypes
QStringList mimetypes
Definition: katemodemanager.h:41
katemodemanager.h
kateglobal.h
KateSyntaxDocument::modeList
const KateSyntaxModeList & modeList()
Get the mode list.
Definition: katesyntaxdocument.h:100
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
katesyntaxdocument.h
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
KateFileType::indenter
QString indenter
Definition: katemodemanager.h:47
QString
QList< KateSyntaxModeListItem * >
QStringList
KateFileType::hlGenerated
bool hlGenerated
Definition: katemodemanager.h:45
QHash::clear
void clear()
QList::end
iterator end()
KateFileType
Definition: katemodemanager.h:34
KateDocument
Definition: katedocument.h:74
KateModeManager::update
void update()
File Type Config changed, update all docs (which will take care of views/renderers) ...
Definition: katemodemanager.cpp:79
KateFileType::priority
int priority
Definition: katemodemanager.h:42
compareKateFileType
bool compareKateFileType(const KateFileType *const left, const KateFileType *const right)
Definition: katemodemanager.cpp:67
QList::ConstIterator
typedef ConstIterator
QString::length
int length() const
QString::left
QString left(int n) const
QStringList::indexOf
int indexOf(const QRegExp &rx, int from) const
QList::prepend
void prepend(const T &value)
QHash::contains
bool contains(const Key &key) const
KateDocumentConfig::global
static KateDocumentConfig * global()
Definition: kateconfig.h:165
KateFileType::varLine
QString varLine
Definition: katemodemanager.h:43
KateFileType::name
QString name
Definition: katemodemanager.h:38
KateDocument::mimeTypeForContent
KMimeType::Ptr mimeTypeForContent()
Definition: katedocument.cpp:1874
QString::compare
int compare(const QString &other) const
KateFileType::version
QString version
Definition: katemodemanager.h:46
kateconfig.h
KateFileType::number
int number
Definition: katemodemanager.h:37
QList::begin
iterator begin()
KateHlManager::self
static KateHlManager * self()
Definition: katesyntaxmanager.cpp:103
KateModeManager::KateModeManager
KateModeManager()
Definition: katemodemanager.cpp:57
KateModeManager::fileType
QString fileType(KateDocument *doc, const QString &fileToReadFrom)
get the right fileType for the given document -1 if none found !
Definition: katemodemanager.cpp:205
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal