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
;
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
) {
33 SubscribedNewsgroups subscribed
= new SubscribedNewsgroups();
35 NewsGroup allGroups
[] = subscribed
.getNewsGroups();
37 if (allGroups
== null) {
38 System
.out
.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files");
40 for (int i
= 0; i
< allGroups
.length
; i
++) {
41 System
.out
.println("Hostname is: " + allGroups
[i
].getHostName() +
42 " Newsgroup is: " + allGroups
[i
].getNewsgroupName());
49 // Only public method of the class
50 // Returns and array of unique NewsGroup objects
51 public NewsGroup
[] getNewsGroups() {
54 if (System
.getProperty("os.name").indexOf("Windows") != -1) {
58 String mozillaHome
= "";
61 mozillaHome
= System
.getProperty("user.home") +
62 System
.getProperty("file.separator") + "Application Data" +
63 System
.getProperty("file.separator") + "Mozilla" +
64 System
.getProperty("file.separator") + "Profiles";
66 mozillaHome
= System
.getProperty("user.home") +
67 System
.getProperty("file.separator") + ".mozilla";
70 if (!new File(mozillaHome
).isDirectory()) {
74 // Get all the profiles belonging to the user
75 File profiles
[] = findProfiles(new File(mozillaHome
));
77 if (profiles
.length
< 1) {
81 // Get the News directory for each profile
82 File allNewsDirs
[] = new File
[ profiles
.length
];
84 for (int i
= 0; i
< profiles
.length
; i
++) {
85 File newsDir
= findNewsDir(profiles
[i
]);
86 allNewsDirs
[i
] = newsDir
;
89 // Check that at least one News directory exists and remove nulls
90 boolean newsFound
= false;
92 for (int i
= 0; i
< allNewsDirs
.length
; i
++) {
93 if (allNewsDirs
[i
] != null) {
103 // Get all the mailrc files for each News directory
104 File allMailrcs
[] = findMailrcFiles(allNewsDirs
);
106 if (allMailrcs
== null) {
110 ArrayList
<NewsGroup
> subscribed
= new ArrayList
<NewsGroup
>();
112 // Get the newsgroups in each mailrc file
113 for (int i
= 0; i
< allMailrcs
.length
; i
++) {
114 File mailrc
= (File
) allMailrcs
[i
];
115 NewsGroup newsgroup
[] = findNewsgroups(mailrc
);
117 //if the Newsgroup has not already been added to the list
118 for (int j
= 0; j
< newsgroup
.length
; j
++) {
119 // if newsgroup is unique then add to the list
120 if (!listed(newsgroup
[j
], subscribed
)) {
121 subscribed
.addElement(newsgroup
[j
]);
126 // Copy all unique Newsgroups into the global array
127 allSubscribed
= new NewsGroup
[ subscribed
.size() ];
128 subscribed
.toArray(allSubscribed
);
130 // Test that at least one subscribed newsgroup has been found
131 if (allSubscribed
.length
< 1) {
135 return allSubscribed
;
141 // Tests if the NewsGroup object has already been listed by another mailrc file
142 private static boolean listed(NewsGroup newsgroup
,
143 ArrayList
<NewsGroup
> uniqueSubscription
) {
144 for (int i
= 0; i
< uniqueSubscription
.size(); i
++) {
145 NewsGroup tempGroup
= uniqueSubscription
.elementAt(i
);
147 // Test for duplication
148 if (newsgroup
.getHostName().equalsIgnoreCase(tempGroup
.getHostName()) &&
149 newsgroup
.getNewsgroupName().equalsIgnoreCase(tempGroup
.getNewsgroupName()))
159 // Finds all the NewsGroups in an individual mailrc file
160 private static NewsGroup
[] findNewsgroups(File mailrcfile
) {
162 String hostname
= "";
163 String newsgroup
= "";
164 NewsGroup mailrcNewsGroups
[] = null;
166 //Retrieve name of news host/server from file name
167 //Sequentially access each of the newsgroups
168 //If the newsgroup is not already contained in the global NewsGroup[] array then add it
170 String filename
= mailrcfile
.getPath();
173 // Windows format "staroffice-news.germany.sun.com.rc"
174 int hostNameStart
= filename
.lastIndexOf("\\") + 1;
175 int hostNameEnd
= filename
.indexOf(".rc");
176 hostname
= filename
.substring(hostNameStart
, hostNameEnd
);
178 // Unix/Linux format "newsrc-staroffice-news.germany.sun.com"
179 int hostNameStart
= filename
.lastIndexOf("newsrc-") + 7;
180 hostname
= filename
.substring(hostNameStart
, filename
.length());
183 // Assumes the content format in Window is the same as Unix/Linux (unknown at the moment)
184 // i.e. a list of newsgroups each ending with a ":"
185 LineNumberReader in
= null;
188 in
= new LineNumberReader(new FileReader(mailrcfile
));
189 ArrayList groups
= new ArrayList();
190 String inString
= "";
193 while (inString
!= null) {
194 in
.setLineNumber(line
);
195 inString
= in
.readLine();
198 if (inString
!= null) {
199 int newsgroupEnd
= inString
.indexOf(":");
200 newsgroup
= inString
.substring(0, newsgroupEnd
);
201 NewsGroup group
= new NewsGroup(hostname
, newsgroup
);
202 groups
.addElement(group
);
206 mailrcNewsGroups
= new NewsGroup
[ groups
.size() ];
207 groups
.copyInto(mailrcNewsGroups
);
209 } catch (IOException ioe
) {
210 ioe
.printStackTrace();
213 return mailrcNewsGroups
;
217 // Finds all the mailrc files for all the given News directories
218 private static File
[] findMailrcFiles(File
[] newsDirs
) {
219 ArrayList allFiles
= new ArrayList();
221 for (int i
= 0; i
< newsDirs
.length
; i
++) {
222 if (newsDirs
[i
] != null) {
223 File mailrcFiles
[] = newsDirs
[i
].listFiles(new VersionFilter());
225 if (mailrcFiles
!= null) {
226 for (int j
= 0; j
< mailrcFiles
.length
; j
++) {
227 allFiles
.addElement(mailrcFiles
[j
]);
233 File allMailrcFiles
[] = new File
[ allFiles
.size() ];
234 allFiles
.copyInto(allMailrcFiles
);
236 if (allMailrcFiles
.length
== 0) {
240 return allMailrcFiles
;
244 // Finds all profiles belonging to one user (can be more than one)
245 private static File
[] findProfiles(File start
) {
246 // Get all files and directories in .mozilla
247 File allFiles
[] = start
.listFiles();
248 File
[] dirs
= new File
[allFiles
.length
];
251 // Remove files leaving directories only
252 for (int i
= 0; i
< allFiles
.length
; i
++) {
253 if (allFiles
[i
].isDirectory()) {
254 dirs
[dirCounter
] = allFiles
[i
];
259 // Add each directory to a user profile array
260 File
[] profileDirs
= new File
[dirCounter
];
262 for (int i
= 0; i
< dirCounter
; i
++) {
263 profileDirs
[i
] = dirs
[i
];
266 // return a File array containing the profile dirs
271 // Recursively searches for the News directory for a given profile directory
272 private static File
findNewsDir(File start
) {
273 File mailrcFile
= null;
275 // File array containing all matches for the version filter ("News")
276 File files
[] = start
.listFiles(new VersionFilter());
278 // If the array is empty then no matches were found
279 if (files
.length
== 0) {
280 // File array of all the directories in File start
281 File dirs
[] = start
.listFiles(new DirFilter());
283 // for each of the directories check for a match
284 for (int i
= 0; i
< dirs
.length
; i
++) {
285 mailrcFile
= findNewsDir(dirs
[i
]);
287 if (mailrcFile
!= null) {
288 // break the for loop
294 // Check for a News directory inside the News directory (fix for bug)
295 // Original solution had only "mailrcFile = files[0];"
297 boolean noChildNews
= true;
298 File checkChildNewsDirs
[] = files
[0].listFiles(new VersionFilter());
300 if (checkChildNewsDirs
!= null) {
301 for (int i
= 0; i
< checkChildNewsDirs
.length
; i
++) {
302 if (checkChildNewsDirs
[i
].getName().equals("News")) {
310 mailrcFile
= files
[0];
312 String childNewsPathName
= files
[0].getAbsolutePath() +
313 System
.getProperty("file.separator") + "News";
314 mailrcFile
= new File(childNewsPathName
);
319 // return a File representing the News dir in a profile
326 class DirFilter
implements FileFilter
{
327 public boolean accept(File aFile
) {
328 return aFile
.isDirectory();
333 class VersionFilter
implements FileFilter
{
334 public boolean accept(File aFile
) {
335 if (System
.getProperty("os.name").indexOf("Windows") != -1) {
336 if (aFile
.getName().compareToIgnoreCase("News") == 0 ||
337 aFile
.getName().indexOf(".rc") != -1) {
341 if (aFile
.getName().compareToIgnoreCase("News") == 0 ||
342 aFile
.getName().indexOf("newsrc") != -1) {