/qt/widgets/common/src/MantidDesktopServices.cpp

https://github.com/mantidproject/mantid · C++ · 75 lines · 33 code · 8 blank · 34 comment · 2 complexity · 1086417e2f50bf1764a359ab560dab32 MD5 · raw file

  1. // Mantid Repository : https://github.com/mantidproject/mantid
  2. //
  3. // Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
  4. // NScD Oak Ridge National Laboratory, European Spallation Source,
  5. // Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. // SPDX - License - Identifier: GPL - 3.0 +
  7. #include "MantidQtWidgets/Common/MantidDesktopServices.h"
  8. #include <QDesktopServices>
  9. #include <QUrl>
  10. #ifdef __linux__
  11. #include <QProcessEnvironment>
  12. #include <cstdlib>
  13. namespace {
  14. // String name of LD_PRELOAD environment variable
  15. constexpr const char *LDPRELOAD_ENV = "LD_PRELOAD";
  16. } // namespace
  17. #endif
  18. namespace MantidQt::API {
  19. /**
  20. * Opens a url in the appropriate web browser. On Linux systems if LD_PRELOAD is
  21. * defined as an environment variable then it is removed for the duration of the
  22. * call
  23. * to the web browser. This is to avoid known issues with LD_PRELOAD libraries
  24. * and some
  25. * web browsers, e.g. firefox. On all other systems the method simply passes
  26. * through to
  27. * QDesktopServies
  28. * @param url Address to be opened
  29. */
  30. bool MantidDesktopServices::openUrl(const QUrl &url) {
  31. #ifndef __linux__
  32. return QDesktopServices::openUrl(url);
  33. #else
  34. // Remove LD_PRELOAD if present
  35. auto systemEnv = QProcessEnvironment::systemEnvironment();
  36. auto ldpreload = systemEnv.value(LDPRELOAD_ENV, QString());
  37. if (!ldpreload.isEmpty()) {
  38. unsetenv(LDPRELOAD_ENV);
  39. }
  40. auto status = QDesktopServices::openUrl(url);
  41. if (!ldpreload.isEmpty()) {
  42. setenv(LDPRELOAD_ENV, qPrintable(ldpreload), 1 /* overwrite*/);
  43. }
  44. return status;
  45. #endif
  46. }
  47. bool MantidDesktopServices::openUrl(const QString &url) { return openUrl(QUrl(url)); }
  48. /**
  49. * Pass through method to MantidDesktopServices::setUrlHandler. See Qt
  50. * documentation
  51. * for
  52. * further details.
  53. * @param scheme Name of scheme to handle
  54. * @param receiver Handler object
  55. * @param method Method called on the receiver object
  56. */
  57. void MantidDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method) {
  58. QDesktopServices::setUrlHandler(scheme, receiver, method);
  59. }
  60. /**
  61. * Pass through method to MantidDesktopServices::unsetUrlHandler. See Qt
  62. * documentation for
  63. * further details.
  64. * @param scheme Name of scheme to drop
  65. */
  66. void MantidDesktopServices::unsetUrlHandler(const QString &scheme) { QDesktopServices::unsetUrlHandler(scheme); }
  67. } // namespace MantidQt::API