bool CGUIWindowBoxeeWizardNetwork::OnAction(const CAction &action)
{
   int iControl = GetFocusedControlID();

   if (action.wID == ACTION_PREVIOUS_MENU || (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_BACK))
   {
      Close();
   }
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_INTERFACES)
   {  
     ShowWirelessNetworksIfNeeded();
     CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_INTERFACES);
     if (pList)
       pList->SetSingleSelectedItem();

     return true;
   }
   else if (action.wID == ACTION_MOVE_LEFT && iControl == CONTROL_WIRELESS)
   {
     ShowInterfaces();
     SET_CONTROL_FOCUS(CONTROL_INTERFACES, 0);
     CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_WIRELESS);
     if (pList)
       pList->SetSingleSelectedItem();

      return true;
   }
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_WIRELESS)
   {      
      ShowPasswordIfNeeded();
      CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_WIRELESS);
      if (pList)
        pList->SetSingleSelectedItem();
      return true;
   }
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_PASSWORD)   
   {      
      CGUIButtonControl* passwordButton = (CGUIButtonControl*) GetControl(iControl);
      CStdString password = passwordButton->GetLabel();
      if (CGUIDialogKeyboard::ShowAndGetInput(password, g_localizeStrings.Get(789), false))
      {         
         passwordButton->SetLabel(password);
         CONTROL_ENABLE(CONTROL_NEXT);                        
         SET_CONTROL_FOCUS(CONTROL_NEXT, 0);
      }
      return true;
   }
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_ENC)
   {      
      CGUIButtonControl* encSelectionButton = (CGUIButtonControl*) GetControl(CONTROL_ENC_SELECTION);
   
      CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_ENC);
      OnMessage(msg);
      int iItem = msg.GetParam1();
      encSelectionButton->SetLabel(ENC_LABELS[iItem]);
      
      SET_CONTROL_HIDDEN(CONTROL_ENC);
      
      if (iItem == ENC_NONE)
      {
         SET_CONTROL_HIDDEN(CONTROL_PASSWORD_GROUP);
         SET_CONTROL_FOCUS(CONTROL_NEXT, 0);
      }
      else
      {
         SET_CONTROL_FOCUS(CONTROL_PASSWORD, 0);
      }
      return true;
   }
   else if (action.wID == ACTION_MOVE_LEFT && (iControl == CONTROL_ENC_SELECTION || iControl == CONTROL_PASSWORD))
   {
      SET_CONTROL_HIDDEN(CONTROL_ENC_GROUP);
      SET_CONTROL_HIDDEN(CONTROL_PASSWORD_GROUP);
      SET_CONTROL_FOCUS(CONTROL_WIRELESS, 0);
      return true;
   }
   else if (action.wID == ACTION_MOVE_DOWN && iControl == CONTROL_ENC_SELECTION)
   {
      if (GetControl(CONTROL_PASSWORD_GROUP)->IsVisible())
      {
         SET_CONTROL_FOCUS(CONTROL_PASSWORD, 0);
      }
      else if (!GetControl(CONTROL_NEXT)->IsDisabled())
      {
         SET_CONTROL_FOCUS(CONTROL_NEXT, 0);
      }
      return true;     
   }
   else if (action.wID == ACTION_MOVE_UP && (iControl == CONTROL_NEXT || iControl == CONTROL_BACK))
   {
      if (GetControl(CONTROL_PASSWORD_GROUP)->IsVisible())
      {
         SET_CONTROL_FOCUS(CONTROL_PASSWORD, 0);
      }
      else if (GetControl(CONTROL_ENC_GROUP)->IsVisible())
      {
         SET_CONTROL_FOCUS(CONTROL_ENC_SELECTION, 0);
      }
      else 
      {
         SET_CONTROL_FOCUS(CONTROL_INTERFACES, 0);
      }
      return true;     
   }
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_NEXT)
   {
      if (GetControl(CONTROL_ENC_SELECTION)->IsVisible() && GetControl(CONTROL_PASSWORD)->IsVisible())
      {
        CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_ENC);
        OnMessage(msg);
        int iItem = msg.GetParam1();
        
        CGUIButtonControl* passwordButton = (CGUIButtonControl*) GetControl(CONTROL_PASSWORD);
        CStdString password = passwordButton->GetLabel();
    
        if (ENC_MODES[iItem] == ENC_WEP_HEX && !IsHexString(password))
        {
          CGUIDialogOK *pDialogOK = (CGUIDialogOK *)m_gWindowManager.GetWindow(WINDOW_DIALOG_OK);
          pDialogOK->SetHeading("");
          pDialogOK->SetLine(0, 51018);
          pDialogOK->SetLine(1, 51019);
          pDialogOK->DoModal();

          return true;
        }
      }
               
      if (SaveConfiguration())
      {
         // Close all wizard dialogs
         Close();
         CGUIDialog* dialog = (CGUIDialog*) m_gWindowManager.GetWindow(WINDOW_BOXEE_WIZARD_AUDIO);
         dialog->Close();
         dialog = (CGUIDialog*) m_gWindowManager.GetWindow(WINDOW_BOXEE_WIZARD_RESOLUTION);
         dialog->Close();
      }
      
      return true;
   }
   
   return CGUIWindow::OnAction(action);
}
void CGUIWindowBoxeeWizardResolution::ShowResolutionsList(bool HD, XMode* selectedMode)
{
   // Set either HD/SD according to the selected mode
   if (selectedMode != NULL)
   {
      CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), CONTROL_SD_HD, IsResolutionHD(*selectedMode) ? VALUE_HD : VALUE_SD);
      OnMessage(msg); 
   }
   
   SET_CONTROL_VISIBLE(CONTROL_RESOLUTIONS);

   // Clear the list first
   CGUIMessage msgReset(GUI_MSG_LABEL_RESET, GetID(), CONTROL_RESOLUTIONS);
   OnMessage(msgReset);
   
   for (unsigned int i = 0; i < m_resolutions.size(); i++)
      delete m_resolutions[i];
   m_resolutions.clear();
   m_resolutionNames.clear();

   // Go over all the resolutions and display them
   int iSelectedItem = 0;
   XOutput output = GetCurrentOutput();
   for (unsigned int i = 0; i < output.modes.size(); i++)
   {
      // skip modes with similar names (but different Hz)
      //if (i > 0 && output.modes[i].name == output.modes[i-1].name)
      //   continue;
         
      if (IsResolutionHD(output.modes[i]) && IsResolutionValid(output.modes[i]))
      {
         CStdString name;
         name.Format("%s / %.0fHz", output.modes[i].name.c_str(), output.modes[i].hz); 
         CFileItem *item = new CFileItem(name);
         item->SetProperty("resolutionName", output.modes[i].name);
         item->SetProperty("resolutionHz", (int) output.modes[i].hz);
         CGUIMessage msg(GUI_MSG_LABEL_ADD, GetID(), CONTROL_RESOLUTIONS, 0, 0, item);
         OnMessage(msg);            

         m_resolutions.push_back(item);
         
         CStdString resolutionName;
         resolutionName.Format("%s: %s @ %.2fHz", output.name.c_str(), output.modes[i].name, output.modes[i].hz);  
         m_resolutionNames.push_back(resolutionName);

         CStdString modeHz;
         modeHz.Format("%.2f", output.modes[i].hz);
         CStdString selectedHz;
         selectedHz.Format("%.2f", selectedMode->hz);
         
         if (selectedMode != NULL && output.modes[i].name == selectedMode->name && modeHz == selectedHz)
           iSelectedItem = m_resolutions.size() - 1;            
      }
   }
   
   CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), CONTROL_RESOLUTIONS, iSelectedItem);
   OnMessage(msg);      

   CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_RESOLUTIONS);
   if (pList)
     pList->SetSingleSelectedItem();
   
   SET_CONTROL_FOCUS(CONTROL_RESOLUTIONS, 0);
}
void CGUIWindowBoxeeWizardResolution::OnInitWindow()
{
   CGUIWindow::OnInitWindow();
   
   delete m_xrandr;
   m_xrandr = new CXRandR();
   
   if (!m_wizardCompletedOnce)
   {             
      CONTROL_DISABLE(CONTROL_NEXT);
   }
   
   if (m_afterResolutionChange)
   {
       CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_SD_HD);
       if (pList)
         pList->SetSingleSelectedItem();

      ShowResolutionsList(IsResolutionHD(m_newResolution), &m_newResolution);     
      
      CStdString origHz;
      origHz.Format("%.2f", m_originalResolution.hz);
      CStdString newHz;
      newHz.Format("%.2f", m_newResolution.hz);

      if (m_newResolution.name != m_originalResolution.name || origHz != newHz)
      {
         CGUIDialogYesNo *pDlgYesNo = (CGUIDialogYesNo*)m_gWindowManager.GetWindow(WINDOW_DIALOG_YES_NO);
         pDlgYesNo->SetHeading("Resolution Change");
         pDlgYesNo->SetLine(0, "Do you want to keep this resolution");
         pDlgYesNo->SetLine(1, "(will revert automatically in 6 seconds)");
         pDlgYesNo->SetLine(2, "");
         pDlgYesNo->SetLine(3, "");
         pDlgYesNo->SetChoice(0, "Revert");
         pDlgYesNo->SetChoice(1, "Keep");
         pDlgYesNo->SetDefaultChoice(0);
         pDlgYesNo->SetAutoClose(6000);
         pDlgYesNo->DoModal();
         
         if (!pDlgYesNo->IsConfirmed())
         {
           m_afterResolutionChange = false;   
           // Set the requested resolution
           g_graphicsContext.SetVideoResolution(m_originalResolutionId, TRUE);
           // Reload the fonts to they will scale correctly
           g_fontManager.ReloadTTFFonts();
           // Close the dialog and restart it so it will re-set the size of the labels to 
           // fit for the new resolutions
           Close();
                    
           m_gWindowManager.ActivateWindow(WINDOW_BOXEE_WIZARD_RESOLUTION);
         }
         else
         {
            int xbmcResolutionId = GetXBMCResolutionId();
            if (xbmcResolutionId != -1)
            {
               g_guiSettings.SetInt("videoscreen.resolution", xbmcResolutionId);
               g_guiSettings.SetInt("videoplayer.displayresolution", xbmcResolutionId);
               g_guiSettings.SetInt("pictures.displayresolution", xbmcResolutionId);
               g_settings.Save();
               delete m_xrandr;
               m_xrandr = new CXRandR();
            }
         
            m_wizardCompletedOnce = true;
            CONTROL_ENABLE(CONTROL_NEXT);
            SET_CONTROL_FOCUS(CONTROL_NEXT, 0);            
         }
      }
      else
      {
         m_wizardCompletedOnce = true;
         CONTROL_ENABLE(CONTROL_NEXT);
         SET_CONTROL_FOCUS(CONTROL_NEXT, 0);         
      }
   }
   else if (m_resolutionChangedOnce || m_wizardCompletedOnce)
   {
      XOutput output = GetCurrentOutput();
      XMode currentResolution =  m_xrandr->GetCurrentMode(output.name);
      ShowResolutionsList(IsResolutionHD(currentResolution), &currentResolution);      
   }
   else
   {
      XOutput output = GetCurrentOutput();
      XMode currentResolution =  m_xrandr->GetCurrentMode(output.name);
      CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), CONTROL_SD_HD, IsResolutionHD(currentResolution) ? VALUE_HD : VALUE_SD);
      OnMessage(msg); 
   }
}
bool CGUIWindowBoxeeWizardResolution::OnAction(const CAction &action)
{
   int iControl = GetFocusedControlID();

   if (action.wID == ACTION_PREVIOUS_MENU || (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_BACK))
   {
      Close();
      return true;     
   }
   else if (action.wID == ACTION_MOVE_LEFT && iControl == CONTROL_RESOLUTIONS)
   {
      SET_CONTROL_HIDDEN(CONTROL_RESOLUTIONS);
      SET_CONTROL_FOCUS(CONTROL_SD_HD, 0);
      return true;      
   }   
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_SD_HD)
   {
      SET_CONTROL_VISIBLE(CONTROL_RESOLUTIONS);
      
      CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_SD_HD);
      OnMessage(msg);
      int iItem = msg.GetParam1();

      CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_SD_HD);
      if (pList)
        pList->SetSingleSelectedItem();

      XOutput output = GetCurrentOutput();
      XMode currentResolution =  m_xrandr->GetCurrentMode(output.name);
      ShowResolutionsList(iItem == VALUE_HD, &currentResolution);
      
      return true;      
   }      
   else if (action.wID == ACTION_SELECT_ITEM && iControl == CONTROL_RESOLUTIONS)
   {
      CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_RESOLUTIONS);
      OnMessage(msg);
      int iItem = msg.GetParam1();

      CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_RESOLUTIONS);
      if (pList)
        pList->SetSingleSelectedItem();
   
      XOutput output = GetCurrentOutput();
      m_originalResolution =  m_xrandr->GetCurrentMode(output.name);
      m_originalResolutionId = g_graphicsContext.GetVideoResolution();
      
      for (unsigned int i = 0; i < output.modes.size(); i++)
      {
         if (output.modes[i].name == m_resolutions[iItem]->GetProperty("resolutionName") &&
             (int)output.modes[i].hz == m_resolutions[iItem]->GetPropertyInt("resolutionHz"))
         {
            m_newResolution = output.modes[i];

            CStdString origHz;
            origHz.Format("%.2f", m_originalResolution.hz);
            CStdString newHz;
            newHz.Format("%.2f", m_newResolution.hz);
            
            if (m_newResolution.name != m_originalResolution.name || newHz != origHz)
            {
               // Set the requested resolution
               RESOLUTION res = (RESOLUTION) GetXBMCResolutionId();
               g_graphicsContext.SetVideoResolution(res, TRUE);
   
               // Reload the fonts to they will scale correctly
               g_fontManager.ReloadTTFFonts();
            }
            
            m_afterResolutionChange = true;
            m_resolutionChangedOnce = true;
            
            // Close the dialog and restart it so it will re-set the size of the labels to 
            // fit for the new resolutions
            Close();
            m_gWindowManager.ActivateWindow(WINDOW_BOXEE_WIZARD_RESOLUTION);
                                  
            break;
         }
      }
      
      return true;      
   }
   else if (action.wID == ACTION_MOVE_UP && (iControl == CONTROL_NEXT || iControl == CONTROL_BACK))
   {
      if (GetControl(CONTROL_RESOLUTIONS)->IsVisible())
      {
         SET_CONTROL_FOCUS(CONTROL_RESOLUTIONS, 0);
      }
      else
      {
         SET_CONTROL_FOCUS(CONTROL_SD_HD, 0);
      }
      
      return true;
   }
  
   return CGUIWindow::OnAction(action);
}