/x11/xcolorsel/

Git repository'/>
aboutsummaryrefslogblamecommitdiffstats
path: root/composer/e-msg-composer.c
blob: f6929710ef92e9ed59705ec9d7d4e6508a8e88a9 (plain) (tree)
1
2
3
4
5
6
7
8
9
  
                                                                


                                                               



                                                                    



                                                                               
  
  
           





                                                        
  





       
                                                                               

                                     

                                                                                

  
                    


                   
                   
                   
                      
                     
                     
                   


                  
 
                 

                    
                           
 


                               
                               
 
                        
 
                                  

                                  
                           
                               
                                  
                          
                                  
                          
 
                                    
                              

                                            





                                            
                                       
                      

                                      
 
                                   

                          
                             
                             
                          
                            
 
                           

                             


                                    
 



                           

            

































                                                          
 

             
                   


                   

                                
                            
                               
                              
                            
                              



                                                         




                                                        

  
               
                      


                        
                                                    






                                                    

  

                                  


                                                               
 
                      
                                                                
 

                                                                                        
 
                                                       
                                                                                              
                                                                               
 




                                                                                                         
 
                     
                                                      

                             
                   
 

                                         
 

                            
 







                                                      
 








                                                           

                   
                            
                                                     
 


                                          
                   
 

                          
 
                                                 

                               
 
                                 



                                         
                                                                                       
                                                        
                                               

                                        
                                                         
                               
 
                                               
                          
 
                       
                                                    
                                          
                                                               
            
                                                      

 

                                                                                             
 
                       
 
                                
                                                    
                                                      
                            
 
                                                                  
                                                         
                            
                                                  
 
                                                                     
                                                    


                                                 
 
                                                  
                                                                                            
                                                         

                            
 
                                                 
 
                                  

 


                                             


                                                

 


                                                           

                                                

                                                          


           

                                                                      


                                                
                          
 
                                                                 


                                              



                                                                   

                            
                                     

 
                                                                              

                          







































































































































































                                                                                                
                         


                                              
 
                              

                                                


                                       
                                                 
                                             
                                          
                                     
                                    
                               
                              
                            
                            
                          
                         
                          





                               
 
                                                                  
 



                                                              
 
                                           
                          
                                                                    

                                               
         
 
                                        
                                                     
                                                       
                                                            

                                                                        
         

                                              


                                                                   
                                                  
                                                           
 


                                                                    
         

                              



                                                                 
 
                           
                                                              
                                                   
                                                             
                                                                                         



                                           
                                                                                                
                                                                
                






                                                                            
 
                                                                  
 
                                                                
                                                                                   
                                                                                
                                                                           

                                         
         
 
                                                             
 
                                                           
                                                                                

                                                 
 

                                                                             
 
                                                       
                                                                                        


                                                                                    
 
                                          
                                          
                                                                 
                                    
 
                                                             
                                        
 



                             
                                                
 

                                                                            
 






                                                                             
 
                                                 
 

                                                                        
                                            
                                                                                    
 
                                                     



                                                                            
 
                                              
                                                                             
                                           
                                                                    
                                                      
                                          
 
                                              
                                                                            
                                          
                                                      
                                          
 



                                                           
                                        
                                                         
 
                                                                  


                                                                                     
                                                                              
 

                                                                                                         
                                                  
                                                                          
                                                  
 
                                                                        
                                                        
 




                                                                        
 
                                                                    
                                                                   
 
                                        


                                                                                         
 

                                                               
 
                                              


                                                                               
                                             
                                                           
                                          
 
                                                                                      
 
                                        




                                                                                                        
 
                                                         
         
 
                                   
 





                                                            
                      







                                                              
      
 






                                                                                                                      
                                                         
                                             
 


                                                                                           


                 
 

                                        

                                                  
                                  
 



                                                                               
                                             
 

                                                                      

                                                                       
                        

                                                                                
                 
 

                                                                      
 


                                                                                                         
 

                                                           
                                               
                         
 
                                                  
                                     
                 
 

                                                                      
 
                                                                                                              
                                                                                  
                                                                                    
 
                                                                              
                                                                                                
                                                    
 
                                                                                  
                                                                                       
 
                                                           
                                                           
                                               
                         

                                                  
                                     
                 

                         

                                                  
                                                                                

                                           
         
 
                      
                                          
                                                  

                                           

                                                                               
                                     

                                                                            
 
                              
                                                                                                                 

                                                                                                                             

                                       
 
                                 
                                                                                                                 

                                                                                                                                   
                                       
                 
 

                                                                      
                                              
 
                                                                        
                                                                   
 
                                                                                            


                                                                                                                                    
                                                                                                          
                                                                                                                                    
                         
 

                                                                                                                      
 

                                                           
                                               
                         
 
                                                  
                                     
                 
 
                                    

                                              
                                                                                             
                                                           
                                                                                                    
 
                                                                        

                                                                                                                            
 

                                                                                                         
 
                                                         

                                               
                                                           
                                                                                       
                 

                         
                                                  

                                                                                           

                                                  

                                          


                                                                                        
                 
         
                     





                                                                                     
                      
             
      

                                                 

                                                      

         
                                                             


                                                           
 
                   
 
           
 
                                          
                                          
 
                                 
 
                                                   

                                                                                     





                                                 

                                                      
         
 
                    

 





































































































                                                                                                                                 
 





                                         
 
                                           



                                        


                       
 
                                            
                       
                         

                                                                                        
                                     
         
 
                                                  
 
                        

                                                                               
 

                                                                
                                          
 
                                                         
         
 


                                                               
 

                                                                         
 




                                                                                            
                                                                                               


                                                                                 
 

                                                                               
 
                                                                                               
                                                                                           
                                                                                                               


                                                                           
 
                                 
 


                                                                                                          
 

                                           
 
                                       
 
                                                            
                                      
                                          
 
                       
 
 

                                                                            
 

                                    
         
 





                                                                             

 

                                         
 


                         
 






                                                        
 
                                       
























                                       
 


                     

                                         
 


                         











                                                                          
                                      

























                                                                            

                                                              
              
                                           
 

                                         
                              
                             
 

                                                                  

                       
                            
 

                                         
                                    
 
                                              
 

                                                                                      
                        
                                                                                                      

                 
                                     


                                    
 
                                                                     


                                                                                                                  
 













                                                                                              
 
                                          
                   
                                          
 

                                                                             
 



                                                                                                         
                                                                                                                 
                                                                                                                           


                                                                                                             
                                                                       
                                                                     
                                                                                                                                


                                                                       
                                     
                            
         
 
                    


           


                                        
 
                                        
                                         
                           
 

                                                        
 
                                            
 


                                                          

                                                           

                                                                           
                                                                  
                             




                    
                                                                                                       
 


                                                      
                                  



                                                                                                           
                                   






                                                                                        





                                                                                                                                          

                 
                                       
         
 
                                                                           
 
                                                     
                                                        

 

                
                     
                                           



                               
 
                                                      
 
                                                                
 
                                                                              
                            
 

                                                                                    
                                    
 
                                                         
                       
                                                            
                                            
 


                                                                      
 


                                                                            
 

                                                        
 


                        
                               
 


                                                                     
 

                                 
 

                               
 






                                                                     
 
                                                         
 
                    

 
           

                                                          
 


                              
 

                                                                               
 

                                 
 





                                                                      
 
                                                                           
 














                                                                           

 


                                                                  
 

                                                                  

                            
 

                     
 

                                                             
 
                                               
 
                    

 



                                                      
 

                          
 
                                                                      
 




                                                 
 
                                                       


           
                                                        
 

                                    
 

                                                              
 



                                                              

 




                        
           


                                                    
 




                                           
 




                                                                                            
 

                                                                                         
 

                                                                              
 

                                                                            
 

                                                                             
 


                                                                   
 

                                           
 



















                                                                             
         
 
                                     
 

                                                                
 
                           
 


                                                                           
                      

                                                                            
                      

                                       
         
 
                                    
 

           
                                                        
 
                                                




                                    
                           


                                      
 

                                                              
 

                            
 




                                                                              
 


                                                         
 


                                                            
 



                                               
 


                                                                        
 






                                                                         




                                                                   


           






































                                                                          

                                                    
 



                                                
 


                                                              
 




                                                                       
 





                                                                       
 

                                       
 
           



                                                        
 


                              
 
                                           
 







                                                                    
 







                                                                
 






                                                                  

         
 

                               
 


                                                                  
 


                       
 



                               
 

                                                                 
 
                                       
 
                                                               
 

                                                                           


           






                                         
 





                                               
                                                
 






                                                                                               
 





                                                                                                       
 







                                                                                     
 

                                                                  
 







                                                                                     
 



                                                            
 

                                                             
 






                                                               
 


                                                                                                                         
 
                                                                                                    
 

                                               
 






                                                                
 
                                                                                           
 
                                          
 



                                                                     
 

                                                       
 

                                                                                       
 

                                
 










                                                                                                     
 































                                                                                                                                             
 









                                                                                                                                    
 

                                                    
 






                                                     
 

                                                         

           
                                                             
 
                                    
 


                                                         


           
                                                             
 
                                    
 


                                                         


           
                                                               
 
                                    
 
                                                            

 






                                                                                            
           
                                                          
 
                                    
 






                                     


           
                                                      
 
                              
 

                                                  

 



                                                                      
 


                                
                               



































































                                                                         
                                                    



                                                            










                                                                       

 
           
                                      
 
                                                         







                                                                          
 



                                                        


           
                                       
 


                                                         
 

                                                         


           
                                        
 
                                                         
 

                                                               



                                                                    
 

                                        
 




                                                                               
 


                                                          
 





                                    
 

                                                      
 
                                                                          
 








                                                             
 








                                                                
 






                                                                           
 

                                                         
 
                                             
 

                    
 





                                                         




                                                
 



                                                           
 





                                                                      
 



                                                     
 
                                                                             






                                                                  

                            
 

                                                                                

 





                                                  
 


                                    
 


                                   
 


                                                                           
 

                                                
 

                                                  
 




                                                                
 
                                                       
 
                                    

 
           






                                                             
 
                               
 

                                                                     
 

                                    
 

                                    
 





                                                
 









                                                                                
 


                                                                                    
 






                                                                                   
         
 
 





                                                  
 


                                                            
 



                                                                   
 


                                                                    
 





                                                   
 


                                                            
 


                                                                    
         
 

                                                                     

 
           
                                                    
 


                               
 






                                                                     
         
 

                                                                      

 
           
                                               
 


                               
 


                                                            
 






                                                                          
 





                                                   
 
                                           
 

                                                      
 

                                                
 





                                                                          
 




                                                                          


           

                                                  
 

                               
 
                                           
 

                                                      
 

                                                
 
                                                          
 



                                                                  
 


                                                                       
 


                                                                             
 

                                                                         
 

                                                                             
         
 


                                                                  
 







                                              
 
                                           
 

                                                          
 


                                                                  
 


                                                             
         
 










                                                                      

 
           

                                                 

                             
 














                                                               
                                                 

                                     

 



                                                   
 

                                                        
 









                                                                          
 


                                                                             

 
           


                                                  
 






                                  
 

                                                                       
 

                                                
 

                                                          
 



                                                             
 



                                                                   
 



                                                                        
 


                                                         
 


                                                        

 

                                                  
 




















































                                                                           

                                              

 
           
                                          
 
                                    

                              
                      
 
                                                               
 
                                           
 


                                                         
                                                                  
                                                                       
 

                                                                             
 
                                   
 



                                                                    
 




















                                                                     


                                                                    
 
                               
 



                                                      
 



                                                                        


                                                                             



















                                                                        
 
                                                   
                                                        
 
                            
 














                                                                         
 
                                                
 
                                                                  



                                                                   
 
 



                              
 












                                                                 
 

                                                                            
         
 
                    

 
                 
 

                                   
 
                               


                                    
 

                                                                       
 
                                 
 




                                                            
 




                                                                 
 




                                                              
 




                                                               
 

                                                           
 
                        

 

                                
                                        





                                                                        
 
              
                                        
 






                                                                        
 


                                                                        
 




                                                                  

         
                                                  
 
                                             
 
                        

 








                                                 
 
                                                                  

 

                                         
 





                                                                      
 

                                                               
 
                                               
 
                     

 



                                                        
 


                                                 
 


                                                   

 
           
                                                          
 


                          
 


                                                                      
 

                                                        
 
                                                                   

 





                                                           
 

                                       
 

                       
 

                                                                             
 









                                                                        

                                                                                     

                                                                                                                           



                                                            
 









                                                                         
 


                                                          
 








                                                                           
 





                                                                            
         
 
 















                                                                           
 

                                                                           
                       
 

                                                                       

 
           


                                                   
 



                                       
 


                                                                 
 

                                                           
 

                              
 
                                                                    
 
                                                                             
 

                                                      
 




















                                                                                                                   
 





                                                                         
 











                                                     
 


                                                                  
 








                                                                         
 
                                                                    
 
                                                                             
 

                                                                              
 



                                                                       
 















                                                                                                                   
 




                                                                         
 
                                       

 
           


                                                        
 











                                                                                
 

                                 
 

                                                                                     
 

                                                   
 
                                                       
 






















                                                                                                                           
 


                              
 


                                                                         

 
           


                                            
 
                       
 
                                                        
 



                                               
 
                                                                    
 

                                 
 

                                                                                     
 

                                                   
 
                                                       
 














                                                                                                                           
 












                                                                                             


           
                                          
 




                                     
 

                                                           
 












                                                                                     
 
                                                                 

 











                                                                        
 









                                                                  
                               



                                      
                               
 


                                                                                                
         
 




                                                                              
 


                                                             

                            
 
                                                           
 




                                                                         
         
 




                                                                                                        
 






                                                                                      
         
 




                                                                
 



                                                                
 

                                                    
 





                                                                                                            
 

                                                                                                         
 




                                                                                                                       
 




                                                                                                             
 

                                                                                                         
 





                                                                                                                        
 


                                                                                            
 


                                                         
 








                                                                               
 


                                                         
 



                                                                               
 

                                                                                      
 


                                                              
 


                                                      
 


                                                          
 



                                                                                
 

                                                                                      
 


                                                                
 







                                                        
 
                                                           
 




                                                                       
 
                              
 


                                   
 



                                                                                        
 

                                                               
 
























                                                                                         
         
 














                                                                                              
 










                                                                                         
         
 






                                                                                            
 











                                                                                                                   
                 





                                                                                       
         
 







                                                                        
 
 

                                       
 

                              
 
                                                        
 
                                           
 
                                                            
 

                                                            
 

                                                              
 

                                                              
 
                                                                         

 










                                                       
 


                                    
 
                                                                     
 

                                                             
 
                                                           
 

                                           
 

                                                                      
 
                                  
 
                        

 
   

























                                                                         

                              
  
                                 

    
                                            
 
                                                        
 
                                                   


   

                              
  
                                                                         

    
                                                  
 
                              
 
                                                        
 

                                           
                                                         














                                                                 
 




                                                                        
 


                                                          
 

                    
 












                                                           
 
                                                           
 
                                
 








                                                                       
 


                        
 

                                                
 


                                                         
 


                                            
 
                                                
 
                                                      
 
                                                                 
                                                           

                                                                        
                                                           

                                                                         
                                                           


                                                                             
                                                           












                                                                                            
                                                           
































                                                                                                         
                                                           

                                                                                      
 
                                         
 


                                        
                                                                        



                                               
 
                     
 


                                                
 


                          
 


                                                                   
 


                                   
 

                                                             
 

                                
 



                                                                                      

 



                                   
 
                                                

                              
 

                                                             
 




                                                          
 

                               
 
                                                                  
                                                                                       

                                       
 










                                                                                                                        

 
   

                               
  

                                                              
    

                                              
 
                               
 
                                                                                  
 


                                         
 
                                      
 

                        
 
   


                                                     
  
                                             
    



                                                     
 

                                                        
 
                                               

 
   



                                                  
  

                                                                   

    


                                                
 
                                                

                                    
                                                        
 
                                                           
 


                                                                                                                                
 



                                            
 

                                                                           
 


                                                                      
 



                                                                       

 
   



                               
  


                                                                  
    



                                                  
 
                                                
 


                                                        
 


                                                                

   




                                                                  
  



                                                                           
    


                                                        
 
                                                        

                                                
 

                                                                 


   


                                 
  
                                                       
    


                                                     
 

                               
 



                                                        
 






                                                                          
 
   


                                           
  

                                                                      
    
                                                                         
 
                            
                                                
 

                                                           
 

                                                            


   


                                                       
  

                                                                
  

                                                                     
    


                                                                  
 



                                                            
                                                
 

                                            
 

                                                                 
 


                                                                            
 


                                                                   
 
                                                                 



                                                                  
 


                                                                       
 





                                                                            
 

                                                                 
 


                                                          
 
                               
 
                    

 
   


                                                  
  


                                                                    
    

                                                                        
 

                                    
                                                
 



                                                            
         
 


                                                          
 





                                                               

   
                              
                                       
  


                                                                          
  
                                                        
    


                                                           
 

                              
 







                                                                                   
 

                                                             
 





                                                                             
 

                                
 
                                          
 



                                                       
 



                                                       
 



                                                          
 



                                                         
 



                                                            
 

                                             
 


                                                                 
 




                                    
 

                                                             
 


                                                                            
 

                                                              
 
















                                                                         
 










                                    
 


                                                             
 


                                                         
 


                                                            
 


                                                           
 


                                                              
 


                                                   
 

                                                        
 

                                                           
 

                                                          
 

                                                             
 

                            
 





                                                              
 
                                                                         
 









                                                              
 


                                                                      
 
                   

 
   
                           
                                       
  

                  
    
                                                     
 



                              
                                                        
 



                                                
                       
 
                                                   
 


                                                                               
 









                                                                                      
 












                                                                                
 




                                                                       

 

                                                
 



                                      
                                                                  
 
                                                           
 






                                                                  
 
                       

 

                                                    
 



                                      
                                                                  
 










                                                                               
 

                       
 
   
                                       
  

                                                      
            

                                                            



                              
 
                                                                  
 




                                                                     
 
                     
 
 

                                                          
 
                                                                  
 
                                                                 


    

                                                           
 
                                                            
 
 
        







                                                       

                                       
                            
 

                                                               


                                                               
                                  










                                                                     
                                                     
         
 
                                       
 
 

                                                     
 


                               
 
                                                      
 


                                                                       
 



                                                  
 




                                                                      
 


                                                                            
 
                                                        

         
                        


    
                                                 
 


                              
 





                                                            
                 
                       
         
 


                                                                  
 



                                                                   
 




                                                                  
                 
 

                                                                
         

 


                                                       
 
                              
 
                                                        
 



                                                    

 
    
                                                    
 
                              
 
                                                        
 
                                           
 







                                                                                  

                 
 



                                                                  

 

                                                        
 
                                                                  
 
                                                                      
 

    

                                                        
 
                                                        
 
                                                   
 



























































































                                                                               
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 *
 * Authors:
 *      Ettore Perazzoli (ettore@ximian.com)
 *      Jeffrey Stedfast (fejj@ximian.com)
 *      Miguel de Icaza  (miguel@ximian.com)
 *      Radek Doulik     (rodo@ximian.com)
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/*

   TODO

   - Somehow users should be able to see if any file (s) are attached even when
     the attachment bar is not shown.

   Should use EventSources to keep track of global changes made to configuration
   values.  Right now it ignores the problem olympically. Miguel.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include <gconf/gconf.h>
#include <gconf/gconf-client.h>

#include <libgnome/gnome-url.h>

#include <glade/glade.h>

#include "e-util/e-dialog-utils.h"
#include "misc/e-charset-picker.h"
#include "misc/e-expander.h"
#include "e-util/e-error.h"
#include "e-util/e-plugin-ui.h"
#include "e-util/e-util-private.h"
#include "e-util/e-util.h"
#include "e-signature-combo-box.h"
#include "shell/e-shell.h"

#include <camel/camel-charset-map.h>
#include <camel/camel-iconv.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-mime-filter-charset.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-stream-fs.h>
#include <camel/camel-mime-filter-tohtml.h>
#include <camel/camel-multipart-signed.h>
#include <camel/camel-multipart-encrypted.h>
#include <camel/camel-string-utils.h>
#include <camel/camel-cipher-context.h>
#if defined (HAVE_NSS)
#include <camel/camel-smime-context.h>
#endif

#include "mail/em-composer-utils.h"
#include "mail/em-popup.h"
#include "mail/em-utils.h"
#include "mail/mail-config.h"
#include "mail/mail-crypto.h"
#include "mail/mail-ops.h"
#include "mail/mail-tools.h"

#include "e-msg-composer.h"
#include "e-attachment.h"
#include "e-attachment-bar.h"
#include "e-composer-autosave.h"
#include "e-composer-private.h"
#include "e-composer-header-table.h"

#ifdef HAVE_XFREE
#include <X11/XF86keysym.h>
#endif

#define d(x)

#define E_MSG_COMPOSER_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MSG_COMPOSER, EMsgComposerPrivate))

#define E_MSG_COMPOSER_VISIBLE_MASK_SENDER \
    (E_MSG_COMPOSER_VISIBLE_FROM | \
     E_MSG_COMPOSER_VISIBLE_REPLYTO)

#define E_MSG_COMPOSER_VISIBLE_MASK_BASIC \
    (E_MSG_COMPOSER_VISIBLE_MASK_SENDER | \
     E_MSG_COMPOSER_VISIBLE_SUBJECT)

#define E_MSG_COMPOSER_VISIBLE_MASK_RECIPIENTS \
    (E_MSG_COMPOSER_VISIBLE_TO | \
     E_MSG_COMPOSER_VISIBLE_CC | \
     E_MSG_COMPOSER_VISIBLE_BCC)

#define E_MSG_COMPOSER_VISIBLE_MASK_MAIL \
    (E_MSG_COMPOSER_VISIBLE_MASK_BASIC | \
     E_MSG_COMPOSER_VISIBLE_MASK_RECIPIENTS)

#define E_MSG_COMPOSER_VISIBLE_MASK_POST \
    (E_MSG_COMPOSER_VISIBLE_MASK_BASIC | \
     E_MSG_COMPOSER_VISIBLE_POSTTO)

typedef enum {
    E_MSG_COMPOSER_VISIBLE_FROM       = (1 << 0),
    E_MSG_COMPOSER_VISIBLE_REPLYTO    = (1 << 1),
    E_MSG_COMPOSER_VISIBLE_TO         = (1 << 2),
    E_MSG_COMPOSER_VISIBLE_CC         = (1 << 3),
    E_MSG_COMPOSER_VISIBLE_BCC        = (1 << 4),
    E_MSG_COMPOSER_VISIBLE_POSTTO     = (1 << 5),
    E_MSG_COMPOSER_VISIBLE_SUBJECT    = (1 << 7)
} EMsgComposerHeaderVisibleFlags;

enum {
    SEND,
    SAVE_DRAFT,
    LAST_SIGNAL
};

enum {
    DND_TYPE_MESSAGE_RFC822,
    DND_TYPE_X_UID_LIST,
    DND_TYPE_TEXT_URI_LIST,
    DND_TYPE_NETSCAPE_URL,
    DND_TYPE_TEXT_VCARD,
    DND_TYPE_TEXT_CALENDAR
};

static GtkTargetEntry drop_types[] = {
    { "message/rfc822", 0, DND_TYPE_MESSAGE_RFC822 },
    { "x-uid-list",     0, DND_TYPE_X_UID_LIST },
    { "text/uri-list",  0, DND_TYPE_TEXT_URI_LIST },
    { "_NETSCAPE_URL",  0, DND_TYPE_NETSCAPE_URL },
    { "text/x-vcard",   0, DND_TYPE_TEXT_VCARD },
    { "text/calendar",  0, DND_TYPE_TEXT_CALENDAR }
};

static struct {
    gchar *target;
    GdkAtom atom;
    guint32 actions;
} drag_info[] = {
    { "message/rfc822", NULL, GDK_ACTION_COPY },
    { "x-uid-list",     NULL, GDK_ACTION_ASK |
                                  GDK_ACTION_MOVE |
                                  GDK_ACTION_COPY },
    { "text/uri-list",  NULL, GDK_ACTION_COPY },
    { "_NETSCAPE_URL",  NULL, GDK_ACTION_COPY },
    { "text/x-vcard",   NULL, GDK_ACTION_COPY },
    { "text/calendar",  NULL, GDK_ACTION_COPY }
};

static gpointer parent_class;
static guint signals[LAST_SIGNAL];

/* All the composer windows open, for bookkeeping purposes.  */
static GSList *all_composers = NULL;

