quac_settings.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "quac_settings.h"
  2. #include <flipper_format/flipper_format.h>
  3. // Quac Settings File Info
  4. // "/ext/apps_data/quac/.quac.conf"
  5. #define QUAC_SETTINGS_FILENAME QUAC_DATA_PATH "/.quac.conf"
  6. #define QUAC_SETTINGS_FILE_TYPE "Quac Settings File"
  7. #define QUAC_SETTINGS_FILE_VERSION 1
  8. void quac_set_default_settings(App* app) {
  9. app->settings.rfid_duration = 2500;
  10. app->settings.layout = QUAC_APP_LANDSCAPE;
  11. app->settings.show_icons = true;
  12. app->settings.show_headers = true;
  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_FILENAME);
  20. bool successful = false;
  21. do {
  22. if(!flipper_format_file_open_existing(fff_settings, QUAC_SETTINGS_FILENAME)) {
  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 == 0) ? false : true;
  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. successful = true;
  65. } while(false);
  66. if(!successful) {
  67. quac_set_default_settings(app);
  68. }
  69. furi_string_free(temp_str);
  70. flipper_format_free(fff_settings);
  71. }
  72. void quac_save_settings(App* app) {
  73. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  74. uint32_t temp_data32;
  75. FURI_LOG_I(TAG, "SETTINGS: Saving");
  76. bool successful = false;
  77. do {
  78. if(!flipper_format_file_open_always(fff_settings, QUAC_SETTINGS_FILENAME)) {
  79. FURI_LOG_E(TAG, "SETTINGS: Unable to open file for save!!");
  80. break;
  81. }
  82. if(!flipper_format_write_header_cstr(
  83. fff_settings, QUAC_SETTINGS_FILE_TYPE, QUAC_SETTINGS_FILE_VERSION)) {
  84. FURI_LOG_E(TAG, "SETTINGS: Failed writing file type and version");
  85. break;
  86. }
  87. // layout, icons, headers, duration
  88. if(!flipper_format_write_string_cstr(
  89. fff_settings,
  90. "Layout",
  91. app->settings.layout == QUAC_APP_LANDSCAPE ? "Landscape" : "Portrait")) {
  92. FURI_LOG_E(TAG, "SETTINGS: Failed to write Layout");
  93. break;
  94. }
  95. temp_data32 = app->settings.show_icons ? 1 : 0;
  96. if(!flipper_format_write_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  97. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Icons'");
  98. break;
  99. }
  100. temp_data32 = app->settings.show_headers ? 1 : 0;
  101. if(!flipper_format_write_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  102. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Headers'");
  103. break;
  104. }
  105. if(!flipper_format_write_uint32(
  106. fff_settings, "RFID Duration", &app->settings.rfid_duration, 1)) {
  107. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'RFID Duration'");
  108. break;
  109. }
  110. successful = true;
  111. } while(false);
  112. if(!successful) {
  113. FURI_LOG_E(TAG, "SETTINGS: Failed to save settings!!");
  114. }
  115. flipper_format_file_close(fff_settings);
  116. flipper_format_free(fff_settings);
  117. }