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