diff --git a/src/resources/themes/v_moonlight/v_moonlight.palette b/src/resources/themes/v_moonlight/v_moonlight.palette index 585fa600..d7722e2c 100644 --- a/src/resources/themes/v_moonlight/v_moonlight.palette +++ b/src/resources/themes/v_moonlight/v_moonlight.palette @@ -6,6 +6,7 @@ qss_file=v_moonlight.qss mdhl_file=v_moonlight.mdhl css_file=v_moonlight.css codeblock_css_file=v_moonlight_codeblock.css +version=1 ; This mapping will be used to translate colors when the content of HTML is copied ; without background. You could just specify the foreground colors mapping here. diff --git a/src/resources/themes/v_pure/v_pure.palette b/src/resources/themes/v_pure/v_pure.palette index 4937f50c..e89a182c 100644 --- a/src/resources/themes/v_pure/v_pure.palette +++ b/src/resources/themes/v_pure/v_pure.palette @@ -6,6 +6,7 @@ qss_file=v_pure.qss mdhl_file=v_pure.mdhl css_file=v_pure.css codeblock_css_file=v_pure_codeblock.css +version=1 [phony] ; Abstract color attributes. diff --git a/src/resources/themes/v_white/v_white.palette b/src/resources/themes/v_white/v_white.palette index b751e787..16fd1e60 100644 --- a/src/resources/themes/v_white/v_white.palette +++ b/src/resources/themes/v_white/v_white.palette @@ -6,6 +6,7 @@ qss_file=v_white.qss mdhl_file=v_white.mdhl css_file=v_white.css codeblock_css_file=v_white_codeblock.css +version=1 [phony] ; Abstract color attributes. diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index 33e8c82d..77e1f0e6 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -878,6 +878,16 @@ bool VUtils::deleteDirectory(const VNotebook *p_notebook, } } +bool VUtils::deleteDirectory(const QString &p_path) +{ + if (p_path.isEmpty()) { + return true; + } + + QDir dir(p_path); + return dir.removeRecursively(); +} + bool VUtils::emptyDirectory(const VNotebook *p_notebook, const QString &p_path, bool p_skipRecycleBin) diff --git a/src/utils/vutils.h b/src/utils/vutils.h index 99ca4a61..6e5e2b37 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -221,6 +221,8 @@ public: const QString &p_path, bool p_skipRecycleBin = false); + static bool deleteDirectory(const QString &p_path); + // Empty all files in directory recursively specified by @p_path. // Will just move files to the recycle bin of @p_notebook if // @p_skipRecycleBin is false. diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 7dd44d84..d7e8d3eb 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -1236,8 +1236,11 @@ void VConfigManager::initThemes() m_themes.insert(VPalette::themeName(file), file); */ + outputBuiltInThemes(); + // User theme folder. QDir dir(getThemeConfigFolder()); + Q_ASSERT(dir.exists()); if (!dir.exists()) { dir.mkpath(getThemeConfigFolder()); return; @@ -1257,6 +1260,53 @@ void VConfigManager::initThemes() } } +void VConfigManager::outputBuiltInThemes() +{ + QDir dir(getThemeConfigFolder()); + if (!dir.exists()) { + dir.mkpath(getThemeConfigFolder()); + } + + QStringList suffix({"*.palette"}); + + for (auto it = m_themes.begin(); it != m_themes.end(); ++it) { + QString file = it.value(); + QString srcDir = VUtils::basePathFromPath(file); + QString folder = VUtils::directoryNameFromPath(srcDir); + + bool needOutput = false; + if (dir.exists(folder)) { + QString folderPath = dir.filePath(folder); + QDir tmpDir(folderPath); + QStringList files = tmpDir.entryList(suffix); + if (files.size() == 1) { + int newVer = VPalette::getPaletteVersion(file); + int curVer = VPalette::getPaletteVersion(tmpDir.filePath(files[0])); + if (newVer != curVer) { + needOutput = true; + } + } else { + needOutput = true; + } + + if (needOutput) { + // Delete the folder. + bool ret = VUtils::deleteDirectory(folderPath); + VUtils::sleepWait(100); + Q_UNUSED(ret); + qDebug() << "delete obsolete theme" << folderPath << ret; + } + } else { + needOutput = true; + } + + if (needOutput) { + qDebug() << "output built-in theme" << file << folder; + VUtils::copyDirectory(srcDir, dir.filePath(folder), false); + } + } +} + void VConfigManager::initEditorStyles() { Q_ASSERT(!m_themes.isEmpty()); diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 40a3cb84..5c928d88 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -510,6 +510,10 @@ private: // Init the themes name-file mappings. void initThemes(); + // Output built-in themes to user theme folder if there does not exists folders + // with the same name. + void outputBuiltInThemes(); + // Init the editor styles name-file mappings. void initEditorStyles(); diff --git a/src/vpalette.cpp b/src/vpalette.cpp index 3e8c01bc..3de085eb 100644 --- a/src/vpalette.cpp +++ b/src/vpalette.cpp @@ -202,6 +202,8 @@ VPaletteMetaData VPalette::getPaletteMetaData(const QString &p_paletteFile) QDir dir(VUtils::basePathFromPath(QFileInfo(p_paletteFile).absoluteFilePath())); settings.beginGroup("metadata"); + data.m_version = settings.value("version").toInt(); + QString val = settings.value("qss_file").toString(); if (!val.isEmpty()) { data.m_qssFile = dir.filePath(val); @@ -264,6 +266,11 @@ QString VPalette::themeCodeBlockCssStyle(const QString &p_paletteFile) return themeName(p_paletteFile) + "/" + QFileInfo(data.m_codeBlockCssFile).completeBaseName(); } +int VPalette::getPaletteVersion(const QString &p_paletteFile) +{ + return getPaletteMetaData(p_paletteFile).m_version; +} + void VPalette::fillFontFamily(QString &p_text) const { QRegExp reg("(\\s|^)font-family:([^;]+);"); diff --git a/src/vpalette.h b/src/vpalette.h index 12993666..4e3cdb09 100644 --- a/src/vpalette.h +++ b/src/vpalette.h @@ -9,6 +9,8 @@ class QSettings; struct VPaletteMetaData { + int m_version; + // These are all file PATH, not name. QString m_qssFile; QString m_mdhlFile; @@ -21,7 +23,8 @@ struct VPaletteMetaData QString toString() const { - return QString("palette metadata qss=%1 mdhl=%2 css=%3 codeBlockCss=%4 colorMappingSize=%5") + return QString("palette metadata version=%1 qss=%2 mdhl=%3 css=%4 codeBlockCss=%5 colorMappingSize=%6") + .arg(m_version) .arg(m_qssFile) .arg(m_mdhlFile) .arg(m_cssFile) @@ -59,6 +62,8 @@ public: // Read themes and return the mappings of css styles. static QMap codeBlockCssStylesFromThemes(const QList &p_themeFiles); + static int getPaletteVersion(const QString &p_paletteFile); + static VPaletteMetaData getPaletteMetaData(const QString &p_paletteFile); // Return the name of the theme.