~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/toolkit/mozapps/downloads/content/editAction.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 
4
# The contents of this file are subject to the Mozilla Public License Version
 
5
# 1.1 (the "License"); you may not use this file except in compliance with
 
6
# the License. You may obtain a copy of the License at
 
7
# http://www.mozilla.org/MPL/
 
8
 
9
# Software distributed under the License is distributed on an "AS IS" basis,
 
10
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
# for the specific language governing rights and limitations under the
 
12
# License.
 
13
 
14
# The Original Code is Mozilla.org Code.
 
15
 
16
# The Initial Developer of the Original Code is
 
17
# Doron Rosenberg.
 
18
# Portions created by the Initial Developer are Copyright (C) 2001
 
19
# the Initial Developer. All Rights Reserved.
 
20
 
21
# Contributor(s):
 
22
#   Ben Goodger <ben@bengoodger.com>
 
23
 
24
# Alternatively, the contents of this file may be used under the terms of
 
25
# either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
# in which case the provisions of the GPL or the LGPL are applicable instead
 
28
# of those above. If you wish to allow use of your version of this file only
 
29
# under the terms of either the GPL or the LGPL, and not to allow others to
 
30
# use your version of this file under the terms of the MPL, indicate your
 
31
# decision by deleting the provisions above and replace them with the notice
 
32
# and other provisions required by the GPL or the LGPL. If you do not delete
 
33
# the provisions above, a recipient may use your version of this file under
 
34
# the terms of any one of the MPL, the GPL or the LGPL.
 
35
 
36
# ***** END LICENSE BLOCK *****
 
37
 
 
38
var gRDF, gItemRes, gHelperApps;
 
39
var gNC_URI;
 
40
var gHandlerRes, gExtAppRes;
 
41
 
 
42
var gLastSelectedActionItem = null;
 
43
 
 
44
function init()
 
45
{
 
46
  gRDF = window.opener.gRDF;
 
47
  gItemRes = window.arguments[0];
 
48
  
 
49
  gHelperApps = window.opener.gHelperApps;
 
50
  gHandlerPropArc = gHelperApps._handlerPropArc;
 
51
  gExternalAppArc = gHelperApps._externalAppArc;
 
52
  gFileHandlerArc = gHelperApps._fileHandlerArc;
 
53
  
 
54
  gNC_URI = window.opener.NC_URI;
 
55
  
 
56
  var typeField = document.getElementById("typeField");
 
57
  var str = gHelperApps.getLiteralValue(gItemRes.Value, "FileType");
 
58
  // XXXben localize!
 
59
  str += " (" + gHelperApps.getLiteralValue(gItemRes.Value, "FileExtensions") + ")";
 
60
  typeField.value = str;
 
61
  var typeIcon = document.getElementById("typeIcon");
 
62
  typeIcon.src = gHelperApps.getLiteralValue(gItemRes.Value, "FileIcon");
 
63
 
 
64
  var handlerGroup = document.getElementById("handlerGroup");
 
65
  
 
66
  gHandlerRes = gHelperApps.GetTarget(gItemRes, gHandlerPropArc, true);
 
67
  if (gHandlerRes) {
 
68
    gHandlerRes = gHandlerRes.QueryInterface(Components.interfaces.nsIRDFResource);
 
69
 
 
70
    // Custom App Handler Path - this must be set before we set the selected
 
71
    // radio button because the selection event handler for the radio group
 
72
    // requires the extapp handler field to be non-empty for the extapp radio
 
73
    // button to be selected. 
 
74
    var gExtAppRes = gHelperApps.GetTarget(gHandlerRes, gExternalAppArc, true);
 
75
    if (gExtAppRes) {
 
76
      gExtAppRes = gExtAppRes.QueryInterface(Components.interfaces.nsIRDFResource);
 
77
 
 
78
      var customAppPath = document.getElementById("customAppPath");          
 
79
      customAppPath.value = gHelperApps.getLiteralValue(gExtAppRes.Value, "path");
 
80
    }
 
81
    
 
82
    // Selected Action Radiogroup
 
83
    var handleInternal = gHelperApps.getLiteralValue(gHandlerRes.Value, "useSystemDefault");
 
84
    var saveToDisk = gHelperApps.getLiteralValue(gHandlerRes.Value, "saveToDisk");
 
85
    if (handleInternal == "true")
 
86
      handlerGroup.selectedItem = document.getElementById("openDefault");
 
87
    else if (saveToDisk == "true")
 
88
      handlerGroup.selectedItem = document.getElementById("saveToDisk");
 
89
    else
 
90
      handlerGroup.selectedItem = document.getElementById("openApplication");
 
91
      
 
92
    gLastSelectedActionItem = handlerGroup.selectedItem;
 
93
  }
 
94
  else {
 
95
    // No Handler/ExtApp Resources for this type for some reason
 
96
    handlerGroup.selectedItem = document.getElementById("openDefault");
 
97
  }
 
98
 
 
99
  var defaultAppName = document.getElementById("defaultAppName");
 
100
  var mimeInfo = gHelperApps.getMIMEInfo(gItemRes);
 
101
  defaultAppName.value = mimeInfo.defaultDescription;
 
102
  
 
103
  handlerGroup.focus();
 
104
  
 
105
  // We don't let users open .exe files or random binary data directly 
 
106
  // from the browser at the moment because of security concerns. 
 
107
  var mimeType = mimeInfo.MIMEType;
 
108
  if (mimeType == "application/object-stream" ||
 
109
      mimeType == "application/x-msdownload") {
 
110
    document.getElementById("openApplication").disabled = true;
 
111
    document.getElementById("openDefault").disabled = true;
 
112
    handlerGroup.selectedItem = document.getElementById("saveToDisk");
 
113
  }
 
114
  
 
115
  var x = document.documentElement.getAttribute("screenX");
 
116
  if (x == "")
 
117
    setTimeout(centerOverParent, 0);
 
118
}
 
