Ejemplo n.º 1
0
        /// <summary>
        /// Opens a host file
        /// </summary>	
        /// <param name="sFile">The file to open.</param>		
        public bool OpenHostList( string sFile )
        {
            bool bRet = false;

            if( sFile == null )
            {
                // Ask for a xml file
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.FileName = m_sHostFileName;
                openFileDialog1.Filter = "XML File (*.xml)|*.xml|All Files (*.*)|*.*" ;
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.RestoreDirectory = true ;

                // Read and filter the raw data
                if(openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    sFile = openFileDialog1.FileName;
                }

            }

            if( sFile != null )
            {
                int iCount = 0;

                // Create the XML document
                XmlDocument xml = new XmlDocument();
                try
                {
                    // Try to load the file
                    xml.Load( sFile );

                    // Clean the old host list
                    Monitor.Enter( m_LockHosts );
                    m_Hosts.Clear();
                    m_Groups.Clear();
                    Monitor.Exit( m_LockHosts );

                    m_CurrentGroup = null;

                    // Try to get the host nodes
                    XmlNodeList xmlHosts = xml.SelectNodes( "/WakeOnLan/Hosts/Host" );

                    // Insert all hosts
                    Monitor.Enter( m_LockHosts );

                    foreach( XmlNode hostNode in xmlHosts )
                    {
                        // Insert the host
                        WOL2Host h = new WOL2Host( );
                        XmlNode n = hostNode.FirstChild;

                        if( h.DeserializeXML( n ) )
                        {
                            m_Hosts.Add( h );
                            h.SetState( WOL2HostState.wolHostOffline );
                            iCount++;
                        }

                    }
                    Monitor.Exit( m_LockHosts );

                    // Try to get the group nodes
                    XmlNodeList xmlGroups = xml.SelectNodes( "/WakeOnLan/Groups/Group" );

                    // Insert all groups
                    Monitor.Enter( m_Groups );
                    foreach( XmlNode groupNode in xmlGroups )
                    {
                        XmlNode n = groupNode.FirstChild;
                        WOL2Group g = new WOL2Group();
                        g.DeserializeXML( n );

                        if( g.IsValid() )
                        {
                            m_Groups.Add( g.GetName(), g );
                        }
                    }
                    Monitor.Exit( m_Groups );

                    RefreshGroupList();

                    // Get the HostFile Note
                    XmlNode tmp = xml.SelectSingleNode("/WakeOnLan/Comment");
                    if (tmp != null)
                    {
                        m_sHostFileNote = tmp.InnerText;
                        tbtnNote.ToolTipText = m_sHostFileNote;
                    }

                    // File loaded...
                    m_sHostFileName = sFile;
                    bRet = true;
                    RefreshHostList();

                    lblStatus.Text = String.Format( MOE.Utility.GetStringFromRes("strFileOpened"), iCount );

                    MOE.Logger.DoLog( "Opened host file " + sFile + " with " + iCount + " hosts.", Logger.LogLevel.lvlInfo );

                    // Add the file to the MRU list if not present yet
                    if (m_MruList.IndexOf(sFile) == -1)
                    {
                        m_MruList.Insert(0, sFile);
                        BuildMruList();
                    }

                } catch
                {
                    // Some Error
                }
            }

            // new file
            m_bChangedFile = false;

            return bRet;
        }