Use canonical path in recent file/path list (fix #654)

This commit is contained in:
David Capello 2015-08-27 11:12:30 -03:00
parent eff31223ab
commit 8c961d58ef
5 changed files with 52 additions and 4 deletions

View File

@ -44,7 +44,7 @@ RecentFiles::RecentFiles()
const char* filename = get_config_string("RecentFiles", buf, NULL);
if (filename && *filename && base::is_file(filename)) {
std::string fn = base::fix_path_separators(filename);
std::string fn = normalizePath(filename);
m_files.addItem(fn, compare_path(fn));
}
}
@ -54,7 +54,7 @@ RecentFiles::RecentFiles()
const char* path = get_config_string("RecentPaths", buf, NULL);
if (path && *path) {
std::string p = base::fix_path_separators(path);
std::string p = normalizePath(path);
m_paths.addItem(p, compare_path(p));
}
}
@ -81,7 +81,7 @@ RecentFiles::~RecentFiles()
void RecentFiles::addRecentFile(const char* filename)
{
std::string fn = base::fix_path_separators(filename);
std::string fn = normalizePath(filename);
m_files.addItem(fn, compare_path(fn));
std::string path = base::get_file_path(fn);
@ -92,7 +92,7 @@ void RecentFiles::addRecentFile(const char* filename)
void RecentFiles::removeRecentFile(const char* filename)
{
std::string fn = base::fix_path_separators(filename);
std::string fn = normalizePath(filename);
m_files.removeItem(fn, compare_path(fn));
std::string path = base::get_file_path(filename);
@ -101,4 +101,11 @@ void RecentFiles::removeRecentFile(const char* filename)
Changed();
}
std::string RecentFiles::normalizePath(std::string fn)
{
fn = base::get_canonical_path(fn);
fn = base::fix_path_separators(fn);
return fn;
}
} // namespace app

View File

@ -39,6 +39,8 @@ namespace app {
Signal0<void> Changed;
private:
std::string normalizePath(std::string fn);
List m_files;
List m_paths;
};

View File

@ -32,10 +32,15 @@ namespace base {
void make_all_directories(const std::string& path);
void remove_directory(const std::string& path);
std::string get_current_path();
std::string get_app_path();
std::string get_temp_path();
std::string get_user_docs_folder();
// If the given filename is a relative path, it converts the
// filename to an absolute one.
std::string get_canonical_path(const std::string& path);
std::vector<std::string> list_files(const std::string& path);
} // namespace base

View File

@ -108,6 +108,13 @@ void remove_directory(const std::string& path)
}
}
std::string get_current_path()
{
std::vector<char> path(MAXPATHLEN);
getcwd(&path[0], path.size());
return std::string(&path[0]);
}
std::string get_app_path()
{
std::vector<char> path(MAXPATHLEN);
@ -145,6 +152,13 @@ std::string get_user_docs_folder()
return "/";
}
std::string get_canonical_path(const std::string& path)
{
char buffer[PATH_MAX];
realpath(path.c_str(), buffer);
return buffer;
}
std::vector<std::string> list_files(const std::string& path)
{
std::vector<std::string> files;

View File

@ -101,6 +101,15 @@ void remove_directory(const std::string& path)
throw Win32Exception("Error removing directory");
}
std::string get_current_path()
{
TCHAR buffer[MAX_PATH+1];
if (::GetCurrentDirectory(sizeof(buffer)/sizeof(TCHAR), buffer))
return to_utf8(buffer);
else
return "";
}
std::string get_app_path()
{
TCHAR buffer[MAX_PATH+1];
@ -129,6 +138,17 @@ std::string get_user_docs_folder()
return "";
}
std::string get_canonical_path(const std::string& path)
{
TCHAR buffer[MAX_PATH+1];
GetFullPathName(
from_utf8(path).c_str(),
sizeof(buffer)/sizeof(TCHAR),
buffer,
nullptr);
return to_utf8(buffer);
}
std::vector<std::string> list_files(const std::string& path)
{
WIN32_FIND_DATA fd;