/src/gui/guiutility.cpp

https://github.com/nextcloud/client · C++ · 106 lines · 81 code · 12 blank · 13 comment · 6 complexity · 32f893356c58317c94b162e99787891b MD5 · raw file

  1. /*
  2. * Copyright (C) by Christian Kamm <mail@ckamm.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "guiutility.h"
  15. #include <QClipboard>
  16. #include <QApplication>
  17. #include <QDesktopServices>
  18. #include <QLoggingCategory>
  19. #include <QMessageBox>
  20. #include <QUrlQuery>
  21. #include "common/asserts.h"
  22. using namespace OCC;
  23. Q_LOGGING_CATEGORY(lcUtility, "nextcloud.gui.utility", QtInfoMsg)
  24. bool Utility::openBrowser(const QUrl &url, QWidget *errorWidgetParent)
  25. {
  26. const QStringList allowedUrlSchemes = {
  27. "http",
  28. "https",
  29. "oauthtest"
  30. };
  31. if (!allowedUrlSchemes.contains(url.scheme())) {
  32. qCWarning(lcUtility) << "URL format is not supported, or it has been compromised for:" << url.toString();
  33. return false;
  34. }
  35. if (!QDesktopServices::openUrl(url)) {
  36. if (errorWidgetParent) {
  37. QMessageBox::warning(
  38. errorWidgetParent,
  39. QCoreApplication::translate("utility", "Could not open browser"),
  40. QCoreApplication::translate("utility",
  41. "There was an error when launching the browser to go to "
  42. "URL %1. Maybe no default browser is configured?")
  43. .arg(url.toString()));
  44. }
  45. qCWarning(lcUtility) << "QDesktopServices::openUrl failed for" << url;
  46. return false;
  47. }
  48. return true;
  49. }
  50. bool Utility::openEmailComposer(const QString &subject, const QString &body, QWidget *errorWidgetParent)
  51. {
  52. QUrl url(QLatin1String("mailto:"));
  53. QUrlQuery query;
  54. query.setQueryItems({ { QLatin1String("subject"), subject },
  55. { QLatin1String("body"), body } });
  56. url.setQuery(query);
  57. if (!QDesktopServices::openUrl(url)) {
  58. if (errorWidgetParent) {
  59. QMessageBox::warning(
  60. errorWidgetParent,
  61. QCoreApplication::translate("utility", "Could not open email client"),
  62. QCoreApplication::translate("utility",
  63. "There was an error when launching the email client to "
  64. "create a new message. Maybe no default email client is "
  65. "configured?"));
  66. }
  67. qCWarning(lcUtility) << "QDesktopServices::openUrl failed for" << url;
  68. return false;
  69. }
  70. return true;
  71. }
  72. QString Utility::vfsCurrentAvailabilityText(VfsItemAvailability availability)
  73. {
  74. switch(availability) {
  75. case VfsItemAvailability::AlwaysLocal:
  76. return QCoreApplication::translate("utility", "Always available locally");
  77. case VfsItemAvailability::AllHydrated:
  78. return QCoreApplication::translate("utility", "Currently available locally");
  79. case VfsItemAvailability::Mixed:
  80. return QCoreApplication::translate("utility", "Some available online only");
  81. case VfsItemAvailability::AllDehydrated:
  82. case VfsItemAvailability::OnlineOnly:
  83. return QCoreApplication::translate("utility", "Available online only");
  84. }
  85. Q_UNREACHABLE();
  86. }
  87. QString Utility::vfsPinActionText()
  88. {
  89. return QCoreApplication::translate("utility", "Make always available locally");
  90. }
  91. QString Utility::vfsFreeSpaceActionText()
  92. {
  93. return QCoreApplication::translate("utility", "Free up local space");
  94. }