update credits
[LibreOffice.git] / scripting / examples / java / Newsgroup / SubscribedNewsgroups.java
blobcba9d62f59dc3c0c7bb26e4f00e9b1c265c632f6
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 java.io.*;
22 import java.util.Vector;
25 public class SubscribedNewsgroups {
28 private static NewsGroup[] allSubscribed = null;
29 private static boolean windows = false;
31 public static void main( String[] args ) {
32 // Test the class
33 SubscribedNewsgroups subscribed = new SubscribedNewsgroups();
35 NewsGroup allGroups[] = subscribed.getNewsGroups();
37 if( allGroups == null )
39 System.out.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files");
41 else
43 for( int i=0; i < allGroups.length; i++ )
45 System.out.println( "Hostname is: " + allGroups[i].getHostName() + " Newsgroup is: " + allGroups[i].getNewsgroupName() );
52 // Only public method of the class
53 // Returns and array of unique NewsGroup objects
54 public NewsGroup[] getNewsGroups()
56 windows = false;
57 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
59 windows = true;
62 String mozillaHome = "";
63 if( windows )
65 mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "Application Data" + System.getProperty( "file.separator" ) + "Mozilla" + System.getProperty( "file.separator" ) + "Profiles";
66 //System.out.println( "Windows mozilla path: " + mozillaHome );
68 else
70 mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + ".mozilla";
71 //System.out.println( "Unix/Linux mozilla path: " + mozillaHome );
73 if( !new File( mozillaHome ).isDirectory() )
75 //System.out.println("Could not find .mozilla directory");
76 return null;
78 //System.out.println(".mozilla directory found");
80 // Get all the profiles belonging to the user
81 File profiles[] = findProfiles( new File ( mozillaHome ) );
82 if( profiles.length < 1 )
84 //System.out.println("Could not find Profiles");
85 return null;
87 //System.out.println("Profiles found");
89 // Get the News directory for each profile
90 File allNewsDirs[] = new File[ profiles.length ];
91 for( int i=0; i < profiles.length; i++ ) {
92 File newsDir = findNewsDir( profiles[i] );
93 allNewsDirs[i] = newsDir;
94 //System.out.println( "News is at: " + newsDir.getPath() );
96 // Check that at least one News directory exists and remove nulls
97 boolean newsFound = false;
98 //Vector nonNullNews = new Vector();
99 for( int i=0; i < allNewsDirs.length; i++ ) {
100 if( allNewsDirs[i] != null ) {
101 newsFound = true;
102 break;
105 if( !newsFound )
107 //System.out.println("Could not find News directory");
108 return null;
110 //System.out.println("News directory found");
112 // Get all the mailrc files for each News directory
113 File allMailrcs[] = findMailrcFiles( allNewsDirs );
114 if( allMailrcs == null )
116 //System.out.println("Could not find mailrc files");
117 return null;
119 //System.out.println("mailrc files found");
121 ArrayList<NewsGroup> subscribed = new ArrayList<NewsGroup>();
122 // Get the newsgroups in each mailrc file
123 for( int i=0; i < allMailrcs.length; i++ )
125 File mailrc = (File) allMailrcs[i];
126 NewsGroup newsgroup[] = findNewsgroups( mailrc );
127 //if the Newsgroup has not already been added to the list
128 for( int j=0; j < newsgroup.length; j++ )
130 // if newsgroup is unique then add to the list
131 if( !listed( newsgroup[j], subscribed ) )
133 subscribed.addElement( newsgroup[j] );
138 // Copy all unique Newsgroups into the global array
139 allSubscribed = new NewsGroup[ subscribed.size() ];
140 subscribed.toArray( allSubscribed );
141 // Test that at least one subscribed newsgroup has been found
142 if( allSubscribed.length < 1 )
144 //System.out.println("Could not find Subscribed newsgroups ");
145 return null;
147 //System.out.println("Subscribed newsgroups found");
149 return allSubscribed;
155 // Tests if the NewsGroup object has already been listed by another mailrc file
156 private static boolean listed( NewsGroup newsgroup, ArrayList<NewsGroup> uniqueSubscription )
158 for(int i=0; i < uniqueSubscription.size(); i++)
160 NewsGroup tempGroup = uniqueSubscription.elementAt(i);
161 // Test for duplication
162 if(newsgroup.getHostName().equalsIgnoreCase( tempGroup.getHostName()) &&
163 newsgroup.getNewsgroupName().equalsIgnoreCase( tempGroup.getNewsgroupName() ) )
164 return true;
166 return false;
172 // Finds all the NewsGroups in an individual mailrc file
173 private static NewsGroup[] findNewsgroups(File mailrcfile )
176 String hostname = "";
177 String newsgroup = "";
178 NewsGroup mailrcNewsGroups[] = null;
180 //Retrieve name of news host/server from file name
181 //Sequentially access each of the newsgroups
182 //If the newsgroup is not already contained in the global NewsGroup[] array then add it
184 String filename = mailrcfile.getPath();
185 if( windows )
187 // Windows format "staroffice-news.germany.sun.com.rc"
188 int hostNameStart = filename.lastIndexOf("\\") + 1;
189 int hostNameEnd = filename.indexOf(".rc");
190 hostname = filename.substring( hostNameStart, hostNameEnd );
192 else
194 // Unix/Linux format "newsrc-staroffice-news.germany.sun.com"
195 int hostNameStart = filename.lastIndexOf("newsrc-") + 7;
196 hostname = filename.substring( hostNameStart, filename.length() );
199 // Assumes the content format in Window is the same as Unix/Linux (unknown at the moment)
200 // i.e. a list of newsgroups each ending with a ":"
201 LineNumberReader in = null;
202 try {
203 in = new LineNumberReader( new FileReader( mailrcfile ) );
204 ArrayList groups = new ArrayList();
205 String inString = "";
206 int line = 0;
207 while( inString != null )
209 in.setLineNumber( line );
210 inString = in.readLine();
211 line++;
212 if( inString != null )
214 int newsgroupEnd = inString.indexOf(":");
215 newsgroup = inString.substring( 0, newsgroupEnd );
216 NewsGroup group = new NewsGroup( hostname, newsgroup );
217 groups.addElement( group );
220 mailrcNewsGroups = new NewsGroup[ groups.size() ];
221 groups.copyInto(mailrcNewsGroups);
222 in.close();
224 catch( IOException ioe ) {
225 ioe.printStackTrace();
228 return mailrcNewsGroups;
232 // Finds all the mailrc files for all the given News directories
233 private static File[] findMailrcFiles(File[] newsDirs)
235 ArrayList allFiles = new ArrayList();
237 for( int i=0; i < newsDirs.length; i++ )
239 //System.out.println( "Finding mailrc for: " + newsDirs[i] );
240 if( newsDirs[i] != null )
242 File mailrcFiles[] = newsDirs[i].listFiles( new VersionFilter() );
243 if( mailrcFiles != null )
245 //System.out.println( "Number found: " + mailrcFiles.length );
246 for( int j=0; j < mailrcFiles.length; j++ )
248 //System.out.println( "This mailrc was found: " + mailrcFiles[j] );
249 allFiles.addElement( mailrcFiles[j] );
254 File allMailrcFiles[] = new File[ allFiles.size() ];
255 allFiles.copyInto(allMailrcFiles);
257 //System.out.println( "number of mailrcs in total: " + allMailrcFiles.length );
259 if( allMailrcFiles.length == 0 ) {
260 //System.out.println( "Returning null");
261 return null;
264 //System.out.println( "Returning an File array containing mailrcs");
265 return allMailrcFiles;
269 // Finds all profiles belonging to one user (can be more than one)
270 private static File[] findProfiles(File start)
272 // Get all files and directories in .mozilla
273 File allFiles[] = start.listFiles();
274 File[] dirs = new File[allFiles.length];
275 int dirCounter = 0;
277 // Remove files leaving directories only
278 for(int i=0; i < allFiles.length; i++ )
280 if(allFiles[i].isDirectory())
282 dirs[dirCounter] = allFiles[i];
283 dirCounter++;
287 // Add each directory to a user profile array
288 File[] profileDirs = new File[dirCounter];
289 for( int i=0; i < dirCounter; i++ )
291 profileDirs[i] = dirs[i];
294 // return a File array containing the profile dirs
295 return profileDirs;
299 // Recursively searches for the News directory for a given profile directory
300 private static File findNewsDir(File start)
302 File mailrcFile = null;
304 // File array containing all matches for the version filter ("News")
305 File files[] = start.listFiles(new VersionFilter());
306 // If the array is empty then no matches were found
307 if (files.length == 0)
309 // File array of all the directories in File start
310 File dirs[] = start.listFiles(new DirFilter());
311 // for each of the directories check for a match
312 for (int i=0; i< dirs.length; i++)
314 mailrcFile = findNewsDir(dirs[i]);
315 if (mailrcFile != null)
317 // break the for loop
318 break;
322 else
324 // end recursion
325 // Check for a News directory inside the News directory (fix for bug)
326 // Original solution had only "mailrcFile = files[0];"
328 boolean noChildNews = true;
329 File checkChildNewsDirs[] = files[0].listFiles(new VersionFilter());
330 if( checkChildNewsDirs != null )
332 for( int i=0; i < checkChildNewsDirs.length; i++ )
334 if( checkChildNewsDirs[i].getName().equals( "News" ) )
336 noChildNews = false;
337 break;
342 if( noChildNews )
344 mailrcFile = files[0];
346 else
348 String childNewsPathName = files[0].getAbsolutePath() + System.getProperty( "file.separator" ) + "News";
349 mailrcFile = new File( childNewsPathName );
354 // return a File representing the News dir in a profile
355 return mailrcFile;
361 class DirFilter implements FileFilter
363 public boolean accept(File aFile)
365 return aFile.isDirectory();
370 class VersionFilter implements FileFilter
372 public boolean accept(File aFile)
374 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
376 if (aFile.getName().compareToIgnoreCase("News") == 0 ||
377 aFile.getName().indexOf(".rc") != -1 )
379 return true;
382 else
384 if (aFile.getName().compareToIgnoreCase("News") == 0 ||
385 aFile.getName().indexOf("newsrc") != -1 )
387 return true;
391 return false;