cppfilesettingspage.cpp 15.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

#include "cppfilesettingspage.h"
32

33
#include "cpptoolsconstants.h"
34
#include "cpptoolsplugin.h"
35
#include <ui_cppfilesettingspage.h>
36 37

#include <coreplugin/icore.h>
38 39 40
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppeditorconstants.h>

41
#include <utils/environment.h>
42
#include <utils/fileutils.h>
Eike Ziller's avatar
Eike Ziller committed
43
#include <utils/mimetypes/mimedatabase.h>
44

45 46 47 48 49 50 51 52 53
#include <QSettings>
#include <QDebug>
#include <QFile>
#include <QCoreApplication>
#include <QDate>
#include <QLocale>
#include <QTextCodec>
#include <QTextStream>
#include <QFileDialog>
54

55 56
static const char headerPrefixesKeyC[] = "HeaderPrefixes";
static const char sourcePrefixesKeyC[] = "SourcePrefixes";
57 58
static const char headerSuffixKeyC[] = "HeaderSuffix";
static const char sourceSuffixKeyC[] = "SourceSuffix";
59 60
static const char headerSearchPathsKeyC[] = "HeaderSearchPaths";
static const char sourceSearchPathsKeyC[] = "SourceSearchPaths";
61
static const char licenseTemplatePathKeyC[] = "LicenseTemplate";
62 63 64 65 66 67 68 69

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");
70 71 72 73 74 75 76 77 78 79 80 81

namespace CppTools {
namespace Internal {

CppFileSettings::CppFileSettings() :
    lowerCaseFiles(false)
{
}

void CppFileSettings::toSettings(QSettings *s) const
{
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
82 83
    s->setValue(QLatin1String(headerPrefixesKeyC), headerPrefixes);
    s->setValue(QLatin1String(sourcePrefixesKeyC), sourcePrefixes);
84 85
    s->setValue(QLatin1String(headerSuffixKeyC), headerSuffix);
    s->setValue(QLatin1String(sourceSuffixKeyC), sourceSuffix);
86 87
    s->setValue(QLatin1String(headerSearchPathsKeyC), headerSearchPaths);
    s->setValue(QLatin1String(sourceSearchPathsKeyC), sourceSearchPaths);
88
    s->setValue(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), lowerCaseFiles);
89
    s->setValue(QLatin1String(licenseTemplatePathKeyC), licenseTemplatePath);
90 91 92 93 94
    s->endGroup();
}

void CppFileSettings::fromSettings(QSettings *s)
{
95 96 97 98 99 100 101 102 103
    const QStringList defaultHeaderSearchPaths = QStringList()
            << QLatin1String("include")
            << QLatin1String("Include")
            << QDir::toNativeSeparators(QLatin1String("../include"))
            << QDir::toNativeSeparators(QLatin1String("../Include"));
    const QStringList defaultSourceSearchPaths = QStringList()
            << QDir::toNativeSeparators(QLatin1String("../src"))
            << QDir::toNativeSeparators(QLatin1String("../Src"))
            << QLatin1String("..");
104
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
105 106 107
    headerPrefixes = s->value(QLatin1String(headerPrefixesKeyC)).toStringList();
    sourcePrefixes = s->value(QLatin1String(sourcePrefixesKeyC)).toStringList();
    headerSuffix = s->value(QLatin1String(headerSuffixKeyC), QLatin1String("h")).toString();
108
    sourceSuffix = s->value(QLatin1String(sourceSuffixKeyC), QLatin1String("cpp")).toString();
109 110 111 112
    headerSearchPaths = s->value(QLatin1String(headerSearchPathsKeyC), defaultHeaderSearchPaths)
            .toStringList();
    sourceSearchPaths = s->value(QLatin1String(sourceSearchPathsKeyC), defaultSourceSearchPaths)
            .toStringList();
113 114
    const bool lowerCaseDefault = Constants::lowerCaseFilesDefault;
    lowerCaseFiles = s->value(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), QVariant(lowerCaseDefault)).toBool();
115
    licenseTemplatePath = s->value(QLatin1String(licenseTemplatePathKeyC), QString()).toString();
116 117 118
    s->endGroup();
}

