PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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