Skip to content

Commit

Permalink
Improve the application and URL launching on OS X. (svn #457)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrusdaboo authored and mbert committed Jul 27, 2012
1 parent 1140175 commit 9d763e4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 36 deletions.
9 changes: 5 additions & 4 deletions MacOS/Sources/Application/General/CMulberryApp.cp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "CAdbkSearchWindow.h"
#include "CAddressBookWindow.h"
#include "CAdminLock.h"
#include "CAppLaunch.h"
#include "CApplyRulesMenu.h"
#include "CAttachmentManager.h"
#include "CBalloonDialog.h"
Expand Down Expand Up @@ -1427,15 +1428,15 @@ void CMulberryApp::OnHelpMulberryOnline()
{
// Launch the Mulberry webpage URL
cdstring url = CAdminLock::sAdminLock.mMulberryURL;
OSStatus err = CICSupport::ICLaunchURL(url);
OSStatus err = CAppLaunch::LaunchURL(url);
}

// Go to Mulberry faq web page
void CMulberryApp::OnHelpOnlineFAQ()
{
// Launch the Mulberry FAQ webpage URL
cdstring url = CAdminLock::sAdminLock.mMulberryFAQ;
OSStatus err = CICSupport::ICLaunchURL(url);
OSStatus err = CAppLaunch::LaunchURL(url);
}

// Go to check updates page
Expand Down Expand Up @@ -1472,7 +1473,7 @@ void CMulberryApp::OnHelpCheckUpdates()
url += cdstring(static_cast<unsigned long>(GetVersionNumber().nonRelRev));

// Launch the URL
OSStatus err = CICSupport::ICLaunchURL(url);
OSStatus err = CAppLaunch::LaunchURL(url);
}

// Create Mulberry support message
Expand All @@ -1495,7 +1496,7 @@ void CMulberryApp::OnHelpBuyMulberry()
{
// Launch the Mulberry order form webpage URL
cdstring url = cBuyMulberryURL;
OSStatus err = CICSupport::ICLaunchURL(url);
OSStatus err = CAppLaunch::LaunchURL(url);
}

#pragma mark ____________________________Command handling
Expand Down
6 changes: 2 additions & 4 deletions MacOS/Sources/Formatting/CFormattedTextDisplay.cp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#include "CFormattedTextDisplay.h"

