view dwt/browser/Download.d @ 286:44258e0b6687

More fixes for xpcom
author John Reimer<terminal.node@gmail.com>
date Tue, 05 Aug 2008 10:11:58 -0700
parents 93409d9838c5
children eec6ddb07873
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2003, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *      John Reimer <terminal.node@gmail.com>
 *******************************************************************************/
module dwt.browser.Download;

import dwt.dwthelper.utils;

import dwt.DWT;

import dwt.internal.Compatibility;
import dwt.internal.mozilla.XPCOM;
import dwt.internal.mozilla.XPCOMObject;
import dwt.internal.mozilla.nsID;
import dwt.internal.mozilla.nsIDownload;
import dwt.internal.mozilla.nsIHelperAppLauncher;
import dwt.internal.mozilla.nsILocalFile;
import dwt.internal.mozilla.nsIProgressDialog;
import dwt.internal.mozilla.nsISupports;
import dwt.internal.mozilla.nsIURI;
import dwt.internal.mozilla.nsIWebProgressListener;
import dwt.layout.GridData;
import dwt.layout.GridLayout;
import dwt.widgets.Button;
import dwt.widgets.Event;
import dwt.widgets.Label;
import dwt.widgets.Listener;
import dwt.widgets.Shell;

class Download : nsIProgressDialog {
    nsIHelperAppLauncher helperAppLauncher;
    int refCount = 0;

    Shell shell;
    Label status;
    Button cancel;
    
this () {
}

int AddRef () {
    refCount++;
    return refCount;
}

int QueryInterface (nsID* riid, void** ppvObject) {
    if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
 
    if (*riid == nsISupports.IID)) {
        *ppvObject = cast(void*)cast(nsISupports)this;
        AddRef();
        return XPCOM.NS_OK;
    }
    if (*riid == nsIDownload.IID) {
        *ppvObject = cast(void*)cast(nsIDownload)this;
        AddRef();
        return XPCOM.NS_OK;
    }
    if (*riid == nsIProgressDialog.IID) {
        *ppvObject = cast(void*)cast(nsIProgressDialog)this;
        AddRef();
        return XPCOM.NS_OK;
    }
    if (*riid == nsIWebProgressListener.IID) {
        *ppvObject = cast(void*)cast(nsIWebProgressListener)this;
        AddRef();
        return XPCOM.NS_OK;
    }
    *ppvObject = null;
    return XPCOM.NS_ERROR_NO_INTERFACE;
}
            
int Release () {
    refCount--;
    if (refCount is 0) return 0;
    return refCount;
}

/* nsIDownload */

