diff --git a/src/plugins/cpptools/abstracteditorsupport.cpp b/src/plugins/cpptools/abstracteditorsupport.cpp index 64fd65ff849..c1e49a4a1b0 100644 --- a/src/plugins/cpptools/abstracteditorsupport.cpp +++ b/src/plugins/cpptools/abstracteditorsupport.cpp @@ -33,6 +33,9 @@ #include "cppfilesettingspage.h" #include "cppmodelmanager.h" +#include +#include + namespace CppTools { AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager) : @@ -53,7 +56,14 @@ void AbstractEditorSupport::notifyAboutUpdatedContents() const QString AbstractEditorSupport::licenseTemplate(const QString &file, const QString &className) { - return Internal::CppFileSettings::licenseTemplate(file, className); + const QString license = Internal::CppFileSettings::licenseTemplate(); + Utils::MacroExpander expander; + expander.registerVariable("Cpp:License:FileName", tr("The file name."), + [file]() { return Utils::FileName::fromString(file).fileName(); }); + expander.registerVariable("Cpp:License:ClassName", tr("The class name"), + [className]() { return className; }); + + return expander.expand(license); } } // namespace CppTools diff --git a/src/plugins/cpptools/cppfilesettingspage.cpp b/src/plugins/cpptools/cppfilesettingspage.cpp index f0af6904cce..f6766dbd76d 100644 --- a/src/plugins/cpptools/cppfilesettingspage.cpp +++ b/src/plugins/cpptools/cppfilesettingspage.cpp @@ -145,28 +145,26 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const // Replacements of special license template keywords. static bool keyWordReplacement(const QString &keyWord, - const QString &file, - const QString &className, QString *value) { if (keyWord == QLatin1String("%YEAR%")) { - *value = QString::number(QDate::currentDate().year()); + *value = QLatin1String("%{CurrentDate:yyyy}"); return true; } if (keyWord == QLatin1String("%MONTH%")) { - *value = QString::number(QDate::currentDate().month()); + *value = QLatin1String("%{CurrentDate:M}"); return true; } if (keyWord == QLatin1String("%DAY%")) { - *value = QString::number(QDate::currentDate().day()); + *value = QLatin1String("%{CurrentDate:d}"); return true; } if (keyWord == QLatin1String("%CLASS%")) { - *value = className; + *value = QLatin1String("%{Cpp:License:ClassName}"); return true; } if (keyWord == QLatin1String("%FILENAME%")) { - *value = Utils::FileName::fromString(file).fileName(); + *value = QLatin1String("%{Cpp:License:FileName}"); return true; } if (keyWord == QLatin1String("%DATE%")) { @@ -179,17 +177,17 @@ static bool keyWordReplacement(const QString &keyWord, if (format.count(ypsilon) == 2) format.insert(format.indexOf(ypsilon), QString(2, ypsilon)); } - *value = QDate::currentDate().toString(format); + *value = QString::fromLatin1("%{CurrentDate:") + format + QLatin1Char('}'); return true; } if (keyWord == QLatin1String("%USER%")) { - *value = Utils::Environment::systemEnvironment().userName(); + *value = QLatin1String("%{Env:USER}"); return true; } // Environment variables (for example '%$EMAIL%'). if (keyWord.startsWith(QLatin1String("%$"))) { const QString varName = keyWord.mid(2, keyWord.size() - 3); - *value = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit())); + *value = QString::fromLatin1("%{Env:") + varName + QLatin1Char('}'); return true; } return false; @@ -197,10 +195,11 @@ static bool keyWordReplacement(const QString &keyWord, // Parse a license template, scan for %KEYWORD% and replace if known. // Replace '%%' by '%'. -static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, const QString &className) +static void parseLicenseTemplatePlaceholders(QString *t) { int pos = 0; const QChar placeHolder = QLatin1Char('%'); + bool isCompatibilityStyle = false; do { const int placeHolderPos = t->indexOf(placeHolder, pos); if (placeHolderPos == -1) @@ -214,7 +213,8 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co } else { const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos); QString replacement; - if (keyWordReplacement(keyWord, file, className, &replacement)) { + if (keyWordReplacement(keyWord, &replacement)) { + isCompatibilityStyle = true; t->replace(placeHolderPos, keyWord.size(), replacement); pos = placeHolderPos + replacement.size(); } else { @@ -223,12 +223,14 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co } } } while (pos < t->size()); + + if (isCompatibilityStyle) + t->replace(QLatin1Char('\\'), QLatin1String("\\\\")); } // Convenience that returns the formatted license template. -QString CppFileSettings::licenseTemplate(const QString &fileName, const QString &className) +QString CppFileSettings::licenseTemplate() { - const QSettings *s = Core::ICore::settings(); QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP); key += QLatin1Char('/'); @@ -247,7 +249,7 @@ QString CppFileSettings::licenseTemplate(const QString &fileName, const QString licenseStream.setAutoDetectUnicode(true); QString license = licenseStream.readAll(); - parseLicenseTemplatePlaceholders(&license, fileName, className); + parseLicenseTemplatePlaceholders(&license); // Ensure at least one newline at the end of the license template to separate it from the code const QChar newLine = QLatin1Char('\n'); diff --git a/src/plugins/cpptools/cppfilesettingspage.h b/src/plugins/cpptools/cppfilesettingspage.h index 84ad6529a92..855aefb7d0c 100644 --- a/src/plugins/cpptools/cppfilesettingspage.h +++ b/src/plugins/cpptools/cppfilesettingspage.h @@ -65,7 +65,7 @@ struct CppFileSettings // Convenience to return a license template completely formatted. // Currently made public in - static QString licenseTemplate(const QString &file = QString(), const QString &className = QString()); + static QString licenseTemplate(); bool equals(const CppFileSettings &rhs) const; bool operator==(const CppFileSettings &s) const { return equals(s); }