diff --git a/main.cpp b/main.cpp index 8d94996..5f6314b 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #define IMGUI_IMPLEMENTATION #define GL_SILENCE_DEPRECATION +//DEBUG MACRO #define log_debugc(str, ...) do { if (debug) fprintf(stdout, "[DEBUG] (%s:%d): " (str) "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0) #define log_debugcpp(str) do { \ if (debug) std::cout << "[DEBUG]" << "(" << __FILE__ << ":" << __LINE__ << "): " << str << std::endl; \ @@ -14,10 +15,14 @@ #define log_volume(str) if(debugVerbosity & DEBUG_VOLUME) log_debugcpp((str)) #define log_extension(str) if(debugVerbosity & DEBUG_EXTENSION) log_debugcpp((str)) - +//PATH MACRO #define MAX_LISTDIR_PATH_LENGTH (MAX_PATH - 3) #define MAX_ERRORSTR_LEN 512 +//HISTORY HANDLING MACRO +#define MIN_HISTORY_POS history->historyTraversalPos <= 0 +#define MAX_HISTORY_POS history->historyTraversalPos >= history->historyDepth + #include "imgui/misc/single_file/imgui_single_file.h" #include #include @@ -61,20 +66,49 @@ typedef struct { } directoriesInfo; -struct ErrorMessages{ - const std::string addrBarError = "Failed to open folder: invalid path"; - const std::string tableElementError = "Failed to open folder: access restricted to non-admin users"; - const std::string listDirectoryError = "Failed to open folder: access denied"; - const std::string moveUpError = "Failed to move up: reached volume root?"; +struct History{ + std::vector previousPaths; + //TODO C++ david momento + //int historyDepth = -1; + int historyDepth; + int historyTraversalPos; + bool isAdditionTime; + + History(){ + historyTraversalPos = historyDepth = -1; + isAdditionTime = true; + } +}; + +enum HistoryMovement { + HISTORY_FORWARD = 1, + HISTORY_BACKWARD = 0 +}; + +enum ListDirectoryError { + DIRECTORY_ERROR_PATH_TOO_LONG = -1, + DIRECTORY_ERROR_ACCESSING_CONTENT = -2 +}; + +enum agnosticDirError { + AGDIR_ERROR_ACCESS_DENIED = 1 }; // struct ErrorMessages{ - // static constexpr char addrBarError[] = "Failed to open folder: invalid path"; - // static constexpr char tableElementError[] = "Failed to open folder: access restricted to non-admin users"; - // static constexpr char listDirectoryError[] = "Failed to open folder: access denied"; - // static constexpr char moveUpError[] = "Failed to move up: reached volume root?"; + // const std::string addrBarError = "Failed to open folder: invalid path"; + // const std::string tableElementError = "Failed to open folder: access restricted to non-admin users"; + // const std::string listDirectoryError = "Failed to open folder: access denied"; + // const std::string moveUpError = "Failed to move up: reached volume root?"; // }; +namespace ErrorMessages { + char addrBarError[] = "Failed to open folder: invalid path"; + char tableElementError[] = "Failed to open folder: access restricted to non-admin users"; + char accessDeniedError[] = "Failed to open folder: access denied"; + char listDirectoryError[] = "Failed to open folder: unknown error"; + char moveUpError[] = "Failed to move up: reached volume root?"; +}; + //TODO UNICORDEO /* FILE PICKER MOMENTO */ @@ -99,10 +133,10 @@ int listDirectory(std::string path, std::vector *directoryCont LARGE_INTEGER filesize; int numFiles = 0; - if (path.length() > (MAX_LISTDIR_PATH_LENGTH)) return -1; + if (path.length() > (MAX_LISTDIR_PATH_LENGTH)) return DIRECTORY_ERROR_PATH_TOO_LONG; path = path + "\\*"; hFind = FindFirstFile(path.c_str(), &ffd); - if (INVALID_HANDLE_VALUE == hFind) return -2; + if (INVALID_HANDLE_VALUE == hFind) return DIRECTORY_ERROR_ACCESSING_CONTENT; do { if(!strcmp(ffd.cFileName, ".") || !strcmp(ffd.cFileName, "..")) continue; log_debugcpp("BUCLE listDirectory iteracion " + std::to_string(numFiles)); @@ -141,6 +175,18 @@ int listDirectory(std::string path, std::vector *directoryCont return numFiles; } +long getLastDirectorySystemError(){ + long winLastError = GetLastError(); + switch(winLastError){ + case ERROR_ACCESS_DENIED: + return AGDIR_ERROR_ACCESS_DENIED; + break; + default: + return -1; + break; + } +} + /* VOLUMES */ //new char*[CharCount * sizeof(WCHAR) @@ -166,7 +212,7 @@ int explodePaths(TCHAR* volumePaths, int volumePathsBufferSize, char separator, } itemPath[pathLengthIdx + 1] = '\0'; dest->push_back(itemPath); - //TODO concat de puto C + log_debugcpp(dest->at(dest->size() - 1) << " EXPLODEPATH"); if (nextOcurrence[0] == nextOcurrence[1]) return depth; @@ -198,7 +244,7 @@ int listVolumes(std::vector *onPresentPaths){ if (volumePathLength == 1) { if (debug) std::cout << "Skill Issue" << std::endl; continue; } log_debugcpp(volumeName); - //pathSchecker + //DEBUG: pathSchecker // if (debug && volumePaths[0] == 'E') { // std::cout << volumePathLength << std::endl; // /*exit(1);*/ @@ -260,18 +306,54 @@ bool inputMove(bool* error, char** str){ return true; } -void handleFolderAccessResult(bool isListDirectoriesAdequate, char* currentPath, char* addrBarVal, char* errorDest, const char* errorContent){ - if (isListDirectoriesAdequate) strcpy(currentPath, addrBarVal); +void handleFolderAccessResult(bool isListDirectoriesAdequate, char* currentPath, \ + char* addrBarVal, char* errorDest, const char* errorContent, History* history){ + if (isListDirectoriesAdequate) { strcpy(currentPath, addrBarVal); history->isAdditionTime = true; } else strcpy(addrBarVal, currentPath); strcpy(errorDest, errorContent); } +/* HISTORIAL */ + +void addPathToHistory(History *history, char* pathToAdd){ + char* path; + int ajjj = history->previousPaths.size(); + if (history->historyTraversalPos + 1 >= ajjj){ + path = (char*)calloc(1, MAX_PATH); + history->previousPaths.push_back(path); + } else { + path = history->previousPaths.at(history->historyTraversalPos + 1); + } + strncpy(path, pathToAdd, MAX_PATH); + history->historyTraversalPos++; + history->historyDepth = history->historyTraversalPos; + history->isAdditionTime = false; +} + +char** moveThroughHistory(History *history, HistoryMovement movement){ + switch(movement){ + case HISTORY_BACKWARD: + if(MIN_HISTORY_POS) + return &history->previousPaths.at(history->historyTraversalPos); + history->historyTraversalPos--; + return &history->previousPaths.at(history->historyTraversalPos); + break; + case HISTORY_FORWARD: + if(MAX_HISTORY_POS) + return &history->previousPaths.at(history->historyDepth); + history->historyTraversalPos++; + return &history->previousPaths.at(history->historyTraversalPos); + break; + default: + return &history->previousPaths.at(history->historyDepth); + } +} + /* FIN FILE PICKER MOMENTO */ -int main(int, char**) -{ +int main(int, char**) { // Setup window glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) @@ -387,9 +469,10 @@ int main(int, char**) { ImGui::Begin("File Picker in 4K"); + //TODO is dis bector in hipp? + static History* history = new History(); static bool showError = false; static char error[MAX_ERRORSTR_LEN]; - static ErrorMessages eMsg; static bool isListVolumesAdequate = true; static bool isListDirectoriesAdequate = true; @@ -432,18 +515,46 @@ int main(int, char**) numFiles = listDirectory(std::string(currentPath), &directoryContents); //std::cout << numFiles; if (numFiles < 0) { + //TODO quitar GetLastError() de aqui + long directoryErrorCode; log_debugcpp("FALLO LA FUNCION, VALOR NEGATIVO " + std::to_string(numFiles)); + if (numFiles == DIRECTORY_ERROR_ACCESSING_CONTENT) directoryErrorCode = getLastDirectorySystemError(); showError = true; - strcpy(error, eMsg.listDirectoryError.c_str()); - strcpy(currentPath, addrBarVal); - numFiles = listDirectory(std::string(currentPath), &directoryContents); + switch(directoryErrorCode) { + case AGDIR_ERROR_ACCESS_DENIED: + strcpy(error, ErrorMessages::accessDeniedError); + strcpy(addrBarVal, history->previousPaths.at(history->historyTraversalPos)); + strcpy(currentPath, history->previousPaths.at(history->historyTraversalPos)); + numFiles = listDirectory(history->previousPaths.at(history->historyTraversalPos), &directoryContents); + break; + default: + strcpy(error, ErrorMessages::listDirectoryError); + strcpy(currentPath, addrBarVal); + numFiles = listDirectory(std::string(currentPath), &directoryContents); + history->isAdditionTime = false; + break; + } + history->isAdditionTime = false; } else { showError = false; strcpy(addrBarVal, currentPath); + if(history->isAdditionTime) addPathToHistory(history, addrBarVal); } } - //It's renderin' time + /* + + + + + + It's renderin' time + + + + + + */ static bool showHidden = false; static bool filterByExtension = false; static std::vector extensions; @@ -451,35 +562,57 @@ int main(int, char**) if(showError){ ImGui::Text(error); } - + ImGui::Text("Select a file:"); - //Permanentes e increiblemente utilitarios botones en Cuatro K: MOVE UP + //Permanentes e increiblemente utilitarios botones en Cuatro K: BACK + ImGui::BeginDisabled(MIN_HISTORY_POS); + if(ImGui::Button("Back")) { + char** interfaceMovementButtonsPath = moveThroughHistory(history, HISTORY_BACKWARD); + isListDirectoriesAdequate = inputMove(&showError, interfaceMovementButtonsPath); + //TODO: ERROR + strcpy(error, ErrorMessages::moveUpError); + } + ImGui::EndDisabled(); + //FORWARD + ImGui::SameLine(); + ImGui::BeginDisabled(MAX_HISTORY_POS); + if(ImGui::Button("Forward")) { + char** interfaceMovementButtonsPath = moveThroughHistory(history, HISTORY_FORWARD); + isListDirectoriesAdequate = inputMove(&showError, interfaceMovementButtonsPath); + strcpy(error, ErrorMessages::moveUpError); + } + ImGui::EndDisabled(); + + //MOVE UP + ImGui::SameLine(); //c pervirtio con unicode std::wstring s(L"←→↑↓"); static char moveUp[] = ".."; static char* moveUpPtr = &moveUp[0]; if(ImGui::Button("Move Up")) { isListDirectoriesAdequate = inputMove(&showError, (&moveUpPtr)); - strcpy(error, eMsg.moveUpError.c_str()); + strcpy(error, ErrorMessages::moveUpError); + history->isAdditionTime = true; } ImGui::SameLine(); //BARRA DE DIRECCIONES + //TODO a point ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize("Enter").x + ImGui::GetStyle().ItemSpacing.x * 2) ); if(ImGui::InputText("##addrbar", addrBarVal, IM_ARRAYSIZE(addrBarVal), ImGuiInputTextFlags_EnterReturnsTrue )){ log_debugcpp(currentPath <<" ADDRBAR INTENTO"); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); - handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.addrBarError.c_str()); + handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::addrBarError, history); }; ImGui::PopItemWidth(); ImGui::SameLine(); if(ImGui::Button("Enter")){ log_debugcpp(currentPath <<" ADDRBAR INTENTO"); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); - handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.addrBarError.c_str()); + handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::addrBarError, history); } //ImGui::Text("%s TEMPADDRBAR", currentPath); - log_debugcpp(currentPath <<" LEMISIIMIA"); + //LA GRAN TABLACIÓN: ACTOR EN LAS SOMBRAS static ImGuiTableFlags splitterTableFlags = ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV; if (ImGui::BeginTable("##splitterTable", 2, splitterTableFlags, ImVec2(-FLT_MIN, 0.8f * ImGui::GetTextLineHeightWithSpacing()))){ @@ -504,6 +637,7 @@ int main(int, char**) ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.8f, 0.8f)); if(ImGui::Button(onPresentPaths.at(i))){ isListDirectoriesAdequate = inputMove(&showError, &onPresentPaths.at(i)); + history->isAdditionTime = true; } ImGui::PopStyleColor(3); if (i != onPresentPaths.size() - 1) ImGui::TableNextRow(); @@ -545,7 +679,7 @@ int main(int, char**) strcat(addrBarVal, "\\"); strcat(addrBarVal, directoryContents.at(idx)->name); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); - handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.tableElementError.c_str()); + handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::tableElementError, history); //currentItemIdx = -1; } ImGui::TableNextColumn();