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

https://github.com/wdzhou/mantid · C++ · 97 lines · 46 code · 10 blank · 41 comment · 2 complexity · 944e710ba336919b235f24619e0467a5 MD5 · raw file

  1. #include "MantidQtWidgets/Common/MantidDesktopServices.h"
  2. #include <QDesktopServices>
  3. #ifdef __linux__
  4. #include <QProcessEnvironment>
  5. #include <cstdlib>
  6. namespace {
  7. // String name of LD_PRELOAD environment variable
  8. constexpr const char *LDPRELOAD_ENV = "LD_PRELOAD";
  9. }
  10. #endif
  11. #include <iostream>
  12. namespace MantidQt {
  13. namespace API {
  14. /**
  15. * Opens a url in the appropriate web browser. On Linux systems if LD_PRELOAD is
  16. * defined as an environment variable then it is removed for the duration of the
  17. * call
  18. * to the web browser. This is to avoid known issues with LD_PRELOAD libraries
  19. * and some
  20. * web browsers, e.g. firefox. On all other systems the method simply passes
  21. * through to
  22. * QDesktopServies
  23. * @param url Address to be opened
  24. */
  25. bool MantidDesktopServices::openUrl(const QUrl &url) {
  26. #ifndef __linux__
  27. return QDesktopServices::openUrl(url);
  28. #else
  29. // Remove LD_PRELOAD if present
  30. auto systemEnv = QProcessEnvironment::systemEnvironment();
  31. auto ldpreload = systemEnv.value(LDPRELOAD_ENV, QString());
  32. if (!ldpreload.isEmpty()) {
  33. unsetenv(LDPRELOAD_ENV);
  34. }
  35. auto status = QDesktopServices::openUrl(url);
  36. if (!ldpreload.isEmpty()) {
  37. setenv(LDPRELOAD_ENV, ldpreload.toLatin1().constData(), 1 /* overwrite*/);
  38. }
  39. return status;
  40. #endif
  41. }
  42. /**
  43. * Pass through method to MantidDesktopServices::setUrlHandler. See Qt
  44. * documentation
  45. * for
  46. * further details.
  47. * @param scheme Name of scheme to handle
  48. * @param receiver Handler object
  49. * @param method Method called on the receiver object
  50. */
  51. void MantidDesktopServices::setUrlHandler(const QString &scheme,
  52. QObject *receiver,
  53. const char *method) {
  54. QDesktopServices::setUrlHandler(scheme, receiver, method);
  55. }
  56. /**
  57. * Pass through method to MantidDesktopServices::unsetUrlHandler. See Qt
  58. * documentation for
  59. * further details.
  60. * @param scheme Name of scheme to drop
  61. */
  62. void MantidDesktopServices::unsetUrlHandler(const QString &scheme) {
  63. QDesktopServices::unsetUrlHandler(scheme);
  64. }
  65. /**
  66. * Pass through method to MantidDesktopServices::storageLocation. See Qt
  67. * documentation for
  68. * further details.
  69. * @param type File type
  70. */
  71. QString MantidDesktopServices::storageLocation(
  72. QDesktopServices::StandardLocation type) {
  73. return QDesktopServices::storageLocation(type);
  74. }
  75. /**
  76. * Pass through method to MantidDesktopServices::displayName. See Qt
  77. * documentation
  78. * for
  79. * further details.
  80. * @param type Location type
  81. */
  82. QString
  83. MantidDesktopServices::displayName(QDesktopServices::StandardLocation type) {
  84. return QDesktopServices::displayName(type);
  85. }
  86. }
  87. }