2
0

astandardpaths.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "src/classes/astandardpaths.h"
  19. #include "src/opl.h"
  20. QHash<AStandardPaths::Directories, QDir> AStandardPaths::directories;
  21. bool AStandardPaths::setup()
  22. {
  23. auto data_location = QStandardPaths::AppDataLocation;
  24. directories = {
  25. {Database, QDir(QStandardPaths::writableLocation(data_location))},
  26. {Templates, QDir(QStandardPaths::writableLocation(data_location)
  27. + QLatin1String("/templates"))},
  28. {Backup, QDir(QStandardPaths::writableLocation(data_location)
  29. + QLatin1String("/backup"))},
  30. {Log, QDir(QStandardPaths::writableLocation(data_location)
  31. + QLatin1String("/log"))},
  32. {JSON, QDir(QStandardPaths::writableLocation(data_location)
  33. + QLatin1String("/json"))}
  34. };
  35. if (scan_directories())
  36. return true;
  37. return false;
  38. }
  39. const QDir& AStandardPaths::directory(Directories location)
  40. {
  41. return directories[location];
  42. }
  43. const QString AStandardPaths::asChildOfDir(Directories location, const QString &filename)
  44. {
  45. return directories[location].absoluteFilePath(filename);
  46. }
  47. const QHash<AStandardPaths::Directories, QDir>& AStandardPaths::allDirectories()
  48. {
  49. return directories;
  50. }
  51. bool AStandardPaths::scan_directories()
  52. {
  53. for(const auto& dir : qAsConst(directories)){
  54. if(!dir.exists()) {
  55. LOG << dir << "Does not exist. Creating:" << dir.absolutePath();
  56. if (!dir.mkpath(dir.absolutePath()))
  57. return false;
  58. }
  59. }
  60. return true;
  61. }