comparison dwt/browser/HelperAppLauncherDialog_1_9.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents
children 5583f8eeee6c
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
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 *******************************************************************************/
11 module dwt.browser.HelperAppLauncherDialog_1_9;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.internal.C;
17 import dwt.internal.mozilla.XPCOM;
18 import dwt.internal.mozilla.XPCOMObject;
19 import dwt.internal.mozilla.nsEmbedString;
20 import dwt.internal.mozilla.nsID;
21 import dwt.internal.mozilla.nsIHelperAppLauncherDialog_1_9;
22 import dwt.internal.mozilla.nsIHelperAppLauncher_1_9;
23 import dwt.internal.mozilla.nsISupports;
24 import dwt.widgets.FileDialog;
25 import dwt.widgets.Shell;
26
27 /**
28 * This class implements the nsIHelperAppLauncherDialog interface for mozilla
29 * versions >= 1.9. For mozilla versions 1.4 - 1.8.x this interface is
30 * implemented by class HelperAppLauncherDialog. HelperAppLauncherDialogFactory
31 * determines at runtime which of these classes to instantiate.
32 */
33 class HelperAppLauncherDialog_1_9 {
34 XPCOMObject supports;
35 XPCOMObject helperAppLauncherDialog;
36 int refCount = 0;
37
38 HelperAppLauncherDialog_1_9 () {
39 createCOMInterfaces ();
40 }
41
42 int AddRef () {
43 refCount++;
44 return refCount;
45 }
46
47 void createCOMInterfaces () {
48 /* Create each of the interfaces that this object implements */
49 supports = new XPCOMObject (new int[] {2, 0, 0}) {
50 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
51 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
52 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
53 };
54
55 helperAppLauncherDialog = new XPCOMObject (new int[] {2, 0, 0, 3, 6}) {
56 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
57 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
58 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
59 public int /*long*/ method3 (int /*long*/[] args) {return Show (args[0], args[1], (int)/*64*/args[2]);}
60 public int /*long*/ method4 (int /*long*/[] args) {return PromptForSaveToFile (args[0], args[1], args[2], args[3], (int)/*64*/args[4], args[5]);}
61 };
62 }
63
64 void disposeCOMInterfaces () {
65 if (supports !is null) {
66 supports.dispose ();
67 supports = null;
68 }
69 if (helperAppLauncherDialog !is null) {
70 helperAppLauncherDialog.dispose ();
71 helperAppLauncherDialog = null;
72 }
73 }
74
75 int /*long*/ getAddress () {
76 return helperAppLauncherDialog.getAddress ();
77 }
78
79 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
80 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
81 nsID guid = new nsID ();
82 XPCOM.memmove (guid, riid, nsID.sizeof);
83
84 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
85 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
86 AddRef ();
87 return XPCOM.NS_OK;
88 }
89 if (guid.Equals (nsIHelperAppLauncherDialog_1_9.NS_IHELPERAPPLAUNCHERDIALOG_IID)) {
90 XPCOM.memmove (ppvObject, new int /*long*/[] {helperAppLauncherDialog.getAddress ()}, C.PTR_SIZEOF);
91 AddRef ();
92 return XPCOM.NS_OK;
93 }
94
95 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
96 return XPCOM.NS_ERROR_NO_INTERFACE;
97 }
98
99 int Release () {
100 refCount--;
101 /*
102 * Note. This instance lives as long as the download it is bound to.
103 * Its reference count is expected to go down to 0 when the download
104 * has completed or when it has been cancelled. E.g. when the user
105 * cancels the File Dialog, cancels or closes the Download Dialog
106 * and when the Download Dialog goes away after the download is completed.
107 */
108 if (refCount is 0) disposeCOMInterfaces ();
109 return refCount;
110 }
111
112 /* nsIHelperAppLauncherDialog */
113
114 int Show (int /*long*/ aLauncher, int /*long*/ aContext, int aReason) {
115 nsIHelperAppLauncher_1_9 helperAppLauncher = new nsIHelperAppLauncher_1_9 (aLauncher);
116 return helperAppLauncher.SaveToDisk (0, 0);
117 }
118
119 int PromptForSaveToFile (int /*long*/ aLauncher, int /*long*/ aWindowContext, int /*long*/ aDefaultFileName, int /*long*/ aSuggestedFileExtension, int aForcePrompt, int /*long*/ _retval) {
120 int length = XPCOM.strlen_PRUnichar (aDefaultFileName);
121 char[] dest = new char[length];
122 XPCOM.memmove (dest, aDefaultFileName, length * 2);
123 String defaultFile = new String (dest);
124
125 length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
126 dest = new char[length];
127 XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
128 String suggestedFileExtension = new String (dest);
129
130 Shell shell = new Shell ();
131 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE);
132 fileDialog.setFileName (defaultFile);
133 fileDialog.setFilterExtensions (new String[] {suggestedFileExtension});
134 String name = fileDialog.open ();
135 shell.close ();
136 if (name is null) {
137 nsIHelperAppLauncher_1_9 launcher = new nsIHelperAppLauncher_1_9 (aLauncher);
138 int rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
139 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
140 return XPCOM.NS_ERROR_FAILURE;
141 }
142 nsEmbedString path = new nsEmbedString (name);
143 int /*long*/[] result = new int /*long*/[1];
144 int rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, result);
145 path.dispose ();
146 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
147 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
148 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
149 XPCOM.memmove (_retval, result, C.PTR_SIZEOF);
150 return XPCOM.NS_OK;
151 }
152 }