1 21 22 package org.armedbear.j.mail; 23 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.util.HashMap ; 28 import java.util.Vector ; 29 import javax.swing.Icon ; 30 import javax.swing.SwingUtilities ; 31 import org.armedbear.j.Editor; 32 import org.armedbear.j.EditorIterator; 33 import org.armedbear.j.FastStringBuffer; 34 import org.armedbear.j.FastStringReader; 35 import org.armedbear.j.File; 36 import org.armedbear.j.InputDialog; 37 import org.armedbear.j.Line; 38 import org.armedbear.j.Log; 39 import org.armedbear.j.MessageDialog; 40 import org.armedbear.j.Position; 41 import org.armedbear.j.ProgressNotifier; 42 import org.armedbear.j.StatusBarProgressNotifier; 43 import org.armedbear.j.Utilities; 44 import org.armedbear.j.View; 45 46 public final class NewsGroupSummary extends Mailbox 47 { 48 private final NntpSession session; 49 private final String groupName; 50 private final HashMap map = new HashMap (); 51 52 private ProgressNotifier progressNotifier; 53 private String errorText; 54 private int numberToGet; 55 56 public NewsGroupSummary(NntpSession session, String groupName) 57 { 58 super(); 59 this.session = session; 60 this.groupName = groupName; 61 supportsUndo = false; 62 type = TYPE_MAILBOX; 63 mode = NewsGroupSummaryMode.getMode(); 64 formatter = mode.getFormatter(this); 65 readOnly = true; 66 title = groupName; 67 progressNotifier = new StatusBarProgressNotifier(this); 68 setInitialized(true); 69 } 70 71 public final NntpSession getSession() 72 { 73 return session; 74 } 75 76 public final String getName() 77 { 78 return groupName; 79 } 80 81 public int load() 82 { 83 setBusy(true); 84 new Thread (loadRunnable).start(); 85 setLoaded(true); 86 return LOAD_COMPLETED; 87 } 88 89 private Runnable loadRunnable = new Runnable () { 90 public void run() 91 { 92 if (!session.connect()) 93 { 94 errorText = session.getErrorText(); 95 SwingUtilities.invokeLater(errorRunnable); 96 return; 97 } 98 if (!selectGroup()) { 99 errorText = "No group \"" + groupName + '\"'; 100 SwingUtilities.invokeLater(errorRunnable); 101 return; 102 } 103 final int count = session.getCount(); 104 if (count == 0) { 105 errorText = "No articles"; 106 SwingUtilities.invokeLater(errorRunnable); 107 return; 108 } 109 if (count > 100) { 110 Runnable confirmRunnable = new Runnable () { 111 public void run() 112 { 113 Editor editor = Editor.currentEditor(); 114 editor.setDefaultCursor(); 115 String prompt = "How many headers would you like?"; 116 String defaultValue = String.valueOf(count); 117 String response = 118 InputDialog.showInputDialog(editor, prompt, 119 groupName, defaultValue); 120 editor.setWaitCursor(); 121 numberToGet = 0; 122 if (response != null && response.length() > 0) { 123 try { 124 numberToGet = Integer.parseInt(response); 125 } 126 catch (NumberFormatException e) { 127 Log.error(e); 128 } 129 } 130 } 131 }; 132 try { 133 SwingUtilities.invokeAndWait(confirmRunnable); 134 } 135 catch (Exception e) { 136 Log.error(e); 137 } 138 } else 139 numberToGet = count; 140 if (numberToGet == 0) { 141 SwingUtilities.invokeLater(errorRunnable); 142 return; 143 } 144 getHeaders(); 145 if (entries != null && entries.size() > 0) { 146 addEntriesToBuffer(); 147 SwingUtilities.invokeLater(updateDisplayRunnable); 148 } else { 149 errorText = "No articles"; 150 SwingUtilities.invokeLater(errorRunnable); 151 } 152 } 153 }; 154 155 private Runnable updateDisplayRunnable = new Runnable () { 156 public void run() 157 { 158 setBusy(false); 159 invalidate(); 160 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 161 Editor ed = it.nextEditor(); 162 if (ed.getBuffer() == NewsGroupSummary.this) { 163 ed.setDot(getInitialDotPos()); 164 ed.moveCaretToDotCol(); 165 ed.setTopLine(getFirstLine()); 166 ed.setUpdateFlag(REPAINT); 167 ed.updateDisplay(); 168 } 169 } 170 } 171 }; 172 173 private Runnable errorRunnable = new Runnable () { 174 public void run() 175 { 176 Editor editor = Editor.currentEditor(); 177 editor.setDefaultCursor(); 178 if (errorText != null) 179 MessageDialog.showMessageDialog(errorText, "Error"); 180 if (Editor.getBufferList().contains(NewsGroupSummary.this)) 181 kill(); 182 for (EditorIterator it = new EditorIterator(); it.hasNext();) 183 it.nextEditor().updateDisplay(); 184 } 185 }; 186 187 public void readArticle(Editor editor, Line line, boolean useOtherWindow) 188 { 189 if (line instanceof MailboxLine) { 190 editor.setMark(null); 191 NewsGroupSummaryEntry entry = 192 (NewsGroupSummaryEntry) ((MailboxLine)line).getMailboxEntry(); 193 NewsGroupMessageBuffer mb = new NewsGroupMessageBuffer(this, entry); 194 activateMessageBuffer(editor, mb, useOtherWindow); 195 } 196 } 197 198 public String getArticle(int articleNumber, ProgressNotifier progressNotifier) 199 { 200 String key = String.valueOf(articleNumber); 201 String filename = (String ) map.get(key); 202 if (filename != null) { 203 File file = File.getInstance(filename); 204 if (file != null) { 205 try { 206 MailReader reader = new MailReader(file.getInputStream()); 207 long length = file.length(); 208 FastStringBuffer sb = new FastStringBuffer((int)length); 209 String s; 210 while ((s = reader.readLine()) != null) { 211 sb.append(s); 212 sb.append('\n'); 213 } 214 return sb.toString(); 215 } 216 catch (IOException e) { 217 Log.error(e); 218 } 219 } 220 } 221 String text = session.getArticle(articleNumber, progressNotifier); 222 if (text != null) { 223 File file = Utilities.getTempFile(); 224 try { 225 FastStringReader reader = new FastStringReader(text); 226 BufferedWriter writer = new BufferedWriter ( 227 new OutputStreamWriter (file.getOutputStream(), 228 "ISO-8859-1")); 229 String s; 230 while ((s = reader.readLine()) != null) { 231 writer.write(s); 232 writer.write('\n'); 233 } 234 writer.flush(); 235 writer.close(); 236 } 237 catch (IOException e) { 238 Log.error(e); 239 } 240 map.put(key, file.canonicalPath()); 241 } 242 return text; 243 } 244 245 private void addEntriesToBuffer() 246 { 247 if (entries != null) { 248 try { 249 lockWrite(); 250 } 251 catch (InterruptedException e) { 252 Log.error(e); 253 return; 254 } 255 try { 256 int limit = entries.size(); 257 for (int i = 0; i < limit; i++) 258 appendLine((NewsGroupSummaryEntry)entries.get(i)); 259 renumber(); 260 } 261 finally { 262 unlockWrite(); 263 } 264 } 265 } 266 267 public Position getInitialDotPos() 268 { 269 return getFirstLine() != null ? new Position(getFirstLine(), 0) : null; 270 } 271 272 private boolean selectGroup() 273 { 274 return session.selectGroup(groupName); 275 } 276 277 private void getHeaders() 278 { 279 int last = session.getLast(); 280 int first = session.getFirst(); 281 if (last <= first) 282 return; 283 if (last - first > numberToGet) 284 first = last - numberToGet; 285 entries = new Vector (); 286 if (session.writeLine("XOVER " + first + "-" + last)) { 287 String s = session.readLine(); 288 if (s.startsWith("224")) { 289 progressNotifier.progressStart(); 290 int count = 0; 291 while (true) { 292 s = session.readLine(); 293 if (s == null) 294 break; 295 if (s.equals(".")) 296 break; 297 NewsGroupSummaryEntry entry = 298 NewsGroupSummaryEntry.parseOverviewEntry(s); 299 if (entry != null) 300 entries.add(entry); 301 ++count; 302 progressNotifier.progress("Received " + count + " headers"); 303 } 304 progressNotifier.progressStop(); 305 } else 306 Log.error("XOVER response = " + s); 307 } 308 } 309 310 public void dispose() 311 { 312 if (session != null) { 313 Runnable r = new Runnable (){ 314 public void run() 315 { 316 session.disconnect(); 317 } 318 }; 319 new Thread (r).start(); 320 } 321 } 322 323 public Icon getIcon() 325 { 326 return Utilities.getIconFromFile("mailbox.png"); 327 } 328 329 public void getNewMessages() 330 { 331 } 332 333 public void readMessage(Line line) 334 { 335 } 336 337 public void createFolder() 338 { 339 } 340 341 public void deleteFolder() 342 { 343 } 344 345 public void saveToFolder() 346 { 347 } 348 349 public void moveToFolder() 350 { 351 } 352 353 public void delete() 354 { 355 } 356 357 public void undelete() 358 { 359 } 360 361 public void markRead() 362 { 363 } 364 365 public void markUnread() 366 { 367 } 368 369 public void setAnsweredFlag(MailboxEntry entry) 370 { 371 } 372 373 public void expunge() 374 { 375 } 376 377 public int getMessageCount() 378 { 379 return 0; 380 } 381 382 public void saveView(Editor editor) 383 { 384 final View view = saveViewInternal(editor); 385 editor.setView(this, view); 386 setLastView(view); 387 } 388 } 389 | Popular Tags |