Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #import <ApplicationServices/ApplicationServices.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "nsObjCExceptions.h" |
michael@0 | 10 | #include "nsMIMEInfoMac.h" |
michael@0 | 11 | #include "nsILocalFileMac.h" |
michael@0 | 12 | #include "nsIFileURL.h" |
michael@0 | 13 | |
michael@0 | 14 | // We override this to make sure app bundles display their pretty name (without .app suffix) |
michael@0 | 15 | NS_IMETHODIMP nsMIMEInfoMac::GetDefaultDescription(nsAString& aDefaultDescription) |
michael@0 | 16 | { |
michael@0 | 17 | if (mDefaultApplication) { |
michael@0 | 18 | nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(mDefaultApplication); |
michael@0 | 19 | if (macFile) { |
michael@0 | 20 | bool isPackage; |
michael@0 | 21 | (void)macFile->IsPackage(&isPackage); |
michael@0 | 22 | if (isPackage) |
michael@0 | 23 | return macFile->GetBundleDisplayName(aDefaultDescription); |
michael@0 | 24 | } |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | return nsMIMEInfoImpl::GetDefaultDescription(aDefaultDescription); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | NS_IMETHODIMP |
michael@0 | 31 | nsMIMEInfoMac::LaunchWithFile(nsIFile *aFile) |
michael@0 | 32 | { |
michael@0 | 33 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 34 | |
michael@0 | 35 | nsCOMPtr<nsIFile> application; |
michael@0 | 36 | nsresult rv; |
michael@0 | 37 | |
michael@0 | 38 | NS_ASSERTION(mClass == eMIMEInfo, "only MIME infos are currently allowed" |
michael@0 | 39 | "to pass content by value"); |
michael@0 | 40 | |
michael@0 | 41 | if (mPreferredAction == useHelperApp) { |
michael@0 | 42 | |
michael@0 | 43 | // we don't yet support passing content by value (rather than reference) |
michael@0 | 44 | // to web apps. at some point, we will probably want to. |
michael@0 | 45 | nsCOMPtr<nsILocalHandlerApp> localHandlerApp = |
michael@0 | 46 | do_QueryInterface(mPreferredApplication, &rv); |
michael@0 | 47 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 48 | |
michael@0 | 49 | rv = localHandlerApp->GetExecutable(getter_AddRefs(application)); |
michael@0 | 50 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 51 | |
michael@0 | 52 | } else if (mPreferredAction == useSystemDefault) { |
michael@0 | 53 | application = mDefaultApplication; |
michael@0 | 54 | } |
michael@0 | 55 | else |
michael@0 | 56 | return NS_ERROR_INVALID_ARG; |
michael@0 | 57 | |
michael@0 | 58 | // if we've already got an app, just QI so we have the launchWithDoc method |
michael@0 | 59 | nsCOMPtr<nsILocalFileMac> app; |
michael@0 | 60 | if (application) { |
michael@0 | 61 | app = do_QueryInterface(application, &rv); |
michael@0 | 62 | if (NS_FAILED(rv)) return rv; |
michael@0 | 63 | } else { |
michael@0 | 64 | // otherwise ask LaunchServices for an app directly |
michael@0 | 65 | nsCOMPtr<nsILocalFileMac> tempFile = do_QueryInterface(aFile, &rv); |
michael@0 | 66 | if (NS_FAILED(rv)) return rv; |
michael@0 | 67 | |
michael@0 | 68 | FSRef tempFileRef; |
michael@0 | 69 | tempFile->GetFSRef(&tempFileRef); |
michael@0 | 70 | |
michael@0 | 71 | FSRef appFSRef; |
michael@0 | 72 | if (::LSGetApplicationForItem(&tempFileRef, kLSRolesAll, &appFSRef, nullptr) == noErr) |
michael@0 | 73 | { |
michael@0 | 74 | app = (do_CreateInstance("@mozilla.org/file/local;1")); |
michael@0 | 75 | if (!app) return NS_ERROR_FAILURE; |
michael@0 | 76 | app->InitWithFSRef(&appFSRef); |
michael@0 | 77 | } else { |
michael@0 | 78 | return NS_ERROR_FAILURE; |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | return app->LaunchWithDoc(aFile, false); |
michael@0 | 82 | |
michael@0 | 83 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | nsresult |
michael@0 | 87 | nsMIMEInfoMac::LoadUriInternal(nsIURI *aURI) |
michael@0 | 88 | { |
michael@0 | 89 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 90 | |
michael@0 | 91 | NS_ENSURE_ARG_POINTER(aURI); |
michael@0 | 92 | |
michael@0 | 93 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 94 | |
michael@0 | 95 | nsAutoCString uri; |
michael@0 | 96 | aURI->GetSpec(uri); |
michael@0 | 97 | if (!uri.IsEmpty()) { |
michael@0 | 98 | CFURLRef myURLRef = ::CFURLCreateWithBytes(kCFAllocatorDefault, |
michael@0 | 99 | (const UInt8*)uri.get(), |
michael@0 | 100 | strlen(uri.get()), |
michael@0 | 101 | kCFStringEncodingUTF8, |
michael@0 | 102 | NULL); |
michael@0 | 103 | if (myURLRef) { |
michael@0 | 104 | OSStatus status = ::LSOpenCFURLRef(myURLRef, NULL); |
michael@0 | 105 | if (status == noErr) |
michael@0 | 106 | rv = NS_OK; |
michael@0 | 107 | ::CFRelease(myURLRef); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | return rv; |
michael@0 | 112 | |
michael@0 | 113 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 114 | } |