/* local prototypes */
static GList *add_recipients (GList *list, const gchar *recips);

static void handle_mailto (EMsgComposer *composer, const gchar *mailto);
static void handle_uri    (EMsgComposer *composer, const gchar *uri, gboolean html_dnd);

/* used by e_msg_composer_add_message_attachments () */
static void add_attachments_from_multipart (EMsgComposer *composer, CamelMultipart *multipart,
                        gboolean just_inlines, gint depth);

/* used by e_msg_composer_new_with_message () */
static void handle_multipart (EMsgComposer *composer, CamelMultipart *multipart, gint depth);
static void handle_multipart_alternative (EMsgComposer *composer, CamelMultipart *multipart, gint depth);
static void handle_multipart_encrypted (EMsgComposer *composer, CamelMimePart *multipart, gint depth);
static void handle_multipart_signed (EMsgComposer *composer, CamelMultipart *multipart, gint depth);

static EDestination**
destination_list_to_vector_sized (GList *list, gint n)
{
    EDestination **destv;
    gint i = 0;

    if (n == -1)
        n = g_list_length (list);

    if (n == 0)
        return NULL;

    destv = g_new (EDestination *, n + 1);
    while (list != NULL && i < n) {
        destv[i] = E_DESTINATION (list->data);
        list->data = NULL;
        i++;
        list = g_list_next (list);
    }
    destv[i] = NULL;

    return destv;
}

