Back to index

lightning-sunbird  0.9+nobinonly
nsOSHelperAppService.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK ***** 
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1 
00003  * 
00004  * The contents of this file are subject to the Mozilla Public License Version 
00005  * 1.1 (the "License"); you may not use this file except in compliance with 
00006  * the License. You may obtain a copy of the License at 
00007  * http://www.mozilla.org/MPL/ 
00008  * 
00009  * Software distributed under the License is distributed on an "AS IS" basis, 
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
00011  * for the specific language governing rights and limitations under the 
00012  * License. 
00013  * 
00014  * The Original Code is nsOSHelperAppService.cpp. 
00015  * 
00016  * The Initial Developer of the Original Code is 
00017  * Paul Ashford. 
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved. 
00020  * 
00021  * Contributor(s): 
00022  * 
00023  * Alternatively, the contents of this file may be used under the terms of 
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or 
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 
00026  * in which case the provisions of the GPL or the LGPL are applicable instead 
00027  * of those above. If you wish to allow use of your version of this file only 
00028  * under the terms of either the GPL or the LGPL, and not to allow others to 
00029  * use your version of this file under the terms of the MPL, indicate your 
00030  * decision by deleting the provisions above and replace them with the notice 
00031  * and other provisions required by the GPL or the LGPL. If you do not delete 
00032  * the provisions above, a recipient may use your version of this file under 
00033  * the terms of any one of the MPL, the GPL or the LGPL. 
00034  * 
00035  * ***** END LICENSE BLOCK ***** */ 
00036 
00037 #include "nsOSHelperAppService.h"
00038 #include "nsMIMEInfoBeOS.h"
00039 #include "nsISupports.h"
00040 #include "nsString.h"
00041 #include "nsXPIDLString.h"
00042 #include "nsIURL.h"
00043 #include "nsILocalFile.h"
00044 #include "nsIProcess.h"
00045 #include "prenv.h"      // for PR_GetEnv()
00046 #include <stdlib.h>         // for system()
00047 
00048 #include <Message.h>
00049 #include <Mime.h>
00050 #include <String.h>
00051 #include <Path.h>
00052 #include <Entry.h>
00053 #include <Roster.h>
00054 
00055 #define LOG(args) PR_LOG(mLog, PR_LOG_DEBUG, args)
00056 #define LOG_ENABLED() PR_LOG_TEST(mLog, PR_LOG_DEBUG)
00057 
00058 nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
00059 {
00060 }
00061 
00062 nsOSHelperAppService::~nsOSHelperAppService()
00063 {}
00064 
00065 NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
00066 {
00067        LOG(("-- nsOSHelperAppService::ExternalProtocolHandlerExists for '%s'\n",
00068             aProtocolScheme));
00069        // look up the protocol scheme in the MIME database
00070        *aHandlerExists = PR_FALSE;
00071        if (aProtocolScheme && *aProtocolScheme)
00072        {
00073               BString protoStr(aProtocolScheme);
00074               protoStr.Prepend("application/x-vnd.Be.URL.");
00075               BMimeType protocol;
00076               if (protocol.SetTo(protoStr.String()) == B_OK)
00077               {
00078                      if (protocol.IsInstalled())
00079                             *aHandlerExists = PR_TRUE;
00080               }
00081               if ((!*aHandlerExists) && (!strcmp("mailto", aProtocolScheme)))
00082               {
00083                      // mailto link, no x-vnd.Be.URL.mailto entry
00084                      if (protocol.SetTo("text/x-email") == B_OK)
00085                      {
00086                             if (protocol.IsInstalled())
00087                                    *aHandlerExists = PR_TRUE;
00088                      }
00089               }
00090        }
00091 
00092        return NS_OK;
00093 }
00094 
00095 nsresult nsOSHelperAppService::LoadUriInternal(nsIURI * aURL)
00096 {
00097        LOG(("-- nsOSHelperAppService::LoadUrl\n"));
00098        nsresult rv = NS_OK;
00099 
00100        if (aURL) {
00101               // Get the Protocol
00102               nsCAutoString scheme;
00103               aURL->GetScheme(scheme);
00104               BString protoStr(scheme.get());
00105               protoStr.Prepend("application/x-vnd.Be.URL.");
00106               // Get the Spec
00107               nsCAutoString spec;
00108               aURL->GetSpec(spec);
00109               const char* args[] = { spec.get() };
00110               
00111               //Launch the app            
00112               BMimeType protocol;
00113               bool isInstalled = false;
00114               if (protocol.SetTo(protoStr.String()) == B_OK)
00115               {
00116                      if(protocol.IsInstalled())
00117                      {
00118                             isInstalled = true;  
00119                             be_roster->Launch(protoStr.String(), NS_ARRAY_LENGTH(args), (char **)args);
00120                      }
00121               }
00122               if ((!isInstalled) && (!strcmp("mailto", scheme.get())))
00123                      be_roster->Launch("text/x-email", NS_ARRAY_LENGTH(args), (char **)args);
00124        }
00125        return rv;
00126 }
00127 
00128 
00129 nsresult nsOSHelperAppService::SetMIMEInfoForType(const char *aMIMEType, nsMIMEInfoBeOS**_retval) {
00130 
00131        LOG(("-- nsOSHelperAppService::SetMIMEInfoForType: %s\n",aMIMEType));
00132 
00133        nsresult rv = NS_ERROR_FAILURE;
00134 
00135        nsMIMEInfoBeOS* mimeInfo = new nsMIMEInfoBeOS(aMIMEType);
00136        if (mimeInfo) {
00137               NS_ADDREF(mimeInfo);
00138               BMimeType mimeType(aMIMEType);
00139               BMessage data;
00140               int32 index = 0;
00141               BString strData;
00142               LOG(("   Adding extensions:\n"));
00143               if (mimeType.GetFileExtensions(&data) == B_OK) {
00144                      while (data.FindString("extensions",index,&strData) == B_OK) {
00145                             // if the file extension includes the '.' then we don't want to include that when we append
00146                             // it to the mime info object.
00147                             if (strData.ByteAt(0) == '.')
00148                                    strData.RemoveFirst(".");
00149                             mimeInfo->AppendExtension(nsDependentCString(strData.String()));
00150                             LOG(("      %s\n",strData.String()));
00151                             index++;
00152                      }
00153               }
00154 
00155               char desc[B_MIME_TYPE_LENGTH + 1];
00156               if (mimeType.GetShortDescription(desc) == B_OK) {
00157                      mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc));
00158               } else {
00159                      if (mimeType.GetLongDescription(desc) == B_OK) {
00160                             mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc));
00161                      } else {
00162                             mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(aMIMEType));
00163                      }
00164               }
00165               
00166               LOG(("    Description: %s\n",desc));
00167 
00168               //set preferred app and app description
00169               char appSig[B_MIME_TYPE_LENGTH + 1];
00170               bool doSave = true;
00171               if (mimeType.GetPreferredApp(appSig) == B_OK) {
00172                      LOG(("    Got preferred ap\n"));
00173                      BMimeType app(appSig);
00174                      entry_ref ref;
00175                      BEntry entry;
00176                      BPath path;
00177                      if ((app.GetAppHint(&ref) == B_OK) &&
00178                              (entry.SetTo(&ref, false) == B_OK) &&
00179                              (entry.GetPath(&path) == B_OK)) {
00180 
00181                             LOG(("    Got our path!\n"));
00182                             nsCOMPtr<nsIFile> handlerFile;
00183                             rv = GetFileTokenForPath(NS_ConvertUTF8toUCS2(path.Path()).get(), getter_AddRefs(handlerFile));
00184 
00185                             if (NS_SUCCEEDED(rv)) {
00186                                    mimeInfo->SetDefaultApplication(handlerFile);
00187                                    mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
00188                                    mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUCS2(path.Leaf()));
00189                                    LOG(("    Preferred App: %s\n",path.Leaf()));
00190                                    doSave = false;
00191                             }
00192                      }
00193               }
00194               if (doSave) {
00195                      mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
00196                      LOG(("    No Preferred App\n"));
00197               }
00198 
00199               *_retval = mimeInfo;
00200               rv = NS_OK;
00201        }
00202        else
00203               rv = NS_ERROR_FAILURE;
00204 
00205        return rv;
00206 }
00207 
00208 nsresult nsOSHelperAppService::GetMimeInfoFromExtension(const char *aFileExt,
00209         nsMIMEInfoBeOS ** _retval) {
00210        // if the extension is null, return immediately
00211        if (!aFileExt || !*aFileExt)
00212               return NS_ERROR_INVALID_ARG;
00213 
00214        LOG(("Here we do an extension lookup for '%s'\n", aFileExt));
00215 
00216        BMimeType mimeType, tmpType;
00217 
00218        if( B_OK == mimeType.GuessMimeType(aFileExt, &tmpType))
00219               return SetMIMEInfoForType(tmpType.Type(), _retval);
00220        
00221        // Extension not found
00222        return NS_ERROR_FAILURE;
00223 }
00224 
00225 nsresult nsOSHelperAppService::GetMimeInfoFromMIMEType(const char *aMIMEType,
00226         nsMIMEInfoBeOS ** _retval) {
00227        // if the mime type is null, return immediately
00228        if (!aMIMEType || !*aMIMEType)
00229               return NS_ERROR_INVALID_ARG;
00230 
00231        LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType));
00232        
00233        BMimeType mimeType(aMIMEType);
00234        if (mimeType.IsInstalled())
00235               return SetMIMEInfoForType(aMIMEType, _retval);
00236        
00237        return NS_ERROR_FAILURE;
00238 }
00239 
00240 already_AddRefed<nsIMIMEInfo>
00241 nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType, const nsACString& aFileExt, PRBool* aFound)
00242 {
00243   *aFound = PR_TRUE;
00244   nsMIMEInfoBeOS* mi = nsnull;
00245   const nsCString& flatType = PromiseFlatCString(aMIMEType);
00246   const nsCString& flatExt = PromiseFlatCString(aFileExt);
00247   GetMimeInfoFromMIMEType(flatType.get(), &mi);
00248   if (mi)
00249     return mi;
00250 
00251   GetMimeInfoFromExtension(flatExt.get(), &mi);
00252   if (mi && !aMIMEType.IsEmpty())
00253     mi->SetMIMEType(aMIMEType);
00254   if (mi)
00255     return mi;
00256 
00257   *aFound = PR_FALSE;
00258   mi = new nsMIMEInfoBeOS(flatType);
00259   if (!mi)
00260     return nsnull;
00261   NS_ADDREF(mi);
00262   if (!aFileExt.IsEmpty())
00263     mi->AppendExtension(aFileExt);
00264 
00265   return mi;
00266 }