/src/webots/core/WbDesktopServices.cpp

https://github.com/cyberbotics/webots · C++ · 48 lines · 30 code · 3 blank · 15 comment · 0 complexity · ea88d51dcdbe80dc1567102c91ad339c MD5 · raw file

  1. // Copyright 1996-2020 Cyberbotics Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "WbDesktopServices.hpp"
  15. #ifdef __linux__
  16. #include <QtCore/QProcess>
  17. #else
  18. #include <QtCore/QUrl>
  19. #include <QtGui/QDesktopServices>
  20. #endif
  21. bool WbDesktopServices::openUrl(const QString &url) {
  22. #ifdef __linux__
  23. QProcess process;
  24. process.setProgram("xdg-open"); // we need to use xdg-open to be snap compliant
  25. process.setArguments(QStringList() << url);
  26. process.setStandardErrorFile(QProcess::nullDevice());
  27. process.setStandardOutputFile(QProcess::nullDevice());
  28. return process.startDetached();
  29. #else
  30. #ifdef _WIN32
  31. // The TEMP/TMP environment variables set my the MSYS2 console are confusing Visual C++ (among possibly other apps)
  32. // as they refer to "/tmp" which is not a valid Windows path. It is therefore safer to remove them
  33. const QByteArray TEMP = qgetenv("TEMP");
  34. const QByteArray TMP = qgetenv("TMP");
  35. qunsetenv("TMP");
  36. qunsetenv("TEMP");
  37. #endif
  38. bool result = QDesktopServices::openUrl(QUrl(url));
  39. #ifdef _WIN32
  40. qputenv("TEMP", TEMP);
  41. qputenv("TMP", TMP);
  42. #endif
  43. return result;
  44. #endif
  45. }