Back to index

lightning-sunbird  0.9+nobinonly
nsUnknownContentTypeHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 "nsUnknownContentTypeHandler.h"
00040 #include "nsIGenericFactory.h"
00041 #include "nsIDOMWindowInternal.h"
00042 #include "nsIInterfaceRequestor.h"
00043 #include "nsIInterfaceRequestorUtils.h"
00044 #include "nsIDocShell.h"
00045 #include "mimetype/nsIMIMEInfo.h"
00046 #include "nsIURI.h"
00047 #include "nsILocalFile.h"
00048 #include "nsIComponentRegistrar.h"
00049 
00050 #include "EmbedPrivate.h"
00051 #include "PtMozilla.h"
00052 
00053 nsUnknownContentTypeHandler::nsUnknownContentTypeHandler( ) {
00054        NS_INIT_ISUPPORTS();
00056        }
00057 
00058 nsUnknownContentTypeHandler::~nsUnknownContentTypeHandler( )
00059 {
00061 }
00062 
00063 
00064 NS_IMETHODIMP nsUnknownContentTypeHandler::Show( nsIHelperAppLauncher *aLauncher, nsISupports *aContext, PRUint32 aReason )
00065 {
00066        return aLauncher->SaveToDisk( nsnull, PR_FALSE );
00067 }
00068 
00069 NS_IMETHODIMP nsUnknownContentTypeHandler::PromptForSaveToFile( nsIHelperAppLauncher* aLauncher,
00070                                                                 nsISupports *aWindowContext,
00071                                                                 const PRUnichar *aDefaultFile,
00072                                                                 const PRUnichar *aSuggestedFileExtension,
00073                                                                 nsILocalFile **_retval )
00074 {
00076        NS_ENSURE_ARG_POINTER(_retval);
00077        *_retval = nsnull;
00078 
00079        /* try to get the PtMozillawidget_t* pointer form the aContext - use the fact the the WebBrowserContainer is
00080               registering itself as nsIDocumentLoaderObserver ( SetDocLoaderObserver ) */
00081        nsCOMPtr<nsIDOMWindow> domw( do_GetInterface( aWindowContext ) );
00082        nsIDOMWindow *parent;
00083        domw->GetParent( &parent );
00084        PtWidget_t *w = GetWebBrowser( parent );
00085        PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) w;
00086 
00087        /* get the suggested filename */
00088        NS_ConvertUCS2toUTF8 theUnicodeString( aDefaultFile );
00089        const char *filename = theUnicodeString.get( );
00090 
00091        /* get the url */
00092        nsCOMPtr<nsIURI> aSourceUrl;
00093        aLauncher->GetSource( getter_AddRefs(aSourceUrl) );
00094        const char *url;
00095        nsCAutoString specString;
00096        aSourceUrl->GetSpec(specString);
00097        url = specString.get();
00098 
00099        /* get the mime type */
00100        nsCOMPtr<nsIMIMEInfo> mimeInfo;
00101        aLauncher->GetMIMEInfo( getter_AddRefs(mimeInfo) );
00102        nsCAutoString mimeType;
00103        mimeInfo->GetMIMEType( mimeType );
00104 
00105        PtCallbackInfo_t cbinfo;
00106        PtWebUnknownWithNameCallback_t cb;
00107 
00108        memset( &cbinfo, 0, sizeof( cbinfo ) );
00109        cbinfo.reason = Pt_CB_MOZ_UNKNOWN;
00110        cbinfo.cbdata = &cb;
00111        cb.action = Pt_WEB_ACTION_OK;
00112        cb.content_type = (char*)mimeType.get();
00113        cb.url = (char *)url;
00114        cb.content_length = strlen( cb.url );
00115        cb.suggested_filename = (char*)filename;
00116        PtInvokeCallbackList( moz->web_unknown_cb, (PtWidget_t *)moz, &cbinfo );
00117        /* this will modal wait for a Pt_ARG_WEB_UNKNOWN_RESP, in mozserver */
00118 
00119        /* we have the result in moz->moz_unknown_ctrl */
00120        if( moz->moz_unknown_ctrl->response != Pt_WEB_RESPONSE_OK ) return NS_ERROR_ABORT;
00121 
00122        /* the user chosen filename is moz->moz_unknown_ctrl->filename */
00123        nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
00124        NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
00125 
00126        nsCString s ( moz->moz_unknown_ctrl->filename );
00127        file->InitWithNativePath( s );
00128        if( !file ) return NS_ERROR_FAILURE;
00129 
00130        *_retval = file;
00131        NS_ADDREF( *_retval );
00132 
00133        /* add this download to our list */
00134        EmbedDownload *download = new EmbedDownload( moz, moz->moz_unknown_ctrl->download_ticket, url );
00135        download->mLauncher = aLauncher;
00136        aLauncher->SetWebProgressListener( download );
00137 
00138        return NS_OK;
00139 }
00140 
00141 
00142 PtWidget_t *nsUnknownContentTypeHandler::GetWebBrowser(nsIDOMWindow *aWindow)
00143 {
00144   nsCOMPtr<nsIWebBrowserChrome> chrome;
00145   PtWidget_t *val = 0;
00146 
00147   nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
00148   if (!wwatch) return nsnull;
00149 
00150   if( wwatch ) {
00151     nsCOMPtr<nsIDOMWindow> fosterParent;
00152     if (!aWindow) { // it will be a dependent window. try to find a foster parent.
00153       wwatch->GetActiveWindow(getter_AddRefs(fosterParent));
00154       aWindow = fosterParent;
00155     }
00156     wwatch->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
00157   }
00158 
00159   if (chrome) {
00160     nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
00161     if (site) {
00162       site->GetSiteWindow(reinterpret_cast<void **>(&val));
00163     }
00164   }
00165 
00166   return val;
00167 }
00168 
00169 //#######################################################################################
00170 
00171 #define className             nsUnknownContentTypeHandler
00172 #define interfaceName         nsIHelperAppLauncherDialog
00173 #define contractId            NS_IUNKNOWNCONTENTTYPEHANDLER_CONTRACTID
00174 
00175 /* Component's implementation of Initialize. */
00176 /* nsISupports Implementation for the class */
00177 NS_IMPL_ADDREF( className );  
00178 NS_IMPL_RELEASE( className )
00179 
00180 
00181 /* QueryInterface implementation for this class. */
00182 NS_IMETHODIMP className::QueryInterface( REFNSIID anIID, void **anInstancePtr ) { 
00183        nsresult rv = NS_OK; 
00185 
00186        /* Check for place to return result. */
00187        if( !anInstancePtr ) rv = NS_ERROR_NULL_POINTER;
00188        else { 
00189               *anInstancePtr = 0; 
00190               if ( anIID.Equals( NS_GET_IID(nsIHelperAppLauncherDialog) ) ) { 
00191                      *anInstancePtr = (void*) (nsIHelperAppLauncherDialog*)this; 
00192                      NS_ADDREF_THIS();
00193                      }
00194               else if ( anIID.Equals( NS_GET_IID(nsISupports) ) ) { 
00195                      *anInstancePtr = (void*) ( (nsISupports*) (interfaceName*)this ); 
00196                      NS_ADDREF_THIS();
00197                      }
00198               else rv = NS_NOINTERFACE;
00199               } 
00200        return rv; 
00201        }
00202 
00203 NS_GENERIC_FACTORY_CONSTRUCTOR( nsUnknownContentTypeHandler )
00204 
00205 // The list of components we register
00206 static nsModuleComponentInfo info[] = {
00207               {
00208                      "nsUnknownContentTypeHandler",
00209                      NS_IHELPERAPPLAUNCHERDIALOG_IID,
00210                      NS_IHELPERAPPLAUNCHERDLG_CONTRACTID,
00211                      nsUnknownContentTypeHandlerConstructor
00212               }
00213        };
00214 
00215 int Init_nsUnknownContentTypeHandler_Factory( ) {
00216        nsresult rv;
00217 
00218        // create a generic factory for UnkHandler
00219        nsCOMPtr<nsIGenericFactory> factory;
00220        rv = NS_NewGenericFactory( getter_AddRefs(factory), info );
00221        if (NS_FAILED(rv))
00222               return rv;
00223 
00224        // register this factory with the component registrar.
00225        nsCOMPtr<nsIComponentRegistrar> registrar;
00226        rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
00227        if (NS_FAILED(rv))
00228               return rv;
00229        rv = registrar->RegisterFactory( kCID, "nsUnknownContentTypeHandler", NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, factory);
00230        return NS_OK;
00231        }