/// <summary> /// add an item to path usign given values /// </summary> /// <param name="path"></param> /// <param name="checksum"></param> /// <param name="snapshot"></param> /// <param name="date"></param> /// <returns></returns> public bool additem(string path, string checksum, string snapshot, DateTime date) { string newName = Path.GetFileName(path); bool dir = !File.Exists(path); string[] pathArray = spiltPath(path); if (pathArray.Length <= 2) { if (this.getitem(pathArray[1]) == null) { if (dir) { this.additem(new TrackedFolder(pathArray[1], this, date)); } else { this.additem(new TrackedFile(pathArray[1], this, checksum, snapshot, date)); } return true; } else { return false; } } else { TrackedFolder insertFolder = (TrackedFolder)getitem(pathArray[1]); if (insertFolder == null) { insertFolder = new TrackedFolder(pathArray[1], this, date); this.additem(insertFolder); } return insertFolder.additem(pathArray, 2, dir, checksum, snapshot, date); } }
/// <summary> /// loads folder to xml loading contained file and folder. /// </summary> /// <param name="writer"></param> /// <returns>success</returns> public virtual bool load(XmlReader reader) { string name; string dateString; while (reader.Read()) { // Only detect start elements. if (reader.IsStartElement()) { // Get element name and switch on it. switch (reader.Name) { case "Folder": // Detect this element. name = reader.GetAttribute("Name"); dateString = reader.GetAttribute("Date"); if (name != null) { DateTime date = DateTime.MinValue; if (dateString != null) { long time = 0; if (long.TryParse(dateString, out time)) { date = DateTime.FromFileTime(time); } } TrackedFolder folder = new TrackedFolder(name, this, date); folder.load(reader); additem(folder); } break; case "File": // Detect this article element. Console.WriteLine("Start <article> element."); // Search for the attribute name on this current node. name = reader.GetAttribute("Name"); string readChecksum = reader.GetAttribute("Checksum"); string snapshot = reader.GetAttribute("Snapshot"); dateString = reader.GetAttribute("Date"); if (name != null && readChecksum != null && snapshot != null) { DateTime date = DateTime.MinValue; if (dateString != null) { long time = 0; if (long.TryParse(dateString, out time)) { date = DateTime.FromFileTime(time); } } additem(new TrackedFile(name, this, readChecksum, snapshot, date)); } break; } } } return true; }
/// <summary> /// create a folder using values /// </summary> /// <param name="name"></param> /// <param name="parent"></param> /// <param name="checksum"></param> /// <param name="snapshot"></param> /// <param name="lastUpdate"></param> public TrackedFolder(string name, TrackedFolder parent, DateTime date) : base(name, parent, "", "", date) { if(parent!=null) this.root = parent.getRoot(); contents = new List<TrackedFile>(); directory = true; this.LastUpdate = date; }
/// <summary> /// adds the item from values to given location in tree /// </summary> /// <param name="path"></param> /// <param name="pos"></param> /// <param name="dir"></param> /// <param name="checksum"></param> /// <param name="snapshot"></param> /// <param name="date"></param> /// <returns>success</returns> public bool additem(string[] path, int pos, bool dir, string checksum, string snapshot, DateTime date) { if (path.Length == pos + 1 && !dir) { if (this.getitem(path[pos]) == null) { this.additem(new TrackedFile(path[pos], this, checksum, snapshot, date)); return true; } else { return false; } } else { bool added = false; TrackedFolder insertFolder = (TrackedFolder)getitem(path[pos]); if (insertFolder == null) { insertFolder = new TrackedFolder(path[pos], this, date); this.additem(insertFolder); added = true; } if (path.Length > ++pos) { added = insertFolder.additem(path, pos, dir, checksum, snapshot, date); } return added; } }
/// <summary> /// create a files using values /// </summary> /// <param name="name"></param> /// <param name="parent"></param> /// <param name="checksum"></param> /// <param name="snapshot"></param> /// <param name="lastUpdate"></param> public TrackedFile(string name, TrackedFolder parent, string checksum, string snapshot, DateTime lastUpdate) { if(parent!=null) this.root = parent.getRoot(); this.name = name; this.parent = parent; this.checksum = checksum; this.snapshot = snapshot; this.directory = false; this.lastUpdate = lastUpdate; }