2021-09-10 00:57:43 +00:00
|
|
|
#include "../archive_i.h"
|
|
|
|
#include "../helpers/archive_favorites.h"
|
|
|
|
#include "../helpers/archive_files.h"
|
2021-09-21 10:56:33 +00:00
|
|
|
#include "../helpers/archive_browser.h"
|
2022-06-09 07:09:52 +00:00
|
|
|
#include "archive/views/archive_browser_view.h"
|
|
|
|
#include "toolbox/path.h"
|
2022-09-15 18:00:51 +00:00
|
|
|
#include <dialogs/dialogs.h>
|
|
|
|
|
|
|
|
#define TAG "Archive"
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
#define SCENE_RENAME_CUSTOM_EVENT (0UL)
|
2021-09-28 16:18:06 +00:00
|
|
|
#define MAX_TEXT_INPUT_LEN 22
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
void archive_scene_rename_text_input_callback(void* context) {
|
|
|
|
ArchiveApp* archive = (ArchiveApp*)context;
|
|
|
|
view_dispatcher_send_custom_event(archive->view_dispatcher, SCENE_RENAME_CUSTOM_EVENT);
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
void archive_scene_rename_on_enter(void* context) {
|
2022-09-15 22:21:10 +00:00
|
|
|
ArchiveApp* archive = context;
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
TextInput* text_input = archive->text_input;
|
2021-09-21 10:56:33 +00:00
|
|
|
ArchiveFile_t* current = archive_get_current_file(archive->browser);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-09-15 18:00:51 +00:00
|
|
|
string_t path_name;
|
|
|
|
string_init(path_name);
|
|
|
|
|
|
|
|
if(current->type == ArchiveFileTypeFolder) {
|
|
|
|
path_extract_basename(string_get_cstr(current->path), path_name);
|
2022-09-15 23:16:46 +00:00
|
|
|
strlcpy(archive->text_store, string_get_cstr(path_name), MAX_NAME_LEN);
|
2022-09-15 18:00:51 +00:00
|
|
|
text_input_set_header_text(text_input, "Rename directory:");
|
|
|
|
} else /*if(current->type != ArchiveFileTypeUnknown) */ {
|
|
|
|
path_extract_filename(current->path, path_name, true);
|
2022-09-15 23:16:46 +00:00
|
|
|
strlcpy(archive->text_store, string_get_cstr(path_name), MAX_NAME_LEN);
|
2022-09-15 18:00:51 +00:00
|
|
|
|
|
|
|
path_extract_extension(current->path, archive->file_extension, MAX_EXT_LEN);
|
|
|
|
text_input_set_header_text(text_input, "Rename file:");
|
|
|
|
} /*else {
|
|
|
|
path_extract_filename(current->path, path_name, false);
|
|
|
|
strlcpy(archive->text_store, string_get_cstr(path_name), MAX_NAME_LEN);
|
|
|
|
text_input_set_header_text(text_input, "Rename unknown file:");
|
|
|
|
}*/
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
text_input_set_result_callback(
|
|
|
|
text_input,
|
|
|
|
archive_scene_rename_text_input_callback,
|
2022-09-15 22:21:10 +00:00
|
|
|
context,
|
2021-09-10 00:57:43 +00:00
|
|
|
archive->text_store,
|
2021-09-28 16:18:06 +00:00
|
|
|
MAX_TEXT_INPUT_LEN,
|
2021-09-10 00:57:43 +00:00
|
|
|
false);
|
|
|
|
|
2022-09-15 18:00:51 +00:00
|
|
|
string_clear(path_name);
|
2022-06-09 07:09:52 +00:00
|
|
|
|
2021-09-10 00:57:43 +00:00
|
|
|
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewTextInput);
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
bool archive_scene_rename_on_event(void* context, SceneManagerEvent event) {
|
2022-09-15 22:21:10 +00:00
|
|
|
ArchiveApp* archive = context;
|
2021-09-10 00:57:43 +00:00
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
|
|
|
if(event.event == SCENE_RENAME_CUSTOM_EVENT) {
|
2022-06-09 07:09:52 +00:00
|
|
|
const char* path_src = archive_get_name(archive->browser);
|
2021-09-21 10:56:33 +00:00
|
|
|
ArchiveFile_t* file = archive_get_current_file(archive->browser);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
string_t path_dst;
|
|
|
|
string_init(path_dst);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-09-15 18:00:51 +00:00
|
|
|
if(file->type == ArchiveFileTypeFolder) {
|
|
|
|
// Rename folder/dir
|
|
|
|
path_extract_dirname(path_src, path_dst);
|
|
|
|
string_cat_printf(path_dst, "/%s", archive->text_store);
|
|
|
|
} else if(file->type != ArchiveFileTypeUnknown) {
|
|
|
|
// Rename known type
|
|
|
|
path_extract_dirname(path_src, path_dst);
|
|
|
|
string_cat_printf(path_dst, "/%s%s", archive->text_store, known_ext[file->type]);
|
|
|
|
} else {
|
|
|
|
// Rename unknown type
|
|
|
|
path_extract_dirname(path_src, path_dst);
|
|
|
|
string_cat_printf(path_dst, "/%s%s", archive->text_store, archive->file_extension);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
2022-09-15 18:00:51 +00:00
|
|
|
// Long time process if this is directory
|
2022-09-15 22:21:10 +00:00
|
|
|
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewStack);
|
2022-09-15 18:00:51 +00:00
|
|
|
archive_show_loading_popup(archive, true);
|
|
|
|
FS_Error error =
|
|
|
|
archive_rename_file_or_dir(archive->browser, path_src, string_get_cstr(path_dst));
|
|
|
|
archive_show_loading_popup(archive, false);
|
|
|
|
archive_show_file_menu(archive->browser, false);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
string_clear(path_dst);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-09-15 22:21:10 +00:00
|
|
|
if(error == FSE_OK || error == FSE_EXIST) {
|
|
|
|
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneBrowser);
|
|
|
|
} else {
|
2022-09-15 18:00:51 +00:00
|
|
|
string_t dialog_msg;
|
|
|
|
string_init(dialog_msg);
|
|
|
|
string_cat_printf(dialog_msg, "Cannot rename\nCode: %d", error);
|
|
|
|
dialog_message_show_storage_error(archive->dialogs, string_get_cstr(dialog_msg));
|
|
|
|
string_clear(dialog_msg);
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
void archive_scene_rename_on_exit(void* context) {
|
2022-09-15 22:21:10 +00:00
|
|
|
ArchiveApp* archive = context;
|
2022-01-21 17:32:03 +00:00
|
|
|
text_input_reset(archive->text_input);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|