/* Note. The argument startTime is defined as a PRInt64. This translates into two java ints. */
int Init (nsIURI aSource, nsIURI aTarget, nsAString* aDisplayName, nsIMIMEInfo aMIMEInfo, PRTime startTime, PRBool aPersist) {
    nsIURI source = new nsIURI (aSource);
    scope auto aSpec = new nsEmbedString;
    int rc = source.GetHost (cast(nsAString*)aSpec);
    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
    int length = XPCOM.nsEmbedCString_Length (aSpec);
    int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
    byte[] dest = new byte[length];
    XPCOM.memmove (dest, buffer, length);
    XPCOM.nsEmbedCString_delete (aSpec);
    String url = new String (dest);

    /*
    * As of mozilla 1.7 the second argument of the nsIDownload interface's 
    * Init function changed from nsILocalFile to nsIURI.  Detect which of
    * these interfaces the second argument implements and act accordingly.  
    */
    String filename = null;
    nsISupports supports = new nsISupports (aTarget);
    int /*long*/[] result = new int /*long*/[1];
    rc = supports.QueryInterface (nsIURI.NS_IURI_IID, result);
    if (rc is 0) {  /* >= 1.7 */
        nsIURI target = new nsIURI (result[0]);
        result[0] = 0;
        int /*long*/ aPath = XPCOM.nsEmbedCString_new ();
        rc = target.GetPath (aPath);
        if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
        length = XPCOM.nsEmbedCString_Length (aPath);
        buffer = XPCOM.nsEmbedCString_get (aPath);
        dest = new byte[length];
        XPCOM.memmove (dest, buffer, length);
        XPCOM.nsEmbedCString_delete (aPath);
        filename = new String (dest);
        int separator = filename.lastIndexOf (System.getProperty ("file.separator"));   //$NON-NLS-1$
        filename = filename.substring (separator + 1);
        target.Release ();
    } else {    /* < 1.7 */
        nsILocalFile target = new nsILocalFile (aTarget);
        int /*long*/ aNativeTarget = XPCOM.nsEmbedCString_new ();
        rc = target.GetNativeLeafName (aNativeTarget);
        if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
        length = XPCOM.nsEmbedCString_Length (aNativeTarget);
        buffer = XPCOM.nsEmbedCString_get (aNativeTarget);
        dest = new byte[length];
        XPCOM.memmove (dest, buffer, length);
        XPCOM.nsEmbedCString_delete (aNativeTarget);
        filename = new String (dest);
    }

    Listener listener = new Listener () {
        public void handleEvent (Event event) {
            if (event.widget is cancel) {
                shell.close ();
            }
            if (helperAppLauncher !is null) {
                helperAppLauncher.Cancel ();
                helperAppLauncher.Release ();
            }
            shell = null;
            helperAppLauncher = null;
        }
    };
    shell = new Shell (DWT.DIALOG_TRIM);
    String msg = Compatibility.getMessage ("SWT_Download_File", new Object[] {filename}); //$NON-NLS-1$
    shell.setText (msg);
    GridLayout gridLayout = new GridLayout ();
    gridLayout.marginHeight = 15;
    gridLayout.marginWidth = 15;
    gridLayout.verticalSpacing = 20;
    shell.setLayout(gridLayout);
    msg = Compatibility.getMessage ("SWT_Download_Location", new Object[] {filename, url}); //$NON-NLS-1$
    new Label (shell, DWT.SIMPLE).setText (msg);
    status = new Label (shell, DWT.SIMPLE);
    msg = Compatibility.getMessage ("SWT_Download_Started"); //$NON-NLS-1$
    status.setText (msg);
    GridData data = new GridData ();
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;
    status.setLayoutData (data);

    cancel = new Button (shell, DWT.PUSH);
    cancel.setText (DWT.getMessage ("SWT_Cancel")); //$NON-NLS-1$
    data = new GridData ();
    data.horizontalAlignment = GridData.CENTER;
    cancel.setLayoutData (data);
    cancel.addListener (DWT.Selection, listener);
    shell.addListener (DWT.Close, listener);
    shell.pack ();
    shell.open ();
    return XPCOM.NS_OK;
}

int GetSource (int /*long*/ aSource) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetTarget (int /*long*/ aTarget) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetPersist (PRBool* aPersist) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetPercentComplete (int /*long*/ aPercentComplete) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetDisplayName (int /*long*/ aDisplayName) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int SetDisplayName (int /*long*/ aDisplayName) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetStartTime (int /*long*/ aStartTime) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetMIMEInfo (int /*long*/ aMIMEInfo) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetListener (int /*long*/ aListener) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int SetListener (int /*long*/ aListener) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

nsresult GetObserver (nsIObserver* aObserver) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

nsresult SetObserver (nsIOBserver aObserver) {
    if (aObserver !is 0) {
        nsISupports supports = new nsISupports (aObserver);
        int /*long*/[] result = new int /*long*/[1];
        int rc = supports.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result);
        if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
        if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
        helperAppLauncher = new nsIHelperAppLauncher (result[0]);
    }
    return XPCOM.NS_OK;
}

/* nsIProgressDialog */
nsresult Open (int /*long*/ aParent) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int SetCancelDownloadOnClose (int aCancelDownloadOnClose) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int GetDialog (int /*long*/ aDialog) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

int SetDialog (int /*long*/ aDialog) {
    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
}

/* nsIWebProgressListener */

int OnStateChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aStateFlags, int aStatus) {
    if ((aStateFlags & nsIWebProgressListener.STATE_STOP) !is 0) {
        if (helperAppLauncher !is null) helperAppLauncher.Release ();
        helperAppLauncher = null;
        if (shell !is null && !shell.isDisposed ()) shell.dispose ();
        shell = null;
    }
    return XPCOM.NS_OK;
}

int OnProgressChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress) {
    int currentKBytes = aCurTotalProgress / 1024;
    int totalKBytes = aMaxTotalProgress / 1024;
    if (shell !is null && !shell.isDisposed ()) {
        Object[] arguments = {new Integer (currentKBytes), new Integer (totalKBytes)};
        String statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); //$NON-NLS-1$
        status.setText (statusMsg);
        shell.layout (true);
        shell.getDisplay ().update ();
    }
    return XPCOM.NS_OK;
}

int OnLocationChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aLocation) {
    return XPCOM.NS_OK;
}

int OnStatusChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aStatus, int /*long*/ aMessage) {
    return XPCOM.NS_OK;
}

int OnSecurityChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int state) {
    return XPCOM.NS_OK;
}
}