1 /**
2 *
3 * Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.store.nntp.newsrc;
19
20 import java.io.IOException;
21 import java.io.Writer;
22
23 public class NNTPNewsrcGroup {
24
25 NNTPNewsrc newsrc;
26
27
28 protected String name;
29
30
31 protected boolean subscribed;
32
33
34 protected RangeList ranges;
35
36 /**
37 * Construct a NNTPNewsrcGroup item associated with a given .newsrc
38 * database.
39 *
40 * @param newsrc
41 * The owning .newsrc database.
42 * @param line
43 * The .newsrc range entries in .newsrc format. These ranges are
44 * parsed to create a set of seen flags.
45 *
46 * @return A created NNTPNewsrcGroup item.
47 */
48 public static NNTPNewsrcGroup parse(NNTPNewsrc newsrc, String line) {
49 String groupName = null;
50 String ranges = null;
51
52
53 int marker = line.indexOf(':');
54
55 if (marker != -1) {
56 groupName = line.substring(0, marker);
57 ranges = line.substring(marker + 1);
58 return new NNTPNewsrcGroup(newsrc, groupName, ranges, true);
59 }
60
61
62 marker = line.indexOf('!');
63
64 if (marker != -1) {
65 groupName = line.substring(0, marker);
66 ranges = line.substring(marker + 1);
67 return new NNTPNewsrcGroup(newsrc, groupName, ranges, false);
68 }
69
70
71 return null;
72 }
73
74 /**
75 * Construct a .newsrc group item.
76 *
77 * @param newsrc
78 * The owning newsrc database.
79 * @param name
80 * The group name.
81 * @param newsrcRanges
82 * The initial set of seen ranges for the group (may be null).
83 * @param subscribed
84 * The initial group subscription state.
85 */
86 public NNTPNewsrcGroup(NNTPNewsrc newsrc, String name, String newsrcRanges, boolean subscribed) {
87 this.newsrc = newsrc;
88 this.name = name;
89 this.subscribed = subscribed;
90 this.ranges = new RangeList(newsrcRanges);
91 }
92
93 /**
94 * Get the group name.
95 *
96 * @return The String name of the group.
97 */
98 public String getName() {
99 return name;
100 }
101
102 /**
103 * Get the newsrc subscribed status for an article.
104 *
105 * @return The current subscription flag.
106 */
107 public boolean isSubscribed() {
108 return subscribed;
109 }
110
111 /**
112 * Set the subscription status for an article.
113 *
114 * @param flag
115 * The new subscription value.
116 */
117 public void setSubscribed(boolean flag) {
118
119
120
121 if (flag && !subscribed) {
122 subscribed = true;
123 newsrc.setDirty();
124 } else if (!flag && subscribed) {
125 subscribed = false;
126 newsrc.setDirty();
127 }
128 }
129
130 /**
131 * Test if an article has been seen yet.
132 *
133 * @param article
134 * The target article.
135 *
136 * @return The seen mark for the article.
137 */
138 public boolean isArticleSeen(int article) {
139 return ranges.isMarked(article);
140 }
141
142 /**
143 * Mark an article as seen.
144 *
145 * @param article
146 * The target article number.
147 */
148 public void markArticleSeen(int article) {
149 ranges.setMarked(article);
150 if (ranges.isDirty()) {
151 newsrc.setDirty();
152 }
153 }
154
155 /**
156 * Mark an article as unseen.
157 *
158 * @param article
159 * The target article number.
160 */
161 public void markArticleUnseen(int article) {
162 ranges.setUnmarked(article);
163 if (ranges.isDirty()) {
164 newsrc.setDirty();
165 }
166 }
167
168 /**
169 * Save this group definition to a .newsrc file.
170 *
171 * @param out
172 * The output writer to send the information to.
173 *
174 * @exception IOException
175 */
176 public void save(Writer out) throws IOException {
177 out.write(name);
178 out.write(subscribed ? ": " : "! ");
179 ranges.save(out);
180
181 out.write("\r\n");
182 }
183 }