aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/call/conference/ConferenceInviteDialog.java
blob: 80eedc642e8f8ac83a408934fccc2994de21ad89 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * 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.gui.main.call.conference;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.main.call.*;
import net.java.sip.communicator.impl.gui.main.contactlist.contactsource.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

/**
 * The invite dialog is the one shown when the user clicks on the conference
 * button in the chat toolbar.
 *
 * @author Yana Stamcheva
 * @author Lyubomir Marinov
 */
public class ConferenceInviteDialog
    extends InviteDialog
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    /**
     * The account selector box.
     */
    private final JComboBox accountSelectorBox = new JComboBox();

    /**
     * The last selected account.
     */
    private Object lastSelectedAccount;

    /**
     * The telephony conference into which this instance is to invite
     * participants.
     */
    private final CallConference conference;

    /**
     * The current provider contact source.
     */
    private ContactSourceService currentProviderContactSource;

    /**
     * The current string contact source.
     */
    private ContactSourceService currentStringContactSource;

    /**
     * The previously selected protocol provider, with which this dialog has
     * been instantiated.
     */
    private ProtocolProviderService preselectedProtocolProvider;

    /**
     * Indicates whether this conference invite dialog is associated with a
     * Jitsi Videobridge invite.
     */
    private final boolean isJitsiVideobridge;

    /**
     * Initializes a new <tt>ConferenceInviteDialog</tt> instance which is to
     * invite contacts/participants in a specific telephony conference.
     *
     * @param conference the telephony conference in which the new instance is
     * to invite contacts/participants
     */
    public ConferenceInviteDialog(
                            CallConference conference,
                            ProtocolProviderService preselectedProvider,
                            List<ProtocolProviderService> protocolProviders,
                            final boolean isJitsiVideobridge)
    {
        // Set the correct dialog title depending if we're going to create a
        // video bridge conference call
        super((isJitsiVideobridge
                ? GuiActivator.getResources()
                    .getI18NString("service.gui.INVITE_CONTACT_TO_VIDEO_BRIDGE")
                : GuiActivator.getResources()
                    .getI18NString("service.gui.INVITE_CONTACT_TO_CALL")),
                false);

        this.conference = conference;
        this.preselectedProtocolProvider = preselectedProvider;
        this.isJitsiVideobridge = isJitsiVideobridge;

        if (preselectedProtocolProvider == null)
            initAccountSelectorPanel(protocolProviders);

        // init the list, as we check whether features are supported
        // it may take some time if we have too much contacts
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                initContactSources();

                // Initialize the list of contacts to select from.
                if (preselectedProtocolProvider != null)
                    initContactListData(preselectedProtocolProvider);
                else
                    initContactListData(
                        (ProtocolProviderService) accountSelectorBox
                            .getSelectedItem());
            }
        });

        this.addInviteButtonListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Collection<UIContact> selectedContacts
                    = destContactList.getContacts(null);

                if (selectedContacts != null && selectedContacts.size() > 0)
                {
                    if (preselectedProtocolProvider == null)
                        preselectedProtocolProvider
                            = (ProtocolProviderService) accountSelectorBox
                                .getSelectedItem();

                    if (isJitsiVideobridge)
                        inviteJitsiVideobridgeContacts(  preselectedProtocolProvider,
                                                    selectedContacts);
                    else
                        inviteContacts(selectedContacts);

                    // Store the last used account in order to pre-select it
                    // next time.
                    ConfigurationUtils.setLastCallConferenceProvider(
                        preselectedProtocolProvider);

                    dispose();
                }
                else
                {
                    // TODO: The underlying invite dialog should show a message
                    // to the user that she should select at least two contacts
                    // in order to create a conference.
                }
            }
        });

        this.addCancelButtonListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        });
    }

    /**
     * Constructs the <tt>ConferenceInviteDialog</tt>.
     */
    public ConferenceInviteDialog()
    {
        this(null, null, null, false);
    }

    /**
     * Creates an instance of <tt>ConferenceInviteDialog</tt> by specifying an
     * already created conference. To use when inviting contacts to an existing
     * conference is needed.
     *
     * @param conference the existing <tt>CallConference</tt>
     */
    public ConferenceInviteDialog(CallConference conference)
    {
        this(conference, null, null, false);
    }

    /**
     * Creates an instance of <tt>ConferenceInviteDialog</tt> by specifying an
     * already created conference. To use when inviting contacts to an existing
     * conference is needed.
     *
     * @param conference the existing <tt>CallConference</tt>
     */
    public ConferenceInviteDialog(
                        CallConference conference,
                        ProtocolProviderService preselectedProtocolProvider,
                        boolean isJitsiVideobridge)
    {
        this(conference, preselectedProtocolProvider, null, isJitsiVideobridge);
    }

    /**
     * Creates an instance of <tt>ConferenceInviteDialog</tt> by specifying a
     * preselected protocol provider to be used and if this is an invite for
     * a video bridge conference.
     *
     * @param protocolProviders the protocol providers list
     * @param isJitsiVideobridge <tt>true</tt> if this dialog should create a
     * conference through a Jitsi Videobridge; otherwise, <tt>false</tt>
     */
    public ConferenceInviteDialog(
                                List<ProtocolProviderService> protocolProviders,
                                boolean isJitsiVideobridge)
    {
        this(null, null, protocolProviders, isJitsiVideobridge);
    }

    /**
     * Creates an instance of <tt>ConferenceInviteDialog</tt> by specifying a
     * preselected protocol provider to be used and if this is an invite for
     * a video bridge conference.
     *
     * @param selectedConfProvider the preselected protocol provider
     * @param isJitsiVideobridge <tt>true</tt> if this dialog should create a
     * conference through a Jitsi Videobridge; otherwise, <tt>false</tt>
     */
    public ConferenceInviteDialog(
                                ProtocolProviderService selectedConfProvider,
                                boolean isJitsiVideobridge)
    {
        this(null, selectedConfProvider, null, isJitsiVideobridge);
    }

    /**
     * Initializes the account selector panel.
     *
     * @param protocolProviders the list of protocol providers we'd like to
     * show in the account selector box
     */
    private void initAccountSelectorPanel(
                        List<ProtocolProviderService> protocolProviders)
    {
        JLabel accountSelectorLabel = new JLabel(
            GuiActivator.getResources().getI18NString("service.gui.CALL_VIA"));

        TransparentPanel accountSelectorPanel
            = new TransparentPanel(new BorderLayout());

        accountSelectorPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        accountSelectorPanel.add(accountSelectorLabel, BorderLayout.WEST);
        accountSelectorPanel.add(accountSelectorBox, BorderLayout.CENTER);

        // Initialize the account selector box.
        if (protocolProviders != null && protocolProviders.size() > 0)
            this.initAccountListData(protocolProviders);
        else
            this.initAccountListData();

        this.accountSelectorBox.setRenderer(new DefaultListCellRenderer()
        {
            private static final long serialVersionUID = 0L;

            @Override
            public Component getListCellRendererComponent(JList list,
                Object value, int index, boolean isSelected,
                boolean cellHasFocus)
            {
                ProtocolProviderService protocolProvider
                     = (ProtocolProviderService) value;

                if (protocolProvider != null)
                {
                    this.setText(
                        protocolProvider.getAccountID().getDisplayName());
                    this.setIcon(
                        ImageLoader.getAccountStatusImage(protocolProvider));
                }

                return this;
            }
        });

        this.accountSelectorBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Object accountSelectorBoxSelectedItem
                    = accountSelectorBox.getSelectedItem();

                if (lastSelectedAccount == null
                    || !lastSelectedAccount
                        .equals(accountSelectorBoxSelectedItem))
                {
                    lastSelectedAccount = accountSelectorBoxSelectedItem;

                    initContactListData(
                        (ProtocolProviderService) accountSelectorBox
                            .getSelectedItem());

                    if (isJitsiVideobridge)
                        destContactList.removeAll();
                }
            }
        });

        this.getContentPane().add(accountSelectorPanel, BorderLayout.NORTH);
    }

    /**
     * Initializes the account selector box with the given list of
     * <tt>ProtocolProviderService</tt>-s.
     *
     * @param protocolProviders the list of <tt>ProtocolProviderService</tt>-s
     * we'd like to show in the account selector box
     */
    private void initAccountListData(
                            List<ProtocolProviderService> protocolProviders)
    {
        Iterator<ProtocolProviderService> providersIter
            = protocolProviders.iterator();

        while (providersIter.hasNext())
        {
            ProtocolProviderService protocolProvider
                = providersIter.next();

            accountSelectorBox.addItem(protocolProvider);
        }

        if (accountSelectorBox.getItemCount() > 0)
            accountSelectorBox.setSelectedIndex(0);
    }

    /**
     * Initializes the account list.
     */
    private void initAccountListData()
    {
        Iterator<ProtocolProviderService> protocolProviders
            = GuiActivator.getUIService().getMainFrame().getProtocolProviders();

        while(protocolProviders.hasNext())
        {
            ProtocolProviderService protocolProvider
                = protocolProviders.next();
            OperationSet opSet
                = protocolProvider.getOperationSet(
                        OperationSetTelephonyConferencing.class);

            if ((opSet != null) && protocolProvider.isRegistered())
                accountSelectorBox.addItem(protocolProvider);
        }

        // Try to select the last used account if available.
        ProtocolProviderService pps
            = ConfigurationUtils.getLastCallConferenceProvider();

        if (pps == null && conference != null)
        {
            /*
             * Pick up the first account from the ones participating in the
             * associated telephony conference which supports
             * OperationSetTelephonyConferencing.
             */
            for (Call call : conference.getCalls())
            {
                ProtocolProviderService callPps = call.getProtocolProvider();

                if (callPps.getOperationSet(
                            OperationSetTelephonyConferencing.class)
                        != null)
                {
                    pps = callPps;
                    break;
                }
            }
        }

        if (pps != null)
            accountSelectorBox.setSelectedItem(pps);
        else if (accountSelectorBox.getItemCount() > 0)
            accountSelectorBox.setSelectedIndex(0);
    }

    /**
     * Initializes contact list sources.
     */
    private void initContactSources()
    {
        DemuxContactSourceService demuxCSService
             = GuiActivator.getDemuxContactSourceService();

        // If the DemuxContactSourceService isn't registered we use the default
        // contact source set.
        if (demuxCSService == null)
            return;

        Iterator<UIContactSource> sourcesIter
            = new ArrayList<UIContactSource>(
                srcContactList.getContactSources()).iterator();

        srcContactList.removeAllContactSources();

        while (sourcesIter.hasNext())
        {
            ContactSourceService contactSource
                = sourcesIter.next().getContactSourceService();

            srcContactList.addContactSource(
                demuxCSService.createDemuxContactSource(contactSource));
        }
    }

    /**
     * Initializes the left contact list with the contacts that could be added
     * to the current chat session.
     * @param protocolProvider the protocol provider from which to initialize
     * the contact list data
     */
    private void initContactListData(ProtocolProviderService protocolProvider)
    {
        this.setCurrentProvider(protocolProvider);

        Iterator<UIContactSource> sourcesIter
            = new ArrayList<UIContactSource>(
                srcContactList.getContactSources()).iterator();

        while (sourcesIter.hasNext())
        {
            ContactSourceService contactSource
                = sourcesIter.next().getContactSourceService();

            if (contactSource instanceof ProtocolAwareContactSourceService)
            {
                ((ProtocolAwareContactSourceService) contactSource)
                    .setPreferredProtocolProvider(
                        OperationSetBasicTelephony.class, protocolProvider);
            }
        }

        srcContactList.removeContactSource(currentProviderContactSource);
        srcContactList.removeContactSource(currentStringContactSource);

        currentProviderContactSource
            = new ProtocolContactSourceServiceImpl(
                                            protocolProvider,
                                            OperationSetBasicTelephony.class);
        currentStringContactSource
            = new StringContactSourceServiceImpl(
                                            protocolProvider,
                                            OperationSetBasicTelephony.class);

        srcContactList.addContactSource(currentProviderContactSource);
        srcContactList.addContactSource(currentStringContactSource);

        srcContactList.applyDefaultFilter();
    }

    /**
     * Invites the contacts to the chat conference.
     *
     * @param contacts the list of contacts to invite
     */
    private void inviteContacts(Collection<UIContact> contacts)
    {
        ProtocolProviderService selectedProvider;
        Map<ProtocolProviderService, List<String>> selectedProviderCallees
            = new HashMap<ProtocolProviderService, List<String>>();
        List<String> callees;

        Iterator<UIContact> contactsIter = contacts.iterator();

        while (contactsIter.hasNext())
        {
            UIContact uiContact = contactsIter.next();

            Iterator<UIContactDetail> contactDetailsIter = uiContact
                .getContactDetailsForOperationSet(
                    OperationSetBasicTelephony.class).iterator();

            // We invite the first protocol contact that corresponds to the
            // invite provider.
            if (contactDetailsIter.hasNext())
            {
                UIContactDetail inviteDetail = contactDetailsIter.next();
                selectedProvider = inviteDetail
                    .getPreferredProtocolProvider(
                        OperationSetBasicTelephony.class);

                if (selectedProvider == null)
                {
                    selectedProvider
                        = (ProtocolProviderService)
                            accountSelectorBox.getSelectedItem();
                }

                if(selectedProvider != null
                    && selectedProviderCallees.get(selectedProvider) != null)
                {
                    callees = selectedProviderCallees.get(selectedProvider);
                }
                else
                {
                    callees = new ArrayList<String>();
                }

                callees.add(inviteDetail.getAddress());
                selectedProviderCallees.put(selectedProvider, callees);
            }
        }

        if(conference != null)
        {
            CallManager.inviteToConferenceCall(
                    selectedProviderCallees,
                    conference);
        }
        else
        {
            CallManager.createConferenceCall(selectedProviderCallees);
        }
    }

    /**
     * Invites the contacts to the chat conference.
     *
     * @param contacts the list of contacts to invite
     */
    private void inviteJitsiVideobridgeContacts(
                                ProtocolProviderService preselectedProvider,
                                Collection<UIContact> contacts)
    {
        List<String> callees = new ArrayList<String>();

        Iterator<UIContact> contactsIter = contacts.iterator();

        while (contactsIter.hasNext())
        {
            UIContact uiContact = contactsIter.next();

            Iterator<UIContactDetail> contactDetailsIter = uiContact
                .getContactDetailsForOperationSet(
                    OperationSetBasicTelephony.class).iterator();

            // We invite the first protocol contact that corresponds to the
            // invite provider.
            if (contactDetailsIter.hasNext())
            {
                UIContactDetail inviteDetail = contactDetailsIter.next();

                callees.add(inviteDetail.getAddress());
            }
        }

        if(conference != null)
        {
            CallManager.inviteToJitsiVideobridgeConfCall(
                    callees.toArray(new String[callees.size()]),
                    conference.getCalls().get(0));
        }
        else
        {
            CallManager.createJitsiVideobridgeConfCall(
                preselectedProvider,
                callees.toArray(new String[callees.size()]));
        }
    }
}