|
9 | 9 | #include <type_traits> |
10 | 10 |
|
11 | 11 | // libraries |
| 12 | +extern "C" { |
| 13 | +#include <glob.h> |
| 14 | +} |
12 | 15 | #include <boost/filesystem.hpp> |
13 | 16 | #include <boost/algorithm/string.hpp> |
| 17 | +#include <XdgUtils/BaseDir/BaseDir.h> |
| 18 | +#include <XdgUtils/DesktopEntry/DesktopEntry.h> |
14 | 19 |
|
15 | 20 | // local |
16 | | -#include <XdgUtils/DesktopEntry/DesktopEntry.h> |
17 | 21 | #include <appimage/utils/ResourcesExtractor.h> |
18 | 22 | #include <appimage/core/AppImage.h> |
19 | 23 | #include "utils/Logger.h" |
@@ -228,6 +232,50 @@ off_t appimage_get_payload_offset(char const* path) { |
228 | 232 | return 0; |
229 | 233 | } |
230 | 234 |
|
| 235 | + |
| 236 | +char* appimage_registered_desktop_file_path(const char* path, char* md5, bool verbose) { |
| 237 | + glob_t pglob = {}; |
| 238 | + |
| 239 | + // if md5 has been calculated before, we can just use it to save these extra calculations |
| 240 | + // if not, we need to calculate it here |
| 241 | + if (md5 == nullptr) |
| 242 | + md5 = appimage_get_md5(path); |
| 243 | + |
| 244 | + // sanity check |
| 245 | + if (md5 == nullptr) { |
| 246 | + if (verbose) |
| 247 | + fprintf(stderr, "appimage_get_md5() failed\n"); |
| 248 | + return nullptr; |
| 249 | + } |
| 250 | + |
| 251 | + std::string data_home = XdgUtils::BaseDir::XdgDataHome(); |
| 252 | + |
| 253 | + // TODO: calculate this value exactly |
| 254 | + char* glob_pattern = static_cast<char*>(malloc(PATH_MAX)); |
| 255 | + sprintf(glob_pattern, "%s/applications/appimagekit_%s-*.desktop", data_home.c_str(), md5); |
| 256 | + |
| 257 | + glob(glob_pattern, 0, nullptr, &pglob); |
| 258 | + |
| 259 | + char* rv = nullptr; |
| 260 | + |
| 261 | + if (pglob.gl_pathc <= 0) { |
| 262 | + if (verbose) { |
| 263 | + fprintf(stderr, "No results found by glob()"); |
| 264 | + } |
| 265 | + } else if (pglob.gl_pathc >= 1) { |
| 266 | + if (pglob.gl_pathc > 1 && verbose) { |
| 267 | + fprintf(stderr, "Too many results returned by glob(), returning first result found"); |
| 268 | + } |
| 269 | + |
| 270 | + // need to copy value to be able to globfree() later on |
| 271 | + rv = strdup(pglob.gl_pathv[0]); |
| 272 | + } |
| 273 | + |
| 274 | + globfree(&pglob); |
| 275 | + |
| 276 | + return rv; |
| 277 | +} |
| 278 | + |
231 | 279 | #ifdef LIBAPPIMAGE_DESKTOP_INTEGRATION_ENABLED |
232 | 280 | using namespace appimage::desktop_integration; |
233 | 281 |
|
|
0 commit comments