Files
zeal/src/core/settings.cpp
T

157 lines
5.8 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2015 Oleg Shparber
** Contact: http://zealdocs.org/contact.html
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
2015-01-12 22:21:44 -08:00
#include "settings.h"
2015-02-19 22:38:19 -08:00
#include <QCoreApplication>
2015-01-29 17:03:36 -08:00
#include <QDir>
2015-01-12 22:21:44 -08:00
#include <QSettings>
#include <QStandardPaths>
#include <QUrl>
2015-01-12 22:21:44 -08:00
2015-04-04 15:18:48 -07:00
#ifdef USE_WEBENGINE
2015-05-15 19:05:31 -07:00
#include <QWebEngineSettings>
#define QWebSettings QWebEngineSettings
2015-04-04 15:18:48 -07:00
#else
2015-05-15 19:05:31 -07:00
#include <QWebSettings>
2015-04-04 15:18:48 -07:00
#endif
2015-01-12 22:21:44 -08:00
using namespace Zeal::Core;
Settings::Settings(QObject *parent) :
QObject(parent),
2015-05-15 19:05:31 -07:00
#ifndef PORTABLE_BUILD
2015-01-12 22:21:44 -08:00
m_settings(new QSettings(this))
2015-05-15 19:05:31 -07:00
#else
2015-02-19 22:38:19 -08:00
m_settings(new QSettings(QCoreApplication::applicationDirPath() + QLatin1String("/zeal.ini"),
QSettings::IniFormat, this))
2015-05-15 19:05:31 -07:00
#endif
2015-01-12 22:21:44 -08:00
{
/// TODO: Move to user style sheet (related to #268)
2015-04-04 15:18:48 -07:00
#ifndef USE_WEBENGINE
QWebSettings::globalSettings()->setUserStyleSheetUrl(QStringLiteral("qrc:///browser/highlight.css"));
2015-04-04 15:18:48 -07:00
#endif
2015-01-12 22:21:44 -08:00
load();
}
Settings::~Settings()
{
save();
}
void Settings::load()
{
/// TODO: Put everything in groups
startMinimized = m_settings->value(QStringLiteral("start_minimized"), false).toBool();
checkForUpdate = m_settings->value(QStringLiteral("check_for_update"), true).toBool();
2015-01-12 22:21:44 -08:00
2015-02-19 22:38:36 -08:00
showSystrayIcon = m_settings->value(QStringLiteral("show_systray_icon"), true).toBool();
2015-01-12 22:21:44 -08:00
minimizeToSystray = m_settings->value(QStringLiteral("minimize_to_systray"), false).toBool();
hideOnClose = m_settings->value(QStringLiteral("hide_on_close"), false).toBool();
m_settings->beginGroup(QStringLiteral("global_shortcuts"));
#ifndef Q_OS_OSX
showShortcut = m_settings->value(QStringLiteral("show"), QStringLiteral("Meta+Z")).value<QKeySequence>();
2015-01-12 22:21:44 -08:00
#else
showShortcut = m_settings->value(QStringLiteral("show"), QStringLiteral("Alt+Space")).value<QKeySequence>();
2015-01-12 22:21:44 -08:00
#endif
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("browser"));
2015-03-02 22:50:54 -08:00
minimumFontSize = m_settings->value(QStringLiteral("minimum_font_size"),
QWebSettings::globalSettings()->fontSize(QWebSettings::MinimumFontSize)).toInt();
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("proxy"));
2015-03-02 22:50:54 -08:00
proxyType = static_cast<ProxyType>(m_settings->value(QStringLiteral("type"), ProxyType::System).toUInt());
proxyHost = m_settings->value(QStringLiteral("host")).toString();
proxyPort = m_settings->value(QStringLiteral("port"), 0).toInt();
proxyAuthenticate = m_settings->value(QStringLiteral("authenticate"), false).toBool();
proxyUserName = m_settings->value(QStringLiteral("username")).toString();
proxyPassword = m_settings->value(QStringLiteral("password")).toString();
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("docsets"));
2015-03-02 22:50:54 -08:00
if (m_settings->contains(QStringLiteral("path"))) {
docsetPath = m_settings->value(QStringLiteral("path")).toString();
2015-01-12 22:21:44 -08:00
} else {
2015-02-19 22:38:19 -08:00
#ifndef PORTABLE_BUILD
2015-01-12 22:21:44 -08:00
docsetPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation)
+ QLatin1String("/docsets");
2015-02-19 22:38:19 -08:00
#else
docsetPath = QCoreApplication::applicationDirPath() + QLatin1String("/docsets");
#endif
2015-01-12 22:21:44 -08:00
QDir().mkpath(docsetPath);
}
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("state"));
2015-03-02 22:50:54 -08:00
windowGeometry = m_settings->value(QStringLiteral("window_geometry")).toByteArray();
splitterGeometry = m_settings->value(QStringLiteral("splitter_geometry")).toByteArray();
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
}
void Settings::save()
{
/// TODO: Put everything in groups
m_settings->setValue(QStringLiteral("start_minimized"), startMinimized);
m_settings->setValue(QStringLiteral("check_for_update"), checkForUpdate);
2015-01-12 22:21:44 -08:00
m_settings->setValue(QStringLiteral("show_systray_icon"), showSystrayIcon);
m_settings->setValue(QStringLiteral("minimize_to_systray"), minimizeToSystray);
m_settings->setValue(QStringLiteral("hide_on_close"), hideOnClose);
m_settings->beginGroup(QStringLiteral("global_shortcuts"));
m_settings->setValue(QStringLiteral("show"), showShortcut);
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("browser"));
2015-03-02 22:50:54 -08:00
m_settings->setValue(QStringLiteral("minimum_font_size"), minimumFontSize);
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
m_settings->beginGroup(QStringLiteral("proxy"));
2015-03-02 22:50:54 -08:00
m_settings->setValue(QStringLiteral("type"), proxyType);
m_settings->setValue(QStringLiteral("host"), proxyHost);
m_settings->setValue(QStringLiteral("port"), proxyPort);
m_settings->setValue(QStringLiteral("authenticate"), proxyAuthenticate);
m_settings->setValue(QStringLiteral("username"), proxyUserName);
m_settings->setValue(QStringLiteral("password"), proxyPassword);
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
2015-02-19 22:38:19 -08:00
#ifndef PORTABLE_BUILD
2015-01-12 22:21:44 -08:00
m_settings->beginGroup(QStringLiteral("docsets"));
2015-03-02 22:50:54 -08:00
m_settings->setValue(QStringLiteral("path"), docsetPath);
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
2015-02-19 22:38:19 -08:00
#endif
2015-01-12 22:21:44 -08:00
m_settings->beginGroup(QStringLiteral("state"));
2015-03-02 22:50:54 -08:00
m_settings->setValue(QStringLiteral("window_geometry"), windowGeometry);
m_settings->setValue(QStringLiteral("splitter_geometry"), splitterGeometry);
2015-01-12 22:21:44 -08:00
m_settings->endGroup();
m_settings->sync();
2015-01-14 12:14:26 -08:00
emit updated();
2015-01-12 22:21:44 -08:00
}