michael@0: /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #import michael@0: michael@0: #include "nsObjCExceptions.h" michael@0: #include "nsMIMEInfoMac.h" michael@0: #include "nsILocalFileMac.h" michael@0: #include "nsIFileURL.h" michael@0: michael@0: // We override this to make sure app bundles display their pretty name (without .app suffix) michael@0: NS_IMETHODIMP nsMIMEInfoMac::GetDefaultDescription(nsAString& aDefaultDescription) michael@0: { michael@0: if (mDefaultApplication) { michael@0: nsCOMPtr macFile = do_QueryInterface(mDefaultApplication); michael@0: if (macFile) { michael@0: bool isPackage; michael@0: (void)macFile->IsPackage(&isPackage); michael@0: if (isPackage) michael@0: return macFile->GetBundleDisplayName(aDefaultDescription); michael@0: } michael@0: } michael@0: michael@0: return nsMIMEInfoImpl::GetDefaultDescription(aDefaultDescription); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsMIMEInfoMac::LaunchWithFile(nsIFile *aFile) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: nsCOMPtr application; michael@0: nsresult rv; michael@0: michael@0: NS_ASSERTION(mClass == eMIMEInfo, "only MIME infos are currently allowed" michael@0: "to pass content by value"); michael@0: michael@0: if (mPreferredAction == useHelperApp) { michael@0: michael@0: // we don't yet support passing content by value (rather than reference) michael@0: // to web apps. at some point, we will probably want to. michael@0: nsCOMPtr localHandlerApp = michael@0: do_QueryInterface(mPreferredApplication, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = localHandlerApp->GetExecutable(getter_AddRefs(application)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: } else if (mPreferredAction == useSystemDefault) { michael@0: application = mDefaultApplication; michael@0: } michael@0: else michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: // if we've already got an app, just QI so we have the launchWithDoc method michael@0: nsCOMPtr app; michael@0: if (application) { michael@0: app = do_QueryInterface(application, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: } else { michael@0: // otherwise ask LaunchServices for an app directly michael@0: nsCOMPtr tempFile = do_QueryInterface(aFile, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: FSRef tempFileRef; michael@0: tempFile->GetFSRef(&tempFileRef); michael@0: michael@0: FSRef appFSRef; michael@0: if (::LSGetApplicationForItem(&tempFileRef, kLSRolesAll, &appFSRef, nullptr) == noErr) michael@0: { michael@0: app = (do_CreateInstance("@mozilla.org/file/local;1")); michael@0: if (!app) return NS_ERROR_FAILURE; michael@0: app->InitWithFSRef(&appFSRef); michael@0: } else { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: return app->LaunchWithDoc(aFile, false); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult michael@0: nsMIMEInfoMac::LoadUriInternal(nsIURI *aURI) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: michael@0: nsAutoCString uri; michael@0: aURI->GetSpec(uri); michael@0: if (!uri.IsEmpty()) { michael@0: CFURLRef myURLRef = ::CFURLCreateWithBytes(kCFAllocatorDefault, michael@0: (const UInt8*)uri.get(), michael@0: strlen(uri.get()), michael@0: kCFStringEncodingUTF8, michael@0: NULL); michael@0: if (myURLRef) { michael@0: OSStatus status = ::LSOpenCFURLRef(myURLRef, NULL); michael@0: if (status == noErr) michael@0: rv = NS_OK; michael@0: ::CFRelease(myURLRef); michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: }