From 5a97697702a05ff562e32802d6117356f6c4bce4 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Mon, 14 Jun 2010 13:22:45 +0000 Subject: [PATCH] Add support for opening files via Finder in OS X. Update issue #405 Status: Started Opening from Finder works but Clementine still isn't a "recommended" application. --- src/core/mac_startup.h | 11 ++++++++++- src/core/mac_startup.mm | 33 ++++++++++++++++++++++----------- src/ui/mainwindow.cpp | 17 ++++++++++++----- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/core/mac_startup.h b/src/core/mac_startup.h index 7643381eb..5a99646f9 100644 --- a/src/core/mac_startup.h +++ b/src/core/mac_startup.h @@ -4,11 +4,20 @@ class GlobalShortcuts; class QObject; +class PlatformInterface { + public: + // Called when the application should show itself. + virtual void Activate() = 0; + virtual bool LoadUrl(const QString& url) = 0; + + virtual ~PlatformInterface() {} +}; + namespace mac { void MacMain(); void SetShortcutHandler(GlobalShortcuts* handler); -void SetApplicationHandler(QObject* handler); +void SetApplicationHandler(PlatformInterface* handler); void CheckForUpdates(); QString GetBundlePath(); diff --git a/src/core/mac_startup.mm b/src/core/mac_startup.mm index 31fc0cb03..361ad9a71 100644 --- a/src/core/mac_startup.mm +++ b/src/core/mac_startup.mm @@ -15,30 +15,31 @@ #include #include -#include + +#include // Capture global media keys on Mac (Cocoa only!) // See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/ @interface MacApplication :NSApplication { GlobalShortcuts* shortcut_handler_; - QObject* application_handler_; + PlatformInterface* application_handler_; } - (GlobalShortcuts*) shortcut_handler; - (void) SetShortcutHandler: (GlobalShortcuts*)handler; -- (QObject*) application_handler; -- (void) SetApplicationHandler: (QObject*)handler; +- (PlatformInterface*) application_handler; +- (void) SetApplicationHandler: (PlatformInterface*)handler; - (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat; @end @interface AppDelegate :NSObject { // { - QObject* application_handler_; + PlatformInterface* application_handler_; } -- (id) initWithHandler: (QObject*)handler; +- (id) initWithHandler: (PlatformInterface*)handler; // NSApplicationDelegate - (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag; @end @@ -52,17 +53,27 @@ return self; } -- (id) initWithHandler: (QObject*)handler { +- (id) initWithHandler: (PlatformInterface*)handler { application_handler_ = handler; return self; } - (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag { if (application_handler_) { - qApp->postEvent(application_handler_, new QEvent(QEvent::ApplicationActivate)); + application_handler_->Activate(); } return YES; } + +- (BOOL) application: (NSApplication*)app openFile:(NSString*)filename { + qDebug() << "Wants to open:" << [filename UTF8String]; + + if (application_handler_->LoadUrl(QString::fromUtf8([filename UTF8String]))) { + return YES; + } + + return NO; +} @end @implementation MacApplication @@ -83,11 +94,11 @@ shortcut_handler_ = handler; } -- (QObject*) application_handler { +- (PlatformInterface*) application_handler { return application_handler_; } -- (void) SetApplicationHandler: (QObject*)handler { +- (void) SetApplicationHandler: (PlatformInterface*)handler { AppDelegate* delegate = [[AppDelegate alloc] initWithHandler:handler]; [self setDelegate:delegate]; } @@ -144,7 +155,7 @@ void SetShortcutHandler(GlobalShortcuts* handler) { [NSApp SetShortcutHandler: handler]; } -void SetApplicationHandler(QObject* handler) { +void SetApplicationHandler(PlatformInterface* handler) { [NSApp SetApplicationHandler: handler]; } diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 2dcfe10ab..242e9b9ae 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -1169,13 +1169,20 @@ void MainWindow::ForceShowOSD(const Song &song) { osd_->SongChanged(song); } -bool MainWindow::event(QEvent* event) { - // ApplicationActivate is received when the dock is clicked on OS X. - if (event->type() == QEvent::ApplicationActivate) { - show(); +void MainWindow::Activate() { + show(); +} + +bool MainWindow::LoadUrl(const QString& path) { + // Currently this is only local files. + QFileInfo info(path); + if (info.exists()) { + QList urls; + urls << QUrl::fromLocalFile(path); + AddFilesToPlaylist(urls); return true; } - return QMainWindow::event(event); + return false; } void MainWindow::CheckForUpdates() {