scene_action_rename.c 4.1 KB

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