Merged purge functions

This commit is contained in:
Mark Watkins 2014-05-19 13:46:02 +10:00
parent 10db88005b
commit 0e69dbd370
10 changed files with 60 additions and 43 deletions

View File

@ -12,4 +12,4 @@ TRANSLATIONS += \
Translations/Svenska.se.ts \
Translations/Espaniol.es.ts \
Translations/Bulgarian.bg.ts \
Translations/English.en_US.ts
Translations/English.en_UK.ts

View File

@ -775,6 +775,20 @@ int Day::count(ChannelID code)
return sum;
}
bool Day::summaryOnly()
{
QList<Session *>::iterator end = sessions.end();
for (QList<Session *>::iterator it = sessions.begin(); it != end; ++it) {
Session & sess = *(*it);
QVariant v = sess.setting(CPAP_SummaryOnly);
if (!v.isNull()) {
if (v.toBool())
return true;
}
}
return false;
}
bool Day::settingExists(ChannelID id)
{
QList<Session *>::iterator end = sessions.end();

View File

@ -139,6 +139,9 @@ class Day
//! \brief Return the end session record as a QVector<Session*>::iterator
QList<Session *>::iterator end() { return sessions.end(); }
//! \brief Check if day contains SummaryOnly records
bool summaryOnly();
//! \brief Finds and returns the index of a session, otherwise -1 if it's not there
int find(Session *sess) { return sessions.indexOf(sess); }

View File

@ -215,18 +215,12 @@ QDate Machine::AddSession(Session *s, Profile *p)
}
// This functions purpose is murder and mayhem... It deletes all of a machines data.
// Therefore this is the most dangerous function in this software..
bool Machine::Purge(int secret)
{
// Boring api key to stop this function getting called by accident :)
if (secret != 3478216) { return false; }
// FIXME: should really clear out the majority of the machine properties here
// It would be joyous if this function screwed up..
QString path = profile->Get(properties[STR_PROP_Path]); //STR_GEN_DataFolder)+"/"+m_class+"_";
//if (properties.contains(STR_PROP_Serial)) path+=properties[STR_PROP_Serial]; else path+=hexid();
QString path = profile->Get(properties[STR_PROP_Path]);
QDir dir(path);
@ -239,37 +233,58 @@ bool Machine::Purge(int secret)
}
qDebug() << "Purging " << QDir::toNativeSeparators(path);
qDebug() << "Purging" << m_class << properties[STR_PROP_Serial] << dir.absoluteFilePath(path);
// Create a copy of the list so the hash can be manipulated
QList<Session *> sessions = sessionlist.values();
// Clean up any loaded sessions from memory first..
bool success = true;
for (int i=0; i < sessions.size(); ++i) {
Session * sess = sessions[i];
if (!sess->Destroy()) {
qDebug() << "Could not destroy "+ m_class+" ("+properties[STR_PROP_Serial]+") session" << sess->session();
success = false;
} else {
sessionlist.erase(sessionlist.find(sess->session()));
}
delete sess;
}
// Clean up any straggling files (like from short sessions not being loaded...)
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Name);
QFileInfoList list = dir.entryInfoList();
int could_not_kill = 0;
for (int i = 0; i < list.size(); ++i) {
int size = list.size();
for (int i = 0; i < size; ++i) {
QFileInfo fi = list.at(i);
QString fullpath = fi.canonicalFilePath();
//int j=fullpath.lastIndexOf(".");
QString ext_s = fullpath.section('.', -1); //right(j);
QString ext_s = fullpath.section('.', -1);
bool ok;
ext_s.toInt(&ok, 10);
if (ok) {
qDebug() << "Deleting " << QDir::toNativeSeparators(fullpath);
dir.remove(fullpath);
} else { could_not_kill++; }
if (!dir.remove(fullpath)) {
qDebug() << "Could not purge file" << fullpath;
success=false;
could_not_kill++;
}
} else {
qDebug() << "Didn't bother deleting cruft file" << fullpath;
// cruft file..
}
}
if (could_not_kill > 0) {
// qWarning() << "Could not purge path\n" << path << "\n\n" << could_not_kill << " file(s) remain.. Suggest manually deleting this path\n";
// return false;
qWarning() << "Could not purge path" << could_not_kill << "files in " << path;
return false;
}
// PROFILE.machlist.erase(PROFILE.machlist.find(m->id()));
PROFILE.p_preferences[STR_PREF_ReimportBackup] = true;
return true;
}

