aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/bookmark/BookmarkManager.java
blob: f85cc9c10c535397dfd640f8a57c31cbf8cb46d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * $Revision$
 * $Date$
 *
 * 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.
 */

package org.jivesoftware.smackx.bookmark;

import org.jivesoftware.smack.Connection;
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 {
    private static final Map<Connection, BookmarkManager> bookmarkManagerMap = new HashMap<Connection, BookmarkManager>();
    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.
     */
    public synchronized static BookmarkManager getBookmarkManager(Connection connection)
            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.
     */
    private BookmarkManager(Connection connection) throws XMPPException {
        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
     */
    public Collection<BookmarkedConference> getBookmarkedConferences() throws XMPPException {
        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);
        List<BookmarkedConference> conferences = bookmarks.getBookmarkedConferences();
        if(conferences.contains(bookmark)) {
            BookmarkedConference oldConference = conferences.get(conferences.indexOf(bookmark));
            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();
        Iterator<BookmarkedConference> it = bookmarks.getBookmarkedConferences().iterator();
        while(it.hasNext()) {
            BookmarkedConference conference = it.next();
            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.
     */
    public Collection<BookmarkedURL> getBookmarkedURLs() throws XMPPException {
        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);
        List<BookmarkedURL> urls = bookmarks.getBookmarkedURLS();
        if(urls.contains(bookmark)) {
            BookmarkedURL oldURL = urls.get(urls.indexOf(bookmark));
            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();
        Iterator<BookmarkedURL> it = bookmarks.getBookmarkedURLS().iterator();
        while(it.hasNext()) {
            BookmarkedURL bookmark = it.next();
            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;
        }
    }
}