From 86a74b4a93ab62c8ef7819dc549fe7b3ace24916 Mon Sep 17 00:00:00 2001 From: houz Date: Thu, 2 Jun 2016 15:12:07 +0200 Subject: Sort color profile lists Fixed bugs: - https://launchpad.net/bugs/1457126 (bzr r14946) --- src/color-profile.cpp | 83 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 28 deletions(-) (limited to 'src/color-profile.cpp') diff --git a/src/color-profile.cpp b/src/color-profile.cpp index 523026aa5..9e545df03 100644 --- a/src/color-profile.cpp +++ b/src/color-profile.cpp @@ -705,7 +705,16 @@ gint Inkscape::CMSSystem::getChannelCount(ColorProfile const *profile) #endif // defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) -std::vector ColorProfile::getBaseProfileDirs() { +// sort home dir before the rest, and alphabetically oterhwise +bool compareProfileBoolPair(const std::pair & a, const std::pair & b) +{ + if (a.second != b.second) return a.second; // a comes first iff it's home, i.e., second == true + return a.first < b.first; +} + +// the bool return value tells if it's a user's directory or a system location +// note that this will treat places under $HOME as system directories when they are found via $XDG_DATA_DIRS +std::vector > ColorProfile::getBaseProfileDirs() { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) static bool warnSet = false; if (!warnSet) { @@ -715,17 +724,17 @@ std::vector ColorProfile::getBaseProfileDirs() { warnSet = true; } #endif // defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) - std::vector sources; + std::vector > sources; // first try user's local dir gchar* path = g_build_filename(g_get_user_data_dir(), "color", "icc", NULL); - sources.push_back(path); + sources.push_back(std::make_pair(path, true)); g_free(path); const gchar* const * dataDirs = g_get_system_data_dirs(); for ( int i = 0; dataDirs[i]; i++ ) { gchar* path = g_build_filename(dataDirs[i], "color", "icc", NULL); - sources.push_back(path); + sources.push_back(std::make_pair(path, false)); g_free(path); } @@ -737,14 +746,14 @@ std::vector ColorProfile::getBaseProfileDirs() { possible.push_back("/Library/ColorSync/Profiles"); for ( std::vector::const_iterator it = possible.begin(); it != possible.end(); ++it ) { if ( g_file_test(it->c_str(), G_FILE_TEST_EXISTS) && g_file_test(it->c_str(), G_FILE_TEST_IS_DIR) ) { - sources.push_back(it->c_str()); + sources.push_back(std::make_pair(it->c_str(), false)); onOSX = true; } } if ( onOSX ) { gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL); if ( g_file_test(path, G_FILE_TEST_EXISTS) && g_file_test(path, G_FILE_TEST_IS_DIR) ) { - sources.push_back(path); + sources.push_back(std::make_pair(path, true)); } g_free(path); } @@ -760,12 +769,17 @@ std::vector ColorProfile::getBaseProfileDirs() { if ( !g_utf8_validate(utf8Path, -1, NULL) ) { g_warning( "GetColorDirectoryW() resulted in invalid UTF-8" ); } else { - sources.push_back(utf8Path); + sources.push_back(std::make_pair(utf8Path, false)); } g_free( utf8Path ); } #endif // WIN32 + std::sort(sources.begin(), sources.end(), compareProfileBoolPair); + std::vector >::iterator last = std::unique(sources.begin(), sources.end()); + sources.erase(last, sources.end()); + + return sources; } @@ -808,28 +822,28 @@ static bool isIccFile( gchar const *filepath ) return isIccFile; } -std::vector ColorProfile::getProfileFiles() +std::vector > ColorProfile::getProfileFiles() { - std::vector files; + std::vector > files; - std::list sources; + std::list > sources; { - std::vector tmp = ColorProfile::getBaseProfileDirs(); + std::vector > tmp = ColorProfile::getBaseProfileDirs(); sources.insert(sources.begin(), tmp.begin(), tmp.end()); } - for ( std::list::const_iterator it = sources.begin(); it != sources.end(); ++it ) { - if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) { + for ( std::list >::const_iterator it = sources.begin(); it != sources.end(); ++it ) { + if ( g_file_test( it->first.c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->first.c_str(), G_FILE_TEST_IS_DIR ) ) { GError *err = 0; - GDir *dir = g_dir_open(it->c_str(), 0, &err); + GDir *dir = g_dir_open(it->first.c_str(), 0, &err); if (dir) { for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) { - gchar *filepath = g_build_filename(it->c_str(), file, NULL); + gchar *filepath = g_build_filename(it->first.c_str(), file, NULL); if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) { - sources.push_back( filepath ); + sources.push_back( std::make_pair(filepath, it->second) ); } else { if ( isIccFile( filepath ) ) { - files.push_back( filepath ); + files.push_back( std::make_pair(filepath, it->second) ); } } @@ -838,31 +852,44 @@ std::vector ColorProfile::getProfileFiles() g_dir_close(dir); dir = 0; } else { - gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str()); + gchar *safeDir = Inkscape::IO::sanitizeString(it->first.c_str()); g_warning(_("Color profiles directory (%s) is unavailable."), safeDir); g_free(safeDir); } } } + std::sort(files.begin(), files.end(), compareProfileBoolPair); + std::vector >::iterator last = std::unique(files.begin(), files.end()); + files.erase(last, files.end()); + return files; } -std::vector > ColorProfile::getProfileFilesWithNames() +bool compareProfilePairByName(const std::pair, Glib::ustring> & a, + const std::pair, Glib::ustring> & b) { - std::vector > result; + if (a.first.second != b.first.second) return a.first.second; + return a.second < b.second; +} + +std::vector, Glib::ustring> > ColorProfile::getProfileFilesWithNames() +{ + std::vector, Glib::ustring> > result; #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) - std::vector files = getProfileFiles(); - for ( std::vector::const_iterator it = files.begin(); it != files.end(); ++it ) { - cmsHPROFILE hProfile = cmsOpenProfileFromFile(it->c_str(), "r"); + std::vector > files = getProfileFiles(); + for ( std::vector >::const_iterator it = files.begin(); it != files.end(); ++it ) { + cmsHPROFILE hProfile = cmsOpenProfileFromFile(it->first.c_str(), "r"); if ( hProfile ) { Glib::ustring name = getNameFromProfile(hProfile); result.push_back( std::make_pair(*it, name) ); cmsCloseProfile(hProfile); } } - std::sort(result.begin(), result.end()); + + std::sort(result.begin(), result.end(), compareProfilePairByName); + #endif // defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) return result; @@ -942,12 +969,12 @@ void loadProfiles() static bool profiles_searched = false; if ( !profiles_searched ) { knownProfiles.clear(); - std::vector files = ColorProfile::getProfileFiles(); + std::vector > files = ColorProfile::getProfileFiles(); - for ( std::vector::const_iterator it = files.begin(); it != files.end(); ++it ) { - cmsHPROFILE prof = cmsOpenProfileFromFile( it->c_str(), "r" ); + for ( std::vector >::const_iterator it = files.begin(); it != files.end(); ++it ) { + cmsHPROFILE prof = cmsOpenProfileFromFile( it->first.c_str(), "r" ); if ( prof ) { - ProfileInfo info( prof, Glib::filename_to_utf8( it->c_str() ) ); + ProfileInfo info( prof, Glib::filename_to_utf8( it->first.c_str() ) ); cmsCloseProfile( prof ); prof = 0; -- cgit v1.2.3