static EDestination**
destination_list_to_vector (GList *list)
{
    return destination_list_to_vector_sized (list, -1);
}

#define LINE_LEN 72

static CamelTransferEncoding
best_encoding (GByteArray *buf, const gchar *charset)
{
    gchar *in, *out, outbuf[256], *ch;
    gsize inlen, outlen;
    gint status, count = 0;
    iconv_t cd;

    if (!charset)
        return -1;

    cd = camel_iconv_open (charset, "utf-8");
    if (cd == (iconv_t) -1)
        return -1;

    in = (gchar *) buf->data;
    inlen = buf->len;
    do {
        out = outbuf;
        outlen = sizeof (outbuf);
        status = camel_iconv (cd, (const gchar **) &in, &inlen, &out, &outlen);
        for (ch = out - 1; ch >= outbuf; ch--) {
            if ((guchar) *ch > 127)
                count++;
        }
    } while (status == (gsize) -1 && errno == E2BIG);
    camel_iconv_close (cd);

    if (status == (gsize) -1 || status > 0)
        return -1;

    if (count == 0)
        return CAMEL_TRANSFER_ENCODING_7BIT;
    else if (count <= buf->len * 0.17)
        return CAMEL_TRANSFER_ENCODING_QUOTEDPRINTABLE;
    else
        return CAMEL_TRANSFER_ENCODING_BASE64;
}

