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 
15 #include "guiutility.h"
16 
17 #include <QClipboard>
18 #include <QApplication>
19 #include <QDesktopServices>
20 #include <QLoggingCategory>
21 #include <QMessageBox>
22 #include <QUrlQuery>
23 #include <QIcon>
24 
25 #include "theme.h"
26 
27 #include "common/asserts.h"
28 
29 using namespace OCC;
30 
31 Q_LOGGING_CATEGORY(lcUtility, "gui.utility", QtInfoMsg)
32 
openBrowser(const QUrl & url,QWidget * errorWidgetParent)33 bool Utility::openBrowser(const QUrl &url, QWidget *errorWidgetParent)
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 
openEmailComposer(const QString & subject,const QString & body,QWidget * errorWidgetParent)51 bool Utility::openEmailComposer(const QString &subject, const QString &body, QWidget *errorWidgetParent)
52 {
53     QUrl url(QLatin1String("mailto:"));
54     QUrlQuery query;
55     query.setQueryItems({ { QLatin1String("subject"), subject },
56         { QLatin1String("body"), body } });
57     url.setQuery(query);
58 
59     if (!QDesktopServices::openUrl(url)) {
60         if (errorWidgetParent) {
61             QMessageBox::warning(
62                 errorWidgetParent,
63                 QCoreApplication::translate("utility", "Could not open email client"),
64                 QCoreApplication::translate("utility",
65                     "There was an error when launching the email client to "
66                     "create a new message. Maybe no default email client is "
67                     "configured?"));
68         }
69         qCWarning(lcUtility) << "QDesktopServices::openUrl failed for" << url;
70         return false;
71     }
72     return true;
73 }
74 
vfsCurrentAvailabilityText(VfsItemAvailability availability)75 QString Utility::vfsCurrentAvailabilityText(VfsItemAvailability availability)
76 {
77     switch(availability) {
78     case VfsItemAvailability::AlwaysLocal:
79         return QCoreApplication::translate("utility", "Always available locally");
80     case VfsItemAvailability::AllHydrated:
81         return QCoreApplication::translate("utility", "Currently available locally");
82     case VfsItemAvailability::Mixed:
83         return QCoreApplication::translate("utility", "Some available online only");
84     case VfsItemAvailability::AllDehydrated:
85         return QCoreApplication::translate("utility", "Available online only");
86     case VfsItemAvailability::OnlineOnly:
87         return QCoreApplication::translate("utility", "Available online only");
88     }
89     Q_UNREACHABLE();
90 }
91 
vfsPinActionText()92 QString Utility::vfsPinActionText()
93 {
94     return QCoreApplication::translate("utility", "Make always available locally");
95 }
96 
vfsFreeSpaceActionText()97 QString Utility::vfsFreeSpaceActionText()
98 {
99     return QCoreApplication::translate("utility", "Free up local space");
100 }
101 
102 
getCoreIcon(const QString & icon_name)103 QIcon Utility::getCoreIcon(const QString &icon_name)
104 {
105     if (icon_name.isEmpty()) {
106         return {};
107     }
108     const QString path = Theme::instance()->isUsingDarkTheme() ? QStringLiteral("dark") : QStringLiteral("light");
109     const QIcon icon(QStringLiteral(":/client/resources/%1/%2").arg(path, icon_name));
110     Q_ASSERT(!icon.isNull());
111     return icon;
112 }
113