diff --git a/SleepLib/preferences.h b/SleepLib/preferences.h index d266db22..fef540a0 100644 --- a/SleepLib/preferences.h +++ b/SleepLib/preferences.h @@ -64,6 +64,11 @@ public: bool Exists(QString name) { return (p_preferences.find(name)!=p_preferences.end()); }; + void Erase(QString name) { + QHash::iterator i=p_preferences.find(name); + if (i!=p_preferences.end()) + p_preferences.erase(i); + } virtual void ExtraLoad(QDomElement & root) { root=root; } virtual QDomElement ExtraSave(QDomDocument & doc) { doc=doc; QDomElement e; return e; } diff --git a/mainwindow.cpp b/mainwindow.cpp index 643a99cf..162cbdd0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -16,6 +16,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "preferencesdialog.h" +#include "newprofile.h" #include "SleepLib/schema.h" @@ -438,5 +439,8 @@ void MainWindow::on_actionPrint_Report_triggered() void MainWindow::on_action_Edit_Profile_triggered() { + NewProfile newprof(this); + newprof.edit(pref["Profile"].toString()); + newprof.exec(); } diff --git a/newprofile.cpp b/newprofile.cpp index f7743848..31c59af6 100644 --- a/newprofile.cpp +++ b/newprofile.cpp @@ -24,6 +24,7 @@ NewProfile::NewProfile(QWidget *parent) : ui->stackedWidget->setCurrentIndex(0); on_cpapModeCombo_activated(0); + m_passwordHashed=false; } NewProfile::~NewProfile() @@ -84,8 +85,12 @@ void NewProfile::on_nextButton_clicked() prof["Phone"]=ui->phoneEdit->text(); prof["Address"]=ui->addressEdit->toPlainText(); if (ui->passwordGroupBox->isChecked()) { - QByteArray ba=ui->passwordEdit1->text().toUtf8(); - prof["Password"]=QString(QCryptographicHash::hash(ba,QCryptographicHash::Sha1).toHex()); + if (!m_passwordHashed) { + QByteArray ba=ui->passwordEdit1->text().toUtf8(); + prof["Password"]=QString(QCryptographicHash::hash(ba,QCryptographicHash::Sha1).toHex()); + } + } else { + prof.Erase("Password"); } //prof["Password"]=""; if (ui->genderCombo->currentIndex()==1) { @@ -160,3 +165,56 @@ void NewProfile::skipWelcomeScreen() ui->backButton->setEnabled(false); ui->nextButton->setEnabled(true); } +void NewProfile::edit(const QString name) +{ + skipWelcomeScreen(); + Profile *profile=Profiles::Get(name); + if (!profile) { + Profiles::Create(name); + } + ui->userNameEdit->setText(name); + ui->userNameEdit->setReadOnly(true); + ui->firstNameEdit->setText((*profile)["FirstName"].toString()); + ui->lastNameEdit->setText((*profile)["LastName"].toString()); + if (profile->Exists("Password")) { + // leave the password box blank.. + ui->passwordEdit1->setText(""); + ui->passwordEdit2->setText(""); + ui->passwordGroupBox->setChecked(true); + m_passwordHashed=true; + } + ui->dobEdit->setDate((*profile)["DOB"].toDate()); + if (profile->Get("Gender").toLower()=="male") { + ui->genderCombo->setCurrentIndex(1); + } else if (profile->Get("Gender").toLower()=="female") { + ui->genderCombo->setCurrentIndex(2); + } else ui->genderCombo->setCurrentIndex(0); + ui->heightEdit->setValue((*profile)["Height"].toDouble()); + ui->addressEdit->setText(profile->Get("Address")); + ui->emailEdit->setText(profile->Get("EmailAddress")); + ui->phoneEdit->setText(profile->Get("Phone")); + ui->dateDiagnosedEdit->setDate((*profile)["DateDiagnosed"].toDate()); + ui->cpapNotes->clear(); + ui->cpapNotes->appendPlainText(profile->Get("CPAPNotes")); + ui->minPressureEdit->setValue((*profile)["CPAPPrescribedMinPressure"].toDouble()); + ui->maxPressureEdit->setValue((*profile)["CPAPPrescribedMaxPressure"].toDouble()); + ui->untreatedAHIEdit->setValue((*profile)["UntreatedAHI"].toDouble()); + ui->cpapModeCombo->setCurrentIndex((*profile)["CPAPPrescribedMode"].toInt()); + + ui->doctorNameEdit->setText(profile->Get("DoctorName")); + ui->doctorPracticeEdit->setText(profile->Get("DoctorPractice")); + ui->doctorPhoneEdit->setText(profile->Get("DoctorPhone")); + ui->doctorEmailEdit->setText(profile->Get("DoctorEmail")); + ui->doctorAddressEdit->setText(profile->Get("DoctorAddress")); + ui->doctorPatientIDEdit->setText(profile->Get("DoctorPatientID")); +} + +void NewProfile::on_passwordEdit1_editingFinished() +{ + m_passwordHashed=false; +} + +void NewProfile::on_passwordEdit2_editingFinished() +{ + m_passwordHashed=false; +} diff --git a/newprofile.h b/newprofile.h index 071eb612..9862bdd8 100644 --- a/newprofile.h +++ b/newprofile.h @@ -15,6 +15,7 @@ public: explicit NewProfile(QWidget *parent = 0); ~NewProfile(); void skipWelcomeScreen(); + void edit(const QString name); private slots: void on_nextButton_clicked(); @@ -24,10 +25,15 @@ private slots: void on_agreeCheckbox_clicked(bool checked); + void on_passwordEdit1_editingFinished(); + + void on_passwordEdit2_editingFinished(); + private: Ui::NewProfile *ui; bool m_editMode; int m_firstPage; + bool m_passwordHashed; }; #endif // NEWPROFILE_H diff --git a/newprofile.ui b/newprofile.ui index 2d685922..0b63cddd 100644 --- a/newprofile.ui +++ b/newprofile.ui @@ -7,7 +7,7 @@ 0 0 605 - 374 + 392 @@ -24,7 +24,7 @@ - 4 + 1 diff --git a/profileselect.cpp b/profileselect.cpp index 6567da09..b6ce1f3c 100644 --- a/profileselect.cpp +++ b/profileselect.cpp @@ -56,20 +56,90 @@ ProfileSelect::ProfileSelect(QWidget *parent) : QTimer::singleShot(0,this,SLOT(earlyExit())); hide(); } + popupMenu=new QMenu(this); + popupMenu->addAction("Open Profile",this,SLOT(openProfile())); + popupMenu->addAction("Edit Profile",this,SLOT(editProfile())); + popupMenu->addSeparator(); + popupMenu->addAction("Delete Profile",this,SLOT(deleteProfile())); } ProfileSelect::~ProfileSelect() { + delete popupMenu; delete ui; } void ProfileSelect::earlyExit() { accept(); } +void ProfileSelect::editProfile() +{ + QString name=ui->listView->currentIndex().data().toString(); + + NewProfile newprof(this); + newprof.edit(name); + newprof.exec(); + + //qDebug() << "edit" << name; +} +void ProfileSelect::deleteProfile() +{ + QString name=ui->listView->currentIndex().data().toString(); + if (QMessageBox::question(this,"Question","Are you sure you want to trash the profile \""+name+"\"?",QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){ + if (QMessageBox::question(this,"Question","Double Checking: Do you really want \""+name+"\" profile to be obliterated?",QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){ + if (QMessageBox::question(this,"Question","Last chance to save the \""+name+"\" profile. Are you totally sure?",QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){ + bool reallydelete=false; + Profile *profile=Profiles::profiles[name]; + if (!profile) { + QMessageBox::warning(this,"WTH???","If you can read this you need to delete this profile directory manually (It's under Your Documents folder -> SleepApp -> Profiles -> [profile_name])",QMessageBox::Ok); + return; + } + if (profile->Exists("Password")) { + QDialog dialog(this,Qt::Dialog); + QLineEdit *e=new QLineEdit(&dialog); + e->setEchoMode(QLineEdit::Password); + dialog.connect(e,SIGNAL(returnPressed()),&dialog,SLOT(accept())); + dialog.setWindowTitle("Enter Password for "+name); + dialog.setMinimumWidth(300); + QVBoxLayout *lay=new QVBoxLayout(); + dialog.setLayout(lay); + lay->addWidget(e); + int tries=0; + do { + e->setText(""); + if (dialog.exec()!=QDialog::Accepted) break; + QByteArray ba=e->text().toUtf8(); + tries++; + if (QCryptographicHash::hash(ba,QCryptographicHash::Sha1).toHex()==(*profile)["Password"]) { + reallydelete=true; + break; + } else { + if (tries<3) { + QMessageBox::warning(this,"Error","Incorrect Password",QMessageBox::Ok); + } else { + QMessageBox::warning(this,"Error","Meheh... If your trying to delete because you forgot the password, your going the wrong way about it. Read the docs.\n\nSigned: Nasty Programmer",QMessageBox::Ok); + } + } + } while (tries<3); + } else reallydelete=true; + + if (reallydelete) { + QMessageBox::information(this,"Whoops.","After all that nagging, I haven't got around to writing this code yet.. For now you can delete the directory in SleepApp -> Profiles -> [profile_name]",QMessageBox::Ok); + qDebug() << "delete" << name; + } + } + } + } +} + void ProfileSelect::on_selectButton_clicked() { on_listView_activated(ui->listView->currentIndex()); } +void ProfileSelect::openProfile() +{ + on_listView_activated(ui->listView->currentIndex()); +} void ProfileSelect::on_newProfileButton_clicked() { @@ -114,3 +184,8 @@ void ProfileSelect::on_listView_activated(const QModelIndex &index) } accept(); } + +void ProfileSelect::on_listView_customContextMenuRequested(const QPoint &pos) +{ + popupMenu->popup(QWidget::mapToGlobal(pos)); +} diff --git a/profileselect.h b/profileselect.h index 9077fd26..ace5eb39 100644 --- a/profileselect.h +++ b/profileselect.h @@ -3,6 +3,7 @@ #include #include +#include namespace Ui { class ProfileSelect; @@ -25,10 +26,17 @@ private slots: void on_listView_activated(const QModelIndex &index); void earlyExit(); + void openProfile(); + void editProfile(); + void deleteProfile(); + + void on_listView_customContextMenuRequested(const QPoint &pos); + private: Ui::ProfileSelect *ui; QString m_selectedProfile; int m_tries; + QMenu *popupMenu; }; #endif // PROFILESELECT_H diff --git a/profileselect.ui b/profileselect.ui index a39db627..ce00ebd4 100644 --- a/profileselect.ui +++ b/profileselect.ui @@ -15,7 +15,11 @@ - + + + Qt::CustomContextMenu + +