~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/embedding/browser/photon/src/nsUnknownContentTypeHandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
25
 *
 
26
 * Alternatively, the contents of this file may be used under the terms of
 
27
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
28
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
29
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
30
 * of those above. If you wish to allow use of your version of this file only
 
31
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
32
 * use your version of this file under the terms of the NPL, indicate your
 
33
 * decision by deleting the provisions above and replace them with the notice
 
34
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
35
 * the provisions above, a recipient may use your version of this file under
 
36
 * the terms of any one of the NPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "nsUnknownContentTypeHandler.h"
 
41
#include "nsIGenericFactory.h"
 
42
#include "nsIDOMWindowInternal.h"
 
43
#include "nsIInterfaceRequestor.h"
 
44
#include "nsIInterfaceRequestorUtils.h"
 
45
#include "nsIDocShell.h"
 
46
#include "mimetype/nsIMIMEInfo.h"
 
47
#include "nsIURI.h"
 
48
#include "nsILocalFile.h"
 
49
 
 
50
#include "EmbedPrivate.h"
 
51
#include "PtMozilla.h"
 
52
 
 
53
nsUnknownContentTypeHandler::nsUnknownContentTypeHandler( ) {
 
54
        NS_INIT_ISUPPORTS();
 
55
///* ATENTIE */ printf( "In nsUnknownContentTypeHandler constructor\n" );
 
56
        }
 
57
 
 
58
nsUnknownContentTypeHandler::~nsUnknownContentTypeHandler( )
 
59
{
 
60
///* ATENTIE */ printf( "In nsUnknownContentTypeHandler destr\n" );
 
61
}
 
62
 
 
63
 
 
64
NS_IMETHODIMP nsUnknownContentTypeHandler::Show( nsIHelperAppLauncher *aLauncher, nsISupports *aContext, PRBool aForced )
 
65
{
 
66
        return aLauncher->SaveToDisk( nsnull, PR_FALSE );
 
67
}
 
68
 
 
69
NS_IMETHODIMP nsUnknownContentTypeHandler::PromptForSaveToFile( nsIHelperAppLauncher* aLauncher,
 
70
                                                                nsISupports *aWindowContext,
 
71
                                                                const PRUnichar *aDefaultFile,
 
72
                                                                const PRUnichar *aSuggestedFileExtension,
 
73
                                                                nsILocalFile **_retval )
 
74
{
 
75
///* ATENTIE */ printf("PromptForSaveToFile!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
 
76
        NS_ENSURE_ARG_POINTER(_retval);
 
77
        *_retval = nsnull;
 
78
 
 
79
        /* try to get the PtMozillawidget_t* pointer form the aContext - use the fact the the WebBrowserContainer is
 
80
                registering itself as nsIDocumentLoaderObserver ( SetDocLoaderObserver ) */
 
81
        nsCOMPtr<nsIDOMWindow> domw( do_GetInterface( aWindowContext ) );
 
82
        nsIDOMWindow *parent;
 
83
        domw->GetParent( &parent );
 
84
        PtWidget_t *w = GetWebBrowser( parent );
 
85
        PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) w;
 
86
 
 
87
        /* get the suggested filename */
 
88
        NS_ConvertUCS2toUTF8 theUnicodeString( aDefaultFile );
 
89
        const char *filename = theUnicodeString.get( );
 
90
 
 
91
        /* get the url */
 
92
        nsCOMPtr<nsIURI> aSourceUrl;
 
93
        aLauncher->GetSource( getter_AddRefs(aSourceUrl) );
 
94
        const char *url;
 
95
        nsCAutoString specString;
 
96
        aSourceUrl->GetSpec(specString);
 
97
        url = specString.get();
 
98
 
 
99
        /* get the mime type */
 
100
        nsCOMPtr<nsIMIMEInfo> mimeInfo;
 
101
        aLauncher->GetMIMEInfo( getter_AddRefs(mimeInfo) );
 
102
        char *mimeType;
 
103
        mimeInfo->GetMIMEType( &mimeType );
 
104
 
 
105
 
 
106
        PtCallbackInfo_t cbinfo;
 
107
        PtWebUnknownWithNameCallback_t cb;
 
108
 
 
109
        memset( &cbinfo, 0, sizeof( cbinfo ) );
 
110
        cbinfo.reason = Pt_CB_MOZ_UNKNOWN;
 
111
        cbinfo.cbdata = &cb;
 
