Actually use new publish interfaces for publishing.

bug 285016, r=jminta
This commit is contained in:
mvl%exedo.nl 2005-08-22 17:43:01 +00:00
Родитель 11e8da901d
Коммит d1243bef37
3 изменённых файлов: 173 добавлений и 312 удалений

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

@ -1023,68 +1023,6 @@ function print()
window.openDialog("chrome://calendar/content/printDialog.xul","printdialog","chrome",args); window.openDialog("chrome://calendar/content/printDialog.xul","printdialog","chrome",args);
} }
function publishEntireCalendar()
{
var args = new Object();
args.onOk = self.publishEntireCalendarDialogResponse;
var remotePath = ""; // get a remote path as a pref of the calendar
if (remotePath != "" && remotePath != null) {
var publishObject = new Object( );
publishObject.remotePath = remotePath;
args.publishObject = publishObject;
}
openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", "chrome,titlebar,modal", args );
}
function publishEntireCalendarDialogResponse( CalendarPublishObject )
{
var icsURL = makeURL(CalendarPublishObject.remotePath);
var oldCalendar = getDefaultCalendar(); // get the currently selected calendar
// create an ICS calendar, but don't register it
var calManager = getCalendarManager();
try {
var newCalendar = calManager.createCalendar("ics", icsURL);
} catch (ex) {
dump(ex);
return;
}
var getListener = {
onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
{
// delete the new calendar now that we're done with it
calManager.deleteCalendar(newCalendar);
}
};
appendCalendars(newCalendar, [oldCalendar], getListener);
}
function publishCalendarData()
{
var args = new Object();
args.onOk = self.publishCalendarDataDialogResponse;
openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", "chrome,titlebar,modal", args );
}
function publishCalendarDataDialogResponse( CalendarPublishObject )
{
var calendarString = eventArrayToICalString( gCalendarWindow.EventSelection.selectedEvents );
calendarPublish(calendarString, CalendarPublishObject.remotePath, "text/calendar");
}
function getCharPref (prefObj, prefName, defaultValue) function getCharPref (prefObj, prefName, defaultValue)
{ {
try { try {

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

@ -36,279 +36,192 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
// Original code: function publishCalendarData()
// http://lxr.mozilla.org/mozilla/source/editor/ui/composer/content/publish.js {
// TODO Implement uploadfile with: var args = new Object();
//void setUploadFile(in nsIFile file, in string contentType, in long contentLength);
args.onOk = self.publishCalendarDataDialogResponse;
var gPublishHandler = null;
openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", "chrome,titlebar,modal", args );
/* Create an instance of the given ContractID, with given interface */
function createInstance(contractId, intf) {
return Components.classes[contractId].createInstance(Components.interfaces[intf]);
} }
function publishCalendarDataDialogResponse(CalendarPublishObject, aProgressDialog)
var gPublishIOService;
function GetIOService()
{ {
if (gPublishIOService) publishItemArray(gCalendarWindow.EventSelection.selectedEvents,
return gPublishIOService; CalendarPublishObject.remotePath, aProgressDialog);
gPublishIOService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
if (!gPublishIOService)
dump("failed to get io service\n");
return gPublishIOService;
} }
function publishEntireCalendar()
/**
* This is the entry point into the file from calendar.js
* contentType is always text/calendar
*/
function calendarPublish(aDataString, newLocation, contentType)
{ {
try var args = new Object();
{ var publishObject = new Object( );
var protocolChannel = get_destination_channel(newLocation);
if (!protocolChannel) args.onOk = self.publishEntireCalendarDialogResponse;
{
dump("failed to get a destination channel\n"); // get the currently selected calendar
return; var cal = getDefaultCalendar();
publishObject.calendar = cal;
// get a remote path as a pref of the calendar
var remotePath = getCalendarManager().getCalendarPref(cal, "publishpath");
if (remotePath && remotePath != "") {
publishObject.remotePath = remotePath;
} }
output_string_to_channel(protocolChannel, aDataString, contentType);
protocolChannel.asyncOpen(gPublishingListener, null); args.publishObject = publishObject;
} openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", "chrome,titlebar,modal", args );
catch (e)
{
alert("an error occurred in calendarPublish: " + e + "\n");
}
} }
function calendarUploadFile(aSourceFilename, newLocation, contentType) function publishEntireCalendarDialogResponse(CalendarPublishObject, aProgressDialog)
{ {
try var itemArray = [];
{ var getListener = {
var protocolChannel = get_destination_channel(newLocation); onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
if (!protocolChannel) {
{ publishItemArray(itemArray, CalendarPublishObject.remotePath, aProgressDialog);
dump("failed to get a destination channel\n"); },
return; onGetResult: function(aCalendar, aStatus, aItemType, aDetail, aCount, aItems)
} {
output_file_to_channel(protocolChannel, aSourceFilename, contentType); if (!Components.isSuccessCode(aStatus)) {
protocolChannel.asyncOpen(gPublishingListener, protocolChannel); aborted = true;
return;
}
if (aCount) {
for (var i=0; i<aCount; ++i) {
// Store a (short living) reference to the item.
var itemCopy = aItems[i].clone();
itemArray.push(itemCopy);
}
}
}
};
aProgressDialog.onStartUpload();
var oldCalendar = CalendarPublishObject.calendar;
oldCalendar.getItems(Components.interfaces.calICalendar.ITEM_FILTER_TYPE_ALL,
0, null, null, getListener);
return;
}
catch (e)
{
alert("an error occurred in calendarUploadFile: " + e + "\n");
}
} }
function publishItemArray(aItemArray, aPath, aProgressDialog) {
var outputStream;
var inputStream;
var storageStream;
function output_string_to_channel( aChannel, aDataString, contentType ) var icsURL = makeURL(aPath);
{
var uploadChannel = aChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
var postStream = createInstance('@mozilla.org/io/string-input-stream;1', 'nsIStringInputStream');
// Create the stream var ioService = Components.classes["@mozilla.org/network/io-service;1"]
postStream.setData(aDataString, aDataString.length); .getService(Components.interfaces.nsIIOService);
// Send the stream (data) through the channel
uploadChannel.setUploadStream(postStream, contentType, -1); var channel = ioService.newChannelFromURI(icsURL);
if (icsURL.schemeIs('webcal'))
icsURL.scheme = 'http';
if (icsURL.schemeIs('webcals'))
icsURL.scheme = 'https';
switch(icsURL.scheme) {
case 'http':
case 'https':
channel = channel.QueryInterface(Components.interfaces.nsIHttpChannel);
break;
case 'ftp':
channel = channel.QueryInterface(Components.interfaces.nsIFTPChannel);
break;
case 'file':
channel = channel.QueryInterface(Components.interfaces.nsIFileChannel);
break;
default:
dump("No such scheme\n");
return;
}
var uploadChannel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);
uploadChannel.notificationCallbacks = notificationCallbacks;
storageStream = Components.classes["@mozilla.org/storagestream;1"]
.createInstance(Components.interfaces.nsIStorageStream);
storageStream.init(32768, 0xffffffff, null);
outputStream = storageStream.getOutputStream(0);
var exporter = Components.classes["@mozilla.org/calendar/export;1?type=ics"]
.getService(Components.interfaces.calIExporter);
exporter.exportToStream(outputStream,
aItemArray.length, aItemArray);
outputStream.close();
inputStream = storageStream.newInputStream(0);
uploadChannel.setUploadStream(inputStream,
"text/calendar", -1);
try {
channel.asyncOpen(publishingListener, aProgressDialog);
} catch (e) {
var calendarStringBundle = srGetStrBundle("chrome://calendar/locale/calendar.properties");
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
promptService.alert(null, calendarStringBundle.GetStringFromName('errorTitle'),
calendarStringBundle.formatStringFromName('otherPutError',[e.message],1));
}
} }
function output_file_to_channel( aChannel, aFilePath, contentType )
{
var uploadChannel = aChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
var thisFile = new File( aFilePath );
thisFile.open( "r" );
var theFileContents = thisFile.read();
output_string_to_channel( aChannel, theFileContents, contentType );
}
var notificationCallbacks = var notificationCallbacks =
{ {
// nsIInterfaceRequestor interface // nsIInterfaceRequestor interface
getInterface: function(iid, instance) { getInterface: function(iid, instance) {
if (iid.equals(Components.interfaces.nsIAuthPrompt)) { if (iid.equals(Components.interfaces.nsIAuthPrompt)) {
// use the window watcher service to get a nsIAuthPrompt impl // use the window watcher service to get a nsIAuthPrompt impl
var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher); .getService(Components.interfaces.nsIWindowWatcher);
return ww.getNewAuthPrompter(null); return ww.getNewAuthPrompter(null);
}
Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
return null;
} }
Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
return null;
}
} }
// this creates a channel for the destination, and hooks up the password mananager var publishingListener =
function get_destination_channel(destinationDirectoryLocation)
{ {
var ioService = GetIOService(); QueryInterface: function(aIId, instance)
if (!ioService)
{ {
return null; if (aIId.equals(Components.interfaces.nsIStreamListener) ||
} aIId.equals(Components.interfaces.nsISupports))
return this;
// create a channel for the destination location Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
destChannel = create_channel_from_url(ioService, destinationDirectoryLocation); return null;
if (!destChannel) },
{
dump("can't create dest channel\n");
return null;
}
try onStartRequest: function(request, ctxt)
{ {
dump("about to set callbacks\n"); },
destChannel.notificationCallbacks = notificationCallbacks; // docshell
dump("notification callbacks set\n");
}
catch(e) {
dump(e+"\n");
}
try { onStopRequest: function(request, ctxt, status, errorMsg)
switch(destChannel.URI.scheme) {
{ ctxt.wrappedJSObject.onStopUpload();
case 'http':
case 'https': var channel;
return destChannel.QueryInterface( var calendarStringBundle = srGetStrBundle("chrome://calendar/locale/calendar.properties");
Components.interfaces.nsIHttpChannel var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
); .getService(Components.interfaces.nsIPromptService);
case 'ftp': try {
return destChannel.QueryInterface( channel = request.QueryInterface(Components.interfaces.nsIHttpChannel);
Components.interfaces.nsIFTPChannel dump(ch.requestSucceeded+"\n");
); } catch(e) {
case 'file': }
return destChannel.QueryInterface( if (channel && !channel.requestSucceeded) {
Components.interfaces.nsIFileChannel promptService.alert(null, calendarStringBundle.GetStringFromName('errorTitle'),
); calendarStringBundle.formatStringFromName('httpPutError',[channel.responseStatus, channel.responseStatusText],2));
default: }
return null; else if (!channel && !Components.isSuccessCode(request.status)) {
} // XXX this should be made human-readable.
} promptService.alert(null, calendarStringBundle.GetStringFromName('errorTitle'),
catch( e ) calendarStringBundle.formatStringFromName('otherPutError',[request.status.toString(16)],1));
}
},
onDataAvailable: function(request, ctxt, inStream, sourceOffset, count)
{ {
return null;
} }
} }
// this function takes a full url, creates an nsIURI, and then creates a channel from that nsIURI
function create_channel_from_url(ioService, aURL)
{
try
{
var nsiuri = ioService.newURI(aURL, null, null);
if (!nsiuri)
return null;
return ioService.newChannelFromURI(nsiuri);
}
catch (e)
{
alert(e+"\n");
return null;
}
}
/*
function PublishCopyFile(srcDirectoryLocation, destinationDirectoryLocation, aLogin, aPassword)
{
// append '/' if it's not there; inputs should be directories so should end in '/'
if ( srcDirectoryLocation.charAt(srcDirectoryLocation.length-1) != '/' )
srcDirectoryLocation = srcDirectoryLocation + '/';
if ( destinationDirectoryLocation.charAt(destinationDirectoryLocation.length-1) != '/' )
destinationDirectoryLocation = destinationDirectoryLocation + '/';
try
{
// grab an io service
var ioService = GetIOService();
if (!ioService)
return;
// create a channel for the source location
srcChannel = create_channel_from_url(ioService, srcDirectoryLocation, null, null);
if (!srcChannel)
{
dump("can't create src channel\n");
return;
}
// create a channel for the destination location
var ftpChannel = get_destination_channel(destinationDirectoryLocation, aLogin, aPassword);
if (!ftpChannel)
{
dump("failed to get ftp channel\n");
return;
}
ftpChannel.open();
protocolChannel.uploadStream = srcChannel.open();
protocolChannel.asyncOpen(null, null);
dump("done\n");
}
catch (e)
{
dump("an error occurred: " + e + "\n");
}
}
*/
var gPublishingListener =
{
QueryInterface: function(aIId, instance)
{
if (aIId.equals(Components.interfaces.nsIStreamListener) ||
aIId.equals(Components.interfaces.nsISupports))
return this;
Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
return null;
},
onStartRequest: function(request, ctxt)
{
dump("onStartRequest status = " + request.status.toString(16) + "\n");
},
onStopRequest: function(request, ctxt, status, errorMsg)
{
dump("onStopRequest status = " + request.status.toString(16) + " " + errorMsg + "\n");
var ch;
var calendarStringBundle = srGetStrBundle("chrome://calendar/locale/calendar.properties");
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
try {
ch = request.QueryInterface(Components.interfaces.nsIHttpChannel);
dump(ch.requestSucceeded+"\n");
} catch(e) {
}
if (ch && !ch.requestSucceeded) {
promptService.alert(null, calendarStringBundle.GetStringFromName('errorTitle'),
calendarStringBundle.formatStringFromName('httpPutError',[ch.responseStatus, ch.responseStatusText],2));
}
else if (!ch && !Components.isSuccessCode(request.status)) {
// XXX this should be made human-readable.
promptService.alert(null, calendarStringBundle.GetStringFromName('errorTitle'),
calendarStringBundle.formatStringFromName('otherPutError',[request.status.toString(16)],1));
}
},
onDataAvailable: function(request, ctxt, inStream, sourceOffset, count)
{
dump("onDataAvailable status = " + request.status.toString(16) + " " + count + "\n");
}
}

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

@ -46,6 +46,7 @@
var gOnOkFunction; // function to be called when user clicks OK var gOnOkFunction; // function to be called when user clicks OK
var gPublishObject;
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
* W I N D O W F U N C T I O N S * W I N D O W F U N C T I O N S
@ -65,10 +66,13 @@ function loadCalendarPublishDialog()
if( args.publishObject ) if( args.publishObject )
{ {
document.getElementById( "publish-remotePath-textbox" ).value = args.publishObject.remotePath; gPublishObject = args.publishObject;
if ( args.publishObject.remotePath )
document.getElementById( "publish-remotePath-textbox" ).value = args.publishObject.remotePath;
} }
else else
{ {
gPublishObject = new Object();
//get default values from the prefs //get default values from the prefs
document.getElementById( "publish-remotePath-textbox" ).value = opener.getCharPref( opener.gCalendarWindow.calendarPreferences.calendarPref, "publish.path", "" ); document.getElementById( "publish-remotePath-textbox" ).value = opener.getCharPref( opener.gCalendarWindow.calendarPreferences.calendarPref, "publish.path", "" );
} }
@ -88,16 +92,12 @@ function loadCalendarPublishDialog()
function onOKCommand() function onOKCommand()
{ {
var CalendarPublishObject = new Object(); gPublishObject.remotePath = document.getElementById( "publish-remotePath-textbox" ).value;
CalendarPublishObject.remotePath = document.getElementById( "publish-remotePath-textbox" ).value;
document.getElementById( "publish-progressmeter" ).setAttribute( "mode", "undetermined" );
// call caller's on OK function // call caller's on OK function
gOnOkFunction( CalendarPublishObject ); gOnOkFunction(gPublishObject, progressDialog);
document.getElementById( "calendar-publishwindow" ).getButton( "accept" ).setAttribute( "label", closeButtonLabel ); document.getElementById( "calendar-publishwindow" ).getButton( "accept" ).setAttribute( "label", closeButtonLabel );
document.getElementById( "calendar-publishwindow" ).getButton( "accept" ).setAttribute( "oncommand", "closeDialog()" ); document.getElementById( "calendar-publishwindow" ).setAttribute( "ondialogaccept", "closeDialog()" );
document.getElementById( "publish-progressmeter" ).setAttribute( "mode", "determined" );
return( false ); return( false );
} }
@ -115,3 +115,13 @@ function closeDialog( )
self.close( ); self.close( );
} }
var progressDialog = {
onStartUpload: function() {
document.getElementById( "publish-progressmeter" ).setAttribute( "mode", "undetermined" );
},
onStopUpload: function() {
document.getElementById( "publish-progressmeter" ).setAttribute( "mode", "determined" );
}
};
progressDialog.wrappedJSObject = progressDialog;