Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / java / Newsgroup / MimeConfiguration.java
blob03b9648343846faeb8bb6415b8b5c22ce5ef24e7
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 {
35 // Office Installation path
36 private static String instPath = "";
39 public static boolean createFiles(XScriptContext xsc) {
40 try {
41 XComponentContext xcc = xsc.getComponentContext();
42 XMultiComponentFactory xmf = xcc.getServiceManager();
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)");
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();
59 writeMailCap();
60 writeMimeTypes();
62 // ToDo: include status feedback to StatusWindow
63 return true;
69 private static void writeMailCap() {
70 String mailcapPath = getConfigDir() + System.getProperty("file.separator") +
71 "mailcap";
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();
79 for (int i = 0; i < lines.length; i++) {
80 out.write(lines[i], 0, lines[i].length());
83 out.close();
84 } else {
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();
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"
116 return mailcapText;
121 private static void writeMimeTypes() {
122 String mimetypesPath = getConfigDir() + System.getProperty("file.separator") +
123 "mimetypes.default";
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();
131 for (int i = 0; i < lines.length; i++) {
132 out.write(lines[i], 0, lines[i].length());
135 out.close();
136 } else {
139 MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap(mimetypesPath);
140 FileTypeMap.setDefaultFileTypeMap(mimeTypes);
141 } catch (IOException ioe) {
142 ioe.printStackTrace();
143 } catch (Exception e) {
144 e.printStackTrace();
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"
177 return mimesText;
181 private static String getConfigDir() {
182 // mailcap file must be written to the Office user/config directory
184 // instPath is a URL, needs to be converted to a system pathname
185 String config = instPath + "/user/config";
186 String configNonURL = "";
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());
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;
201 } else {
202 // Unix/Linux
203 // removes "file://"
204 int start = 7;
205 configNonURL = config.substring(start, config.length());
208 return configNonURL;