Back to index

lightning-sunbird  0.9+nobinonly
nsMIMEInfoOS2.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
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 the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Scott MacGregor <mscott@netscape.com>
00025  *   Boris Zbarsky <bzbarsky@mit.edu>  (Added mailcap and mime.types support)
00026  *   Christian Biesinger <cbiesinger@web.de>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either of the GNU General Public License Version 2 or later (the "GPL"),
00030  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 
00043 #define INCL_DOS
00044 #include <os2.h>
00045 
00046 #include "nsMIMEInfoOS2.h"
00047 #include "nsExternalHelperAppService.h"
00048 #include "nsCExternalHandlerService.h"
00049 #include "nsReadableUtils.h"
00050 #include "nsIProcess.h"
00051 #include <stdlib.h>
00052 
00053 #define SALT_SIZE 8
00054 #define TABLE_SIZE 36
00055 static const PRUnichar table[] = 
00056   { 'a','b','c','d','e','f','g','h','i','j',
00057     'k','l','m','n','o','p','q','r','s','t',
00058     'u','v','w','x','y','z','0','1','2','3',
00059     '4','5','6','7','8','9'};
00060 
00061 
00062 nsMIMEInfoOS2::~nsMIMEInfoOS2()
00063 {
00064 }
00065 
00066 NS_IMETHODIMP nsMIMEInfoOS2::LaunchWithFile(nsIFile* aFile)
00067 {
00068   nsresult rv = NS_OK;
00069 
00070   nsCAutoString path;
00071   aFile->GetNativePath(path);
00072 
00073   nsIFile* application;
00074   if (mPreferredAction == useHelperApp) {
00075     application = mPreferredApplication;
00076   } else if (mPreferredAction == useSystemDefault) {
00077     application = mDefaultApplication;
00078   } else {
00079     return NS_ERROR_INVALID_ARG;
00080   }
00081 
00082   // The nsIMIMEInfo should have either the default or preferred 
00083   // application handler attribute set to match the preferredAction!
00084   if (!application) {
00085     HOBJECT hobject = WinQueryObject(path.get());
00086     if (WinSetObjectData( hobject, "OPEN=DEFAULT" ))
00087       return NS_OK;
00088     else
00089       return NS_ERROR_FAILURE;
00090   }
00091   
00092   ULONG ulAppType;
00093   nsCAutoString apppath;
00094   application->GetNativePath(apppath);
00095   DosQueryAppType(apppath.get(), &ulAppType);
00096   if (ulAppType & (FAPPTYP_DOS |
00097                    FAPPTYP_WINDOWSPROT31 |
00098                    FAPPTYP_WINDOWSPROT |
00099                    FAPPTYP_WINDOWSREAL)) {
00100     // if the helper application is a DOS app, create an 8.3 filename
00101     // we do this even if the filename is valid because it's 8.3, who cares
00102     nsCOMPtr<nsPIExternalAppLauncher> helperAppService (do_GetService(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID));
00103     if (helperAppService)
00104     {
00105       nsCAutoString leafName; 
00106       aFile->GetNativeLeafName(leafName);
00107       const char* lastDot = strrchr(leafName.get(), '.');
00108       char suffix[CCHMAXPATH + 1] = "";
00109       if (lastDot)
00110       {
00111           strcpy(suffix, lastDot);
00112       }
00113       suffix[4] = '\0';
00114       
00115       nsAutoString saltedTempLeafName;
00116       do {
00117           saltedTempLeafName.Truncate();
00118           // this salting code was ripped directly from the profile manager.
00119           // turn PR_Now() into milliseconds since epoch 1058 // and salt rand with that. 
00120           double fpTime;
00121           LL_L2D(fpTime, PR_Now());
00122           srand((uint)(fpTime * 1e-6 + 0.5));
00123           PRInt32 i;
00124           for (i=0;i<SALT_SIZE;i++) {
00125             saltedTempLeafName.Append(table[(rand()%TABLE_SIZE)]);
00126           }
00127           AppendASCIItoUTF16(suffix, saltedTempLeafName);
00128           nsresult rv = aFile->MoveTo(nsnull, saltedTempLeafName);
00129       } while (NS_FAILED(rv));
00130       helperAppService->DeleteTemporaryFileOnExit(aFile);
00131       aFile->GetNativePath(path);
00132     }
00133   } else {
00134     path.Insert('\"', 0);
00135     path.Append('\"');
00136   }
00137     
00138   const char * strPath = path.get();
00139   // if we were given an application to use then use it....otherwise
00140   // make the registry call to launch the app
00141   nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID);
00142   if (NS_FAILED(rv = process->Init(application)))
00143     return rv;
00144   PRUint32 pid;
00145   return process->Run(PR_FALSE, &strPath, 1, &pid);
00146 }
00147