Skip to content
Snippets Groups Projects
Commit 96bff6ea authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Cleaned up creation of new color scheme files

The creation of the styles directory was in two places and the importing
of an old customized style didn't have any safety when the name
'customized.xml' already existed.
parent 3607ad17
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,28 @@ static QString customStylesPath() ...@@ -97,6 +97,28 @@ static QString customStylesPath()
return path; return path;
} }
static QString createColorSchemeFileName(const QString &pattern)
{
const QString stylesPath = customStylesPath();
QString baseFileName = stylesPath;
baseFileName += pattern;
// Find an available file name
int i = 1;
QString fileName;
do {
fileName = baseFileName.arg((i == 1) ? QString() : QString::number(i));
++i;
} while (QFile::exists(fileName));
// Create the base directory when it doesn't exist
if (!QFile::exists(stylesPath) && !QDir().mkpath(stylesPath)) {
qWarning() << "Failed to create color scheme directory:" << stylesPath;
return QString();
}
return fileName;
}
// ------- FontSettingsPagePrivate // ------- FontSettingsPagePrivate
FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescriptions &fd, FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescriptions &fd,
...@@ -135,9 +157,8 @@ FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescrip ...@@ -135,9 +157,8 @@ FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescrip
} }
if (m_value.colorScheme() != defaultScheme) { if (m_value.colorScheme() != defaultScheme) {
// Save it as a color scheme file // Save it as a color scheme file
QString stylesPath = customStylesPath(); QString schemeFileName = createColorSchemeFileName(QLatin1String("customized%1.xml"));
if (QFile::exists(stylesPath) || QDir().mkpath(stylesPath)) { if (!schemeFileName.isEmpty()) {
QString schemeFileName = stylesPath + QLatin1String("customized.xml");
if (m_value.saveColorScheme(schemeFileName) && settings) if (m_value.saveColorScheme(schemeFileName) && settings)
m_value.toSettings(m_category, settings); m_value.toSettings(m_category, settings);
} }
...@@ -335,31 +356,18 @@ void FontSettingsPage::cloneColorScheme() ...@@ -335,31 +356,18 @@ void FontSettingsPage::cloneColorScheme()
if (!d_ptr->m_value.loadColorScheme(entry.fileName, d_ptr->m_descriptions)) if (!d_ptr->m_value.loadColorScheme(entry.fileName, d_ptr->m_descriptions))
return; return;
QString baseDir = customStylesPath(); QString baseFileName = QFileInfo(entry.fileName).completeBaseName();
QString baseFileName = baseDir; baseFileName += QLatin1String("_copy%1.xml");
baseFileName.append(QFileInfo(entry.fileName).completeBaseName()); QString fileName = createColorSchemeFileName(baseFileName);
// Find an available file name if (!fileName.isEmpty()) {
int i = 1; ColorScheme scheme = d_ptr->m_value.colorScheme();
QString fileName; scheme.setName(tr("%1 (copy)").arg(scheme.name()));
do { scheme.save(fileName);
fileName = baseFileName; d_ptr->m_value.setColorSchemeFileName(fileName);
fileName.append(QString("_copy%1.xml").arg((i == 1) ? QString() : QString::number(i)));
++i;
} while (QFile::exists(fileName));
// Create the base directory when it doesn't exist refreshColorSchemeList();
if (!QFile::exists(baseDir) && !QDir().mkpath(baseDir)) {
qWarning() << "Failed to create color scheme directory:" << baseDir;
return;
} }
ColorScheme scheme = d_ptr->m_value.colorScheme();
scheme.setName(tr("%1 (copy)").arg(scheme.name()));
scheme.save(fileName);
d_ptr->m_value.setColorSchemeFileName(fileName);
refreshColorSchemeList();
} }
void FontSettingsPage::deleteColorScheme() void FontSettingsPage::deleteColorScheme()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment