cppfilesettingspage.cpp 13.2 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** 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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30 31 32 33 34 35 36 37
**
**************************************************************************/

#include "cppfilesettingspage.h"
#include "cpptoolsconstants.h"
#include "ui_cppfilesettingspage.h"

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

42 43
#include <extensionsystem/pluginmanager.h>

44 45
#include <utils/fileutils.h>

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

56 57
#include <QFileDialog>
#include <QMessageBox>
58

59 60 61
static const char headerSuffixKeyC[] = "HeaderSuffix";
static const char sourceSuffixKeyC[] = "SourceSuffix";
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 82 83 84

namespace CppTools {
namespace Internal {

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

void CppFileSettings::toSettings(QSettings *s) const
{
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
    s->setValue(QLatin1String(headerSuffixKeyC), headerSuffix);
    s->setValue(QLatin1String(sourceSuffixKeyC), sourceSuffix);
    s->setValue(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), lowerCaseFiles);
85
    s->setValue(QLatin1String(licenseTemplatePathKeyC), licenseTemplatePath);
86 87 88 89 90 91 92 93
    s->endGroup();
}

void CppFileSettings::fromSettings(QSettings *s)
{
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
    headerSuffix= s->value(QLatin1String(headerSuffixKeyC), QLatin1String("h")).toString();
    sourceSuffix = s->value(QLatin1String(sourceSuffixKeyC), QLatin1String("cpp")).toString();
94 95
    const bool lowerCaseDefault = Constants::lowerCaseFilesDefault;
    lowerCaseFiles = s->value(QLatin1String(Constants::LOWERCASE_CPPFILES_KEY), QVariant(lowerCaseDefault)).toBool();
96
    licenseTemplatePath = s->value(QLatin1String(licenseTemplatePathKeyC), QString()).toString();
97 98 99
    s->endGroup();
}

100
bool CppFileSettings::applySuffixesToMimeDB()
101
{
hjk's avatar
hjk committed
102
    Core::MimeDatabase *mdb = Core::ICore::mimeDatabase();
103 104
    return mdb->setPreferredSuffix(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE), sourceSuffix)
            && mdb->setPreferredSuffix(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE), headerSuffix);
105 106 107 108 109 110
}

bool CppFileSettings::equals(const CppFileSettings &rhs) const
{
    return lowerCaseFiles == rhs.lowerCaseFiles
           && headerSuffix == rhs.headerSuffix
111 112 113 114 115
           && sourceSuffix == rhs.sourceSuffix
           && licenseTemplatePath == rhs.licenseTemplatePath;
}

// Replacements of special license template keywords.
116 117 118 119
static bool keyWordReplacement(const QString &keyWord,
                               const QString &file,
                               const QString &className,
                               QString *value)
120 121
{
    if (keyWord == QLatin1String("%YEAR%")) {
122 123 124
        *value = QString::number(QDate::currentDate().year());
        return true;
    }
125 126 127 128 129 130 131 132
    if (keyWord == QLatin1String("%MONTH%")) {
        *value = QString::number(QDate::currentDate().month());
        return true;
    }
    if (keyWord == QLatin1String("%DAY%")) {
        *value = QString::number(QDate::currentDate().day());
        return true;
    }
133 134 135 136 137 138 139
    if (keyWord == QLatin1String("%CLASS%")) {
        *value = className;
        return true;
    }
    if (keyWord == QLatin1String("%FILENAME%")) {
        *value = QFileInfo(file).fileName();
        return true;
140 141 142 143 144 145 146 147 148 149 150
    }
    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));
        }
151 152
        *value = QDate::currentDate().toString(format);
        return true;
153 154 155
    }
    if (keyWord == QLatin1String("%USER%")) {
#ifdef Q_OS_WIN
156
        *value = QString::fromLocal8Bit(qgetenv("USERNAME"));
157
#else
158
        *value = QString::fromLocal8Bit(qgetenv("USER"));
159
#endif
160
        return true;
161 162 163 164
    }
    // Environment variables (for example '%$EMAIL%').
    if (keyWord.startsWith(QLatin1String("%$"))) {
        const QString varName = keyWord.mid(2, keyWord.size() - 3);
165 166
        *value = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit()));
        return true;
167
    }
168
    return false;
169 170 171 172
}

// Parse a license template, scan for %KEYWORD% and replace if known.
// Replace '%%' by '%'.
173
static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, const QString &className)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
    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);
189 190
            QString replacement;
            if (keyWordReplacement(keyWord, file, className, &replacement)) {
191 192
                t->replace(placeHolderPos, keyWord.size(), replacement);
                pos = placeHolderPos + replacement.size();
193 194 195
            } else {
                // Leave invalid keywords as is.
                pos = endPlaceHolderPos + 1;
196 197 198 199 200 201
            }
        }
    } while (pos < t->size());
}

// Convenience that returns the formatted license template.
202
QString CppFileSettings::licenseTemplate(const QString &fileName, const QString &className)
203 204
{

hjk's avatar
hjk committed
205
    const QSettings *s = Core::ICore::settings();
206 207 208 209 210 211 212 213 214 215 216
    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();
    }
217

218
    QTextCodec *codec = Core::EditorManager::instance()->defaultTextCodec();
219 220 221 222 223
    QTextStream licenseStream(&file);
    licenseStream.setCodec(codec);
    licenseStream.setAutoDetectUnicode(true);
    QString license = licenseStream.readAll();

224
    parseLicenseTemplatePlaceholders(&license, fileName, className);