View File

@ -311,6 +311,10 @@ class Session
//! \brief Returns this sessions MachineID
Machine *machine() { return s_machine; }
//! \brief Returns true if session only contains summary data
bool summaryOnly();
//! \brief Completely purges Session from memory and disk.
bool Destroy();
void wipeSummary() {

View File

@ -75,7 +75,7 @@ QColor COLOR_VibratorySnore = QColor("#ff4040");
QColor COLOR_FlowLimit = QColor("#404040");
QColor COLOR_SensAwake = COLOR_Gold;
QColor COLOR_LeakFlag = QColor("#40c0c0"); // Qt::darkBlue;
QColor COLOR_NRI = COLOR_ClearAirway;
QColor COLOR_NRI = QColor("orange"); //COLOR_ClearAirway;
QColor COLOR_ExP = Qt::darkCyan;
QColor COLOR_PressurePulse = Qt::red;
QColor COLOR_PulseChange = COLOR_LightGray;

View File

@ -1738,7 +1738,7 @@ void MainWindow::purgeMachine(Machine * mach)
if (QMessageBox::question(this,
STR_MessageBox_Question,
tr("Are you sure you want to purge all CPAP data for the following machine:\n\n") +
tr("Are you sure you want to purge all CPAP data for the following machine:")+ "\n\n" +
mach->properties[STR_PROP_Brand] + " " + mach->properties[STR_PROP_Model] + " " +
mach->properties[STR_PROP_ModelNumber] + " (" + mach->properties[STR_PROP_Serial] + ")",
QMessageBox::Yes | QMessageBox::No,
@ -1746,28 +1746,10 @@ void MainWindow::purgeMachine(Machine * mach)
return;
}
QHash<SessionID, Session *>::iterator it;
QHash<SessionID, Session *>::iterator s_end = mach->sessionlist.end();
QList<Session *> sessions;
for (it = mach->sessionlist.begin(); it != s_end; ++it) {
Session * sess = *it;
sessions.push_back(sess);
}
bool success = true;
for (int i=0; i < sessions.size(); ++i) {
Session * sess = sessions[i];
if (!sess->Destroy()) {
qDebug() << "Could not destroy "+mach->GetClass()+" ("+mach->properties[STR_PROP_Serial]+") session" << sess->session();
success = false;
} else {
mach->sessionlist.erase(mach->sessionlist.find(sess->session()));
}
delete sess;
}
if (success) {
// Technicially the above won't sessions under short session limit.. Using Purge to clean up the rest.
if (mach->Purge(3478216)) {
mach->sessionlist.clear();
mach->day.clear();
} else {
QMessageBox::warning(this, STR_MessageBox_Error,
tr("Not all session data could be removed, you have to delete the following folder manually.")

View File

@ -226,7 +226,6 @@ OTHER_FILES += \
docs/script.js \
../update.xml \
docs/changelog.txt \
docs/update_notes.html \
docs/intro.html \
docs/statistics.xml

View File

@ -40,7 +40,7 @@ void initTranslations(QSettings & settings) {
langNames["es"] = "Español";
langNames["bg"] = "български";
langNames["fr"] = "Français";
langNames["en_US"] = "English US";
langNames["en_UK"] = "English UK";
// CHECK: Will the above break with MS VisualC++ compiler?
QHash<QString, QString> langFiles;