static gchar *
best_charset (GByteArray *buf, const gchar *default_charset, CamelTransferEncoding *encoding)
{
    gchar *charset;

    /* First try US-ASCII */
    *encoding = best_encoding (buf, "US-ASCII");
    if (*encoding == CAMEL_TRANSFER_ENCODING_7BIT)
        return NULL;

    /* Next try the user-specified charset for this message */
    *encoding = best_encoding (buf, default_charset);
    if (*encoding != -1)
        return g_strdup (default_charset);

    /* Now try the user's default charset from the mail config */
    charset = e_composer_get_default_charset ();
    *encoding = best_encoding (buf, charset);
    if (*encoding != -1)
        return charset;

    /* Try to find something that will work */
    if (!(charset = (char *) camel_charset_best ((const gchar *)buf->data, buf->len))) {
        *encoding = CAMEL_TRANSFER_ENCODING_7BIT;
        return NULL;
    }

    *encoding = best_encoding (buf, charset);

    return g_strdup (charset);
}

static void
clear_current_images (EMsgComposer *composer)
{
    EMsgComposerPrivate *p = composer->priv;
    g_list_free (p->current_images);
    p->current_images = NULL;
}

void
e_msg_composer_clear_inlined_table (EMsgComposer *composer)
{
    EMsgComposerPrivate *p = composer->priv;

    g_hash_table_remove_all (p->inline_images);
    g_hash_table_remove_all (p->inline_images_by_url);
}

