2014-06-14 21:32:11 +02:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 11:23:14 +01:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-01-10 07:05:23 +01:00
|
|
|
|
2018-01-03 11:23:14 +01:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-06-14 21:32:11 +02:00
|
|
|
*/
|
2017-03-04 11:23:56 +01:00
|
|
|
#include "platform/mac/specific_mac.h"
|
2014-05-30 10:53:19 +02:00
|
|
|
|
2017-04-13 10:27:10 +02:00
|
|
|
#include "lang/lang_keys.h"
|
2014-05-30 10:53:19 +02:00
|
|
|
#include "mainwidget.h"
|
2017-06-29 12:27:09 +02:00
|
|
|
#include "history/history_widget.h"
|
2017-12-11 15:45:29 +01:00
|
|
|
#include "core/crash_reports.h"
|
2019-04-07 08:59:37 +02:00
|
|
|
#include "core/sandbox.h"
|
2020-06-19 19:13:43 +02:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2017-03-04 11:23:56 +01:00
|
|
|
#include "storage/localstorage.h"
|
2021-02-25 16:12:51 +01:00
|
|
|
#include "window/window_controller.h"
|
2017-02-25 18:46:21 +01:00
|
|
|
#include "mainwindow.h"
|
2016-09-28 23:33:05 +02:00
|
|
|
#include "history/history_location_manager.h"
|
2023-02-14 17:47:11 +01:00
|
|
|
#include "base/platform/mac/base_confirm_quit.h"
|
2019-09-26 12:20:34 +02:00
|
|
|
#include "base/platform/mac/base_utilities_mac.h"
|
2020-07-23 10:30:20 +02:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2015-03-02 13:34:16 +01:00
|
|
|
|
2019-09-04 09:19:15 +02:00
|
|
|
#include <QtGui/QDesktopServices>
|
2019-09-08 22:04:21 +02:00
|
|
|
#include <QtWidgets/QApplication>
|
2019-09-04 09:19:15 +02:00
|
|
|
|
2017-12-11 15:45:29 +01:00
|
|
|
#include <cstdlib>
|
2016-01-30 19:24:18 +01:00
|
|
|
#include <execinfo.h>
|
2019-01-29 11:09:37 +01:00
|
|
|
#include <sys/xattr.h>
|
2016-01-30 19:24:18 +01:00
|
|
|
|
2017-06-06 12:40:15 +02:00
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#include <CoreFoundation/CFURL.h>
|
|
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
#include <IOKit/hidsystem/ev_keymap.h>
|
2017-08-15 19:12:57 +02:00
|
|
|
#include <mach-o/dyld.h>
|
2018-09-30 17:42:50 +02:00
|
|
|
#include <AVFoundation/AVFoundation.h>
|
2017-06-06 12:40:15 +02:00
|
|
|
|
2023-02-28 17:49:45 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
[[nodiscard]] QImage ImageFromNS(NSImage *icon) {
|
|
|
|
CGImageRef image = [icon CGImageForProposedRect:NULL context:nil hints:nil];
|
|
|
|
|
2023-03-01 20:12:11 +01:00
|
|
|
const int width = CGImageGetWidth(image);
|
|
|
|
const int height = CGImageGetHeight(image);
|
|
|
|
auto result = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.fill(Qt::transparent);
|
2023-02-28 17:49:45 +01:00
|
|
|
|
|
|
|
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
|
|
CGBitmapInfo info = CGBitmapInfo(kCGImageAlphaPremultipliedFirst) | kCGBitmapByteOrder32Host;
|
2023-03-01 20:12:11 +01:00
|
|
|
CGContextRef context = CGBitmapContextCreate(
|
2023-02-28 17:49:45 +01:00
|
|
|
result.bits(),
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
8,
|
2023-03-01 20:12:11 +01:00
|
|
|
result.bytesPerLine(),
|
|
|
|
space,
|
|
|
|
info);
|
2023-02-28 17:49:45 +01:00
|
|
|
|
2023-03-01 20:12:11 +01:00
|
|
|
CGRect rect = CGRectMake(0, 0, width, height);
|
|
|
|
CGContextDrawImage(context, rect, image);
|
2023-02-28 17:49:45 +01:00
|
|
|
|
2023-03-01 20:12:11 +01:00
|
|
|
CFRelease(space);
|
|
|
|
CFRelease(context);
|
2023-02-28 17:49:45 +01:00
|
|
|
|
2023-03-01 20:12:11 +01:00
|
|
|
return result;
|
2023-02-28 17:49:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] QImage ResolveBundleIconDefault() {
|
|
|
|
NSString *path = [[NSBundle mainBundle] bundlePath];
|
|
|
|
NSString *icon = [path stringByAppendingString:@"/Contents/Resources/Icon.icns"];
|
|
|
|
NSImage *image = [[NSImage alloc] initWithContentsOfFile:icon];
|
|
|
|
if (!image) {
|
|
|
|
return Window::Logo();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = ImageFromNS(image);
|
|
|
|
[image release];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-05-30 10:53:19 +02:00
|
|
|
QString psAppDataPath() {
|
2014-06-25 09:25:55 +02:00
|
|
|
return objc_appDataPath();
|
2014-05-30 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void psDoCleanup() {
|
|
|
|
try {
|
2021-11-04 09:35:34 +01:00
|
|
|
Platform::AutostartToggle(false);
|
2014-07-18 12:37:34 +02:00
|
|
|
psSendToMenu(false, true);
|
2014-05-30 10:53:19 +02:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int psCleanup() {
|
2014-06-25 09:25:55 +02:00
|
|
|
psDoCleanup();
|
2014-05-30 10:53:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void psDoFixPrevious() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int psFixPrevious() {
|
2014-06-25 09:25:55 +02:00
|
|
|
psDoFixPrevious();
|
2014-05-30 10:53:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:59:54 +02:00
|
|
|
namespace Platform {
|
2016-01-11 16:43:29 +01:00
|
|
|
|
2016-06-16 14:59:54 +02:00
|
|
|
void start() {
|
|
|
|
objc_start();
|
|
|
|
}
|
2016-01-11 16:43:29 +01:00
|
|
|
|
2016-06-16 14:59:54 +02:00
|
|
|
void finish() {
|
|
|
|
objc_finish();
|
|
|
|
}
|
2014-09-20 23:31:03 +02:00
|
|
|
|
2020-01-22 04:12:07 +01:00
|
|
|
QString SingleInstanceLocalServerName(const QString &hash) {
|
|
|
|
#ifndef OS_MAC_STORE
|
2022-11-29 22:46:36 +01:00
|
|
|
return u"/tmp/"_q + hash + '-' + cGUIDStr();
|
2020-01-22 04:12:07 +01:00
|
|
|
#else // OS_MAC_STORE
|
|
|
|
return objc_documentsPath() + hash.left(4);
|
|
|
|
#endif // OS_MAC_STORE
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:01:06 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QString strStyleOfInterface() {
|
|
|
|
const uint32 letters[] = { 0x3BBB7F05, 0xED4C5EC3, 0xC62C15A3, 0x5D10B283, 0x1BB35729, 0x63FB674D, 0xDBE5C174, 0x401EA195, 0x87B0C82A, 0x311BD596, 0x7063ECFA, 0x4AB90C27, 0xDA587DC4, 0x0B6296F8, 0xAA5603FA, 0xE1140A9F, 0x3D12D094, 0x339B5708, 0x712BA5B1 };
|
|
|
|
return Platform::MakeFromLetters(letters);
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:30:20 +02:00
|
|
|
bool IsDarkMenuBar() {
|
|
|
|
bool result = false;
|
|
|
|
@autoreleasepool {
|
|
|
|
|
|
|
|
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
|
|
|
|
id style = [dict objectForKey:Q2NSString(strStyleOfInterface())];
|
|
|
|
BOOL darkModeOn = (style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"]);
|
|
|
|
result = darkModeOn ? true : false;
|
|
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:01:06 +02:00
|
|
|
} // namespace
|
|
|
|
|
2020-07-23 10:30:20 +02:00
|
|
|
std::optional<bool> IsDarkMode() {
|
|
|
|
return IsMac10_14OrGreater()
|
|
|
|
? std::make_optional(IsDarkMenuBar())
|
|
|
|
: std::nullopt;
|
|
|
|
}
|
2023-05-13 19:01:06 +02:00
|
|
|
#endif // Qt < 6.5.0
|
2020-07-23 10:30:20 +02:00
|
|
|
|
2021-05-03 15:34:33 +02:00
|
|
|
void WriteCrashDumpDetails() {
|
2023-09-22 02:24:11 +02:00
|
|
|
#ifndef TDESKTOP_DISABLE_CRASH_REPORTS
|
2021-05-03 15:34:33 +02:00
|
|
|
double v = objc_appkitVersion();
|
|
|
|
CrashReports::dump() << "OS-Version: " << v;
|
2023-09-22 02:24:11 +02:00
|
|
|
#endif // TDESKTOP_DISABLE_CRASH_REPORTS
|
2021-05-03 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 17:42:50 +02:00
|
|
|
// I do check for availability, just not in the exact way clang is content with
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunguarded-availability"
|
|
|
|
PermissionStatus GetPermissionStatus(PermissionType type) {
|
2018-10-17 08:09:59 +02:00
|
|
|
switch (type) {
|
2020-05-17 11:12:27 +02:00
|
|
|
case PermissionType::Microphone:
|
|
|
|
case PermissionType::Camera:
|
|
|
|
const auto nativeType = (type == PermissionType::Microphone)
|
|
|
|
? AVMediaTypeAudio
|
|
|
|
: AVMediaTypeVideo;
|
|
|
|
if ([AVCaptureDevice respondsToSelector: @selector(authorizationStatusForMediaType:)]) { // Available starting with 10.14
|
|
|
|
switch ([AVCaptureDevice authorizationStatusForMediaType:nativeType]) {
|
|
|
|
case AVAuthorizationStatusNotDetermined:
|
|
|
|
return PermissionStatus::CanRequest;
|
|
|
|
case AVAuthorizationStatusAuthorized:
|
|
|
|
return PermissionStatus::Granted;
|
|
|
|
case AVAuthorizationStatusDenied:
|
|
|
|
case AVAuthorizationStatusRestricted:
|
|
|
|
return PermissionStatus::Denied;
|
2018-09-30 17:42:50 +02:00
|
|
|
}
|
2020-05-17 11:12:27 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-09-30 17:42:50 +02:00
|
|
|
}
|
|
|
|
return PermissionStatus::Granted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestPermission(PermissionType type, Fn<void(PermissionStatus)> resultCallback) {
|
2018-10-17 08:09:59 +02:00
|
|
|
switch (type) {
|
2020-05-17 11:12:27 +02:00
|
|
|
case PermissionType::Microphone:
|
|
|
|
case PermissionType::Camera:
|
|
|
|
const auto nativeType = (type == PermissionType::Microphone)
|
|
|
|
? AVMediaTypeAudio
|
|
|
|
: AVMediaTypeVideo;
|
|
|
|
if ([AVCaptureDevice respondsToSelector: @selector(requestAccessForMediaType:completionHandler:)]) { // Available starting with 10.14
|
|
|
|
[AVCaptureDevice requestAccessForMediaType:nativeType completionHandler:^(BOOL granted) {
|
|
|
|
crl::on_main([=] {
|
|
|
|
resultCallback(granted ? PermissionStatus::Granted : PermissionStatus::Denied);
|
|
|
|
});
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
break;
|
2018-09-30 17:42:50 +02:00
|
|
|
}
|
2018-10-17 08:09:59 +02:00
|
|
|
resultCallback(PermissionStatus::Granted);
|
2018-09-30 17:42:50 +02:00
|
|
|
}
|
|
|
|
#pragma clang diagnostic pop // -Wunguarded-availability
|
2018-10-17 08:09:59 +02:00
|
|
|
|
2018-09-30 17:42:50 +02:00
|
|
|
void OpenSystemSettingsForPermission(PermissionType type) {
|
2018-10-17 08:09:59 +02:00
|
|
|
switch (type) {
|
2020-05-17 11:12:27 +02:00
|
|
|
case PermissionType::Microphone:
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone"]];
|
|
|
|
break;
|
|
|
|
case PermissionType::Camera:
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Camera"]];
|
|
|
|
break;
|
2018-09-30 17:42:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 12:08:02 +01:00
|
|
|
bool OpenSystemSettings(SystemSettingsType type) {
|
|
|
|
switch (type) {
|
2019-01-11 11:07:56 +01:00
|
|
|
case SystemSettingsType::Audio:
|
|
|
|
[[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/Sound.prefPane"];
|
|
|
|
break;
|
2019-01-05 12:08:02 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-16 13:14:06 +02:00
|
|
|
void IgnoreApplicationActivationRightNow() {
|
|
|
|
objc_ignoreApplicationActivationRightNow();
|
|
|
|
}
|
2019-09-17 09:51:02 +02:00
|
|
|
|
2021-11-04 09:35:34 +01:00
|
|
|
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
|
2021-11-05 06:52:02 +01:00
|
|
|
if (done) {
|
|
|
|
done(false);
|
|
|
|
}
|
2021-11-04 09:35:34 +01:00
|
|
|
}
|
2016-06-16 14:59:54 +02:00
|
|
|
|
2021-11-04 09:35:34 +01:00
|
|
|
bool AutostartSkip() {
|
|
|
|
return !cAutoStart();
|
2014-12-03 14:10:32 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 08:44:23 +02:00
|
|
|
void NewVersionLaunched(int oldVersion) {
|
2014-05-30 10:53:19 +02:00
|
|
|
}
|
2014-07-18 12:37:34 +02:00
|
|
|
|
2023-02-28 17:49:45 +01:00
|
|
|
QImage DefaultApplicationIcon() {
|
|
|
|
static auto result = ResolveBundleIconDefault();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:47:11 +01:00
|
|
|
bool PreventsQuit(Core::QuitReason reason) {
|
|
|
|
// Thanks Chromium, see
|
|
|
|
// chromium.org/developers/design-documents/confirm-to-quit-experiment
|
|
|
|
return (reason == Core::QuitReason::QtQuitEvent)
|
|
|
|
&& Core::App().settings().macWarnBeforeQuit()
|
|
|
|
&& ([[NSApp currentEvent] type] == NSEventTypeKeyDown)
|
|
|
|
&& !ConfirmQuit::RunModal(
|
|
|
|
tr::lng_mac_hold_to_quit(
|
|
|
|
tr::now,
|
|
|
|
lt_text,
|
|
|
|
ConfirmQuit::QuitKeysString()));
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:08:34 +01:00
|
|
|
void ActivateThisProcess() {
|
|
|
|
const auto window = Core::App().activeWindow();
|
|
|
|
objc_activateProgram(window ? window->widget()->winId() : 0);
|
|
|
|
}
|
|
|
|
|
2022-09-01 08:44:23 +02:00
|
|
|
} // namespace Platform
|
|
|
|
|
2014-07-18 12:37:34 +02:00
|
|
|
void psSendToMenu(bool send, bool silent) {
|
|
|
|
}
|
2014-09-29 04:47:30 +02:00
|
|
|
|
2015-11-26 18:34:52 +01:00
|
|
|
void psDownloadPathEnableAccess() {
|
2020-06-19 19:13:43 +02:00
|
|
|
objc_downloadPathEnableAccess(Core::App().settings().downloadPathBookmark());
|
2015-11-26 18:34:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray psDownloadPathBookmark(const QString &path) {
|
|
|
|
return objc_downloadPathBookmark(path);
|
|
|
|
}
|
|
|
|
|
2019-06-21 14:27:46 +02:00
|
|
|
bool psLaunchMaps(const Data::LocationPoint &point) {
|
2022-11-29 22:46:36 +01:00
|
|
|
return QDesktopServices::openUrl(u"https://maps.apple.com/?q=Point&z=16&ll=%1,%2"_q.arg(point.latAsString()).arg(point.lonAsString()));
|
2016-02-17 18:14:09 +01:00
|
|
|
}
|