Skip to content

Commit

Permalink
guilib: add input validation functionality to CGUIEditControl
Browse files Browse the repository at this point in the history
  • Loading branch information
Montellese committed Sep 9, 2013
1 parent dc2e092 commit c1baf21
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
43 changes: 43 additions & 0 deletions xbmc/guilib/GUIEditControl.cpp
Expand Up @@ -66,6 +66,9 @@ void CGUIEditControl::DefaultConstructor()
m_label.SetAlign(m_label.GetLabelInfo().align & XBFONT_CENTER_Y); // left align m_label.SetAlign(m_label.GetLabelInfo().align & XBFONT_CENTER_Y); // left align
m_label2.GetLabelInfo().offsetX = 0; m_label2.GetLabelInfo().offsetX = 0;
m_isMD5 = false; m_isMD5 = false;
m_invalidInput = false;
m_inputValidator = NULL;
m_inputValidatorData = NULL;
} }


CGUIEditControl::CGUIEditControl(const CGUIButtonControl &button) CGUIEditControl::CGUIEditControl(const CGUIButtonControl &button)
Expand Down Expand Up @@ -335,6 +338,8 @@ void CGUIEditControl::UpdateText(bool sendUpdate)
m_smsTimer.Stop(); m_smsTimer.Stop();
if (sendUpdate) if (sendUpdate)
{ {
ValidateInput();

SEND_CLICK_MESSAGE(GetID(), GetParentID(), 0); SEND_CLICK_MESSAGE(GetID(), GetParentID(), 0);


m_textChangeActions.ExecuteActions(GetID(), GetParentID()); m_textChangeActions.ExecuteActions(GetID(), GetParentID());
Expand Down Expand Up @@ -508,6 +513,7 @@ void CGUIEditControl::SetLabel2(const std::string &text)
m_isMD5 = (m_inputType == INPUT_TYPE_PASSWORD_MD5 || m_inputType == INPUT_TYPE_PASSWORD_NUMBER_VERIFY_NEW); m_isMD5 = (m_inputType == INPUT_TYPE_PASSWORD_MD5 || m_inputType == INPUT_TYPE_PASSWORD_NUMBER_VERIFY_NEW);
m_text2 = newText; m_text2 = newText;
m_cursorPos = m_text2.size(); m_cursorPos = m_text2.size();
ValidateInput();
SetInvalid(); SetInvalid();
} }
} }
Expand Down Expand Up @@ -598,3 +604,40 @@ void CGUIEditControl::OnPasteClipboard()
UpdateText(); UpdateText();
} }
} }

void CGUIEditControl::SetInputValidation(StringValidation::Validator inputValidator, void *data /* = NULL */)
{
if (m_inputValidator == inputValidator)
return;

m_inputValidator = inputValidator;
m_inputValidatorData = data;
// the input validator has changed, so re-validate the current data
ValidateInput();
}

bool CGUIEditControl::ValidateInput(const CStdStringW &data) const
{
if (m_inputValidator == NULL)
return true;

return m_inputValidator(GetLabel2(), (void*)(m_inputValidatorData != NULL ? m_inputValidatorData : this));
}

void CGUIEditControl::ValidateInput()
{
// validate the input
bool invalid = !ValidateInput(m_text2);
// nothing to do if still valid/invalid
if (invalid != m_invalidInput)
{
// the validity state has changed so we need to update the control
m_invalidInput = invalid;

// let the window/dialog know that the validity has changed
CGUIMessage msg(GUI_MSG_VALIDITY_CHANGED, GetID(), GetID(), m_invalidInput ? 0 : 1);
SendWindowMessage(msg);

SetInvalid();
}
}
11 changes: 11 additions & 0 deletions xbmc/guilib/GUIEditControl.h
Expand Up @@ -30,6 +30,7 @@


#include "GUIButtonControl.h" #include "GUIButtonControl.h"
#include "utils/Stopwatch.h" #include "utils/Stopwatch.h"
#include "utils/StringValidation.h"


/*! /*!
\ingroup controls \ingroup controls
Expand Down Expand Up @@ -80,6 +81,9 @@ class CGUIEditControl : public CGUIButtonControl


bool HasTextChangeActions() const { return m_textChangeActions.HasActionsMeetingCondition(); }; bool HasTextChangeActions() const { return m_textChangeActions.HasActionsMeetingCondition(); };


virtual bool HasInvalidInput() const { return m_invalidInput; }
virtual void SetInputValidation(StringValidation::Validator inputValidator, void *data = NULL);

protected: protected:
virtual void ProcessText(unsigned int currentTime); virtual void ProcessText(unsigned int currentTime);
virtual void RenderText(); virtual void RenderText();
Expand All @@ -91,6 +95,9 @@ class CGUIEditControl : public CGUIButtonControl
void OnSMSCharacter(unsigned int key); void OnSMSCharacter(unsigned int key);
void DefaultConstructor(); void DefaultConstructor();


virtual bool ValidateInput(const CStdStringW &data) const;
void ValidateInput();

/*! \brief Clear out the current text input if it's an MD5 password. /*! \brief Clear out the current text input if it's an MD5 password.
\return true if the password is cleared, false otherwise. \return true if the password is cleared, false otherwise.
*/ */
Expand All @@ -114,6 +121,10 @@ class CGUIEditControl : public CGUIButtonControl


CGUIAction m_textChangeActions; CGUIAction m_textChangeActions;


bool m_invalidInput;
StringValidation::Validator m_inputValidator;
void *m_inputValidatorData;

unsigned int m_smsKeyIndex; unsigned int m_smsKeyIndex;
unsigned int m_smsLastKey; unsigned int m_smsLastKey;
CStopWatch m_smsTimer; CStopWatch m_smsTimer;
Expand Down
2 changes: 2 additions & 0 deletions xbmc/guilib/GUIMessage.h
Expand Up @@ -133,6 +133,8 @@


#define GUI_MSG_WINDOW_LOAD 43 #define GUI_MSG_WINDOW_LOAD 43


#define GUI_MSG_VALIDITY_CHANGED 44

#define GUI_MSG_USER 1000 #define GUI_MSG_USER 1000


/*! /*!
Expand Down
33 changes: 33 additions & 0 deletions xbmc/utils/StringValidation.h
@@ -0,0 +1,33 @@
#pragma once
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include <string>

class StringValidation
{
public:
typedef bool (*Validator)(const std::string &input, void *data);

static bool NonEmpty(const std::string &input, void *data) { return !input.empty(); }

private:
StringValidation() { }
};

0 comments on commit c1baf21

Please sign in to comment.