Exemplo n.º 1
0
        private void ImportNZB(string filename)
        {
            queueContainsNewItems = true;
            m_ImportNZB_Filename = filename;

            frmProgress frm = new frmProgress();
            frm.LongDurationCall += new NZB_O_Matic.frmProgress.DelLongDurationCall(m_ImportNZB);
            frm.ShowDialog(this);
            frm.Close();
            frm.Dispose();
            m_ServerManager.InitializeQueues();
            m_ServerManager.FillQueues();
        }
Exemplo n.º 2
0
        /*private void m_ImportNZB_Fast( frmProgress frm)
        {
            ArrayList newArticles = new ArrayList();
            System.IO.StreamReader strReader = new System.IO.StreamReader(m_ImportNZB_Filename);
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(strReader);
            xmlReader.XmlResolver = null;

            try
            {
                string Directory = System.IO.Path.GetFileNameWithoutExtension(m_ImportNZB_Filename);
                frm.lAction.Text = "Importing NZB file [" + Directory + "]";
                frm.pbProgress.Maximum = (int)strReader.BaseStream.Length;
                frm.Update();

                string Subject = "";
                DateTime Date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                string Poster = "";
                string ImportFile = "";
                ArrayList aGroups = new ArrayList();

                Article article = null;
                while( xmlReader.Read())
                {
                    if( xmlReader.NodeType == System.Xml.XmlNodeType.Element || xmlReader.NodeType == System.Xml.XmlNodeType.EndElement)
                    {
                        if( xmlReader.Name == "file" && xmlReader.NodeType == System.Xml.XmlNodeType.Element)
                        {
                            Subject = "";
                            Date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                            Poster = "";
                            ImportFile = System.IO.Path.GetFileNameWithoutExtension(m_ImportNZB_Filename);

                            for( int i = 0; i < xmlReader.AttributeCount; i++)
                            {
                                if( xmlReader.Name == "subject")
                                    Subject = xmlReader.Value;
                                if( xmlReader.Name == "date")
                                    Date = Date.AddSeconds(double.Parse(xmlReader.Value));
                                if( xmlReader.Name == "poster")
                                    Poster = xmlReader.Value;
                                if( xmlReader.Name == "importfile")
                                    ImportFile = xmlReader.Value;
                            }

                            xmlReader.MoveToElement();
                        }

                        if( xmlReader.Name == "groups" && xmlReader.NodeType == System.Xml.XmlNodeType.Element)
                            aGroups.Clear();

                        if( xmlReader.Name == "group")
                            aGroups.Add(xmlReader.Value);

                        if( xmlReader.Name == "groups" && xmlReader.NodeType == System.Xml.XmlNodeType.EndElement)
                        {
                            string[] Groups = new string[aGroups.Count];
                            for( int i = 0; i < aGroups.Count; i++)
                                Groups[i] = (string)aGroups[i];

                            article = new Article( Subject, Date, Poster, Groups, ImportFile);
                        }

                        if( xmlReader.Name == "segment" && article != null && xmlReader.NodeType == System.Xml.XmlNodeType.Element)
                        {
                            int Number = int.Parse(xmlReader.GetAttribute("number"));
                            int Bytes = int.Parse(xmlReader.GetAttribute("bytes"));
                            string ArticleID = xmlReader.Value;

                            article.AddSegment( Number, Bytes, ArticleID);
                        }

                        if( xmlReader.Name == "file" && xmlReader.NodeType == System.Xml.XmlNodeType.EndElement)
                        {
                            newArticles.Add( article);
                            article = null;
                        }
                    }

                    frm.pbProgress.Value = (int)strReader.BaseStream.Position;
                    frm.Update();
                }
            }
            catch
            {
                MessageBox.Show( "Error importing NZB file");
                return;
            }
        }*/
        private void m_ImportNZB( frmProgress frm)
        {
            this.lvArticles.BeginUpdate();
            System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            try
            {
                using(System.IO.StreamReader sr = new System.IO.StreamReader(m_ImportNZB_Filename))
                {
                    using(System.IO.StringWriter sw = new System.IO.StringWriter(sb))
                    {
                        string line;
                        do
                        {
                            line = sr.ReadLine();
                            if(line != null)
                                if(line != "" && line != "\n")
                                    sw.WriteLine(line);
                        } while (line != null);
                    }
                }
            }
            catch
            {
                MessageBox.Show( "Error importing NZB file");
                return;
            }

            try
            {
                XmlDoc.XmlResolver = null;
                System.IO.StringReader xmlsr = new System.IO.StringReader(sb.ToString());
                XmlDoc.Load(xmlsr);

                // Ugly way of removing the XML namespace
                if( XmlDoc.InnerXml.IndexOf( "xmlns=\"http://www.newzbin.com/DTD/2003/nzb\"") > 0)
                    XmlDoc.InnerXml = XmlDoc.InnerXml.Replace("xmlns=\"http://www.newzbin.com/DTD/2003/nzb\"", "");
                //just in case
                if( XmlDoc.InnerXml.IndexOf( "xmlns=\"http://www.newzbin.com/DTD/2004/nzb\"") > 0)
                    XmlDoc.InnerXml = XmlDoc.InnerXml.Replace("xmlns=\"http://www.newzbin.com/DTD/2004/nzb\"", "");
                // quick hack for newzbin2.es
                Regex rgx = new Regex("xmlns=\"http.*newzbin.*/dtd/.*/nzb\"", RegexOptions.IgnoreCase);
                XmlDoc.InnerXml = rgx.Replace(XmlDoc.InnerXml, "");
            }
            catch
            {
                MessageBox.Show( "Error importing NZB file");
                return;
            }

            if( XmlDoc.DocumentElement == null)
            {
                MessageBox.Show( "Error importing NZB file");
                return;
            }

            string Directory = System.IO.Path.GetFileNameWithoutExtension(m_ImportNZB_Filename);

            frm.lAction.Text = "Importing NZB file [" + Directory + "]";
            frm.pbProgress.Maximum = XmlDoc.DocumentElement.SelectNodes("file").Count;
            frm.Update();
            foreach( System.Xml.XmlNode XmlArticle in XmlDoc.DocumentElement.SelectNodes("file"))
            {
                string Subject = XmlArticle.SelectSingleNode("@subject").Value;
                DateTime Date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                Date = Date.AddSeconds(double.Parse(XmlArticle.SelectSingleNode("@date").Value));
                string Poster = XmlArticle.SelectSingleNode("@poster").Value;

                string ImportFile = "";
                if(( XmlArticle.SelectSingleNode("@importfile") != null) && XmlArticle.SelectSingleNode("@importfile").Value != "")
                    ImportFile = XmlArticle.SelectSingleNode("@importfile").Value;
                else
                    ImportFile = System.IO.Path.GetFileNameWithoutExtension(m_ImportNZB_Filename);

                int i = 0;
                string[] Groups = new string[XmlArticle.SelectNodes("groups/group").Count];
                foreach( System.Xml.XmlNode XmlGroup in XmlArticle.SelectNodes("groups/group"))
                {
                    Groups[i] = XmlGroup.InnerText;
                    i++;
                }

                Article article = new Article( Subject, Date, Poster, Groups, ImportFile);
                article.ListViewControl = lvArticles;

                article.Status = ArticleStatus.Loading;
                foreach( System.Xml.XmlNode XmlSegment in XmlArticle.SelectNodes("segments/segment"))
                {
                    int Number = int.Parse(XmlSegment.SelectSingleNode("@number").Value);
                    int Bytes = int.Parse(XmlSegment.SelectSingleNode("@bytes").Value);
                    string ArticleID = XmlSegment.InnerText;

                    article.AddSegment( Number, Bytes, ArticleID);
                }
                article.Segments.Sort( new SegmentSorter());

                if(article.FinishedParts == article.Segments.Count)
                {
                    article.Status = ArticleStatus.DecodeQueued;
                    m_ServerManager.AddArticle(article);
                    Decoder.DecodeQueue.Enqueue(article);
                }
                else
                {
                    if(Global.m_Options.PausePar2)
                    {
                        if( article.Subject.IndexOf(".vol") != -1 )
                        {
                            if( article.Subject.IndexOf(".par2") != -1 || article.Subject.IndexOf(".PAR2") != -1)
                            {
                                article.Status = ArticleStatus.Paused;
                            }
                        }
                        else
                        {
                            article.Status = ArticleStatus.Queued;
                        }
                    }
                    else
                    {
                        article.Status = ArticleStatus.Queued;
                    }
                    m_ServerManager.AddArticle(article);
                }

                lvArticles.Items.Add(article.StatusItem);

                frm.pbProgress.Value ++;
                frm.Update();
            }

            try
            {
                if(Global.m_Options.DeleteNZB)
                    System.IO.File.Delete(m_ImportNZB_Filename);
            }
            catch
            {
            }
            this.lvArticles.EndUpdate();
        }