2002-04-02 18:47:56 +00:00
/ * * * * * * B E G I N L I C E N S E B L O C K * * * * *
* Version : MPL 1.1 / GPL 2.0 / LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 ( the "License" ) ; you may not use this file except in compliance with
* the License . You may obtain a copy of the License at
* http : //www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis ,
* WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
* for the specific language governing rights and limitations under the
* License .
*
* The Original Code is OEone Calendar Code , released October 31 st , 2001.
*
* The Initial Developer of the Original Code is
* OEone Corporation .
* Portions created by the Initial Developer are Copyright ( C ) 2001
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) : Dan Parent < danp @ oeone . com >
*
* Alternatively , the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later ( the "GPL" ) , or
* the GNU Lesser General Public License Version 2.1 or later ( the "LGPL" ) ,
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above . If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL , and not to allow others to
* use your version of this file under the terms of the MPL , indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL . If you do not delete
* the provisions above , a recipient may use your version of this file under
* the terms of any one of the MPL , the GPL or the LGPL .
*
* * * * * * END LICENSE BLOCK * * * * * * /
var gMailAccounts = false ;
2002-04-03 16:43:14 +00:00
var gMAILDEBUG = 1 ; // 0 - no output, 1 dump to terminal, > 1 use alerts
var gMailIdentity ;
2002-04-02 18:47:56 +00:00
2002-09-11 18:27:09 +00:00
var emailStringBundle ;
2002-04-12 20:24:10 +00:00
/ * * * * c h e c k F o r M a i l N e w s
2002-04-02 18:47:56 +00:00
*
* PURPOSE : if mailnews is installed return true , else false
*
* /
2002-04-12 20:24:10 +00:00
function checkForMailNews ( )
2002-04-02 18:47:56 +00:00
{
2002-09-11 18:27:09 +00:00
emailStringBundle = srGetStrBundle ( "chrome://calendar/locale/email.properties" ) ;
var AccountManagerComponent ;
2002-04-02 18:47:56 +00:00
var AccountManagerService ;
var AccountManager ;
var DefaultAccount ;
var SmtpServiceComponent ;
var SmtpService ;
var Smtp ;
var DefaultSmtp ;
try
{
AccountManagerComponent = Components . classes [ "@mozilla.org/messenger/account-manager;1" ] ;
AccountManagerService = AccountManagerComponent . getService ( ) ;
}
catch ( ex ) // yikes no account manager == no accounts
{
// from bug 122651 - scenario 1
noMail ( ) ;
return ;
}
try
{
AccountManager = AccountManagerService . QueryInterface ( Components . interfaces . nsIMsgAccountManager ) ;
DefaultAccount = AccountManager . defaultAccount ;
2002-04-03 16:43:14 +00:00
gMailIdentity = DefaultAccount . defaultIdentity ; // we'll store the user's account info globally for now
2002-04-02 18:47:56 +00:00
}
catch ( ex )
{
// from bug 122651 - scenario 2
noAccount ( ) ;
return ;
}
try
{
SmtpServiceComponent = Components . classes [ "@mozilla.org/messengercompose/smtp;1" ] ;
SmtpService = SmtpServiceComponent . getService ( ) ;
Smtp = SmtpService . QueryInterface ( Components . interfaces . nsISmtpService ) ;
DefaultSmtp = Smtp . defaultServer ;
}
catch ( ex )
{
// if we don't have an SMTP server to use I'm not going
// to allow mail features to be enabled for now
// from bug 122651: scenarios 3, and 6
noSmtp ( ) ;
return ;
}
// if we get here that means we must have a default account and default smtp server otherwise
// the thrown exceptions would have been caught.
// from bug 122651 - scenarios 4, 5, 7, and 8
// we'll just use the default account for now.
gMailAccounts = true ;
}
/ * * * * n o M a i l
*
* PURPOSE : function to handle the scenario when mailnews is not installed
*
* /
function noMail ( )
{
// maybe provide a way of telling the user how to get a mailnews build
// this should be a pref with a window that has a checkbox to shut off
// this warning (for people that don't want mailnews installed)
mdebug ( "You don't have mail installed" ) ;
}
/ * * * * n o A c c o u n t
*
* PURPOSE : function to handle the scenario where mailnews is installed but
* an account has not been created
* /
function noAccount ( )
{
2005-11-25 08:16:51 +00:00
// this could call the account wizard, I guess that's a later feature
2002-04-02 18:47:56 +00:00
mdebug ( "You don't have a mail account" ) ;
}
/ * * * * n o S m t p
*
* PURPOSE : function to handle the scenario where mailnews is installed , has
* a default incoming account but does not have an smtp service
*
* XXX : can this happen ?
*
* /
function noSmtp ( )
{
/ * I d o n ' t r e a l l y k n o w i f i t s p o s s i b l e t o c r e a t e t h i s s c e n a r i o , I ' m p u t t i n g i t h e r e f o r
* the sake of completion so that I can say I ' ve at least covered all possible scenarios
* from bug 122651
* /
mdebug ( "You don't have an smtp account" ) ;
}
2002-04-12 20:24:10 +00:00
/ * * * * s e n d E v e n t
*
* PURPOSE : implement iMip : ) - that is send out a calendar event both
* as an attachment of type text / calendar and as text / plain
* /
2002-04-03 16:43:14 +00:00
function sendEvent ( )
{
2004-05-31 13:27:57 +00:00
var CalendarDataFilePath ;
var nsIMsgCompFieldsComponent ;
var nsIMsgCompFields ;
var nsIMsgComposeParamsComponent ;
var nsIMsgComposeParams ;
var nsIMsgComposeServiceComponent ;
var nsIMsgComposeService ;
var nsIMsgCompFormat ;
var nsIMsgCompType ;
var nsIMsgAttachmentComponent ;
var nsIMsgAttachment ;
2002-04-03 16:43:14 +00:00
2004-05-31 13:27:57 +00:00
CalendarDataFilePath = getCalendarDataFilePath ( ) ;
if ( ! CalendarDataFilePath ) {
alert ( "No calendarDataFilePath in calendarMail.js" ) ;
return ;
}
/ * O u t p u t s p l a i n t e x t f o r m e s s a g e b o d y l i k e
*
* Summary : Pre - midnight gathering
* When : Friday , 31 December 1999 11 : 00 PM -- 11 : 45 PM
* Where : City hall
* Organizer : Moz < moz @ mozilla . org >
*
* We will gather and ride over en - masse .
*
* For recurring event , date is next occurrence ( or prev if none ) .
* Date is not repeated if end is same . Time is omitted for all day events .
* /
var CalendarText = "" ;
var EmailBody = "" ;
var Separator = "" ;
2004-06-08 13:10:09 +00:00
var dateFormat = new DateFormater ( ) ;
2004-05-31 13:27:57 +00:00
for ( var i = 0 ; i < gCalendarWindow . EventSelection . selectedEvents . length ; i ++ ) {
var event = gCalendarWindow . EventSelection . selectedEvents [ i ] . clone ( ) ;
2002-10-30 18:15:02 +00:00
2004-05-31 13:27:57 +00:00
if ( event ) {
event . alarm = false ;
if ( event . method == 0 )
event . method = event . ICAL _METHOD _PUBLISH ;
2002-10-30 18:15:02 +00:00
2004-05-31 13:27:57 +00:00
CalendarText += event . getIcalString ( ) ;
var eventDuration = event . end . getTime ( ) - event . start . getTime ( ) ;
2004-06-08 13:10:09 +00:00
var nextStartDate = getNextOrPreviousOccurrence ( event ) ;
var nextEndDate = new Date ( nextStartDate . getTime ( ) + eventDuration ) ;
var when = dateFormat . formatInterval ( nextStartDate , nextEndDate , event . allDay ) ;
2004-05-31 13:27:57 +00:00
EmailBody += Separator ;
Separator = "\n\n" ;
EmailBody += ( emailStringBundle . GetStringFromName ( "Summary" ) + " " + nullToEmpty ( event . title ) + "\n" +
emailStringBundle . GetStringFromName ( "When" ) + " " + when + "\n" +
emailStringBundle . GetStringFromName ( "Where" ) + " " + nullToEmpty ( event . location ) + "\n" +
emailStringBundle . GetStringFromName ( "Organizer" ) + " " + gMailIdentity . fullName + " <" + gMailIdentity . email + ">" + "\n" +
"\n" +
( event . description == null ? "" : event . description + "\n" ) ) ;
}
}
2002-10-30 18:15:02 +00:00
2004-05-31 13:27:57 +00:00
var EmailSubject ;
if ( gCalendarWindow . EventSelection . selectedEvents . length == 1 )
EmailSubject = gCalendarWindow . EventSelection . selectedEvents [ 0 ] . title ;
else
EmailSubject = emailStringBundle . GetStringFromName ( "EmailSubject" ) ;
saveCalendarObject ( CalendarDataFilePath , CalendarText ) ;
// lets open a composer with fields and body prefilled
try {
nsIMsgAttachmentComponent = Components . classes [ "@mozilla.org/messengercompose/attachment;1" ] ;
nsIMsgAttachment = nsIMsgAttachmentComponent . createInstance ( Components . interfaces . nsIMsgAttachment ) ;
nsIMsgAttachment . name = emailStringBundle . GetStringFromName ( "AttachmentName" ) ;
nsIMsgAttachment . contentType = "text/calendar" ;
nsIMsgAttachment . temporary = true ;
nsIMsgAttachment . url = "file://" + CalendarDataFilePath ;
// lets setup the fields for the message
nsIMsgCompFieldsComponent = Components . classes [ "@mozilla.org/messengercompose/composefields;1" ] ;
nsIMsgCompFields = nsIMsgCompFieldsComponent . createInstance ( Components . interfaces . nsIMsgCompFields ) ;
nsIMsgCompFields . useMultipartAlternative = true ;
nsIMsgCompFields . attachVCard = true ;
nsIMsgCompFields . from = gMailIdentity . email ;
nsIMsgCompFields . replyTo = gMailIdentity . replyTo ;
nsIMsgCompFields . subject = EmailSubject ;
nsIMsgCompFields . organization = gMailIdentity . organization ;
nsIMsgCompFields . body = EmailBody ;
nsIMsgCompFields . addAttachment ( nsIMsgAttachment ) ;
/ * l a t e r o n w e m a y b e a b l e t o a d d :
* returnReceipt , attachVCard
* /
// time to handle the message paramaters
nsIMsgComposeParamsComponent = Components . classes [ "@mozilla.org/messengercompose/composeparams;1" ] ;
nsIMsgComposeParams = nsIMsgComposeParamsComponent . createInstance ( Components . interfaces . nsIMsgComposeParams ) ;
nsIMsgComposeParams . composeFields = nsIMsgCompFields ;
nsIMsgCompFormat = Components . interfaces . nsIMsgCompFormat ;
nsIMsgCompType = Components . interfaces . nsIMsgCompType ;
nsIMsgComposeParams . format = nsIMsgCompFormat . PlainText ; // this could be a pref for the user
nsIMsgComposeParams . type = nsIMsgCompType . New ;
// finally lets pop open a composer window after all this work :)
nsIMsgComposeServiceComponent = Components . classes [ "@mozilla.org/messengercompose;1" ] ;
nsIMsgComposeService = nsIMsgComposeServiceComponent . getService ( ) . QueryInterface ( Components . interfaces . nsIMsgComposeService ) ;
nsIMsgComposeService . OpenComposeWindowWithParams ( null , nsIMsgComposeParams ) ;
} catch ( ex ) {
alert ( ex ) ;
mdebug ( "failed to get composer window\nex: " + ex ) ;
}
}
2002-11-27 15:20:28 +00:00
2004-05-31 13:27:57 +00:00
/** from unifinder.js **/
function getNextOrPreviousOccurrence ( calendarEvent )
{
var isValid = false ;
if ( calendarEvent . recur ) {
var now = new Date ( ) ;
var result = new Object ( ) ;
isValid = calendarEvent . getNextRecurrence ( now . getTime ( ) , result ) ;
if ( isValid )
return ( new Date ( result . value ) ) ;
isValid = calendarEvent . getPreviousOccurrence ( now . getTime ( ) , result ) ;
if ( isValid )
return ( new Date ( result . value ) ) ;
}
// !isValid or !calendarEvent.recur
if ( calendarEvent . start != null )
return ( new Date ( calendarEvent . start . getTime ( ) ) ) ;
dump ( "calendarEvent == " + calendarEvent + "\n" ) ;
return null ;
}
2002-10-30 18:15:02 +00:00
2004-05-31 13:27:57 +00:00
/** PRIVATE Return string unless string is null, in which case return "" **/
function nullToEmpty ( string ) {
return string == null ? "" : string ;
}
2002-04-12 20:24:10 +00:00
/ * * * * g e t C a l e n d a r D a t a F i l e P a t h
*
* PURPOSE : get the user ' s current profile directory and use it as a place
* to temporarily store the iCal tempfile that I need to attach
* to the email
* /
function getCalendarDataFilePath ( )
{
var FilePath ;
var DirUtilsComponent ;
var DirUtilsInstance ;
var nsIFile ;
try
{
DirUtilsComponent = Components . classes [ "@mozilla.org/file/directory_service;1" ] ;
DirUtilsInstance = DirUtilsComponent . createInstance ( Components . interfaces . nsIProperties ) ;
nsIFile = Components . interfaces . nsIFile ;
FilePath = DirUtilsInstance . get ( "ProfD" , nsIFile ) . path ;
2002-07-05 18:41:18 +00:00
var aFile = Components . classes [ "@mozilla.org/file/local;1" ] . createInstance ( ) ;
var aLocalFile = aFile . QueryInterface ( Components . interfaces . nsILocalFile ) ;
if ( ! aLocalFile ) return false ;
aLocalFile . initWithPath ( FilePath ) ;
2003-01-03 17:40:18 +00:00
aLocalFile . append ( "Calendar" ) ;
2002-07-05 18:41:18 +00:00
aLocalFile . append ( "tempIcal.ics" ) ;
FilePath = aLocalFile . path ;
2002-04-12 20:24:10 +00:00
}
catch ( ex )
{
mdebug ( "No filepath has been found, ex:\n" + ex ) ;
FilePath = null ;
}
return ( FilePath ) ;
}
/ * * * * s a v e C a l e n d a r O b j e c t
*
* PURPOSE : open a tmp file and write Ical info to it , this has
* to be done until there is a way to pass an ical URL to
* the messenger attachment code
* /
function saveCalendarObject ( FilePath , CalendarStream )
{
2003-03-21 16:11:16 +00:00
var TempFile = new File ( FilePath ) ;
TempFile . open ( "w" , 0644 ) ;
TempFile . write ( CalendarStream ) ;
TempFile . close ( ) ;
return ;
2002-04-12 20:24:10 +00:00
}
2002-10-29 21:27:10 +00:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* AUTHOR : Dan Parent
* DATE : Monday May 22 , 2001
*
* NOTES :
* Example Implementation ( use brackets properly I removed them to keep this short ) :
* function sendMessage ( )
* {
* var EmailSent = sendEmail ( "Here is a subject" , "Here is the body" , "danp@oeone.com" , null , null , "Here is some text" , null , null ) ;
* if ( EmailSent )
* alert ( "Mail should be on it's way" ) ;
* else
* alert ( "OOOoooouuuuuGAAAAHHHH!!!" ) ;
* }
*
* IMPLEMENTATION NOTES
* Attachments are still very untested , as is priority , all arguments to
2003-12-19 15:06:58 +00:00
* this function are strings , with the to , cc and bcc being comma separated
2002-10-29 21:27:10 +00:00
* email addresses .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
function sendEmail ( Subject , Body , To , Cc , Bcc , Attachment , Priority )
{
/* The message composer component */
var MessageComposeComponent = Components . classes [ "@mozilla.org/messengercompose/compose;1" ] ;
var MessageComposeService = MessageComposeComponent . getService ( ) ;
var MessageCompose = MessageComposeService . QueryInterface ( Components . interfaces . nsIMsgCompose ) ;
/* The message composer parameters component, needed by MessageCompose in order to initialize */
var MessageComposeParamsComponent = Components . classes [ "@mozilla.org/messengercompose/composeparams;1" ] ;
var MessageComposeParams = MessageComposeParamsComponent . createInstance ( Components . interfaces . nsIMsgComposeParams ) ;
nsIMsgCompFormat = Components . interfaces . nsIMsgCompFormat ;
MessageComposeParams . format = nsIMsgCompFormat . PlainText ; // this could be a pref for the user
/* The type of delivery to use, we always use send now */
var MessageDeliverMode = Components . interfaces . nsIMsgCompDeliverMode ;
var AccountExists = true ;
if ( ! hasDefaultAccount ( ) )
{
AccountExists = accountSetupError ( ) ;
}
/* The account manager is needed to get the default identity to send the mail out */
var AccountManagerComponent = Components . classes [ "@mozilla.org/messenger/account-manager;1" ] ;
var AccountManagerService = AccountManagerComponent . getService ( ) ;
var AccountManager = AccountManagerService . QueryInterface ( Components . interfaces . nsIMsgAccountManager ) ;
try
{
var Account = AccountManager . defaultAccount ;
}
catch ( e )
{
2003-01-21 18:18:45 +00:00
alert ( "\n------------------\nThere is no default account, and I caught an exception in penemail.js on line 45. " + e + "\n--------------------------------\n" ) ;
2002-10-29 21:27:10 +00:00
AccountExists = accountSetupError ( ) ;
}
if ( ! AccountExists )
{
return ( false ) ;
}
/* try to initialize the message composer, if not return false */
try
{
MessageCompose . Initialize ( null , MessageComposeParams ) ;
}
catch ( Exception )
{
alert ( "can't Initialize" + Exception ) ;
return ( false ) ;
}
/* The composer fields that need to be set in order to properly send an email */
/ * T O D O : s h o u l d t h i s r e a l l y b e s e t b y t h e d e f a u l t a c c o u n t o r i s t h e r e g o i n g t o b e
a system account , or is the system account the default account ? * /
var MessageComposeFields = MessageCompose . compFields ;
if ( Subject == "" )
{
MessageComposeFields . subject = "[no subject]" ;
}
else
{
MessageComposeFields . subject = Subject ;
}
if ( To == "" || To == null )
{
return ( false ) ; // the email *has* to be addressed to somebody
}
else
{
MessageComposeFields . to = To ;
}
if ( Cc != "" && Cc != null )
{
MessageComposeFields . cc = Cc ;
}
if ( Bcc != "" && Bcc != null )
{
MessageComposeFields . bcc = Bcc ;
}
if ( Body != "" && Body != null )
2003-12-19 22:14:32 +00:00
MessageComposeFields . body = "\n" + Body + "\n" ;
2002-10-29 21:27:10 +00:00
if ( Attachment != "" && Attachment != null )
MessageComposeFields . attachments = Attachment ;
if ( Priority != "" && Priority != null )
MessageComposeFields . priority = Priority ;
MessageComposeFields . from = Account . email ;
2003-12-19 22:14:32 +00:00
MessageComposeFields . forcePlainText = true ;
MessageComposeFields . useMultipartAlternative = false ;
2002-10-29 21:27:10 +00:00
/* try to send the email away, don't set a listener, we just trust that it makes it for now :) */
/* TODO: properly set a listener on this action and report errors back to the calling function */
try
{
2004-02-13 19:23:17 +00:00
MessageCompose . SendMsg ( MessageDeliverMode . Now , Account . defaultIdentity , Account . key , null , null ) ;
2002-10-29 21:27:10 +00:00
}
catch ( Exception )
{
alert ( "can't SendMsg" + Exception ) ;
return ( false ) ;
}
/* we made it here so everything must be good unless something happens during the transport of the message */
return ( true ) ;
}
function accountSetupError ( )
{
var rv = confirm ( "You do not have a default email account setup. Click OK and the Account Wizard will be started, otherwise click Cancel." ) ;
if ( rv )
{
launchAccountWizard ( ) ;
}
var AccountManagerComponent = Components . classes [ "@mozilla.org/messenger/account-manager;1" ] ;
var AccountManagerService = AccountManagerComponent . getService ( ) ;
var AccountManager = AccountManagerService . QueryInterface ( Components . interfaces . nsIMsgAccountManager ) ;
try
{
if ( AccountManager . defaultAccount )
{
return ( true ) ;
}
else
{
return ( false ) ;
}
}
catch ( ex )
{
return ( false ) ;
}
}
/ * * * * A D D E D B Y D A N 2 0 0 1 - 1 0 - 2 9 * * * *
*
* RETURNS true if default account , false if no account
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
function hasDefaultAccount ( )
{
try
{
var AccountManagerComponent = Components . classes [ "@mozilla.org/messenger/account-manager;1" ] ;
var AccountManagerService = AccountManagerComponent . getService ( ) ;
var AccountManager = AccountManagerService . QueryInterface ( Components . interfaces . nsIMsgAccountManager ) ;
var DefaultAccount = AccountManager . defaultAccount ;
var DefaultIncomingServer = DefaultAccount . incomingServer ;
2003-01-03 16:02:33 +00:00
emailStringBundle = srGetStrBundle ( "chrome://messenger/locale/messenger.properties" ) ;
var LocalFolders = emailStringBundle . GetStringFromName ( "localFolders" ) ;
if ( DefaultIncomingServer . hostName == LocalFolders )
2002-10-29 21:27:10 +00:00
{
return ( false ) ;
}
else
{
return ( true ) ;
}
}
catch ( ex )
{
return ( false ) ;
}
}
/ * * * * l a u n c h A c c o u n t W i z a r d * * * *
*
* This will launch the Account Wizard from any point the email service can be used
*
* * * * /
function launchAccountWizard ( )
{
window . openDialog ( "chrome://messenger/content/AccountWizard.xul" , "" , "chrome,modal,titlebar,resizable" ) ;
}
2002-04-02 18:47:56 +00:00
/ * * * * m d e b u g
*
* PURPOSE : display debugging messages
*
* /
function mdebug ( message )
{
if ( gMAILDEBUG == 1 )
{
dump ( message + "\n" ) ;
}
else if ( gMAILDEBUG > 1 )
{
alert ( message ) ;
}
}