RSS news reader; initially copied from "all the news"
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.news / src / net / sourceforge / phpeclipse / news / pref / ChannelStore.java
1 /*
2  * Created on 9 juin 2004
3  * Copyright 2004 Jérôme Nègre
4  */
5 package net.sourceforge.phpeclipse.news.pref;
6
7 import java.net.URL;
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;
13
14 import net.sourceforge.phpeclipse.news.Channel;
15 import net.sourceforge.phpeclipse.news.Item;
16 import net.sourceforge.phpeclipse.news.Plugin;
17
18 import org.eclipse.jface.dialogs.IDialogSettings;
19
20 /**
21  * @author Jérôme Nègre
22  */
23 public class ChannelStore {
24         
25         private final static String DEFAULT_CHANNELS_FILE = "default_feeds.properties";
26         
27         private final static String BACKENDS_SECTION = "backends";
28         
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";
34         
35         private final static int TYPE_CHANNEL = 1;
36         
37         private static Plugin plugin = null;
38         
39         public static void init(Plugin plugin) {
40                 ChannelStore.plugin = plugin;
41         }
42         
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++) {
48                         String uid = uids[i];
49                         IDialogSettings channelSection = section.getSection(uid);
50                         String title = channelSection.get(TITLE_KEY);
51                         String url = channelSection.get(URL_KEY);
52                         
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++) {
57                                         set.add(readUids[k]);
58                                 }
59                         }
60                         
61                         result.add(new Channel(title, url, set));
62                 }
63                 return result;
64         }
65
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);
73                 }
74         }
75         
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());
88                                 }
89                         }
90                         section.put(READ_KEY,(String[])readItems.toArray(new String[0]));
91                 }
92         }
93         
94         public static synchronized ArrayList getDefaultChannels() {
95                 ArrayList result = new ArrayList();
96                 try {
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));
105                         }
106                 } catch(Exception e) {
107                         Plugin.logError("Error while getting default feed list", e);
108                 }
109                 return result;
110                 
111         }
112         
113         /**
114          * Returns a non null Channels Section,
115          * creating it if needed.
116          * @return
117          */
118         private static IDialogSettings getChannelsSection() {
119         IDialogSettings section = plugin.getDialogSettings().getSection(BACKENDS_SECTION);
120         if(section == null) {
121                 section = createDefaultChannelsSection();
122         }
123         return section;
124         }
125         
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());
133                 }
134                 return section;
135         }
136         
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) {
147                         //create section
148                         section = backendSection.addNewSection(uid);
149                 } else {
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)) {
154                                         addInOrder = false;
155                                         break;
156                                 }
157                         }
158                 }
159                 //set data
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
164                 if(addInOrder) {
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);
170                 }
171         }
172         
173 }