PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/CBR/CBR.Core/Services/WorkspaceService.cs

#
C# | 103 lines | 80 code | 16 blank | 7 comment | 16 complexity | da3623576fe056f587b0256ea38f9b16 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using CBR.Core.Models;
  6using CBR.Core.Helpers;
  7using System.IO;
  8
  9namespace CBR.Core.Services
 10{
 11    /// <summary>
 12    /// Provide access to the workspace settings
 13    /// </summary>
 14    public class WorkspaceService
 15    {
 16        #region ----------------SINGLETON----------------
 17
 18		public static readonly WorkspaceService Instance = new WorkspaceService();
 19
 20		/// <summary>
 21		/// Private constructor for singleton pattern
 22		/// </summary>
 23        private WorkspaceService()
 24		{
 25		}
 26
 27		#endregion
 28
 29        #region ----------------PROPERTIES----------------
 30
 31        private WorkspaceInfo _Settings = new WorkspaceInfo();
 32        public WorkspaceInfo Settings
 33        {
 34            get { return _Settings; }
 35            set
 36            {
 37                if (value != null && value != _Settings) _Settings = value;
 38            }
 39        }
 40
 41        #endregion
 42
 43        #region ----------------METHODS----------------
 44
 45        public void AddRecent(Catalog catlog)
 46        {
 47            if (Settings.RecentCatalogList == null)
 48                Settings.RecentCatalogList = new List<RecentFileInfo>();
 49
 50            Add(Path.GetDirectoryName(catlog.CatalogFilePath), Path.GetFileName(catlog.CatalogFilePath), Settings.RecentCatalogList);
 51        }
 52
 53        public void AddRecent(Book bk)
 54        {
 55            if (Settings.RecentFileList == null)
 56                Settings.RecentFileList = new List<RecentFileInfo>();
 57
 58            Add(Path.GetDirectoryName(bk.FilePath), Path.GetFileName(bk.FilePath), Settings.RecentFileList);
 59        }
 60
 61        internal void Add(string filePath, string fileName, List<RecentFileInfo> list)
 62        {
 63            try
 64            {
 65                RecentFileInfo rfi = list.Find(p => p.FileName == fileName && p.FilePath == filePath);
 66                if (rfi == null)
 67                {
 68                    list.Add(new RecentFileInfo()
 69                    {
 70                        FilePath = filePath,
 71                        FileName = fileName,
 72                        IsPined = false,
 73                        LastAccess = DateTime.Now
 74                    });
 75                    Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentListChanged, list);
 76                }
 77                else
 78                {
 79                    rfi.LastAccess = DateTime.Now;
 80                    Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentFileChanged, rfi);
 81                }
 82                //check the max
 83                int execiding = list.Count - Settings.MaxRecentFile;
 84                if (execiding > 0)
 85                {
 86                    List<RecentFileInfo> temp = new List<RecentFileInfo>();
 87                    temp.AddRange(list.OrderBy(o => o.LastAccess).Where(p => p.IsPined == false));
 88
 89                    for (int i = 0; i < execiding; i++)
 90                        list.Remove(temp[i]);
 91
 92                    Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentListChanged, list);
 93                }
 94            }
 95            catch (Exception err)
 96            {
 97                ExceptionHelper.Manage("CatalogService:Load", err);
 98            }
 99        }
100
101        #endregion
102    }
103}