112
        cb.action = Pt_WEB_ACTION_OK;
 
113
        cb.content_type = mimeType;
 
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 */
 
119
 
 
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;
 
122
 
 
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);
 
126
 
 
127
        nsCString s ( moz->moz_unknown_ctrl->filename );
 
128
        file->InitWithNativePath( s );
 
129
        if( !file ) return NS_ERROR_FAILURE;
 
130
 
 
131
        *_retval = file;
 
132
        NS_ADDREF( *_retval );
 
133
 
 
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 );
 
138
 
 
139
        return NS_OK;
 
140
}
 
141
 
 
142
 
 
143
PtWidget_t *nsUnknownContentTypeHandler::GetWebBrowser(nsIDOMWindow *aWindow)
 
144
{
 
145
  nsCOMPtr<nsIWebBrowserChrome> chrome;
 
146
  PtWidget_t *val = 0;
 
147
 
 
148
  nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
 
149
  if (!wwatch) return nsnull;
 
150
 
 
151
  if( wwatch ) {
 
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;
 
156
    }
 
157
    wwatch->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
 
158
  }
 
159
 
 
160
  if (chrome) {
 
161
    nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
 
162
    if (site) {
 
163
      site->GetSiteWindow(reinterpret_cast<void **>(&val));
 
164
    }
 
165
  }
 
166
 
 
167
  return val;
 
168
}
 
169
 
 
170
//#######################################################################################
 
171
 
 
172
#define className             nsUnknownContentTypeHandler
 
173
#define interfaceName         nsIHelperAppLauncherDialog
 
174
#define contractId            NS_IUNKNOWNCONTENTTYPEHANDLER_CONTRACTID
 
175
 
 
176
/* Component's implementation of Initialize. */
 
177
/* nsISupports Implementation for the class */
 
178
NS_IMPL_ADDREF( className );  
 
179
NS_IMPL_RELEASE( className )
 
180
 
 
181
 
 
182
/* QueryInterface implementation for this class. */
 
183
NS_IMETHODIMP className::QueryInterface( REFNSIID anIID, void **anInstancePtr ) { 
 
184
        nsresult rv = NS_OK; 
 
185
///* ATENTIE */ printf("QueryInterface!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
 
186
 
 
187
        /* Check for place to return result. */
 
188
        if( !anInstancePtr ) rv = NS_ERROR_NULL_POINTER;
 
189
        else { 
 
190
                *anInstancePtr = 0; 
 
191
                if ( anIID.Equals( NS_GET_IID(nsIHelperAppLauncherDialog) ) ) { 
 
192
                        *anInstancePtr = (void*) (nsIHelperAppLauncherDialog*)this; 
 
193
                        NS_ADDREF_THIS();
 
194
                        }
 
195
                else if ( anIID.Equals( NS_GET_IID(nsISupports) ) ) { 
 
196
                        *anInstancePtr = (void*) ( (nsISupports*) (interfaceName*)this ); 
 
197
                        NS_ADDREF_THIS();
 
198
                        }
 
199
                else rv = NS_NOINTERFACE;
 
200
                } 
 
201
        return rv; 
 
202
        }
 
203
 
 
204
NS_GENERIC_FACTORY_CONSTRUCTOR( nsUnknownContentTypeHandler )
 
205
 
 
206
// The list of components we register
 
207
static nsModuleComponentInfo info[] = {
 
208
                {
 
209
                        "nsUnknownContentTypeHandler",
 
210
                        NS_IHELPERAPPLAUNCHERDIALOG_IID,
 
211
                        NS_IHELPERAPPLAUNCHERDLG_CONTRACTID,
 
212
                        nsUnknownContentTypeHandlerConstructor
 
213
                }
 
214
        };
 
215
 
 
216
int Init_nsUnknownContentTypeHandler_Factory( ) {
 
217
        nsresult rv;
 
218
 
 
219
        // create a generic factory for UnkHandler
 
220
        nsCOMPtr<nsIGenericFactory> factory;
 
221
        rv = NS_NewGenericFactory( getter_AddRefs(factory), info );
 
222
        if (rv != NS_OK)
 
223
                return rv;
 
224
 
 
225
        // register this factory with the component manager.
 
226
        rv = nsComponentManager::RegisterFactory( kCID, "nsUnknownContentTypeHandler", NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, factory, PR_TRUE);
 
227
        return NS_OK;
 
228
        }