2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Use some methods from lib_base

This commit is contained in:
Ilya Fedin
2020-10-29 23:56:13 +04:00
committed by John Preston
parent df9c7f07a1
commit a38b4f039a
22 changed files with 42 additions and 485 deletions

View File

@@ -11,11 +11,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "platform/linux/linux_gdk_helper.h"
#include "platform/linux/linux_desktop_environment.h"
#include "platform/linux/specific_linux.h"
#include "boxes/abstract_box.h"
#include "storage/localstorage.h"
#include "base/platform/base_platform_file_utilities.h"
#include <QtCore/QProcess>
#include <QtGui/QDesktopServices>
extern "C" {
@@ -91,12 +88,6 @@ void UnsafeLaunch(const QString &filepath) {
}
}
void UnsafeShowInFolder(const QString &filepath) {
// Hide mediaview to make other apps visible.
Ui::hideLayer(anim::type::instant);
base::Platform::ShowInFolder(filepath);
}
} // namespace File
namespace FileDialog {

View File

@@ -174,33 +174,6 @@ QString FlatpakID() {
return Result;
}
QString ProcessNameByPID(const QString &pid) {
constexpr auto kMaxPath = 1024;
char result[kMaxPath] = { 0 };
auto count = readlink("/proc/" + pid.toLatin1() + "/exe", result, kMaxPath);
if (count > 0) {
auto filename = QFile::decodeName(result);
auto deletedPostfix = qstr(" (deleted)");
if (filename.endsWith(deletedPostfix) && !QFileInfo(filename).exists()) {
filename.chop(deletedPostfix.size());
}
return filename;
}
return QString();
}
QString RealExecutablePath(int argc, char *argv[]) {
const auto processName = ProcessNameByPID(qsl("self"));
// Fallback to the first command line argument.
return !processName.isEmpty()
? processName
: argc
? QFile::decodeName(argv[0])
: QString();
}
bool RunShellCommand(const QString &program, const QStringList &arguments) {
const auto result = QProcess::execute(program, arguments);
@@ -310,131 +283,6 @@ bool GetImageFromClipboardSupported() {
}
#endif // !TDESKTOP_DISABLE_GTK_INTEGRATION
std::optional<crl::time> XCBLastUserInputTime() {
const auto connection = base::Platform::XCB::GetConnectionFromQt();
if (!connection) {
return std::nullopt;
}
if (!base::Platform::XCB::IsExtensionPresent(
connection,
&xcb_screensaver_id)) {
return std::nullopt;
}
const auto root = base::Platform::XCB::GetRootWindowFromQt();
if (!root.has_value()) {
return std::nullopt;
}
const auto cookie = xcb_screensaver_query_info(
connection,
*root);
auto reply = xcb_screensaver_query_info_reply(
connection,
cookie,
nullptr);
if (!reply) {
return std::nullopt;
}
const auto idle = reply->ms_since_user_input;
free(reply);
return (crl::now() - static_cast<crl::time>(idle));
}
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
std::optional<crl::time> FreedesktopDBusLastUserInputTime() {
static auto NotSupported = false;
if (NotSupported) {
return std::nullopt;
}
static const auto Message = QDBusMessage::createMethodCall(
qsl("org.freedesktop.ScreenSaver"),
qsl("/org/freedesktop/ScreenSaver"),
qsl("org.freedesktop.ScreenSaver"),
qsl("GetSessionIdleTime"));
const QDBusReply<uint> reply = QDBusConnection::sessionBus().call(
Message);
static const auto NotSupportedErrors = {
QDBusError::ServiceUnknown,
QDBusError::NotSupported,
};
static const auto NotSupportedErrorsToLog = {
QDBusError::Disconnected,
QDBusError::AccessDenied,
};
if (reply.isValid()) {
return (crl::now() - static_cast<crl::time>(reply.value()));
} else if (ranges::contains(NotSupportedErrors, reply.error().type())) {
NotSupported = true;
} else {
if (ranges::contains(NotSupportedErrorsToLog, reply.error().type())) {
NotSupported = true;
}
LOG(("App Error: Unable to get last user input time "
"from org.freedesktop.ScreenSaver: %1: %2")
.arg(reply.error().name())
.arg(reply.error().message()));
}
return std::nullopt;
}
std::optional<crl::time> MutterDBusLastUserInputTime() {
static auto NotSupported = false;
if (NotSupported) {
return std::nullopt;
}
static const auto Message = QDBusMessage::createMethodCall(
qsl("org.gnome.Mutter.IdleMonitor"),
qsl("/org/gnome/Mutter/IdleMonitor/Core"),
qsl("org.gnome.Mutter.IdleMonitor"),
qsl("GetIdletime"));
const QDBusReply<qulonglong> reply = QDBusConnection::sessionBus().call(
Message);
static const auto NotSupportedErrors = {
QDBusError::ServiceUnknown,
};
static const auto NotSupportedErrorsToLog = {
QDBusError::Disconnected,
QDBusError::AccessDenied,
};
if (reply.isValid()) {
return (crl::now() - static_cast<crl::time>(reply.value()));
} else if (ranges::contains(NotSupportedErrors, reply.error().type())) {
NotSupported = true;
} else {
if (ranges::contains(NotSupportedErrorsToLog, reply.error().type())) {
NotSupported = true;
}
LOG(("App Error: Unable to get last user input time "
"from org.gnome.Mutter.IdleMonitor: %1: %2")
.arg(reply.error().name())
.arg(reply.error().message()));
}
return std::nullopt;
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
uint XCBMoveResizeFromEdges(Qt::Edges edges) {
if (edges == (Qt::TopEdge | Qt::LeftEdge))
return 0;
@@ -682,11 +530,6 @@ bool InSnap() {
return Result;
}
bool InAppImage() {
static const auto Result = qEnvironmentVariableIsSet("APPIMAGE");
return Result;
}
bool IsStaticBinary() {
#ifdef DESKTOP_APP_USE_PACKAGED
return false;
@@ -773,25 +616,6 @@ bool CanOpenDirectoryWithPortal() {
return false;
}
QString CurrentExecutablePath(int argc, char *argv[]) {
if (InAppImage()) {
const auto appimagePath = QString::fromUtf8(qgetenv("APPIMAGE"));
const auto appimagePathList = appimagePath.split('/');
if (qEnvironmentVariableIsSet("ARGV0")
&& appimagePathList.size() >= 5
&& appimagePathList[1] == qstr("run")
&& appimagePathList[2] == qstr("user")
&& appimagePathList[4] == qstr("appimagelauncherfs")) {
return QString::fromUtf8(qgetenv("ARGV0"));
}
return appimagePath;
}
return RealExecutablePath(argc, argv);
}
QString AppRuntimeDirectory() {
static const auto Result = [&] {
auto runtimeDir = QStandardPaths::writableLocation(
@@ -908,29 +732,6 @@ QImage GetImageFromClipboard() {
return data;
}
std::optional<crl::time> LastUserInputTime() {
if (!IsWayland()) {
const auto xcbResult = XCBLastUserInputTime();
if (xcbResult.has_value()) {
return xcbResult;
}
}
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
const auto freedesktopResult = FreedesktopDBusLastUserInputTime();
if (freedesktopResult.has_value()) {
return freedesktopResult;
}
const auto mutterResult = MutterDBusLastUserInputTime();
if (mutterResult.has_value()) {
return mutterResult;
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
return std::nullopt;
}
std::optional<bool> IsDarkMode() {
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
if (Libs::GtkSettingSupported() && Libs::GtkLoaded()) {
@@ -1089,41 +890,6 @@ QRect psDesktopRect() {
void psWriteDump() {
}
bool _removeDirectory(const QString &path) { // from http://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c
QByteArray pathRaw = QFile::encodeName(path);
DIR *d = opendir(pathRaw.constData());
if (!d) return false;
while (struct dirent *p = readdir(d)) {
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) continue;
QString fname = path + '/' + p->d_name;
QByteArray fnameRaw = QFile::encodeName(fname);
struct stat statbuf;
if (!stat(fnameRaw.constData(), &statbuf)) {
if (S_ISDIR(statbuf.st_mode)) {
if (!_removeDirectory(fname)) {
closedir(d);
return false;
}
} else {
if (unlink(fnameRaw.constData())) {
closedir(d);
return false;
}
}
}
}
closedir(d);
return !rmdir(pathRaw.constData());
}
void psDeleteDir(const QString &dir) {
_removeDirectory(dir);
}
void psActivateProcess(uint64 pid) {
// objc_activateProgram();
}
@@ -1444,10 +1210,6 @@ void psNewVersion() {
Platform::RegisterCustomScheme();
}
bool psShowOpenWithMenu(int x, int y, const QString &file) {
return false;
}
void psAutoStart(bool start, bool silent) {
if (InFlatpak()) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION

View File

@@ -22,7 +22,6 @@ inline void SetWatchingMediaKeys(bool watching) {
bool InFlatpak();
bool InSnap();
bool InAppImage();
bool IsStaticBinary();
bool UseGtkIntegration();
bool IsGtkIntegrationForced();
@@ -55,8 +54,6 @@ inline void psCheckLocalSocket(const QString &serverName) {
void psWriteDump();
void psDeleteDir(const QString &dir);
QStringList psInitLogs();
void psClearInitLogs();