1 21 22 package org.armedbear.j.mail; 23 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStreamWriter ; 28 import javax.swing.SwingUtilities ; 29 import org.armedbear.j.Buffer; 30 import org.armedbear.j.Directories; 31 import org.armedbear.j.Editor; 32 import org.armedbear.j.EditorIterator; 33 import org.armedbear.j.File; 34 import org.armedbear.j.Line; 35 import org.armedbear.j.Log; 36 import org.armedbear.j.MessageDialog; 37 import org.armedbear.j.ProgressNotifier; 38 import org.armedbear.j.StatusBarProgressNotifier; 39 40 public final class News extends Buffer 41 { 42 private static final File newsDir = 43 File.getInstance(Directories.getEditorDirectory(), "news"); 44 45 private final NntpSession session; 46 private boolean error; 47 48 public News(NntpSession session) 49 { 50 this.session = session; 51 supportsUndo = false; 52 mode = NewsGroupsMode.getMode(); 53 formatter = mode.getFormatter(this); 54 readOnly = true; 55 title = session.getHost(); 56 setInitialized(true); 57 } 58 59 public String getHost() 60 { 61 return session.getHost(); 62 } 63 64 public int load() 65 { 66 setBusy(true); 67 new Thread (loadRunnable).start(); 68 setLoaded(true); 69 return LOAD_COMPLETED; 70 } 71 72 private Runnable loadRunnable = new Runnable () { 73 public void run() 74 { 75 try { 76 lockWrite(); 77 } 78 catch (InterruptedException e) { 79 Log.error(e); 80 return; 81 } 82 try { 83 _load(); 84 } 85 finally { 86 unlockWrite(); 87 } 88 if (error) 89 SwingUtilities.invokeLater(errorRunnable); 90 else 91 SwingUtilities.invokeLater(updateDisplayRunnable); 92 } 93 }; 94 95 private void _load() 96 { 97 File file = File.getInstance(newsDir, session.getHost()); 98 if (newsDir.isDirectory()) { 99 if (file.isFile()) { 100 try { 101 InputStream in = file.getInputStream(); 102 if (in != null) { 103 load(in, null); 104 in.close(); 105 } 106 } 107 catch (IOException e) { 108 Log.error(e); 109 } 110 } 111 } else 112 newsDir.mkdirs(); 113 if (getFirstLine() == null) { 114 if (session.connect()) { 115 if (session.writeLine("LIST")) { 116 String response = session.readLine(); 117 if (response.startsWith("215")) { 118 session.setEcho(false); 119 int count = 0; 120 ProgressNotifier progressNotifier = 121 new StatusBarProgressNotifier(this); 122 progressNotifier.progressStart(); 123 while (true) { 124 String s = session.readLine(); 125 if (s == null) 126 break; 127 if (s.equals(".")) 128 break; 129 int index = s.indexOf(' '); 130 if (index >= 0) 131 appendLine(s.substring(0, index)); 132 else 133 appendLine(s); 134 ++count; 135 progressNotifier.progress(String.valueOf(count)); 136 } 137 if (newsDir.isDirectory()) { 138 try { 139 BufferedWriter writer = new BufferedWriter ( 140 new OutputStreamWriter (file.getOutputStream())); 141 for (Line line = getFirstLine(); line != null; line = line.next()) { 142 writer.write(line.getText()); 143 writer.write('\n'); 144 } 145 writer.flush(); 146 writer.close(); 147 } 148 catch (IOException e) { 149 Log.error(e); 150 } 151 } 152 progressNotifier.progressStop(); 153 session.setEcho(NntpSession.DEFAULT_ECHO); 154 } 155 } 156 session.disconnect(); 157 } 158 } 159 if (getFirstLine() == null) { 160 error = true; 161 appendLine(""); 162 } 163 renumber(); 164 } 165 166 private Runnable errorRunnable = new Runnable () { 167 public void run() 168 { 169 kill(); 170 String errorText = session.getErrorText(); 171 if (errorText != null) 172 MessageDialog.showMessageDialog(errorText, "Error"); 173 } 174 }; 175 176 private Runnable updateDisplayRunnable = new Runnable () { 177 public void run() 178 { 179 setBusy(false); 180 invalidate(); 181 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 182 Editor ed = it.nextEditor(); 183 if (ed.getBuffer() == News.this) { 184 ed.setDot(getFirstLine(), 0); 185 ed.moveCaretToDotCol(); 186 ed.setTopLine(getFirstLine()); 187 ed.setUpdateFlag(REPAINT); 188 ed.updateDisplay(); 189 } 190 } 191 } 192 }; 193 } 194 | Popular Tags |