Beispiel #1
0
        /// <summary>
        /// Load all external mugshots into the cache.
        /// </summary>
        private static void LoadExternalMugshots()
        {
            string mugshotFolder = Path.Combine(CIX.HomeFolder, "Mugshots");

            if (Directory.Exists(mugshotFolder))
            {
                string[] allFiles = Directory.GetFiles(mugshotFolder);
                foreach (string filename in allFiles)
                {
                    try
                    {
                        string username     = Path.GetFileNameWithoutExtension(filename);
                        Image  mugshotImage = new Bitmap(filename);
                        mugshotImage = mugshotImage.ResizeImage(MaxMugshotWidth, MaxMugshotHeight);

                        if (username != null && mugshotImage != null)
                        {
                            Mugshot mugshot = new Mugshot();

                            ImageConverter converter = new ImageConverter();
                            mugshot.Image    = (byte[])converter.ConvertTo(mugshotImage, typeof(byte[]));
                            mugshot.Username = username;

                            Cache[username] = mugshot;
                        }
                    }
                    catch (Exception e)
                    {
                        LogFile.WriteLine("Error loading {0} : {1}", filename, e.Message);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Set the mugshot for the current user. First change in the database and if we
        /// have network access, change on the server. If there's no network access we
        /// mark this as needing updating in the configuration.
        /// </summary>
        /// <param name="newMugshot">An image specifying the new mugshot</param>
        public void Update(Image newMugshot)
        {
            // Make sure the input image is the maximum size allowed
            newMugshot = newMugshot.ResizeImage(MaxMugshotWidth, MaxMugshotHeight);

            ImageConverter converter = new ImageConverter();

            Mugshot mugshot = CIX.DB.Table <Mugshot>().SingleOrDefault(c => c.Username == CIX.Username);

            if (mugshot != null)
            {
                mugshot.Image        = (byte[])converter.ConvertTo(newMugshot, typeof(byte[]));
                mugshot.CreationTime = DateTime.Now;

                lock (CIX.DBLock)
                {
                    CIX.DB.Update(mugshot);
                }
            }
            else
            {
                mugshot = new Mugshot
                {
                    Username     = CIX.Username,
                    CreationTime = DateTime.Now,
                    Image        = (byte[])converter.ConvertTo(newMugshot, typeof(byte[]))
                };

                lock (CIX.DBLock)
                {
                    CIX.DB.Insert(mugshot);
                }
            }

            Cache.Remove(CIX.Username);
            _realImage = null;

            if (!CIX.Online)
            {
                Pending = true;
                lock (CIX.DBLock)
                {
                    CIX.DB.Update(mugshot);
                }
            }
            else
            {
                mugshot.Sync();
            }
            CIX.NotifyMugshotUpdated(mugshot);
        }
Beispiel #3
0
        /// <summary>
        /// Retrieve the mugshot image for the specified user.
        /// <para>
        /// If no mugshot is stored locally then we will attempt to retrieve one from the server
        /// asynchronously. The caller will need to add to the MugshotUpdated to be notified when
        /// the image is retrieved. A default image is returned by this function in the meantime.
        /// </para>
        /// If the specified user has no mugshot then the default image is returned.
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="refresh">True if we force refresh the mugshot</param>
        /// <returns>An image for the mugshot to be used</returns>
        public static Mugshot MugshotForUser(string username, bool refresh)
        {
            Mugshot mugshot = null;

            if (username != null)
            {
                if (!_loadedExternalMugshots)
                {
                    LoadExternalMugshots();
                    _loadedExternalMugshots = true;
                }

                if (Cache.ContainsKey(username))
                {
                    mugshot = Cache[username];
                }
                else
                {
                    username = username.ToLower();
                    mugshot  = CIX.DB.Table <Mugshot>().SingleOrDefault(c => c.Username == username);

                    if (mugshot == null || mugshot.Image.Length == 0)
                    {
                        ImageConverter converter    = new ImageConverter();
                        Image          mugshotImage = GetDefaultMugshot();
                        mugshot = new Mugshot
                        {
                            Username = username,
                            Image    = (byte[])converter.ConvertTo(mugshotImage, typeof(byte[]))
                        };
                        lock (CIX.DBLock)
                        {
                            CIX.DB.Insert(mugshot);
                        }
                        if (refresh && username != "cix")
                        {
                            mugshot.Refresh();
                        }
                    }
                    Cache[username] = mugshot;
                }
            }
            return(mugshot);
        }
Beispiel #4
0
 /// <summary>
 /// Handle mugshot change events. We pass this through to the moderator component to
 /// refresh any that are affected.
 /// </summary>
 private void OnMugshotUpdated(Mugshot mugshot)
 {
     Platform.UIThread(this, () => _welcomePage.Refresh(mugshot.Username));
 }
Beispiel #5
0
 /// <summary>
 /// This notification is triggered if a mugshot is updated from the server. We're
 /// only interested if the current user's mugshot changes.
 /// </summary>
 /// <param name="mugshot">The mugshot</param>
 private void OnMugshotUpdated(Mugshot mugshot)
 {
     Platform.UIThread(this, delegate
     {
         if (mugshot.Username == CIX.Username)
         {
             actMugshot.Image = mugshot.RealImage;
         }
     });
 }
Beispiel #6
0
 /// <summary>
 /// Handle a mugshot updated. If this mugshot appears in any friend profile then we update
 /// the friend panel shown here.
 /// </summary>
 private void OnMugshotUpdated(Mugshot mugshot)
 {
     Platform.UIThread(this, delegate
     {
         if (Profile != null && Profile.Username == mugshot.Username)
         {
             ProfileItem item = (ProfileItem) prvDetails.Items[0];
             item.Image = mugshot.RealImage;
             item.UpdateImage();
         }
     });
 }
Beispiel #7
0
        /// <summary>
        /// This notification is triggered if a mugshot is updated from the server. We use this
        /// to refresh the images shown in the thread.
        /// </summary>
        /// <param name="mugshot">The mugshot object</param>
        private void OnMugshotUpdated(Mugshot mugshot)
        {
            Platform.UIThread(this, delegate
            {
                RefreshList();

                // Update any mugshot in the message pane too
                if (tsvMessagePane.Items.Count > 0)
                {
                    MessageItem control = tsvMessagePane.Items[0] as MessageItem;
                    if (control != null)
                    {
                        CIXMessage message = control.Message;
                        if (message.Author == mugshot.Username)
                        {
                            control.Image = mugshot.RealImage;
                            control.UpdateImage();
                        }
                    }
                }
            });
        }
Beispiel #8
0
        /// <summary>
        /// Load all external mugshots into the cache.
        /// </summary>
        private static void LoadExternalMugshots()
        {
            string mugshotFolder = Path.Combine(CIX.HomeFolder, "Mugshots");
            if (Directory.Exists(mugshotFolder))
            {
                string[] allFiles = Directory.GetFiles(mugshotFolder);
                foreach (string filename in allFiles)
                {
                    try
                    {
                        string username = Path.GetFileNameWithoutExtension(filename);
                        Image mugshotImage = new Bitmap(filename);
                        mugshotImage = mugshotImage.ResizeImage(MaxMugshotWidth, MaxMugshotHeight);

                        if (username != null && mugshotImage != null)
                        {
                            Mugshot mugshot = new Mugshot();

                            ImageConverter converter = new ImageConverter();
                            mugshot.Image = (byte[])converter.ConvertTo(mugshotImage, typeof(byte[]));
                            mugshot.Username = username;

                            Cache[username] = mugshot;
                        }
                    }
                    catch (Exception e)
                    {
                        LogFile.WriteLine("Error loading {0} : {1}", filename, e.Message);
                    }
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Set the mugshot for the current user. First change in the database and if we
        /// have network access, change on the server. If there's no network access we
        /// mark this as needing updating in the configuration.
        /// </summary>
        /// <param name="newMugshot">An image specifying the new mugshot</param>
        public void Update(Image newMugshot)
        {
            // Make sure the input image is the maximum size allowed
            newMugshot = newMugshot.ResizeImage(MaxMugshotWidth, MaxMugshotHeight);

            ImageConverter converter = new ImageConverter();

            Mugshot mugshot = CIX.DB.Table<Mugshot>().SingleOrDefault(c => c.Username == CIX.Username);
            if (mugshot != null)
            {
                mugshot.Image = (byte[])converter.ConvertTo(newMugshot, typeof(byte[]));
                mugshot.CreationTime = DateTime.Now;

                lock (CIX.DBLock)
                {
                    CIX.DB.Update(mugshot);
                }
            }
            else
            {
                mugshot = new Mugshot
                {
                    Username = CIX.Username,
                    CreationTime = DateTime.Now,
                    Image = (byte[])converter.ConvertTo(newMugshot, typeof(byte[]))
                };

                lock (CIX.DBLock)
                {
                    CIX.DB.Insert(mugshot);
                }
            }

            Cache.Remove(CIX.Username);
            _realImage = null;

            if (!CIX.Online)
            {
                Pending = true;
                lock (CIX.DBLock)
                {
                    CIX.DB.Update(mugshot);
                }
            }
            else
            {
                mugshot.Sync();
            }
            CIX.NotifyMugshotUpdated(mugshot);
        }
Beispiel #10
0
        /// <summary>
        /// Retrieve the mugshot image for the specified user.
        /// <para>
        /// If no mugshot is stored locally then we will attempt to retrieve one from the server
        /// asynchronously. The caller will need to add to the MugshotUpdated to be notified when
        /// the image is retrieved. A default image is returned by this function in the meantime.
        /// </para>
        /// If the specified user has no mugshot then the default image is returned.
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="refresh">True if we force refresh the mugshot</param>
        /// <returns>An image for the mugshot to be used</returns>
        public static Mugshot MugshotForUser(string username, bool refresh)
        {
            Mugshot mugshot = null;

            if (username != null)
            {
                if (!_loadedExternalMugshots)
                {
                    LoadExternalMugshots();
                    _loadedExternalMugshots = true;
                }

                if (Cache.ContainsKey(username))
                {
                    mugshot = Cache[username];
                }
                else
                {
                    username = username.ToLower();
                    mugshot = CIX.DB.Table<Mugshot>().SingleOrDefault(c => c.Username == username);

                    if (mugshot == null || mugshot.Image.Length == 0)
                    {
                        ImageConverter converter = new ImageConverter();
                        Image mugshotImage = GetDefaultMugshot();
                        mugshot = new Mugshot
                        {
                            Username = username,
                            Image = (byte[])converter.ConvertTo(mugshotImage, typeof(byte[]))
                        };
                        lock (CIX.DBLock)
                        {
                            CIX.DB.Insert(mugshot);
                        }
                        if (refresh && username != "cix")
                        {
                            mugshot.Refresh();
                        }
                    }
                    Cache[username] = mugshot;
                }
            }
            return mugshot;
        }
Beispiel #11
0
 /// <summary>
 /// This notification is triggered if a mugshot is updated from the server. We use this
 /// to refresh the images shown in the root message list.
 /// </summary>
 /// <param name="mugshot">The mugshot task</param>
 private void OnMugshotUpdated(Mugshot mugshot)
 {
     Platform.UIThread(this, delegate
     {
         // Update in the thread list.
         foreach (InboxItem item in inboxMessagePane.Items.Cast<InboxItem>())
         {
             InboxMessage message = item.Message;
             if (message.Author == mugshot.Username)
             {
                 item.Image = mugshot.RealImage;
                 item.Invalidate();
             }
         }
     });
 }
Beispiel #12
0
 /// <summary>
 /// Handle mugshot change events. We pass this through to the moderator component to
 /// refresh any that are affected.
 /// </summary>
 private void OnMugshotUpdated(Mugshot mugshot)
 {
     Platform.UIThread(this, delegate
     {
         if (frmCanvas.Items.Count > 1)
         {
             ProfileGroupItem moderatorsItem = (ProfileGroupItem) frmCanvas.Items[1];
             moderatorsItem.Refresh(mugshot.Username);
         }
     });
 }