2006-06-17 00:58:24 +02:00
|
|
|
/**
|
2007-03-08 00:03:48 +01:00
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
2006-06-17 00:58:24 +02:00
|
|
|
*
|
2007-03-08 00:03:48 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2006-06-17 00:58:24 +02:00
|
|
|
*/
|
2007-03-08 00:03:48 +01:00
|
|
|
|
2006-06-17 00:58:24 +02:00
|
|
|
package org.jivesoftware.smackx.bookmark;
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
import org.jivesoftware.smack.Connection;
|
2006-06-17 00:58:24 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smackx.PrivateDataManager;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides methods to manage bookmarks in accordance with JEP-0048. Methods for managing URLs and
|
|
|
|
* Conferences are provided.
|
|
|
|
* </p>
|
|
|
|
* It should be noted that some extensions have been made to the JEP. There is an attribute on URLs
|
|
|
|
* that marks a url as a news feed and also a sub-element can be added to either a URL or conference
|
|
|
|
* indicated that it is shared amongst all users on a server.
|
|
|
|
*
|
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public class BookmarkManager {
|
2010-02-09 12:55:56 +01:00
|
|
|
private static final Map<Connection, BookmarkManager> bookmarkManagerMap = new HashMap<Connection, BookmarkManager>();
|
2006-06-17 00:58:24 +02:00
|
|
|
static {
|
|
|
|
PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks",
|
|
|
|
new Bookmarks.Provider());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the <i>BookmarkManager</i> for a connection, if it doesn't exist it is created.
|
|
|
|
*
|
|
|
|
* @param connection the connection for which the manager is desired.
|
|
|
|
* @return Returns the <i>BookmarkManager</i> for a connection, if it doesn't
|
|
|
|
* exist it is created.
|
|
|
|
* @throws XMPPException Thrown if the connection is null or has not yet been authenticated.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public synchronized static BookmarkManager getBookmarkManager(Connection connection)
|
2006-06-17 00:58:24 +02:00
|
|
|
throws XMPPException
|
|
|
|
{
|
|
|
|
BookmarkManager manager = (BookmarkManager) bookmarkManagerMap.get(connection);
|
|
|
|
if(manager == null) {
|
|
|
|
manager = new BookmarkManager(connection);
|
|
|
|
bookmarkManagerMap.put(connection, manager);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PrivateDataManager privateDataManager;
|
|
|
|
private Bookmarks bookmarks;
|
|
|
|
private final Object bookmarkLock = new Object();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor. Registers the data provider with the private data manager in the
|
|
|
|
* storage:bookmarks namespace.
|
|
|
|
*
|
|
|
|
* @param connection the connection for persisting and retrieving bookmarks.
|
|
|
|
* @throws XMPPException thrown when the connection is null or has not been authenticated.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
private BookmarkManager(Connection connection) throws XMPPException {
|
2006-06-17 00:58:24 +02:00
|
|
|
if(connection == null || !connection.isAuthenticated()) {
|
|
|
|
throw new XMPPException("Invalid connection.");
|
|
|
|
}
|
|
|
|
this.privateDataManager = new PrivateDataManager(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all currently bookmarked conferences.
|
|
|
|
*
|
|
|
|
* @return returns all currently bookmarked conferences
|
|
|
|
* @throws XMPPException thrown when there was an error retrieving the current bookmarks from
|
|
|
|
* the server.
|
|
|
|
* @see BookmarkedConference
|
|
|
|
*/
|
2009-06-04 11:56:20 +02:00
|
|
|
public Collection<BookmarkedConference> getBookmarkedConferences() throws XMPPException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
|
|
|
return Collections.unmodifiableCollection(bookmarks.getBookmarkedConferences());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds or updates a conference in the bookmarks.
|
|
|
|
*
|
|
|
|
* @param name the name of the conference
|
|
|
|
* @param jid the jid of the conference
|
|
|
|
* @param isAutoJoin whether or not to join this conference automatically on login
|
|
|
|
* @param nickname the nickname to use for the user when joining the conference
|
|
|
|
* @param password the password to use for the user when joining the conference
|
|
|
|
* @throws XMPPException thrown when there is an issue retrieving the current bookmarks from
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
public void addBookmarkedConference(String name, String jid, boolean isAutoJoin,
|
|
|
|
String nickname, String password) throws XMPPException
|
|
|
|
{
|
|
|
|
retrieveBookmarks();
|
|
|
|
BookmarkedConference bookmark
|
|
|
|
= new BookmarkedConference(name, jid, isAutoJoin, nickname, password);
|
2009-06-04 11:56:20 +02:00
|
|
|
List<BookmarkedConference> conferences = bookmarks.getBookmarkedConferences();
|
2006-06-17 00:58:24 +02:00
|
|
|
if(conferences.contains(bookmark)) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedConference oldConference = conferences.get(conferences.indexOf(bookmark));
|
2006-06-17 00:58:24 +02:00
|
|
|
if(oldConference.isShared()) {
|
|
|
|
throw new IllegalArgumentException("Cannot modify shared bookmark");
|
|
|
|
}
|
|
|
|
oldConference.setAutoJoin(isAutoJoin);
|
|
|
|
oldConference.setName(name);
|
|
|
|
oldConference.setNickname(nickname);
|
|
|
|
oldConference.setPassword(password);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bookmarks.addBookmarkedConference(bookmark);
|
|
|
|
}
|
|
|
|
privateDataManager.setPrivateData(bookmarks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a conference from the bookmarks.
|
|
|
|
*
|
|
|
|
* @param jid the jid of the conference to be removed.
|
|
|
|
* @throws XMPPException thrown when there is a problem with the connection attempting to
|
|
|
|
* retrieve the bookmarks or persist the bookmarks.
|
|
|
|
* @throws IllegalArgumentException thrown when the conference being removed is a shared
|
|
|
|
* conference
|
|
|
|
*/
|
|
|
|
public void removeBookmarkedConference(String jid) throws XMPPException {
|
|
|
|
retrieveBookmarks();
|
2009-06-04 11:56:20 +02:00
|
|
|
Iterator<BookmarkedConference> it = bookmarks.getBookmarkedConferences().iterator();
|
2006-06-17 00:58:24 +02:00
|
|
|
while(it.hasNext()) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedConference conference = it.next();
|
2006-06-17 00:58:24 +02:00
|
|
|
if(conference.getJid().equalsIgnoreCase(jid)) {
|
|
|
|
if(conference.isShared()) {
|
|
|
|
throw new IllegalArgumentException("Conference is shared and can't be removed");
|
|
|
|
}
|
|
|
|
it.remove();
|
|
|
|
privateDataManager.setPrivateData(bookmarks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an unmodifiable collection of all bookmarked urls.
|
|
|
|
*
|
|
|
|
* @return returns an unmodifiable collection of all bookmarked urls.
|
|
|
|
* @throws XMPPException thrown when there is a problem retriving bookmarks from the server.
|
|
|
|
*/
|
2009-06-04 11:56:20 +02:00
|
|
|
public Collection<BookmarkedURL> getBookmarkedURLs() throws XMPPException {
|
2006-06-17 00:58:24 +02:00
|
|
|
retrieveBookmarks();
|
|
|
|
return Collections.unmodifiableCollection(bookmarks.getBookmarkedURLS());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new url or updates an already existing url in the bookmarks.
|
|
|
|
*
|
|
|
|
* @param URL the url of the bookmark
|
|
|
|
* @param name the name of the bookmark
|
|
|
|
* @param isRSS whether or not the url is an rss feed
|
|
|
|
* @throws XMPPException thrown when there is an error retriving or saving bookmarks from or to
|
|
|
|
* the server
|
|
|
|
*/
|
|
|
|
public void addBookmarkedURL(String URL, String name, boolean isRSS) throws XMPPException {
|
|
|
|
retrieveBookmarks();
|
|
|
|
BookmarkedURL bookmark = new BookmarkedURL(URL, name, isRSS);
|
2009-06-04 11:56:20 +02:00
|
|
|
List<BookmarkedURL> urls = bookmarks.getBookmarkedURLS();
|
2006-06-17 00:58:24 +02:00
|
|
|
if(urls.contains(bookmark)) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedURL oldURL = urls.get(urls.indexOf(bookmark));
|
2006-06-17 00:58:24 +02:00
|
|
|
if(oldURL.isShared()) {
|
|
|
|
throw new IllegalArgumentException("Cannot modify shared bookmarks");
|
|
|
|
}
|
|
|
|
oldURL.setName(name);
|
|
|
|
oldURL.setRss(isRSS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bookmarks.addBookmarkedURL(bookmark);
|
|
|
|
}
|
|
|
|
privateDataManager.setPrivateData(bookmarks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a url from the bookmarks.
|
|
|
|
*
|
|
|
|
* @param bookmarkURL the url of the bookmark to remove
|
|
|
|
* @throws XMPPException thrown if there is an error retriving or saving bookmarks from or to
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
public void removeBookmarkedURL(String bookmarkURL) throws XMPPException {
|
|
|
|
retrieveBookmarks();
|
2009-06-04 11:56:20 +02:00
|
|
|
Iterator<BookmarkedURL> it = bookmarks.getBookmarkedURLS().iterator();
|
2006-06-17 00:58:24 +02:00
|
|
|
while(it.hasNext()) {
|
2009-06-04 11:56:20 +02:00
|
|
|
BookmarkedURL bookmark = it.next();
|
2006-06-17 00:58:24 +02:00
|
|
|
if(bookmark.getURL().equalsIgnoreCase(bookmarkURL)) {
|
|
|
|
if(bookmark.isShared()) {
|
|
|
|
throw new IllegalArgumentException("Cannot delete a shared bookmark.");
|
|
|
|
}
|
|
|
|
it.remove();
|
|
|
|
privateDataManager.setPrivateData(bookmarks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bookmarks retrieveBookmarks() throws XMPPException {
|
|
|
|
synchronized(bookmarkLock) {
|
|
|
|
if(bookmarks == null) {
|
|
|
|
bookmarks = (Bookmarks) privateDataManager.getPrivateData("storage",
|
|
|
|
"storage:bookmarks");
|
|
|
|
}
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|