quac_settings.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "quac_settings.h"
  2. #include <flipper_format/flipper_format.h>
  3. // Quac Settings File Info
  4. // TODO: Fix this path to use existing #defs for /ext, etc
  5. #define QUAC_SETTINGS_FILENAME "/ext/apps_data/quac/.quac.conf"
  6. #define QUAC_SETTINGS_FILE_TYPE "Quac Settings File"
  7. #define QUAC_SETTINGS_FILE_VERSION 1
  8. // Quac Settings Defaults
  9. #define QUAC_SETTINGS_DEFAULT_RFID_DURATION 2500
  10. #define QUAC_SETTINGS_DEFAULT_LAYOUT QUAC_APP_LANDSCAPE // QUAC_APP_PORTRAIT
  11. #define QUAC_SETTINGS_DEFAULT_SHOW_ICONS true
  12. #define QUAC_SETTINGS_DEFAULT_SHOW_HEADERS true
  13. void quac_set_default_settings(App* app) {
  14. app->settings.rfid_duration = QUAC_SETTINGS_DEFAULT_RFID_DURATION;
  15. app->settings.layout = QUAC_SETTINGS_DEFAULT_LAYOUT;
  16. app->settings.show_icons = QUAC_SETTINGS_DEFAULT_SHOW_ICONS;
  17. app->settings.show_headers = QUAC_SETTINGS_DEFAULT_SHOW_HEADERS;
  18. }
  19. void quac_load_settings(App* app) {
  20. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  21. FuriString* temp_str;
  22. temp_str = furi_string_alloc();
  23. uint32_t temp_data32 = 0;
  24. FURI_LOG_I(TAG, "SETTINGS: Reading settings file");
  25. bool successful = false;
  26. do {
  27. if(!flipper_format_file_open_existing(fff_settings, QUAC_SETTINGS_FILENAME)) {
  28. FURI_LOG_I(TAG, "SETTINGS: File not found, loading defaults");
  29. break;
  30. }
  31. if(!flipper_format_read_header(fff_settings, temp_str, &temp_data32)) {
  32. FURI_LOG_E(TAG, "SETTINGS: Missing or incorrect header");
  33. break;
  34. }
  35. if((!strcmp(furi_string_get_cstr(temp_str), QUAC_SETTINGS_FILE_TYPE)) &&
  36. (temp_data32 == QUAC_SETTINGS_FILE_VERSION)) {
  37. } else {
  38. FURI_LOG_E(TAG, "SETTINGS: Type or version mismatch");
  39. break;
  40. }
  41. // Now read actual values we care about
  42. if(!flipper_format_read_string(fff_settings, "Layout", temp_str)) {
  43. FURI_LOG_E(TAG, "SETTINGS: Missing Layout");
  44. break;
  45. }
  46. if(!strcmp(furi_string_get_cstr(temp_str), "Landscape")) {
  47. app->settings.layout = QUAC_APP_LANDSCAPE;
  48. } else if(!strcmp(furi_string_get_cstr(temp_str), "Portrait")) {
  49. app->settings.layout = QUAC_APP_PORTRAIT;
  50. } else {
  51. FURI_LOG_E(TAG, "SETTINGS: Invalid Layout");
  52. break;
  53. }
  54. if(!flipper_format_read_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  55. FURI_LOG_E(TAG, "SETTINGS: Missing 'Show Icons'");
  56. break;
  57. }
  58. app->settings.show_icons = (temp_data32 == 0) ? false : true;
  59. if(!flipper_format_read_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  60. FURI_LOG_E(TAG, "SETTINGS: Missing 'Show Headers'");
  61. break;
  62. }
  63. app->settings.show_headers = (temp_data32 == 0) ? false : true;
  64. if(!flipper_format_read_uint32(fff_settings, "RFID Duration", &temp_data32, 1)) {
  65. FURI_LOG_E(TAG, "SETTINGS: Missing 'RFID Duration'");
  66. break;
  67. }
  68. app->settings.rfid_duration = temp_data32;
  69. successful = true;
  70. } while(false);
  71. if(!successful) {
  72. quac_set_default_settings(app);
  73. }
  74. furi_string_free(temp_str);
  75. flipper_format_free(fff_settings);
  76. }
  77. void quac_save_settings(App* app) {
  78. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  79. uint32_t temp_data32;
  80. bool successful = false;
  81. do {
  82. if(!flipper_format_file_open_always(fff_settings, QUAC_SETTINGS_FILENAME)) {
  83. FURI_LOG_E(TAG, "SETTINGS: Unable to open file for save!!");
  84. break;
  85. }
  86. if(!flipper_format_write_header_cstr(
  87. fff_settings, QUAC_SETTINGS_FILE_TYPE, QUAC_SETTINGS_FILE_VERSION)) {
  88. FURI_LOG_E(TAG, "SETTINGS: Failed writing file type and version");
  89. break;
  90. }
  91. // layout, icons, headers, duration
  92. if(!flipper_format_write_string_cstr(
  93. fff_settings,
  94. "Layout",
  95. app->settings.layout == QUAC_APP_LANDSCAPE ? "Landscape" : "Portrait")) {
  96. FURI_LOG_E(TAG, "SETTINGS: Failed to write Layout");
  97. break;
  98. }
  99. temp_data32 = app->settings.show_icons ? 1 : 0;
  100. if(!flipper_format_write_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  101. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Icons'");
  102. break;
  103. }
  104. temp_data32 = app->settings.show_headers ? 1 : 0;
  105. if(!flipper_format_write_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  106. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Headers'");
  107. break;
  108. }
  109. if(!flipper_format_write_uint32(
  110. fff_settings, "RFID Duration", &app->settings.rfid_duration, 1)) {
  111. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'RFID Duration'");
  112. break;
  113. }
  114. successful = true;
  115. } while(false);
  116. if(!successful) {
  117. FURI_LOG_E(TAG, "SETTINGS: Failed to save settings!!");
  118. }
  119. flipper_format_file_close(fff_settings);
  120. flipper_format_free(fff_settings);
  121. }