Kate
katemodemanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00054
00055 KateModeManager::KateModeManager ()
00056 {
00057 update ();
00058 }
00059
00060 KateModeManager::~KateModeManager ()
00061 {
00062 qDeleteAll (m_types);
00063 }
00064
00065
00066
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
00090 type->hl = cg.readEntry ("Highlighting");
00091
00092
00093 type->hlGenerated = cg.readEntry ("Highlighting Generated", false);
00094 type->version = cg.readEntry ("Highlighting Version");
00095
00096
00097 m_types.append(type);
00098 m_name2Type.insert (type->name, type);
00099 }
00100
00101
00102 const KateSyntaxModeList &modes = KateHlManager::self()->syntaxDocument()->modeList();
00103 for (int i = 0; i < modes.size(); ++i)
00104 {
00105 KateFileType *type = 0;
00106 bool newType = false;
00107 if (m_name2Type.contains (modes[i]->name))
00108 type = m_name2Type[modes[i]->name];
00109 else
00110 {
00111 newType = true;
00112 type = new KateFileType ();
00113 type->name = modes[i]->name;
00114 type->priority = 0;
00115 m_types.append (type);
00116 m_name2Type.insert (type->name, type);
00117 }
00118
00119 if (newType || type->version != modes[i]->version)
00120 {
00121 type->name = modes[i]->name;
00122 type->section = modes[i]->section;
00123 type->wildcards = modes[i]->extension.split (';', QString::SkipEmptyParts);
00124 type->mimetypes = modes[i]->mimetype.split (';', QString::SkipEmptyParts);
00125 type->priority = modes[i]->priority.toInt();
00126 type->version = modes[i]->version;
00127 type->hl = modes[i]->name;
00128 type->hlGenerated = true;
00129 }
00130 }
00131
00132
00133 QList<KateFileType *> newList;
00134 for (int i=0; i < m_types.count(); i++)
00135 {
00136 KateFileType *t = m_types[i];
00137
00138 int insert = 0;
00139 for (; insert <= newList.count(); insert++)
00140 {
00141 if (insert == newList.count())
00142 break;
00143
00144 if ( QString(newList.at(insert)->section + newList.at(insert)->name).toLower()
00145 > QString(t->section + t->name).toLower() )
00146 break;
00147 }
00148
00149 newList.insert (insert, t);
00150 }
00151
00152
00153 m_types = newList;
00154
00155
00156 KateFileType *t = new KateFileType ();
00157 t->name = "Normal";
00158 t->hl = "None";
00159 t->hlGenerated = true;
00160
00161 m_types.prepend (t);
00162 }
00163
00164
00165
00166
00167 void KateModeManager::save (const QList<KateFileType *>& v)
00168 {
00169 KConfig katerc("katemoderc", KConfig::NoGlobals);
00170 KConfigGroup config(&katerc, QString());
00171
00172 QStringList newg;
00173 foreach (const KateFileType *type, v)
00174 {
00175 config.changeGroup(type->name);
00176
00177 config.writeEntry ("Section", type->section);
00178 config.writeXdgListEntry ("Wildcards", type->wildcards);
00179 config.writeXdgListEntry ("Mimetypes", type->mimetypes);
00180 config.writeEntry ("Priority", type->priority);
00181
00182 QString varLine = type->varLine;
00183 if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
00184 varLine.prepend ("kate: ");
00185
00186 config.writeEntry ("Variables", varLine);
00187
00188 config.writeEntry ("Highlighting", type->hl);
00189
00190
00191 config.writeEntry ("Highlighting Generated", type->hlGenerated);
00192 config.writeEntry ("Highlighting Version", type->version);
00193
00194 newg << type->name;
00195 }
00196
00197 QStringList g (katerc.groupList());
00198
00199 for (int z=0; z < g.count(); z++)
00200 {
00201 if (newg.indexOf (g[z]) == -1)
00202 {
00203 katerc.deleteGroup (g[z]);
00204 }
00205 }
00206
00207 config.sync ();
00208
00209 update ();
00210 }
00211
00212 QString KateModeManager::fileType (KateDocument *doc)
00213 {
00214 kDebug(13020);
00215 if (!doc)
00216 return "";
00217
00218 if (m_types.isEmpty())
00219 return "";
00220
00221 QString fileName = doc->url().prettyUrl();
00222 int length = doc->url().prettyUrl().length();
00223
00224 QString result;
00225
00226
00227 if ( ! fileName.isEmpty() )
00228 {
00229 static const QStringList commonSuffixes = QString(".orig;.new;~;.bak;.BAK").split (';');
00230
00231 if (!(result = wildcardsFind(fileName)).isEmpty())
00232 return result;
00233
00234 QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
00235 if (fileName.endsWith(backupSuffix)) {
00236 if (!(result = wildcardsFind(fileName.left(length - backupSuffix.length()))).isEmpty())
00237 return result;
00238 }
00239
00240 for (QStringList::ConstIterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
00241 if (*it != backupSuffix && fileName.endsWith(*it)) {
00242 if (!(result = wildcardsFind(fileName.left(length - (*it).length()))).isEmpty())
00243 return result;
00244 }
00245 }
00246 }
00247
00248
00249 KMimeType::Ptr mt = doc->mimeTypeForContent();
00250
00251 QList<KateFileType*> types;
00252
00253 foreach (KateFileType *type, m_types)
00254 {
00255 if (type->mimetypes.indexOf (mt->name()) > -1)
00256 types.append (type);
00257 }
00258
00259 if ( !types.isEmpty() )
00260 {
00261 int pri = -1;
00262 QString name;
00263
00264 foreach (KateFileType *type, types)
00265 {
00266 if (type->priority > pri)
00267 {
00268 pri = type->priority;
00269 name = type->name;
00270 }
00271 }
00272
00273 return name;
00274 }
00275
00276
00277 return "";
00278 }
00279
00280 QString KateModeManager::wildcardsFind (const QString &fileName)
00281 {
00282 KateFileType * match = NULL;
00283 int minPrio = -1;
00284 foreach (KateFileType *type, m_types)
00285 {
00286 if (type->priority <= minPrio) {
00287 continue;
00288 }
00289
00290 foreach (const QString &wildcard, type->wildcards)
00291 {
00292 if (KateWildcardMatcher::exactMatch(fileName, wildcard)) {
00293 match = type;
00294 minPrio = type->priority;
00295 break;
00296 }
00297 }
00298 }
00299
00300 return (match == NULL) ? "" : match->name;
00301 }
00302
00303 const KateFileType& KateModeManager::fileType(const QString &name) const
00304 {
00305 for (int i = 0; i < m_types.size(); ++i)
00306 if (m_types[i]->name == name)
00307 return *m_types[i];
00308
00309 static KateFileType notype;
00310 return notype;
00311 }
00312
00313