aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cpptools/cppfilesettingspage.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <[email protected]>2009-06-22 16:22:15 +0200
committerFriedemann Kleint <[email protected]>2009-06-22 16:22:15 +0200
commit696cfa043215d4140b0d9c5ac2fdb8b8789b92c9 (patch)
tree4ff1cd4d7e733d73370217c5f060128495e8c915 /src/plugins/cpptools/cppfilesettingspage.cpp
parent1b168b686d8eaeca536891096d7e1620331b87f9 (diff)
Add a license template setting to the CppTools settings.
Acked-by: con <[email protected]>
Diffstat (limited to 'src/plugins/cpptools/cppfilesettingspage.cpp')
-rw-r--r--src/plugins/cpptools/cppfilesettingspage.cpp149
1 files changed, 148 insertions, 1 deletions
diff --git a/src/plugins/cpptools/cppfilesettingspage.cpp b/src/plugins/cpptools/cppfilesettingspage.cpp
index 11f776784d3..f3e3e574c8a 100644
--- a/src/plugins/cpptools/cppfilesettingspage.cpp
+++ b/src/plugins/cpptools/cppfilesettingspage.cpp
@@ -32,15 +32,33 @@
#include "ui_cppfilesettingspage.h"
#include <coreplugin/icore.h>
+#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/mimedatabase.h>
+#include <cppeditor/cppeditorconstants.h>
+
#include <extensionsystem/pluginmanager.h>
#include <QtCore/QSettings>
#include <QtCore/QDebug>
+#include <QtCore/QFile>
#include <QtCore/QCoreApplication>
+#include <QtCore/QDate>
+#include <QtCore/QLocale>
+
+#include <QtGui/QFileDialog>
+#include <QtGui/QMessageBox>
static const char *headerSuffixKeyC = "HeaderSuffix";
static const char *sourceSuffixKeyC = "SourceSuffix";
+static const char *licenseTemplatePathKeyC = "LicenseTemplate";
+
+const char *licenseTemplateTemplate = QT_TRANSLATE_NOOP("CppTools::Internal::CppFileSettingsWidget",
+"/**************************************************************************\n"
+"** Qt Creator license header template\n"
+"** Special keywords: %USER% %DATE% %YEAR%\n"
+"** Environment variables: %$VARIABLE%\n"
+"** To protect a percent sign, use '%%'.\n"
+"**************************************************************************/\n");
namespace CppTools {
namespace Internal {
@@ -56,6 +74,7 @@ void CppFileSettings::toSettings(QSettings *s) const
s->setValue(QLatin1String(headerSuffixKeyC), headerSuffix);
s->setValue(QLatin1String(sourceSuffixKeyC), sourceSuffix);
s->setValue(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), lowerCaseFiles);
+ s->setValue(QLatin1String(licenseTemplatePathKeyC), licenseTemplatePath);
s->endGroup();
}
@@ -66,6 +85,7 @@ void CppFileSettings::fromSettings(QSettings *s)
sourceSuffix = s->value(QLatin1String(sourceSuffixKeyC), QLatin1String("cpp")).toString();
const bool lowerCaseDefault = Constants::lowerCaseFilesDefault;
lowerCaseFiles = s->value(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), QVariant(lowerCaseDefault)).toBool();
+ licenseTemplatePath = s->value(QLatin1String(licenseTemplatePathKeyC), QString()).toString();
s->endGroup();
}
@@ -80,7 +100,96 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const
{
return lowerCaseFiles == rhs.lowerCaseFiles
&& headerSuffix == rhs.headerSuffix
- && sourceSuffix == rhs.sourceSuffix;
+ && sourceSuffix == rhs.sourceSuffix
+ && licenseTemplatePath == rhs.licenseTemplatePath;
+}
+
+// Replacements of special license template keywords.
+static QString keyWordReplacement(const QString &keyWord)
+{
+ if (keyWord == QLatin1String("%YEAR%")) {
+ return QString::number(QDate::currentDate().year());
+ }
+ if (keyWord == QLatin1String("%DATE%")) {
+ static QString format;
+ // ensure a format with 4 year digits. Some have locales have 2.
+ if (format.isEmpty()) {
+ QLocale loc;
+ format = loc.dateFormat(QLocale::ShortFormat);
+ const QChar ypsilon = QLatin1Char('y');
+ if (format.count(ypsilon) == 2)
+ format.insert(format.indexOf(ypsilon), QString(2, ypsilon));
+ }
+ return QDate::currentDate().toString(format);
+ }
+ if (keyWord == QLatin1String("%USER%")) {
+#ifdef Q_OS_WIN
+ return QString::fromLocal8Bit(qgetenv("USERNAME"));
+#else
+ return QString::fromLocal8Bit(qgetenv("USER"));
+#endif
+ }
+ // Environment variables (for example '%$EMAIL%').
+ if (keyWord.startsWith(QLatin1String("%$"))) {
+ const QString varName = keyWord.mid(2, keyWord.size() - 3);
+ return QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit()));
+ }
+ return QString();
+}
+
+// Parse a license template, scan for %KEYWORD% and replace if known.
+// Replace '%%' by '%'.
+static void parseLicenseTemplatePlaceholders(QString *t)
+{
+ int pos = 0;
+ const QChar placeHolder = QLatin1Char('%');
+ do {
+ const int placeHolderPos = t->indexOf(placeHolder, pos);
+ if (placeHolderPos == -1)
+ break;
+ const int endPlaceHolderPos = t->indexOf(placeHolder, placeHolderPos + 1);
+ if (endPlaceHolderPos == -1)
+ break;
+ if (endPlaceHolderPos == placeHolderPos + 1) { // '%%' -> '%'
+ t->remove(placeHolderPos, 1);
+ pos = placeHolderPos + 1;
+ } else {
+ const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos);
+ const QString replacement = keyWordReplacement(keyWord);
+ if (replacement.isEmpty()) {
+ pos = endPlaceHolderPos + 1;
+ } else {
+ t->replace(placeHolderPos, keyWord.size(), replacement);
+ pos = placeHolderPos + replacement.size();
+ }
+ }
+ } while (pos < t->size());
+}
+
+// Convenience that returns the formatted license template.
+QString CppFileSettings::licenseTemplate()
+{
+
+ const QSettings *s = Core::ICore::instance()->settings();
+ QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP);
+ key += QLatin1Char('/');
+ key += QLatin1String(licenseTemplatePathKeyC);
+ const QString path = s->value(key, QString()).toString();
+ if (path.isEmpty())
+ return QString();
+ QFile file(path);
+ if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
+ qWarning("Unable to open the license template %s: %s", qPrintable(path), qPrintable(file.errorString()));
+ return QString();
+ }
+ QString license = QString::fromUtf8(file.readAll());
+ parseLicenseTemplatePlaceholders(&license);
+ // Ensure exactly one additional new line separating stuff
+ const QChar newLine = QLatin1Char('\n');
+ if (!license.endsWith(newLine))
+ license += newLine;
+ license += newLine;
+ return license;
}
// ------------------ CppFileSettingsWidget
@@ -99,6 +208,8 @@ CppFileSettingsWidget::CppFileSettingsWidget(QWidget *parent) :
if (const Core::MimeType headerMt = mdb->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)))
foreach (const QString &suffix, headerMt.suffixes())
m_ui->headerSuffixComboBox->addItem(suffix);
+ m_ui->licenseTemplatePathChooser->setExpectedKind(Core::Utils::PathChooser::File);
+ m_ui->licenseTemplatePathChooser->addButton(tr("Edit..."), this, SLOT(slotEdit()));
}
CppFileSettingsWidget::~CppFileSettingsWidget()
@@ -106,12 +217,23 @@ CppFileSettingsWidget::~CppFileSettingsWidget()
delete m_ui;
}
+QString CppFileSettingsWidget::licenseTemplatePath() const
+{
+ return m_ui->licenseTemplatePathChooser->path();
+}
+
+void CppFileSettingsWidget::setLicenseTemplatePath(const QString &lp)
+{
+ m_ui->licenseTemplatePathChooser->setPath(lp);
+}
+
CppFileSettings CppFileSettingsWidget::settings() const
{
CppFileSettings rc;
rc.lowerCaseFiles = m_ui->lowerCaseFileNamesCheckBox->isChecked();
rc.headerSuffix = m_ui->headerSuffixComboBox->currentText();
rc.sourceSuffix = m_ui->sourceSuffixComboBox->currentText();
+ rc.licenseTemplatePath = licenseTemplatePath();
return rc;
}
@@ -126,6 +248,31 @@ void CppFileSettingsWidget::setSettings(const CppFileSettings &s)
m_ui->lowerCaseFileNamesCheckBox->setChecked(s.lowerCaseFiles);
setComboText(m_ui->headerSuffixComboBox, s.headerSuffix);
setComboText(m_ui->sourceSuffixComboBox, s.sourceSuffix);
+ setLicenseTemplatePath(s.licenseTemplatePath);
+}
+
+void CppFileSettingsWidget::slotEdit()
+{
+ QString path = licenseTemplatePath();
+ // Edit existing file with C++
+ if (!path.isEmpty()) {
+ Core::EditorManager::instance()->openEditor(path, QLatin1String(CppEditor::Constants::CPPEDITOR_KIND));
+ return;
+ }
+ // Pick a file name and write new template, edit with C++
+ path = QFileDialog::getSaveFileName(this, tr("Choose a new license template file"));
+ if (path.isEmpty())
+ return;
+ QFile file(path);
+ if (!file.open(QIODevice::ReadWrite|QIODevice::Text)) {
+ QMessageBox::warning(this, tr("Template write error"),
+ tr("Cannot write to %1: %2").arg(path, file.errorString()));
+ return;
+ }
+ file.write(tr(licenseTemplateTemplate).toUtf8());
+ file.close();
+ setLicenseTemplatePath(path);
+ Core::EditorManager::instance()->openEditor(path, QLatin1String(CppEditor::Constants::CPPEDITOR_KIND));
}
// --------------- CppFileSettingsPage