From 023a0b18777540459dbaf43cc8698429b5a05bfe Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Wed, 11 Nov 2020 01:09:38 +0100 Subject: Replace prefined application name by a generic name If the path returned from a call to a method of the QStandardPaths class contains the application name 'qtpaths' it is replaced by '' to output this generic name. Task-number: QTBUG-87058 Change-Id: I7a9e848f42bf94ef4bbd4757e77ccfeefa8dfa9f Reviewed-by: David Faure (cherry picked from commit 8e642309ffd57637eed2841971f1d938662232b6) --- src/qtpaths/qtpaths.cpp | 112 ++++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/src/qtpaths/qtpaths.cpp b/src/qtpaths/qtpaths.cpp index eaff69e83..9a9c90577 100644 --- a/src/qtpaths/qtpaths.cpp +++ b/src/qtpaths/qtpaths.cpp @@ -79,36 +79,44 @@ Q_NORETURN static void error(const QString &message) ::exit(EXIT_FAILURE); } - -/* - * NOTE: that DataLocation and CacheLocation are missing as - * they don't really make sense for a utility like this because - * they include the application name. - */ -static const struct StringEnum { +class StringEnum { +public: const char *stringvalue; QStandardPaths::StandardLocation enumvalue; -} lookupTableData[] = { - { "AppConfigLocation", QStandardPaths::AppConfigLocation }, - { "AppDataLocation", QStandardPaths::AppDataLocation }, - { "AppLocalDataLocation", QStandardPaths::AppLocalDataLocation }, - { "ApplicationsLocation", QStandardPaths::ApplicationsLocation }, - { "CacheLocation", QStandardPaths::CacheLocation }, - { "ConfigLocation", QStandardPaths::ConfigLocation }, - { "DataLocation", QStandardPaths::DataLocation }, - { "DesktopLocation", QStandardPaths::DesktopLocation }, - { "DocumentsLocation", QStandardPaths::DocumentsLocation }, - { "DownloadLocation", QStandardPaths::DownloadLocation }, - { "FontsLocation", QStandardPaths::FontsLocation }, - { "GenericCacheLocation", QStandardPaths::GenericCacheLocation }, - { "GenericConfigLocation", QStandardPaths::GenericConfigLocation }, - { "GenericDataLocation", QStandardPaths::GenericDataLocation }, - { "HomeLocation", QStandardPaths::HomeLocation }, - { "MoviesLocation", QStandardPaths::MoviesLocation }, - { "MusicLocation", QStandardPaths::MusicLocation }, - { "PicturesLocation", QStandardPaths::PicturesLocation }, - { "RuntimeLocation", QStandardPaths::RuntimeLocation }, - { "TempLocation", QStandardPaths::TempLocation } + bool hasappname; + + /** + * Replace application name by generic name if requested + */ + QString mapName(const QString &s) const + { + return hasappname ? QString(s).replace("qtpaths", "") : s; + } +}; + +static const StringEnum lookupTableData[] = { + { "AppConfigLocation", QStandardPaths::AppConfigLocation, true }, + { "AppDataLocation", QStandardPaths::AppDataLocation, true }, + { "AppLocalDataLocation", QStandardPaths::AppLocalDataLocation, true }, + { "ApplicationsLocation", QStandardPaths::ApplicationsLocation, false }, + { "CacheLocation", QStandardPaths::CacheLocation, true }, + { "ConfigLocation", QStandardPaths::ConfigLocation, false }, +#if QT_VERSION < QT_VERSION_CHECK(6,0,0) + { "DataLocation", QStandardPaths::DataLocation, true }, +#endif + { "DesktopLocation", QStandardPaths::DesktopLocation, false }, + { "DocumentsLocation", QStandardPaths::DocumentsLocation, false }, + { "DownloadLocation", QStandardPaths::DownloadLocation, false }, + { "FontsLocation", QStandardPaths::FontsLocation, false }, + { "GenericCacheLocation", QStandardPaths::GenericCacheLocation, false }, + { "GenericConfigLocation", QStandardPaths::GenericConfigLocation, false }, + { "GenericDataLocation", QStandardPaths::GenericDataLocation, false }, + { "HomeLocation", QStandardPaths::HomeLocation, false }, + { "MoviesLocation", QStandardPaths::MoviesLocation, false }, + { "MusicLocation", QStandardPaths::MusicLocation, false }, + { "PicturesLocation", QStandardPaths::PicturesLocation, false }, + { "RuntimeLocation", QStandardPaths::RuntimeLocation, false }, + { "TempLocation", QStandardPaths::TempLocation, false } }; /** @@ -124,14 +132,14 @@ static QStringList types() } /** - * Tries to parse the location string into a StandardLocation or alternatively + * Tries to arse the location string into a reference to a StringEnum entry or alternatively * calls \ref error with a error message */ -static QStandardPaths::StandardLocation parseLocationOrError(const QString &locationString) +static const StringEnum &parseLocationOrError(const QString &locationString) { for (const StringEnum &se : lookupTableData) if (locationString == QLatin1String(se.stringvalue)) - return se.enumvalue; + return se; QString message = QCoreApplication::translate("qtpaths", "Unknown location: %1"); error(message.arg(locationString)); @@ -252,21 +260,21 @@ int main(int argc, char **argv) } if (parser.isSet(display)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(display)); - QString text = QStandardPaths::displayName(location); - results << text; + const StringEnum &location = parseLocationOrError(parser.value(display)); + QString text = QStandardPaths::displayName(location.enumvalue); + results << location.mapName(text); } if (parser.isSet(paths)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(paths)); - QStringList paths = QStandardPaths::standardLocations(location); - results << paths.join(pathsep); + const StringEnum &location = parseLocationOrError(parser.value(paths)); + QStringList paths = QStandardPaths::standardLocations(location.enumvalue); + results << location.mapName(paths.join(pathsep)); } if (parser.isSet(writablePath)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(writablePath)); - QString path = QStandardPaths::writableLocation(location); - results << path; + const StringEnum &location = parseLocationOrError(parser.value(writablePath)); + QString path = QStandardPaths::writableLocation(location.enumvalue); + results << location.mapName(path); } if (parser.isSet(findExe)) { @@ -276,31 +284,31 @@ int main(int argc, char **argv) } if (parser.isSet(locateDir)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateDir)); + const StringEnum &location = parseLocationOrError(parser.value(locateDir)); QString searchitem = searchStringOrError(&parser); - QString path = QStandardPaths::locate(location, searchitem, QStandardPaths::LocateDirectory); - results << path; + QString path = QStandardPaths::locate(location.enumvalue, searchitem, QStandardPaths::LocateDirectory); + results << location.mapName(path); } if (parser.isSet(locateFile)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateFile)); + const StringEnum &location = parseLocationOrError(parser.value(locateFile)); QString searchitem = searchStringOrError(&parser); - QString path = QStandardPaths::locate(location, searchitem, QStandardPaths::LocateFile); - results << path; + QString path = QStandardPaths::locate(location.enumvalue, searchitem, QStandardPaths::LocateFile); + results << location.mapName(path); } if (parser.isSet(locateDirs)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateDirs)); + const StringEnum &location = parseLocationOrError(parser.value(locateDirs)); QString searchitem = searchStringOrError(&parser); - QStringList paths = QStandardPaths::locateAll(location, searchitem, QStandardPaths::LocateDirectory); - results << paths.join(pathsep); + QStringList paths = QStandardPaths::locateAll(location.enumvalue, searchitem, QStandardPaths::LocateDirectory); + results << location.mapName(paths.join(pathsep)); } if (parser.isSet(locateFiles)) { - QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateFiles)); + const StringEnum &location = parseLocationOrError(parser.value(locateFiles)); QString searchitem = searchStringOrError(&parser); - QStringList paths = QStandardPaths::locateAll(location, searchitem, QStandardPaths::LocateFile); - results << paths.join(pathsep); + QStringList paths = QStandardPaths::locateAll(location.enumvalue, searchitem, QStandardPaths::LocateFile); + results << location.mapName(paths.join(pathsep)); } if (results.isEmpty()) { parser.showHelp(); -- cgit v1.2.3