private void PopulateDirectoryTree() { DirectoryTreeController.Clear(); DirectoryTree.Clear(); IDictionary <Directory, HierarchicalLibraryItem> directoryLookup = new SortedDictionary <Directory, HierarchicalLibraryItem>(); foreach (Directory directory in m_DataModel.Database.Directories.Values) { CreateDirectoryBranchNodesRecursively(directory, directoryLookup); } foreach (Song song in m_DataModel.Database.Songs.Values) { if (song.Directory == null) { DirectoryTree.Add(new HierarchicalLibraryItem(song, DirectoryTreeController)); } else { new HierarchicalLibraryItem(song, directoryLookup[song.Directory]); } } DirectoryTreeController.ResetNodeIds(); }
private void PopulateDirectoryTree() { DirectoryTreeController.ClearMultiSelection(); DirectoryTree.Clear(); DirectoryTreeViewNode rootNode = new DirectoryTreeViewNode("/", null, DirectoryTreeController); IDictionary <string, TreeViewNode> directoryLookup = new SortedDictionary <string, TreeViewNode>(); directoryLookup[rootNode.DisplayString] = rootNode; foreach (SongMetadata song in m_DataModel.Database.Songs) { TreeViewNode parent = FindDirectoryNode(song.Directory, directoryLookup, rootNode); SongMetadataTreeViewNode leaf = new SongMetadataTreeViewNode(song.Filename, song, parent, DirectoryTreeController); parent.AddChild(leaf); } AssignTreeViewNodeIDs(rootNode, 0); if (rootNode.Children.Count > 0) { DirectoryTree.Add(rootNode); rootNode.IsExpanded = true; } }
static void CreateTreeXMLWriter(MainForm mainForm, DirectoryScanner scanner) { DirectoryTree tree = new DirectoryTree(); AsyncEventSubscriber <ItemFoundEventArgs> subscriber = null; // todo: move it to separate class mainForm.onScanStart += (DirectoryScanEventArgs e) => { tree.Clear(); // todo: check second start subscriber = new AsyncEventSubscriber <ItemFoundEventArgs>(); scanner.onItemFound += subscriber.Handler; subscriber.OnEvent += (object sender, ItemFoundEventArgs ev) => { lock (tree) { if (ev.Info is DirectoryInfo) { tree.AddDirectory(ev.Info as DirectoryInfo); } else if (ev.Info is FileInfo) { tree.AddFile(ev.Info as FileInfo); } } }; subscriber.Thread.Start(); }; mainForm.onScanFinish += (DirectoryScanEventArgs e) => { lock (tree) { // it hope it's not null subscriber.Thread.Abort(); // write XML in brand new thread new Thread(() => { // big one try { XmlWriter xmlWriter = XmlWriter.Create(e.XMLFilePath + "-deep.xml"); DirectoryXMLWriter directoryWriter = new DirectoryXMLWriter(xmlWriter); Action <DirectoryModel> writeDirFunc = null; writeDirFunc = (DirectoryModel directory) => { directoryWriter.StartDirectory(); directoryWriter.WriteDirectory(directory.Info); // am I lazy? // I cannot understand the domain // I cannot really come up with some place I can move this shit to // should I create one more "DirectoryXMLWriter" or maybe replace that one with this? xmlWriter.WriteElementString("size", directory.Size + " bytes"); xmlWriter.WriteStartElement("directories"); directory.Directories.ForEach(writeDirFunc); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("files"); directory.Files.ForEach((FileInfo file) => { directoryWriter.StartFile(); directoryWriter.WriteFile(file); directoryWriter.EndFile(); }); xmlWriter.WriteEndElement(); directoryWriter.EndDirectory(); }; directoryWriter.Start(); writeDirFunc(tree.GetRoot()); directoryWriter.End(); } catch { MessageBox.Show("File is not writable"); return; } }).Start(); } }; }