static void
add_inlined_images (EMsgComposer *composer, CamelMultipart *multipart)
{
    EMsgComposerPrivate *p = composer->priv;

    GList *d = p->current_images;
    GHashTable *added;

    added = g_hash_table_new (g_direct_hash, g_direct_equal);
    while (d) {
        CamelMimePart *part = d->data;

        if (!g_hash_table_lookup (added, part)) {
            camel_multipart_add_part (multipart, part);
            g_hash_table_insert (added, part, part);
        }
        d = d->next;
    }
    g_hash_table_destroy (added);
}

/* These functions builds a CamelMimeMessage for the message that the user has
 * composed in `composer'.
 */

static void
set_recipients_from_destv (CamelMimeMessage *msg,
               EDestination **to_destv,
               EDestination **cc_destv,
               EDestination **bcc_destv,
               gboolean redirect)
{
    CamelInternetAddress *to_addr;
    CamelInternetAddress *cc_addr;
    CamelInternetAddress *bcc_addr;
    CamelInternetAddress *target;
    const gchar *text_addr, *header;
    gboolean seen_hidden_list = FALSE;
    gint i;

    to_addr  = camel_internet_address_new ();
    cc_addr  = camel_internet_address_new ();
    bcc_addr = camel_internet_address_new ();

    for (i = 0; to_destv != NULL && to_destv[i] != NULL; ++i) {
        text_addr = e_destination_get_address (to_destv[i]);

        if (text_addr && *text_addr) {
            target = to_addr;
            if (e_destination_is_evolution_list (to_destv[i])
                && !e_destination_list_show_addresses (to_destv[i])) {
                target = bcc_addr;
                seen_hidden_list = TRUE;
            }

            camel_address_decode (CAMEL_ADDRESS (target), text_addr);
        }
    }

    for (i = 0; cc_destv != NULL && cc_destv[i] != NULL; ++i) {
        text_addr = e_destination_get_address (cc_destv[i]);
        if (text_addr && *text_addr) {
            target = cc_addr;
            if (e_destination_is_evolution_list (cc_destv[i])
                && !e_destination_list_show_addresses (cc_destv[i])) {
                target = bcc_addr;
                seen_hidden_list = TRUE;
            }

            camel_address_decode (CAMEL_ADDRESS (target), text_addr);
        }
    }

    for (i = 0; bcc_destv != NULL && bcc_destv[i] != NULL; ++i) {
        text_addr = e_destination_get_address (bcc_destv[i]);
        if (text_addr && *text_addr) {
            camel_address_decode (CAMEL_ADDRESS (bcc_addr), text_addr);
        }
    }

    header = redirect ? CAMEL_RECIPIENT_TYPE_RESENT_TO : CAMEL_RECIPIENT_TYPE_TO;
    if (camel_address_length (CAMEL_ADDRESS (to_addr)) > 0) {
        camel_mime_message_set_recipients (msg, header, to_addr);
    } else if (seen_hidden_list) {
        camel_medium_set_header (CAMEL_MEDIUM (msg), header, "Undisclosed-Recipient:;");
    }

    header = redirect ? CAMEL_RECIPIENT_TYPE_RESENT_CC : CAMEL_RECIPIENT_TYPE_CC;
    if (camel_address_length (CAMEL_ADDRESS (cc_addr)) > 0) {
        camel_mime_message_set_recipients (msg, header, cc_addr);
    }

    header = redirect ? CAMEL_RECIPIENT_TYPE_RESENT_BCC : CAMEL_RECIPIENT_TYPE_BCC;
    if (camel_address_length (CAMEL_ADDRESS (bcc_addr)) > 0) {
        camel_mime_message_set_recipients (msg, header, bcc_addr);
    }

    camel_object_unref (to_addr);
    camel_object_unref (cc_addr);
    camel_object_unref (bcc_addr);
}

