quac_settings.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "quac_settings.h"
  2. #include <flipper_format/flipper_format.h>
  3. // Quac Settings File Info
  4. #define QUAC_SETTINGS_FILE_TYPE "Quac Settings File"
  5. #define QUAC_SETTINGS_FILE_VERSION 1
  6. void quac_set_default_settings(App* app) {
  7. app->settings.layout = QUAC_APP_LANDSCAPE;
  8. app->settings.show_icons = true;
  9. app->settings.show_headers = true;
  10. app->settings.rfid_duration = 2500;
  11. app->settings.nfc_duration = 1000;
  12. app->settings.subghz_use_ext_antenna = false;
  13. app->settings.show_hidden = false;
  14. }
  15. void quac_load_settings(App* app) {
  16. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  17. FuriString* temp_str;
  18. temp_str = furi_string_alloc();
  19. uint32_t temp_data32 = 0;
  20. // Initialize settings to the defaults
  21. quac_set_default_settings(app);
  22. FURI_LOG_I(TAG, "SETTINGS: Reading: %s", QUAC_SETTINGS_PATH);
  23. do {
  24. if(!flipper_format_file_open_existing(fff_settings, QUAC_SETTINGS_PATH)) {
  25. FURI_LOG_I(TAG, "SETTINGS: File not found, using defaults");
  26. break;
  27. }
  28. if(!flipper_format_read_header(fff_settings, temp_str, &temp_data32)) {
  29. FURI_LOG_E(TAG, "SETTINGS: Missing or incorrect header");
  30. break;
  31. }
  32. if((!strcmp(furi_string_get_cstr(temp_str), QUAC_SETTINGS_FILE_TYPE)) &&
  33. (temp_data32 == QUAC_SETTINGS_FILE_VERSION)) {
  34. } else {
  35. FURI_LOG_E(TAG, "SETTINGS: Type or version mismatch");
  36. break;
  37. }
  38. // Now read actual values we care about
  39. if(!flipper_format_read_string(fff_settings, "Layout", temp_str)) {
  40. FURI_LOG_W(TAG, "SETTINGS: Missing Layout");
  41. } else {
  42. if(!strcmp(furi_string_get_cstr(temp_str), "Landscape")) {
  43. app->settings.layout = QUAC_APP_LANDSCAPE;
  44. } else if(!strcmp(furi_string_get_cstr(temp_str), "Portrait")) {
  45. app->settings.layout = QUAC_APP_PORTRAIT;
  46. } else {
  47. FURI_LOG_E(TAG, "SETTINGS: Invalid Layout");
  48. }
  49. }
  50. if(!flipper_format_read_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  51. FURI_LOG_W(TAG, "SETTINGS: Missing 'Show Icons'");
  52. } else {
  53. app->settings.show_icons = (temp_data32 == 0) ? false : true;
  54. }
  55. if(!flipper_format_read_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  56. FURI_LOG_W(TAG, "SETTINGS: Missing 'Show Headers'");
  57. } else {
  58. app->settings.show_headers = (temp_data32 == 1) ? true : false;
  59. }
  60. if(!flipper_format_read_uint32(fff_settings, "RFID Duration", &temp_data32, 1)) {
  61. FURI_LOG_W(TAG, "SETTINGS: Missing 'RFID Duration'");
  62. } else {
  63. app->settings.rfid_duration = temp_data32;
  64. }
  65. if(!flipper_format_read_uint32(fff_settings, "NFC Duration", &temp_data32, 1)) {
  66. FURI_LOG_W(TAG, "SETTINGS: Missing 'NFC Duration'");
  67. } else {
  68. app->settings.nfc_duration = temp_data32;
  69. }
  70. if(!flipper_format_read_uint32(fff_settings, "SubGHz Ext Antenna", &temp_data32, 1)) {
  71. FURI_LOG_W(TAG, "SETTINGS: Missing 'SubGHz Ext Antenna'");
  72. } else {
  73. app->settings.subghz_use_ext_antenna = (temp_data32 == 1) ? true : false;
  74. }
  75. if(!flipper_format_read_uint32(fff_settings, "Show Hidden", &temp_data32, 1)) {
  76. FURI_LOG_W(TAG, "SETTINGS: Missing 'Show Hidden'");
  77. } else {
  78. app->settings.show_hidden = (temp_data32 == 1) ? true : false;
  79. }
  80. } while(false);
  81. furi_string_free(temp_str);
  82. flipper_format_free(fff_settings);
  83. }
  84. void quac_save_settings(App* app) {
  85. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  86. uint32_t temp_data32;
  87. FURI_LOG_I(TAG, "SETTINGS: Saving");
  88. bool successful = false;
  89. do {
  90. if(!flipper_format_file_open_always(fff_settings, QUAC_SETTINGS_PATH)) {
  91. FURI_LOG_E(TAG, "SETTINGS: Unable to open file for save!!");
  92. break;
  93. }
  94. if(!flipper_format_write_header_cstr(
  95. fff_settings, QUAC_SETTINGS_FILE_TYPE, QUAC_SETTINGS_FILE_VERSION)) {
  96. FURI_LOG_E(TAG, "SETTINGS: Failed writing file type and version");
  97. break;
  98. }
  99. // layout, icons, headers, duration
  100. if(!flipper_format_write_string_cstr(
  101. fff_settings,
  102. "Layout",
  103. app->settings.layout == QUAC_APP_LANDSCAPE ? "Landscape" : "Portrait")) {
  104. FURI_LOG_E(TAG, "SETTINGS: Failed to write Layout");
  105. break;
  106. }
  107. temp_data32 = app->settings.show_icons ? 1 : 0;
  108. if(!flipper_format_write_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  109. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Icons'");
  110. break;
  111. }
  112. temp_data32 = app->settings.show_headers ? 1 : 0;
  113. if(!flipper_format_write_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  114. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Headers'");
  115. break;
  116. }
  117. if(!flipper_format_write_uint32(
  118. fff_settings, "RFID Duration", &app->settings.rfid_duration, 1)) {
  119. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'RFID Duration'");
  120. break;
  121. }
  122. if(!flipper_format_write_uint32(
  123. fff_settings, "NFC Duration", &app->settings.nfc_duration, 1)) {
  124. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'NFC Duration'");
  125. break;
  126. }
  127. temp_data32 = app->settings.subghz_use_ext_antenna ? 1 : 0;
  128. if(!flipper_format_write_uint32(fff_settings, "SubGHz Ext Antenna", &temp_data32, 1)) {
  129. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'SubGHz Ext Antenna'");
  130. break;
  131. }
  132. temp_data32 = app->settings.show_hidden ? 1 : 0;
  133. if(!flipper_format_write_uint32(fff_settings, "Show Hidden", &temp_data32, 1)) {
  134. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Hidden'");
  135. break;
  136. }
  137. successful = true;
  138. } while(false);
  139. if(!successful) {
  140. FURI_LOG_E(TAG, "SETTINGS: Failed to save settings!!");
  141. }
  142. flipper_format_file_close(fff_settings);
  143. flipper_format_free(fff_settings);
  144. }