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 "katemodemanager.moc"
00022 #include "katewildcardmatcher.h"
00023
00024 #include "katedocument.h"
00025 #include "kateconfig.h"
00026 #include "kateview.h"
00027 #include "kateglobal.h"
00028 #include "katesyntaxmanager.h"
00029 #include "katesyntaxdocument.h"
00030
00031 #include "ui_filetypeconfigwidget.h"
00032
00033 #include <kconfig.h>
00034 #include <kmimetype.h>
00035 #include <kmimetypechooser.h>
00036 #include <kdebug.h>
00037 #include <kiconloader.h>
00038 #include <knuminput.h>
00039 #include <klocale.h>
00040 #include <kmenu.h>
00041
00042 #include <QtCore/QRegExp>
00043 #include <QtGui/QCheckBox>
00044 #include <QtGui/QComboBox>
00045 #include <QtGui/QGroupBox>
00046
00047 #include <QtGui/QLabel>
00048 #include <QtGui/QLayout>
00049 #include <QtGui/QLineEdit>
00050 #include <QtGui/QPushButton>
00051 #include <QtGui/QToolButton>
00052 #include <kvbox.h>
00053
00054 #define KATE_FT_HOWMANY 1024
00055
00056
00057 KateModeManager::KateModeManager ()
00058 {
00059 update ();
00060 }
00061
00062 KateModeManager::~KateModeManager ()
00063 {
00064 qDeleteAll (m_types);
00065 }
00066
00067
00068
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
00092 type->hl = cg.readEntry ("Highlighting");
00093
00094
00095 type->hlGenerated = cg.readEntry ("Highlighting Generated", false);
00096 type->version = cg.readEntry ("Highlighting Version");
00097
00098
00099 m_types.append(type);
00100 m_name2Type.insert (type->name, type);
00101 }
00102
00103
00104 const KateSyntaxModeList &modes = KateHlManager::self()->syntaxDocument()->modeList();
00105 for (int i = 0; i < modes.size(); ++i)
00106 {
00107 KateFileType *type = 0;
00108 bool newType = false;
00109 if (m_name2Type.contains (modes[i]->name))
00110 type = m_name2Type[modes[i]->name];
00111 else
00112 {
00113 newType = true;
00114 type = new KateFileType ();
00115 type->name = modes[i]->name;
00116 type->priority = 0;
00117 m_types.append (type);
00118 m_name2Type.insert (type->name, type);
00119 }
00120
00121 if (newType || type->version != modes[i]->version)
00122 {
00123 type->name = modes[i]->name;
00124 type->section = modes[i]->section;
00125 type->wildcards = modes[i]->extension.split (';', QString::SkipEmptyParts);
00126 type->mimetypes = modes[i]->mimetype.split (';', QString::SkipEmptyParts);
00127 type->priority = modes[i]->priority.toInt();
00128 type->version = modes[i]->version;
00129 type->hl = modes[i]->name;
00130 type->hlGenerated = true;
00131 }
00132 }
00133
00134
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
00155 m_types = newList;
00156
00157
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
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
00184 QString varLine = type->varLine;
00185 if (QRegExp("kate:(.*)").indexIn(varLine) < 0)
00186 varLine.prepend ("kate: ");
00187
00188 config.writeEntry ("Variables", varLine);
00189
00190 config.writeEntry ("Highlighting", type->hl);
00191
00192
00193 config.writeEntry ("Highlighting Generated", type->hlGenerated);
00194 config.writeEntry ("Highlighting Version", type->version);
00195
00196 newg << type->name;
00197 }
00198
00199 QStringList g (katerc.groupList());
00200
00201 for (int z=0; z < g.count(); z++)
00202 {
00203 if (newg.indexOf (g[z]) == -1)
00204 {
00205 katerc.deleteGroup (g[z]);
00206 }
00207 }
00208
00209 config.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
00229 if ( ! fileName.isEmpty() )
00230 {
00231 static 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::Iterator 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
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