merge the formfield patch from ooo-build
[ooovba.git] / scripting / examples / java / Newsgroup / SubscribedNewsgroups.java
blob60b935c327381c76b3496ad4484a4ee68565eea6
1 import java.io.*;
2 import java.util.Vector;
5 public class SubscribedNewsgroups {
8 private static NewsGroup[] allSubscribed = null;
9 private static boolean windows = false;
11 public static void main( String[] args ) {
12 // Test the class
13 SubscribedNewsgroups subscribed = new SubscribedNewsgroups();
15 NewsGroup allGroups[] = subscribed.getNewsGroups();
17 if( allGroups == null )
19 System.out.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files");
21 else
23 for( int i=0; i < allGroups.length; i++ )
25 System.out.println( "Hostname is: " + allGroups[i].getHostName() + " Newsgroup is: " + allGroups[i].getNewsgroupName() );
32 // Only public method of the class
33 // Returns and array of unique NewsGroup objects
34 public NewsGroup[] getNewsGroups()
36 windows = false;
37 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
39 windows = true;
42 String mozillaHome = "";
43 if( windows )
45 mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "Application Data" + System.getProperty( "file.separator" ) + "Mozilla" + System.getProperty( "file.separator" ) + "Profiles";
46 //System.out.println( "Windows mozilla path: " + mozillaHome );
48 else
50 mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + ".mozilla";
51 //System.out.println( "Unix/Linux mozilla path: " + mozillaHome );
53 if( !new File( mozillaHome ).isDirectory() )
55 //System.out.println("Could not find .mozilla directory");
56 return null;
58 //System.out.println(".mozilla directory found");
60 // Get all the profiles belonging to the user
61 File profiles[] = findProfiles( new File ( mozillaHome ) );
62 if( profiles.length < 1 )
64 //System.out.println("Could not find Profiles");
65 return null;
67 //System.out.println("Profiles found");
69 // Get the News directory for each profile
70 File allNewsDirs[] = new File[ profiles.length ];
71 for( int i=0; i < profiles.length; i++ ) {
72 File newsDir = findNewsDir( profiles[i] );
73 allNewsDirs[i] = newsDir;
74 //System.out.println( "News is at: " + newsDir.getPath() );
76 // Check that at least one News directory exists and remove nulls
77 boolean newsFound = false;
78 //Vector nonNullNews = new Vector();
79 for( int i=0; i < allNewsDirs.length; i++ ) {
80 if( allNewsDirs[i] != null ) {
81 newsFound = true;
82 break;
85 if( !newsFound )
87 //System.out.println("Could not find News directory");
88 return null;
90 //System.out.println("News directory found");
92 // Get all the mailrc files for each News directory
93 File allMailrcs[] = findMailrcFiles( allNewsDirs );
94 if( allMailrcs == null )
96 //System.out.println("Could not find mailrc files");
97 return null;
99 //System.out.println("mailrc files found");
101 Vector subscribed = new Vector();
102 // Get the newsgroups in each mailrc file
103 for( int i=0; i < allMailrcs.length; i++ )
105 File mailrc = (File) allMailrcs[i];
106 NewsGroup newsgroup[] = findNewsgroups( mailrc );
107 //if the Newsgroup has not already been added to the list
108 for( int j=0; j < newsgroup.length; j++ )
110 // if newsgroup is unique then add to the list
111 if( !listed( newsgroup[j], subscribed ) )
113 subscribed.addElement( newsgroup[j] );
118 // Copy all unique Newsgroups into the global array
119 allSubscribed = new NewsGroup[ subscribed.size() ];
120 subscribed.copyInto( allSubscribed );
121 // Test that at least one subscribed newsgroup has been found
122 if( allSubscribed.length < 1 )
124 //System.out.println("Could not find Subscribed newsgroups ");
125 return null;
127 //System.out.println("Subscribed newsgroups found");
129 return allSubscribed;
135 // Tests if the NewsGroup object has already been listed by another mailrc file
136 private static boolean listed( NewsGroup newsgroup, Vector uniqueSubscription )
138 for(int i=0; i < uniqueSubscription.size(); i++)
140 NewsGroup tempGroup = (NewsGroup) uniqueSubscription.elementAt(i);
141 // Test for duplication
142 if(newsgroup.getHostName().equalsIgnoreCase( tempGroup.getHostName()) &&
143 newsgroup.getNewsgroupName().equalsIgnoreCase( tempGroup.getNewsgroupName() ) )
144 return true;
146 return false;
152 // Finds all the NewsGroups in an individual mailrc file
153 private static NewsGroup[] findNewsgroups(File mailrcfile )
156 String hostname = "";
157 String newsgroup = "";
158 NewsGroup mailrcNewsGroups[] = null;
160 //Retrieve name of news host/server from file name
161 //Sequentially access each of the newsgroups
162 //If the newsgroup is not already contained in the global NewsGroup[] array then add it
164 String filename = mailrcfile.getPath();
165 if( windows )
167 // Windows format "staroffice-news.germany.sun.com.rc"
168 int hostNameStart = filename.lastIndexOf("\\") + 1;
169 int hostNameEnd = filename.indexOf(".rc");
170 hostname = filename.substring( hostNameStart, hostNameEnd );
172 else
174 // Unix/Linux format "newsrc-staroffice-news.germany.sun.com"
175 int hostNameStart = filename.lastIndexOf("newsrc-") + 7;
176 hostname = filename.substring( hostNameStart, filename.length() );
179 // Assumes the content format in Window is the same as Unix/Linux (unknown at the moment)
180 // i.e. a list of newsgroups each ending with a ":"
181 LineNumberReader in = null;
182 try {
183 in = new LineNumberReader( new FileReader( mailrcfile ) );
184 Vector groups = new Vector();
185 String inString = "";
186 int line = 0;
187 while( inString != null )
189 in.setLineNumber( line );
190 inString = in.readLine();
191 line++;
192 if( inString != null )
194 int newsgroupEnd = inString.indexOf(":");
195 newsgroup = inString.substring( 0, newsgroupEnd );
196 NewsGroup group = new NewsGroup( hostname, newsgroup );
197 groups.addElement( group );
200 mailrcNewsGroups = new NewsGroup[ groups.size() ];
201 groups.copyInto(mailrcNewsGroups);
202 in.close();
204 catch( IOException ioe ) {
205 ioe.printStackTrace();
208 return mailrcNewsGroups;
212 // Finds all the mailrc files for all the given News directories
213 private static File[] findMailrcFiles(File[] newsDirs)
215 Vector allFiles = new Vector();
217 for( int i=0; i < newsDirs.length; i++ )
219 //System.out.println( "Finding mailrc for: " + newsDirs[i] );
220 if( newsDirs[i] != null )
222 File mailrcFiles[] = newsDirs[i].listFiles( new VersionFilter() );
223 if( mailrcFiles != null )
225 //System.out.println( "Number found: " + mailrcFiles.length );
226 for( int j=0; j < mailrcFiles.length; j++ )
228 //System.out.println( "This mailrc was found: " + mailrcFiles[j] );
229 allFiles.addElement( mailrcFiles[j] );
234 File allMailrcFiles[] = new File[ allFiles.size() ];
235 allFiles.copyInto(allMailrcFiles);
237 //System.out.println( "number of mailrcs in total: " + allMailrcFiles.length );
239 if( allMailrcFiles.length == 0 ) {
240 //System.out.println( "Returning null");
241 return null;
244 //System.out.println( "Returning an File array containing mailrcs");
245 return allMailrcFiles;
249 // Finds all profiles belonging to one user (can be more than one)
250 private static File[] findProfiles(File start)
252 // Get all files and directories in .mozilla
253 File allFiles[] = start.listFiles();
254 File[] dirs = new File[allFiles.length];
255 int dirCounter = 0;
257 // Remove files leaving directories only
258 for(int i=0; i < allFiles.length; i++ )
260 if(allFiles[i].isDirectory())
262 dirs[dirCounter] = allFiles[i];
263 dirCounter++;
267 // Add each directory to a user profile array
268 File[] profileDirs = new File[dirCounter];
269 for( int i=0; i < dirCounter; i++ )
271 profileDirs[i] = dirs[i];
274 // return a File array containing the profile dirs
275 return profileDirs;
279 // Recursively searches for the News directory for a given profile directory
280 private static File findNewsDir(File start)
282 File mailrcFile = null;
284 // File array containing all matches for the version filter ("News")
285 File files[] = start.listFiles(new VersionFilter());
286 // If the array is empty then no matches were found
287 if (files.length == 0)
289 // File array of all the directories in File start
290 File dirs[] = start.listFiles(new DirFilter());
291 // for each of the directories check for a match
292 for (int i=0; i< dirs.length; i++)
294 mailrcFile = findNewsDir(dirs[i]);
295 if (mailrcFile != null)
297 // break the for loop
298 break;
302 else
304 // end recursion
305 // Check for a News directory inside the News directory (fix for bug)
306 // Original solution had only "mailrcFile = files[0];"
308 boolean noChildNews = true;
309 File checkChildNewsDirs[] = files[0].listFiles(new VersionFilter());
310 if( checkChildNewsDirs != null )
312 for( int i=0; i < checkChildNewsDirs.length; i++ )
314 if( checkChildNewsDirs[i].getName().equals( "News" ) )
316 noChildNews = false;
317 break;
322 if( noChildNews )
324 mailrcFile = files[0];
326 else
328 String childNewsPathName = files[0].getAbsolutePath() + System.getProperty( "file.separator" ) + "News";
329 mailrcFile = new File( childNewsPathName );
334 // return a File representing the News dir in a profile
335 return mailrcFile;
341 class DirFilter implements FileFilter
343 public boolean accept(File aFile)
345 return aFile.isDirectory();
350 class VersionFilter implements FileFilter
352 public boolean accept(File aFile)
354 if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
356 if (aFile.getName().compareToIgnoreCase("News") == 0 ||
357 aFile.getName().indexOf(".rc") != -1 )
359 return true;
362 else
364 if (aFile.getName().compareToIgnoreCase("News") == 0 ||
365 aFile.getName().indexOf("newsrc") != -1 )
367 return true;
371 return false;