Back to index

lightning-sunbird  0.9+nobinonly
EmbedDownload.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  *   Adrian Mardare <amardare@qnx.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 "EmbedDownload.h"
00040 
00041 EmbedDownload::EmbedDownload( PtMozillaWidget_t *aMoz, int aDownloadTicket, const char * aURL )
00042 {
00043        mMozillaWidget = aMoz;
00044        mDownloadTicket = aDownloadTicket;
00045        mURL = strdup( aURL );
00046        mDone = PR_FALSE;
00047        mLauncher = nsnull;
00048        mPersist = nsnull;
00049 
00050        /* insert d into aMoz->fDownload */
00051        aMoz->fDownload = ( EmbedDownload ** ) realloc( aMoz->fDownload, ( aMoz->fDownloadCount + 1 ) * sizeof( EmbedDownload * ) );
00052        if( aMoz->fDownload ) {
00053               aMoz->fDownload[ aMoz->fDownloadCount ] = this;
00054               aMoz->fDownloadCount++;
00055               }
00056 }
00057 
00058 EmbedDownload::~EmbedDownload()
00059 {
00060        int i;
00061 
00063 
00064   /* remove d from the mMoz->fDownload */
00065   for( i=0; i<mMozillaWidget->fDownloadCount; i++ ) {
00066     if( mMozillaWidget->fDownload[i] == this ) break;
00067     }
00068 
00069   if( i<mMozillaWidget->fDownloadCount ) {
00070     int j;
00071 
00072     for( j=i; j<mMozillaWidget->fDownloadCount-1; j++ )
00073       mMozillaWidget->fDownload[j] = mMozillaWidget->fDownload[j+1];
00074 
00075     mMozillaWidget->fDownloadCount--;
00076     if( !mMozillaWidget->fDownloadCount ) {
00077       free( mMozillaWidget->fDownload );
00078       mMozillaWidget->fDownload = NULL;
00079       }
00080 
00081               if( mDone == PR_FALSE ) ReportDownload( Pt_WEB_DOWNLOAD_CANCEL, 0, 0, "" );
00082     }
00083 
00085 
00086        free( mURL );
00087 }
00088 
00089 EmbedDownload *FindDownload( PtMozillaWidget_t *moz, int download_ticket )
00090 {
00091        int i;
00092   for( i=0; i<moz->fDownloadCount; i++ ) {
00093     if( moz->fDownload[i]->mDownloadTicket == download_ticket ) return moz->fDownload[i];
00094     }
00095        return NULL;
00096 }
00097 
00098 
00099 void EmbedDownload::ReportDownload( int type, int current, int total, char *message )
00100 {
00101   PtCallbackInfo_t cbinfo;
00102   PtWebDownloadCallback_t cb;
00103 
00104   memset( &cbinfo, 0, sizeof( cbinfo ) );
00105   cbinfo.reason = Pt_CB_MOZ_DOWNLOAD;
00106   cbinfo.cbdata = &cb;
00107 
00108   cb.download_ticket = mDownloadTicket;
00109   cb.type = type;
00110   cb.url = mURL;
00111   cb.current = current;
00112   cb.total = total;
00113   cb.message = message;
00114 
00115   if( type == Pt_WEB_DOWNLOAD_DONE || type == Pt_WEB_DOWNLOAD_ERROR)
00116        mDone = PR_TRUE;
00117 
00118   PtInvokeCallbackList( mMozillaWidget->web_download_cb, (PtWidget_t *)mMozillaWidget, &cbinfo );
00119 }
00120 
00121 
00122 /* nsIWebProgressListener interface */
00123 NS_IMPL_ISUPPORTS2(EmbedDownload, nsIWebProgressListener, nsIWebProgressListener2)
00124 
00125 NS_IMETHODIMP EmbedDownload::OnProgressChange(nsIWebProgress *aProgress, nsIRequest *aRequest, PRInt32 curSelfProgress, PRInt32 maxSelfProgress, PRInt32 curTotalProgress, PRInt32 maxTotalProgress) {
00126 
00128 //this, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress );
00129 
00130        ReportDownload( Pt_WEB_DOWNLOAD_PROGRESS, curSelfProgress, maxSelfProgress, "" );
00131 
00132        return NS_OK;
00133        }
00134 
00135 NS_IMETHODIMP EmbedDownload::OnProgressChange64(nsIWebProgress *aProgress, nsIRequest *aRequest, PRInt64 curSelfProgress, PRInt64 maxSelfProgress, PRInt64 curTotalProgress, PRInt64 maxTotalProgress) {
00136   // XXX truncates 64-bit to 32-bit
00137   return OnProgressChange(aProgress, aRequest,
00138                           PRInt32(curSelfProgress), PRInt32(maxSelfProgress),
00139                           PRInt32(curTotalProgress), PRInt32(maxTotalProgress));
00140   }
00141 
00142 NS_IMETHODIMP EmbedDownload::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus) {
00143        if( aStateFlags & STATE_STOP ) {
00144               ReportDownload( Pt_WEB_DOWNLOAD_DONE, 0, 0, "" );
00145               }
00146        return NS_OK;
00147        }
00148 
00149 NS_IMETHODIMP EmbedDownload::OnLocationChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, nsIURI *location) {
00150        return NS_OK;
00151        }
00152 NS_IMETHODIMP EmbedDownload::OnStatusChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, nsresult aStatus, const PRUnichar* aMessage) {
00153        nsString                    u_message(aMessage);
00154        char *message = ToNewCString(u_message);
00155        //printf("EmbedDownload::OnStatusChange: aRequest 0x%x, message %s\n", aRequest, message);
00156        if (aRequest)
00157               ReportDownload( Pt_WEB_DOWNLOAD_ERROR, 0, 0, message );
00158        if( message ) nsMemory::Free( (void*)message );
00159        return NS_OK;
00160        }
00161 NS_IMETHODIMP EmbedDownload::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state) {
00162        return NS_OK;
00163        }