2010-05-26 10:58:44 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "highlightersettings.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
#include <QtCore/QLatin1String>
|
2010-06-04 10:13:38 +02:00
|
|
|
#include <QtCore/QLatin1Char>
|
2010-05-26 10:58:44 +02:00
|
|
|
#include <QtCore/QDir>
|
2010-09-29 13:52:58 +02:00
|
|
|
#include <QtCore/QFile>
|
2010-11-09 16:01:58 +01:00
|
|
|
#include <QtCore/QStringList>
|
2010-09-29 13:52:58 +02:00
|
|
|
#ifdef Q_OS_UNIX
|
2010-05-26 10:58:44 +02:00
|
|
|
#include <QtCore/QProcess>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2010-11-09 16:01:58 +01:00
|
|
|
QString findFallbackDefinitionsLocation()
|
2010-05-26 10:58:44 +02:00
|
|
|
{
|
2010-11-09 16:01:58 +01:00
|
|
|
QDir dir;
|
|
|
|
|
dir.setNameFilters(QStringList(QLatin1String("*.xml")));
|
|
|
|
|
|
2010-11-01 09:46:55 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
|
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.
|
2010-09-24 10:10:55 +02:00
|
|
|
for (int i = 0; i < kateSyntaxCount; ++i) {
|
2010-09-23 15:38:32 +02:00
|
|
|
QStringList paths;
|
|
|
|
|
paths << QLatin1String("/usr") + kateSyntax[i]
|
|
|
|
|
<< QLatin1String("/usr/local") + kateSyntax[i]
|
|
|
|
|
<< QLatin1String("/opt") + kateSyntax[i];
|
|
|
|
|
foreach (const QString &path, paths) {
|
|
|
|
|
dir.setPath(path);
|
2010-11-09 16:01:58 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
2010-09-23 15:38:32 +02:00
|
|
|
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'));
|
2010-09-24 10:10:55 +02:00
|
|
|
for (int i = 0; i < kateSyntaxCount; ++i) {
|
2010-09-23 15:38:32 +02:00
|
|
|
dir.setPath(output + kateSyntax[i]);
|
2010-11-09 16:01:58 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
2010-09-23 15:38:32 +02:00
|
|
|
return dir.path();
|
2010-09-15 16:09:39 +02:00
|
|
|
}
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-01-24 15:36:40 +01:00
|
|
|
dir.setPath(Core::ICore::resourcePath() + QLatin1String("/generic-highlighter"));
|
2010-11-09 16:01:58 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
|
|
|
|
return dir.path();
|
|
|
|
|
|
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))
|
2010-09-29 13:52:58 +02:00
|
|
|
assignDefaultDefinitionsPath();
|
2010-09-23 16:37:09 +02:00
|
|
|
else
|
|
|
|
|
m_definitionFilesPath = s->value(kDefinitionFilesPath).toString();
|
2010-09-23 16:38:53 +02:00
|
|
|
if (!s->contains(kFallbackDefinitionFilesPath)) {
|
2010-11-09 16:01:58 +01:00
|
|
|
m_fallbackDefinitionFilesPath = findFallbackDefinitionsLocation();
|
2010-09-23 16:38:53 +02:00
|
|
|
if (m_fallbackDefinitionFilesPath.isEmpty())
|
|
|
|
|
m_useFallbackLocation = false;
|
|
|
|
|
else
|
|
|
|
|
m_useFallbackLocation = true;
|
|
|
|
|
} else {
|
2010-09-23 16:37:09 +02:00
|
|
|
m_fallbackDefinitionFilesPath = s->value(kFallbackDefinitionFilesPath).toString();
|
2010-09-23 16:38:53 +02:00
|
|
|
m_useFallbackLocation = s->value(kUseFallbackLocation, true).toBool();
|
|
|
|
|
}
|
2010-05-28 14:37:25 +02:00
|
|
|
m_alertWhenNoDefinition = s->value(kAlertWhenDefinitionIsNotFound, true).toBool();
|
2010-06-04 10:13:38 +02:00
|
|
|
if (!s->contains(kIgnoredFilesPatterns))
|
2010-09-29 13:52:58 +02:00
|
|
|
assignDefaultIgnoredPatterns();
|
2010-06-04 10:13:38 +02:00
|
|
|
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(","));
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-29 13:52:58 +02:00
|
|
|
void HighlighterSettings::assignDefaultIgnoredPatterns()
|
2010-06-04 10:13:38 +02:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-29 13:52:58 +02:00
|
|
|
void HighlighterSettings::assignDefaultDefinitionsPath()
|
|
|
|
|
{
|
|
|
|
|
const QString &path =
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ICore::userResourcePath() + QLatin1String("/generic-highlighter");
|
2010-09-29 13:52:58 +02:00
|
|
|
if (QFile::exists(path) || QDir().mkpath(path))
|
|
|
|
|
m_definitionFilesPath = path;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 10:13:38 +02:00
|
|
|
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
|
|
|
}
|