mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
move some configs from vnote.ini to session.ini
VNote now will first check vnote.ini from the executable folder.
This commit is contained in:
parent
ace96c7ce2
commit
01acd03a71
@ -138,13 +138,13 @@ double_click_close_tab=true
|
||||
; Whether put folder and note panel in one vertical column
|
||||
enable_compact_mode=false
|
||||
|
||||
; Whether enable tools dock widget
|
||||
tools_dock_checked=true
|
||||
|
||||
[web]
|
||||
; Location and configuration for Mathjax
|
||||
mathjax_javascript=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML
|
||||
|
||||
[session]
|
||||
tools_dock_checked=true
|
||||
|
||||
[predefined_colors]
|
||||
1\name=White
|
||||
1\rgb=EEEEEE
|
||||
|
@ -8,52 +8,60 @@
|
||||
#include <QtDebug>
|
||||
#include <QTextEdit>
|
||||
#include <QStandardPaths>
|
||||
#include <QCoreApplication>
|
||||
#include "utils/vutils.h"
|
||||
#include "vstyleparser.h"
|
||||
|
||||
const QString VConfigManager::orgName = QString("vnote");
|
||||
|
||||
const QString VConfigManager::appName = QString("vnote");
|
||||
|
||||
const QString VConfigManager::c_version = QString("1.9");
|
||||
|
||||
const QString VConfigManager::c_obsoleteDirConfigFile = QString(".vnote.json");
|
||||
|
||||
const QString VConfigManager::c_dirConfigFile = QString("_vnote.json");
|
||||
const QString VConfigManager::defaultConfigFilePath = QString(":/resources/vnote.ini");
|
||||
|
||||
const QString VConfigManager::c_defaultConfigFilePath = QString(":/resources/vnote.ini");
|
||||
|
||||
const QString VConfigManager::c_defaultConfigFile = QString("vnote.ini");
|
||||
|
||||
const QString VConfigManager::c_sessionConfigFile = QString("session.ini");
|
||||
|
||||
const QString VConfigManager::c_styleConfigFolder = QString("styles");
|
||||
|
||||
const QString VConfigManager::c_codeBlockStyleConfigFolder = QString("codeblock_styles");
|
||||
|
||||
const QString VConfigManager::c_defaultCssFile = QString(":/resources/styles/default.css");
|
||||
|
||||
const QString VConfigManager::c_defaultCodeBlockCssFile = QString(":/utils/highlightjs/styles/vnote.css");
|
||||
|
||||
const QString VConfigManager::c_defaultMdhlFile = QString(":/resources/styles/default.mdhl");
|
||||
|
||||
const QString VConfigManager::c_solarizedDarkMdhlFile = QString(":/resources/styles/solarized-dark.mdhl");
|
||||
|
||||
const QString VConfigManager::c_solarizedLightMdhlFile = QString(":/resources/styles/solarized-light.mdhl");
|
||||
|
||||
const QString VConfigManager::c_warningTextStyle = QString("color: red; font: bold");
|
||||
|
||||
const QString VConfigManager::c_dataTextStyle = QString("font: bold");
|
||||
|
||||
const QString VConfigManager::c_dangerBtnStyle = QString("QPushButton {color: #fff; border-color: #d43f3a; background-color: #d9534f;}"
|
||||
"QPushButton::hover {color: #fff; border-color: #ac2925; background-color: #c9302c;}");
|
||||
|
||||
const QString VConfigManager::c_vnoteNotebookFolderName = QString("vnote_notebooks");
|
||||
|
||||
VConfigManager::VConfigManager(QObject *p_parent)
|
||||
: QObject(p_parent), userSettings(NULL), defaultSettings(NULL)
|
||||
: QObject(p_parent),
|
||||
userSettings(NULL),
|
||||
defaultSettings(NULL),
|
||||
m_sessionSettings(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
void VConfigManager::migrateIniFile()
|
||||
{
|
||||
const QString originalFolder = "tamlok";
|
||||
const QString newFolder = orgName;
|
||||
QString configFolder = getConfigFolder();
|
||||
QDir dir(configFolder);
|
||||
dir.cdUp();
|
||||
dir.rename(originalFolder, newFolder);
|
||||
userSettings->sync();
|
||||
}
|
||||
|
||||
void VConfigManager::initialize()
|
||||
{
|
||||
Q_ASSERT(!userSettings && !defaultSettings);
|
||||
userSettings = new QSettings(QSettings::IniFormat, QSettings::UserScope,
|
||||
orgName, appName, this);
|
||||
defaultSettings = new QSettings(defaultConfigFilePath, QSettings::IniFormat, this);
|
||||
|
||||
migrateIniFile();
|
||||
initSettings();
|
||||
|
||||
// Override the default css styles on start up.
|
||||
outputDefaultCssStyle();
|
||||
@ -68,7 +76,6 @@ void VConfigManager::initialize()
|
||||
m_templateCss = getConfigFromSettings("global", "template_css").toString();
|
||||
m_templateCodeBlockCss = getConfigFromSettings("global", "template_code_block_css").toString();
|
||||
m_templateCodeBlockCssUrl = getConfigFromSettings("global", "template_code_block_css_url").toString();
|
||||
curNotebookIndex = getConfigFromSettings("global", "current_notebook").toInt();
|
||||
|
||||
markdownExtensions = hoedown_extensions(HOEDOWN_EXT_TABLES | HOEDOWN_EXT_FENCED_CODE |
|
||||
HOEDOWN_EXT_HIGHLIGHT | HOEDOWN_EXT_AUTOLINK |
|
||||
@ -91,11 +98,7 @@ void VConfigManager::initialize()
|
||||
curRenderBackgroundColor = getConfigFromSettings("global",
|
||||
"current_render_background_color").toString();
|
||||
|
||||
m_toolsDockChecked = getConfigFromSettings("session", "tools_dock_checked").toBool();
|
||||
m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray();
|
||||
m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray();
|
||||
m_mainSplitterState = getConfigFromSettings("session", "main_splitter_state").toByteArray();
|
||||
m_naviSplitterState = getConfigFromSettings("session", "navi_splitter_state").toByteArray();
|
||||
m_toolsDockChecked = getConfigFromSettings("global", "tools_dock_checked").toBool();
|
||||
|
||||
m_findCaseSensitive = getConfigFromSettings("global",
|
||||
"find_case_sensitive").toBool();
|
||||
@ -231,6 +234,55 @@ void VConfigManager::initialize()
|
||||
|
||||
m_enableCompactMode = getConfigFromSettings("global",
|
||||
"enable_compact_mode").toBool();
|
||||
|
||||
initFromSessionSettings();
|
||||
}
|
||||
|
||||
void VConfigManager::initSettings()
|
||||
{
|
||||
Q_ASSERT(!userSettings && !defaultSettings && !m_sessionSettings);
|
||||
|
||||
// vnote.ini.
|
||||
// First try to read vnote.ini from the directory of the executable.
|
||||
QString userIniPath = QDir(QCoreApplication::applicationDirPath()).filePath(c_defaultConfigFile);
|
||||
if (QFileInfo::exists(userIniPath)) {
|
||||
userSettings = new QSettings(userIniPath,
|
||||
QSettings::IniFormat,
|
||||
this);
|
||||
} else {
|
||||
userSettings = new QSettings(QSettings::IniFormat,
|
||||
QSettings::UserScope,
|
||||
orgName,
|
||||
appName,
|
||||
this);
|
||||
}
|
||||
|
||||
qDebug() << "use user config" << userSettings->fileName();
|
||||
|
||||
// Default vnote.ini from resource file.
|
||||
defaultSettings = new QSettings(c_defaultConfigFilePath, QSettings::IniFormat, this);
|
||||
|
||||
// session.ini.
|
||||
m_sessionSettings = new QSettings(QDir(getConfigFolder()).filePath(c_sessionConfigFile),
|
||||
QSettings::IniFormat,
|
||||
this);
|
||||
}
|
||||
|
||||
void VConfigManager::initFromSessionSettings()
|
||||
{
|
||||
curNotebookIndex = getConfigFromSessionSettings("global", "current_notebook").toInt();
|
||||
|
||||
m_mainWindowGeometry = getConfigFromSessionSettings("geometry",
|
||||
"main_window_geometry").toByteArray();
|
||||
|
||||
m_mainWindowState = getConfigFromSessionSettings("geometry",
|
||||
"main_window_state").toByteArray();
|
||||
|
||||
m_mainSplitterState = getConfigFromSessionSettings("geometry",
|
||||
"main_splitter_state").toByteArray();
|
||||
|
||||
m_naviSplitterState = getConfigFromSessionSettings("geometry",
|
||||
"navi_splitter_state").toByteArray();
|
||||
}
|
||||
|
||||
void VConfigManager::readPredefinedColorsFromSettings()
|
||||
@ -249,49 +301,70 @@ void VConfigManager::readPredefinedColorsFromSettings()
|
||||
<< "pre-defined colors from [predefined_colors] section";
|
||||
}
|
||||
|
||||
void VConfigManager::readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent)
|
||||
void VConfigManager::readNotebookFromSettings(QSettings *p_settings,
|
||||
QVector<VNotebook *> &p_notebooks,
|
||||
QObject *parent)
|
||||
{
|
||||
Q_ASSERT(p_notebooks.isEmpty());
|
||||
int size = userSettings->beginReadArray("notebooks");
|
||||
int size = p_settings->beginReadArray("notebooks");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
userSettings->setArrayIndex(i);
|
||||
QString name = userSettings->value("name").toString();
|
||||
QString path = userSettings->value("path").toString();
|
||||
p_settings->setArrayIndex(i);
|
||||
QString name = p_settings->value("name").toString();
|
||||
QString path = p_settings->value("path").toString();
|
||||
VNotebook *notebook = new VNotebook(name, path, parent);
|
||||
notebook->readConfigNotebook();
|
||||
p_notebooks.append(notebook);
|
||||
}
|
||||
userSettings->endArray();
|
||||
|
||||
p_settings->endArray();
|
||||
qDebug() << "read" << p_notebooks.size()
|
||||
<< "notebook items from [notebooks] section";
|
||||
}
|
||||
|
||||
void VConfigManager::writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks)
|
||||
void VConfigManager::writeNotebookToSettings(QSettings *p_settings,
|
||||
const QVector<VNotebook *> &p_notebooks)
|
||||
{
|
||||
// Clear it first
|
||||
userSettings->beginGroup("notebooks");
|
||||
userSettings->remove("");
|
||||
userSettings->endGroup();
|
||||
p_settings->beginGroup("notebooks");
|
||||
p_settings->remove("");
|
||||
p_settings->endGroup();
|
||||
|
||||
userSettings->beginWriteArray("notebooks");
|
||||
p_settings->beginWriteArray("notebooks");
|
||||
for (int i = 0; i < p_notebooks.size(); ++i) {
|
||||
userSettings->setArrayIndex(i);
|
||||
p_settings->setArrayIndex(i);
|
||||
const VNotebook ¬ebook = *p_notebooks[i];
|
||||
userSettings->setValue("name", notebook.getName());
|
||||
userSettings->setValue("path", notebook.getPath());
|
||||
p_settings->setValue("name", notebook.getName());
|
||||
p_settings->setValue("path", notebook.getPath());
|
||||
}
|
||||
userSettings->endArray();
|
||||
|
||||
p_settings->endArray();
|
||||
qDebug() << "write" << p_notebooks.size()
|
||||
<< "notebook items in [notebooks] section";
|
||||
}
|
||||
|
||||
static QVariant getConfigFromSettingsBySectionKey(const QSettings *p_settings,
|
||||
const QString &p_section,
|
||||
const QString &p_key)
|
||||
{
|
||||
QString fullKey = p_section + "/" + p_key;
|
||||
return p_settings->value(fullKey);
|
||||
}
|
||||
|
||||
static void setConfigToSettingsBySectionKey(QSettings *p_settings,
|
||||
const QString &p_section,
|
||||
const QString &p_key,
|
||||
const QVariant &p_value)
|
||||
{
|
||||
QString fullKey = p_section + "/" + p_key;
|
||||
return p_settings->setValue(fullKey, p_value);
|
||||
}
|
||||
|
||||
QVariant VConfigManager::getConfigFromSettings(const QString §ion, const QString &key) const
|
||||
{
|
||||
QString fullKey = section + "/" + key;
|
||||
// First, look up the user-scoped config file
|
||||
QVariant value = userSettings->value(fullKey);
|
||||
QVariant value = getConfigFromSettingsBySectionKey(userSettings, section, key);
|
||||
if (!value.isNull()) {
|
||||
qDebug() << "user config:" << fullKey << value.toString();
|
||||
qDebug() << "user config:" << (section + "/" + key) << value.toString();
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -302,18 +375,14 @@ QVariant VConfigManager::getConfigFromSettings(const QString §ion, const QSt
|
||||
void VConfigManager::setConfigToSettings(const QString §ion, const QString &key, const QVariant &value)
|
||||
{
|
||||
// Set the user-scoped config file
|
||||
QString fullKey = section + "/" + key;
|
||||
userSettings->setValue(fullKey, value);
|
||||
qDebug() << "set user config:" << fullKey << value.toString();
|
||||
setConfigToSettingsBySectionKey(userSettings, section, key, value);
|
||||
qDebug() << "set user config:" << (section + "/" + key) << value.toString();
|
||||
}
|
||||
|
||||
QVariant VConfigManager::getDefaultConfig(const QString &p_section, const QString &p_key) const
|
||||
{
|
||||
QString fullKey = p_section + "/" + p_key;
|
||||
|
||||
QVariant value = defaultSettings->value(fullKey);
|
||||
qDebug() << "default config:" << fullKey << value.toString();
|
||||
|
||||
QVariant value = getConfigFromSettingsBySectionKey(defaultSettings, p_section, p_key);
|
||||
qDebug() << "default config:" << (p_section + "/" + p_key) << value.toString();
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -325,6 +394,24 @@ QVariant VConfigManager::resetDefaultConfig(const QString &p_section, const QStr
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
QVariant VConfigManager::getConfigFromSessionSettings(const QString &p_section,
|
||||
const QString &p_key) const
|
||||
{
|
||||
return getConfigFromSettingsBySectionKey(m_sessionSettings,
|
||||
p_section,
|
||||
p_key);
|
||||
}
|
||||
|
||||
void VConfigManager::setConfigToSessionSettings(const QString &p_section,
|
||||
const QString &p_key,
|
||||
const QVariant &p_value)
|
||||
{
|
||||
setConfigToSettingsBySectionKey(m_sessionSettings,
|
||||
p_section,
|
||||
p_key,
|
||||
p_value);
|
||||
}
|
||||
|
||||
QString VConfigManager::fetchDirConfigFilePath(const QString &p_path)
|
||||
{
|
||||
QDir dir(p_path);
|
||||
|
@ -106,7 +106,10 @@ public:
|
||||
int getCurNotebookIndex() const;
|
||||
void setCurNotebookIndex(int index);
|
||||
|
||||
void getNotebooks(QVector<VNotebook *> &p_notebooks, QObject *parent);
|
||||
// Read [notebooks] section from settings into @p_notebooks.
|
||||
void getNotebooks(QVector<VNotebook *> &p_notebooks, QObject *p_parent);
|
||||
|
||||
// Write @p_notebooks to [notebooks] section into settings.
|
||||
void setNotebooks(const QVector<VNotebook *> &p_notebooks);
|
||||
|
||||
hoedown_extensions getMarkdownExtensions() const;
|
||||
@ -323,7 +326,10 @@ public:
|
||||
QVector<QString> getEditorStyles() const;
|
||||
|
||||
private:
|
||||
// Look up a config from user and default settings.
|
||||
QVariant getConfigFromSettings(const QString §ion, const QString &key) const;
|
||||
|
||||
// Set a config to user settings.
|
||||
void setConfigToSettings(const QString §ion, const QString &key, const QVariant &value);
|
||||
|
||||
// Get default config from vnote.ini.
|
||||
@ -332,8 +338,29 @@ private:
|
||||
// Reset user config to default config and return the default config value.
|
||||
QVariant resetDefaultConfig(const QString &p_section, const QString &p_key);
|
||||
|
||||
void readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent);
|
||||
void writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks);
|
||||
// Look up a config from session settings.
|
||||
QVariant getConfigFromSessionSettings(const QString &p_section, const QString &p_key) const;
|
||||
|
||||
// Set a config to session settings.
|
||||
void setConfigToSessionSettings(const QString &p_section,
|
||||
const QString &p_key,
|
||||
const QVariant &p_value);
|
||||
|
||||
// Init defaultSettings, userSettings, and m_sessionSettings.
|
||||
void initSettings();
|
||||
|
||||
// Init from m_sessionSettings.
|
||||
void initFromSessionSettings();
|
||||
|
||||
// Read [notebooks] section from @p_settings.
|
||||
void readNotebookFromSettings(QSettings *p_settings,
|
||||
QVector<VNotebook *> &p_notebooks,
|
||||
QObject *parent);
|
||||
|
||||
// Write to [notebooks] section to @p_settings.
|
||||
void writeNotebookToSettings(QSettings *p_settings,
|
||||
const QVector<VNotebook *> &p_notebooks);
|
||||
|
||||
void readPredefinedColorsFromSettings();
|
||||
|
||||
// 1. Update styles common in HTML and Markdown;
|
||||
@ -342,10 +369,6 @@ private:
|
||||
|
||||
void updateMarkdownEditStyle();
|
||||
|
||||
// Migrate ini file from tamlok/vnote.ini to vnote/vnote.ini.
|
||||
// This is for the change of org name.
|
||||
void migrateIniFile();
|
||||
|
||||
// Output pre-defined CSS styles to style folder.
|
||||
bool outputDefaultCssStyle() const;
|
||||
|
||||
@ -399,6 +422,8 @@ private:
|
||||
QString m_templateCodeBlockCssUrl;
|
||||
|
||||
QString m_editorStyle;
|
||||
|
||||
// Index of current notebook.
|
||||
int curNotebookIndex;
|
||||
|
||||
// Markdown Converter
|
||||
@ -617,15 +642,25 @@ private:
|
||||
// The name of the config file in each directory.
|
||||
static const QString c_dirConfigFile;
|
||||
|
||||
// The name of the default configuration file
|
||||
static const QString defaultConfigFilePath;
|
||||
// The path of the default configuration file
|
||||
static const QString c_defaultConfigFilePath;
|
||||
|
||||
// The name of the config file.
|
||||
static const QString c_defaultConfigFile;
|
||||
|
||||
// The name of the config file for session information.
|
||||
static const QString c_sessionConfigFile;
|
||||
|
||||
// QSettings for the user configuration
|
||||
QSettings *userSettings;
|
||||
|
||||
// Qsettings for @defaultConfigFileName
|
||||
// Qsettings for @c_defaultConfigFilePath.
|
||||
QSettings *defaultSettings;
|
||||
|
||||
// QSettings for the session configuration, such as notebooks,
|
||||
// geometry, last opened files.
|
||||
QSettings *m_sessionSettings;
|
||||
|
||||
// The folder name of style files.
|
||||
static const QString c_styleConfigFolder;
|
||||
|
||||
@ -693,18 +728,35 @@ inline void VConfigManager::setCurNotebookIndex(int index)
|
||||
if (index == curNotebookIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
curNotebookIndex = index;
|
||||
setConfigToSettings("global", "current_notebook", index);
|
||||
setConfigToSessionSettings("global", "current_notebook", index);
|
||||
}
|
||||
|
||||
inline void VConfigManager::getNotebooks(QVector<VNotebook *> &p_notebooks, QObject *parent)
|
||||
inline void VConfigManager::getNotebooks(QVector<VNotebook *> &p_notebooks,
|
||||
QObject *p_parent)
|
||||
{
|
||||
readNotebookFromSettings(p_notebooks, parent);
|
||||
// We used to store it in vnote.ini. For now, we store it in session.ini.
|
||||
readNotebookFromSettings(m_sessionSettings, p_notebooks, p_parent);
|
||||
|
||||
// Migration.
|
||||
if (p_notebooks.isEmpty()) {
|
||||
readNotebookFromSettings(userSettings, p_notebooks, p_parent);
|
||||
|
||||
if (!p_notebooks.isEmpty()) {
|
||||
// Clear and save it in another place.
|
||||
userSettings->beginGroup("notebooks");
|
||||
userSettings->remove("");
|
||||
userSettings->endGroup();
|
||||
|
||||
writeNotebookToSettings(m_sessionSettings, p_notebooks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void VConfigManager::setNotebooks(const QVector<VNotebook *> &p_notebooks)
|
||||
{
|
||||
writeNotebookToSettings(p_notebooks);
|
||||
writeNotebookToSettings(m_sessionSettings, p_notebooks);
|
||||
}
|
||||
|
||||
inline hoedown_extensions VConfigManager::getMarkdownExtensions() const
|
||||
@ -872,7 +924,7 @@ inline bool VConfigManager::getToolsDockChecked() const
|
||||
inline void VConfigManager::setToolsDockChecked(bool p_checked)
|
||||
{
|
||||
m_toolsDockChecked = p_checked;
|
||||
setConfigToSettings("session", "tools_dock_checked",
|
||||
setConfigToSettings("global", "tools_dock_checked",
|
||||
m_toolsDockChecked);
|
||||
}
|
||||
|
||||
@ -884,8 +936,9 @@ inline const QByteArray& VConfigManager::getMainWindowGeometry() const
|
||||
inline void VConfigManager::setMainWindowGeometry(const QByteArray &p_geometry)
|
||||
{
|
||||
m_mainWindowGeometry = p_geometry;
|
||||
setConfigToSettings("session", "main_window_geometry",
|
||||
m_mainWindowGeometry);
|
||||
setConfigToSessionSettings("geometry",
|
||||
"main_window_geometry",
|
||||
m_mainWindowGeometry);
|
||||
}
|
||||
|
||||
inline const QByteArray& VConfigManager::getMainWindowState() const
|
||||
@ -896,8 +949,9 @@ inline const QByteArray& VConfigManager::getMainWindowState() const
|
||||
inline void VConfigManager::setMainWindowState(const QByteArray &p_state)
|
||||
{
|
||||
m_mainWindowState = p_state;
|
||||
setConfigToSettings("session", "main_window_state",
|
||||
m_mainWindowState);
|
||||
setConfigToSessionSettings("geometry",
|
||||
"main_window_state",
|
||||
m_mainWindowState);
|
||||
}
|
||||
|
||||
inline const QByteArray& VConfigManager::getMainSplitterState() const
|
||||
@ -908,7 +962,9 @@ inline const QByteArray& VConfigManager::getMainSplitterState() const
|
||||
inline void VConfigManager::setMainSplitterState(const QByteArray &p_state)
|
||||
{
|
||||
m_mainSplitterState = p_state;
|
||||
setConfigToSettings("session", "main_splitter_state", m_mainSplitterState);
|
||||
setConfigToSessionSettings("geometry",
|
||||
"main_splitter_state",
|
||||
m_mainSplitterState);
|
||||
}
|
||||
|
||||
inline const QByteArray& VConfigManager::getNaviSplitterState() const
|
||||
@ -919,7 +975,9 @@ inline const QByteArray& VConfigManager::getNaviSplitterState() const
|
||||
inline void VConfigManager::setNaviSplitterState(const QByteArray &p_state)
|
||||
{
|
||||
m_naviSplitterState = p_state;
|
||||
setConfigToSettings("session", "navi_splitter_state", m_naviSplitterState);
|
||||
setConfigToSessionSettings("geometry",
|
||||
"navi_splitter_state",
|
||||
m_naviSplitterState);
|
||||
}
|
||||
|
||||
inline bool VConfigManager::getFindCaseSensitive() const
|
||||
|
Loading…
x
Reference in New Issue
Block a user