~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to clientgui/ValidateEmailAddress.cpp

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Berkeley Open Infrastructure for Network Computing
2
 
// http://boinc.berkeley.edu
3
 
// Copyright (C) 2005 University of California
4
 
//
5
 
// This is free software; you can redistribute it and/or
6
 
// modify it under the terms of the GNU Lesser General Public
7
 
// License as published by the Free Software Foundation;
8
 
// either version 2.1 of the License, or (at your option) any later version.
9
 
//
10
 
// This software is distributed in the hope that it will be useful,
11
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 
// See the GNU Lesser General Public License for more details.
14
 
//
15
 
// To view the GNU Lesser General Public License visit
16
 
// http://www.gnu.org/copyleft/lesser.html
17
 
// or write to the Free Software Foundation, Inc.,
18
 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 
20
 
#if defined(__GNUG__) && !defined(__APPLE__)
21
 
#pragma implementation "ValidateEmailAddress.h"
22
 
#endif
23
 
 
24
 
#include "stdwx.h"
25
 
#include "ValidateEmailAddress.h"
26
 
 
27
 
 
28
 
IMPLEMENT_DYNAMIC_CLASS(CValidateEmailAddress, wxValidator)
29
 
 
30
 
 
31
 
CValidateEmailAddress::CValidateEmailAddress(wxString *val) {
32
 
    m_stringValue = val ;
33
 
}
34
 
 
35
 
 
36
 
CValidateEmailAddress::CValidateEmailAddress(const CValidateEmailAddress& val)
37
 
    : wxValidator()
38
 
{
39
 
    Copy(val);
40
 
}
41
 
 
42
 
 
43
 
CValidateEmailAddress::~CValidateEmailAddress() {}
44
 
 
45
 
 
46
 
bool CValidateEmailAddress::Copy(const CValidateEmailAddress& val) {
47
 
    wxValidator::Copy(val);
48
 
 
49
 
    m_stringValue = val.m_stringValue ;
50
 
 
51
 
    return TRUE;
52
 
}
53
 
 
54
 
 
55
 
bool CValidateEmailAddress::Validate(wxWindow *parent) {
56
 
    if(!CheckValidator())
57
 
        return FALSE;
58
 
 
59
 
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
60
 
 
61
 
    if (!control->IsEnabled())
62
 
        return TRUE;
63
 
 
64
 
    bool ok = TRUE;
65
 
    wxString val(control->GetValue().Trim().Trim(false));  // trim spaces before and after
66
 
 
67
 
    // Regular Expression from Frank S. Thomas
68
 
    wxRegEx reEmail(
69
 
        wxT("^([^@]+)@([^@\\.]+)\\.([^@]{2,})$"));
70
 
 
71
 
    if (val.Length() == 0) {
72
 
        ok = FALSE;
73
 
        m_errormsg = _("Please specify an email address");
74
 
    } else if (!reEmail.Matches(val)) {
75
 
        ok = FALSE;
76
 
        m_errormsg = _("Invalid email address; please enter a valid email address");
77
 
    }
78
 
 
79
 
    if (!ok) {
80
 
        wxASSERT_MSG(!m_errormsg.empty(), _T("you forgot to set errormsg"));
81
 
 
82
 
        m_validatorWindow->SetFocus();
83
 
 
84
 
        wxString buf;
85
 
        buf.Printf(m_errormsg, control->GetValue().c_str());
86
 
 
87
 
        wxMessageBox(buf, _("Validation conflict"),
88
 
            wxOK | wxICON_EXCLAMATION, parent
89
 
        );
90
 
    }
91
 
 
92
 
    return ok;
93
 
}
94
 
 
95
 
 
96
 
bool CValidateEmailAddress::TransferToWindow(void) {
97
 
    if(!CheckValidator())
98
 
        return FALSE;
99
 
    
100
 
    if (!m_stringValue)
101
 
        return TRUE;
102
 
 
103
 
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
104
 
    control->SetValue(* m_stringValue) ;
105
 
 
106
 
    return TRUE;
107
 
}
108
 
 
109
 
 
110
 
bool CValidateEmailAddress::TransferFromWindow(void) {
111
 
    if(!CheckValidator())
112
 
        return FALSE;
113
 
 
114
 
    if (!m_stringValue)
115
 
        return TRUE;
116
 
 
117
 
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
118
 
    * m_stringValue = control->GetValue() ;
119
 
 
120
 
    return TRUE;
121
 
}
122
 
 
123
 
 
124
 
bool CValidateEmailAddress::wxIsAlphaNumeric(const wxString& val) {
125
 
    int i;
126
 
    for (i = 0; i < (int)val.Length(); i++) {
127
 
        if (!wxIsalnum(val[i]))
128
 
            return FALSE;
129
 
    }
130
 
    return TRUE;
131
 
}
132
 
 
133
 
 
134
 
bool CValidateEmailAddress::CheckValidator() const {
135
 
    wxCHECK_MSG(m_validatorWindow, FALSE,
136
 
                    _T("No window associated with validator"));
137
 
    wxCHECK_MSG(m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)), FALSE,
138
 
                    _T("wxTextValidator is only for wxTextCtrl's"));
139
 
    wxCHECK_MSG(m_stringValue, FALSE,
140
 
                    _T("No variable storage for validator"));
