2010-05-26 10:58:44 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "highlightersettings.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
#include <QtCore/QLatin1String>
|
2010-06-04 10:13:38 +02:00
|
|
|
#include <QtCore/QLatin1Char>
|
|
|
|
|
#include <QtCore/QDebug>
|
2010-05-28 14:37:25 +02:00
|
|
|
#ifdef Q_OS_UNIX
|
2010-05-26 10:58:44 +02:00
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QProcess>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2010-05-28 14:37:25 +02:00
|
|
|
QString findDefinitionsLocation()
|
2010-05-26 10:58:44 +02:00
|
|
|
{
|
2010-05-28 14:37:25 +02:00
|
|
|
#ifdef Q_OS_UNIX
|
2010-09-23 15:38:32 +02:00
|
|
|
static const QLatin1String kateSyntax[] = {
|
|
|
|
|
QLatin1String("/share/apps/katepart/syntax"),
|
|
|
|
|
QLatin1String("/share/kde4/apps/katepart/syntax")
|
|
|
|
|
};
|
|
|
|
|
static const int kateSyntaxCount =
|
|
|
|
|
sizeof(kateSyntax) / sizeof(kateSyntax[0]);
|
2010-05-26 10:58:44 +02:00
|
|
|
|
2010-05-28 14:37:25 +02:00
|
|
|
// Some wild guesses.
|
|
|
|
|
QDir dir;
|
2010-09-23 15:38:32 +02:00
|
|
|
for (unsigned i = 0; i < kateSyntaxCount; ++i) {
|
|
|
|
|
QStringList paths;
|
|
|
|
|
paths << QLatin1String("/usr") + kateSyntax[i]
|
|
|
|
|
<< QLatin1String("/usr/local") + kateSyntax[i]
|
|
|
|
|
<< QLatin1String("/opt") + kateSyntax[i];
|
|
|
|
|
foreach (const QString &path, paths) {
|
|
|
|
|
dir.setPath(path);
|
|
|
|
|
if (dir.exists())
|
|
|
|
|
return dir.path();
|
2010-05-28 14:37:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 15:38:32 +02:00
|
|
|
// Try kde-config.
|
|
|
|
|
QStringList programs;
|
|
|
|
|
programs << QLatin1String("kde-config") << QLatin1String("kde4-config");
|
|
|
|
|
foreach (const QString &program, programs) {
|
|
|
|
|
QProcess process;
|
|
|
|
|
process.start(program, QStringList(QLatin1String("--prefix")));
|
|
|
|
|
if (process.waitForStarted(5000)) {
|
|
|
|
|
process.waitForFinished(5000);
|
|
|
|
|
QString output = QString::fromLocal8Bit(process.readAllStandardOutput());
|
|
|
|
|
output.remove(QLatin1Char('\n'));
|
|
|
|
|
for (unsigned i = 0; i < kateSyntaxCount; ++i) {
|
|
|
|
|
dir.setPath(output + kateSyntax[i]);
|
|
|
|
|
if (dir.exists())
|
|
|
|
|
return dir.path();
|
2010-09-15 16:09:39 +02:00
|
|
|
}
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-09-23 15:38:32 +02:00
|
|
|
return QString();
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2010-08-06 13:10:21 +02:00
|
|
|
static const QLatin1String kDefinitionFilesPath("UserDefinitionFilesPath");
|
|
|
|
|
static const QLatin1String kFallbackDefinitionFilesPath("FallbackDefinitionFilesPath");
|
2010-05-28 14:37:25 +02:00
|
|
|
static const QLatin1String kAlertWhenDefinitionIsNotFound("AlertWhenDefinitionsIsNotFound");
|
2010-08-06 13:10:21 +02:00
|
|
|
static const QLatin1String kUseFallbackLocation("UseFallbackLocation");
|
2010-06-04 10:13:38 +02:00
|
|
|
static const QLatin1String kIgnoredFilesPatterns("IgnoredFilesPatterns");
|
2010-05-26 10:58:44 +02:00
|
|
|
static const QLatin1String kGroupPostfix("HighlighterSettings");
|
|
|
|
|
|
|
|
|
|
QString groupSpecifier(const QString &postFix, const QString &category)
|
|
|
|
|
{
|
|
|
|
|
if (category.isEmpty())
|
|
|
|
|
return postFix;
|
|
|
|
|
return QString(category + postFix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
2010-08-06 13:10:21 +02:00
|
|
|
HighlighterSettings::HighlighterSettings() :
|
|
|
|
|
m_alertWhenNoDefinition(true), m_useFallbackLocation(true)
|
2010-05-26 10:58:44 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::toSettings(const QString &category, QSettings *s) const
|
|
|
|
|
{
|
|
|
|
|
const QString &group = groupSpecifier(kGroupPostfix, category);
|
|
|
|
|
s->beginGroup(group);
|
|
|
|
|
s->setValue(kDefinitionFilesPath, m_definitionFilesPath);
|
2010-08-06 13:10:21 +02:00
|
|
|
s->setValue(kFallbackDefinitionFilesPath, m_fallbackDefinitionFilesPath);
|
2010-05-28 14:37:25 +02:00
|
|
|
s->setValue(kAlertWhenDefinitionIsNotFound, m_alertWhenNoDefinition);
|
2010-08-06 13:10:21 +02:00
|
|
|
s->setValue(kUseFallbackLocation, m_useFallbackLocation);
|
2010-06-04 10:13:38 +02:00
|
|
|
s->setValue(kIgnoredFilesPatterns, ignoredFilesPatterns());
|
2010-05-26 10:58:44 +02:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::fromSettings(const QString &category, QSettings *s)
|
|
|
|
|
{
|
|
|
|
|
const QString &group = groupSpecifier(kGroupPostfix, category);
|
|
|
|
|
s->beginGroup(group);
|
2010-08-06 13:10:21 +02:00
|
|
|
m_definitionFilesPath = s->value(kDefinitionFilesPath, QString()).toString();
|
2010-09-23 16:37:09 +02:00
|
|
|
if (!s->contains(kDefinitionFilesPath))
|
|
|
|
|
m_definitionFilesPath = Core::ICore::instance()->resourcePath() +
|
|
|
|
|
QLatin1String("/generic-highlighter");
|
|
|
|
|
else
|
|
|
|
|
m_definitionFilesPath = s->value(kDefinitionFilesPath).toString();
|
2010-08-06 13:10:21 +02:00
|
|
|
if (!s->contains(kFallbackDefinitionFilesPath))
|
|
|
|
|
m_fallbackDefinitionFilesPath = findDefinitionsLocation();
|
2010-05-26 10:58:44 +02:00
|
|
|
else
|
2010-09-23 16:37:09 +02:00
|
|
|
m_fallbackDefinitionFilesPath = s->value(kFallbackDefinitionFilesPath).toString();
|
2010-05-28 14:37:25 +02:00
|
|
|
m_alertWhenNoDefinition = s->value(kAlertWhenDefinitionIsNotFound, true).toBool();
|
2010-08-06 13:10:21 +02:00
|
|
|
m_useFallbackLocation = s->value(kUseFallbackLocation, true).toBool();
|
2010-06-04 10:13:38 +02:00
|
|
|
if (!s->contains(kIgnoredFilesPatterns))
|
|
|
|
|
assignInitialIgnoredPatterns();
|
|
|
|
|
else
|
|
|
|
|
setIgnoredFilesPatterns(s->value(kIgnoredFilesPatterns, QString()).toString());
|
2010-05-26 10:58:44 +02:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 10:13:38 +02:00
|
|
|
void HighlighterSettings::setIgnoredFilesPatterns(const QString &patterns)
|
|
|
|
|
{
|
|
|
|
|
setExpressionsFromList(patterns.split(QLatin1Char(','), QString::SkipEmptyParts));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString HighlighterSettings::ignoredFilesPatterns() const
|
|
|
|
|
{
|
|
|
|
|
return listFromExpressions().join(QLatin1String(","));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::assignInitialIgnoredPatterns()
|
|
|
|
|
{
|
|
|
|
|
QStringList patterns;
|
|
|
|
|
patterns << QLatin1String("*.txt")
|
|
|
|
|
<< QLatin1String("LICENSE*")
|
|
|
|
|
<< QLatin1String("README")
|
|
|
|
|
<< QLatin1String("INSTALL")
|
|
|
|
|
<< QLatin1String("COPYING")
|
2010-07-22 13:51:14 +02:00
|
|
|
<< QLatin1String("NEWS")
|
|
|
|
|
<< QLatin1String("qmldir");
|
2010-06-04 10:13:38 +02:00
|
|
|
setExpressionsFromList(patterns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HighlighterSettings::isIgnoredFilePattern(const QString &fileName) const
|
|
|
|
|
{
|
|
|
|
|
foreach (const QRegExp ®Exp, m_ignoredFiles)
|
|
|
|
|
if (regExp.indexIn(fileName) != -1)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 10:58:44 +02:00
|
|
|
bool HighlighterSettings::equals(const HighlighterSettings &highlighterSettings) const
|
|
|
|
|
{
|
2010-05-28 14:37:25 +02:00
|
|
|
return m_definitionFilesPath == highlighterSettings.m_definitionFilesPath &&
|
2010-08-06 13:10:21 +02:00
|
|
|
m_fallbackDefinitionFilesPath == highlighterSettings.m_fallbackDefinitionFilesPath &&
|
2010-06-04 10:13:38 +02:00
|
|
|
m_alertWhenNoDefinition == highlighterSettings.m_alertWhenNoDefinition &&
|
2010-08-06 13:10:21 +02:00
|
|
|
m_useFallbackLocation == highlighterSettings.m_useFallbackLocation &&
|
2010-06-04 10:13:38 +02:00
|
|
|
m_ignoredFiles == highlighterSettings.m_ignoredFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::setExpressionsFromList(const QStringList &patterns)
|
|
|
|
|
{
|
|
|
|
|
m_ignoredFiles.clear();
|
|
|
|
|
QRegExp regExp;
|
|
|
|
|
regExp.setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
regExp.setPatternSyntax(QRegExp::Wildcard);
|
|
|
|
|
foreach (const QString &s, patterns) {
|
|
|
|
|
regExp.setPattern(s);
|
|
|
|
|
m_ignoredFiles.append(regExp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList HighlighterSettings::listFromExpressions() const
|
|
|
|
|
{
|
|
|
|
|
QStringList patterns;
|
|
|
|
|
foreach (const QRegExp ®Exp, m_ignoredFiles)
|
|
|
|
|
patterns.append(regExp.pattern());
|
|
|
|
|
return patterns;
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|