Пример #1
0
        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();
                }
            };
        }