Back to index

lightning-sunbird  0.9+nobinonly
nsMIMEInfoImpl.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=4 sw=4 sts=4 et: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMIMEInfoImpl.h"
00040 #include "nsXPIDLString.h"
00041 #include "nsReadableUtils.h"
00042 #include "nsStringEnumerator.h"
00043 #include "nsIProcess.h"
00044 
00045 // nsISupports methods
00046 NS_IMPL_THREADSAFE_ISUPPORTS1(nsMIMEInfoBase, nsIMIMEInfo)
00047 
00048 // nsMIMEInfoImpl methods
00049 nsMIMEInfoBase::nsMIMEInfoBase(const char *aMIMEType) :
00050     mMacType(0),
00051     mMacCreator(0),
00052     mMIMEType(aMIMEType),
00053     mPreferredAction(nsIMIMEInfo::saveToDisk),
00054     mAlwaysAskBeforeHandling(PR_TRUE)
00055 {
00056 }
00057 
00058 nsMIMEInfoBase::nsMIMEInfoBase(const nsACString& aMIMEType) :
00059     mMacType(0),
00060     mMacCreator(0),
00061     mMIMEType(aMIMEType),
00062     mPreferredAction(nsIMIMEInfo::saveToDisk),
00063     mAlwaysAskBeforeHandling(PR_TRUE)
00064 {
00065 }
00066 
00067 nsMIMEInfoBase::~nsMIMEInfoBase()
00068 {
00069 }
00070 
00071 NS_IMETHODIMP
00072 nsMIMEInfoBase::GetFileExtensions(nsIUTF8StringEnumerator** aResult)
00073 {
00074   return NS_NewUTF8StringEnumerator(aResult, &mExtensions, this);
00075 }
00076 
00077 NS_IMETHODIMP
00078 nsMIMEInfoBase::ExtensionExists(const nsACString& aExtension, PRBool *_retval)
00079 {
00080     NS_ASSERTION(!aExtension.IsEmpty(), "no extension");
00081     PRBool found = PR_FALSE;
00082     PRUint32 extCount = mExtensions.Count();
00083     if (extCount < 1) return NS_OK;
00084 
00085     for (PRUint8 i=0; i < extCount; i++) {
00086         nsCString* ext = (nsCString*)mExtensions.CStringAt(i);
00087         if (ext->Equals(aExtension, nsCaseInsensitiveCStringComparator())) {
00088             found = PR_TRUE;
00089             break;
00090         }
00091     }
00092 
00093     *_retval = found;
00094     return NS_OK;
00095 }
00096 
00097 NS_IMETHODIMP
00098 nsMIMEInfoBase::GetPrimaryExtension(nsACString& _retval)
00099 {
00100     PRUint32 extCount = mExtensions.Count();
00101     if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
00102 
00103     _retval = *(mExtensions.CStringAt(0));
00104     return NS_OK;    
00105 }
00106 
00107 NS_IMETHODIMP
00108 nsMIMEInfoBase::SetPrimaryExtension(const nsACString& aExtension)
00109 {
00110   NS_ASSERTION(!aExtension.IsEmpty(), "no extension");
00111   PRUint32 extCount = mExtensions.Count();
00112   PRUint8 i;
00113   PRBool found = PR_FALSE;
00114   for (i=0; i < extCount; i++) {
00115     nsCString* ext = (nsCString*)mExtensions.CStringAt(i);
00116     if (ext->Equals(aExtension, nsCaseInsensitiveCStringComparator())) {
00117       found = PR_TRUE;
00118       break;
00119     }
00120   }
00121   if (found) {
00122     mExtensions.RemoveCStringAt(i);
00123   }
00124 
00125   mExtensions.InsertCStringAt(aExtension, 0);
00126   
00127   return NS_OK;
00128 }
00129 
00130 NS_IMETHODIMP
00131 nsMIMEInfoBase::AppendExtension(const nsACString& aExtension)
00132 {
00133   mExtensions.AppendCString(aExtension);
00134   return NS_OK;
00135 }
00136 
00137 NS_IMETHODIMP
00138 nsMIMEInfoBase::GetMIMEType(nsACString& aMIMEType)
00139 {
00140     if (mMIMEType.IsEmpty())
00141         return NS_ERROR_NOT_INITIALIZED;
00142 
00143     aMIMEType = mMIMEType;
00144     return NS_OK;
00145 }
00146 
00147 NS_IMETHODIMP
00148 nsMIMEInfoBase::GetDescription(nsAString& aDescription)
00149 {
00150     aDescription = mDescription;
00151     return NS_OK;
00152 }
00153 
00154 NS_IMETHODIMP
00155 nsMIMEInfoBase::SetDescription(const nsAString& aDescription) 
00156 {
00157     mDescription = aDescription;
00158     return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP
00162 nsMIMEInfoBase::Equals(nsIMIMEInfo *aMIMEInfo, PRBool *_retval)
00163 {
00164     if (!aMIMEInfo) return NS_ERROR_NULL_POINTER;
00165 
00166     nsCAutoString type;
00167     nsresult rv = aMIMEInfo->GetMIMEType(type);
00168     if (NS_FAILED(rv)) return rv;
00169 
00170     *_retval = mMIMEType.Equals(type);
00171 
00172     return NS_OK;
00173 }
00174 
00175 NS_IMETHODIMP
00176 nsMIMEInfoBase::GetMacType(PRUint32 *aMacType)
00177 {
00178     *aMacType = mMacType;
00179 
00180     if (!mMacType)
00181         return NS_ERROR_NOT_INITIALIZED;
00182 
00183     return NS_OK;
00184 }
00185 
00186 NS_IMETHODIMP
00187 nsMIMEInfoBase::SetMacType(PRUint32 aMacType)
00188 {
00189     mMacType = aMacType;
00190     return NS_OK;
00191 }
00192 
00193 NS_IMETHODIMP
00194 nsMIMEInfoBase::GetMacCreator(PRUint32 *aMacCreator)
00195 {
00196     *aMacCreator = mMacCreator;
00197 
00198     if (!mMacCreator)
00199         return NS_ERROR_NOT_INITIALIZED;
00200 
00201     return NS_OK;
00202 }
00203 
00204 NS_IMETHODIMP
00205 nsMIMEInfoBase::SetMacCreator(PRUint32 aMacCreator)
00206 {
00207     mMacCreator = aMacCreator;
00208     return NS_OK;
00209 }
00210 
00211 NS_IMETHODIMP
00212 nsMIMEInfoBase::SetFileExtensions(const nsACString& aExtensions)
00213 {
00214     mExtensions.Clear();
00215     nsCString extList( aExtensions );
00216     
00217     PRInt32 breakLocation = -1;
00218     while ( (breakLocation= extList.FindChar(',') )!= -1)
00219     {
00220         mExtensions.AppendCString(Substring(extList.get(), extList.get() + breakLocation));
00221         extList.Cut(0, breakLocation+1 );
00222     }
00223     if ( !extList.IsEmpty() )
00224         mExtensions.AppendCString( extList );
00225     return NS_OK;
00226 }
00227 
00228 NS_IMETHODIMP
00229 nsMIMEInfoBase::GetApplicationDescription(nsAString& aApplicationDescription)
00230 {
00231   if (mPreferredAppDescription.IsEmpty() && mPreferredApplication) {
00232     // Don't want to cache this, just in case someone resets the app
00233     // without changing the description....
00234     mPreferredApplication->GetLeafName(aApplicationDescription);
00235   } else {
00236     aApplicationDescription = mPreferredAppDescription;
00237   }
00238   
00239   return NS_OK;
00240 }
00241  
00242 NS_IMETHODIMP
00243 nsMIMEInfoBase::SetApplicationDescription(const nsAString& aApplicationDescription)
00244 {
00245   mPreferredAppDescription = aApplicationDescription;
00246   return NS_OK;
00247 }
00248 
00249 NS_IMETHODIMP
00250 nsMIMEInfoBase::GetDefaultDescription(nsAString& aDefaultDescription)
00251 {
00252   aDefaultDescription = mDefaultAppDescription;
00253   return NS_OK;
00254 }
00255 
00256 NS_IMETHODIMP
00257 nsMIMEInfoBase::GetPreferredApplicationHandler(nsIFile ** aPreferredAppHandler)
00258 {
00259   *aPreferredAppHandler = mPreferredApplication;
00260   NS_IF_ADDREF(*aPreferredAppHandler);
00261   return NS_OK;
00262 }
00263  
00264 NS_IMETHODIMP
00265 nsMIMEInfoBase::SetPreferredApplicationHandler(nsIFile * aPreferredAppHandler)
00266 {
00267   mPreferredApplication = aPreferredAppHandler;
00268   return NS_OK;
00269 }
00270 
00271 NS_IMETHODIMP
00272 nsMIMEInfoBase::GetPreferredAction(nsMIMEInfoHandleAction * aPreferredAction)
00273 {
00274   *aPreferredAction = mPreferredAction;
00275   return NS_OK;
00276 }
00277  
00278 NS_IMETHODIMP
00279 nsMIMEInfoBase::SetPreferredAction(nsMIMEInfoHandleAction aPreferredAction)
00280 {
00281   mPreferredAction = aPreferredAction;
00282   return NS_OK;
00283 }
00284 
00285 NS_IMETHODIMP
00286 nsMIMEInfoBase::GetAlwaysAskBeforeHandling(PRBool * aAlwaysAsk)
00287 {
00288   *aAlwaysAsk = mAlwaysAskBeforeHandling;
00289 
00290   return NS_OK;
00291 }
00292 
00293 NS_IMETHODIMP
00294 nsMIMEInfoBase::SetAlwaysAskBeforeHandling(PRBool aAlwaysAsk)
00295 {
00296   mAlwaysAskBeforeHandling = aAlwaysAsk;
00297   return NS_OK;
00298 }
00299 
00300 NS_IMETHODIMP
00301 nsMIMEInfoBase::LaunchWithFile(nsIFile* aFile)
00302 {
00303   if (mPreferredAction == useHelperApp) {
00304     if (!mPreferredApplication)
00305       return NS_ERROR_FILE_NOT_FOUND;
00306 
00307     return LaunchWithIProcess(mPreferredApplication, aFile);
00308   }
00309   else if (mPreferredAction == useSystemDefault) {
00310     return LaunchDefaultWithFile(aFile);
00311   }
00312 
00313   return NS_ERROR_INVALID_ARG;
00314 }
00315 
00316 void
00317 nsMIMEInfoBase::CopyBasicDataTo(nsMIMEInfoBase* aOther)
00318 {
00319   aOther->mMIMEType = mMIMEType;
00320   aOther->mDefaultAppDescription = mDefaultAppDescription;
00321   aOther->mExtensions = mExtensions;
00322 
00323   aOther->mMacType = mMacType;
00324   aOther->mMacCreator = mMacCreator;
00325 }
00326 
00327 /* static */
00328 nsresult
00329 nsMIMEInfoBase::LaunchWithIProcess(nsIFile* aApp, nsIFile* aFile)
00330 {
00331   NS_ASSERTION(aApp && aFile, "Unexpected null pointer, fix caller");
00332 
00333   nsresult rv;
00334   nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
00335   if (NS_FAILED(rv))
00336     return rv;
00337 
00338   if (NS_FAILED(rv = process->Init(aApp)))
00339     return rv;
00340 
00341   nsCAutoString path;
00342   aFile->GetNativePath(path);
00343 
00344   const char * strPath = path.get();
00345 
00346   PRUint32 pid;
00347   return process->Run(PR_FALSE, &strPath, 1, &pid);
00348 }
00349 
00350 // nsMIMEInfoImpl implementation
00351 NS_IMETHODIMP
00352 nsMIMEInfoImpl::GetDefaultDescription(nsAString& aDefaultDescription)
00353 {
00354   if (mDefaultAppDescription.IsEmpty() && mDefaultApplication) {
00355     // Don't want to cache this, just in case someone resets the app
00356     // without changing the description....
00357     mDefaultApplication->GetLeafName(aDefaultDescription);
00358   } else {
00359     aDefaultDescription = mDefaultAppDescription;
00360   }
00361   
00362   return NS_OK;
00363 }
00364 
00365 NS_IMETHODIMP
00366 nsMIMEInfoImpl::GetHasDefaultHandler(PRBool * _retval)
00367 {
00368   *_retval = PR_FALSE;
00369   if (mDefaultApplication) {
00370     PRBool exists;
00371     *_retval = NS_SUCCEEDED(mDefaultApplication->Exists(&exists)) && exists;
00372   }
00373   return NS_OK;
00374 }
00375 
00376 nsresult
00377 nsMIMEInfoImpl::LaunchDefaultWithFile(nsIFile* aFile)
00378 {
00379   if (!mDefaultApplication)
00380     return NS_ERROR_FILE_NOT_FOUND;
00381 
00382   return LaunchWithIProcess(mDefaultApplication, aFile);
00383 }
00384