filebackup.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "filebackup.h"
  2. #include <QFile>
  3. #include <OpenDrive/OpenDriveXmlWriter.h>
  4. FileBackup::FileBackup()
  5. {
  6. }
  7. FileBackup::~FileBackup()
  8. {
  9. this->requestInterruption();
  10. QTime xTime;
  11. xTime.start();
  12. while((xTime.elapsed()<200)&&(mbComplete == false))
  13. {
  14. }
  15. }
  16. void FileBackup::SetOpenDrive(OpenDrive &xxodr)
  17. {
  18. mMutex.lock();
  19. mxodr = xxodr;
  20. mMutex.unlock();
  21. mbUpdate = true;
  22. mwc.wakeAll();
  23. }
  24. void FileBackup::Activate(QString strpath)
  25. {
  26. mMutex.lock();
  27. mstrpath = strpath+".bak";
  28. mMutex.unlock();
  29. mbactive = true;
  30. }
  31. void FileBackup::run()
  32. {
  33. QString strpath;
  34. while(!QThread::isInterruptionRequested())
  35. {
  36. if(mbactive)
  37. {
  38. mWaitMutex.lock();
  39. mwc.wait(&mWaitMutex,50);
  40. mWaitMutex.unlock();
  41. if(mbUpdate)
  42. {
  43. mMutex.lock();
  44. OpenDrive xxodr = mxodr;
  45. strpath = mstrpath;
  46. mMutex.unlock();
  47. mbUpdate = false;
  48. //Save bak file
  49. OpenDriveXmlWriter x(&xxodr);
  50. x.WriteFile(strpath.toStdString());
  51. }
  52. }
  53. else
  54. {
  55. msleep(50);
  56. }
  57. }
  58. if(mbactive)
  59. {
  60. QFile fileBack(mstrpath);
  61. fileBack.remove();
  62. //Delete bak file
  63. }
  64. mbComplete = true;
  65. }