1
0
Эх сурвалжийг харах

output built-in themes on startup

Le Tan 8 жил өмнө
parent
commit
0b885e1639

+ 1 - 0
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.

+ 1 - 0
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.

+ 1 - 0
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.

+ 10 - 0
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)

+ 2 - 0
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.

+ 50 - 0
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());

+ 4 - 0
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();
 

+ 7 - 0
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:([^;]+);");

+ 6 - 1
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<QString, QString> codeBlockCssStylesFromThemes(const QList<QString> &p_themeFiles);
 
+    static int getPaletteVersion(const QString &p_paletteFile);
+
     static VPaletteMetaData getPaletteMetaData(const QString &p_paletteFile);
 
     // Return the name of the theme.