aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/account/Account.java
blob: ea9823f82aec0d2aa92e648d46f7ccad70149efe (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
/*
 * 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.gui.main.account;

import java.awt.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

/**
 * Represents an account in the account list.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class Account
{
    /**
     * The corresponding protocol provider.
     */
    private ProtocolProviderService protocolProvider;

    /**
     * The identifier of the account.
     */
    private final AccountID accountID;

    /**
     * The display name of the account
     */
    private final String name;

    /**
     * The icon of the image.
     */
    private ImageIcon icon;

    /**
     * Indicates if the account is enabled.
     */
    private boolean isEnabled;

    /**
     * The corresponding check box in the account list.
     */
    private JCheckBox enableCheckBox;

    /**
     * Creates an <tt>Account</tt> instance from the given
     * <tt>protocolProvider</tt>.
     * @param protocolProvider the protocol provider on which this account is
     * based
     */
    public Account(ProtocolProviderService protocolProvider)
    {
        this.protocolProvider = protocolProvider;

        this.accountID = protocolProvider.getAccountID();

        this.name = accountID.getDisplayName();

        this.icon = getProtocolIcon(protocolProvider);

        this.isEnabled = accountID.isEnabled();
    }

    /**
     * Creates an account object with the given <tt>accountName</tt> and
     * <tt>icon</tt>.
     * @param accountID the identifier of the account
     */
    public Account(AccountID accountID)
    {
        this.accountID = accountID;

        this.name = accountID.getDisplayName();

        String iconPath = accountID.getAccountPropertyString(
            ProtocolProviderFactory.ACCOUNT_ICON_PATH);

        if (iconPath != null)
            this.icon = ImageLoader.getImageForPath(iconPath);

        this.isEnabled = accountID.isEnabled();
    }

    /**
     * Returns the protocol provider, on which this account is based.
     * @return the protocol provider, on which this account is based
     */
    public ProtocolProviderService getProtocolProvider()
    {
        return protocolProvider;
    }

    /**
     * Returns the account identifier.
     * @return the account identifier
     */
    public AccountID getAccountID()
    {
        return accountID;
    }

    /**
     * Returns the account name.
     * @return the account name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the protocol name of the account.
     * @return the protocol name of the account
     */
    public String getProtocolName()
    {
        return accountID.getProtocolDisplayName();
    }

    /**
     * The icon of the account.
     * @return the icon of the account
     */
    public Icon getIcon()
    {
        return icon;
    }

    /**
     * Returns the status name.
     * @return the status name
     */
    public String getStatusName()
    {
        if (protocolProvider != null)
            return getAccountStatus(protocolProvider);
        else
            return Constants.OFFLINE_STATUS;
    }

    /**
     * Returns the status icon of this account.
     * @return the status icon of this account
     */
    public Icon getStatusIcon()
    {
        if (protocolProvider != null)
            return ImageLoader.getAccountStatusImage(protocolProvider);
        else if (icon != null)
        {
            icon = ImageLoader.getImageForPath(
                accountID.getAccountPropertyString(
                    ProtocolProviderFactory.ACCOUNT_ICON_PATH));

            Image scaledImage
                = ImageUtils.scaleImageWithinBounds(icon.getImage(), 16, 16);

            if (scaledImage != null)
                return new ImageIcon(
                    GrayFilter.createDisabledImage(scaledImage));
        }
        return null;
    }

    /**
     * Returns <tt>true</tt> to indicate that this account is enabled,
     * <tt>false</tt> - otherwise.
     * @return <tt>true</tt> to indicate that this account is enabled,
     * <tt>false</tt> - otherwise
     */
    public boolean isEnabled()
    {
        return isEnabled;
    }

    /**
     * Returns the current presence status of the given protocol provider.
     * 
     * @param protocolProvider the protocol provider which status we're looking
     * for.
     * @return the current presence status of the given protocol provider.
     */
    private String getAccountStatus(ProtocolProviderService protocolProvider)
    {
        String status;

        // If our account doesn't have a registered protocol provider we return
        // offline.
        if (protocolProvider == null)
            return GuiActivator.getResources()
            .getI18NString("service.gui.OFFLINE");

        OperationSetPresence presence
            = protocolProvider.getOperationSet(OperationSetPresence.class);

        if (presence != null)
        {
            status = presence.getPresenceStatus().getStatusName();
        }
        else
        {
            status = GuiActivator.getResources()
                        .getI18NString( protocolProvider.isRegistered()
                                        ? "service.gui.ONLINE"
                                        : "service.gui.OFFLINE");
        }

        return status;
    }

    /**
     * Returns the protocol icon. If an icon 32x32 is available, returns it,
     * otherwise tries to scale a bigger icon if available. If we didn't find
     * a bigger icon to scale, we return null.
     *
     * @param protocolProvider the protocol provider, which icon we're looking
     * for
     * @return the protocol icon
     */
    private ImageIcon getProtocolIcon(ProtocolProviderService protocolProvider)
    {
        ProtocolIcon protocolIcon = protocolProvider.getProtocolIcon();
        Image protocolImage
            = GuiUtils.getBytesInImage(
                    protocolIcon.getIcon(ProtocolIcon.ICON_SIZE_32x32));

        if (protocolImage != null)
        {
            return new ImageIcon(protocolImage);
        }
        else
        {
            protocolImage = GuiUtils.getBytesInImage(
                protocolIcon.getIcon(ProtocolIcon.ICON_SIZE_48x48));

            if (protocolImage == null)
                protocolImage
                    = GuiUtils.getBytesInImage(
                        protocolIcon.getIcon(ProtocolIcon.ICON_SIZE_64x64));

            if (protocolImage != null)
                return ImageUtils.scaleIconWithinBounds(protocolImage, 32, 32);
        }

        return null;
    }

    /**
     * Sets the given <tt>protocolProvider</tt> to this account.
     * @param protocolProvider the <tt>ProtocolProviderService</tt>
     * corresponding to this account
     */
    public void setProtocolProvider(ProtocolProviderService protocolProvider)
    {
        this.protocolProvider = protocolProvider;
    }

    /**
     * Sets the <tt>isDisabled</tt> property.
     * @param isEnabled indicates if this account is currently
     * <tt>disabled</tt>
     */
    public void setEnabled(boolean isEnabled)
    {
        this.isEnabled = isEnabled;
    }

    /**
     * Sets the enable check box.
     * @param enableCheckBox the enable check box corresponding to this account
     */
    public void setEnableCheckBox(JCheckBox enableCheckBox)
    {
        this.enableCheckBox = enableCheckBox;
    }

    /**
     * Returns the enable check box corresponding to this account.
     * @return the enable check box corresponding to this account
     */
    public JCheckBox getEnableCheckBox()
    {
        return enableCheckBox;
    }
}