001: /*
002: * News.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: News.java,v 1.5 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.io.BufferedWriter;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStreamWriter;
028: import javax.swing.SwingUtilities;
029: import org.armedbear.j.Buffer;
030: import org.armedbear.j.Directories;
031: import org.armedbear.j.Editor;
032: import org.armedbear.j.EditorIterator;
033: import org.armedbear.j.File;
034: import org.armedbear.j.Line;
035: import org.armedbear.j.Log;
036: import org.armedbear.j.MessageDialog;
037: import org.armedbear.j.ProgressNotifier;
038: import org.armedbear.j.StatusBarProgressNotifier;
039:
040: public final class News extends Buffer {
041: private static final File newsDir = File.getInstance(Directories
042: .getEditorDirectory(), "news");
043:
044: private final NntpSession session;
045: private boolean error;
046:
047: public News(NntpSession session) {
048: this .session = session;
049: supportsUndo = false;
050: mode = NewsGroupsMode.getMode();
051: formatter = mode.getFormatter(this );
052: readOnly = true;
053: title = session.getHost();
054: setInitialized(true);
055: }
056:
057: public String getHost() {
058: return session.getHost();
059: }
060:
061: public int load() {
062: setBusy(true);
063: new Thread(loadRunnable).start();
064: setLoaded(true);
065: return LOAD_COMPLETED;
066: }
067:
068: private Runnable loadRunnable = new Runnable() {
069: public void run() {
070: try {
071: lockWrite();
072: } catch (InterruptedException e) {
073: Log.error(e);
074: return;
075: }
076: try {
077: _load();
078: } finally {
079: unlockWrite();
080: }
081: if (error)
082: SwingUtilities.invokeLater(errorRunnable);
083: else
084: SwingUtilities.invokeLater(updateDisplayRunnable);
085: }
086: };
087:
088: private void _load() {
089: File file = File.getInstance(newsDir, session.getHost());
090: if (newsDir.isDirectory()) {
091: if (file.isFile()) {
092: try {
093: InputStream in = file.getInputStream();
094: if (in != null) {
095: load(in, null);
096: in.close();
097: }
098: } catch (IOException e) {
099: Log.error(e);
100: }
101: }
102: } else
103: newsDir.mkdirs();
104: if (getFirstLine() == null) {
105: if (session.connect()) {
106: if (session.writeLine("LIST")) {
107: String response = session.readLine();
108: if (response.startsWith("215")) {
109: session.setEcho(false);
110: int count = 0;
111: ProgressNotifier progressNotifier = new StatusBarProgressNotifier(
112: this );
113: progressNotifier.progressStart();
114: while (true) {
115: String s = session.readLine();
116: if (s == null)
117: break;
118: if (s.equals("."))
119: break;
120: int index = s.indexOf(' ');
121: if (index >= 0)
122: appendLine(s.substring(0, index));
123: else
124: appendLine(s);
125: ++count;
126: progressNotifier.progress(String
127: .valueOf(count));
128: }
129: if (newsDir.isDirectory()) {
130: try {
131: BufferedWriter writer = new BufferedWriter(
132: new OutputStreamWriter(file
133: .getOutputStream()));
134: for (Line line = getFirstLine(); line != null; line = line
135: .next()) {
136: writer.write(line.getText());
137: writer.write('\n');
138: }
139: writer.flush();
140: writer.close();
141: } catch (IOException e) {
142: Log.error(e);
143: }
144: }
145: progressNotifier.progressStop();
146: session.setEcho(NntpSession.DEFAULT_ECHO);
147: }
148: }
149: session.disconnect();
150: }
151: }
152: if (getFirstLine() == null) {
153: error = true;
154: appendLine("");
155: }
156: renumber();
157: }
158:
159: private Runnable errorRunnable = new Runnable() {
160: public void run() {
161: kill();
162: String errorText = session.getErrorText();
163: if (errorText != null)
164: MessageDialog.showMessageDialog(errorText, "Error");
165: }
166: };
167:
168: private Runnable updateDisplayRunnable = new Runnable() {
169: public void run() {
170: setBusy(false);
171: invalidate();
172: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
173: Editor ed = it.nextEditor();
174: if (ed.getBuffer() == News.this ) {
175: ed.setDot(getFirstLine(), 0);
176: ed.moveCaretToDotCol();
177: ed.setTopLine(getFirstLine());
178: ed.setUpdateFlag(REPAINT);
179: ed.updateDisplay();
180: }
181: }
182: }
183: };
184: }
|