quac_settings.c 5.7 KB

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