2010-11-23 12:42:19 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
#import <IOKit/hidsystem/ev_keymap.h>
|
|
|
|
#import <AppKit/NSEvent.h>
|
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
#import <AppKit/NSApplication.h>
|
|
|
|
|
2010-04-22 15:57:46 +02:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2010-04-15 17:23:12 +02:00
|
|
|
#import <Foundation/NSBundle.h>
|
2010-12-09 13:34:08 +01:00
|
|
|
#import <Foundation/NSError.h>
|
|
|
|
#import <Foundation/NSFileManager.h>
|
2010-12-06 18:06:02 +01:00
|
|
|
#import <Foundation/NSPathUtilities.h>
|
2010-12-09 13:34:08 +01:00
|
|
|
#import <Foundation/NSThread.h>
|
2010-04-15 17:23:12 +02:00
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
#import <Foundation/NSURL.h>
|
|
|
|
#import <AppKit/NSNibDeclarations.h>
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
#import <Kernel/AvailabilityMacros.h>
|
|
|
|
|
2010-06-18 13:41:16 +02:00
|
|
|
#include "config.h"
|
2010-05-11 14:51:31 +02:00
|
|
|
#include "globalshortcuts.h"
|
2011-02-04 16:25:05 +01:00
|
|
|
#include "mac_delegate.h"
|
2010-04-13 15:55:54 +02:00
|
|
|
#include "mac_startup.h"
|
2010-06-17 22:31:34 +02:00
|
|
|
#include "macglobalshortcutbackend.h"
|
2010-12-09 13:34:08 +01:00
|
|
|
#include "utilities.h"
|
2010-04-13 15:55:54 +02:00
|
|
|
|
2010-12-14 19:20:14 +01:00
|
|
|
#ifdef HAVE_SPARKLE
|
|
|
|
#import <Sparkle/SUUpdater.h>
|
|
|
|
#endif
|
|
|
|
|
2010-04-14 23:27:27 +02:00
|
|
|
#include <QCoreApplication>
|
2010-12-09 13:34:08 +01:00
|
|
|
#include <QDir>
|
2010-04-14 23:27:27 +02:00
|
|
|
#include <QEvent>
|
2010-12-09 13:34:08 +01:00
|
|
|
#include <QFile>
|
2010-06-14 15:22:45 +02:00
|
|
|
|
|
|
|
#include <QtDebug>
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
// Capture global media keys on Mac (Cocoa only!)
|
|
|
|
// See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/
|
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
@interface MacApplication :NSApplication {
|
2010-06-17 22:31:34 +02:00
|
|
|
MacGlobalShortcutBackend* shortcut_handler_;
|
2010-06-14 15:22:45 +02:00
|
|
|
PlatformInterface* application_handler_;
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (MacGlobalShortcutBackend*) shortcut_handler;
|
|
|
|
- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler;
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (PlatformInterface*) application_handler;
|
|
|
|
- (void) SetApplicationHandler: (PlatformInterface*)handler;
|
2010-04-13 15:55:54 +02:00
|
|
|
|
2010-04-14 23:27:27 +02:00
|
|
|
- (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
|
2010-04-27 20:44:19 +02:00
|
|
|
@end
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
- (id) init {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
application_handler_ = nil;
|
2010-06-22 16:09:13 +02:00
|
|
|
dock_menu_ = nil;
|
2010-04-27 20:44:19 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (id) initWithHandler: (PlatformInterface*)handler {
|
2010-04-27 20:44:19 +02:00
|
|
|
application_handler_ = handler;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag {
|
|
|
|
if (application_handler_) {
|
2010-06-14 15:22:45 +02:00
|
|
|
application_handler_->Activate();
|
2010-04-27 20:44:19 +02:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
2010-06-14 15:22:45 +02:00
|
|
|
|
2010-06-22 16:09:13 +02:00
|
|
|
- (void) setDockMenu: (NSMenu*)menu {
|
|
|
|
dock_menu_ = menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMenu*) applicationDockMenu: (NSApplication*)sender {
|
|
|
|
return dock_menu_;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (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;
|
|
|
|
}
|
2010-04-27 20:44:19 +02:00
|
|
|
@end
|
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
@implementation MacApplication
|
|
|
|
|
|
|
|
- (id) init {
|
|
|
|
if ((self = [super init])) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[self SetShortcutHandler:nil];
|
|
|
|
[self SetApplicationHandler:nil];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (MacGlobalShortcutBackend*) shortcut_handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
return shortcut_handler_;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
shortcut_handler_ = handler;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (PlatformInterface*) application_handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
return application_handler_;
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (void) SetApplicationHandler: (PlatformInterface*)handler {
|
2010-04-27 20:44:19 +02:00
|
|
|
AppDelegate* delegate = [[AppDelegate alloc] initWithHandler:handler];
|
|
|
|
[self setDelegate:delegate];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) sendEvent: (NSEvent*)event {
|
|
|
|
if ([event type] == NSSystemDefined && [event subtype] == 8) {
|
|
|
|
int keycode = (([event data1] & 0xFFFF0000) >> 16);
|
|
|
|
int keyflags = ([event data1] & 0x0000FFFF);
|
|
|
|
int keystate = (((keyflags & 0xFF00) >> 8)) == 0xA;
|
|
|
|
int keyrepeat = (keyflags & 0x1);
|
|
|
|
|
|
|
|
[self mediaKeyEvent: keycode state: keystate repeat: keyrepeat];
|
|
|
|
}
|
|
|
|
|
|
|
|
[super sendEvent: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat {
|
2010-04-14 23:27:27 +02:00
|
|
|
if (!shortcut_handler_) {
|
2010-04-13 15:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state == 0) {
|
2010-06-17 22:31:34 +02:00
|
|
|
shortcut_handler_->MacMediaKeyPressed(key);
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
namespace mac {
|
|
|
|
|
|
|
|
void MacMain() {
|
2010-04-22 15:57:46 +02:00
|
|
|
[[NSAutoreleasePool alloc] init];
|
2010-04-13 15:55:54 +02:00
|
|
|
// Creates and sets the magic global variable so QApplication will find it.
|
|
|
|
[MacApplication sharedApplication];
|
2010-12-14 19:20:14 +01:00
|
|
|
#ifdef HAVE_SPARKLE
|
|
|
|
// Creates and sets the magic global variable for Sparkle.
|
|
|
|
[[SUUpdater sharedUpdater] setDelegate: NSApp];
|
|
|
|
#endif
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
void SetShortcutHandler(MacGlobalShortcutBackend* handler) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[NSApp SetShortcutHandler: handler];
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
void SetApplicationHandler(PlatformInterface* handler) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[NSApp SetApplicationHandler: handler];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 17:23:12 +02:00
|
|
|
void CheckForUpdates() {
|
2010-12-14 19:20:14 +01:00
|
|
|
#ifdef HAVE_SPARKLE
|
2010-04-15 17:23:12 +02:00
|
|
|
[[SUUpdater sharedUpdater] checkForUpdates: NSApp];
|
2010-12-14 19:20:14 +01:00
|
|
|
#endif
|
2010-04-15 17:23:12 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:49:20 +02:00
|
|
|
QString GetBundlePath() {
|
|
|
|
CFURLRef app_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
CFStringRef mac_path = CFURLCopyFileSystemPath(app_url, kCFURLPOSIXPathStyle);
|
2010-06-07 13:38:43 +02:00
|
|
|
const char* path = CFStringGetCStringPtr(mac_path, CFStringGetSystemEncoding());
|
2010-06-07 11:49:20 +02:00
|
|
|
QString bundle_path = QString::fromUtf8(path);
|
|
|
|
CFRelease(app_url);
|
|
|
|
CFRelease(mac_path);
|
|
|
|
return bundle_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GetResourcesPath() {
|
|
|
|
QString bundle_path = GetBundlePath();
|
|
|
|
return bundle_path + "/Contents/Resources";
|
|
|
|
}
|
|
|
|
|
2010-12-06 18:06:02 +01:00
|
|
|
QString GetApplicationSupportPath() {
|
2010-12-09 13:34:08 +01:00
|
|
|
NSAutoreleasePool* pool = [NSAutoreleasePool alloc];
|
|
|
|
[pool init];
|
2010-12-06 18:06:02 +01:00
|
|
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(
|
|
|
|
NSApplicationSupportDirectory,
|
|
|
|
NSUserDomainMask,
|
|
|
|
YES);
|
2010-12-09 13:34:08 +01:00
|
|
|
QString ret;
|
2010-12-06 18:06:02 +01:00
|
|
|
if ([paths count] > 0) {
|
|
|
|
NSString* user_path = [paths objectAtIndex:0];
|
2010-12-09 13:34:08 +01:00
|
|
|
ret = QString::fromUtf8([user_path UTF8String]);
|
|
|
|
} else {
|
|
|
|
ret = "~/Library/Application Support";
|
2010-12-06 18:06:02 +01:00
|
|
|
}
|
2010-12-09 13:34:08 +01:00
|
|
|
[pool drain];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-14 16:00:46 +01:00
|
|
|
QString GetMusicDirectory() {
|
|
|
|
NSAutoreleasePool* pool = [NSAutoreleasePool alloc];
|
|
|
|
[pool init];
|
|
|
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(
|
|
|
|
NSMusicDirectory,
|
|
|
|
NSUserDomainMask,
|
|
|
|
YES);
|
|
|
|
QString ret;
|
|
|
|
if ([paths count] > 0) {
|
|
|
|
NSString* user_path = [paths objectAtIndex:0];
|
|
|
|
ret = QString::fromUtf8([user_path UTF8String]);
|
|
|
|
} else {
|
|
|
|
ret = "~/Music";
|
|
|
|
}
|
|
|
|
[pool drain];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-09 13:34:08 +01:00
|
|
|
bool MigrateLegacyConfigFiles() {
|
|
|
|
QString old_config_dir = QString("%1/.config/%2").arg(
|
|
|
|
QDir::homePath(), QCoreApplication::organizationName());
|
|
|
|
if (!QFile::exists(old_config_dir)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-09 14:06:00 +01:00
|
|
|
QString new_config_dir = Utilities::GetConfigPath(Utilities::Path_Root);
|
|
|
|
// Create ~/Library/Application Support which should already exist anyway.
|
|
|
|
QDir::root().mkpath(GetApplicationSupportPath());
|
2010-12-09 13:34:08 +01:00
|
|
|
|
|
|
|
qDebug() << "Move from:" << old_config_dir
|
|
|
|
<< "to:" << new_config_dir;
|
|
|
|
|
|
|
|
NSFileManager* file_manager = [[NSFileManager alloc] init];
|
|
|
|
NSError* error;
|
|
|
|
bool ret = [file_manager moveItemAtPath:
|
|
|
|
[NSString stringWithUTF8String: old_config_dir.toUtf8().constData()]
|
|
|
|
toPath:[NSString stringWithUTF8String: new_config_dir.toUtf8().constData()]
|
|
|
|
error: &error];
|
|
|
|
if (!ret) {
|
|
|
|
qWarning() << [[error localizedDescription] UTF8String];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2010-12-06 18:06:02 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
} // namespace mac
|