141
 
 
142
 
    return TRUE;
143
 
}
 
1
// This file is part of BOINC.
 
2
// http://boinc.berkeley.edu
 
3
// Copyright (C) 2008 University of California
 
4
//
 
5
// BOINC is free software; you can redistribute it and/or modify it
 
6
// under the terms of the GNU Lesser General Public License
 
7
// as published by the Free Software Foundation,
 
8
// either version 3 of the License, or (at your option) any later version.
 
9
//
 
10
// BOINC is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
// See the GNU Lesser General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU Lesser General Public License
 
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
#if defined(__GNUG__) && !defined(__APPLE__)
 
19
#pragma implementation "ValidateEmailAddress.h"
 
20
#endif
 
21
 
 
22
#include "stdwx.h"
 
23
#include "ValidateEmailAddress.h"
 
24
#include "BOINCGUIApp.h"
 
25
 
 
26
 
 
27
IMPLEMENT_DYNAMIC_CLASS(CValidateEmailAddress, wxValidator)
 
28
 
 
29
 
 
30
CValidateEmailAddress::CValidateEmailAddress(wxString *val) {
 
31
    m_stringValue = val ;
 
32
}
 
33
 
 
34
 
 
35
CValidateEmailAddress::CValidateEmailAddress(const CValidateEmailAddress& val)
 
36
    : wxValidator()
 
37
{
 
38
    Copy(val);
 
39
}
 
40
 
 
41
 
 
42
CValidateEmailAddress::~CValidateEmailAddress() {}
 
43
 
 
44
 
 
45
bool CValidateEmailAddress::Copy(const CValidateEmailAddress& val) {
 
46
    wxValidator::Copy(val);
 
47
 
 
48
    m_stringValue = val.m_stringValue ;
 
49
 
 
50
    return TRUE;
 
51
}
 
52
 
 
53
 
 
54
bool CValidateEmailAddress::Validate(wxWindow *parent) {
 
55
    if(!CheckValidator())
 
56
        return FALSE;
 
57
 
 
58
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
 
59
 
 
60
    if (!control->IsEnabled())
 
61
        return TRUE;
 
62
 
 
63
    bool ok = TRUE;
 
64
    wxString val(control->GetValue().Trim().Trim(false));  // trim spaces before and after
 
65
 
 
66
    // Regular Expression from Frank S. Thomas
 
67
    wxRegEx reEmail(
 
68
        wxT("^([^@]+)@([^@\\.]+)\\.([^@]{2,})$"));
 
69
 
 
70
    if (val.Length() == 0) {
 
71
        ok = FALSE;
 
72
        m_errormsg = _("Please specify an email address");
 
73
    } else if (!reEmail.Matches(val)) {
 
74
        ok = FALSE;
 
75
        m_errormsg = _("Invalid email address; please enter a valid email address");
 
76
    }
 
77
 
 
78
    if (!ok) {
 
79
        wxASSERT_MSG(!m_errormsg.empty(), _T("you forgot to set errormsg"));
 
80
 
 
81
        m_validatorWindow->SetFocus();
 
82
 
 
83
        wxString buf;
 
84
        buf.Printf(m_errormsg, control->GetValue().c_str());
 
85
 
 
86
        wxGetApp().SafeMessageBox(buf, _("Validation conflict"),
 
87
            wxOK | wxICON_EXCLAMATION, parent
 
88
        );
 
89
    }
 
90
 
 
91
    return ok;
 
92
}
 
93
 
 
94
 
 
95
bool CValidateEmailAddress::TransferToWindow(void) {
 
96
    if(!CheckValidator())
 
97
        return FALSE;
 
98
    
 
99
    if (!m_stringValue)
 
100
        return TRUE;
 
101
 
 
102
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
 
103
    control->SetValue(* m_stringValue) ;
 
104
 
 
105
    return TRUE;
 
106
}
 
107
 
 
108
 
 
109
bool CValidateEmailAddress::TransferFromWindow(void) {
 
110
    if(!CheckValidator())
 
111
        return FALSE;
 
112
 
 
113
    if (!m_stringValue)
 
114
        return TRUE;
 
115
 
 
116
    wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
 
117
    * m_stringValue = control->GetValue() ;
 
118
 
 
119
    return TRUE;
 
120
}
 
121
 
 
122
 
 
123
bool CValidateEmailAddress::wxIsAlphaNumeric(const wxString& val) {
 
124
    int i;
 
125
    for (i = 0; i < (int)val.Length(); i++) {
 
126
        if (!wxIsalnum(val[i]))
 
127
            return FALSE;
 
128
    }
 
129
    return TRUE;
 
130
}
 
131
 
 
132
 
 
133
bool CValidateEmailAddress::CheckValidator() const {
 
134
    wxCHECK_MSG(m_validatorWindow, FALSE,
 
135
                    _T("No window associated with validator"));
 
136
    wxCHECK_MSG(m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)), FALSE,
 
137
                    _T("wxTextValidator is only for wxTextCtrl's"));
 
138
    wxCHECK_MSG(m_stringValue, FALSE,
 
139
                    _T("No variable storage for validator"));
 
140
 
 
141
    return TRUE;
 
142
}