static void
build_message_headers (EMsgComposer *composer,
                       CamelMimeMessage *msg,
                       gboolean redirect)
{
    EComposerHeaderTable *table;
    EAccount *account;
    const gchar *subject;
    const gchar *reply_to;

    g_return_if_fail (E_IS_MSG_COMPOSER (composer));
    g_return_if_fail (CAMEL_IS_MIME_MESSAGE (msg));

    table = e_msg_composer_get_header_table (composer);

    /* Subject: */
    subject = e_composer_header_table_get_subject (table);
    camel_mime_message_set_subject (msg, subject);

    /* From: / Resent-From: */
    account = e_composer_header_table_get_account (table);
    if (account != NULL) {
        CamelInternetAddress *addr;
        const gchar *name = account->id->name;
        const gchar *address = account->id->address;

        addr = camel_internet_address_new ();
        camel_internet_address_add (addr, name, address);

        if (redirect) {
            gchar *value;

            value = camel_address_encode (CAMEL_ADDRESS (addr));
            camel_medium_set_header (
                CAMEL_MEDIUM (msg), "Resent-From", value);
            g_free (value);
        } else
            camel_mime_message_set_from (msg, addr);

        camel_object_unref (addr);
    }

    /* Reply-To: */
    reply_to = e_composer_header_table_get_reply_to (table);
    if (reply_to != NULL && *reply_to != '\0') {
        CamelInternetAddress *addr;

        addr = camel_internet_address_new ();

        if (camel_address_unformat (CAMEL_ADDRESS (addr), reply_to) > 0)
            camel_mime_message_set_reply_to (msg, addr);

        camel_object_unref (addr);
    }

    /* To:, Cc:, Bcc: */
    if (e_composer_header_table_get_header_visible (table, E_COMPOSER_HEADER_TO) ||
        e_composer_header_table_get_header_visible (table, E_COMPOSER_HEADER_CC) ||
        e_composer_header_table_get_header_visible (table, E_COMPOSER_HEADER_BCC)) {
        EDestination **to, **cc, **bcc;

        to = e_composer_header_table_get_destinations_to (table);
        cc = e_composer_header_table_get_destinations_cc (table);
        bcc = e_composer_header_table_get_destinations_bcc (table);

        set_recipients_from_destv (msg, to, cc, bcc, redirect);

        e_destination_freev (to);
        e_destination_freev (cc);
        e_destination_freev (bcc);
    }

    /* X-Evolution-PostTo: */
    if (e_composer_header_table_get_header_visible (table, E_COMPOSER_HEADER_POST_TO)) {
        CamelMedium *medium = CAMEL_MEDIUM (msg);
        const gchar *name = "X-Evolution-PostTo";
        GList *list, *iter;

        camel_medium_remove_header (medium, name);

        list = e_composer_header_table_get_post_to (table);
        for (iter = list; iter != NULL; iter = iter->next) {
            gchar *folder = iter->data;
            camel_medium_add_header (medium, name, folder);
            g_free (folder);
        }
        g_list_free (list);
    }
}

