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