/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** RSSOwl Development Team - initial API and implementation **
** **
** ********************************************************************** */
package org.rssowl.ui.internal;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.rssowl.core.persist.IFeed;
import org.rssowl.core.persist.INews;
import org.rssowl.core.persist.NewsCounter;
import org.rssowl.core.persist.NewsCounterItem;
import org.rssowl.core.persist.dao.DynamicDAO;
import org.rssowl.core.persist.dao.INewsCounterDAO;
import org.rssowl.core.persist.event.FeedAdapter;
import org.rssowl.core.persist.event.FeedEvent;
import org.rssowl.core.persist.event.NewsAdapter;
import org.rssowl.core.persist.event.NewsEvent;
import org.rssowl.core.persist.reference.FeedLinkReference;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* An internal Service helping Viewers to deal with the presentation of
* INews. Current implemented features:
*
* TODO Introduce a Listener that allows others to register on changes to one of * the fields. This reduces possible errors in case some plugin manages to * register as News-Listener right before this service. *
* * @author bpasero */ public class NewsService { /* The Counter for various aspects of News, the key is the feed link */ private NewsCounter fCounter; /* Delay before Progress is shown */ private static final int SHOW_PROGRESS_THRESHOLD = 200; private INewsCounterDAO fNewsCounterDao; /* Subclass of a Progress Monitor Dialog to show progress after a Crash */ private class NewsServiceProgressMonitorDialog extends ProgressMonitorDialog { NewsServiceProgressMonitorDialog(Shell parent) { super(parent); } /* * @see org.eclipse.jface.dialogs.Dialog#getInitialLocation(org.eclipse.swt.graphics.Point) */ @Override protected Point getInitialLocation(Point initialSize) { Rectangle displayBounds = getParentShell().getDisplay().getPrimaryMonitor().getBounds(); Point shellSize = getInitialSize(); int x = displayBounds.x + (displayBounds.width - shellSize.x) >> 1; int y = displayBounds.y + (displayBounds.height - shellSize.y) >> 1; return new Point(x, y); } } NewsService() { fNewsCounterDao = DynamicDAO.getDAO(INewsCounterDAO.class); fCounter = loadCounter(); registerListeners(); } /** * Returns the number of unread News for the Feed referenced by *feedLinkRef.
*
* @param feedLinkRef The reference to the link of the Feed.
* @return the number of unread News for the Feed having the given Id.
*/
public int getUnreadCount(FeedLinkReference feedLinkRef) {
synchronized (this) {
NewsCounterItem counter = getFromCounter(feedLinkRef);
/* Feed has no news */
if (counter == null)
return 0;
return counter.getUnreadCounter();
}
}
/**
* Returns the number of new News for the Feed referenced by
* feedLinkRef.
*
* @param feedLinkRef The reference to the link of the Feed.
* @return the number of unread News for the Feed having the given link.
*/
public int getNewCount(FeedLinkReference feedLinkRef) {
synchronized (this) {
NewsCounterItem counter = getFromCounter(feedLinkRef);
/* Feed has no news */
if (counter == null)
return 0;
return counter.getNewCounter();
}
}
/**
* Returns the number of sticky News for the Feed referenced by
* feedLinkRef.
*
* @param feedLinkRef The reference to the link of the Feed.
* @return the number of sticky News for the Feed having the given Id.
*/
public int getStickyCount(FeedLinkReference feedLinkRef) {
synchronized (this) {
NewsCounterItem counter = getFromCounter(feedLinkRef);
/* Feed has no news */
if (counter == null)
return 0;
return counter.getStickyCounter();
}
}
/**
* Stops the News-Service and saves all data.
*/
public void stopService() {
synchronized (this) {
saveState();
}
}
/**
* Method only used by Tests!
*/
public void testDirtyShutdown() {
synchronized (this) {
fCounter = loadCounter();
}
}
private synchronized NewsCounter loadCounter() {
/* Load from DB */
NewsCounter counter = fNewsCounterDao.load();
/* Perform initial counting */
if (counter == null)
counter = countAll();
/* Delete it to force recount on dirty shutdown */
else
fNewsCounterDao.delete();
return counter;
}
private NewsCounter countAll() {
final NewsCounter newsCounter = new NewsCounter();
final long start = System.currentTimeMillis();
final Collection