/CBR/CBR.Core/Services/WorkspaceService.cs
C# | 103 lines | 80 code | 16 blank | 7 comment | 16 complexity | da3623576fe056f587b0256ea38f9b16 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CBR.Core.Models;
- using CBR.Core.Helpers;
- using System.IO;
-
- namespace CBR.Core.Services
- {
- /// <summary>
- /// Provide access to the workspace settings
- /// </summary>
- public class WorkspaceService
- {
- #region ----------------SINGLETON----------------
-
- public static readonly WorkspaceService Instance = new WorkspaceService();
-
- /// <summary>
- /// Private constructor for singleton pattern
- /// </summary>
- private WorkspaceService()
- {
- }
-
- #endregion
-
- #region ----------------PROPERTIES----------------
-
- private WorkspaceInfo _Settings = new WorkspaceInfo();
- public WorkspaceInfo Settings
- {
- get { return _Settings; }
- set
- {
- if (value != null && value != _Settings) _Settings = value;
- }
- }
-
- #endregion
-
- #region ----------------METHODS----------------
-
- public void AddRecent(Catalog catlog)
- {
- if (Settings.RecentCatalogList == null)
- Settings.RecentCatalogList = new List<RecentFileInfo>();
-
- Add(Path.GetDirectoryName(catlog.CatalogFilePath), Path.GetFileName(catlog.CatalogFilePath), Settings.RecentCatalogList);
- }
-
- public void AddRecent(Book bk)
- {
- if (Settings.RecentFileList == null)
- Settings.RecentFileList = new List<RecentFileInfo>();
-
- Add(Path.GetDirectoryName(bk.FilePath), Path.GetFileName(bk.FilePath), Settings.RecentFileList);
- }
-
- internal void Add(string filePath, string fileName, List<RecentFileInfo> list)
- {
- try
- {
- RecentFileInfo rfi = list.Find(p => p.FileName == fileName && p.FilePath == filePath);
- if (rfi == null)
- {
- list.Add(new RecentFileInfo()
- {
- FilePath = filePath,
- FileName = fileName,
- IsPined = false,
- LastAccess = DateTime.Now
- });
- Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentListChanged, list);
- }
- else
- {
- rfi.LastAccess = DateTime.Now;
- Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentFileChanged, rfi);
- }
- //check the max
- int execiding = list.Count - Settings.MaxRecentFile;
- if (execiding > 0)
- {
- List<RecentFileInfo> temp = new List<RecentFileInfo>();
- temp.AddRange(list.OrderBy(o => o.LastAccess).Where(p => p.IsPined == false));
-
- for (int i = 0; i < execiding; i++)
- list.Remove(temp[i]);
-
- Mediator.Instance.NotifyColleagues(ViewModelBaseMessages.RecentListChanged, list);
- }
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("CatalogService:Load", err);
- }
- }
-
- #endregion
- }
- }