scene_action_rename.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <furi.h>
  2. #include <gui/view_dispatcher.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/modules/text_input.h>
  5. #include "quac.h"
  6. #include "scenes.h"
  7. #include "scene_action_rename.h"
  8. #include "../actions/action.h"
  9. #include <lib/toolbox/path.h>
  10. enum {
  11. SceneActionRenameEvent,
  12. };
  13. void scene_action_rename_callback(void* context) {
  14. App* app = context;
  15. view_dispatcher_send_custom_event(app->view_dispatcher, SceneActionRenameEvent);
  16. }
  17. void scene_action_rename_on_enter(void* context) {
  18. App* app = context;
  19. TextInput* text = app->text_input;
  20. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  21. text_input_set_header_text(text, "Enter new name:");
  22. FuriString* file_name = furi_string_alloc();
  23. path_extract_filename_no_ext(furi_string_get_cstr(item->path), file_name);
  24. strncpy(app->temp_cstr, furi_string_get_cstr(file_name), MAX_NAME_LEN);
  25. text_input_set_result_callback(
  26. text, scene_action_rename_callback, app, app->temp_cstr, MAX_NAME_LEN, false);
  27. furi_string_free(file_name);
  28. view_dispatcher_switch_to_view(app->view_dispatcher, QView_TextInput);
  29. }
  30. bool scene_action_rename_on_event(void* context, SceneManagerEvent event) {
  31. App* app = context;
  32. bool consumed = false;
  33. if(event.type == SceneManagerEventTypeCustom) {
  34. if(event.event == SceneActionRenameEvent) {
  35. // FURI_LOG_I(TAG, "Attempting rename to %s", app->temp_cstr);
  36. if(!strcmp(app->temp_cstr, "")) {
  37. return false;
  38. }
  39. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  40. const char* old_path = furi_string_get_cstr(item->path);
  41. FuriString* file_name = furi_string_alloc();
  42. path_extract_filename(item->path, file_name, true);
  43. // FURI_LOG_I(TAG, "Original name is %s", furi_string_get_cstr(file_name));
  44. if(!furi_string_cmp_str(file_name, app->temp_cstr)) {
  45. FURI_LOG_W(TAG, "Rename: File names are the same!");
  46. furi_string_free(file_name);
  47. return false;
  48. }
  49. // build the new name full path, with extension
  50. FuriString* dir_name = furi_string_alloc();
  51. path_extract_dirname(old_path, dir_name);
  52. FuriString* new_path = furi_string_alloc_printf(
  53. "%s/%s%s", furi_string_get_cstr(dir_name), app->temp_cstr, item->ext);
  54. FURI_LOG_I(TAG, "Rename: %s to %s", old_path, furi_string_get_cstr(new_path));
  55. FS_Error fs_result =
  56. storage_common_rename(app->storage, old_path, furi_string_get_cstr(new_path));
  57. if(fs_result == FSE_OK) {
  58. ItemsView* new_items = item_get_items_view_from_path(app, dir_name);
  59. item_items_view_free(app->items_view);
  60. app->items_view = new_items;
  61. // furi_string_swap(item->path, new_path);
  62. // furi_string_set_str(item->name, app->temp_cstr);
  63. // item_prettify_name(item->name);
  64. } else {
  65. FURI_LOG_E(
  66. TAG, "Rename file failed! %s", filesystem_api_error_get_desc(fs_result));
  67. FuriString* error_msg = furi_string_alloc_printf(
  68. "Rename failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  69. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  70. furi_string_free(error_msg);
  71. }
  72. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, QScene_Items);
  73. furi_string_free(dir_name);
  74. furi_string_free(file_name);
  75. furi_string_free(new_path);
  76. consumed = true;
  77. }
  78. }
  79. return consumed;
  80. }
  81. void scene_action_rename_on_exit(void* context) {
  82. App* app = context;
  83. text_input_reset(app->text_input);
  84. }