michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:set ts=4 sw=4 sts=4 et: */ 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: #ifndef __nsmimeinfoimpl_h___ michael@0: #define __nsmimeinfoimpl_h___ michael@0: michael@0: #include "nsIMIMEInfo.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsString.h" michael@0: #include "nsTArray.h" michael@0: #include "nsIMutableArray.h" michael@0: #include "nsIFile.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIURI.h" michael@0: #include "nsIProcess.h" michael@0: michael@0: /** michael@0: * UTF8 moz-icon URI string for the default handler application's icon, if michael@0: * available. michael@0: */ michael@0: #define PROPERTY_DEFAULT_APP_ICON_URL "defaultApplicationIconURL" michael@0: /** michael@0: * UTF8 moz-icon URI string for the user's preferred handler application's michael@0: * icon, if available. michael@0: */ michael@0: #define PROPERTY_CUSTOM_APP_ICON_URL "customApplicationIconURL" michael@0: michael@0: /** michael@0: * Basic implementation of nsIMIMEInfo. Incomplete - it is meant to be michael@0: * subclassed, and GetHasDefaultHandler as well as LaunchDefaultWithFile need to michael@0: * be implemented. michael@0: */ michael@0: class nsMIMEInfoBase : public nsIMIMEInfo { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: // I'd use NS_DECL_NSIMIMEINFO, but I don't want GetHasDefaultHandler michael@0: NS_IMETHOD GetFileExtensions(nsIUTF8StringEnumerator **_retval); michael@0: NS_IMETHOD SetFileExtensions(const nsACString & aExtensions); michael@0: NS_IMETHOD ExtensionExists(const nsACString & aExtension, bool *_retval); michael@0: NS_IMETHOD AppendExtension(const nsACString & aExtension); michael@0: NS_IMETHOD GetPrimaryExtension(nsACString & aPrimaryExtension); michael@0: NS_IMETHOD SetPrimaryExtension(const nsACString & aPrimaryExtension); michael@0: NS_IMETHOD GetType(nsACString & aType); michael@0: NS_IMETHOD GetMIMEType(nsACString & aMIMEType); michael@0: NS_IMETHOD GetDescription(nsAString & aDescription); michael@0: NS_IMETHOD SetDescription(const nsAString & aDescription); michael@0: NS_IMETHOD Equals(nsIMIMEInfo *aMIMEInfo, bool *_retval); michael@0: NS_IMETHOD GetPreferredApplicationHandler(nsIHandlerApp * *aPreferredAppHandler); michael@0: NS_IMETHOD SetPreferredApplicationHandler(nsIHandlerApp * aPreferredAppHandler); michael@0: NS_IMETHOD GetPossibleApplicationHandlers(nsIMutableArray * *aPossibleAppHandlers); michael@0: NS_IMETHOD GetDefaultDescription(nsAString & aDefaultDescription); michael@0: NS_IMETHOD LaunchWithFile(nsIFile *aFile); michael@0: NS_IMETHOD LaunchWithURI(nsIURI *aURI, michael@0: nsIInterfaceRequestor *aWindowContext); michael@0: NS_IMETHOD GetPreferredAction(nsHandlerInfoAction *aPreferredAction); michael@0: NS_IMETHOD SetPreferredAction(nsHandlerInfoAction aPreferredAction); michael@0: NS_IMETHOD GetAlwaysAskBeforeHandling(bool *aAlwaysAskBeforeHandling); michael@0: NS_IMETHOD SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling); michael@0: NS_IMETHOD GetPossibleLocalHandlers(nsIArray **_retval); michael@0: michael@0: enum HandlerClass { michael@0: eMIMEInfo, michael@0: eProtocolInfo michael@0: }; michael@0: michael@0: // nsMIMEInfoBase methods michael@0: nsMIMEInfoBase(const char *aMIMEType = "") NS_HIDDEN; michael@0: nsMIMEInfoBase(const nsACString& aMIMEType) NS_HIDDEN; michael@0: nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) NS_HIDDEN; michael@0: virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's Release should call the subclass's destructor michael@0: michael@0: void SetMIMEType(const nsACString & aMIMEType) { mSchemeOrType = aMIMEType; } michael@0: michael@0: void SetDefaultDescription(const nsString& aDesc) { mDefaultAppDescription = aDesc; } michael@0: michael@0: /** michael@0: * Copies basic data of this MIME Info Implementation to the given other michael@0: * MIME Info. The data consists of the MIME Type, the (default) description, michael@0: * the MacOS type and creator, and the extension list (this object's michael@0: * extension list will replace aOther's list, not append to it). This michael@0: * function also ensures that aOther's primary extension will be the same as michael@0: * the one of this object. michael@0: */ michael@0: void CopyBasicDataTo(nsMIMEInfoBase* aOther); michael@0: michael@0: /** michael@0: * Return whether this MIMEInfo has any extensions michael@0: */ michael@0: bool HasExtensions() const { return mExtensions.Length() != 0; } michael@0: michael@0: protected: michael@0: /** michael@0: * Launch the default application for the given file. michael@0: * For even more control over the launching, override launchWithFile. michael@0: * Also see the comment about nsIMIMEInfo in general, above. michael@0: * michael@0: * @param aFile The file that should be opened michael@0: */ michael@0: virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile) = 0; michael@0: michael@0: /** michael@0: * Loads the URI with the OS default app. michael@0: * michael@0: * @param aURI The URI to pass off to the OS. michael@0: */ michael@0: virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0; michael@0: michael@0: static already_AddRefed InitProcess(nsIFile* aApp, michael@0: nsresult* aResult); michael@0: michael@0: /** michael@0: * This method can be used to launch the file or URI with a single michael@0: * argument (typically either a file path or a URI spec). This is michael@0: * meant as a helper method for implementations of michael@0: * LaunchWithURI/LaunchDefaultWithFile. michael@0: * michael@0: * @param aApp The application to launch (may not be null) michael@0: * @param aArg The argument to pass on the command line michael@0: */ michael@0: static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp, michael@0: const nsCString &aArg); michael@0: static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp, michael@0: const nsString &aArg); michael@0: michael@0: /** michael@0: * Given a file: nsIURI, return the associated nsIFile michael@0: * michael@0: * @param aURI the file: URI in question michael@0: * @param aFile the associated nsIFile (out param) michael@0: */ michael@0: static NS_HIDDEN_(nsresult) GetLocalFileFromURI(nsIURI *aURI, michael@0: nsIFile **aFile); michael@0: michael@0: // member variables michael@0: nsTArray mExtensions; ///< array of file extensions associated w/ this MIME obj michael@0: nsString mDescription; ///< human readable description michael@0: nsCString mSchemeOrType; michael@0: HandlerClass mClass; michael@0: nsCOMPtr mPreferredApplication; michael@0: nsCOMPtr mPossibleApplications; michael@0: nsHandlerInfoAction mPreferredAction; ///< preferred action to associate with this type michael@0: nsString mPreferredAppDescription; michael@0: nsString mDefaultAppDescription; michael@0: bool mAlwaysAskBeforeHandling; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * This is a complete implementation of nsIMIMEInfo, and contains all necessary michael@0: * methods. However, depending on your platform you may want to use a different michael@0: * way of launching applications. This class stores the default application in a michael@0: * member variable and provides a function for setting it. For local michael@0: * applications, launching is done using nsIProcess, native path of the file to michael@0: * open as first argument. michael@0: */ michael@0: class nsMIMEInfoImpl : public nsMIMEInfoBase { michael@0: public: michael@0: nsMIMEInfoImpl(const char *aMIMEType = "") : nsMIMEInfoBase(aMIMEType) {} michael@0: nsMIMEInfoImpl(const nsACString& aMIMEType) : nsMIMEInfoBase(aMIMEType) {} michael@0: nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass) : michael@0: nsMIMEInfoBase(aType, aClass) {} michael@0: virtual ~nsMIMEInfoImpl() {} michael@0: michael@0: // nsIMIMEInfo methods michael@0: NS_IMETHOD GetHasDefaultHandler(bool *_retval); michael@0: NS_IMETHOD GetDefaultDescription(nsAString& aDefaultDescription); michael@0: michael@0: // additional methods michael@0: /** michael@0: * Sets the default application. Supposed to be only called by the OS Helper michael@0: * App Services; the default application is immutable after it is first set. michael@0: */ michael@0: void SetDefaultApplication(nsIFile* aApp) { if (!mDefaultApplication) mDefaultApplication = aApp; } michael@0: michael@0: protected: michael@0: // nsMIMEInfoBase methods michael@0: /** michael@0: * The base class implementation is to use LaunchWithIProcess in combination michael@0: * with mDefaultApplication. Subclasses can override that behaviour. michael@0: */ michael@0: virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile); michael@0: michael@0: /** michael@0: * Loads the URI with the OS default app. This should be overridden by each michael@0: * OS's implementation. michael@0: */ michael@0: virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0; michael@0: michael@0: nsCOMPtr mDefaultApplication; ///< default application associated with this type. michael@0: }; michael@0: michael@0: #endif //__nsmimeinfoimpl_h___