001 package com.valhalla.jbother.jabber.smack;
002
003 import java.util.Vector;
004 import java.util.Iterator;
005 import org.jivesoftware.smack.packet.*;
006 import org.jivesoftware.smackx.packet.*;
007 /**
008 * Represents jabber bookmarks (JEP-0048)
009 * @author George Weinberg
010 *
011 *
012 * To create bookmarks on the server, create the bookmark object,
013 * use the addURL and addConference methods to add all the bookmarks,
014 * and call the PrivateDataManager.setData method on the bookmark.
015 *
016 * to retrieve bookmarks from the server, register the BookmarkProvider
017 * class with a PrivateDataManager, then callthe getPrivateData method.
018 *
019 ** There are two types of bookmarks, urls and conferences.
020 * This class will not allow you to add two urls or two conferences with
021 * the same name, but an url may have the same name as a conference,
022 * and bookmarks created with other apps might have duplicate names, the
023 * spec doesn't seem to address the point of whether such duplicates should be allowed.
024 */
025
026 public class Bookmark implements PrivateData {
027 private Vector vUrls;
028 private Vector vConferences;
029 /**
030 *Constructor
031 */
032 public Bookmark(){
033 vUrls = new Vector();
034 vConferences = new Vector();
035 }
036 /**
037 * adds a URL bookmark
038 * @param name a name which will represent the url bookmark
039 * @param value a string representing the url
040 * @return whether or not the url was successfully added to the Bookmark object
041 */
042 public boolean addurl(String name,String value){
043 URL url;
044 Iterator it = vUrls.iterator();
045 while (it.hasNext()){
046 url = (URL)it.next();
047 if (url.name.equals(name)) return false;
048 }
049 url = new url(name,value);
050 vUrls.add(url);
051 return true;
052 }
053
054 public void removeConference(Conference c)
055 {
056 vConferences.remove(c);
057 com.valhalla.Logger.debug("removing conference");
058 }
059 /**
060 * adds a Conference (jabber conference room) bookmark.
061 * nick and pasword may be null
062 * @param name a name which will represent the conference bookmark
063 * @param jid the jid of the conference
064 * @param nick a nickname representing the participant which will be displyed to the other participants in the conference
065 * @param password password to join the conference
066 * @param autojoin indicates whether the client should attempt to join the conference immediately upon connection
067 * @return whether or not the bookmark was successfully added to the Bookmark object
068 */
069 public boolean addConference(String name, String jid, String nick, String password, boolean autojoin){
070 Conference conference;
071 Iterator it = vConferences.iterator();
072 while (it.hasNext()){
073 conference = (Conference)it.next();
074 if (conference.name.equals(name)) return false;
075 }
076 conference = new Conference(name,jid);
077 conference.setNick(nick);
078 conference.setPassword(password);
079 conference.setAutojoin(autojoin);
080 vConferences.add(conference);
081 return true;
082 }
083 /**
084 * Returns an iterator over the URL bookmarks
085 */
086 public Iterator getURLs(){
087 return vUrls.iterator();
088 }
089 /**
090 * @return an iterator over the Conference bookmarks
091 */
092 public Iterator getConferences(){
093 return vConferences.iterator();
094 }
095 /**
096 * @param name the name of the url
097 * @return the first url with the given name, or null if no url with that name exists
098 */
099 public URL geturl(String name){
100 URL url;
101 Iterator it = vUrls.iterator();
102 while (it.hasNext()){
103 url = (URL)it.next();
104 if (url.name.equals(name)) return url;
105 }
106 return null;
107 }
108 /**
109 * @param name the name of the url
110 * @return the first url with the given name, or null if no conferenence with that name exists
111 */
112 public Conference getConference(String name){
113 Conference conference;
114 Iterator it = vConferences.iterator();
115 while (it.hasNext()){
116 conference = (Conference)it.next();
117 if (conference.name.equals(name)) return conference;
118 }
119 return null;
120 }
121
122 /**
123 * Returns the name of the XML element representing the bookmark (query)
124 */
125 public String getElementName(){
126 return "query";
127 }
128 /**
129 * Returns the string "jabber:iq:private"
130 */
131 public String getNamespace(){
132 return "jabber:iq:private";
133 }
134 /**
135 * Returns an XML representaion of the bookmark, according to JEP-48
136 */
137 public String toXML(){
138 String[] s;
139 StringBuffer buf = new StringBuffer();
140 URL url;
141 Conference conference;
142 buf.append("<storage xmlns=\"storage:bookmarks\"");
143 if ((vUrls.size() == 0) && vConferences.size() == 0){
144 buf.append("/>\n");
145 } else {
146 buf.append(">\n");
147 Iterator it = vUrls.iterator();
148 while (it.hasNext()){
149 url = (URL)it.next();
150 buf.append("<url name=\"");
151 buf.append(url.name);
152 buf.append("\" url=\"");
153 buf.append(url.url);
154 buf.append("\"/>\n");
155 }
156 it = vConferences.iterator();
157 while (it.hasNext()){
158 conference = (Conference)it.next();
159 buf.append("<conference name=\"");
160 buf.append(conference.name);
161 buf.append("\" autojoin=\"");
162 if (conference.autojoin){
163 buf.append("1");
164 } else {
165 buf.append("0");
166 }
167 buf.append("\" jid=\"");
168 buf.append(conference.jid);
169 buf.append("\">\n");
170 if (conference.nick != null){
171 buf.append("<nick>");
172 buf.append(conference.nick);
173 buf.append("</nick>\n");
174 }
175 if (conference.password != null){
176 buf.append("<password>");
177 buf.append(conference.password);
178 buf.append("</password>\n");
179 }
180 buf.append("</conference>\n");
181
182 }
183
184 buf.append("</storage>\n");
185 }
186 return buf.toString();
187
188 }
189 /**
190 * Represents an URL bookmark. The bookmark consists of the name of bookmark and a string representing the url.
191 */
192 public class URL {
193 String name;
194 String url;
195 public URL (String name, String url){
196 this.name = name;
197 this.url = url;
198 }
199 public String getName(){
200 return name;
201 }
202 public String getUrl(){
203 return url;
204 }
205 }
206
207 public class Conference {
208 private String name;
209 private String jid;
210 private String nick;
211 private String password;
212 private boolean autojoin;
213 public Conference(String name, String jid){
214 this.name = name;
215 this.jid = jid;
216 autojoin = false;
217 }
218 public void setNick(String nick){
219 this.nick = nick;
220 }
221 public void setPassword(String password){
222 this.password = password;
223 }
224 public void setAutojoin(boolean autojoin){
225 this.autojoin = autojoin;
226 }
227 public String getName(){
228 return this.name;
229 }
230 public String getJid(){
231 return this.jid;
232 }
233 public String getNick(){
234 return this.nick;
235 }
236 public String getPassword(){
237 return this.password;
238 }
239 public boolean getAutojoin(){
240 return this.autojoin;
241 }
242 }
243 }