aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/CallRecordingConfigForm.java
blob: a3b88a984cf8c1a0481f6492f7c207f9347d5874 (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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.neomedia;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.service.neomedia.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.swing.*;

/**
 * The saved calls management and configuration form.
 *
 * @author Dmitri Melnikov
 */
public class CallRecordingConfigForm
    extends TransparentPanel 
    implements ActionListener
{
    /**
     * The <tt>Logger</tt> used by the <tt>CallRecordingConfigForm</tt> class and
     * its instances for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(CallRecordingConfigForm.class);
    
    /**
     * The resource service.
     */
    private static final ResourceManagementService resources
        = NeomediaActivator.getResources();
    
    /**
     * Directory where calls are stored. Default is SC_HOME/calls.
     */
    private String savedCallsDir;
    
    /**
     * Directory choose dialog.
     */
    private final SipCommFileChooser dirChooser;
    
    /**
     * UI components.
     */
    private JButton callDirChooseButton;
    private JTextField callDirTextField;
    private JComboBox formatsComboBox;
    private JCheckBox saveCallsToCheckBox;
    
    /**
     * Creates an instance of the <tt>CallConfigurationPanel</tt>.
     * Checks for the <tt>SAVED_CALLS_PATH</tt> and sets it if it does not
     * exist.
     */
    public CallRecordingConfigForm()
    {
        super(new BorderLayout());
        
        initComponents();
        loadValues();

        dirChooser
            = GenericFileDialog.create(
                    null,
                    resources.getI18NString(
                            "plugin.callrecordingconfig.CHOOSE_DIR"),
                    SipCommFileChooser.LOAD_FILE_OPERATION);
        if (dirChooser instanceof JFileChooser)
        {
            ((JFileChooser) dirChooser).setFileSelectionMode(
                    JFileChooser.DIRECTORIES_ONLY);
        }
    }

    /**
     * Loads values from the configuration and sets the UI components to these
     * values.
     */
    private void loadValues()
    {
        ConfigurationService configurationService
            = NeomediaActivator.getConfigurationService();
        String callFormat
            = configurationService.getString(Recorder.CALL_FORMAT);

        formatsComboBox.setSelectedItem(
                (callFormat == null)
                    ? SoundFileUtils.DEFAULT_CALL_RECORDING_FORMAT
                    : callFormat);

        savedCallsDir
            = configurationService.getString(Recorder.SAVED_CALLS_PATH);
        saveCallsToCheckBox.setSelected(savedCallsDir != null);
        callDirTextField.setText(savedCallsDir);
        callDirTextField.setEnabled(saveCallsToCheckBox.isSelected());
        callDirChooseButton.setEnabled(saveCallsToCheckBox.isSelected());
    }

    /**
     * Creates a panel with call management components.
     */
    private void initComponents()
    {
        // labels panel
        JPanel labelsPanel = new TransparentPanel(new GridLayout(2, 1));        
        JLabel formatsLabel
            = new JLabel(
                    resources.getI18NString(
                            "plugin.callrecordingconfig.SUPPORTED_FORMATS"));

        saveCallsToCheckBox
            = new SIPCommCheckBox(
                    resources.getI18NString(
                            "plugin.callrecordingconfig.SAVE_CALLS"));
        saveCallsToCheckBox.addActionListener(this);

        labelsPanel.add(formatsLabel);
        labelsPanel.add(saveCallsToCheckBox);

        // combo box panel
        JPanel comboPanel
            = new TransparentPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel emptyLabel = new JLabel();

        emptyLabel.setPreferredSize(new Dimension(30, 30));
        comboPanel.add(createFormatsComboBox());
        comboPanel.add(emptyLabel);

        // saved calls directory panel 
        JPanel callDirPanel
            = new TransparentPanel(new FlowLayout(FlowLayout.LEFT));

        callDirTextField = new JTextField();
        callDirTextField.setPreferredSize(new Dimension(200, 30));
        callDirTextField.addActionListener(this);
        callDirPanel.add(callDirTextField);

        callDirChooseButton
            = new JButton(
                    new ImageIcon(
                            resources.getImageInBytes(
                                    "plugin.notificationconfig.FOLDER_ICON")));
        callDirChooseButton.setMinimumSize(new Dimension(30,30));
        callDirChooseButton.setPreferredSize(new Dimension(30,30));
        callDirChooseButton.addActionListener(this);
        callDirPanel.add(callDirChooseButton);

        // values panel
        JPanel valuesPanel = new TransparentPanel(new GridLayout(2, 1));

        valuesPanel.add(comboPanel);
        valuesPanel.add(callDirPanel);

        // main panel
        JPanel mainPanel = new TransparentPanel(new BorderLayout());

        mainPanel.add(labelsPanel, BorderLayout.WEST);
        mainPanel.add(valuesPanel, BorderLayout.CENTER);

        this.add(mainPanel, BorderLayout.NORTH);
    }

    /**
     * Creates a combo box with supported audio formats.
     * 
     * @return a combo box with supported audio formats
     */
    private Component createFormatsComboBox()
    {
        ComboBoxModel formatsComboBoxModel
            = new DefaultComboBoxModel(RecorderImpl.SUPPORTED_FORMATS);

        formatsComboBox = new JComboBox();
        formatsComboBox.setPreferredSize(new Dimension(200, 30));
        formatsComboBox.setModel(formatsComboBoxModel);

        formatsComboBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent event)
            {
                if (event.getStateChange() == ItemEvent.SELECTED)
                {
                    NeomediaActivator
                        .getConfigurationService()
                            .setProperty(Recorder.CALL_FORMAT, event.getItem());
                }
            }
        });
        return formatsComboBox;
    }

    /**
     * Indicates that one of the contained in this panel components has
     * performed an action.
     * 
     * @param e the <tt>ActionEvent</tt> that notified us
     */
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == saveCallsToCheckBox)
        {
            boolean selected = saveCallsToCheckBox.isSelected();
            callDirTextField.setEnabled(selected);
            callDirChooseButton.setEnabled(selected);
            if (selected)
            {
                // set default directory
                try
                {
                    changeCallsDir(
                        NeomediaActivator
                            .getFileAccessService()
                                .getDefaultDownloadDirectory());
                }
                catch (IOException ioex)
                {
                }
            }
            else
            {
                // remove default directory prop
                NeomediaActivator
                    .getConfigurationService()
                        .setProperty(Recorder.SAVED_CALLS_PATH, null);
                callDirTextField.setText(null);
            }
        }
        else if (source == callDirChooseButton)
        {
            File newDir = dirChooser.getFileFromDialog();
            changeCallsDir(newDir);
        }
        else if (source == callDirTextField)
        {
            File newDir = new File(callDirTextField.getText());
            changeCallsDir(newDir);
        }
    }

    /**
     * Sets the new directory for the saved calls to <tt>dir</tt>.
     * 
     * @param dir the new chosen directory
     * @return <tt>true</tt> if directory was changed successfully,
     *         <tt>false</tt> otherwise
     */
    private boolean changeCallsDir(File dir)
    {
        if (dir != null && dir.isDirectory())
        {
            savedCallsDir = dir.getAbsolutePath();
            callDirTextField.setText(savedCallsDir);
            NeomediaActivator
                .getConfigurationService()
                    .setProperty(Recorder.SAVED_CALLS_PATH, savedCallsDir);
            
            if (logger.isDebugEnabled())
                logger.debug("Calls directory changed to " + savedCallsDir);
            return true;
        }
        else
        {
            if (logger.isDebugEnabled())
                logger.debug("Calls directory not changed.");
            return false;
        }
    }
}