Migrate some more Application wide preferences to AppSettings

This commit is contained in:
Mark Watkins 2018-04-23 00:22:18 +10:00
parent dc5ec0046f
commit f940064482
8 changed files with 79 additions and 63 deletions

View File

@ -13,6 +13,7 @@
#include "preferences.h"
#include "common.h"
#include "version.h"
class Preferences;
@ -50,6 +51,13 @@ const QString STR_US_AutoLaunchImport = "AutoLaunchImport";
const QString STR_US_RemoveCardReminder = "RemoveCardReminder";
const QString STR_IS_CacheSessions = "MemoryHog";
const QString STR_GEN_UpdatesLastChecked = "UpdatesLastChecked";
const QString STR_GEN_UpdatesAutoCheck = "Updates_AutoCheck";
const QString STR_GEN_UpdateCheckFrequency = "Updates_CheckFrequency";
const QString STR_PREF_AllowEarlyUpdates = "AllowEarlyUpdates";
const QString STR_PREF_VersionString = "VersionString";
class AppWideSetting: public PrefSettings
{
@ -86,8 +94,20 @@ public:
initPref(STR_IS_CacheSessions, false);
initPref(STR_US_RemoveCardReminder, true);
initPref(STR_GEN_Profile, "");
initPref(STR_GEN_UpdatesAutoCheck, true);
initPref(STR_GEN_UpdateCheckFrequency, 7);
initPref(STR_PREF_AllowEarlyUpdates, false);
initPref(STR_GEN_UpdatesLastChecked, QDateTime());
initPref(STR_PREF_VersionString, VersionString);
}
QString versionString() const { return getPref(STR_PREF_VersionString).toString(); }
bool updatesAutoCheck() const { return getPref(STR_GEN_UpdatesAutoCheck).toBool(); }
bool allowEarlyUpdates() const { return getPref(STR_PREF_AllowEarlyUpdates).toBool(); }
QDateTime updatesLastChecked() const { return getPref(STR_GEN_UpdatesLastChecked).toDateTime(); }
int updateCheckFrequency() const { return getPref(STR_GEN_UpdateCheckFrequency).toInt(); }
QString profileName() const { return getPref(STR_GEN_Profile).toString(); }
bool autoLaunchImport() const { return getPref(STR_US_AutoLaunchImport).toBool(); }
bool cacheSessions() const { return getPref(STR_IS_CacheSessions).toBool(); }
@ -138,9 +158,7 @@ public:
int openTabAfterImport() const { return getPref(STR_US_OpenTabAfterImport).toInt(); }
bool removeCardReminder() const { return getPref(STR_US_RemoveCardReminder).toBool(); }
void setProfileName(QString name) { setPref(STR_GEN_Profile, name); }
void setAutoLaunchImport(bool b) { setPref(STR_US_AutoLaunchImport, b); }
void setCacheSessions(bool c) { setPref(STR_IS_CacheSessions, c); }
void setMultithreading(bool enabled) { setPref(STR_IS_Multithreading, enabled); }
@ -187,6 +205,13 @@ public:
void setOpenTabAtStart(int idx) { setPref(STR_US_OpenTabAtStart, idx); }
void setOpenTabAfterImport(int idx) { setPref(STR_US_OpenTabAfterImport, idx); }
void setRemoveCardReminder(bool b) { setPref(STR_US_RemoveCardReminder, b); }
void setVersionString(QString version) { setPref(STR_PREF_VersionString, version); }
void setUpdatesAutoCheck(bool b) { setPref(STR_GEN_UpdatesAutoCheck, b); }
void setAllowEarlyUpdates(bool b) { setPref(STR_PREF_AllowEarlyUpdates, b); }
void setUpdatesLastChecked(QDateTime datetime) { setPref(STR_GEN_UpdatesLastChecked, datetime); }
void setUpdateCheckFrequency(int freq) { setPref(STR_GEN_UpdateCheckFrequency,freq); }
};

View File

@ -121,12 +121,8 @@ enum OverlayDisplayType { ODT_Bars, ODT_TopAndBottom };
const QString STR_GEN_Profile = "Profile";
const QString STR_GEN_SkipLogin = "SkipLoginScreen";
const QString STR_GEN_UpdatesLastChecked = "UpdatesLastChecked";
const QString STR_GEN_UpdatesAutoCheck = "Updates_AutoCheck";
const QString STR_GEN_UpdateCheckFrequency = "Updates_CheckFrequency";
const QString STR_GEN_DataFolder = "DataFolder";
const QString STR_PREF_AllowEarlyUpdates = "AllowEarlyUpdates";
const QString STR_PREF_ReimportBackup = "ReimportBackup";
const QString STR_PREF_LastCPAPPath = "LastCPAPPath";
@ -140,7 +136,6 @@ const QString STR_MACH_MSeries = "MSeries";
const QString STR_MACH_CMS50 = "CMS50";
const QString STR_MACH_ZEO = "Zeo";
const QString STR_PREF_VersionString = "VersionString";
const QString STR_PREF_Language = "Language";
const QString STR_AppName = "SleepyHead";

