unlock.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "unlock.h"
  2. #include "ui_unlock.h"
  3. #include <QDebug>
  4. #include <QJsonObject>
  5. #include <QJsonDocument>
  6. #include <QFile>
  7. #include <QCryptographicHash>
  8. #include <QProcess>
  9. QJsonObject unlockConfig;
  10. bool unlocked = false;
  11. void Unlock::alarm() {
  12. QProcess* p = new QProcess;
  13. p->start(unlockConfig["command"].toString());
  14. }
  15. void Unlock::quit() {
  16. Unlock::~Unlock();
  17. exit(0);
  18. }
  19. Unlock::Unlock(QWidget *parent) : QWidget(parent), ui(new Ui::Unlock) {
  20. ui->setupUi(this);
  21. QString homeDirectory = getenv("HOME");
  22. QFile file;
  23. QString data;
  24. file.setFileName(homeDirectory + "/.config/fake-gnome-keyring/config.json");
  25. file.open(QIODevice::ReadOnly | QIODevice::Text);
  26. data = file.readAll();
  27. file.close();
  28. unlockConfig = QJsonDocument::fromJson(data.toUtf8()).object();
  29. this->connect(ui->unlockPushButton, &QPushButton::clicked, this,
  30. [this]() {
  31. QString realMD5 = unlockConfig["pwdHash"].toString();
  32. QString userMD5 = QCryptographicHash::hash(ui->pwdLineEdit->text().toLatin1(), QCryptographicHash::Md5).toHex();
  33. if (userMD5 == realMD5) {
  34. unlocked = true;
  35. }
  36. quit();
  37. });
  38. this->connect(ui->cancelPushButton, &QPushButton::clicked, this,
  39. [this]() {
  40. quit();
  41. });
  42. }
  43. Unlock::~Unlock() {
  44. if (!unlocked) {
  45. alarm();
  46. }
  47. delete ui;
  48. }