1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsUnknownContentTypeHandler.h"
40 #include "nsIGenericFactory.h"
41 #include "nsIDOMWindowInternal.h"
42 #include "nsIInterfaceRequestor.h"
43 #include "nsIInterfaceRequestorUtils.h"
44 #include "nsIDocShell.h"
45 #include "mimetype/nsIMIMEInfo.h"
47 #include "nsILocalFile.h"
48 #include "nsIComponentRegistrar.h"
50 #include "EmbedPrivate.h"
51 #include "PtMozilla.h"
53 nsUnknownContentTypeHandler::nsUnknownContentTypeHandler( ) {
55 ///* ATENTIE */ printf( "In nsUnknownContentTypeHandler constructor\n" );
58 nsUnknownContentTypeHandler::~nsUnknownContentTypeHandler( )
60 ///* ATENTIE */ printf( "In nsUnknownContentTypeHandler destr\n" );
64 NS_IMETHODIMP
nsUnknownContentTypeHandler::Show( nsIHelperAppLauncher
*aLauncher
, nsISupports
*aContext
, PRUint32 aReason
)
66 return aLauncher
->SaveToDisk( nsnull
, PR_FALSE
);
69 NS_IMETHODIMP
nsUnknownContentTypeHandler::PromptForSaveToFile( nsIHelperAppLauncher
* aLauncher
,
70 nsISupports
*aWindowContext
,
71 const PRUnichar
*aDefaultFile
,
72 const PRUnichar
*aSuggestedFileExtension
,
74 nsILocalFile
**_retval
)
76 ///* ATENTIE */ printf("PromptForSaveToFile!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
77 NS_ENSURE_ARG_POINTER(_retval
);
80 /* try to get the PtMozillawidget_t* pointer form the aContext - use the fact the the WebBrowserContainer is
81 registering itself as nsIDocumentLoaderObserver ( SetDocLoaderObserver ) */
82 nsCOMPtr
<nsIDOMWindow
> domw( do_GetInterface( aWindowContext
) );
84 domw
->GetParent( &parent
);
85 PtWidget_t
*w
= GetWebBrowser( parent
);
86 PtMozillaWidget_t
*moz
= ( PtMozillaWidget_t
* ) w
;
88 /* get the suggested filename */
89 NS_ConvertUTF16toUTF8
theUnicodeString( aDefaultFile
);
90 const char *filename
= theUnicodeString
.get( );
93 nsCOMPtr
<nsIURI
> aSourceUrl
;
94 aLauncher
->GetSource( getter_AddRefs(aSourceUrl
) );
96 nsCAutoString specString
;
97 aSourceUrl
->GetSpec(specString
);
98 url
= specString
.get();
100 /* get the mime type */
101 nsCOMPtr
<nsIMIMEInfo
> mimeInfo
;
102 aLauncher
->GetMIMEInfo( getter_AddRefs(mimeInfo
) );
103 nsCAutoString mimeType
;
104 mimeInfo
->GetMIMEType( mimeType
);
106 PtCallbackInfo_t cbinfo
;
107 PtWebUnknownWithNameCallback_t cb
;
109 memset( &cbinfo
, 0, sizeof( cbinfo
) );
110 cbinfo
.reason
= Pt_CB_MOZ_UNKNOWN
;
112 cb
.action
= Pt_WEB_ACTION_OK
;
113 cb
.content_type
= (char*)mimeType
.get();
114 cb
.url
= (char *)url
;
115 cb
.content_length
= strlen( cb
.url
);
116 cb
.suggested_filename
= (char*)filename
;
117 PtInvokeCallbackList( moz
->web_unknown_cb
, (PtWidget_t
*)moz
, &cbinfo
);
118 /* this will modal wait for a Pt_ARG_WEB_UNKNOWN_RESP, in mozserver */
120 /* we have the result in moz->moz_unknown_ctrl */
121 if( moz
->moz_unknown_ctrl
->response
!= Pt_WEB_RESPONSE_OK
) return NS_ERROR_ABORT
;
123 /* the user chosen filename is moz->moz_unknown_ctrl->filename */
124 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance("@mozilla.org/file/local;1"));
125 NS_ENSURE_TRUE(file
, NS_ERROR_FAILURE
);
127 nsCString
s ( moz
->moz_unknown_ctrl
->filename
);
128 file
->InitWithNativePath( s
);
129 if( !file
) return NS_ERROR_FAILURE
;
132 NS_ADDREF( *_retval
);
134 /* add this download to our list */
135 EmbedDownload
*download
= new EmbedDownload( moz
, moz
->moz_unknown_ctrl
->download_ticket
, url
);
136 download
->mLauncher
= aLauncher
;
137 aLauncher
->SetWebProgressListener( download
);
143 PtWidget_t
*nsUnknownContentTypeHandler::GetWebBrowser(nsIDOMWindow
*aWindow
)
145 nsCOMPtr
<nsIWebBrowserChrome
> chrome
;
148 nsCOMPtr
<nsIWindowWatcher
> wwatch(do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
149 if (!wwatch
) return nsnull
;
152 nsCOMPtr
<nsIDOMWindow
> fosterParent
;
153 if (!aWindow
) { // it will be a dependent window. try to find a foster parent.
154 wwatch
->GetActiveWindow(getter_AddRefs(fosterParent
));
155 aWindow
= fosterParent
;
157 wwatch
->GetChromeForWindow(aWindow
, getter_AddRefs(chrome
));
161 nsCOMPtr
<nsIEmbeddingSiteWindow
> site(do_QueryInterface(chrome
));
163 site
->GetSiteWindow(reinterpret_cast<void **>(&val
));
170 //#######################################################################################
172 #define className nsUnknownContentTypeHandler
173 #define interfaceName nsIHelperAppLauncherDialog
174 #define contractId NS_IUNKNOWNCONTENTTYPEHANDLER_CONTRACTID
176 /* Component's implementation of Initialize. */
177 /* nsISupports Implementation for the class */
178 NS_IMPL_ADDREF( className
);
179 NS_IMPL_RELEASE( className
)
182 /* QueryInterface implementation for this class. */
183 NS_IMETHODIMP
className::QueryInterface( REFNSIID anIID
, void **anInstancePtr
) {
185 ///* ATENTIE */ printf("QueryInterface!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
187 /* Check for place to return result. */
188 if( !anInstancePtr
) rv
= NS_ERROR_NULL_POINTER
;
191 if ( anIID
.Equals( NS_GET_IID(nsIHelperAppLauncherDialog
) ) ) {
192 *anInstancePtr
= (void*) (nsIHelperAppLauncherDialog
*)this;
195 else if ( anIID
.Equals( NS_GET_IID(nsISupports
) ) ) {
196 *anInstancePtr
= (void*) ( (nsISupports
*) (interfaceName
*)this );
199 else rv
= NS_NOINTERFACE
;
204 NS_GENERIC_FACTORY_CONSTRUCTOR( nsUnknownContentTypeHandler
)
206 // The list of components we register
207 static nsModuleComponentInfo info
[] = {
209 "nsUnknownContentTypeHandler",
210 NS_IHELPERAPPLAUNCHERDIALOG_IID
,
211 NS_IHELPERAPPLAUNCHERDLG_CONTRACTID
,
212 nsUnknownContentTypeHandlerConstructor
216 int Init_nsUnknownContentTypeHandler_Factory( ) {
219 // create a generic factory for UnkHandler
220 nsCOMPtr
<nsIGenericFactory
> factory
;
221 rv
= NS_NewGenericFactory( getter_AddRefs(factory
), info
);
225 // register this factory with the component registrar.
226 nsCOMPtr
<nsIComponentRegistrar
> registrar
;
227 rv
= NS_GetComponentRegistrar(getter_AddRefs(registrar
));
230 rv
= registrar
->RegisterFactory( kCID
, "nsUnknownContentTypeHandler", NS_IHELPERAPPLAUNCHERDLG_CONTRACTID
, factory
);