Put main desktop file lookup into a separated function

This commit is contained in:
Alexis Lopez Zubieta
2018-11-04 17:47:43 -06:00
parent 93fea697e5
commit cab57f86d6
+54 -41
View File
@@ -14,6 +14,9 @@ using namespace linuxdeploy::util;
namespace bf = boost::filesystem;
namespace linuxdeploy {
desktopfile::DesktopFile getMainDesktopFile(std::vector<std::string>& desktopFilePaths,
std::vector<desktopfile::DesktopFile>& deployedDesktopFiles);
int deployAppDirRootFiles(std::vector<std::string> desktopFilePaths,
std::string customAppRunPath, appdir::AppDir& appDir) {
// search for desktop file and deploy it to AppDir root
@@ -28,55 +31,65 @@ namespace linuxdeploy {
} else {
auto deployedDesktopFiles = appDir.deployedDesktopFiles();
desktopfile::DesktopFile desktopFile;
try {
desktopfile::DesktopFile desktopFile = getMainDesktopFile(desktopFilePaths, deployedDesktopFiles);
if (deployedDesktopFiles.empty()) {
ldLog() << LD_WARNING
<< "Could not find desktop file in AppDir, cannot create links for AppRun, desktop file and icon in AppDir root"
<< std::endl;
} else {
if (!desktopFilePaths.empty()) {
auto firstDeployedDesktopFileName = boost::filesystem::path(
desktopFilePaths.front()).filename().string();
ldLog() << "Deploying desktop file:" << desktopFile.path() << std::endl;
auto desktopFileMatchingName = find_if(
deployedDesktopFiles.begin(),
deployedDesktopFiles.end(),
[&firstDeployedDesktopFileName](const desktopfile::DesktopFile& desktopFile) {
auto fileName = desktopFile.path().filename().string();
return fileName == firstDeployedDesktopFileName;
}
);
bool rv;
if (desktopFileMatchingName != deployedDesktopFiles.end()) {
desktopFile = *desktopFileMatchingName;
} else {
ldLog() << LD_ERROR << "Could not find desktop file deployed earlier any more:"
<< firstDeployedDesktopFileName << std::endl;
return 1;
}
if (!customAppRunPath.empty()) {
rv = appDir.createLinksInAppDirRoot(desktopFile, customAppRunPath);
} else {
desktopFile = deployedDesktopFiles[0];
ldLog() << LD_WARNING << "No desktop file specified, using first desktop file found:"
<< desktopFile.path() << std::endl;
rv = appDir.createLinksInAppDirRoot(desktopFile);
}
}
ldLog() << "Deploying desktop file:" << desktopFile.path() << std::endl;
bool rv;
if (!customAppRunPath.empty()) {
rv = appDir.createLinksInAppDirRoot(desktopFile, customAppRunPath);
} else {
rv = appDir.createLinksInAppDirRoot(desktopFile);
}
if (!rv) {
return 1;
if (!rv) {
return 1;
}
} catch (const std::runtime_error& er) {
return -1;
}
}
return true;
}
desktopfile::DesktopFile getMainDesktopFile(std::vector<std::string>& desktopFilePaths,
std::vector<desktopfile::DesktopFile>& deployedDesktopFiles) {
desktopfile::DesktopFile desktopFile;
if (deployedDesktopFiles.empty()) {
ldLog() << LD_WARNING
<< "Could not find desktop file in AppDir, cannot create links for AppRun, desktop file and icon in AppDir root"
<< std::endl;
} else {
if (!desktopFilePaths.empty()) {
auto firstDeployedDesktopFileName = boost::filesystem::path(
desktopFilePaths.front()).filename().string();
auto desktopFileMatchingName = find_if(
deployedDesktopFiles.begin(),
deployedDesktopFiles.end(),
[&firstDeployedDesktopFileName](const desktopfile::DesktopFile& desktopFile) {
auto fileName = desktopFile.path().filename().string();
return fileName == firstDeployedDesktopFileName;
}
);
if (desktopFileMatchingName != deployedDesktopFiles.end()) {
desktopFile = *desktopFileMatchingName;
} else {
ldLog() << LD_ERROR << "Could not find desktop file deployed earlier any more:"
<< firstDeployedDesktopFileName << std::endl;
throw std::runtime_error("Old desktop file is not reachable.");
}
} else {
desktopFile = deployedDesktopFiles[0];
ldLog() << LD_WARNING << "No desktop file specified, using first desktop file found:"
<< desktopFile.path() << std::endl;
}
}
return desktopFile;
}
}