119
 
 
120
function setLiteralValue(aResource, aProperty, aValue)
 
121
{
 
122
  var prop = gRDF.GetResource(gNC_URI(aProperty));
 
123
  var val = gRDF.GetLiteral(aValue);
 
124
  var oldVal = gHelperApps.GetTarget(aResource, prop, true);
 
125
  if (oldVal)
 
126
    gHelperApps.Change(aResource, prop, oldVal, val);
 
127
  else
 
128
    gHelperApps.Assert(aResource, prop, val, true);
 
129
}
 
130
 
 
131
function onAccept()
 
132
{
 
133
  var dirty = false;
 
134
  
 
135
  if (gHandlerRes) {  
 
136
    var handlerGroup = document.getElementById("handlerGroup");
 
137
    var value = handlerGroup.selectedItem.getAttribute("value");
 
138
    switch (value) {
 
139
    case "system":
 
140
      setLiteralValue(gHandlerRes, "saveToDisk", "false");
 
141
      setLiteralValue(gHandlerRes, "useSystemDefault", "true");
 
142
      break;
 
143
    case "app":
 
144
      setLiteralValue(gHandlerRes, "saveToDisk", "false");
 
145
      setLiteralValue(gHandlerRes, "useSystemDefault", "false");
 
146
      break;  
 
147
    case "save":
 
148
      setLiteralValue(gHandlerRes, "saveToDisk", "true");
 
149
      setLiteralValue(gHandlerRes, "useSystemDefault", "false");
 
150
      break;  
 
151
    }
 
152
    
 
153
    dirty = true;
 
154
  }
 
155
    
 
156
  if (gExtAppRes) {
 
157
    var customAppPath = document.getElementById("customAppPath");
 
158
    if (customAppPath.value != "") {
 
159
      setLiteralValue(gExtAppRes, "path", customAppPath.value);
 
160
      setLiteralValue(gExtAppRes, "prettyName", customAppPath.getAttribute("prettyName"));
 
161
    }
 
162
    
 
163
    dirty = true;
 
164
  }
 
165
  
 
166
  if (dirty) {
 
167
    gHelperApps.flush();
 
168
   
 
169
    // Get the template builder to refresh the display by announcing our imaginary
 
170
    // "FileHandler" property has been updated. (NC:FileHandler is a property
 
171
    // that our wrapper DS uses, its value is built dynamically based on real
 
172
    // values of other properties.
 
173
    var newHandler = gHelperApps.GetTarget(gItemRes, gFileHandlerArc, true);
 
174
    gHelperApps.onChange(gHelperApps, gItemRes, gFileHandlerArc, newHandler);
 
175
    
 
176
    // ... this doesn't seem to work, so...
 
177
    var fileHandlersList = window.opener.document.getElementById("fileHandlersList");
 
178
    fileHandlersList.builder.rebuild();
 
179
  }
 
180
    
 
181
  return true;
 
182
}
 
183
 
 
184
function centerOverParent()
 
185
{
 
186
  var parent = window.opener;
 
187
  
 
188
  x = parent.screenX + (parent.outerWidth / 2) - (window.outerWidth / 2);
 
189
  y = parent.screenY + (parent.outerHeight / 2) - (window.outerHeight / 2);
 
190
  window.moveTo(x, y);
 
191
}
 
192
 
 
193
function doEnabling(aSelectedItem)
 
194
{
 
195
  if (aSelectedItem.id == "openApplication") {
 
196
    var customAppPath = document.getElementById("customAppPath")
 
197
    customAppPath.disabled = false;
 
198
    document.getElementById("changeApp").disabled = false;
 
199
    
 
200
    if (customAppPath.value == "" && !changeApp()) {
 
201
      gLastSelectedActionItem.click();
 
202
      return;
 
203
    }
 
204
  }
 
205
  else {
 
206
    document.getElementById("customAppPath").disabled = true;
 
207
    document.getElementById("changeApp").disabled = true;
 
208
  }
 
209
  
 
210
  gLastSelectedActionItem = aSelectedItem;
 
211
}
 
212
 
 
213
function changeApp()
 
214
{
 
215
  const nsIFilePicker = Components.interfaces.nsIFilePicker;
 
216
  var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
 
217
 
 
218
  // extract the window title
 
219
  var winTitle = document.getElementById('changeApp').getAttribute('filepickertitle');
 
220
  fp.init(window, winTitle, nsIFilePicker.modeOpen);
 
221
  
 
222
  fp.appendFilters(nsIFilePicker.filterApps);
 
223
  if (fp.show() == nsIFilePicker.returnOK && fp.file) {
 
224
    var customAppPath = document.getElementById("customAppPath");
 
225
    customAppPath.value = fp.file.path;
 
226
    
 
227
    var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
 
228
    var uri = ioService.newFileURI(fp.file);
 
229
    var url = uri.QueryInterface(Components.interfaces.nsIURL);
 
230
    customAppPath.setAttribute("prettyName", url.fileName);
 
231
 
 
232
    var mimeInfo = gHelperApps.getMIMEInfo(gItemRes);
 
233
    gExtAppRes = gRDF.GetResource("urn:mimetype:externalApplication:" + mimeInfo.MIMEType);
 
234
    
 
235
    
 
236
    return true;
 
237
  }
 
238
  
 
239
  return false;
 
240
}
 
241