Bug 513471 Fix some issues with the contact photo editor r=Josh Geenen sr=bienvenu

This commit is contained in:
Neil Rashbrook 2009-09-07 09:48:00 +01:00
Родитель 504aefded8
Коммит 4b805a58e5
5 изменённых файлов: 210 добавлений и 261 удалений

Просмотреть файл

@ -93,7 +93,7 @@ var gEditCard;
var gOnSaveListeners = new Array();
var gOkCallback = null;
var gHideABPicker = false;
var originalPhotoURI = "";
var gOriginalPhotoURI = "";
function OnLoadNewCard()
{
@ -279,15 +279,6 @@ function OnLoadEditCard()
if (directory.readOnly)
{
// Disable the photo field and buttons
document.getElementById("generic").disabled = true;
document.getElementById("GenericPhotoList").disabled = true;
document.getElementById("file").disabled = true;
document.getElementById("web").disabled = true;
document.getElementById("PhotoURI").readOnly = true;
document.getElementById("PhotoURI").emptyText = "";
document.getElementById("BrowsePhoto").disabled = true;
document.getElementById("UpdatePhoto").disabled = true;
// Set all the editable vcard fields to read only
for (var i = kVcardFields.length; i-- > 0; )
document.getElementById(kVcardFields[i][0]).readOnly = true;
@ -297,6 +288,14 @@ function OnLoadEditCard()
document.getElementById("BirthYear").readOnly = true;
document.getElementById("Age").readOnly = true;
// the photo field and buttons
document.getElementById("PhotoType").disabled = true;
document.getElementById("GenericPhotoList").disabled = true;
document.getElementById("PhotoURI").disabled = true;
document.getElementById("PhotoURI").emptyText = "";
document.getElementById("BrowsePhoto").disabled = true;
document.getElementById("UpdatePhoto").disabled = true;
// And the phonetic fields
document.getElementById(kPhoneticFields[0]).readOnly = true;
document.getElementById(kPhoneticFields[3]).readOnly = true;
@ -487,34 +486,32 @@ function GetCardValues(cardproperty, doc)
// Store the original photo URI and update the photo
// Select the type if there is a valid value stored for that type, otherwise
// select the generic photo
var type = cardproperty.getProperty("PhotoType", "");
document.getElementById("PhotoType").selectedItem =
document.getElementById(type ? type : "generic");
if (type == "file") {
originalPhotoURI = getPhotoURI(cardproperty.getProperty("PhotoName", ""));
var file = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI(originalPhotoURI, null, null)
.QueryInterface(Components.interfaces.nsIFileURL)
.file;
if (file) {
document.getElementById("PhotoFile").file = file;
updatePhoto("file");
}
else
gOriginalPhotoURI = cardproperty.getProperty("PhotoURI", "");
switch (cardproperty.getProperty("PhotoType", "")) {
case "file":
try {
var file = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI(gOriginalPhotoURI, null, null)
.QueryInterface(Components.interfaces.nsIFileURL)
.file;
} catch (e) {}
if (file) {
document.getElementById("PhotoFile").file = file;
updatePhoto("file");
}
else
updatePhoto("generic");
break;
case "web":
document.getElementById("PhotoURI").value = gOriginalPhotoURI;
updatePhoto("web");
break;
default:
if (gOriginalPhotoURI)
document.getElementById("GenericPhotoList").value = gOriginalPhotoURI;
updatePhoto("generic");
}
else if (type == "web") {
originalPhotoURI = getPhotoURI(cardproperty.getProperty("PhotoName", ""));
document.getElementById("PhotoURI").value = originalPhotoURI;
updatePhoto("web");
}
else {
originalPhotoURI = cardproperty.getProperty("PhotoURI", "");
if (originalPhotoURI)
document.getElementById("GenericPhotoList").value = originalPhotoURI;
updatePhoto("generic");
}
}
// when the ab card dialog is being loaded to show a vCard,
@ -572,31 +569,36 @@ function CheckAndSetCardValues(cardproperty, doc, check)
}
catch (ex) {}
var type = document.getElementById("PhotoType").selectedItem.id;
var photoURI = originalPhotoURI;
var type = document.getElementById("PhotoType").value;
var photoURI = gOriginalPhotoURI;
if (type == "file" && document.getElementById("PhotoFile").file)
photoURI = "file://" + document.getElementById("PhotoFile").file.path;
photoURI = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(document.getElementById("PhotoFile").file)
.spec;
else if (type == "web" && document.getElementById("PhotoURI").value)
photoURI = document.getElementById("PhotoURI").value;
else {
type = "generic";
photoURI = document.getElementById("GenericPhotoList").value;
}
if (photoURI != originalPhotoURI) {
cardproperty.setProperty("PhotoType", type);
if (photoURI != gOriginalPhotoURI) {
// Store the original URI
cardproperty.setProperty("PhotoURI", photoURI);
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
// Save the photo if it isn't one of the generic photos
if (type != "generic") {
cardproperty.setProperty("PhotoType", "file");
if (type == "generic") {
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
} else {
// Save the new file and store its URI as PhotoName
var file = savePhoto(photoURI);
if (file)
if (file) {
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
cardproperty.setProperty("PhotoName", file.leafName);
}
}
else
cardproperty.setProperty("PhotoType", "generic");
}
return true;
}
@ -905,25 +907,28 @@ function modifyDatepicker(aDatepicker) {
* selected type.
*/
function updatePhoto(aType) {
if (aType) {
if (aType)
// Select the type's radio button
document.getElementById("PhotoType").selectedItem =
document.getElementById(aType);
}
document.getElementById("PhotoType").value = aType;
else
aType = document.getElementById("PhotoType").selectedItem.id;
aType = document.getElementById("PhotoType").value;
var value;
if (aType == "file") {
var file = document.getElementById("PhotoFile").file;
value = file ? "file://" + file.path : "";
switch (aType) {
case "file":
var file = document.getElementById("PhotoFile").file;
value = file ? Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(file)
.spec : "";
break;
case "web":
value = document.getElementById("PhotoURI").value;
break;
default:
value = document.getElementById("GenericPhotoList").value;
}
else if (aType == "web")
value = document.getElementById("PhotoURI").value;
else
value = document.getElementById("GenericPhotoList").value;
document.getElementById("photo").setAttribute("src", value ? value
: defaultPhotoURI);
document.getElementById("photo").setAttribute("src", value || defaultPhotoURI);
}
/**
@ -941,17 +946,10 @@ function removePhoto(aName) {
// Get the photo (throws an exception for invalid names)
try {
file.append(aName);
file.remove(false);
return true;
}
catch (e) {
return false;
}
if (file.exists()) {
try {
file.remove(false);
return true;
}
catch (e) {}
}
catch (e) {}
return false;
}
@ -969,8 +967,8 @@ function browsePhoto() {
fp.init(window, gAddressBookBundle.getString("browsePhoto"), nsIFilePicker.modeOpen);
// Add All Files & Image Files filters and select the latter
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterImages);
fp.filterIndex = 1;
fp.appendFilters(nsIFilePicker.filterImages);
fp.appendFilters(nsIFilePicker.filterAll);
if (fp.show() == nsIFilePicker.returnOK) {
document.getElementById("PhotoFile").file = fp.file;

Просмотреть файл

@ -1024,7 +1024,7 @@ function savePhoto(aUri) {
var istream = channel.open();
// Get the photo file
file.append(makePhotoFilename(file.path, findPhotoExt(aUri, channel)));
file = makePhotoFile(file, findPhotoExt(channel));
return saveStreamToFile(istream, file);
}
@ -1041,27 +1041,17 @@ function savePhoto(aUri) {
*
* @return The extension of the file, if any, including the period.
*/
function findPhotoExt(aUri, aChannel) {
if (aChannel) {
try {
aChannel.QueryInterface(Components.interfaces.nsIHttpChannel);
var header = aChannel.getResponseHeader("Content-Type");
var type = header ? header.split(";")[0] : "";
switch (type.toLowerCase()) {
case "image/png":
return ".png";
case "image/jpeg":
return ".jpg";
case "image/gif":
return ".gif";
}
} catch (e) {}
}
var index = aUri ? aUri.lastIndexOf(".") : -1;
if (index == -1)
return "";
return aUri.substring(index);
function findPhotoExt(aChannel) {
var mimeSvc = Components.classes["@mozilla.org/mime;1"]
.getService(Components.interfaces.nsIMIMEService);
var ext = "";
var uri = aChannel.URI;
if (uri instanceof Components.interfaces.nsIURL)
ext = uri.fileExtension;
try {
return mimeSvc.getPrimaryExtension(aChannel.contentType, ext);
} catch (e) {}
return ext;
}
/**
@ -1072,15 +1062,13 @@ function findPhotoExt(aUri, aChannel) {
*
* @return A unique filename in the given path.
*/
function makePhotoFilename(aPath, aExtension) {
function makePhotoFile(aDir, aExtension) {
var filename, newFile;
// Find a random filename for the photo that doesn't exist yet
do {
filename = new String(Math.random()).replace("0.", "") + aExtension;
newFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
newFile.initWithPath(aPath);
filename = new String(Math.random()).replace("0.", "") + "." + aExtension;
newFile = aDir.clone();
newFile.append(filename);
} while (newFile.exists());
return filename;
return newFile;
}

Просмотреть файл

@ -393,66 +393,43 @@
</vbox>
<!-- ** Photo Tab ** -->
<vbox id="abPhotoTab" >
<hbox flex="1">
<vbox align="left">
<spacer flex="1"/>
<hbox id="photoBox" style="min-width: 25ch; max-width: 25ch;">
<spacer flex="1"/>
<html:img align="center" src="" id="photo"
style="max-width: 25ch; max-height: 25ch; min-width: 1ch;"/>
<spacer flex="1"/>
<hbox id="abPhotoTab" align="center">
<description style="min-width: 25ch; max-width: 25ch; text-align: center">
<html:img id="photo" style="max-width: 25ch; max-height: 25ch;"/>
</description>
<groupbox flex="1">
<caption label="&PhotoDesc.label;"/>
<radiogroup id="PhotoType" onselect="updatePhoto();">
<radio value="generic" label="&GenericPhoto.label;"
accesskey="&GenericPhoto.accesskey;"/>
<menulist id="GenericPhotoList" class="indent" flex="1"
oncommand="updatePhoto('generic');">
<menupopup>
<menuitem label="&DefaultPhoto.label;" selected="true"
value="chrome://messenger/skin/addressbook/icons/contact-generic.png"
image="chrome://messenger/skin/addressbook/icons/contact-generic-tiny.png"/>
</menupopup>
</menulist>
<radio value="file" label="&PhotoFile.label;"
accesskey="&PhotoFile.accesskey;"/>
<hbox class="indent">
<filefield id="PhotoFile" maxlength="255" flex="1"/>
<button oncommand="browsePhoto();" id="BrowsePhoto"
label="&BrowsePhoto.label;"
accesskey="&BrowsePhoto.accesskey;"/>
</hbox>
<spacer flex="1"/>
</vbox>
<vbox>
<command id="PhotoCmd" oncommand="updatePhoto();"/>
<spacer flex="1"/>
<groupbox flex="1">
<caption label="&PhotoDesc.label;"/>
<radiogroup id="PhotoType">
<radio id="generic" label="&GenericPhoto.label;"
command="PhotoCmd"
accesskey="&GenericPhoto.accesskey;"
selected="true"/>
<hbox class="indent">
<menulist id="GenericPhotoList"
oncommand="updatePhoto('generic');">
<menupopup>
<menuitem label="&DefaultPhoto.label;" selected="true"
value="chrome://messenger/skin/addressbook/icons/contact-generic.png"
image="chrome://messenger/skin/addressbook/icons/contact-generic-tiny.png"/>
</menupopup>
</menulist>
</hbox>
<radio id="file" label="&PhotoFile.label;"
command="PhotoCmd"
accesskey="&PhotoFile.accesskey;"/>
<hbox class="indent">
<filefield id="PhotoFile" maxlength="255" flex="1"
disabled="true"
style="-moz-margin-start:2px;"/>
<button oncommand="browsePhoto();" id="BrowsePhoto"
label="&BrowsePhoto.label;"
accesskey="&BrowsePhoto.accesskey;"/>
</hbox>
<radio id="web" label="&PhotoURL.label;"
command="PhotoCmd"
accesskey="&PhotoURL.accesskey;"/>
<hbox class="indent">
<textbox id="PhotoURI" maxlength="255" style="width: 45ch;"
emptytext="&PhotoURL.emptytext;"
class="AddressCardEditWidth"/>
<button oncommand="updatePhoto('web');" id="UpdatePhoto"
label="&UpdatePhoto.label;"
accesskey="&UpdatePhoto.accesskey;"/>
</hbox>
</radiogroup>
</groupbox>
<spacer flex="1"/>
</vbox>
</hbox>
</vbox>
<radio value="web" label="&PhotoURL.label;"
accesskey="&PhotoURL.accesskey;"/>
<hbox class="indent">
<textbox id="PhotoURI" maxlength="255" flex="1"
emptytext="&PhotoURL.emptytext;"/>
<button oncommand="updatePhoto('web');" id="UpdatePhoto"
label="&UpdatePhoto.label;"
accesskey="&UpdatePhoto.accesskey;"/>
</hbox>
</radiogroup>
</groupbox>
</hbox>
</tabpanels>
</tabbox>
</vbox>

Просмотреть файл

@ -93,7 +93,7 @@ var gEditCard;
var gOnSaveListeners = new Array();
var gOkCallback = null;
var gHideABPicker = false;
var originalPhotoURI = "";
var gOriginalPhotoURI = "";
function OnLoadNewCard()
{
@ -279,15 +279,6 @@ function OnLoadEditCard()
if (directory.readOnly)
{
// Disable the photo field and buttons
document.getElementById("generic").disabled = true;
document.getElementById("GenericPhotoList").disabled = true;
document.getElementById("file").disabled = true;
document.getElementById("web").disabled = true;
document.getElementById("PhotoURI").readOnly = true;
document.getElementById("PhotoURI").emptyText = "";
document.getElementById("BrowsePhoto").disabled = true;
document.getElementById("UpdatePhoto").disabled = true;
// Set all the editable vcard fields to read only
for (var i = kVcardFields.length; i-- > 0; )
document.getElementById(kVcardFields[i][0]).readOnly = true;
@ -297,6 +288,14 @@ function OnLoadEditCard()
document.getElementById("BirthYear").readOnly = true;
document.getElementById("Age").readOnly = true;
// the photo field and buttons
document.getElementById("PhotoType").disabled = true;
document.getElementById("GenericPhotoList").disabled = true;
document.getElementById("PhotoURI").disabled = true;
document.getElementById("PhotoURI").emptyText = "";
document.getElementById("BrowsePhoto").disabled = true;
document.getElementById("UpdatePhoto").disabled = true;
// And the phonetic fields
document.getElementById(kPhoneticFields[0]).readOnly = true;
document.getElementById(kPhoneticFields[3]).readOnly = true;
@ -484,34 +483,32 @@ function GetCardValues(cardproperty, doc)
// Store the original photo URI and update the photo
// Select the type if there is a valid value stored for that type, otherwise
// select the generic photo
var type = cardproperty.getProperty("PhotoType", "");
document.getElementById("PhotoType").selectedItem =
document.getElementById(type ? type : "generic");
if (type == "file") {
originalPhotoURI = getPhotoURI(cardproperty.getProperty("PhotoName", ""));
var file = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI(originalPhotoURI, null, null)
.QueryInterface(Components.interfaces.nsIFileURL)
.file;
if (file) {
document.getElementById("PhotoFile").file = file;
updatePhoto("file");
}
else
gOriginalPhotoURI = cardproperty.getProperty("PhotoURI", "");
switch (cardproperty.getProperty("PhotoType", "")) {
case "file":
try {
var file = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI(gOriginalPhotoURI, null, null)
.QueryInterface(Components.interfaces.nsIFileURL)
.file;
} catch (e) {}
if (file) {
document.getElementById("PhotoFile").file = file;
updatePhoto("file");
}
else
updatePhoto("generic");
break;
case "web":
document.getElementById("PhotoURI").value = gOriginalPhotoURI;
updatePhoto("web");
break;
default:
if (gOriginalPhotoURI)
document.getElementById("GenericPhotoList").value = gOriginalPhotoURI;
updatePhoto("generic");
}
else if (type == "web") {
originalPhotoURI = getPhotoURI(cardproperty.getProperty("PhotoName", ""));
document.getElementById("PhotoURI").value = originalPhotoURI;
updatePhoto("web");
}
else {
originalPhotoURI = cardproperty.getProperty("PhotoURI", "");
if (originalPhotoURI)
document.getElementById("GenericPhotoList").value = originalPhotoURI;
updatePhoto("generic");
}
}
// when the ab card dialog is being loaded to show a vCard,
@ -569,31 +566,36 @@ function CheckAndSetCardValues(cardproperty, doc, check)
}
catch (ex) {}
var type = document.getElementById("PhotoType").selectedItem.id;
var photoURI = originalPhotoURI;
var type = document.getElementById("PhotoType").value;
var photoURI = gOriginalPhotoURI;
if (type == "file" && document.getElementById("PhotoFile").file)
photoURI = "file://" + document.getElementById("PhotoFile").file.path;
photoURI = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(document.getElementById("PhotoFile").file)
.spec;
else if (type == "web" && document.getElementById("PhotoURI").value)
photoURI = document.getElementById("PhotoURI").value;
else {
type = "generic";
photoURI = document.getElementById("GenericPhotoList").value;
}
if (photoURI != originalPhotoURI) {
cardproperty.setProperty("PhotoType", type);
if (photoURI != gOriginalPhotoURI) {
// Store the original URI
cardproperty.setProperty("PhotoURI", photoURI);
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
// Save the photo if it isn't one of the generic photos
if (type != "generic") {
cardproperty.setProperty("PhotoType", "file");
if (type == "generic") {
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
} else {
// Save the new file and store its URI as PhotoName
var file = savePhoto(photoURI);
if (file)
if (file) {
// Remove the original, if any
removePhoto(cardproperty.getProperty("PhotoName", null));
cardproperty.setProperty("PhotoName", file.leafName);
}
}
else
cardproperty.setProperty("PhotoType", "generic");
}
return true;
@ -903,25 +905,28 @@ function modifyDatepicker(aDatepicker) {
* selected type.
*/
function updatePhoto(aType) {
if (aType) {
if (aType)
// Select the type's radio button
document.getElementById("PhotoType").selectedItem =
document.getElementById(aType);
}
document.getElementById("PhotoType").value = aType;
else
aType = document.getElementById("PhotoType").selectedItem.id;
aType = document.getElementById("PhotoType").value;
var value;
if (aType == "file") {
var file = document.getElementById("PhotoFile").file;
value = file ? "file://" + file.path : "";
switch (aType) {
case "file":
var file = document.getElementById("PhotoFile").file;
value = file ? Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(file)
.spec : "";
break;
case "web":
value = document.getElementById("PhotoURI").value;
break;
default:
value = document.getElementById("GenericPhotoList").value;
}
else if (aType == "web")
value = document.getElementById("PhotoURI").value;
else
value = document.getElementById("GenericPhotoList").value;
document.getElementById("photo").setAttribute("src", value ? value
: defaultPhotoURI);
document.getElementById("photo").setAttribute("src", value || defaultPhotoURI);
}
/**
@ -939,17 +944,10 @@ function removePhoto(aName) {
// Get the photo (throws an exception for invalid names)
try {
file.append(aName);
file.remove(false);
return true;
}
catch (e) {
return false;
}
if (file.exists()) {
try {
file.remove(false);
return true;
}
catch (e) {}
}
catch (e) {}
return false;
}
@ -967,8 +965,8 @@ function browsePhoto() {
fp.init(window, gAddressBookBundle.getString("browsePhoto"), nsIFilePicker.modeOpen);
// Add All Files & Image Files filters and select the latter
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterImages);
fp.filterIndex = 1;
fp.appendFilters(nsIFilePicker.filterImages);
fp.appendFilters(nsIFilePicker.filterAll);
if (fp.show() == nsIFilePicker.returnOK) {
document.getElementById("PhotoFile").file = fp.file;

Просмотреть файл

@ -734,7 +734,7 @@ function savePhoto(aUri) {
var istream = channel.open();
// Get the photo file
file.append(makePhotoFilename(file.path, findPhotoExt(aUri, channel)));
file = makePhotoFile(file, findPhotoExt(channel));
return saveStreamToFile(istream, file);
}
@ -751,27 +751,17 @@ function savePhoto(aUri) {
*
* @return The extension of the file, if any, including the period.
*/
function findPhotoExt(aUri, aChannel) {
if (aChannel) {
try {
aChannel.QueryInterface(Components.interfaces.nsIHttpChannel);
var header = aChannel.getResponseHeader("Content-Type");
var type = header ? header.split(";")[0] : "";
switch (type.toLowerCase()) {
case "image/png":
return ".png";
case "image/jpeg":
return ".jpg";
case "image/gif":
return ".gif";
}
} catch (e) {}
}
var index = aUri ? aUri.lastIndexOf(".") : -1;
if (index == -1)
return "";
return aUri.substring(index);
function findPhotoExt(aChannel) {
var mimeSvc = Components.classes["@mozilla.org/mime;1"]
.getService(Components.interfaces.nsIMIMEService);
var ext = "";
var uri = aChannel.URI;
if (uri instanceof Components.interfaces.nsIURL)
ext = uri.fileExtension;
try {
return mimeSvc.getPrimaryExtension(aChannel.contentType, ext);
} catch (e) {}
return ext;
}
/**
@ -782,15 +772,13 @@ function findPhotoExt(aUri, aChannel) {
*
* @return A unique filename in the given path.
*/
function makePhotoFilename(aPath, aExtension) {
function makePhotoFile(aDir, aExtension) {
var filename, newFile;
// Find a random filename for the photo that doesn't exist yet
do {
filename = new String(Math.random()).replace("0.", "") + aExtension;
newFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
newFile.initWithPath(aPath);
filename = new String(Math.random()).replace("0.", "") + "." + aExtension;
newFile = aDir.clone();
newFile.append(filename);
} while (newFile.exists());
return filename;
return newFile;
}