summaryrefslogtreecommitdiffstats
path: root/samples/IrcChatSystem/ChatClientApp/Controls/UserCardControl.xaml.cs
blob: 6be642207ee66b55547fd5fcb8f9319d94043da6 (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
using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Hik.Samples.Scs.IrcChat.Arguments;
using Hik.Samples.Scs.IrcChat.Client;

namespace Hik.Samples.Scs.IrcChat.Controls
{
    /// <summary>
    /// This control is used to show a User Card in right area of chat room.
    /// </summary>
    public partial class UserCardControl : UserControl
    {
        /// <summary>
        /// Gets/sets the of the user.
        /// </summary>
        public string UserNick
        {
            get { return lblNick.Content.ToString(); }
            set { lblNick.Content = value; }
        }

        /// <summary>
        /// Sets status of the user.
        /// </summary>
        public UserStatus UserStatus
        {
            get { return _userStatus; }
            set
            {
                _userStatus = value;
                RefreshStatusLabel();
            }
        }
        private UserStatus _userStatus;

        /// <summary>
        /// Sets avatar image of the user.
        /// </summary>
        public byte[] AvatarBytes
        {
            set
            {
                try
                {
                    ChangeAvatar(value);
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// Gets ImageSource property of user avatar.
        /// </summary>
        public ImageSource AvatarImageSource
        {
            get { return imgAvatar.Source; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public UserCardControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Refreshes status of user on label according to _userStatus.
        /// </summary>
        private void RefreshStatusLabel()
        {
            switch (_userStatus)
            {
                case UserStatus.Busy:
                    lblStatus.Content = "Busy";
                    lblStatus.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF4E4E"));
                    break;
                case UserStatus.Out:
                    lblStatus.Content = "Out";
                    lblStatus.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF3179FE"));
                    break;
                default: //Default: Available
                    lblStatus.Content = "Available";
                    lblStatus.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF2BE400"));
                    break;
            }
        }

        /// <summary>
        /// Changes avatar image of the user.
        /// </summary>
        /// <param name="bytesOfAvatar">byte of avatar file</param>
        private void ChangeAvatar(byte[] bytesOfAvatar)
        {
            if (bytesOfAvatar == null)
            {
                var defaultAvatar = Path.Combine((Path.Combine(ClientHelper.GetCurrentDirectory(), @"Images\user_male.png")));
                imgAvatar.Source = new BitmapImage(new Uri(defaultAvatar));
                return;
            }

            //Save bytes into a temporary file
            var tempSavePath = Path.GetTempFileName();
            File.WriteAllBytes(tempSavePath, bytesOfAvatar);

            //Change avatar picture.
            imgAvatar.Source = new BitmapImage(new Uri(tempSavePath));
        }
    }
}