quac_settings.c 7.2 KB

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