scene_action_rename.c 3.8 KB

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