Add "Send File via KDE Connect" Finder integration on macOS

This commit is contained in:
Weixuan Xiao 2020-05-19 13:41:02 +00:00
parent fca66244ab
commit b57a321632
5 changed files with 161 additions and 1 deletions

View file

@ -13,7 +13,7 @@ ecm_add_app_icon(indicator_SRCS ICONS
if (WIN32)
list(APPEND indicator_SRCS indicatorhelper_win.cpp)
elseif (APPLE)
list(APPEND indicator_SRCS indicatorhelper_mac.cpp)
list(APPEND indicator_SRCS indicatorhelper_mac.cpp serviceregister_mac.mm)
else ()
list(APPEND indicator_SRCS indicatorhelper.cpp)
endif()
@ -26,6 +26,8 @@ if (WIN32)
endif()
if (APPLE)
# Apple frameworks
target_link_libraries(kdeconnect-indicator "-framework AppKit")
# Apple app package
set_target_properties(kdeconnect-indicator PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist)
set_target_properties(kdeconnect-indicator PROPERTIES

View file

@ -34,5 +34,38 @@
<string>1</string>
<key>NSHighResolutionCapable</key>
<string>True</string>
<key>NSServices</key>
<array>
<dict>
<key>NSBackgroundColorName</key>
<string>background</string>
<key>NSIconName</key>
<string>NSActionTemplate</string>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Send File via KDE Connect</string>
</dict>
<key>NSMessage</key>
<string>sendViaKDEConnect</string>
<key>NSPortName</key>
<string>SendViaKDEConnect</string>
<key>NSRequiredContext</key>
<dict>
<key>NSApplicationIdentifier</key>
<string>com.apple.finder</string>
</dict>
<key>NSRequiredContext</key>
<dict>
<key>NSTextContent</key>
<string>FilePath</string>
</dict>
<key>NSSendTypes</key>
<array>
<string>NSFilenamesPboardType</string>
<string>public.plain-text</string>
</array>
</dict>
</array>
</dict>
</plist>

View file

@ -32,9 +32,12 @@
#include "indicatorhelper.h"
#include "serviceregister_mac.h"
IndicatorHelper::IndicatorHelper()
{
registerServices();
QIcon kdeconnectIcon = QIcon::fromTheme(QStringLiteral("kdeconnect"));
QPixmap splashPixmap(kdeconnectIcon.pixmap(256, 256));

View file

@ -0,0 +1,26 @@
/*
* Copyright 2020 Weixuan XIAO <veyx.shaw@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SERVICE_REGISTER_H
#define SERVICE_REGISTER_H
void registerServices();
#endif

View file

@ -0,0 +1,96 @@
/*
* Copyright 2020 Weixuan XIAO <veyx.shaw@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <QFile>
#include <QProcess>
#include <QApplication>
@interface KDEConnectSendFileService : NSObject
- (void)sendViaKDEConnect:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error;
@end
static KDEConnectSendFileService *sendFileService = nil;
static void cleanup_service()
{
if (sendFileService != nil)
[sendFileService release];
sendFileService = nil;
}
void registerServices() {
NSLog(@"Registering KDE Connect Send File Service");
KDEConnectSendFileService *sendFileService = [[KDEConnectSendFileService alloc] init];
qAddPostRoutine(cleanup_service); // Remove after quit
NSRegisterServicesProvider(sendFileService, @"SendViaKDEConnect");
NSLog(@"KDE Connect Send File Service registered");
}
@implementation KDEConnectSendFileService
- (void)sendViaKDEConnect:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
Q_UNUSED(userData);
Q_UNUSED(error);
NSAlert *alert = nil;
// NSPasteboardTypeFileURL is first introduced in macOS 13, avoid to use it now
NSArray *filePathArray = [pboard propertyListForType:NSFilenamesPboardType];
for (NSString *path in filePathArray) {
BOOL isDirectory = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
if (!isDirectory) {
if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnect-handler"))) {
QProcess kdeconnect_handler;
kdeconnect_handler.setProgram(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnect-handler"));
kdeconnect_handler.setArguments({QString::fromNSString(path)});
kdeconnect_handler.startDetached();
} else {
alert = [[NSAlert alloc] init];
[alert setInformativeText:@"Cannot find kdeconnect-handler"];
}
} else {
alert = [[NSAlert alloc] init];
[alert setInformativeText:@"Cannot share a directory"];
}
}
break; // Now we only support single file sharing
}
if ([filePathArray count] < 1) {
alert = [[NSAlert alloc] init];
[alert setInformativeText:@"Cannot share selected item"];
}
if (alert) {
[alert setMessageText:@"Share file failed"];
[alert addButtonWithTitle:@"OK"];
[alert setAlertStyle:NSAlertStyleCritical];
[alert runModal];
[alert release];
}
}
@end