static CamelMimeMessage *
build_message (EMsgComposer *composer,
               gboolean html_content,
               gboolean save_html_object_data)
{
    GtkhtmlEditor *editor;
    EMsgComposerPrivate *p = composer->priv;

    EAttachmentBar *attachment_bar;
    EComposerHeaderTable *table;
    GtkToggleAction *action;
    CamelDataWrapper *plain, *html, *current;
    CamelTransferEncoding plain_encoding;
    const gchar *iconv_charset = NULL;
    GPtrArray *recipients = NULL;
    CamelMultipart *body = NULL;
    CamelContentType *type;
    CamelMimeMessage *new;
    CamelStream *stream;
    CamelMimePart *part;
    CamelException ex;
    GByteArray *data;
    EAccount *account;
    gchar *charset;
    gboolean pgp_sign;
    gboolean pgp_encrypt;
    gboolean smime_sign;
    gboolean smime_encrypt;
    gint i;

    g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), NULL);

    editor = GTKHTML_EDITOR (composer);
    table = e_msg_composer_get_header_table (composer);
    account = e_composer_header_table_get_account (table);
    attachment_bar = E_ATTACHMENT_BAR (p->attachment_bar);

    /* evil kludgy hack for Redirect */
    if (p->redirect) {
        build_message_headers (composer, p->redirect, TRUE);
        camel_object_ref (p->redirect);
        return p->redirect;
    }

    new = camel_mime_message_new ();
    build_message_headers (composer, new, FALSE);
    for (i = 0; i < p->extra_hdr_names->len; i++) {
        camel_medium_add_header (CAMEL_MEDIUM (new),
                     p->extra_hdr_names->pdata[i],
                     p->extra_hdr_values->pdata[i]);
    }

    /* Message Disposition Notification */
    action = GTK_TOGGLE_ACTION (ACTION (REQUEST_READ_RECEIPT));
    if (gtk_toggle_action_get_active (action)) {
        gchar *mdn_address = account->id->reply_to;
        if (!mdn_address || !*mdn_address)
            mdn_address = account->id->address;

        camel_medium_add_header (
            CAMEL_MEDIUM (new),
            "Disposition-Notification-To", mdn_address);
    }

    /* Message Priority */
    action = GTK_TOGGLE_ACTION (ACTION (PRIORITIZE_MESSAGE));
    if (gtk_toggle_action_get_active (action))
        camel_medium_add_header (
            CAMEL_MEDIUM (new), "X-Priority", "1");

    if (p->mime_body) {
        plain_encoding = CAMEL_TRANSFER_ENCODING_7BIT;
        for (i = 0; p->mime_body[i]; i++) {
            if ((guchar) p->mime_body[i] > 127) {
                plain_encoding = CAMEL_TRANSFER_ENCODING_QUOTEDPRINTABLE;
                break;
            }
        }
        data = g_byte_array_new ();
        g_byte_array_append (data, (const guint8 *)p->mime_body, strlen (p->mime_body));
        type = camel_content_type_decode (p->mime_type);
    } else {
        gchar *text;
        gsize length;

        data = g_byte_array_new ();
        text = gtkhtml_editor_get_text_plain (editor, &length);
        g_byte_array_append (data, (guint8 *) text, (guint) length);
        g_free (text);

        /* FIXME: we may want to do better than this... */

        type = camel_content_type_new ("text", "plain");
        if ((charset = best_charset (data, p->charset, &plain_encoding))) {
            camel_content_type_set_param (type, "charset", charset);
            iconv_charset = camel_iconv_charset_name (charset);
            g_free (charset);
        }
    }

    stream = camel_stream_mem_new_with_byte_array (data);

    /* convert the stream to the appropriate charset */
    if (iconv_charset && g_ascii_strcasecmp (iconv_charset, "UTF-8") != 0) {
        CamelStreamFilter *filter_stream;
        CamelMimeFilterCharset *filter;

        filter_stream = camel_stream_filter_new_with_stream (stream);
        camel_object_unref (stream);

        stream = (CamelStream *) filter_stream;
        filter = camel_mime_filter_charset_new_convert ("UTF-8", iconv_charset);
        camel_stream_filter_add (filter_stream, (CamelMimeFilter *) filter);
        camel_object_unref (filter);
    }

    /* construct the content object */
    plain = camel_data_wrapper_new ();
    camel_data_wrapper_construct_from_stream (plain, stream);
    camel_object_unref (stream);

    camel_data_wrapper_set_mime_type_field (plain, type);
    camel_content_type_unref (type);

    if (html_content) {
        gchar *text;
        gsize length;

        clear_current_images (composer);

        if (save_html_object_data)
            gtkhtml_editor_run_command (editor, "save-data-on");

        data = g_byte_array_new ();
        text = gtkhtml_editor_get_text_html (editor, &length);
        g_byte_array_append (data, (guint8 *) text, (guint) length);
        g_free (text);

        if (save_html_object_data)
            gtkhtml_editor_run_command (editor, "save-data-off");

        html = camel_data_wrapper_new ();

        stream = camel_stream_mem_new_with_byte_array (data);
        camel_data_wrapper_construct_from_stream (html, stream);
        camel_object_unref (stream);
        camel_data_wrapper_set_mime_type (html, "text/html; charset=utf-8");

        /* Build the multipart/alternative */
        body = camel_multipart_new ();
        camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (body),
                          "multipart/alternative");
        camel_multipart_set_boundary (body, NULL);

        part = camel_mime_part_new ();
        camel_medium_set_content_object (CAMEL_MEDIUM (part), plain);
        camel_object_unref (plain);
        camel_mime_part_set_encoding (part, plain_encoding);
        camel_multipart_add_part (body, part);
        camel_object_unref (part);

        part = camel_mime_part_new ();
        camel_medium_set_content_object (CAMEL_MEDIUM (part), html);
        camel_object_unref (html);
        camel_multipart_add_part (body, part);
        camel_object_unref (part);

        /* If there are inlined images, construct a
         * multipart/related containing the
         * multipart/alternative and the images.
         */
        if (p->current_images) {
            CamelMultipart *html_with_images;

            html_with_images = camel_multipart_new ();
            camel_data_wrapper_set_mime_type (
                CAMEL_DATA_WRAPPER (html_with_images),
                "multipart/related; type=\"multipart/alternative\"");
            camel_multipart_set_boundary (html_with_images, NULL);

            part = camel_mime_part_new ();
            camel_medium_set_content_object (CAMEL_MEDIUM (part), CAMEL_DATA_WRAPPER (body));
            camel_object_unref (body);
            camel_multipart_add_part (html_with_images, part);
            camel_object_unref (part);

            add_inlined_images (composer, html_with_images);
            clear_current_images (composer);

            current = CAMEL_DATA_WRAPPER (html_with_images);
        } else
            current = CAMEL_DATA_WRAPPER (body);
    } else
        current = plain;

    if (e_attachment_bar_get_num_attachments (attachment_bar)) {
        CamelMultipart *multipart = camel_multipart_new ();

        if (p->is_alternative) {
            camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (multipart),
                              "multipart/alternative");
        }

        /* Generate a random boundary. */
        camel_multipart_set_boundary (multipart, NULL);

        part = camel_mime_part_new ();
        camel_medium_set_content_object (CAMEL_MEDIUM (part), current);
        if (current == plain)
            camel_mime_part_set_encoding (part, plain_encoding);
        camel_object_unref (current);
        camel_multipart_add_part (multipart, part);
        camel_object_unref (part);

        e_attachment_bar_to_multipart (attachment_bar, multipart, p->charset);

        if (p->is_alternative) {
            for (i = camel_multipart_get_number (multipart); i > 1; i--) {
                part = camel_multipart_get_part (multipart, i - 1);
                camel_medium_remove_header (CAMEL_MEDIUM (part), "Content-Disposition");
            }
        }

        current = CAMEL_DATA_WRAPPER (multipart);
    }

    camel_exception_init (&ex);

    action = GTK_TOGGLE_ACTION (ACTION (PGP_SIGN));
    pgp_sign = gtk_toggle_action_get_active (action);

    action = GTK_TOGGLE_ACTION (ACTION (PGP_ENCRYPT));
    pgp_encrypt = gtk_toggle_action_get_active (action);

#if defined (HAVE_NSS)
    action = GTK_TOGGLE_ACTION (ACTION (SMIME_SIGN));
    smime_sign = gtk_toggle_action_get_active (action);

    action = GTK_TOGGLE_ACTION (ACTION (SMIME_ENCRYPT));
    smime_encrypt = gtk_toggle_action_get_active (action);
#else
    smime_sign = FALSE;
    smime_encrypt = FALSE;
#endif

    /* Setup working recipient list if we're encrypting */
    if (pgp_encrypt || smime_encrypt) {
        gint j;
        const gchar *types[] = { CAMEL_RECIPIENT_TYPE_TO, CAMEL_RECIPIENT_TYPE_CC, CAMEL_RECIPIENT_TYPE_BCC };

        recipients = g_ptr_array_new ();
        for (i = 0; i < G_N_ELEMENTS (types); i++) {
            const CamelInternetAddress *addr;
            const gchar *address;

            addr = camel_mime_message_get_recipients (new, types[i]);
            for (j=0;camel_internet_address_get (addr, j, NULL, &address); j++)
                g_ptr_array_add (recipients, g_strdup (address));

        }
    }

    if (pgp_sign || pgp_encrypt) {
        const gchar *pgp_userid;
        CamelInternetAddress *from = NULL;
        CamelCipherContext *cipher;
        EAccount *account;

        part = camel_mime_part_new ();
        camel_medium_set_content_object (CAMEL_MEDIUM (part), current);
        if (current == plain)
            camel_mime_part_set_encoding (part, plain_encoding);
        camel_object_unref (current);

        account = e_composer_header_table_get_account (table);

        if (account && account->pgp_key && *account->pgp_key) {
            pgp_userid = account->pgp_key;
        } else {
            from = e_msg_composer_get_from (composer);
            camel_internet_address_get (from, 0, NULL, &pgp_userid);
        }

        if (pgp_sign) {
            CamelMimePart *npart = camel_mime_part_new ();

            cipher = mail_crypto_get_pgp_cipher_context (account);
            camel_cipher_sign (cipher, pgp_userid, CAMEL_CIPHER_HASH_SHA1, part, npart, &ex);
            camel_object_unref (cipher);

            if (camel_exception_is_set (&ex)) {
                camel_object_unref (npart);
                goto exception;
            }

            camel_object_unref (part);
            part = npart;
        }

        if (pgp_encrypt) {
            CamelMimePart *npart = camel_mime_part_new ();

            /* check to see if we should encrypt to self, NB gets removed immediately after use */
            if (account && account->pgp_encrypt_to_self && pgp_userid)
                g_ptr_array_add (recipients, g_strdup (pgp_userid));

            cipher = mail_crypto_get_pgp_cipher_context (account);
            camel_cipher_encrypt (cipher, pgp_userid, recipients, part, npart, &ex);
            camel_object_unref (cipher);

            if (account && account->pgp_encrypt_to_self && pgp_userid)
                g_ptr_array_set_size (recipients, recipients->len - 1);

            if (camel_exception_is_set (&ex)) {
                camel_object_unref (npart);
                goto exception;
            }

            camel_object_unref (part);
            part = npart;
        }

        if (from)
            camel_object_unref (from);

        current = camel_medium_get_content_object (CAMEL_MEDIUM (part));
        camel_object_ref (current);
        camel_object_unref (part);
    }