225 226 227 228 229 230
    // Ensure exactly one additional new line separating stuff
    const QChar newLine = QLatin1Char('\n');
    if (!license.endsWith(newLine))
        license += newLine;
    license += newLine;
    return license;
231 232 233 234 235 236
}

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

CppFileSettingsWidget::CppFileSettingsWidget(QWidget *parent) :
    QWidget(parent),
237
    m_ui(new Internal::Ui::CppFileSettingsPage)
238 239
{
    m_ui->setupUi(this);
hjk's avatar
hjk committed
240
    const Core::MimeDatabase *mdb = Core::ICore::mimeDatabase();
241 242 243 244 245 246 247 248
    // populate suffix combos
    if (const Core::MimeType sourceMt = mdb->findByType(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)))
        foreach (const QString &suffix, sourceMt.suffixes())
            m_ui->sourceSuffixComboBox->addItem(suffix);

    if (const Core::MimeType headerMt = mdb->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)))
        foreach (const QString &suffix, headerMt.suffixes())
            m_ui->headerSuffixComboBox->addItem(suffix);
249
    m_ui->licenseTemplatePathChooser->setExpectedKind(Utils::PathChooser::File);
250
    m_ui->licenseTemplatePathChooser->addButton(tr("Edit..."), this, SLOT(slotEdit()));
251 252 253 254 255 256 257
}

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

258 259 260 261 262 263 264 265 266 267
QString CppFileSettingsWidget::licenseTemplatePath() const
{
    return m_ui->licenseTemplatePathChooser->path();
}

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

268 269 270 271 272 273
CppFileSettings CppFileSettingsWidget::settings() const
{
    CppFileSettings rc;
    rc.lowerCaseFiles = m_ui->lowerCaseFileNamesCheckBox->isChecked();
    rc.headerSuffix = m_ui->headerSuffixComboBox->currentText();
    rc.sourceSuffix = m_ui->sourceSuffixComboBox->currentText();
274
    rc.licenseTemplatePath = licenseTemplatePath();
275 276 277
    return rc;
}

278 279 280 281 282
QString CppFileSettingsWidget::searchKeywords() const
{
    QString rc;
    QTextStream(&rc) << m_ui->headerSuffixLabel->text()
            << ' ' << m_ui->sourceSuffixLabel->text()
283 284
            << ' ' << m_ui->lowerCaseFileNamesCheckBox->text()
            << ' ' << m_ui->licenseTemplateLabel->text();
285 286 287 288
    rc.remove(QLatin1Char('&'));
    return rc;
}

289 290 291 292 293 294 295 296 297 298 299
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)
{
    m_ui->lowerCaseFileNamesCheckBox->setChecked(s.lowerCaseFiles);
    setComboText(m_ui->headerSuffixComboBox, s.headerSuffix);
    setComboText(m_ui->sourceSuffixComboBox, s.sourceSuffix);
300 301 302 303 304 305
    setLicenseTemplatePath(s.licenseTemplatePath);
}

void CppFileSettingsWidget::slotEdit()
{
    QString path = licenseTemplatePath();
306 307 308 309 310 311 312 313 314 315
    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);
316
    }
317
    // Edit (now) existing file with C++
hjk's avatar
hjk committed
318 319
    Core::EditorManager::openEditor(path, CppEditor::Constants::CPPEDITOR_ID,
                                    Core::EditorManager::ModeSwitch);
320 321 322
}

// --------------- CppFileSettingsPage
323 324 325 326
CppFileSettingsPage::CppFileSettingsPage(QSharedPointer<CppFileSettings> &settings,
                                         QObject *parent) :
    Core::IOptionsPage(parent),
    m_settings(settings)
327 328 329 330 331 332 333 334 335
{
}

CppFileSettingsPage::~CppFileSettingsPage()
{
}

QString CppFileSettingsPage::id() const
{
336
    return QLatin1String(Constants::CPP_FILE_SETTINGS_ID);
337 338
}

339
QString CppFileSettingsPage::displayName() const
340
{
341
    return QCoreApplication::translate("CppTools", Constants::CPP_FILE_SETTINGS_NAME);
342 343 344 345 346 347 348
}

QString CppFileSettingsPage::category() const
{
    return QLatin1String(Constants::CPP_SETTINGS_CATEGORY);
}

349
QString CppFileSettingsPage::displayCategory() const
350
{
351
    return QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_TR_CATEGORY);
352 353
}

354 355
QIcon CppFileSettingsPage::categoryIcon() const
{
con's avatar
con committed
356
    return QIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPP_ICON));
357 358
}

359 360 361 362
QWidget *CppFileSettingsPage::createPage(QWidget *parent)
{

    m_widget = new CppFileSettingsWidget(parent);
363
    m_widget->setSettings(*m_settings);
364 365
    if (m_searchKeywords.isEmpty())
        m_searchKeywords = m_widget->searchKeywords();
366 367 368 369 370 371 372
    return m_widget;
}

void CppFileSettingsPage::apply()
{
    if (m_widget) {
        const CppFileSettings newSettings = m_widget->settings();
373 374
        if (newSettings != *m_settings) {
            *m_settings = newSettings;
hjk's avatar
hjk committed
375
            m_settings->toSettings(Core::ICore::settings());
376
            m_settings->applySuffixesToMimeDB();
377 378 379 380
        }
    }
}

381 382 383 384 385
bool CppFileSettingsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

386 387
} // namespace Internal
} // namespace CppTools