#include "CAddressBookManager.h"
#include "CAppLaunch.h"
#include "CATSUIStyle.h"
#include "CBetterScrollerX.h"
#include "CGUtils.h"
#include "CClickList.h"
#include "CCommands.h"
#include "CICSupport.h"
#include "CMulberryCommon.h"
#include "CParserEnriched.h"
#include "CParserHTML.h"
Expand Down Expand Up @@ -1118,9 +1118,7 @@ bool CFormattedTextDisplay::GetLineRange(SInt32 &first, SInt32 &last, SInt32 sta

bool CFormattedTextDisplay::LaunchURL(const char* url)
{
OSStatus err;
err = CICSupport::ICLaunchURL((char*) url); // justin grab_c_str change

OSStatus err = CAppLaunch::LaunchURL(url); // justin grab_c_str change
return (err == noErr);
}

Expand Down
54 changes: 51 additions & 3 deletions MacOS/Sources/Support/CAppLaunch.cp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include "CAppLaunch.h"

#include <SysCFArray.h>
#include <SysCFString.h>
#include <SysCFURL.h>

#include <LaunchServices.h>

// OpenSpecifiedDocument searches to see if the application which
Expand All @@ -29,14 +33,37 @@
// (remember that, because of puppet strings, this works even
// if the target application is not Apple event-aware.)

OSErr CAppLaunch::OpenDocumentWithApp(const PPx::FSObject* doc, OSType appCreator)
OSErr CAppLaunch::OpenDocumentWithApp(const PPx::FSObject* doc, const cdstring& mimeType, OSType appCreator)
{
// verify the document file exists and get its creator type

if (!doc->Exists())
return fnfErr;

return LaunchApplicationWithDocument(appCreator, doc);
if (!appCreator)
{
// Try mime type first, then just use file name (extension)
PPx::CFString inMIMEType(mimeType, kCFStringEncodingUTF8);
CFURLRef outAppURL;
if (::LSCopyApplicationForMIMEType(inMIMEType, kLSRolesAll, &outAppURL) == noErr)
{
OSErr err = LaunchApplicationWithDocument(outAppURL, doc);
::CFRelease(outAppURL);
return err;
}

return ::LSOpenFSRef(&doc->UseRef(), NULL);
}
else
return LaunchApplicationWithDocument(appCreator, doc);
}

// Launch the URL
OSErr CAppLaunch::LaunchURL(const cdstring& url)
{
PPx::CFString urlStr(url, kCFStringEncodingUTF8);
PPx::CFURL inURL(urlStr);
return ::LSOpenCFURLRef(inURL, NULL);
}

//----------------------------------------------------------------------------
Expand All @@ -48,7 +75,6 @@ OSErr CAppLaunch::OpenDocumentWithApp(const PPx::FSObject* doc, OSType appCreato
//----------------------------------------------------------------------------
OSErr CAppLaunch::LaunchApplicationWithDocument(OSType appCreator, const PPx::FSObject* doc)
{
// Code taken from TechNote #2017 on Launch Services

// Find the application on disk
FSRef outAppRef;
Expand All @@ -68,3 +94,25 @@ OSErr CAppLaunch::LaunchApplicationWithDocument(OSType appCreator, const PPx::FS

return err;
}

OSErr CAppLaunch::LaunchApplicationWithDocument(CFURLRef appURL, const PPx::FSObject* doc)
{
PPx::CFURL docURL(doc->UseRef());
PPx::CFArray<CFURLRef> array(1);
array.AppendValue(docURL);


// Try to launch it with the document

LSLaunchURLSpec inLaunchSpec;
inLaunchSpec.appURL = appURL;
inLaunchSpec.itemURLs = array;
inLaunchSpec.passThruParams = NULL;
inLaunchSpec.launchFlags = kLSLaunchDefaults;
inLaunchSpec.asyncRefCon = NULL;

OSErr err = ::LSOpenFromURLSpec(&inLaunchSpec, NULL);

return err;
}

5 changes: 4 additions & 1 deletion MacOS/Sources/Support/CAppLaunch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class CAppLaunch
{
public:

static OSErr OpenDocumentWithApp(const PPx::FSObject* doc, OSType appCreator);
static OSErr OpenDocumentWithApp(const PPx::FSObject* doc, const cdstring& mimeType, OSType appCreator);

static OSErr LaunchURL(const cdstring& url);

private:
static OSErr LaunchApplicationWithDocument(OSType appCreator, const PPx::FSObject* doc);
static OSErr LaunchApplicationWithDocument(CFURLRef appURL, const PPx::FSObject* doc);

CAppLaunch(); // Never create
~CAppLaunch();
Expand Down
7 changes: 4 additions & 3 deletions Sources_Common/Mail/Attachments/CAttachment.cp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,6 @@ const cdstring CAttachment::GetMappedName(bool not_empty, bool force) const
#endif
}

#if __dest_os == __win32_os || __dest_os == __linux_os
// Must have proper file extension
if (!::strchr(name.c_str(), '.'))
name += CMIMESupport::MapMIMEToFileExt((CAttachment&) *part_to_map);
Expand All @@ -1216,7 +1215,6 @@ const cdstring CAttachment::GetMappedName(bool not_empty, bool force) const
if (::strrchr(result.c_str(), '.'))
name += cdstring(::strrchr(result.c_str(), '.'));
}
#endif

const_cast<CAttachment*>(this)->SetName(name);
}
Expand Down Expand Up @@ -1322,10 +1320,12 @@ bool CAttachment::UniqueFile(const cdstring& fpath) const
// Get suite of icons from icon cache
const CIconRef* CAttachment::GetIconRef() const
{
#if 0
#if __dest_os == __mac_os || __dest_os == __mac_os_x
ICMapEntry entry;
if (CICSupport::ICMapFileName(mContent, entry, true) == noErr)
return CDesktopIcons::GetDesktopIcons(entry.fileCreator, entry.fileType);
#endif
#endif
return CDesktopIcons::GetDesktopIconsFromMIME(mContent.GetContentTypeText(), mContent.GetContentSubtypeText());
}
Expand Down Expand Up @@ -2078,7 +2078,8 @@ void CAttachment::TryLaunch(CFullFileStream* aFile) const
appCreator = finfo.file.fileCreator;
}

CAppLaunch::OpenDocumentWithApp(&fspec, appCreator);
cdstring mimeType = CMIMESupport::GenerateContentHeader(this, false, lendl, false);
CAppLaunch::OpenDocumentWithApp(&fspec, mimeType, appCreator);
#elif __dest_os == __win32_os
TCHAR dir[MAX_PATH];
if (::GetCurrentDirectory(MAX_PATH, dir))
Expand Down
15 changes: 12 additions & 3 deletions Sources_Common/Mail/Attachments/CMIMESupport.cp
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,6 @@ void CMIMESupport::MapFileToMIME(CAttachment& attach)
}
#endif

#if ( __dest_os == __win32_os ) || (__dest_os == __linux_os)
// Get file extension for MIME
cdstring CMIMESupport::MapMIMEToFileExt(const CAttachment& attach)
{
Expand All @@ -944,12 +943,18 @@ cdstring CMIMESupport::MapMIMEToFileExt(const CAttachment& attach)
#if __dest_os == __win32_os
// Look in registry
return CWinRegistry::GetSuffixFromMIME(type, original);
#else
#elif __dest_os == __linux_os
// Look in MIME map
return CMIMETypesMap::sMIMETypesMap.GetExtension(type, original);
#else
if (type == "text/plain")
return ".txt";
else if (type == "text/html")
return ".html";
else
return "";
#endif
}
#endif

