1 /*******************************************************************************
2  * Copyright (c) 2003, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Ported to the D Programming Language:
11  *     John Reimer <terminal.node@gmail.com>
12  *******************************************************************************/
13 module org.eclipse.swt.browser.HelperAppLauncherDialog_1_9;
14 
15 import java.lang.all;
16 
17 import org.eclipse.swt.SWT;
18 
19 import org.eclipse.swt.internal.mozilla.Common;
20 
21 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
22 
23 import org.eclipse.swt.internal.mozilla.nsEmbedString;
24 import org.eclipse.swt.internal.mozilla.nsID;
25 import org.eclipse.swt.internal.mozilla.nsIHelperAppLauncherDialog_1_9;
26 import org.eclipse.swt.internal.mozilla.nsIHelperAppLauncher_1_9;
27 import org.eclipse.swt.internal.mozilla.nsILocalFile;
28 import org.eclipse.swt.internal.mozilla.nsISupports;
29 import org.eclipse.swt.internal.mozilla.nsStringAPI;
30 import org.eclipse.swt.internal.mozilla.nsEmbedString;
31 
32 import org.eclipse.swt.widgets.FileDialog;
33 import org.eclipse.swt.widgets.Shell;
34 
35 import org.eclipse.swt.browser.Mozilla;
36 
37 class HelperAppLauncherDialog_1_9 : nsIHelperAppLauncherDialog_1_9 {
38 
39     int refCount = 0;
40 
41 this () {
42 }
43 
44 extern(System)
45 nsrefcnt AddRef () {
46     refCount++;
47     return refCount;
48 }
49 
50 extern(System)
51 nsresult QueryInterface (in nsID* riid, void** ppvObject) {
52     if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
53 
54     if (*riid is nsISupports.IID) {
55         *ppvObject = cast(void*)cast(nsIHelperAppLauncherDialog_1_9)this;
56         AddRef ();
57         return XPCOM.NS_OK;
58     }
59     if (*riid is nsIHelperAppLauncherDialog_1_9.IID) {
60         *ppvObject = cast(void*)cast(nsIHelperAppLauncherDialog_1_9)this;
61         AddRef ();
62         return XPCOM.NS_OK;
63     }
64 
65     *ppvObject = null;
66     return XPCOM.NS_ERROR_NO_INTERFACE;
67 }
68 
69 extern(System)          
70 nsresult Release () {
71     refCount--;
72     /*
73     * Note.  This instance lives as long as the download it is bound to.
74     * Its reference count is expected to go down to 0 when the download
75     * has completed or when it has been cancelled. E.g. when the user
76     * cancels the File Dialog, cancels or closes the Download Dialog
77     * and when the Download Dialog goes away after the download is completed.
78     */
79     if (refCount is 0) return 0;
80     return refCount;
81 }
82 
83 /* nsIHelperAppLauncherDialog */
84 extern(System)
85 nsresult Show (nsIHelperAppLauncher_1_9 aLauncher, nsISupports aContext, PRUint32 aReason) {
86     return aLauncher.SaveToDisk (null, 0);
87 }
88 
89 extern(System)
90 nsresult PromptForSaveToFile (nsIHelperAppLauncher_1_9 aLauncher, nsISupports aWindowContext, PRUnichar* aDefaultFileName, PRUnichar* aSuggestedFileExtension, PRBool aForcePrompt, nsILocalFile* _retval) {
91     //int length = XPCOM.strlen_PRUnichar (aDefaultFileName);
92     //char[] dest = new char[length];
93     //XPCOM.memmove (dest, aDefaultFileName, length * 2);
94     String defaultFile = String_valueOf(fromString16z(aDefaultFileName));
95 
96     //length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
97     //dest = new char[length];
98     //XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
99     String suggestedFileExtension = String_valueOf(fromString16z(aSuggestedFileExtension));
100 
101     Shell shell = new Shell ();
102     FileDialog fileDialog = new FileDialog (shell, SWT.SAVE);
103     fileDialog.setFileName (defaultFile);
104     String[] tmp;
105     tmp ~= suggestedFileExtension; 
106     fileDialog.setFilterExtensions (tmp);
107     String name = fileDialog.open ();
108     shell.close ();
109     if (name is null) {
110         //nsIHelperAppLauncher_1_9 launcher = new nsIHelperAppLauncher_1_9 (aLauncher);
111         int rc = aLauncher.Cancel (XPCOM.NS_BINDING_ABORTED);
112         if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__);
113         return XPCOM.NS_ERROR_FAILURE;
114     }
115     scope auto path = new nsEmbedString (name.toWCharArray());
116     
117     nsILocalFile localFile;
118     int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, &localFile);
119     //path.dispose ();
120     if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__);
121     if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER,__FILE__,__LINE__);
122     /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
123     *_retval = localFile; 
124     //XPCOM.memmove (_retval, result, C.PTR_SIZEOF);  
125     return XPCOM.NS_OK;
126 }
127 }
128 
129