private void Init()
    {
      CheckForIllegalCrossThreadCalls = false;
      //
      // Set caption
      //
      Text = "MediaPortal - TV Server Configuration";

      //
      // Build options tree
      //
      try
      {
        XmlDocument doc = new XmlDocument();
        doc.Load(String.Format(@"{0}\gentle.config", PathManager.GetDataPath));
        XmlNode nodeKey = doc.SelectSingleNode("/Gentle.Framework/DefaultProvider");
        XmlNode node = nodeKey.Attributes.GetNamedItem("connectionString");
        XmlNode nodeProvider = nodeKey.Attributes.GetNamedItem("name");

        Gentle.Framework.ProviderFactory.ResetGentle(true);
        Gentle.Framework.GentleSettings.DefaultProviderName = nodeProvider.InnerText;
        Gentle.Framework.ProviderFactory.GetDefaultProvider();
        Gentle.Framework.ProviderFactory.SetDefaultProviderConnectionString(node.InnerText);
      }
      catch (Exception ex)
      {
        MessageBox.Show("Unable to open:" + String.Format(@"{0}\gentle.config", PathManager.GetDataPath));
        Log.Write(ex);
      }

      try
      {
        Server.ListAll();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Failed to open database");
        Log.Error("Unable to get list of servers");
        Log.Write(ex);
      }

      Project project = new Project();
      AddSection(project);

      layer = new TvBusinessLayer();
      servers = new Servers();
      AddSection(servers);
      IList<Server> dbsServers = Server.ListAll();

      if (dbsServers != null)
      {
        foreach (Server server in dbsServers)
        {
          if (server.IsMaster)
          {
            bool connected = false;
            while (!connected)
            {
              RemoteControl.HostName = server.HostName;

              if (server.ReferringCard().Count > 0)
              {
                try
                {
                  Card c = (Card)server.ReferringCard()[0];
                  RemoteControl.Instance.Type(c.IdCard);
                  connected = true;
                }
                catch (Exception ex)
                {
                  string localHostname = Dns.GetHostName();
                  if (localHostname != server.HostName)
                  {
                    DialogResult dlg = MessageBox.Show(String.Format("Unable to connect to <{0}>.\n" +
                                                                     "Do you want to try the current comupter name ({1}) instead?",
                                                                     server.HostName, localHostname),
                                                       "Wrong config detected",
                                                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (dlg == DialogResult.Yes)
                    {
                      Log.Info("Controller: server {0} changed to {1}", server.HostName, localHostname);
                      server.HostName = localHostname;
                      server.Persist();
                      RemoteControl.Clear();
                      ServiceHelper.Restart();
                      ServiceHelper.WaitInitialized();
                    }
                    else
                    {
                      MessageBox.Show("Setup will now close");
                      Environment.Exit(-1);
                    }
                  }
                  else
                  {
                    Log.Error("Cannot connect to server {0}", server.HostName);
                    Log.Write(ex);
                    DialogResult dlg = MessageBox.Show("Unable to connect to <" + server.HostName + ">.\n" +
                                                       "Please check the TV Server logs for details.\n\n" +
                                                       "Setup will now close.");
                    Environment.Exit(-1);
                  }
                }
              }
            }
            break;
          }
        }

        AddServerTvCards(servers, dbsServers, false);

        TvChannels tvChannels = new TvChannels();
        AddSection(tvChannels);
        AddChildSection(tvChannels, new TvCombinations("TV Combinations"));
        AddChildSection(tvChannels, new TvChannelMapping());

        RadioChannels radioChannels = new RadioChannels();
        AddSection(radioChannels);
        AddChildSection(radioChannels, new RadioCombinations("Radio Combinations"));
        AddChildSection(radioChannels, new RadioChannelMapping());

        Epg EpgSection = new Epg();
        AddSection(EpgSection);
        AddChildSection(EpgSection, new TvEpgGrabber());
        AddChildSection(EpgSection, new RadioEpgGrabber());

        AddSection(new ScanSettings());
        AddSection(new TvRecording());
        AddSection(new TvTimeshifting());
        AddSection(new TvSchedules());
        AddSection(new StreamingServer());
        AddSection(new UserPriorities());

        AddSection(new TestService("Manual Control"));
        AddSection(new TestChannels("Test Channels"));

        _pluginLoader.Load();
        pluginsRoot = new Plugins("Plugins", _pluginLoader);
        AddSection(pluginsRoot);

        pluginsRoot.ChangedActivePlugins += SectChanged;

        foreach (ITvServerPlugin plugin in _pluginLoader.Plugins)
        {
          SectionSettings settings = plugin.Setup;
          if (settings != null)
          {
            Setting isActive = layer.GetSetting(String.Format("plugin{0}", plugin.Name), "false");
            settings.Text = plugin.Name;
            if (isActive.Value == "true")
            {
              AddChildSection(pluginsRoot, settings);
            }
          }
        }
        if (showAdvancedSettings)
        {
          AddSection(new DebugOptions());
        }
        AddSection(new ImportExport());
        AddSection(new ThirdPartyChecks());

        sectionTree.SelectedNode = sectionTree.Nodes[0];
        // make sure window is in front of mediaportal
      }
      BringToFront();
    }
 private void AddServerTvCards(Servers servers, IList<Server> dbsServers, bool reloaded)
 {
   foreach (Server server in dbsServers)
   {
     bool isLocal = (server.HostName.ToLowerInvariant() == Dns.GetHostName().ToLowerInvariant() ||
                     server.HostName.ToLowerInvariant() == Dns.GetHostName().ToLowerInvariant() + "."
                     +
                     System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName.
                       ToLowerInvariant());
     cardPage = new TvCards(server.HostName);
     cardPage.TvCardsChanged += OnTvCardsChanged;
     AddChildSection(servers, cardPage, 0);
     foreach (Card dbsCard in server.ReferringCard())
     {
       if (dbsCard.Enabled == true && RemoteControl.Instance.CardPresent(dbsCard.IdCard))
       {
         CardType type = RemoteControl.Instance.Type(dbsCard.IdCard);
         int cardId = dbsCard.IdCard;
         string cardName = dbsCard.Name;
         switch (type)
         {
           case CardType.Analog:
             cardName = String.Format("{0} Analog {1}", cardId, cardName);
             AddChildSection(cardPage, new CardAnalog(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.DvbT:
             cardName = String.Format("{0} DVB-T {1}", cardId, cardName);
             AddChildSection(cardPage, new CardDvbT(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.DvbC:
             cardName = String.Format("{0} DVB-C {1}", cardId, cardName);
             AddChildSection(cardPage, new CardDvbC(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.DvbS:
             cardName = String.Format("{0} DVB-S {1}", cardId, cardName);
             AddChildSection(cardPage, new CardDvbS(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.Atsc:
             cardName = String.Format("{0} ATSC {1}", cardId, cardName);
             AddChildSection(cardPage, new CardAtsc(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.DvbIP:
             cardName = String.Format("{0} DVB-IP {1}", cardId, cardName);
             AddChildSection(cardPage, new CardDvbIP(cardName, dbsCard.IdCard), 1);
             break;
           case CardType.RadioWebStream:
             cardName = String.Format("{0} {1}", cardId, cardName);
             InfoPage RadioWebStreamInfo = new InfoPage(cardName);
             RadioWebStreamInfo.InfoText =
               "The RadioWebStream card does not have any options.\n\n\nYou can add your favourite radio webstreams under:\n\n --> 'Radio Channels', 'Add', 'Web-Stream' or by importing a playlist.";
             AddChildSection(cardPage, RadioWebStreamInfo, 1);
             break;
           case CardType.Unknown:
             cardName = String.Format("{0} Unknown {1}", cardId, cardName);
             AddChildSection(cardPage, new CardAnalog(cardName, dbsCard.IdCard), 1);
             break;
         }
       }
     }
     if (isLocal)
     {
       Utils.CheckForDvbHotfix();
     }
     if (reloaded)
     {
       SectionTreeNode activeNode = (SectionTreeNode)settingSections[server.HostName];
       if (activeNode != null)
       {
         activeNode.Expand();
       }
     }
   }
 }