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