1*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 2*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 3*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 4*cdf0e10cSrcweir import com.sun.star.script.framework.runtime.XScriptContext; 5*cdf0e10cSrcweir import com.sun.star.util.XStringSubstitution; 6*cdf0e10cSrcweir 7*cdf0e10cSrcweir import javax.mail.*; 8*cdf0e10cSrcweir import javax.activation.*; 9*cdf0e10cSrcweir 10*cdf0e10cSrcweir import java.io.*; 11*cdf0e10cSrcweir 12*cdf0e10cSrcweir 13*cdf0e10cSrcweir public class MimeConfiguration 14*cdf0e10cSrcweir { 15*cdf0e10cSrcweir 16*cdf0e10cSrcweir // Office Installation path 17*cdf0e10cSrcweir private static String instPath = ""; 18*cdf0e10cSrcweir 19*cdf0e10cSrcweir 20*cdf0e10cSrcweir public static boolean createFiles( XScriptContext xsc ) 21*cdf0e10cSrcweir { 22*cdf0e10cSrcweir try 23*cdf0e10cSrcweir { 24*cdf0e10cSrcweir XComponentContext xcc = xsc.getComponentContext(); 25*cdf0e10cSrcweir XMultiComponentFactory xmf = xcc.getServiceManager(); 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir Object pathSub = xmf.createInstanceWithContext( "com.sun.star.comp.framework.PathSubstitution", xcc ); 28*cdf0e10cSrcweir XStringSubstitution stringSub = ( XStringSubstitution ) UnoRuntime.queryInterface( XStringSubstitution.class, pathSub ); 29*cdf0e10cSrcweir instPath = stringSub.getSubstituteVariableValue( "$(inst)" ); 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir } 32*cdf0e10cSrcweir catch( com.sun.star.beans.UnknownPropertyException upe ) 33*cdf0e10cSrcweir { 34*cdf0e10cSrcweir System.out.println( "com.sun.star.beans.UnknownPropertyException" ); 35*cdf0e10cSrcweir upe.printStackTrace(); 36*cdf0e10cSrcweir } 37*cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir System.out.println( "com.sun.star.uno.Exception" ); 40*cdf0e10cSrcweir e.printStackTrace(); 41*cdf0e10cSrcweir } 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir writeMailCap(); 44*cdf0e10cSrcweir writeMimeTypes(); 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // ToDo: include status feedback to StatusWindow 47*cdf0e10cSrcweir return true; 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir private static void writeMailCap() 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir String mailcapPath = getConfigDir() + System.getProperty( "file.separator" ) + "mailcap"; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir try 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir if( ! new File( java.net.URLDecoder.decode( mailcapPath ) ).exists() ) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) ); 62*cdf0e10cSrcweir File mailcapFile = new File( mailcapPath ); 63*cdf0e10cSrcweir FileWriter out = new FileWriter( mailcapFile ); 64*cdf0e10cSrcweir String[] lines = getMailcapText(); 65*cdf0e10cSrcweir for( int i=0; i<lines.length; i++ ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir out.write( lines[i], 0, lines[i].length() ); 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir out.close(); 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir else 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) ); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir // use prog dir, if not there then java.io to create/write new file 79*cdf0e10cSrcweir MailcapCommandMap map = new MailcapCommandMap( mailcapPath ); 80*cdf0e10cSrcweir CommandMap.setDefaultCommandMap ( map ); 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir catch( IOException ioe ) 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir ioe.printStackTrace(); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir catch( Exception e ) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir e.printStackTrace(); 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir private static String[] getMailcapText() 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir String[] mailcapText = { 96*cdf0e10cSrcweir "#\n", 97*cdf0e10cSrcweir "# Default mailcap file for the JavaMail System.\n", 98*cdf0e10cSrcweir "#\n", 99*cdf0e10cSrcweir "# JavaMail content-handlers:\n", 100*cdf0e10cSrcweir "#\n", 101*cdf0e10cSrcweir "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n", 102*cdf0e10cSrcweir "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n", 103*cdf0e10cSrcweir "text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n", 104*cdf0e10cSrcweir "image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n", 105*cdf0e10cSrcweir "image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n", 106*cdf0e10cSrcweir "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n", 107*cdf0e10cSrcweir "message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n" 108*cdf0e10cSrcweir }; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir return mailcapText; 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir private static void writeMimeTypes() 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir String mimetypesPath = getConfigDir() + System.getProperty( "file.separator" ) + "mimetypes.default"; 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir try 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir if( ! new File( java.net.URLDecoder.decode( mimetypesPath ) ).exists() ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) ); 124*cdf0e10cSrcweir File mimetypesFile = new File( mimetypesPath ); 125*cdf0e10cSrcweir FileWriter out = new FileWriter( mimetypesFile ); 126*cdf0e10cSrcweir String[] lines = getMimeTypesText(); 127*cdf0e10cSrcweir for( int i=0; i<lines.length; i++ ) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir out.write( lines[i], 0, lines[i].length() ); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir out.close(); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir else 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) ); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap( mimetypesPath ); 139*cdf0e10cSrcweir FileTypeMap.setDefaultFileTypeMap( mimeTypes ); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir catch( IOException ioe ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir ioe.printStackTrace(); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir catch( Exception e ) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir e.printStackTrace(); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir private static String[] getMimeTypesText() 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir String[] mimesText = { 155*cdf0e10cSrcweir "#\n", 156*cdf0e10cSrcweir "# A simple, old format, mime.types file\n", 157*cdf0e10cSrcweir "#\n", 158*cdf0e10cSrcweir "text/html html htm HTML HTM\n", 159*cdf0e10cSrcweir "text/plain txt text TXT TEXT\n", 160*cdf0e10cSrcweir "image/gif gif GIF\n", 161*cdf0e10cSrcweir "image/ief ief\n", 162*cdf0e10cSrcweir "image/jpeg jpeg jpg jpe JPG\n", 163*cdf0e10cSrcweir "image/tiff tiff tif\n", 164*cdf0e10cSrcweir "image/x-xwindowdump xwd\n", 165*cdf0e10cSrcweir "application/postscript ai eps ps\n", 166*cdf0e10cSrcweir "application/rtf rtf\n", 167*cdf0e10cSrcweir "application/x-tex tex\n", 168*cdf0e10cSrcweir "application/x-texinfo texinfo texi\n", 169*cdf0e10cSrcweir "application/x-troff t tr roff\n", 170*cdf0e10cSrcweir "audio/basic au\n", 171*cdf0e10cSrcweir "audio/midi midi mid\n", 172*cdf0e10cSrcweir "audio/x-aifc aifc\n", 173*cdf0e10cSrcweir "audio/x-aiff aif aiff\n", 174*cdf0e10cSrcweir "audio/x-mpeg mpeg mpg\n", 175*cdf0e10cSrcweir "audio/x-wav wav\n", 176*cdf0e10cSrcweir "video/mpeg mpeg mpg mpe\n", 177*cdf0e10cSrcweir "video/quicktime qt mov\n", 178*cdf0e10cSrcweir "video/x-msvideo avi\n" 179*cdf0e10cSrcweir }; 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir return mimesText; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir private static String getConfigDir() 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir // mailcap file must be written to the Office user/config directory 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir // instPath is a URL, needs to be converted to a system pathname 190*cdf0e10cSrcweir String config = instPath + "/user/config"; 191*cdf0e10cSrcweir String configNonURL = ""; 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 ) 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir // Windows 196*cdf0e10cSrcweir // removes "file:///" 197*cdf0e10cSrcweir int start = 8; 198*cdf0e10cSrcweir configNonURL = config.substring( start, config.length() ); 199*cdf0e10cSrcweir // Convert forward to back-slashes 200*cdf0e10cSrcweir while( configNonURL.indexOf( "/" ) != -1 ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir int fSlash = configNonURL.indexOf( "/" ); 203*cdf0e10cSrcweir String firstPart = configNonURL.substring( 0, fSlash ); 204*cdf0e10cSrcweir String secondPart = configNonURL.substring( fSlash + 1, configNonURL.length() ); 205*cdf0e10cSrcweir configNonURL = firstPart + "\\" + secondPart; 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir else 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir // Unix/Linux 211*cdf0e10cSrcweir // removes "file://" 212*cdf0e10cSrcweir int start = 7; 213*cdf0e10cSrcweir configNonURL = config.substring( start, config.length() ); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir return configNonURL; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir } 220