core/com.nokia.carbide.cpp.news.reader/src/com/nokia/carbide/cpp/internal/news/reader/feed/FeedManager.java
author stechong
Thu, 16 Apr 2009 14:39:51 -0500
branchRCL_2_0
changeset 99 3604ec4c983e
parent 83 6c6d4b0c9171
child 104 4ca9a303c257
permissions -rw-r--r--
Fix for Bug 8890.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
package com.nokia.carbide.cpp.internal.news.reader.feed;
cawthron
parents:
diff changeset
    19
cawthron
parents:
diff changeset
    20
import java.io.File;
cawthron
parents:
diff changeset
    21
import java.io.FileInputStream;
cawthron
parents:
diff changeset
    22
import java.io.InputStream;
cawthron
parents:
diff changeset
    23
import java.net.URL;
cawthron
parents:
diff changeset
    24
import java.util.ArrayList;
cawthron
parents:
diff changeset
    25
import java.util.Iterator;
cawthron
parents:
diff changeset
    26
import java.util.List;
cawthron
parents:
diff changeset
    27
import java.util.Properties;
cawthron
parents:
diff changeset
    28
import java.util.Timer;
cawthron
parents:
diff changeset
    29
import java.util.TimerTask;
cawthron
parents:
diff changeset
    30
cawthron
parents:
diff changeset
    31
import org.eclipse.core.runtime.IPath;
cawthron
parents:
diff changeset
    32
import org.eclipse.core.runtime.Path;
cawthron
parents:
diff changeset
    33
import org.eclipse.core.runtime.Platform;
cawthron
parents:
diff changeset
    34
import org.eclipse.jface.preference.IPreferenceStore;
cawthron
parents:
diff changeset
    35
import org.eclipse.osgi.service.datalocation.Location;
cawthron
parents:
diff changeset
    36
cawthron
parents:
diff changeset
    37
import com.nokia.carbide.cpp.internal.news.reader.CarbideNewsReaderPlugin;
cawthron
parents:
diff changeset
    38
import com.nokia.carbide.cpp.internal.news.reader.gen.FeedCache.FeedCacheManager;
cawthron
parents:
diff changeset
    39
import com.nokia.carbide.cpp.internal.news.reader.gen.FeedInfo.FeedInfoConstants;
cawthron
parents:
diff changeset
    40
import com.nokia.carbide.cpp.internal.news.reader.gen.FeedInfo.FeedInfoManager;
cawthron
parents:
diff changeset
    41
import com.nokia.carbide.cpp.internal.news.reader.gen.FeedInfo.FeedType;
cawthron
parents:
diff changeset
    42
import com.nokia.carbide.cpp.internal.news.reader.ui.NewsPreferenceConstants;
cawthron
parents:
diff changeset
    43
import com.sun.syndication.feed.synd.SyndFeed;
cawthron
parents:
diff changeset
    44
import com.sun.syndication.fetcher.FeedFetcher;
cawthron
parents:
diff changeset
    45
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
cawthron
parents:
diff changeset
    46
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
cawthron
parents:
diff changeset
    47
cawthron
parents:
diff changeset
    48
/**
cawthron
parents:
diff changeset
    49
 * A class to manage feeds for the Carbide.c++ news reader.
cawthron
parents:
diff changeset
    50
 *
cawthron
parents:
diff changeset
    51
 */
cawthron
parents:
diff changeset
    52
