| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.libreoffice.example.java_scripts; |
| 20 |
|
| 21 |
import java.io.*; |
| 22 |
import java.util.Vector; |
| 23 |
|
| 24 |
|
| 25 |
public class SubscribedNewsgroups { |
| 26 |
|
| 27 |
|
| 28 |
private static NewsGroup[] allSubscribed = null; |
| 29 |
private static boolean windows = false; |
| 30 |
|
| 31 |
public static void main(String[] args) { |
| 32 |
// Test the class |
| 33 |
SubscribedNewsgroups subscribed = new SubscribedNewsgroups(); |
| 34 |
|
| 35 |
NewsGroup allGroups[] = subscribed.getNewsGroups(); |
| 36 |
|
| 37 |
if (allGroups == null) { |
| 38 |
System.out.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files"); |
| 39 |
} else { |
| 40 |
for (int i = 0; i < allGroups.length; i++) { |
| 41 |
System.out.println("Hostname is: " + allGroups[i].getHostName() + |
| 42 |
" Newsgroup is: " + allGroups[i].getNewsgroupName()); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
// Only public method of the class |
| 50 |
// Returns and array of unique NewsGroup objects |
| 51 |
public NewsGroup[] getNewsGroups() { |
| 52 |
windows = false; |
| 53 |
|
| 54 |
if (System.getProperty("os.name").indexOf("Windows") != -1) { |
| 55 |
windows = true; |
| 56 |
} |
| 57 |
|
| 58 |
String mozillaHome = ""; |
| 59 |
|
| 60 |
if (windows) { |
| 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"; |
| 65 |
} else { |
| 66 |
mozillaHome = System.getProperty("user.home") + |
| 67 |
System.getProperty("file.separator") + ".mozilla"; |
| 68 |
} |
| 69 |
|
| 70 |
if (!new File(mozillaHome).isDirectory()) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
// Get all the profiles belonging to the user |
| 75 |
File profiles[] = findProfiles(new File(mozillaHome)); |
| 76 |
|
| 77 |
if (profiles.length < 1) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
// Get the News directory for each profile |
| 82 |
File allNewsDirs[] = new File[ profiles.length ]; |
| 83 |
|
| 84 |
for (int i = 0; i < profiles.length; i++) { |
| 85 |
File newsDir = findNewsDir(profiles[i]); |
| 86 |
allNewsDirs[i] = newsDir; |
| 87 |
} |
| 88 |
|
| 89 |
// Check that at least one News directory exists and remove nulls |
| 90 |
boolean newsFound = false; |
| 91 |
|
| 92 |
for (int i = 0; i < allNewsDirs.length; i++) { |
| 93 |
if (allNewsDirs[i] != null) { |
| 94 |
newsFound = true; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (!newsFound) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
// Get all the mailrc files for each News directory |
| 104 |
File allMailrcs[] = findMailrcFiles(allNewsDirs); |
| 105 |
|
| 106 |
if (allMailrcs == null) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
ArrayList<NewsGroup> subscribed = new ArrayList<NewsGroup>(); |
| 111 |
|
| 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); |
| 116 |
|
| 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]); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Copy all unique Newsgroups into the global array |
| 127 |
allSubscribed = new NewsGroup[ subscribed.size() ]; |
| 128 |
subscribed.toArray(allSubscribed); |
| 129 |
|
| 130 |
// Test that at least one subscribed newsgroup has been found |
| 131 |
if (allSubscribed.length < 1) { |
| 132 |
return null; |
| 133 |
} |
| 134 |
|
| 135 |
return allSubscribed; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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); |
| 146 |
|
| 147 |
// Test for duplication |
| 148 |
if (newsgroup.getHostName().equalsIgnoreCase(tempGroup.getHostName()) && |
| 149 |
newsgroup.getNewsgroupName().equalsIgnoreCase(tempGroup.getNewsgroupName())) |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
// Finds all the NewsGroups in an individual mailrc file |
| 160 |
private static NewsGroup[] findNewsgroups(File mailrcfile) { |
| 161 |
|
| 162 |
String hostname = ""; |
| 163 |
String newsgroup = ""; |
| 164 |
NewsGroup mailrcNewsGroups[] = null; |
| 165 |
|
| 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 |
| 169 |
|
| 170 |
String filename = mailrcfile.getPath(); |
| 171 |
|
| 172 |
if (windows) { |
| 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); |
| 177 |
} else { |
| 178 |
// Unix/Linux format "newsrc-staroffice-news.germany.sun.com" |
| 179 |
int hostNameStart = filename.lastIndexOf("newsrc-") + 7; |
| 180 |
hostname = filename.substring(hostNameStart, filename.length()); |
| 181 |
} |
| 182 |
|
| 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; |
| 186 |
|
| 187 |
try { |
| 188 |
in = new LineNumberReader(new FileReader(mailrcfile)); |
| 189 |
ArrayList groups = new ArrayList(); |
| 190 |
String inString = ""; |
| 191 |
int line = 0; |
| 192 |
|
| 193 |
while (inString != null) { |
| 194 |
in.setLineNumber(line); |
| 195 |
inString = in.readLine(); |
| 196 |
line++; |
| 197 |
|
| 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); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
mailrcNewsGroups = new NewsGroup[ groups.size() ]; |
| 207 |
groups.copyInto(mailrcNewsGroups); |
| 208 |
in.close(); |
| 209 |
} catch (IOException ioe) { |
| 210 |
ioe.printStackTrace(); |
| 211 |
} |
| 212 |
|
| 213 |
return mailrcNewsGroups; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
// Finds all the mailrc files for all the given News directories |
| 218 |
private static File[] findMailrcFiles(File[] newsDirs) { |
| 219 |
ArrayList allFiles = new ArrayList(); |
| 220 |
|
| 221 |
for (int i = 0; i < newsDirs.length; i++) { |
| 222 |
if (newsDirs[i] != null) { |
| 223 |
File mailrcFiles[] = newsDirs[i].listFiles(new VersionFilter()); |
| 224 |
|
| 225 |
if (mailrcFiles != null) { |
| 226 |
for (int j = 0; j < mailrcFiles.length; j++) { |
| 227 |
allFiles.addElement(mailrcFiles[j]); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
File allMailrcFiles[] = new File[ allFiles.size() ]; |
| 234 |
allFiles.copyInto(allMailrcFiles); |
| 235 |
|
| 236 |
if (allMailrcFiles.length == 0) { |
| 237 |
return null; |
| 238 |
} |
| 239 |
|
| 240 |
return allMailrcFiles; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 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]; |
| 249 |
int dirCounter = 0; |
| 250 |
|
| 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]; |
| 255 |
dirCounter++; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Add each directory to a user profile array |
| 260 |
File[] profileDirs = new File[dirCounter]; |
| 261 |
|
| 262 |
for (int i = 0; i < dirCounter; i++) { |
| 263 |
profileDirs[i] = dirs[i]; |
| 264 |
} |
| 265 |
|
| 266 |
// return a File array containing the profile dirs |
| 267 |
return profileDirs; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
// Recursively searches for the News directory for a given profile directory |
| 272 |
private static File findNewsDir(File start) { |
| 273 |
File mailrcFile = null; |
| 274 |
|
| 275 |
// File array containing all matches for the version filter ("News") |
| 276 |
File files[] = start.listFiles(new VersionFilter()); |
| 277 |
|
| 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()); |
| 282 |
|
| 283 |
// for each of the directories check for a match |
| 284 |
for (int i = 0; i < dirs.length; i++) { |
| 285 |
mailrcFile = findNewsDir(dirs[i]); |
| 286 |
|
| 287 |
if (mailrcFile != null) { |
| 288 |
// break the for loop |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
} else { |
| 293 |
// end recursion |
| 294 |
// Check for a News directory inside the News directory (fix for bug) |
| 295 |
// Original solution had only "mailrcFile = files[0];" |
| 296 |
|
| 297 |
boolean noChildNews = true; |
| 298 |
File checkChildNewsDirs[] = files[0].listFiles(new VersionFilter()); |
| 299 |
|
| 300 |
if (checkChildNewsDirs != null) { |
| 301 |
for (int i = 0; i < checkChildNewsDirs.length; i++) { |
| 302 |
if (checkChildNewsDirs[i].getName().equals("News")) { |
| 303 |
noChildNews = false; |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if (noChildNews) { |
| 310 |
mailrcFile = files[0]; |
| 311 |
} else { |
| 312 |
String childNewsPathName = files[0].getAbsolutePath() + |
| 313 |
System.getProperty("file.separator") + "News"; |
| 314 |
mailrcFile = new File(childNewsPathName); |
| 315 |
} |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
// return a File representing the News dir in a profile |
| 320 |
return mailrcFile; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
class DirFilter implements FileFilter { |
| 327 |
public boolean accept(File aFile) { |
| 328 |
return aFile.isDirectory(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 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) { |
| 338 |
return true; |
| 339 |
} |
| 340 |
} else { |
| 341 |
if (aFile.getName().compareToIgnoreCase("News") == 0 || |
| 342 |
aFile.getName().indexOf("newsrc") != -1) { |
| 343 |
return true; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return false; |
| 348 |
} |
| 349 |
} |