token_info_iterator.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include "../../types/token_info.h"
  3. #include <flipper_format/flipper_format.h>
  4. #include "constants.h"
  5. typedef int TotpIteratorUpdateTokenResult;
  6. typedef TotpIteratorUpdateTokenResult (
  7. *TOTP_ITERATOR_UPDATE_TOKEN_ACTION)(TokenInfo* const token_info, const void* context);
  8. typedef struct TokenInfoIteratorContext TokenInfoIteratorContext;
  9. enum TotpIteratorUpdateTokenResults {
  10. /**
  11. * @brief Token successfully updated
  12. */
  13. TotpIteratorUpdateTokenResultSuccess = 0,
  14. /**
  15. * @brief An error ocurred during updating config file
  16. */
  17. TotpIteratorUpdateTokenResultFileUpdateFailed = -1
  18. };
  19. /**
  20. * @brief Initializes a new token info iterator
  21. * @param storage storage reference
  22. * @param config_file config file to use
  23. * @param crypto_settings crypto settings
  24. * @return Token info iterator context
  25. */
  26. TokenInfoIteratorContext* totp_token_info_iterator_alloc(
  27. Storage* storage,
  28. FlipperFormat* config_file,
  29. CryptoSettings* crypto_settings);
  30. /**
  31. * @brief Navigates iterator to the token with given index
  32. * @param context token info iterator context
  33. * @param token_index token index to navigate to
  34. * @return \c true if navigation succeeded; \c false otherwise
  35. */
  36. bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t token_index);
  37. /**
  38. * @brief Moves current token to a given new index
  39. * @param context token info iterator context
  40. * @param new_index new token index to move current token to
  41. * @return \c true if operation succeeded; \c false otherwise
  42. */
  43. bool totp_token_info_iterator_move_current_token_info(
  44. TokenInfoIteratorContext* context,
  45. size_t new_index);
  46. /**
  47. * @brief Updates current token info using given update action
  48. * @param context token info iterator context
  49. * @param update action which is responsible to make all the necessary updates to token info
  50. * @param update_context update action context
  51. * @return \c true if operation succeeded; \c false otherwise
  52. */
  53. TotpIteratorUpdateTokenResult totp_token_info_iterator_update_current_token(
  54. TokenInfoIteratorContext* context,
  55. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  56. const void* update_context);
  57. /**
  58. * @brief Adds new token info to the end of the list using given update action
  59. * @param context token info iterator context
  60. * @param update action which is responsible to make all the necessary updates to token info
  61. * @param update_context update action context
  62. * @return \c true if operation succeeded; \c false otherwise
  63. */
  64. TotpIteratorUpdateTokenResult totp_token_info_iterator_add_new_token(
  65. TokenInfoIteratorContext* context,
  66. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  67. const void* update_context);
  68. /**
  69. * @brief Remvoves current token info
  70. * @param context token info iterator context
  71. * @return \c true if operation succeeded; \c false otherwise
  72. */
  73. bool totp_token_info_iterator_remove_current_token_info(TokenInfoIteratorContext* context);
  74. /**
  75. * @brief Disposes token info iterator and releases all the resources
  76. * @param context token info iterator context
  77. */
  78. void totp_token_info_iterator_free(TokenInfoIteratorContext* context);
  79. /**
  80. * @brief Gets current token info
  81. * @param context token info iterator context
  82. * @return current token info
  83. */
  84. const TokenInfo*
  85. totp_token_info_iterator_get_current_token(const TokenInfoIteratorContext* context);
  86. /**
  87. * @brief Gets current token info index
  88. * @param context token info iterator context
  89. * @return current token info index
  90. */
  91. size_t totp_token_info_iterator_get_current_token_index(const TokenInfoIteratorContext* context);
  92. /**
  93. * @brief Gets total amount of token infos found
  94. * @param context token info iterator context
  95. * @return amount of token infos found
  96. */
  97. size_t totp_token_info_iterator_get_total_count(const TokenInfoIteratorContext* context);
  98. /**
  99. * @brief Attaches token info iterator to another config file
  100. * @param context token info iterator context
  101. * @param config_file config file reference to attach token info iterator to
  102. */
  103. void totp_token_info_iterator_attach_to_config_file(
  104. TokenInfoIteratorContext* context,
  105. FlipperFormat* config_file);