public class FeedManager {
cawthron
parents:
diff changeset
    53
cawthron
parents:
diff changeset
    54
	// public data
cawthron
parents:
diff changeset
    55
	public static final String FEED_CACHE_FILE_NAME = "carbide.c++.news.xml";
cawthron
parents:
diff changeset
    56
	public static final String PROPERTIES_FILE_LOCATION = "configuration/server.properties";
cawthron
parents:
diff changeset
    57
cawthron
parents:
diff changeset
    58
	// private data
cawthron
parents:
diff changeset
    59
	private FeedCacheManager feedCacheManager;
cawthron
parents:
diff changeset
    60
	private FeedInfoManager feedListingManager;
cawthron
parents:
diff changeset
    61
	private List<CarbideSyndFeed> newsFeeds;
cawthron
parents:
diff changeset
    62
	private CarbideSyndFeed resourceFeed;
cawthron
parents:
diff changeset
    63
	private Timer updateTimer;
cawthron
parents:
diff changeset
    64
cawthron
parents:
diff changeset
    65
	/**
cawthron
parents:
diff changeset
    66
	 * Private class to handle scheduled feeds updates.
cawthron
parents:
diff changeset
    67
	 *
cawthron
parents:
diff changeset
    68
	 */
cawthron
parents:
diff changeset
    69
	private class UpdateTimerTask extends TimerTask {
cawthron
parents:
diff changeset
    70
		/*
cawthron
parents:
diff changeset
    71
		 * (non-Javadoc)
cawthron
parents:
diff changeset
    72
		 * @see java.util.TimerTask#run()
cawthron
parents:
diff changeset
    73
		 */
cawthron
parents:
diff changeset
    74
		public void run(){
cawthron
parents:
diff changeset
    75
			CarbideNewsReaderPlugin.updateFeeds();
cawthron
parents:
diff changeset
    76
		}
cawthron
parents:
diff changeset
    77
	}
cawthron
parents:
diff changeset
    78
cawthron
parents:
diff changeset
    79
	/**
cawthron
parents:
diff changeset
    80
	 * The constructor.
cawthron
parents:
diff changeset
    81
	 */
cawthron
parents:
diff changeset
    82
	public FeedManager() {
cawthron
parents:
diff changeset
    83
		feedCacheManager = new FeedCacheManager();
cawthron
parents:
diff changeset
    84
		feedListingManager = new FeedInfoManager();
cawthron
parents:
diff changeset
    85
		newsFeeds = new ArrayList<CarbideSyndFeed>();
cawthron
parents:
diff changeset
    86
	}
cawthron
parents:
diff changeset
    87
cawthron
parents:
diff changeset
    88
	public boolean isCacheEmpty() {
cawthron
parents:
diff changeset
    89
		return feedCacheManager.isCacheEmpty();
cawthron
parents:
diff changeset
    90
	}
cawthron
parents:
diff changeset
    91
cawthron
parents:
diff changeset
    92
	/**
cawthron
parents:
diff changeset
    93
	 * Load feeds listed in the news feeds listing file.
cawthron
parents:
diff changeset
    94
	 */
cawthron
parents:
diff changeset
    95
	@SuppressWarnings("static-access")
cawthron
parents:
diff changeset
    96
	public void loadFeeds() {
cawthron
parents:
diff changeset
    97
		try {
cawthron
parents:
diff changeset
    98
			if (!loadFeedListing()) {
cawthron
parents:
diff changeset
    99
				return;
cawthron
parents:
diff changeset
   100
			}
cawthron
parents:
diff changeset
   101
cawthron
parents:
diff changeset
   102
			if (feedCacheManager.isCacheEmpty()) {
cawthron
parents:
diff changeset
   103
				loadFeedCache();
cawthron
parents:
diff changeset
   104
			}
cawthron
parents:
diff changeset
   105
			clearFeeds();
cawthron
parents:
diff changeset
   106
			updateCache();
cawthron
parents:
diff changeset
   107
			feedCacheManager.fireFeedCacheChanged(feedCacheManager.isCacheUpdated());
cawthron
parents:
diff changeset
   108
			feedCacheManager.setCacheUpdated(false);
cawthron
parents:
diff changeset
   109
		} catch (Exception e) {
cawthron
parents:
diff changeset
   110
			CarbideNewsReaderPlugin.log(e);
cawthron
parents:
diff changeset
   111
		}		
cawthron
parents:
diff changeset
   112
	}
cawthron
parents:
diff changeset
   113
cawthron
parents:
diff changeset
   114
	/**
cawthron
parents:
diff changeset
   115
	 * Retrieve all the currently available news feeds.
cawthron
parents:
diff changeset
   116
	 * @return list of feed objects
cawthron
parents:
diff changeset
   117
	 */
cawthron
parents:
diff changeset
   118
	public List<CarbideSyndFeed> getNewsFeeds() {
cawthron
parents:
diff changeset
   119
		validateFeeds();
cawthron
parents:
diff changeset
   120
		return newsFeeds;
cawthron
parents:
diff changeset
   121
	}
cawthron
parents:
diff changeset
   122
cawthron
parents:
diff changeset
   123
	/**
cawthron
parents:
diff changeset
   124
	 * Retrieve a property string from the news reader properties file.
cawthron
parents:
diff changeset
   125
	 * @param key - key used to identify the property string
cawthron
parents:
diff changeset
   126
	 * @return property string if found; null otherwise
cawthron
parents:
diff changeset
   127
	 */
cawthron
parents:
diff changeset
   128
	public String getProperty(String key) {
cawthron
parents:
diff changeset
   129
		try {
cawthron
parents:
diff changeset
   130
			Location installLocation = Platform.getInstallLocation();
cawthron
parents:
diff changeset
   131
			if (installLocation == null) {
cawthron
parents:
diff changeset
   132
				return null;
cawthron
parents:
diff changeset
   133
			}
cawthron
parents:
diff changeset
   134
cawthron
parents:
diff changeset
   135
			URL url = installLocation.getURL();
cawthron
parents:
diff changeset
   136
			if (url == null) {
cawthron
parents:
diff changeset
   137
				return null;
cawthron
parents:
diff changeset
   138
			}
cawthron
parents:
diff changeset
   139
cawthron
parents:
diff changeset
   140
			IPath path = new Path(url.getPath());
cawthron
parents:
diff changeset
   141
			path = path.append(PROPERTIES_FILE_LOCATION);
cawthron
parents:
diff changeset
   142
			File file = path.toFile();
cawthron
parents:
diff changeset
   143
			InputStream is = new FileInputStream(file);
cawthron
parents:
diff changeset
   144
			Properties properties = new Properties();
cawthron
parents:
diff changeset
   145
			properties.load(is);
cawthron
parents:
diff changeset
   146
			is.close();
cawthron
parents:
diff changeset
   147
			String result = (String)properties.get(key);
cawthron
parents:
diff changeset
   148
			return result;
cawthron
parents:
diff changeset
   149
		} catch (Exception e) {
cawthron
parents:
diff changeset
   150
			CarbideNewsReaderPlugin.log(e);
cawthron
parents:
diff changeset
   151
		}		
cawthron
parents:
diff changeset
   152
cawthron
parents:
diff changeset
   153
		return null;
cawthron
parents:
diff changeset
   154
	}
cawthron
parents:
diff changeset
   155
cawthron
parents:
diff changeset
   156
	/**
cawthron
parents:
diff changeset
   157
	 * Retrieve the resource feed.
cawthron
parents:
diff changeset
   158
	 * @return resource feed object
cawthron
parents:
diff changeset
   159
	 */
cawthron
parents:
diff changeset
   160
	public CarbideSyndFeed getResourceFeed() {
cawthron
parents:
diff changeset
   161
		validateFeeds();
cawthron
parents:
diff changeset
   162
		return resourceFeed;
cawthron
parents:
diff changeset
   163
	}
cawthron
parents:
diff changeset
   164
cawthron
parents:
diff changeset
   165
	public List<CarbideSyndFeed> getSubscribedNewsFeeds() {
cawthron
parents:
diff changeset
   166
		validateFeeds();
cawthron
parents:
diff changeset
   167
		List<CarbideSyndFeed> subscribedFeeds = new ArrayList<CarbideSyndFeed>();
cawthron
parents:
diff changeset
   168
		for (Iterator<CarbideSyndFeed> iterator = newsFeeds.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   169
			CarbideSyndFeed feed = iterator.next();
cawthron
parents:
diff changeset
   170
			if (feed.isSubscribed()) {
cawthron
parents:
diff changeset
   171
				subscribedFeeds.add(feed);
cawthron
parents:
diff changeset
   172
			}
cawthron
parents:
diff changeset
   173
		}
cawthron
parents:
diff changeset
   174
		return subscribedFeeds;
cawthron
parents:
diff changeset
   175
	}
cawthron
parents:
diff changeset
   176
cawthron
parents:
diff changeset
   177
	/**
cawthron
parents:
diff changeset
   178
	 * Return the total number of unread entries from subscribed news feeds.
cawthron
parents:
diff changeset
   179
	 * @return total number of unread entries from subscribed news feeds
cawthron
parents:
diff changeset
   180
	 */
cawthron
parents:
diff changeset
   181
	public int getUnreadEntriesCount() {
cawthron
parents:
diff changeset
   182
		int count = 0;
cawthron
parents:
diff changeset
   183
		for (Iterator<CarbideSyndFeed> iterator = newsFeeds.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   184
			CarbideSyndFeed feed = iterator.next();
cawthron
parents:
diff changeset
   185
			if (feed.isSubscribed()) {
cawthron
parents:
diff changeset
   186
				count += feed.getUnreadEntries().size();
cawthron
parents:
diff changeset
   187
			}
cawthron
parents:
diff changeset
   188
		}
cawthron
parents:
diff changeset
   189
		return count;
cawthron
parents:
diff changeset
   190
	}
cawthron
parents:
diff changeset
   191
cawthron
parents:
diff changeset
   192
	/**
99
3604ec4c983e Fix for Bug 8890.
stechong
parents: 83
diff changeset
   193
	 * Search for a feed entry by name and then mark it as read.
3604ec4c983e Fix for Bug 8890.
stechong
parents: 83
diff changeset
   194
	 * @param entries - feed entries to be checked
3604ec4c983e Fix for Bug 8890.
stechong
parents: 83
diff changeset
   195
	 * @param entryTitle - title of the entry to be marked as read
2
cawthron
parents:
diff changeset
   196
	 */
99
3604ec4c983e Fix for Bug 8890.
stechong
parents: 83
diff changeset
   197
	public void markEntryAsRead(List<CarbideSyndEntry> entries, String entryTitle) {
2
cawthron
parents:
diff changeset
   198
		if (entries == null || entryTitle == null) {
cawthron
parents:
diff changeset
   199
			return;
cawthron
parents:
diff changeset
   200
		}
cawthron
parents:
diff changeset
   201
cawthron
parents:
diff changeset
   202
		for (Iterator<CarbideSyndEntry> iterator = entries.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   203
			CarbideSyndEntry entry = iterator.next();
cawthron
parents:
diff changeset
   204
			String title = entry.getTitle();
cawthron
parents:
diff changeset
   205
			title = title.replaceAll("\n", "");
99
3604ec4c983e Fix for Bug 8890.
stechong
parents: 83
diff changeset
   206
			if (title.equals(entryTitle) && !entry.isRead()) {
2
cawthron
parents:
diff changeset
   207
				entry.setRead(true);
cawthron
parents:
diff changeset
   208
				break;
cawthron
parents:
diff changeset
   209
			}
cawthron
parents:
diff changeset
   210
		}
cawthron
parents:
diff changeset
   211
		
cawthron
parents:
diff changeset
   212
	}
cawthron
parents:
diff changeset
   213
cawthron
parents:
diff changeset
   214
	/**
cawthron
parents:
diff changeset
   215
	 * Reset the update timer.
cawthron
parents:
diff changeset
   216
	 */
cawthron
parents:
diff changeset
   217
	public void resetUpdateTimer() {
cawthron
parents:
diff changeset
   218
		if (updateTimer == null) {
cawthron
parents:
diff changeset
   219
			updateTimer = new Timer();
cawthron
parents:
diff changeset
   220
		}
cawthron
parents:
diff changeset
   221
		long updateInterval = getUpdateInterval();
cawthron
parents:
diff changeset
   222
		UpdateTimerTask updateTask = new UpdateTimerTask();
cawthron
parents:
diff changeset
   223
		updateTimer.schedule(updateTask, updateInterval);
cawthron
parents:
diff changeset
   224
	}
cawthron
parents:
diff changeset
   225
cawthron
parents:
diff changeset
   226
	/**
cawthron
parents:
diff changeset
   227
	 * Save relevant feed information to local cache.
cawthron
parents:
diff changeset
   228
	 */
cawthron
parents:
diff changeset
   229
	public void saveFeeds() {
cawthron
parents:
diff changeset
   230
		feedCacheManager.clearFeedCache();
cawthron
parents:
diff changeset
   231
		for (Iterator<CarbideSyndFeed> iterator = newsFeeds.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   232
			CarbideSyndFeed feed = iterator.next();
cawthron
parents:
diff changeset
   233
			feedCacheManager.saveFeedToCache(feed);
cawthron
parents:
diff changeset
   234
		}
cawthron
parents:
diff changeset
   235
		feedCacheManager.saveFeedToCache(resourceFeed);
cawthron
parents:
diff changeset
   236
		saveFeedCache();
cawthron
parents:
diff changeset
   237
	}
cawthron
parents:
diff changeset
   238
cawthron
parents:
diff changeset
   239
	/**
cawthron
parents:
diff changeset
   240
	 * Perform any necessary actions when the total number of unread entries from 
cawthron
parents:
diff changeset
   241
	 * subscribed news feeds has changed.
cawthron
parents:
diff changeset
   242
	 */
cawthron
parents:
diff changeset
   243
	@SuppressWarnings("static-access")
cawthron
parents:
diff changeset
   244
	public void unreadEntriesCountChanged() {
cawthron
parents:
diff changeset
   245
		feedCacheManager.fireFeedCacheChanged(false);
cawthron
parents:
diff changeset
   246
	}
cawthron
parents:
diff changeset
   247
cawthron
parents:
diff changeset
   248
	/**
cawthron
parents:
diff changeset
   249
	 * Update all news and resource feeds.
cawthron
parents:
diff changeset
   250
	 */
cawthron
parents:
diff changeset
   251
	@SuppressWarnings("static-access")
cawthron
parents:
diff changeset
   252
	public void updateFeeds() {
cawthron
parents:
diff changeset
   253
		try {
cawthron
parents:
diff changeset
   254
			if (!loadFeedListing()) {
cawthron
parents:
diff changeset
   255
				return;
cawthron
parents:
diff changeset
   256
			}
cawthron
parents:
diff changeset
   257
cawthron
parents:
diff changeset
   258
			clearFeeds();
cawthron
parents:
diff changeset
   259
			updateCache();
cawthron
parents:
diff changeset
   260
			feedCacheManager.fireFeedCacheChanged(feedCacheManager.isCacheUpdated());
cawthron
parents:
diff changeset
   261
			feedCacheManager.setCacheUpdated(false);
cawthron
parents:
diff changeset
   262
		} catch (Exception e) {
cawthron
parents:
diff changeset
   263
			CarbideNewsReaderPlugin.log(e);
cawthron
parents:
diff changeset
   264
		}		
cawthron
parents:
diff changeset
   265
	}
cawthron
parents:
diff changeset
   266
cawthron
parents:
diff changeset
   267
	/**
cawthron
parents:
diff changeset
   268
	 * Add a news feed. The news reader can have multiple news feeds.
cawthron
parents:
diff changeset
   269
	 * @param feed - incoming news feed
cawthron
parents:
diff changeset
   270
	 * @return true on success
cawthron
parents:
diff changeset
   271
	 */
cawthron
parents:
diff changeset
   272
	private boolean addNewsFeed(CarbideSyndFeed feed) {
cawthron
parents:
diff changeset
   273
		if (feed == null) {
cawthron
parents:
diff changeset
   274
			return false;
cawthron
parents:
diff changeset
   275
		}
cawthron
parents:
diff changeset
   276
cawthron
parents:
diff changeset
   277
		if (feed != null ) {
cawthron
parents:
diff changeset
   278
			boolean feedExist = false;
cawthron
parents:
diff changeset
   279
			for (Iterator<CarbideSyndFeed> iterator = newsFeeds.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   280
				CarbideSyndFeed aFeed = iterator.next();
cawthron
parents:
diff changeset
   281
				if (aFeed.getTitle().equals(feed.getTitle()) &&
cawthron
parents:
diff changeset
   282
					aFeed.getLink().equals(feed.getLink())) {
cawthron
parents:
diff changeset
   283
					int index = newsFeeds.indexOf(aFeed);
cawthron
parents:
diff changeset
   284
					newsFeeds.set(index, feed);
cawthron
parents:
diff changeset
   285
					feedExist = true;
cawthron
parents:
diff changeset
   286
				}
cawthron
parents:
diff changeset
   287
			}
cawthron
parents:
diff changeset
   288
cawthron
parents:
diff changeset
   289
			if (!feedExist) {
cawthron
parents:
diff changeset
   290
				feed.setType(FeedInfoConstants.FEED_TYPE_NEWS);
cawthron
parents:
diff changeset
   291
				newsFeeds.add(feed);
cawthron
parents:
diff changeset
   292
			}
cawthron
parents:
diff changeset
   293
			return true;
cawthron
parents:
diff changeset
   294
		}
cawthron
parents:
diff changeset
   295
		return false;
cawthron
parents:
diff changeset
   296
	}
cawthron
parents:
diff changeset
   297
cawthron
parents:
diff changeset
   298
	/**
cawthron
parents:
diff changeset
   299
	 * Add a resource feed. The news reader can only have one resource feed.
cawthron
parents:
diff changeset
   300
	 * @param feed - incoming resource feed
cawthron
parents:
diff changeset
   301
	 * @return true on success
cawthron
parents:
diff changeset
   302
	 */
cawthron
parents:
diff changeset
   303
	private boolean addResourceFeed(CarbideSyndFeed feed) {
cawthron
parents:
diff changeset
   304
		if (feed == null) {
cawthron
parents:
diff changeset
   305
			return false;
cawthron
parents:
diff changeset
   306
		}
cawthron
parents:
diff changeset
   307
cawthron
parents:
diff changeset
   308
		resourceFeed = feed;
cawthron
parents:
diff changeset
   309
		if (resourceFeed != null) {
cawthron
parents:
diff changeset
   310
			resourceFeed.setType(FeedInfoConstants.FEED_TYPE_RESOURCE);
cawthron
parents:
diff changeset
   311
			return true;
cawthron
parents:
diff changeset
   312
		}
cawthron
parents:
diff changeset
   313
		return false;
cawthron
parents:
diff changeset
   314
	}
cawthron
parents:
diff changeset
   315
cawthron
parents:
diff changeset
   316
	/**
cawthron
parents:
diff changeset
   317
	 * Clear all news and resource feeds.
cawthron
parents:
diff changeset
   318
	 */
cawthron
parents:
diff changeset
   319
	private void clearFeeds() {
cawthron
parents:
diff changeset
   320
		newsFeeds.clear();
cawthron
parents:
diff changeset
   321
		resourceFeed = null;
cawthron
parents:
diff changeset
   322
	}
cawthron
parents:
diff changeset
   323
cawthron
parents:
diff changeset
   324
	/**
cawthron
parents:
diff changeset
   325
	 * Feed feed content from its server.
cawthron
parents:
diff changeset
   326
	 * @param feedUrl - URL of the feed
cawthron
parents:
diff changeset
   327
	 * @return feed object on success; null otherwise
cawthron
parents:
diff changeset
   328
	 */
cawthron
parents:
diff changeset
   329
	private CarbideSyndFeed fetchFeed(URL feedUrl) throws Exception {
cawthron
parents:
diff changeset
   330
		if (feedUrl == null) {
cawthron
parents:
diff changeset
   331
			return null;
cawthron
parents:
diff changeset
   332
		}
cawthron
parents:
diff changeset
   333
cawthron
parents:
diff changeset
   334
		FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
83
6c6d4b0c9171 Specifies acceptable content types in connection request header; fix for Bug 8775.
stechong
parents: 81
diff changeset
   335
		FeedFetcher fetcher = new CarbideFeedFetcher(feedInfoCache);
2
cawthron
parents:
diff changeset
   336
		SyndFeed sFeed = fetcher.retrieveFeed(feedUrl);
cawthron
parents:
diff changeset
   337
		CarbideSyndFeed feed = new CarbideSyndFeed(sFeed);
cawthron
parents:
diff changeset
   338
		if (feed != null) {
cawthron
parents:
diff changeset
   339
			feed.setSubscribed(true);
cawthron
parents:
diff changeset
   340
			feed.setLink(feedUrl.toString());
cawthron
parents:
diff changeset
   341
			return feed;
cawthron
parents:
diff changeset
   342
		}
cawthron
parents:
diff changeset
   343
		return null;
cawthron
parents:
diff changeset
   344
	}
cawthron
parents:
diff changeset
   345
cawthron
parents:
diff changeset
   346
	/**
cawthron
parents:
diff changeset
   347
	 * Get the default local feeds cache file.
cawthron
parents:
diff changeset
   348
	 * @return default local feeds cache
cawthron
parents:
diff changeset
   349
	 */
cawthron
parents:
diff changeset
   350
	private File getDefaultFeedCacheFile() {
cawthron
parents:
diff changeset
   351
		String location = System.getProperty("user.home") + File.separator + 
cawthron
parents:
diff changeset
   352
						  FEED_CACHE_FILE_NAME;
cawthron
parents:
diff changeset
   353
		File file = new File(location);
cawthron
parents:
diff changeset
   354
		return file;
cawthron
parents:
diff changeset
   355
	}
cawthron
parents:
diff changeset
   356
cawthron
parents:
diff changeset
   357
	/**
cawthron
parents:
diff changeset
   358
	 * Get the update interval from news reader preferences.
cawthron
parents:
diff changeset
   359
	 * @return update interval
cawthron
parents:
diff changeset
   360
	 */
cawthron
parents:
diff changeset
   361
	private long getUpdateInterval() {
cawthron
parents:
diff changeset
   362
		IPreferenceStore store = CarbideNewsReaderPlugin.getPrefsStore();
cawthron
parents:
diff changeset
   363
		long interval = store.getLong(NewsPreferenceConstants.UPDATE_INTERVAL) * 1000 * 60 * 60;
cawthron
parents:
diff changeset
   364
		return interval;
cawthron
parents:
diff changeset
   365
	}
cawthron
parents:
diff changeset
   366
cawthron
parents:
diff changeset
   367
	/**
cawthron
parents:
diff changeset
   368
	 * Load feed information from local cache.
cawthron
parents:
diff changeset
   369
	 */
cawthron
parents:
diff changeset
   370
	private void loadFeedCache() throws Exception {
cawthron
parents:
diff changeset
   371
		File file = getDefaultFeedCacheFile();
cawthron
parents:
diff changeset
   372
		URL url = file.toURL();
cawthron
parents:
diff changeset
   373
		if (file.exists()) {
cawthron
parents:
diff changeset
   374
			if (url != null) {
cawthron
parents:
diff changeset
   375
				feedCacheManager.loadCacheFromFile(url);
cawthron
parents:
diff changeset
   376
			}
cawthron
parents:
diff changeset
   377
		}
cawthron
parents:
diff changeset
   378
		else {
cawthron
parents:
diff changeset
   379
			file.createNewFile();
cawthron
parents:
diff changeset
   380
			feedCacheManager.createDefaultFeedCache();
cawthron
parents:
diff changeset
   381
			feedCacheManager.saveCacheToFile(url);
cawthron
parents:
diff changeset
   382
		}
cawthron
parents:
diff changeset
   383
	}
cawthron
parents:
diff changeset
   384
cawthron
parents:
diff changeset
   385
	/**
cawthron
parents:
diff changeset
   386
	 * Load feed information from feed listing file. 
cawthron
parents:
diff changeset
   387
	 * @return true on success; false otherwise
cawthron
parents:
diff changeset
   388
	 */
cawthron
parents:
diff changeset
   389
	private boolean loadFeedListing() throws Exception {
cawthron
parents:
diff changeset
   390
		URL url = feedListingManager.getFeedInfoFileURL();
cawthron
parents:
diff changeset
   391
		if (url != null) {
cawthron
parents:
diff changeset
   392
			return feedListingManager.loadFeedInfo(url);
cawthron
parents:
diff changeset
   393
		}
cawthron
parents:
diff changeset
   394
		return false;
cawthron
parents:
diff changeset
   395
	}
cawthron
parents:
diff changeset
   396
cawthron
parents:
diff changeset
   397
	/**
cawthron
parents:
diff changeset
   398
	 * Save feed information to local cache.
cawthron
parents:
diff changeset
   399
	 */
cawthron
parents:
diff changeset
   400
	private void saveFeedCache() {
cawthron
parents:
diff changeset
   401
		try {
cawthron
parents:
diff changeset
   402
			File file = getDefaultFeedCacheFile();
cawthron
parents:
diff changeset
   403
			URL url = file.toURL();
cawthron
parents:
diff changeset
   404
			if (file.exists()) {
cawthron
parents:
diff changeset
   405
				if (url != null) {
cawthron
parents:
diff changeset
   406
					feedCacheManager.saveCacheToFile(url);
cawthron
parents:
diff changeset
   407
				}
cawthron
parents:
diff changeset
   408
			}
cawthron
parents:
diff changeset
   409
			else {
cawthron
parents:
diff changeset
   410
				file.createNewFile();
cawthron
parents:
diff changeset
   411
				feedCacheManager.saveCacheToFile(url);
cawthron
parents:
diff changeset
   412
			}
cawthron
parents:
diff changeset
   413
		} catch (Exception e) {
cawthron
parents:
diff changeset
   414
			CarbideNewsReaderPlugin.log(e);
cawthron
parents:
diff changeset
   415
		}		
cawthron
parents:
diff changeset
   416
	}
cawthron
parents:
diff changeset
   417
cawthron
parents:
diff changeset
   418
	/**
cawthron
parents:
diff changeset
   419
	 * Update local cache of feeds.
cawthron
parents:
diff changeset
   420
	 */
cawthron
parents:
diff changeset
   421
	private void updateCache() throws Exception {
cawthron
parents:
diff changeset
   422
		List<FeedType> feedInfoList = feedListingManager.getFeedInfo().getFeeds().getFeed();
cawthron
parents:
diff changeset
   423
		for (Iterator<FeedType> iterator = feedInfoList.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   424
			FeedType feedInfo = iterator.next();
cawthron
parents:
diff changeset
   425
			URL feedUrl = new URL(feedInfo.getUrl());
cawthron
parents:
diff changeset
   426
			if (feedUrl != null) {
cawthron
parents:
diff changeset
   427
				CarbideSyndFeed feed = fetchFeed(feedUrl);
cawthron
parents:
diff changeset
   428
				if (feed != null) {
cawthron
parents:
diff changeset
   429
					if (feedCacheManager.isFeedInCache(feed)) {
cawthron
parents:
diff changeset
   430
						feedCacheManager.syncFeedWithCache(feed);
cawthron
parents:
diff changeset
   431
					}
cawthron
parents:
diff changeset
   432
					else {
cawthron
parents:
diff changeset
   433
						feed.setIsNew(true);
cawthron
parents:
diff changeset
   434
					}
cawthron
parents:
diff changeset
   435
					feedCacheManager.saveFeedToCache(feed);
cawthron
parents:
diff changeset
   436
cawthron
parents:
diff changeset
   437
					if (FeedInfoManager.isNewsFeed(feedInfo)) {
cawthron
parents:
diff changeset
   438
						if (!addNewsFeed(feed)) {
cawthron
parents:
diff changeset
   439
							break;
cawthron
parents:
diff changeset
   440
						}
cawthron
parents:
diff changeset
   441
					}
cawthron
parents:
diff changeset
   442
					else
cawthron
parents:
diff changeset
   443
					if (FeedInfoManager.isResourceFeed(feedInfo)) {
cawthron
parents:
diff changeset
   444
						if (!addResourceFeed(feed)) {
cawthron
parents:
diff changeset
   445
							break;
cawthron
parents:
diff changeset
   446
						}
cawthron
parents:
diff changeset
   447
					}
cawthron
parents:
diff changeset
   448
				}
cawthron
parents:
diff changeset
   449
			}
cawthron
parents:
diff changeset
   450
		}
cawthron
parents:
diff changeset
   451
		// reset the update timer after each cache update
cawthron
parents:
diff changeset
   452
		resetUpdateTimer();
cawthron
parents:
diff changeset
   453
	}
cawthron
parents:
diff changeset
   454
cawthron
parents:
diff changeset
   455
	/**
cawthron
parents:
diff changeset
   456
	 * Check news and resource feeds to make sure they are valid.
cawthron
parents:
diff changeset
   457
	 * Reload feeds from cache if necessary.
cawthron
parents:
diff changeset
   458
	 */
cawthron
parents:
diff changeset
   459
	private void validateFeeds() {
81
985775f74012 Fix for Bug 8705.
stechong
parents: 2
diff changeset
   460
		if ((newsFeeds == null || newsFeeds.size() == 0) && resourceFeed == null) {
2
cawthron
parents:
diff changeset
   461
			try {
cawthron
parents:
diff changeset
   462
				if (!loadFeedListing()) {
cawthron
parents:
diff changeset
   463
					return;
cawthron
parents:
diff changeset
   464
				}
cawthron
parents:
diff changeset
   465
cawthron
parents:
diff changeset
   466
				loadFeedCache();
cawthron
parents:
diff changeset
   467
				List<FeedType> feedInfoList = feedListingManager.getFeedInfo().getFeeds().getFeed();
cawthron
parents:
diff changeset
   468
				for (Iterator<FeedType> iterator = feedInfoList.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   469
					FeedType feedInfo = iterator.next();
cawthron
parents:
diff changeset
   470
					URL feedUrl = new URL(feedInfo.getUrl());
cawthron
parents:
diff changeset
   471
					CarbideSyndFeed feed = feedCacheManager.loadFeedFromCache(feedUrl);
cawthron
parents:
diff changeset
   472
					if (feed != null) {
cawthron
parents:
diff changeset
   473
						if (FeedInfoManager.isNewsFeed(feedInfo)) {
cawthron
parents:
diff changeset
   474
							if (!addNewsFeed(feed)) {
cawthron
parents:
diff changeset
   475
								break;
cawthron
parents:
diff changeset
   476
							}
cawthron
parents:
diff changeset
   477
						}
cawthron
parents:
diff changeset
   478
						else
cawthron
parents:
diff changeset
   479
						if (FeedInfoManager.isResourceFeed(feedInfo)) {
cawthron
parents:
diff changeset
   480
							if (!addResourceFeed(feed)) {
cawthron
parents:
diff changeset
   481
								break;
cawthron
parents:
diff changeset
   482
							}
cawthron
parents:
diff changeset
   483
						}
cawthron
parents:
diff changeset
   484
					}
cawthron
parents:
diff changeset
   485
				}
cawthron
parents:
diff changeset
   486
			} catch (Exception e) {
cawthron
parents:
diff changeset
   487
				CarbideNewsReaderPlugin.log(e);
cawthron
parents:
diff changeset
   488
			}		
cawthron
parents:
diff changeset
   489
		}
cawthron
parents:
diff changeset
   490
	}
cawthron
parents:
diff changeset
   491
cawthron
parents:
diff changeset
   492
}