119
bool CppFileSettings::applySuffixesToMimeDB()
120
{
Eike Ziller's avatar
Eike Ziller committed
121 122 123 124 125 126 127 128 129 130 131
    Utils::MimeDatabase mdb;
    Utils::MimeType mt;
    mt = mdb.mimeTypeForName(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE));
    if (!mt.isValid())
        return false;
    mt.setPreferredSuffix(sourceSuffix);
    mt = mdb.mimeTypeForName(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE));
    if (!mt.isValid())
        return false;
    mt.setPreferredSuffix(headerSuffix);
    return true;
132 133 134 135 136
}

bool CppFileSettings::equals(const CppFileSettings &rhs) const
{
    return lowerCaseFiles == rhs.lowerCaseFiles
137 138
           && headerPrefixes == rhs.headerPrefixes
           && sourcePrefixes == rhs.sourcePrefixes
139
           && headerSuffix == rhs.headerSuffix
140
           && sourceSuffix == rhs.sourceSuffix
141 142
           && headerSearchPaths == rhs.headerSearchPaths
           && sourceSearchPaths == rhs.sourceSearchPaths
143 144 145 146
           && licenseTemplatePath == rhs.licenseTemplatePath;
}

// Replacements of special license template keywords.
147 148
static bool keyWordReplacement(const QString &keyWord,
                               QString *value)
149 150
{
    if (keyWord == QLatin1String("%YEAR%")) {
151
        *value = QLatin1String("%{CurrentDate:yyyy}");
152 153
        return true;
    }
154
    if (keyWord == QLatin1String("%MONTH%")) {
155
        *value = QLatin1String("%{CurrentDate:M}");
156 157 158
        return true;
    }
    if (keyWord == QLatin1String("%DAY%")) {
159
        *value = QLatin1String("%{CurrentDate:d}");
160 161
        return true;
    }
162
    if (keyWord == QLatin1String("%CLASS%")) {
163
        *value = QLatin1String("%{Cpp:License:ClassName}");
164 165 166
        return true;
    }
    if (keyWord == QLatin1String("%FILENAME%")) {
167
        *value = QLatin1String("%{Cpp:License:FileName}");
168
        return true;
169 170 171 172 173 174 175 176 177 178 179
    }
    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));
        }
180
        *value = QString::fromLatin1("%{CurrentDate:") + format + QLatin1Char('}');
181
        return true;
182 183
    }
    if (keyWord == QLatin1String("%USER%")) {
184
        *value = QLatin1String("%{Env:USER}");
185
        return true;
186 187 188 189
    }
    // Environment variables (for example '%$EMAIL%').
    if (keyWord.startsWith(QLatin1String("%$"))) {
        const QString varName = keyWord.mid(2, keyWord.size() - 3);
190
        *value = QString::fromLatin1("%{Env:") + varName + QLatin1Char('}');
191
        return true;
192
    }
193
    return false;
194 195 196 197
}

// Parse a license template, scan for %KEYWORD% and replace if known.
// Replace '%%' by '%'.
198
static void parseLicenseTemplatePlaceholders(QString *t)
199 200 201
{
    int pos = 0;
    const QChar placeHolder = QLatin1Char('%');
202
    bool isCompatibilityStyle = false;
203 204 205 206 207 208 209 210 211 212 213 214
    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);
215
            QString replacement;
216 217
            if (keyWordReplacement(keyWord, &replacement)) {
                isCompatibilityStyle = true;
218 219
                t->replace(placeHolderPos, keyWord.size(), replacement);
                pos = placeHolderPos + replacement.size();
220 221 222
            } else {
                // Leave invalid keywords as is.
                pos = endPlaceHolderPos + 1;
223 224 225
            }
        }
    } while (pos < t->size());
