summaryrefslogtreecommitdiffstats
blob: b370f6345a24a376eb6e3f48f1a1cf886595b7c5 (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
/*
 * Copyright (c) 2011-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.client;

import com.isode.stroke.elements.BlockPayload;
import com.isode.stroke.elements.BlockListPayload;
import com.isode.stroke.elements.UnblockPayload;
import com.isode.stroke.elements.DiscoInfo;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.elements.IQ;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.queries.SetResponder;
import com.isode.stroke.queries.GenericRequest;
import com.isode.stroke.client.BlockList;
import com.isode.stroke.client.BlockListImpl;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.jid.JID;
import java.util.Vector;

public class ClientBlockListManager {

	private IQRouter iqRouter;
	private SetResponder<BlockPayload> blockResponder;
	private SetResponder<UnblockPayload> unblockResponder;
	private BlockListImpl blockList;

	class BlockResponder extends SetResponder<BlockPayload> {

		public BlockResponder(BlockListImpl blockList, IQRouter iqRouter) {
			super(new BlockPayload(), iqRouter);
			this.blockList = blockList;
		}

		public boolean handleSetRequest(final JID from, final JID to, final String id, BlockPayload payload) {
			if (getIQRouter().isAccountJID(from)) {
					if (payload != null) {
						blockList.addItems(payload.getItems());
					}
					sendResponse(from, id, null);
			}
			else {
				sendError(from, id, ErrorPayload.Condition.NotAuthorized, ErrorPayload.Type.Cancel);
			}
			return true;
		}

		private BlockListImpl blockList;
	};

	class UnblockResponder extends SetResponder<UnblockPayload> {

		public UnblockResponder(BlockListImpl blockList, IQRouter iqRouter) {
			super(new UnblockPayload(), iqRouter);
			this.blockList = blockList;
		}

		public boolean handleSetRequest(final JID from, final JID to, final String id, UnblockPayload payload) {
			if (getIQRouter().isAccountJID(from)) {
				if (payload != null) {
					if (payload.getItems().isEmpty()) {
						blockList.removeAllItems();
					}
					else {
						blockList.removeItems(payload.getItems());
					}
				}
				sendResponse(from, id, null);
			}
			else {
				sendError(from, id, ErrorPayload.Condition.NotAuthorized, ErrorPayload.Type.Cancel);
			}
			return true;
		}

		private BlockListImpl blockList;
	};

	public ClientBlockListManager(IQRouter iqRouter) {
		this.iqRouter = iqRouter;
	}

	protected void finalize() throws Throwable {
		try {
			if (blockList != null && BlockList.State.Available.equals(blockList.getState())) {
				unblockResponder.stop();
				blockResponder.stop();
			}
		}
		finally {
			super.finalize();
		}
	}

	/**
	 * Returns the blocklist.
	 */
	public BlockList getBlockList() {
		if (blockList == null) {
			blockList = new BlockListImpl();
			blockList.setState(BlockList.State.Init);
		}
		return blockList;
	}

	/**
	 * Get the blocklist from the server.
	 */
	public BlockList requestBlockList() {
		if (blockList == null) {
			blockList = new BlockListImpl();
		}
		blockList.setState(BlockList.State.Requesting);
		GenericRequest<BlockListPayload> getRequest = new GenericRequest<BlockListPayload>(IQ.Type.Get, new JID(), new BlockListPayload(), iqRouter);
		getRequest.onResponse.connect(new Slot2<BlockListPayload, ErrorPayload>() {
			@Override
			public void call(BlockListPayload p, ErrorPayload e) {
				handleBlockListReceived(p, e);
			}
		});
		getRequest.send();
		return blockList;		
	}

	public GenericRequest<BlockPayload> createBlockJIDRequest(final JID jid) {
		Vector<JID> vec = new Vector<JID>();
		vec.add(jid);
		return createBlockJIDsRequest(vec);
	}

	public GenericRequest<BlockPayload> createBlockJIDsRequest(final Vector<JID> jids) {
		BlockPayload payload = new BlockPayload(jids);
		return new GenericRequest<BlockPayload>(IQ.Type.Set, new JID(), payload, iqRouter);
	}

	public GenericRequest<UnblockPayload> createUnblockJIDRequest(final JID jid) {
		Vector<JID> vec = new Vector<JID>();
		vec.add(jid);		
		return createUnblockJIDsRequest(vec);
	}

	public GenericRequest<UnblockPayload> createUnblockJIDsRequest(final Vector<JID> jids) {
		UnblockPayload payload = new UnblockPayload(jids);
		return new GenericRequest<UnblockPayload>(IQ.Type.Set, new JID(), payload, iqRouter);
	}

	public GenericRequest<UnblockPayload> createUnblockAllRequest() {
		return createUnblockJIDsRequest(new Vector<JID>());
	}

	private void handleBlockListReceived(BlockListPayload payload, ErrorPayload error) {
		if (error != null || payload == null) {
			blockList.setState(BlockList.State.Error);
		}
		else {
			blockList.setItems(payload.getItems());
			blockList.setState(BlockList.State.Available);
			if (blockResponder == null) {
				blockResponder = new BlockResponder(blockList, iqRouter);
				blockResponder.start();
			}
			if (unblockResponder == null) {
				unblockResponder = new UnblockResponder(blockList, iqRouter);
				unblockResponder.start();
			}
		}
	}
}