update credits
[LibreOffice.git] / scripting / examples / java / Newsgroup / MimeConfiguration.java
blobd2cce9396c52c9558e9e35195ad5d382ef27cb37
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org.libreoffice.example.java_scripts;
21 import com.sun.star.uno.UnoRuntime;
22 import com.sun.star.lang.XMultiComponentFactory;
23 import com.sun.star.uno.XComponentContext;
24 import com.sun.star.script.framework.runtime.XScriptContext;
25 import com.sun.star.util.XStringSubstitution;
27 import javax.mail.*;
28 import javax.activation.*;
30 import java.io.*;
33 public class MimeConfiguration
36 // Office Installation path
37 private static String instPath = "";
40 public static boolean createFiles( XScriptContext xsc )
42 try
44 XComponentContext xcc = xsc.getComponentContext();
45 XMultiComponentFactory xmf = xcc.getServiceManager();
47 Object pathSub = xmf.createInstanceWithContext( "com.sun.star.comp.framework.PathSubstitution", xcc );
48 XStringSubstitution stringSub = ( XStringSubstitution ) UnoRuntime.queryInterface( XStringSubstitution.class, pathSub );
49 instPath = stringSub.getSubstituteVariableValue( "$(inst)" );
52 catch( com.sun.star.beans.UnknownPropertyException upe )
54 System.out.println( "com.sun.star.beans.UnknownPropertyException" );
55 upe.printStackTrace();
57 catch( com.sun.star.uno.Exception e )
59 System.out.println( "com.sun.star.uno.Exception" );
60 e.printStackTrace();
63 writeMailCap();
64 writeMimeTypes();
66 // ToDo: include status feedback to StatusWindow
67 return true;
73 private static void writeMailCap()
75 String mailcapPath = getConfigDir() + System.getProperty( "file.separator" ) + "mailcap";
77 try
79 if( ! new File( java.net.URLDecoder.decode( mailcapPath ) ).exists() )
81 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
82 File mailcapFile = new File( mailcapPath );
83 FileWriter out = new FileWriter( mailcapFile );
84 String[] lines = getMailcapText();
85 for( int i=0; i<lines.length; i++ )
87 out.write( lines[i], 0, lines[i].length() );
89 out.close();
91 else
93 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
98 // use prog dir, if not there then java.io to create/write new file
99 MailcapCommandMap map = new MailcapCommandMap( mailcapPath );
100 CommandMap.setDefaultCommandMap ( map );
102 catch( IOException ioe )
104 ioe.printStackTrace();
106 catch( Exception e )
108 e.printStackTrace();
113 private static String[] getMailcapText()
115 String[] mailcapText = {
116 "#\n",
117 "# Default mailcap file for the JavaMail System.\n",
118 "#\n",
119 "# JavaMail content-handlers:\n",
120 "#\n",
121 "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n",
122 "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n",
123 "text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n",
124 "image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n",
125 "image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n",
126 "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n",
127 "message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n"
130 return mailcapText;
135 private static void writeMimeTypes()
137 String mimetypesPath = getConfigDir() + System.getProperty( "file.separator" ) + "mimetypes.default";
141 if( ! new File( java.net.URLDecoder.decode( mimetypesPath ) ).exists() )
143 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
144 File mimetypesFile = new File( mimetypesPath );
145 FileWriter out = new FileWriter( mimetypesFile );
146 String[] lines = getMimeTypesText();
147 for( int i=0; i<lines.length; i++ )
149 out.write( lines[i], 0, lines[i].length() );
151 out.close();
153 else
155 //System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
158 MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap( mimetypesPath );
159 FileTypeMap.setDefaultFileTypeMap( mimeTypes );
161 catch( IOException ioe )
163 ioe.printStackTrace();
165 catch( Exception e )
167 e.printStackTrace();
172 private static String[] getMimeTypesText()
174 String[] mimesText = {
175 "#\n",
176 "# A simple, old format, mime.types file\n",
177 "#\n",
178 "text/html html htm HTML HTM\n",
179 "text/plain txt text TXT TEXT\n",
180 "image/gif gif GIF\n",
181 "image/ief ief\n",
182 "image/jpeg jpeg jpg jpe JPG\n",
183 "image/tiff tiff tif\n",
184 "image/x-xwindowdump xwd\n",
185 "application/postscript ai eps ps\n",
186 "application/rtf rtf\n",
187 "application/x-tex tex\n",
188 "application/x-texinfo texinfo texi\n",
189 "application/x-troff t tr roff\n",
190 "audio/basic au\n",
191 "audio/midi midi mid\n",
192 "audio/x-aifc aifc\n",
193 "audio/x-aiff aif aiff\n",
194 "audio/x-mpeg mpeg mpg\n",
195 "audio/x-wav wav\n",
196 "video/mpeg mpeg mpg mpe\n",
197 "video/quicktime qt mov\n",
198 "video/x-msvideo avi\n"
201 return mimesText;
205 private static String getConfigDir()
207 // mailcap file must be written to the Office user/config directory
209 // instPath is a URL, needs to be converted to a system pathname
210 String config = instPath + "/user/config";
211 String configNonURL = "";
213 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
215 // Windows
216 // removes "file:///"
217 int start = 8;
218 configNonURL = config.substring( start, config.length() );
219 // Convert forward to back-slashes
220 while( configNonURL.indexOf( "/" ) != -1 )
222 int fSlash = configNonURL.indexOf( "/" );
223 String firstPart = configNonURL.substring( 0, fSlash );
224 String secondPart = configNonURL.substring( fSlash + 1, configNonURL.length() );
225 configNonURL = firstPart + "\\" + secondPart;
228 else
230 // Unix/Linux
231 // removes "file://"
232 int start = 7;
233 configNonURL = config.substring( start, config.length() );
236 return configNonURL;