• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

katemodemanager.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 //BEGIN Includes
00020 #include "katemodemanager.h"
00021 #include "katewildcardmatcher.h"
00022 
00023 #include "katedocument.h"
00024 #include "kateconfig.h"
00025 #include "kateview.h"
00026 #include "kateglobal.h"
00027 #include "katesyntaxmanager.h"
00028 #include "katesyntaxdocument.h"
00029 
00030 #include "ui_filetypeconfigwidget.h"
00031 
00032 #include <kconfig.h>
00033 #include <kmimetype.h>
00034 #include <kmimetypechooser.h>
00035 #include <kdebug.h>
00036 #include <kiconloader.h>
00037 #include <knuminput.h>
00038 #include <klocale.h>
00039 #include <kmenu.h>
00040 
00041 #include <QtCore/QRegExp>
00042 #include <QtGui/QCheckBox>
00043 #include <QtGui/QComboBox>
00044 #include <QtGui/QGroupBox>
00045 
00046 #include <QtGui/QLabel>
00047 #include <QtGui/QLayout>
00048 #include <QtGui/QPushButton>
00049 #include <QtGui/QToolButton>
00050 #include <kvbox.h>
00051 
00052 #define KATE_FT_HOWMANY 1024
00053 //END Includes
00054 
00055 KateModeManager::KateModeManager ()
00056 {
00057   update ();
00058 }
00059 
00060 KateModeManager::~KateModeManager ()
00061 {
00062   qDeleteAll (m_types);
00063 }
00064 
00065 //
00066 // read the types from config file and update the internal list
00067 //
00068 void KateModeManager::update ()
00069 {
00070   KConfig config ("katemoderc", KConfig::NoGlobals);
00071 
00072   QStringList g (config.groupList());
00073 
00074   qDeleteAll (m_types);
00075   m_types.clear ();
00076   m_name2Type.clear ();
00077   for (int z=0; z < g.count(); z++)
00078   {
00079     KConfigGroup cg(&config, g[z]);
00080 
00081     KateFileType *type = new KateFileType ();
00082     type->number = z;
00083     type->name = g[z];
00084     type->section = cg.readEntry ("Section");
00085     type->wildcards = cg.readXdgListEntry ("Wildcards");
00086     type->mimetypes = cg.readXdgListEntry ("Mimetypes");
00087     type->priority = cg.readEntry ("Priority", 0);
00088     type->varLine = cg.readEntry ("Variables");
00089     type->indenter = cg.readEntry ("Indenter");
00090     
00091     type->hl = cg.readEntry ("Highlighting");
00092    
00093     // only for generated types...
00094     type->hlGenerated = cg.readEntry ("Highlighting Generated", false);
00095     type->version = cg.readEntry ("Highlighting Version");
00096  
00097     // insert into the list + hash...
00098     m_types.append(type);
00099     m_name2Type.insert (type->name, type);
00100   }
00101   
00102   // try if the hl stuff is up to date...
00103   const KateSyntaxModeList &modes = KateHlManager::self()->syntaxDocument()->modeList();
00104   for (int i = 0; i < modes.size(); ++i)
00105   {
00106     KateFileType *type = 0;
00107     bool newType = false;
00108     if (m_name2Type.contains (modes[i]->name))
00109       type = m_name2Type[modes[i]->name];
00110     else
00111     {
00112       newType = true;
00113       type = new KateFileType ();
00114       type->name = modes[i]->name;
00115       type->priority = 0;
00116       m_types.append (type);
00117       m_name2Type.insert (type->name, type);
00118     }
00119 
00120     if (newType || type->version != modes[i]->version)
00121     {
00122       type->name = modes[i]->name;
00123       type->section = modes[i]->section;
00124       type->wildcards = modes[i]->extension.split (';', QString::SkipEmptyParts);
00125       type->mimetypes = modes[i]->mimetype.split (';', QString::SkipEmptyParts);
00126       type->priority = modes[i]->priority.toInt();
00127       type->version = modes[i]->version;
00128       type->indenter = modes[i]->indenter;
00129       type->hl = modes[i]->name;
00130       type->hlGenerated = true;
00131     }
00132   }
00133 
00134   // sort the list...
00135   QList<KateFileType *> newList;  
00136   for (int i=0; i < m_types.count(); i++)
00137   {
00138     KateFileType *t = m_types[i];
00139 
00140     int insert = 0;
00141     for (; insert <= newList.count(); insert++)
00142     {
00143       if (insert == newList.count())
00144         break;
00145 
00146       if ( QString(newList.at(insert)->section + newList.at(insert)->name).toLower()
00147             > QString(t->section + t->name).toLower() )
00148         break;
00149     }
00150 
00151     newList.insert (insert, t);
00152   }
00153 
00154   // replace old list with new sorted list...
00155   m_types = newList;
00156 
00157   // add the none type...
00158   KateFileType *t = new KateFileType ();
00159   t->name = "Normal";
00160   t->hl = "None";
00161   t->hlGenerated = true;
00162 
00163   m_types.prepend (t);
00164 }
00165 
00166 //
00167 // save the given list to config file + update
00168 //
00169 void KateModeManager::save (const QList<KateFileType *>& v)
00170 {
00171   KConfig katerc("katemoderc", KConfig::NoGlobals);
00172   KConfigGroup config(&katerc, QString());
00173 
00174   QStringList newg;
00175   foreach (const KateFileType *type, v)
00176   {
00177     config.changeGroup(type->name);
00178 
00179     config.writeEntry ("Section", type->section);
00180     config.writeXdgListEntry ("Wildcards", type->wildcards);
00181     config.writeXdgListEntry ("Mimetypes", type->mimetypes);
00182     config.writeEntry ("Priority", type->priority);
00183     config.writeEntry ("Indenter", type->indenter);
00184 
00185     QString varLine = type->varLine;
00186     if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
00187       varLine.prepend ("kate: ");
00188 
00189     config.writeEntry ("Variables", varLine);
00190     
00191     config.writeEntry ("Highlighting", type->hl);
00192     
00193     // only for generated types...
00194     config.writeEntry ("Highlighting Generated", type->hlGenerated);
00195     config.writeEntry ("Highlighting Version", type->version);
00196 
00197     newg << type->name;
00198   }
00199 
00200   QStringList g (katerc.groupList());
00201 
00202   for (int z=0; z < g.count(); z++)
00203   {
00204     if (newg.indexOf (g[z]) == -1)
00205     {
00206       katerc.deleteGroup (g[z]);
00207     }
00208   }
00209 
00210   config.sync ();
00211 
00212   update ();
00213 }
00214 
00215 QString KateModeManager::fileType (KateDocument *doc)
00216 {
00217   kDebug(13020);
00218   if (!doc)
00219     return "";
00220 
00221   if (m_types.isEmpty())
00222     return "";
00223 
00224   QString fileName = doc->url().prettyUrl();
00225   int length = doc->url().prettyUrl().length();
00226 
00227   QString result;
00228 
00229   // Try wildcards
00230   if ( ! fileName.isEmpty() )
00231   {
00232     static const QStringList commonSuffixes = QString(".orig;.new;~;.bak;.BAK").split (';');
00233 
00234     if (!(result = wildcardsFind(fileName)).isEmpty())
00235       return result;
00236 
00237     QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
00238     if (fileName.endsWith(backupSuffix)) {
00239       if (!(result = wildcardsFind(fileName.left(length - backupSuffix.length()))).isEmpty())
00240         return result;
00241     }
00242 
00243     for (QStringList::ConstIterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
00244       if (*it != backupSuffix && fileName.endsWith(*it)) {
00245         if (!(result = wildcardsFind(fileName.left(length - (*it).length()))).isEmpty())
00246           return result;
00247       }
00248     }
00249   }
00250 
00251   // Try content-based mimetype
00252   KMimeType::Ptr mt = doc->mimeTypeForContent();
00253 
00254   QList<KateFileType*> types;
00255 
00256   foreach (KateFileType *type, m_types)
00257   {
00258     if (type->mimetypes.indexOf (mt->name()) > -1)
00259       types.append (type);
00260   }
00261 
00262   if ( !types.isEmpty() )
00263   {
00264     int pri = -1;
00265     QString name;
00266 
00267     foreach (KateFileType *type, types)
00268     {
00269       if (type->priority > pri)
00270       {
00271         pri = type->priority;
00272         name = type->name;
00273       }
00274     }
00275 
00276     return name;
00277   }
00278 
00279 
00280   return "";
00281 }
00282 
00283 QString KateModeManager::wildcardsFind (const QString &fileName)
00284 {
00285   KateFileType * match = NULL;
00286   int minPrio = -1;
00287   foreach (KateFileType *type, m_types)
00288   {
00289     if (type->priority <= minPrio) {
00290       continue;
00291     }
00292 
00293     foreach (const QString &wildcard, type->wildcards)
00294     {
00295       if (KateWildcardMatcher::exactMatch(fileName, wildcard)) {
00296         match = type;
00297         minPrio = type->priority;
00298         break;
00299       }
00300     }
00301   }
00302 
00303   return (match == NULL) ? "" : match->name;
00304 }
00305 
00306 const KateFileType& KateModeManager::fileType(const QString &name) const
00307 {
00308   for (int i = 0; i < m_types.size(); ++i)
00309     if (m_types[i]->name == name)
00310       return *m_types[i];
00311       
00312   static KateFileType notype;
00313   return notype;
00314 }
00315 
00316 // kate: space-indent on; indent-width 2; replace-tabs on;

Kate

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal