git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / doc / webdeveloper / common / application.js
1 // Constructs an application object
2 function WebDeveloperApplication(applicationPath)
3 {
4 this.applicationPath = applicationPath;
5 this.executable = this.getExecutable();
6 this.file = null;
7 }
8
9 // Creates a source file
10 WebDeveloperApplication.prototype.createSourceFile = function(temporaryDirectory, uri)
11 {
12 var sourceFile = null;
13
14 // If the URI has a file scheme
15 if(uri.scheme == "file")
16 {
17 var fileProtocolHandler = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler);
18
19 sourceFile = fileProtocolHandler.getFileFromURLSpec(uri.spec);
20 }
21
22 // If the source file is not set
23 if(!sourceFile)
24 {
25 var fileExtension = "html";
26 var fileName = uri.host;
27 var url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
28
29 sourceFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
30 url.spec = uri.spec;
31
32 // If the URL has a file extension
33 if(url.fileExtension)
34 {
35 fileExtension = url.fileExtension;
36 }
37
38 temporaryDirectory.append("webdeveloper_" + fileName + "_" + new Date().getTime() + "." + fileExtension);
39 sourceFile.initWithPath(temporaryDirectory.path);
40 }
41
42 return sourceFile;
43 }
44
45 // Returns an executable for the application
46 WebDeveloperApplication.prototype.getExecutable = function()
47 {
48 var executable = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
49
50 executable.initWithPath(this.applicationPath);
51
52 return executable;
53 }
54
55 // Returns the post data
56 WebDeveloperApplication.prototype.getPostData = function()
57 {
58 // Try to get the post data
59 try
60 {
61 var sessionHistory = getWebNavigation().sessionHistory;
62 var entry = sessionHistory.getEntryAtIndex(sessionHistory.index, false).QueryInterface(Components.interfaces.nsISHEntry);
63
64 return entry.postData;
65 }
66 catch(exception)
67 {
68 return null;
69 }
70 }
71
72 // Launch the application with the given file
73 WebDeveloperApplication.prototype.launchWithFile = function()
74 {
75 var mimeService = Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"].getService(Components.interfaces.nsIMIMEService);
76 var mimeInfo = mimeService.getFromTypeAndExtension(this.file.path, null);
77
78 mimeInfo.alwaysAskBeforeHandling = false;
79 mimeInfo.preferredAction = Components.interfaces.nsIMIMEInfo.useHelperApp;
80 mimeInfo.preferredApplicationHandler = this.executable;
81
82 mimeInfo.launchWithFile(this.file);
83 }
84
85 // Launch the application with the source from the given URI
86 WebDeveloperApplication.prototype.launchWithSource = function(uri)
87 {
88 var stringBundle = document.getElementById("webdeveloper-string-bundle");
89 var temporaryDirectory = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("TmpD", Components.interfaces.nsIFile);
90
91 // If the temporary directory exists, is a directory and is writable
92 if(temporaryDirectory.exists() && temporaryDirectory.isDirectory() && temporaryDirectory.isWritable())
93 {
94 // If the executable exists
95 if(this.executable.exists())
96 {
97 this.file = this.createSourceFile(temporaryDirectory, uri);
98
99 if(uri.scheme == "file")
100 {
101 this.launchWithFile();
102 }
103 else
104 {
105 var webBrowserPersistInterface = Components.interfaces.nsIWebBrowserPersist;
106 var webBrowserPersist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(webBrowserPersistInterface);
107
108 webBrowserPersist.persistFlags = webBrowserPersistInterface.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION | webBrowserPersistInterface.PERSIST_FLAGS_FROM_CACHE | webBrowserPersistInterface.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
109 webBrowserPersist.progressListener = this;
110
111 webBrowserPersist.saveURI(uri, null, uri, this.getPostData(), null, this.file);
112 }
113 }
114 else
115 {
116 webdeveloper_error(stringBundle.getFormattedString("webdeveloper_launchApplicationFailed", [this.applicationPath]));
117 }
118 }
119 else
120 {
121 webdeveloper_error(stringBundle.getFormattedString("webdeveloper_tempDirectoryFailed", [temporaryDirectory.path]));
122 }
123 }
124
125 // Launch the application with the given URL
126 WebDeveloperApplication.prototype.launchWithURL = function(url)
127 {
128 // If the executable exists, is a file and is executable
129 if(this.executable.exists() && this.executable.isFile() && this.executable.isExecutable())
130 {
131 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
132 var processArguments = new Array(url);
133
134 process.init(this.executable);
135 process.run(false, processArguments, processArguments.length);
136 }
137 else
138 {
139 webdeveloper_error(document.getElementById("webdeveloper-string-bundle").getFormattedString("webdeveloper_launchApplicationFailed", [this.applicationPath]));
140 }
141 }
142
143 // Called when the progress state changes
144 WebDeveloperApplication.prototype.onStateChange = function(webProgress, request, stateFlags, status)
145 {
146 // If the progress has stopped
147 if(stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
148 {
149 this.launchWithFile();
150 }
151 }
152
153 // Indicates the interfaces this object supports
154 WebDeveloperApplication.prototype.QueryInterface = function(id)
155 {
156 // If the query is for a supported interface
157 if(id.equals(Components.interfaces.nsISupports) || id.equals(Components.interfaces.nsIWebProgressListener))
158 {
159 return this;
160 }
161
162 throw Components.results.NS_NOINTERFACE;
163 }
164
165 // Dummy methods requiring implementations
166 WebDeveloperApplication.prototype.onLocationChange = function(webProgress, request, location) {}
167 WebDeveloperApplication.prototype.onProgressChange = function(webProgress, request, currentSelfProgress, maximumSelfProgress, currentTotalProgress, maximumTotalProgress) {}
168 WebDeveloperApplication.prototype.onSecurityChange = function(webProgress, request, state) {}
169 WebDeveloperApplication.prototype.onStatusChange = function(webProgress, request, status, message) {}