aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/callhistory/CallHistoryContactSource.java
blob: 4cd0bc7a8a67b961dad98f962c99ece6e1db65db (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * 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 net.java.sip.communicator.impl.callhistory;

import java.util.*;

import net.java.sip.communicator.service.callhistory.*;
import net.java.sip.communicator.service.callhistory.event.*;
import net.java.sip.communicator.service.contactsource.*;

/**
 * The <tt>CallHistoryContactSource</tt> is the contact source for the call
 * history.
 *
 * @author Yana Stamcheva
 * @author Hristo Terezov
 */
public class CallHistoryContactSource
    implements ContactSourceService
{
    /**
     * Returns the display name of this contact source.
     * @return the display name of this contact source
     */
    public String getDisplayName()
    {
        return CallHistoryActivator.getResources().getI18NString(
            "service.gui.CALL_HISTORY_GROUP_NAME");
    }

    /**
     * Creates query for the given <tt>searchString</tt>.
     * @param queryString the string to search for
     * @return the created query
     */
    public ContactQuery createContactQuery(String queryString)
    {
        return createContactQuery(queryString, 50);
    }

    /**
     * Creates query for the given <tt>searchString</tt>.
     * @param queryString the string to search for
     * @param contactCount the maximum count of result contacts
     * @return the created query
     */
    public ContactQuery createContactQuery(String queryString, int contactCount)
    {
        if (queryString != null && queryString.length() > 0)
        {
            return new CallHistoryContactQuery(
                CallHistoryActivator.getCallHistoryService()
                    .findByPeer(queryString, contactCount));
        }
        else
        {
            return new CallHistoryContactQuery(
                CallHistoryActivator.getCallHistoryService()
                    .findLast(contactCount));
        }
    }

    /**
     * The <tt>CallHistoryContactQuery</tt> contains information about a current
     * query to the contact source.
     */
    private class CallHistoryContactQuery
        implements ContactQuery
    {
        /**
         * A list of all registered query listeners.
         */
        private final List<ContactQueryListener> queryListeners
            = new LinkedList<ContactQueryListener>();

        /**
         * A list of all source contact results.
         */
        private final List<SourceContact> sourceContacts
            = new LinkedList<SourceContact>();

        /**
         * The underlying <tt>CallHistoryQuery</tt>, on which this
         * <tt>ContactQuery</tt> is based.
         */
        private CallHistoryQuery callHistoryQuery;

        /**
         * Indicates the status of this query. When created this query is in
         * progress.
         */
        private int status = QUERY_IN_PROGRESS;

        /**
         * Iterator for the queried contacts.
         */
        Iterator<CallRecord> recordsIter = null;

        /**
         * Indicates whether show more label should be displayed or not.
         */
        private boolean showMoreLabelAllowed = true;

        /**
         * Creates an instance of <tt>CallHistoryContactQuery</tt> by specifying
         * the list of call records results.
         * @param callRecords the list of call records, which are the result
         * of this query
         */
        public CallHistoryContactQuery(Collection<CallRecord> callRecords)
        {
            recordsIter = callRecords.iterator();
            Iterator<CallRecord> recordsIter = callRecords.iterator();

            while (recordsIter.hasNext() && status != QUERY_CANCELED)
            {
                sourceContacts.add(
                    new CallHistorySourceContact(
                        CallHistoryContactSource.this,
                        recordsIter.next()));
            }

            showMoreLabelAllowed = false;
        }

        @Override
        public void start()
        {
            if(callHistoryQuery != null)
            {
                callHistoryQuery.addQueryListener(new CallHistoryQueryListener()
                {
                    public void callRecordReceived(CallRecordEvent event)
                    {
                        if (getStatus() == ContactQuery.QUERY_CANCELED)
                            return;

                        SourceContact contact = new CallHistorySourceContact(
                                                    CallHistoryContactSource.this,
                                                    event.getCallRecord());
                        sourceContacts.add(contact);
                        fireQueryEvent(contact);
                    }

                    public void queryStatusChanged(
                        CallHistoryQueryStatusEvent event)
                    {
                        status = event.getEventType();
                        fireQueryStatusEvent(status);
                    }
                });
                recordsIter = callHistoryQuery.getCallRecords().iterator();
            }

            while (recordsIter.hasNext())
            {
                SourceContact contact = new CallHistorySourceContact(
                    CallHistoryContactSource.this,
                    recordsIter.next());
                sourceContacts.add(contact);
                fireQueryEvent(contact);
            }
            if (status != QUERY_CANCELED)
            {
                status = QUERY_COMPLETED;
                if(callHistoryQuery == null)
                    fireQueryStatusEvent(status);
            }
        }

        /**
         * Creates an instance of <tt>CallHistoryContactQuery</tt> based on the
         * given <tt>callHistoryQuery</tt>.
         * @param callHistoryQuery the query used to track the call history
         */
        public CallHistoryContactQuery(CallHistoryQuery callHistoryQuery)
        {
            this.callHistoryQuery = callHistoryQuery;
        }

        /**
         * Adds the given <tt>ContactQueryListener</tt> to the list of query
         * listeners.
         * @param l the <tt>ContactQueryListener</tt> to add
         */
        public void addContactQueryListener(ContactQueryListener l)
        {
            synchronized (queryListeners)
            {
                queryListeners.add(l);
            }
        }

        /**
         * This query could not be canceled.
         */
        public void cancel()
        {
            status = QUERY_CANCELED;

            if (callHistoryQuery != null)
                callHistoryQuery.cancel();
        }

        /**
         * Returns the status of this query. One of the static constants defined
         * in this class.
         * @return the status of this query
         */
        public int getStatus()
        {
            return status;
        }

        /**
         * Removes the given <tt>ContactQueryListener</tt> from the list of
         * query listeners.
         * @param l the <tt>ContactQueryListener</tt> to remove
         */
        public void removeContactQueryListener(ContactQueryListener l)
        {
            synchronized (queryListeners)
            {
                queryListeners.remove(l);
            }
        }

        /**
         * Returns a list containing the results of this query.
         * @return a list containing the results of this query
         */
        public List<SourceContact> getQueryResults()
        {
            return sourceContacts;
        }

        /**
         * Returns the <tt>ContactSourceService</tt>, where this query was first
         * initiated.
         * @return the <tt>ContactSourceService</tt>, where this query was first
         * initiated
         */
        public ContactSourceService getContactSource()
        {
            return CallHistoryContactSource.this;
        }

        /**
         * Notifies all registered <tt>ContactQueryListener</tt>s that a new
         * contact has been received.
         * @param contact the <tt>SourceContact</tt> this event is about
         */
        private void fireQueryEvent(SourceContact contact)
        {
            ContactReceivedEvent event
                = new ContactReceivedEvent(this, contact, showMoreLabelAllowed);

            Collection<ContactQueryListener> listeners;
            synchronized (queryListeners)
            {
                listeners
                    = new ArrayList<ContactQueryListener>(queryListeners);
            }

            for (ContactQueryListener l : listeners)
                l.contactReceived(event);
        }

        /**
         * Notifies all registered <tt>ContactQueryListener</tt>s that a new
         * record has been received.
         * @param newStatus the new status
         */
        private void fireQueryStatusEvent(int newStatus)
        {
            Collection<ContactQueryListener> listeners;
            ContactQueryStatusEvent event
                = new ContactQueryStatusEvent(this, newStatus);

            synchronized (queryListeners)
            {
                listeners
                    = new ArrayList<ContactQueryListener>(queryListeners);
            }

            for (ContactQueryListener l : listeners)
                l.queryStatusChanged(event);
        }

        public String getQueryString()
        {
            return callHistoryQuery.getQueryString();
        }
    }

    /**
     * Returns default type to indicate that this contact source can be queried
     * by default filters.
     *
     * @return the type of this contact source
     */
    public int getType()
    {
        return HISTORY_TYPE;
    }

    /**
     * Returns the index of the contact source in the result list.
     *
     * @return the index of the contact source in the result list
     */
    public int getIndex()
    {
        return -1;
    }
}