2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-23 18:57:12 +00:00
tdesktop/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp

132 lines
2.8 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "platform/linux/file_utilities_linux.h"
2021-01-10 07:51:38 +04:00
#include "platform/linux/linux_gtk_integration.h"
#include "platform/linux/specific_linux.h"
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
#include "platform/linux/linux_xdp_file_dialog.h"
#include "platform/linux/linux_xdp_open_with_dialog.h"
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
#include <QtCore/QProcess>
#include <QtGui/QDesktopServices>
2019-11-18 10:52:08 +03:00
2021-02-28 06:34:41 +04:00
#include <glibmm.h>
#include <giomm.h>
2020-10-27 09:03:50 +04:00
2021-01-10 07:51:38 +04:00
using Platform::internal::GtkIntegration;
namespace Platform {
namespace File {
void UnsafeOpenUrl(const QString &url) {
2021-02-28 06:34:41 +04:00
try {
if (Gio::AppInfo::launch_default_for_uri(url.toStdString())) {
return;
}
} catch (const Glib::Error &e) {
LOG(("App Error: %1").arg(QString::fromStdString(e.what())));
}
2021-02-28 06:34:41 +04:00
if (QDesktopServices::openUrl(url)) {
return;
}
QProcess::startDetached(qsl("xdg-open"), { url });
}
void UnsafeOpenEmailLink(const QString &email) {
2020-10-27 09:03:50 +04:00
UnsafeOpenUrl(qstr("mailto:") + email);
}
2020-11-08 01:28:24 +04:00
bool UnsafeShowOpenWith(const QString &filepath) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
if (internal::ShowXDPOpenWithDialog(filepath)) {
return true;
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
2021-01-10 07:51:38 +04:00
if (InFlatpak() || InSnap()) {
2020-11-08 01:28:24 +04:00
return false;
}
2021-01-10 07:51:38 +04:00
if (const auto integration = GtkIntegration::Instance()) {
2021-02-28 06:34:41 +04:00
return integration->showOpenWithDialog(filepath);
2021-01-10 07:51:38 +04:00
}
2020-11-08 01:28:24 +04:00
return false;
}
void UnsafeLaunch(const QString &filepath) {
2021-02-28 06:34:41 +04:00
try {
if (Gio::AppInfo::launch_default_for_uri(
Glib::filename_to_uri(filepath.toStdString()))) {
return;
2020-11-08 01:28:24 +04:00
}
2021-02-28 06:34:41 +04:00
} catch (const Glib::Error &e) {
LOG(("App Error: %1").arg(QString::fromStdString(e.what())));
}
2021-02-28 06:34:41 +04:00
if (UnsafeShowOpenWith(filepath)) {
return;
}
const auto qUrlPath = QUrl::fromLocalFile(filepath);
if (QDesktopServices::openUrl(qUrlPath)) {
return;
}
QProcess::startDetached(qsl("xdg-open"), { qUrlPath.toEncoded() });
}
} // namespace File
namespace FileDialog {
bool Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
2021-01-10 07:51:38 +04:00
::FileDialog::internal::Type type,
QString startFile) {
if (parent) {
parent = parent->window();
}
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
2021-06-23 03:20:40 +04:00
{
const auto result = XDP::Get(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
if (result.has_value()) {
return *result;
}
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
return ::FileDialog::internal::GetDefault(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
}
} // namespace FileDialog
} // namespace Platform