View File

@ -1918,6 +1918,9 @@ bool Profile::hasChannel(ChannelID code)
const quint16 chandata_version = 1;
void Profile::saveChannels()
{
// First save the XML version for Mobile versions
schema::channel.Save(Get("{DataFolder}/") + "channels.xml");
QString filename = Get("{DataFolder}/") + "channels.dat";
QFile f(filename);
qDebug() << "Saving Channel States";

View File

@ -1,4 +1,4 @@
/* Channel / Schema Implementation
/* Channel / Schema Implementation
*
* Copyright (c) 2011-2018 Mark Watkins <mark@jedimark.net>
*
@ -895,16 +895,14 @@ bool ChannelList::Load(QString filename)
scope = Scopes[scopestr];
// if (PREF[STR_PREF_ResetEventNames].toBool()) {
name = e.attribute("name", "");
details = e.attribute("details", "");
label = e.attribute("label", "");
name = e.attribute("name", "");
details = e.attribute("details", "");
label = e.attribute("label", "");
if (name.isEmpty() || details.isEmpty() || label.isEmpty()) {
qWarning() << "Missing name,details or label attribute in" << filename << "line" << line;
continue;
}
// }
if (name.isEmpty() || details.isEmpty() || label.isEmpty()) {
qWarning() << "Missing name,details or label attribute in" << filename << "line" << line;
continue;
}
unit = e.attribute("unit");
@ -1018,13 +1016,10 @@ void ChannelList::add(QString group, Channel *chan)
bool ChannelList::Save(QString filename)
{
if (!p_profile)
if (filename.isEmpty())
return false;
qDebug() << "Saving Channels.xml";
if (filename.isEmpty()) {
filename = p_profile->Get("{DataFolder}/") + "channels.xml";
}
qDebug() << "In ChannelList::Save() saving " << filename;;
QDomDocument doc("channels");

View File

@ -366,10 +366,6 @@ retry_directory:
////////////////////////////////////////////////////////////////////////////////////////////
// Register Importer Modules for autoscanner
////////////////////////////////////////////////////////////////////////////////////////////
p_pref = new Preferences("Preferences");
PREF.Open();
AppSetting = new AppWideSetting(p_pref);
initialize();
PRS1Loader::Register();
ResmedLoader::Register();
@ -383,63 +379,65 @@ retry_directory:
schema::setOrders();
///////////////////////////////////////////////////////////////////////////////////////////
// Initialize preferences system (Don't use PREF before this point)
///////////////////////////////////////////////////////////////////////////////////////////
p_pref = new Preferences("Preferences");
PREF.Open();
AppSetting = new AppWideSetting(p_pref);
// Clean up some legacy crap
QString layout = PREF.Get("{home}/Layout.xml");
QFile lf(layout);
QFile lf(PREF.Get("{home}/Layout.xml"));
if (lf.exists()) {
lf.remove();
}
PREF.Erase(STR_AppName);
PREF.Erase(STR_GEN_SkipLogin);
// Todo: Make a wrapper for Preference settings, like Profile settings have..
QDateTime lastchecked, today = QDateTime::currentDateTime();
PREF.init(STR_GEN_UpdatesAutoCheck, true);
PREF.init(STR_GEN_UpdateCheckFrequency, 7); // days
PREF.init(STR_PREF_AllowEarlyUpdates, false);
////////////////////////////////////////////////////////////////////////////////////////////
// Check when last checked for updates..
////////////////////////////////////////////////////////////////////////////////////////////
bool check_updates = false;
if (PREF[STR_GEN_UpdatesAutoCheck].toBool()) {
int update_frequency = PREF[STR_GEN_UpdateCheckFrequency].toInt();
if (AppSetting->updatesAutoCheck()) {
int update_frequency = AppSetting->updateCheckFrequency();
int days = 1000;
lastchecked = PREF[STR_GEN_UpdatesLastChecked].toDateTime();
lastchecked = AppSetting->updatesLastChecked();
if (PREF.contains(STR_GEN_UpdatesLastChecked)) {
if (lastchecked.isValid()) {
days = lastchecked.secsTo(today);
days /= 86400;
};
}
if (days > update_frequency) {
check_updates = true;
}
}
if (PREF.contains(STR_PREF_VersionString)) {
int vc = compareVersion(AppSetting->versionString());
if (vc < 0) {
release_notes();
int vc = compareVersion(PREF[STR_PREF_VersionString].toString());
if (vc < 0) {
release_notes();
check_updates = false;
} else if (vc > 0) {
if (QMessageBox::warning(nullptr, STR_MessageBox_Error,
QObject::tr("The version of SleepyHead you just ran is OLDER than the one used to create this data (%1).").
arg(PREF[STR_PREF_VersionString].toString()) +"\n\n"+
QObject::tr("It is likely that doing this will cause data corruption, are you sure you want to do this?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) {
return 0;
}
check_updates = false;
} else if (vc > 0) {
if (QMessageBox::warning(nullptr, STR_MessageBox_Error,
QObject::tr("The version of SleepyHead you just ran is OLDER than the one used to create this data (%1).").
arg(AppSetting->versionString()) +"\n\n"+
QObject::tr("It is likely that doing this will cause data corruption, are you sure you want to do this?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) {
return 0;
}
}
PREF[STR_PREF_VersionString] = VersionString;
AppSetting->setVersionString(VersionString);
// int id=QFontDatabase::addApplicationFont(":/fonts/FreeSans.ttf");
// QFontDatabase fdb;

View File

@ -261,10 +261,10 @@ void MainWindow::on_aboutToQuit()
void MainWindow::closeEvent(QCloseEvent * event)
{
schema::channel.Save();
if (p_profile) {
CloseProfile();
}
schema::channel.Save();
if (AppSetting->removeCardReminder()) {
Notify(QObject::tr("Don't forget to place your datacard back in your CPAP machine"), QObject::tr("SleepyHead Reminder"));

View File

@ -163,7 +163,7 @@ PreferencesDialog::PreferencesDialog(QWidget *parent, Profile *_profile) :
ui->allowYAxisScaling->setChecked(AppSetting->allowYAxisScaling());
ui->autoLaunchImporter->setChecked(AppSetting->autoLaunchImport());
ui->allowEarlyUpdates->setChecked(PREF[STR_PREF_AllowEarlyUpdates].toBool());
ui->allowEarlyUpdates->setChecked(AppSetting->allowEarlyUpdates());
int s = profile->cpap->clockDrift();
int m = (s / 60) % 60;
@ -205,14 +205,14 @@ PreferencesDialog::PreferencesDialog(QWidget *parent, Profile *_profile) :
ui->graphHeight->setValue(AppSetting->graphHeight());
ui->automaticallyCheckUpdates->setChecked(PREF[STR_GEN_UpdatesAutoCheck].toBool());
ui->automaticallyCheckUpdates->setChecked(AppSetting->updatesAutoCheck());
ui->updateCheckEvery->setValue(PREF[STR_GEN_UpdateCheckFrequency].toInt());
ui->updateCheckEvery->setValue(AppSetting->updateCheckFrequency());
if (PREF.contains(STR_GEN_UpdatesLastChecked)) {
if (AppSetting->updatesLastChecked().isValid()) {
RefreshLastChecked();
} else { ui->updateLastChecked->setText("Never"); }
} else { ui->updateLastChecked->setText(tr("Never")); }
ui->overlayFlagsCombo->setCurrentIndex(AppSetting->overlayType());
ui->overviewLinecharts->setCurrentIndex(AppSetting->overviewLinechartMode());
@ -818,9 +818,9 @@ bool PreferencesDialog::Save()
AppSetting->setAutoLaunchImport(ui->autoLaunchImporter->isChecked());
PREF[STR_GEN_UpdatesAutoCheck] = ui->automaticallyCheckUpdates->isChecked();
PREF[STR_GEN_UpdateCheckFrequency] = ui->updateCheckEvery->value();
PREF[STR_PREF_AllowEarlyUpdates] = ui->allowEarlyUpdates->isChecked();
AppSetting->setUpdatesAutoCheck(ui->automaticallyCheckUpdates->isChecked());
AppSetting->setUpdateCheckFrequency(ui->updateCheckEvery->value());
AppSetting->setAllowEarlyUpdates(ui->allowEarlyUpdates->isChecked());
PREF["Fonts_Application_Name"] = ui->applicationFont->currentText();
@ -978,7 +978,7 @@ void PreferencesDialog::on_IgnoreSlider_valueChanged(int position)
extern MainWindow *mainwin;
void PreferencesDialog::RefreshLastChecked()
{
ui->updateLastChecked->setText(PREF[STR_GEN_UpdatesLastChecked].toDateTime().toString(Qt::SystemLocaleLongDate));
ui->updateLastChecked->setText(AppSetting->updatesLastChecked().toString(Qt::SystemLocaleLongDate));
}
void PreferencesDialog::on_checkForUpdatesButton_clicked()

View File

@ -310,7 +310,7 @@ void ProfileSelect::on_listView_activated(const QModelIndex &index)
if (!profile->user->hasPassword()) {
m_selectedProfile = name;
PREF[STR_GEN_Profile] = name;
AppSetting->setProfileName(name);
accept();
return;
} else {