253 lines
8.4 KiB
C++
253 lines
8.4 KiB
C++
#include "settings.h"
|
|
|
|
#include <QDebug>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QStandardPaths>
|
|
|
|
QString Settings::configPath()
|
|
{
|
|
const QString dir = QStringLiteral("/home/nemo/.config/harbour");
|
|
QDir().mkpath(dir);
|
|
return dir + QStringLiteral("/calibreweb.conf");
|
|
}
|
|
|
|
Settings::Settings(QObject *parent)
|
|
: QObject(parent)
|
|
, m_settings(configPath(), QSettings::IniFormat)
|
|
{
|
|
migrateIfNeeded();
|
|
openPermissions();
|
|
// diagnostica: path reale del file di configurazione e valori letti all'avvio
|
|
qDebug() << "Settings file:" << m_settings.fileName();
|
|
if (m_settings.status() != QSettings::NoError)
|
|
qDebug() << "Settings WARNING: file di configurazione non accessibile (permessi?)";
|
|
qDebug() << "Settings read: serverUrl=[" << serverUrl() << "] user=[" << username()
|
|
<< "] ssl=" << ignoreSslErrors() << "margin=" << readerMargin()
|
|
<< "marginV=" << readerMarginV();
|
|
}
|
|
|
|
void Settings::migrateIfNeeded()
|
|
{
|
|
if (!serverUrl().isEmpty())
|
|
return; // il file condiviso ha gia' una configurazione
|
|
// Sorgenti candidate: config per-utente corrente + i path noti degli altri
|
|
// utenti (launcher=nemo, debug SSH=defaultuser). La prima con valori vince.
|
|
const QStringList candidates = QStringList()
|
|
<< QSettings(QStringLiteral("harbour"), QStringLiteral("calibreweb")).fileName()
|
|
<< QStringLiteral("/home/nemo/.config/harbour/calibreweb.conf")
|
|
<< QStringLiteral("/home/defaultuser/.config/harbour/calibreweb.conf");
|
|
for (const QString &cand : candidates) {
|
|
QSettings src(cand, QSettings::IniFormat);
|
|
const QString url = src.value(QStringLiteral("serverUrl")).toString();
|
|
if (url.isEmpty())
|
|
continue;
|
|
m_settings.setValue(QStringLiteral("serverUrl"), url);
|
|
m_settings.setValue(QStringLiteral("username"), src.value(QStringLiteral("username")));
|
|
m_settings.setValue(QStringLiteral("password"), src.value(QStringLiteral("password")));
|
|
m_settings.setValue(QStringLiteral("ignoreSslErrors"),
|
|
src.value(QStringLiteral("ignoreSslErrors")));
|
|
m_settings.setValue(QStringLiteral("readerMargin"),
|
|
src.value(QStringLiteral("readerMargin"), 24));
|
|
m_settings.setValue(QStringLiteral("readerMarginV"),
|
|
src.value(QStringLiteral("readerMarginV"), 0));
|
|
// segnalibri di lettura per libro+formato
|
|
src.beginGroup(QStringLiteral("bookmarks"));
|
|
const QStringList groups = src.childGroups();
|
|
for (const QString &g : groups) {
|
|
src.beginGroup(g);
|
|
const QStringList keys = src.childKeys();
|
|
for (const QString &k : keys)
|
|
m_settings.setValue(QStringLiteral("bookmarks/%1/%2").arg(g).arg(k),
|
|
src.value(k));
|
|
src.endGroup();
|
|
}
|
|
src.endGroup();
|
|
m_settings.sync();
|
|
openPermissions();
|
|
qDebug() << "Settings: importati i valori da" << cand;
|
|
return;
|
|
}
|
|
}
|
|
|
|
void Settings::openPermissions()
|
|
{
|
|
// Apre la catena di directory e il file di configurazione: la home di
|
|
// nemo e' 700 di default e defaultuser (debug SSH) non puo' entrarci.
|
|
// Al primo avvio dal launcher (nemo) questi permessi restano aperti, e
|
|
// da quel momento entrambi gli utenti condividono lo stesso file.
|
|
QDir home(QStringLiteral("/home/nemo"));
|
|
QFile::setPermissions(home.absolutePath(),
|
|
QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
|
|
| QFile::ReadGroup | QFile::ExeGroup
|
|
| QFile::ReadOther | QFile::ExeOther);
|
|
QDir config(QStringLiteral("/home/nemo/.config"));
|
|
QFile::setPermissions(config.absolutePath(),
|
|
QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
|
|
| QFile::ReadGroup | QFile::ExeGroup
|
|
| QFile::ReadOther | QFile::ExeOther);
|
|
QDir dir(QFileInfo(m_settings.fileName()).absolutePath());
|
|
QFile::setPermissions(dir.absolutePath(),
|
|
QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
|
|
| QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup
|
|
| QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
|
|
QFile::setPermissions(m_settings.fileName(),
|
|
QFile::ReadOwner | QFile::WriteOwner
|
|
| QFile::ReadGroup | QFile::WriteGroup
|
|
| QFile::ReadOther | QFile::WriteOther);
|
|
}
|
|
|
|
QString Settings::serverUrl() const
|
|
{
|
|
return m_settings.value(QStringLiteral("serverUrl")).toString();
|
|
}
|
|
|
|
void Settings::setServerUrl(const QString &value)
|
|
{
|
|
if (value == serverUrl())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("serverUrl"), value);
|
|
m_settings.sync(); // scrivi subito su disco: l'app può crashare alla chiusura (WebView)
|
|
openPermissions();
|
|
emit serverUrlChanged();
|
|
emit baseUrlChanged();
|
|
}
|
|
|
|
QString Settings::username() const
|
|
{
|
|
return m_settings.value(QStringLiteral("username")).toString();
|
|
}
|
|
|
|
void Settings::setUsername(const QString &value)
|
|
{
|
|
if (value == username())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("username"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit usernameChanged();
|
|
}
|
|
|
|
QString Settings::password() const
|
|
{
|
|
return m_settings.value(QStringLiteral("password")).toString();
|
|
}
|
|
|
|
void Settings::setPassword(const QString &value)
|
|
{
|
|
if (value == password())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("password"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit passwordChanged();
|
|
}
|
|
|
|
bool Settings::ignoreSslErrors() const
|
|
{
|
|
return m_settings.value(QStringLiteral("ignoreSslErrors"), false).toBool();
|
|
}
|
|
|
|
void Settings::setIgnoreSslErrors(bool value)
|
|
{
|
|
if (value == ignoreSslErrors())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("ignoreSslErrors"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit ignoreSslErrorsChanged();
|
|
}
|
|
|
|
int Settings::readerMargin() const
|
|
{
|
|
return m_settings.value(QStringLiteral("readerMargin"), 24).toInt();
|
|
}
|
|
|
|
void Settings::setReaderMargin(int value)
|
|
{
|
|
if (value == readerMargin())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("readerMargin"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit readerMarginChanged();
|
|
}
|
|
|
|
int Settings::readerMarginV() const
|
|
{
|
|
return m_settings.value(QStringLiteral("readerMarginV"), 0).toInt();
|
|
}
|
|
|
|
void Settings::setReaderMarginV(int value)
|
|
{
|
|
if (value == readerMarginV())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("readerMarginV"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit readerMarginVChanged();
|
|
}
|
|
|
|
QString Settings::language() const
|
|
{
|
|
return m_settings.value(QStringLiteral("language"), QStringLiteral("system")).toString();
|
|
}
|
|
|
|
void Settings::setLanguage(const QString &value)
|
|
{
|
|
if (value == language())
|
|
return;
|
|
m_settings.setValue(QStringLiteral("language"), value);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
emit languageChanged();
|
|
}
|
|
|
|
QString Settings::downloadDir() const
|
|
{
|
|
QString dir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
if (dir.isEmpty())
|
|
dir = QStringLiteral("/home/nemo/Downloads");
|
|
QDir().mkpath(dir);
|
|
return dir;
|
|
}
|
|
|
|
QString Settings::authHeader() const
|
|
{
|
|
const QString user = username();
|
|
if (user.isEmpty())
|
|
return QString();
|
|
const QString credentials = user + QLatin1Char(':') + password();
|
|
return QStringLiteral("Basic ") + QString::fromLatin1(credentials.toUtf8().toBase64());
|
|
}
|
|
|
|
QString Settings::baseUrl() const
|
|
{
|
|
QString url = serverUrl().trimmed();
|
|
while (url.endsWith(QLatin1Char('/')))
|
|
url.chop(1);
|
|
return url;
|
|
}
|
|
|
|
QString Settings::bookmarkKey(int bookId, const QString &format) const
|
|
{
|
|
return m_settings.value(QStringLiteral("bookmarks/%1/%2").arg(bookId).arg(format))
|
|
.toString();
|
|
}
|
|
|
|
void Settings::setBookmarkKey(int bookId, const QString &format, const QString &key)
|
|
{
|
|
m_settings.setValue(QStringLiteral("bookmarks/%1/%2").arg(bookId).arg(format), key);
|
|
m_settings.sync();
|
|
openPermissions();
|
|
}
|
|
|
|
QString Settings::bookFilePath(int bookId, const QString &format) const
|
|
{
|
|
const QString dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
|
|
+ QStringLiteral("/books");
|
|
QDir().mkpath(dir);
|
|
return dir + QStringLiteral("/%1.%2").arg(bookId).arg(format);
|
|
}
|