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