226 227 228

    if (isCompatibilityStyle)
        t->replace(QLatin1Char('\\'), QLatin1String("\\\\"));
229 230 231
}

// Convenience that returns the formatted license template.
232
QString CppFileSettings::licenseTemplate()
233
{
hjk's avatar
hjk committed
234
    const QSettings *s = Core::ICore::settings();
235 236 237 238 239 240 241 242 243 244 245
    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();
    }
246 247

    QTextStream licenseStream(&file);
248
    licenseStream.setCodec(Core::EditorManager::defaultTextCodec());
249 250 251
    licenseStream.setAutoDetectUnicode(true);
    QString license = licenseStream.readAll();

252
    parseLicenseTemplatePlaceholders(&license);
253 254

    // Ensure at least one newline at the end of the license template to separate it from the code
255 256 257
    const QChar newLine = QLatin1Char('\n');
    if (!license.endsWith(newLine))
        license += newLine;
258

259
    return license;
260 261 262 263 264 265
}

// ------------------ CppFileSettingsWidget

CppFileSettingsWidget::CppFileSettingsWidget(QWidget *parent) :
    QWidget(parent),
266
    m_ui(new Internal::Ui::CppFileSettingsPage)
267 268 269
{
    m_ui->setupUi(this);
    // populate suffix combos
Eike Ziller's avatar
Eike Ziller committed
270 271 272
    Utils::MimeDatabase mdb;
    const Utils::MimeType sourceMt = mdb.mimeTypeForName(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE));
    if (sourceMt.isValid()) {
273 274
        foreach (const QString &suffix, sourceMt.suffixes())
            m_ui->sourceSuffixComboBox->addItem(suffix);
Eike Ziller's avatar
Eike Ziller committed
275
    }
276

Eike Ziller's avatar
Eike Ziller committed
277 278
    const Utils::MimeType headerMt = mdb.mimeTypeForName(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE));
    if (headerMt.isValid()) {
279 280
        foreach (const QString &suffix, headerMt.suffixes())
            m_ui->headerSuffixComboBox->addItem(suffix);
Eike Ziller's avatar
Eike Ziller committed
281
    }
282
    m_ui->licenseTemplatePathChooser->setExpectedKind(Utils::PathChooser::File);
283
    m_ui->licenseTemplatePathChooser->setHistoryCompleter(QLatin1String("Cpp.LicenseTemplate.History"));
284
    m_ui->licenseTemplatePathChooser->addButton(tr("Edit..."), this, [this] { slotEdit(); });
285 286 287 288 289 290 291
}

CppFileSettingsWidget::~CppFileSettingsWidget()
{
    delete m_ui;
}

292 293 294 295 296 297 298 299 300 301
QString CppFileSettingsWidget::licenseTemplatePath() const
{
    return m_ui->licenseTemplatePathChooser->path();
}

void CppFileSettingsWidget::setLicenseTemplatePath(const QString &lp)
{
    m_ui->licenseTemplatePathChooser->setPath(lp);
}

302 303 304 305 306 307 308 309
static QStringList trimmedPaths(const QString &paths)
{
    QStringList res;
    foreach (const QString &path, paths.split(QLatin1Char(','), QString::SkipEmptyParts))
        res << path.trimmed();
    return res;
}

310 311 312 313
CppFileSettings CppFileSettingsWidget::settings() const
{
    CppFileSettings rc;
    rc.lowerCaseFiles = m_ui->lowerCaseFileNamesCheckBox->isChecked();
314 315
    rc.headerPrefixes = trimmedPaths(m_ui->headerPrefixesEdit->text());
    rc.sourcePrefixes = trimmedPaths(m_ui->sourcePrefixesEdit->text());
316 317
    rc.headerSuffix = m_ui->headerSuffixComboBox->currentText();
    rc.sourceSuffix = m_ui->sourceSuffixComboBox->currentText();
318 319
    rc.headerSearchPaths = trimmedPaths(m_ui->headerSearchPathsEdit->text());
    rc.sourceSearchPaths = trimmedPaths(m_ui->sourceSearchPathsEdit->text());
320
    rc.licenseTemplatePath = licenseTemplatePath();
321 322 323 324 325 326 327 328 329 330 331
    return rc;
}