// Get application that will open MIME part
cdstring CMIMESupport::MapMIMEToApp(const CAttachment& attach)
Expand Down Expand Up @@ -1027,6 +1032,7 @@ OSType CMIMESupport::MapMIMEToCreator(const CAttachment& attach)
if (attach.GetContent().IsBinHexed() || attach.GetContent().IsUUed() || attach.IsApplefile())
return 0L;

#if 0 // Will rely on LaunchServices from now on
// Lookup attachment with internet config
ICMapEntry entry;
if (CICSupport::ICMapFileName(attach.GetContent(), entry) == noErr)
Expand All @@ -1037,12 +1043,14 @@ OSType CMIMESupport::MapMIMEToCreator(const CAttachment& attach)
return entry.fileCreator;
}
else
#endif
return 0L;
}

// Get application creator that will open MIME part
OSType CMIMESupport::MapMIMEToCreator(const cdstring& fname, const cdstring& type)
{
#if 0 // Will rely on LaunchServices from now on
// Lookup attachment with internet config
ICMapEntry entry;
if (CICSupport::ICMapMIMEType(fname, type, entry) == noErr)
Expand All @@ -1053,6 +1061,7 @@ OSType CMIMESupport::MapMIMEToCreator(const cdstring& fname, const cdstring& typ
return entry.fileCreator;
}
else
#endif
return 0L;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources_Common/Mail/Attachments/CMIMESupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class CMIMESupport

#if (__dest_os == __win32_os) || (__dest_os == __linux_os)
static void MapFileToMIME(CAttachment& attach); // Map file extension to MIME content
static cdstring MapMIMEToFileExt(const CAttachment& attach); // Get file extension for MIME
#endif
static cdstring MapMIMEToFileExt(const CAttachment& attach); // Get file extension for MIME
static cdstring MapMIMEToApp(const CAttachment& attach); // Get application that will open MIME part
#if __dest_os == __mac_os || __dest_os == __mac_os_x
static OSType MapMIMEToCreator(const CAttachment& attach); // Get application that will open MIME part
Expand Down
27 changes: 10 additions & 17 deletions Sources_Common/Mail/Control/CAttachmentManager.cp
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ LStream* CAttachmentManager::GetFileStream(CAttachment& attach, bool view)
case eBinaryEncoding:
case eXtokenEncoding:
// No filter - copy as is
ICMapEntry entry;
CICSupport::ICMapFileName(attach.GetContent(), entry);
aFile->CreateNewDataFile(entry.fileCreator, entry.fileType, smCurrentScript);
newFlavor.fileType = entry.fileType;
newFlavor.fileCreator = entry.fileCreator;
ICMapEntry entry2;
CICSupport::ICMapFileName(attach.GetContent(), entry2);
aFile->CreateNewDataFile(entry2.fileCreator, entry2.fileType, smCurrentScript);
newFlavor.fileType = entry2.fileType;
newFlavor.fileCreator = entry2.fileCreator;
newFlavor.fdFlags = 0;
break;

Expand Down Expand Up @@ -725,8 +725,8 @@ bool CAttachmentManager::MapToFile(const cdstring& name, fspectype& file, bool v
// Add number to name
nname = fname + cdstring((long) ctr++);
fpath = CPreferences::sPrefs->mDefaultDownload.GetValue() + nname;
MyCFString cfstr(fpath, kCFStringEncodingUTF8);
fspec = PPx::FSObject(cfstr);
MyCFString cfstr2(fpath, kCFStringEncodingUTF8);
fspec = PPx::FSObject(cfstr2);
}

// Alert if directory not found
Expand Down Expand Up @@ -902,15 +902,7 @@ bool CAttachmentManager::LaunchURL(const cdstring& url) const
// Try default map first
OSType appCreator = CMIMESupport::MapMIMEToCreator(name, GetEntry(url)->second.mMimeType);

// If not found or decoded file use file's creator
if (!appCreator)
{
FinderInfo finfo;
fspec.GetFinderInfo(&finfo, NULL, NULL);
appCreator = finfo.file.fileCreator;
}

CAppLaunch::OpenDocumentWithApp(&fspec, appCreator);
CAppLaunch::OpenDocumentWithApp(&fspec, GetEntry(url)->second.mMimeType, appCreator);
#elif __dest_os == __win32_os
TCHAR dir[MAX_PATH];
if (::GetCurrentDirectory(MAX_PATH, dir))
Expand Down Expand Up @@ -1051,7 +1043,8 @@ void CAttachmentManager::TryLaunch(const CAttachment* attach, const fspectype& f
appCreator = finfo.file.fileCreator;
}

CAppLaunch::OpenDocumentWithApp(&fspec, appCreator);
cdstring mimeType = CMIMESupport::GenerateContentHeader(attach, false, lendl, false);
CAppLaunch::OpenDocumentWithApp(&fspec, mimeType, appCreator);
#elif __dest_os == __win32_os
TCHAR dir[MAX_PATH];
if (::GetCurrentDirectory(MAX_PATH, dir))
Expand Down

0 comments on commit 9d763e4

Please sign in to comment.