You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 line
3.0 KiB

  1. #include <string.h>
  2. #include "core/common.h"
  3. #include "core/buttons.h"
  4. #include "core/display.h"
  5. #include <epaper/EPD.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. #include "reader/reader_storage.h"
  12. #include "SdCardMenuMode.h"
  13. #include "esp_log.h"
  14. static char* TAG = "SdCardMenuMode";
  15. #define SDCARD_BASEDIR "/sdcard/"
  16. // TODO: make use of bytes
  17. void SdCardMenuMode::start()
  18. {
  19. display_refresh();
  20. dir_entry_t* dir_chain_start = new dir_entry_t;
  21. strcpy(dir_chain_start->name, "[Back]");
  22. dir_entry_t* last_entry = dir_chain_start;
  23. int entries = 1;
  24. struct stat stats;
  25. struct dirent *dir;
  26. DIR* d = opendir(this->basedir);
  27. if (d)
  28. {
  29. while ((dir = readdir(d)) != NULL)
  30. {
  31. ESP_LOGI(TAG, "* %s", dir->d_name);
  32. dir_entry_t* new_entry = new dir_entry_t;
  33. strcpy(new_entry->name, dir->d_name);
  34. char path[64];
  35. strcpy(path, this->basedir);
  36. strcat(path, dir->d_name);
  37. if (stat(path, &stats) == 0) {
  38. last_entry->bytes = stats.st_size;
  39. }
  40. new_entry->next = NULL;
  41. if (dir_chain_start == NULL) {
  42. dir_chain_start = new_entry;
  43. }
  44. if (last_entry == NULL) {
  45. last_entry = new_entry;
  46. } else {
  47. last_entry->next = new_entry;
  48. last_entry = new_entry;
  49. }
  50. entries++;
  51. }
  52. closedir(d);
  53. } else {
  54. ESP_LOGE(TAG, "Could not open dir.");
  55. }
  56. this->optionsNames = new char[entries*DIR_ENTRY_NAME_SIZE];
  57. this->options = new char*[entries];
  58. this->optionsBytes = new unsigned long[entries];
  59. this->optionsSize = entries;
  60. last_entry = dir_chain_start;
  61. int i = 0;
  62. while (last_entry != NULL) {
  63. this->options[i] = &this->optionsNames[i * DIR_ENTRY_NAME_SIZE];
  64. memcpy(this->options[i], last_entry->name, DIR_ENTRY_NAME_SIZE);
  65. this->optionsBytes[i] = last_entry->bytes;
  66. dir_entry_t* next = (dir_entry_t*)last_entry->next;
  67. delete last_entry;
  68. last_entry = next;
  69. i++;
  70. }
  71. }
  72. void SdCardMenuMode::finish()
  73. {
  74. delete this->options;
  75. delete this->optionsNames;
  76. delete this->optionsBytes;
  77. }
  78. char* SdCardMenuMode::getTitle()
  79. {
  80. return "SD Card";
  81. }
  82. char** SdCardMenuMode::getOptions()
  83. {
  84. return this->options;
  85. }
  86. int SdCardMenuMode::getOptionsSize()
  87. {
  88. return this->optionsSize;
  89. }
  90. void SdCardMenuMode::onOptionSelected(int option)
  91. {
  92. if (option == 0) {
  93. this->setFinished();
  94. return;
  95. }
  96. // TODO: spawn a copy AppMode
  97. display_alert("Copying into internal memory...");
  98. char source[64];
  99. strcpy(source, SDCARD_BASEDIR);
  100. strcat(source, this->options[option]);
  101. reader_storage_store_file(source);
  102. reader_storage_set_position(0);
  103. this->setFinished();
  104. }
  105. int SdCardMenuMode::getOptionsX()
  106. {
  107. return 35;
  108. }
  109. int SdCardMenuMode::getOptionsFont()
  110. {
  111. return SMALL_FONT;
  112. }