static inline void setComboText(QComboBox *cb, const QString &text, int defaultIndex = 0)
{
    const int index = cb->findText(text);
    cb->setCurrentIndex(index == -1 ? defaultIndex: index);
}

void CppFileSettingsWidget::setSettings(const CppFileSettings &s)
{
hjk's avatar
hjk committed
332
    const QChar comma = QLatin1Char(',');
333
    m_ui->lowerCaseFileNamesCheckBox->setChecked(s.lowerCaseFiles);
hjk's avatar
hjk committed
334 335
    m_ui->headerPrefixesEdit->setText(s.headerPrefixes.join(comma));
    m_ui->sourcePrefixesEdit->setText(s.sourcePrefixes.join(comma));
336 337
    setComboText(m_ui->headerSuffixComboBox, s.headerSuffix);
    setComboText(m_ui->sourceSuffixComboBox, s.sourceSuffix);
hjk's avatar
hjk committed
338 339
    m_ui->headerSearchPathsEdit->setText(s.headerSearchPaths.join(comma));
    m_ui->sourceSearchPathsEdit->setText(s.sourceSearchPaths.join(comma));
340 341 342 343 344 345
    setLicenseTemplatePath(s.licenseTemplatePath);
}

void CppFileSettingsWidget::slotEdit()
{
    QString path = licenseTemplatePath();
346 347 348 349 350 351 352 353 354 355
    if (path.isEmpty()) {
        // Pick a file name and write new template, edit with C++
        path = QFileDialog::getSaveFileName(this, tr("Choose Location for New License Template File"));
        if (path.isEmpty())
            return;
        Utils::FileSaver saver(path, QIODevice::Text);
        saver.write(tr(licenseTemplateTemplate).toUtf8());
        if (!saver.finalize(this))
            return;
        setLicenseTemplatePath(path);
356
    }
357
    // Edit (now) existing file with C++
Eike Ziller's avatar
Eike Ziller committed
358
    Core::EditorManager::openEditor(path, CppEditor::Constants::CPPEDITOR_ID);
359 360 361
}

// --------------- CppFileSettingsPage
362 363 364 365
CppFileSettingsPage::CppFileSettingsPage(QSharedPointer<CppFileSettings> &settings,
                                         QObject *parent) :
    Core::IOptionsPage(parent),
    m_settings(settings)
366
{
hjk's avatar
hjk committed
367
    setId(Constants::CPP_FILE_SETTINGS_ID);
368
    setDisplayName(QCoreApplication::translate("CppTools", Constants::CPP_FILE_SETTINGS_NAME));
hjk's avatar
hjk committed
369
    setCategory(Constants::CPP_SETTINGS_CATEGORY);
370 371
    setDisplayCategory(QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_TR_CATEGORY));
    setCategoryIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPP_ICON));
372 373
}

374
QWidget *CppFileSettingsPage::widget()
375 376
{

377 378 379 380
    if (!m_widget) {
        m_widget = new CppFileSettingsWidget;
        m_widget->setSettings(*m_settings);
    }
381 382 383 384 385 386 387
    return m_widget;
}

void CppFileSettingsPage::apply()
{
    if (m_widget) {
        const CppFileSettings newSettings = m_widget->settings();
388 389
        if (newSettings != *m_settings) {
            *m_settings = newSettings;
hjk's avatar
hjk committed
390
            m_settings->toSettings(Core::ICore::settings());
391
            m_settings->applySuffixesToMimeDB();
392
            CppToolsPlugin::clearHeaderSourceCache();
393 394 395 396
        }
    }
}

397
void CppFileSettingsPage::finish()
398
{
399
    delete m_widget;
400 401
}

402 403
} // namespace Internal
} // namespace CppTools