1 /** 2 * @file NodeViewModel.cs 3 * @brief Class for the node view model. 4 * 5 * (c) 2013-2014 by Mega Limited, Auckland, New Zealand 6 * 7 * This file is part of the MEGA SDK - Client Access Engine. 8 * 9 * Applications using the MEGA API must present a valid application key 10 * and comply with the the rules set forth in the Terms of Service. 11 * 12 * The MEGA SDK is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 * 16 * @copyright Simplified (2-clause) BSD License. 17 * 18 * You should have received a copy of the license along with this 19 * program. 20 */ 21 22 using System; 23 using System.Collections.Generic; 24 using System.ComponentModel; 25 using System.Linq; 26 using System.Runtime.InteropServices.ComTypes; 27 using System.Text; 28 using System.Threading.Tasks; 29 using mega; 30 using MegaApp.Converters; 31 using MegaApp.Extensions; 32 using MegaApp.Resources; 33 34 namespace MegaApp.Models 35 { 36 /// <summary> 37 /// ViewModel of the main MEGA datatype (MNode) 38 /// </summary> 39 public class NodeViewModel : BaseViewModel 40 { 41 private readonly MegaSDK _megaSdk; 42 // Original MNode object from the MEGA SDK 43 private readonly MNode _baseNode; 44 // Offset DateTime value to calculate the correct creation and modification time 45 private static readonly DateTime OriginalDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); 46 NodeViewModel(MegaSDK megaSdk, MNode baseNode)47 public NodeViewModel(MegaSDK megaSdk, MNode baseNode) 48 { 49 this._megaSdk = megaSdk; 50 this._baseNode = baseNode; 51 this.Name = baseNode.getName(); 52 this.Size = baseNode.getSize(); 53 this.CreationTime = ConvertDateToString(_baseNode.getCreationTime()).ToString("dd MMM yyyy"); 54 this.ModificationTime = ConvertDateToString(_baseNode.getModificationTime()).ToString("dd MMM yyyy"); 55 this.SizeAndSuffix = Size.ToStringAndSuffix(); 56 this.Type = baseNode.getType(); 57 this.NumberOfFiles = this.Type != MNodeType.TYPE_FOLDER ? null : String.Format("{0} {1}", this._megaSdk.getNumChildren(this._baseNode), UiResources.Files); 58 59 if (this.Type == MNodeType.TYPE_FOLDER || this.Type == MNodeType.TYPE_FILE) 60 this.ThumbnailImageUri = new Uri((String)new FileTypeToImageConverter().Convert(this, null, null, null), UriKind.Relative); 61 } 62 63 #region Methods 64 65 /// <summary> 66 /// Convert the MEGA time to a C# DateTime object in local time 67 /// </summary> 68 /// <param name="time">MEGA time</param> 69 /// <returns>DateTime object in local time</returns> ConvertDateToString(ulong time)70 private static DateTime ConvertDateToString(ulong time) 71 { 72 return OriginalDateTime.AddSeconds(time).ToLocalTime(); 73 } 74 75 #endregion 76 77 #region Properties 78 79 public string Name { get; private set;} 80 81 public ulong Size { get; private set; } 82 83 public MNodeType Type { get; private set ; } 84 85 public string CreationTime { get; private set; } 86 87 public string ModificationTime { get; private set; } 88 89 public string SizeAndSuffix { get; private set; } 90 91 public string NumberOfFiles { get; private set; } 92 GetBaseNode()93 public MNode GetBaseNode() 94 { 95 return this._baseNode; 96 } 97 98 public Uri ThumbnailImageUri { get; private set; } 99 100 #endregion 101 102 103 } 104 } 105