2 * Created on 9 juin 2004
3 * Copyright 2004 Jérôme Nègre
5 package net.sourceforge.phpeclipse.news.pref;
8 import java.util.ArrayList;
9 import java.util.Enumeration;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.Properties;
14 import net.sourceforge.phpeclipse.news.Channel;
15 import net.sourceforge.phpeclipse.news.Item;
16 import net.sourceforge.phpeclipse.news.Plugin;
18 import org.eclipse.jface.dialogs.IDialogSettings;
21 * @author Jérôme Nègre
23 public class ChannelStore {
25 private final static String DEFAULT_CHANNELS_FILE = "default_feeds.properties";
27 private final static String BACKENDS_SECTION = "backends";
29 private final static String CHANNELS_ORDER_KEY = "order";
30 private final static String TITLE_KEY = "title";
31 private final static String URL_KEY = "url";
32 private final static String TYPE_KEY = "type";
33 private final static String READ_KEY = "read";
35 private final static int TYPE_CHANNEL = 1;
37 private static Plugin plugin = null;
39 public static void init(Plugin plugin) {
40 ChannelStore.plugin = plugin;
43 public static synchronized ArrayList getChannels() {
44 IDialogSettings section = getChannelsSection();
45 String[] uids = section.getArray(CHANNELS_ORDER_KEY);
46 ArrayList result = new ArrayList();
47 for(int i=0; i<uids.length; i++) {
49 IDialogSettings channelSection = section.getSection(uid);
50 String title = channelSection.get(TITLE_KEY);
51 String url = channelSection.get(URL_KEY);
53 String[] readUids = channelSection.getArray(READ_KEY);
54 HashSet set = new HashSet();
55 if(readUids != null) {
56 for(int k=0; k<readUids.length; k++) {
61 result.add(new Channel(title, url, set));
66 public static synchronized void setChannels(ArrayList channels) {
67 IDialogSettings section = getChannelsSection();
68 section.put(CHANNELS_ORDER_KEY,new String[0]);
69 int newSize = channels.size();
70 for(int i=0; i<newSize; i++) {
71 Channel channel = (Channel)channels.get(i);
72 addChannel(section,channel);
76 public static synchronized void saveReadStatus(ArrayList channels) {
77 IDialogSettings channelsSection = getChannelsSection();
78 Iterator channelIter = channels.iterator();
79 while(channelIter.hasNext()) {
80 Channel channel = (Channel)channelIter.next();
81 IDialogSettings section = channelsSection.getSection(channel.getUID());
82 Iterator itemIter = channel.getItems().iterator();
83 ArrayList readItems = new ArrayList();
84 while(itemIter.hasNext()) {
85 Item item = (Item)itemIter.next();
86 if(item.isReadFlag()) {
87 readItems.add(item.getUID());
90 section.put(READ_KEY,(String[])readItems.toArray(new String[0]));
94 public static synchronized ArrayList getDefaultChannels() {
95 ArrayList result = new ArrayList();
97 Properties prop = new Properties();
98 URL propLocation = new URL(Plugin.getDefault().getDescriptor().getInstallURL(), DEFAULT_CHANNELS_FILE);
99 prop.load(propLocation.openStream());
100 Enumeration e = prop.propertyNames();
101 while(e.hasMoreElements()) {
102 String url = (String)e.nextElement();
103 String title = prop.getProperty(url);
104 result.add(new Channel(title, url));
106 } catch(Exception e) {
107 Plugin.logError("Error while getting default feed list", e);
114 * Returns a non null Channels Section,
115 * creating it if needed.
118 private static IDialogSettings getChannelsSection() {
119 IDialogSettings section = plugin.getDialogSettings().getSection(BACKENDS_SECTION);
120 if(section == null) {
121 section = createDefaultChannelsSection();
126 private static IDialogSettings createDefaultChannelsSection() {
127 IDialogSettings section = plugin.getDialogSettings().addNewSection(BACKENDS_SECTION);
128 section.put(CHANNELS_ORDER_KEY,new String[0]);
129 //add some default channels from config file
130 Iterator iterator = getDefaultChannels().iterator();
131 while(iterator.hasNext()) {
132 addChannel(section, (Channel)iterator.next());
137 private static void addChannel(IDialogSettings backendSection, Channel channel) {
138 String title = channel.getTitle();
139 String url = channel.getUrl();
140 String uid = channel.getUID();
141 //check that section does not already exist before
142 //creating it, and if it exists, add it to the order key
143 //only if it's not already in it.
144 IDialogSettings section = backendSection.getSection(uid);
145 boolean addInOrder = true;
146 if(section == null) {
148 section = backendSection.addNewSection(uid);
150 //check if the section is already in the order key
151 String[] orders = backendSection.getArray(CHANNELS_ORDER_KEY);
152 for(int i=0; i<orders.length; i++) {
153 if(orders[i].equals(uid)) {
160 section.put(TITLE_KEY, title);
161 section.put(URL_KEY, url);
162 section.put(TYPE_KEY, TYPE_CHANNEL);
163 //set order key if needed
165 String[] oldOrder = backendSection.getArray(CHANNELS_ORDER_KEY);
166 String[] newOrder = new String[oldOrder.length+1];
167 System.arraycopy(oldOrder, 0, newOrder, 0, oldOrder.length);
168 newOrder[oldOrder.length] = uid;
169 backendSection.put(CHANNELS_ORDER_KEY,newOrder);