#if defined (HAVE_NSS)
    if (smime_sign || smime_encrypt) {
        CamelInternetAddress *from = NULL;
        CamelCipherContext *cipher;

        part = camel_mime_part_new ();
        camel_medium_set_content_object ((CamelMedium *)part, current);
        if (current == plain)
            camel_mime_part_set_encoding (part, plain_encoding);
        camel_object_unref (current);

        if (smime_sign
            && (account == NULL || account->smime_sign_key == NULL || account->smime_sign_key[0] == 0)) {
            camel_exception_set (&ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Cannot sign outgoing message: No signing certificate set for this account"));
            goto exception;
        }

        if (smime_encrypt
            && (account == NULL || account->smime_sign_key == NULL || account->smime_sign_key[0] == 0)) {
            camel_exception_set (&ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Cannot encrypt outgoing message: No encryption certificate set for this account"));
            goto exception;
        }

        if (smime_sign) {
            CamelMimePart *npart = camel_mime_part_new ();
            CamelSession *session;

            session = e_msg_composer_get_session (composer);
            cipher = camel_smime_context_new (session);

            /* if we're also encrypting, envelope-sign rather than clear-sign */
            if (smime_encrypt) {
                camel_smime_context_set_sign_mode ((CamelSMIMEContext *)cipher, CAMEL_SMIME_SIGN_ENVELOPED);
                camel_smime_context_set_encrypt_key ((CamelSMIMEContext *)cipher, TRUE, account->smime_encrypt_key);
            } else if (account && account->smime_encrypt_key && *account->smime_encrypt_key) {
                camel_smime_context_set_encrypt_key ((CamelSMIMEContext *)cipher, TRUE, account->smime_encrypt_key);
            }

            camel_cipher_sign (cipher, account->smime_sign_key, CAMEL_CIPHER_HASH_SHA1, part, npart, &ex);
            camel_object_unref (cipher);

            if (camel_exception_is_set (&ex)) {
                camel_object_unref (npart);
                goto exception;
            }

            camel_object_unref (part);
            part = npart;
        }

        if (smime_encrypt) {
            CamelSession *session;

            /* check to see if we should encrypt to self, NB removed after use */
            if (account->smime_encrypt_to_self)
                g_ptr_array_add (recipients, g_strdup (account->smime_encrypt_key));

            session = e_msg_composer_get_session (composer);
            cipher = camel_smime_context_new (session);
            camel_smime_context_set_encrypt_key ((CamelSMIMEContext *)cipher, TRUE, account->smime_encrypt_key);

            camel_cipher_encrypt (cipher, NULL, recipients, part, (CamelMimePart *)new, &ex);
            camel_object_unref (cipher);

            if (camel_exception_is_set (&ex))
                goto exception;

            if (account->smime_encrypt_to_self)
                g_ptr_array_set_size (recipients, recipients->len - 1);
        }

        if (from)
            camel_object_unref (from);

        /* we replaced the message directly, we don't want to do reparenting foo */
        if (smime_encrypt) {
            camel_object_unref (part);
            goto skip_content;
        } else {
            current = camel_medium_get_content_object ((CamelMedium *)part);
            camel_object_ref (current);
            camel_object_unref (part);
        }
    }
#endif /* HAVE_NSS */

    camel_medium_set_content_object (CAMEL_MEDIUM (new), current);
    if (current == plain)
        camel_mime_part_set_encoding (CAMEL_MIME_PART (new), plain_encoding);
    camel_object_unref (current);

#if defined (HAVE_NSS)
skip_content:
#endif
    if (recipients) {
        for (i=0; i<recipients->len; i++)
            g_free (recipients->pdata[i]);
        g_ptr_array_free (recipients, TRUE);
    }

    /* Attach whether this message was written in HTML */
    camel_medium_set_header (
        CAMEL_MEDIUM (new), "X-Evolution-Format",
        html_content ? "text/html" : "text/plain");

    return new;

 exception:

    if (part != CAMEL_MIME_PART (new))
        camel_object_unref (part);

    camel_object_unref (new);

    if (ex.id != CAMEL_EXCEPTION_USER_CANCEL) {
        e_error_run ((GtkWindow *)composer, "mail-composer:no-build-message",
                camel_exception_get_description (&ex), NULL);
    }

    camel_exception_clear (&ex);

    if (recipients) {
        for (i=0; i<recipients->len; i++)
            g_free (recipients->pdata[i]);
        g_ptr_array_free (recipients, TRUE);
    }

    return NULL;
}

/* Attachment Bar */

static void
emcab_add (EPopup *ep, EPopupItem *item, gpointer data)
{
    GtkWidget *widget = data;
    GtkWidget *composer;

    composer = gtk_widget_get_toplevel (widget);
    gtk_action_activate (ACTION (ATTACH));
}

static void
emcab_properties (EPopup *ep, EPopupItem *item, gpointer data)
{
    EAttachmentBar *attachment_bar = data;

    e_attachment_bar_edit_selected (attachment_bar);
}

static void
emcab_remove (EPopup *ep, EPopupItem *item, gpointer data)
{
    EAttachmentBar *attachment_bar = data;

    e_attachment_bar_remove_selected (attachment_bar);
}

static void
emcab_popup_position (GtkMenu *menu, int *x, int *y, gboolean *push_in, gpointer user_data)
{
    GtkWidget *widget = user_data;
    GnomeIconList *icon_list = user_data;
    GList *selection;
    GnomeCanvasPixbuf *image;

    gdk_window_get_origin (widget->window, x, y);

    selection = gnome_icon_list_get_selection (icon_list);