aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/shellapi.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/shellapi.js')
-rw-r--r--js/io/system/shellapi.js806
1 files changed, 806 insertions, 0 deletions
diff --git a/js/io/system/shellapi.js b/js/io/system/shellapi.js
new file mode 100644
index 00000000..9976dbed
--- /dev/null
+++ b/js/io/system/shellapi.js
@@ -0,0 +1,806 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/* /////////////////////////////////////////////////////////////////////
8////////////////////////////////////////////////////////////////////////
9NOTES: All logic should be handled in the FileSystem and I/O classes
10
11Dialog methods on NativeShellApp
12ShowFileOpenDialog(initialDir) - shows a file open dialog
13initialDir is optional and if specified will cause the dialog to initially display that directory as the open location
14ShowFileSaveAsDialog(initialURI) - shows a file Save As dialog
15initialURI is optional and if specified will cause the dialog to initially display the directory as the default location
16and the filename as the current filename.
17ShowSelectDirectoryDialog(initialDir, dialogTitle) - displays a directory select/chooser dialog
18intitalDir is optional and specifies the directory that should be selected/shown when the dialog opens
19dialogTitle is optional and specifies the title that should appear in the dialog caption
20////////////////////////////////////////////////////////////////////////
21///////////////////////////////////////////////////////////////////// */
22//Exporting as Project I/O
23exports.ShellApi = (require("montage/core/core").Montage).create(require("montage/ui/component").Component, {
24 ////////////////////////////////////////////////////////////////////
25 //
26 init: {
27 enumerable: false,
28 value: function() {
29 try {
30 var xhr = new XMLHttpRequest(), file, directory;
31 //
32 xhr.open("GET", 'cloud/config.xml', false);
33 xhr.send();
34 //
35 if (xhr.readyState === 4) {
36 file = xhr.responseXML.getElementsByTagName('file')[0].firstChild.nodeValue;
37 directory = xhr.responseXML.getElementsByTagName('directory')[0].firstChild.nodeValue;
38 if (file.length)
39 this._fileServiceURL = file;
40 if (directory.length)
41 this._directoryServiceURL = directory;
42 //
43 //console.log(file, directory);
44 }
45 }
46 catch(error) {
47 console.log(error);
48 }
49 }
50 },
51 ////////////////////////////////////////////////////////////////////
52 //
53 openShellDialog: {
54 enumerable: false,
55 value: function(dialog) {
56 //Initializing return variable
57 var input = null;
58 //Checking for the type of prompt set via object
59 switch (dialog.type) {
60 case 'file':
61 //Checking for action the prompt will ask the user
62 if (dialog.action.toLowerCase() == 'open') {
63 //File open dialog
64 input = window.NativeShellApp.ShowFileOpenDialog();
65 } else if (dialog.action.toLowerCase() == 'new') {
66 //File new dialog
67 input = window.NativeShellApp.ShowFileSaveAsDialog();
68 }
69 break;
70 case 'directory':
71 //Checking for action the prompt will ask the user
72 if (dialog.action.toLowerCase() == 'open') {
73 //Directory open dialog
74 input = window.NativeShellApp.ShowSelectDirectoryDialog();
75 } else if (dialog.action.toLowerCase() == 'new') {
76 //Directory new dialog
77 input = window.NativeShellApp.ShowSelectDirectoryDialog();
78 }
79 break;
80 break;
81 default:
82 break;
83 }
84 return input;
85 }
86 },
87 ////////////////////////////////////////////////////////////////////
88 //
89 startServer: {
90 enumerable: false,
91 value: function (dir) {
92 var server = window.NativeShellApp.StartWebServer(dir);
93 return server;
94 }
95 },
96 ////////////////////////////////////////////////////////////////////
97 // private property containing the file service URL to use for all file IO calls
98 _fileServiceURL: {
99 enumerable: false,
100 value: "http://localhost:16380/file" //default value.. updated with base uri in config.xml
101 },
102 ////////////////////////////////////////////////////////////////////
103 // private property containing the directory service URL to use for all file IO calls
104 _directoryServiceURL: {
105 enumerable: false,
106 value: "http://localhost:16380/directory" //default value.. updated with base uri in config.xml
107 },
108 ////////////////////////////////////////////////////////////////////
109 // private helper to parse URIs and append them to the service URL
110 _prepareServiceURL: {
111 enumerable: false,
112 value: function(serviceURL, path) {
113 var urlOut = path.replace(/\\/g,"/");
114 urlOut = urlOut.replace(/:/g,"");
115 urlOut = encodeURI(urlOut);
116 //add leading / if not already there
117 if((urlOut.length > 0) && (urlOut.charAt(0) !== "/")){
118 urlOut = "/" + urlOut;
119 }
120 return serviceURL + urlOut;
121 }
122 },
123 ////////////////////////////////////////////////////////////////////
124 // Checks for the existence of a file
125 // Parameters:
126 // the file parameter must contain the following properties
127 // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html"
128 //
129 // Return values:
130 // returns an object with two properties
131 // success: boolean indicating if the call succeeded or failed
132 // status: int indicating the request HTTP status code
133 // 204 - the file exists
134 // 404 - the file does not exist
135 // 500 - unknown server error occurred
136 fileExists: {
137 enumerable: false,
138 value: function(file) {
139 //
140 var retValue = { success:null, status:null };
141 //
142 if(file && file.uri && file.uri.length) {
143 try {
144 var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri),
145 xhr = new XMLHttpRequest();
146 //
147 xhr.open("GET", serviceURL, false);
148 xhr.setRequestHeader("check-existence-only", "true");
149 xhr.send();
150 //
151 if (xhr.readyState === 4) {
152 retValue.status = xhr.status;
153 retValue.success = true;
154 }
155 }
156 catch(error) {
157 xhr = null;
158 retValue.success = false;
159 }
160 }
161 //
162 return retValue;
163 }
164 },
165 ////////////////////////////////////////////////////////////////////
166 // Creates a new file at the specified path
167 // Parameters:
168 // the file parameter must contain the following properties
169 // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html"
170 // it can optionally contain the following properties
171 // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified
172 // contents: string containing the file contents. These contents will be saved to the new file.
173 //
174 // Return values:
175 // returns an object with two properties
176 // success: boolean indicating if the call succeeded or failed
177 // status: int indicating the request HTTP status code
178 // 201 - the file was created and contents were saved if they were passed
179 // 400 - the file already exists and could not be created
180 // 500 - unknown server error occurred
181 createFile: {
182 enumerable: false,
183 value: function(file) {
184 var retValue = { success:null, status:null };
185 if(file && file.uri && file.uri.length) {
186 try {
187 var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri),
188 xhr = new XMLHttpRequest();
189 //
190 xhr.open("POST", serviceURL, false);
191 if(file.contentType && file.contentType.length)
192 xhr.setRequestHeader("Content-Type", file.contentType);
193 else
194 xhr.setRequestHeader("Content-Type", "text/plain");
195
196 if(file.contents && file.contents.length)
197 xhr.send(file.contents);
198 else
199 xhr.send();
200
201 if (xhr.readyState === 4) {
202 retValue.status = xhr.status;
203 retValue.success = true;
204 }
205 }
206 catch(error) {
207 xhr = null;
208 retValue.success = false;
209 }
210 }
211
212 return retValue;
213 }
214 },
215 ////////////////////////////////////////////////////////////////////
216 // Save contents into an existing file at the specified path
217 // Parameters:
218 // the file parameter must contain the following properties
219 // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html"
220 // it can optionally contain the following properties
221 // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified
222 // contents: string containing the file contents. These contents will be saved to the new file.
223 //
224 // Return values:
225 // returns an object with two properties
226 // success: boolean indicating if the call succeeded or failed
227 // status: int indicating the request HTTP status code
228 // 204 - the file was saved
229 // 404 - the file specified does not exist
230 // 500 - unknown server error occurred
231 updateFile: {
232 enumer