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