2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program 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. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "Application.h"
22 #include "LocalizeStrings.h"
23 #include "GUIKeyboardFactory.h"
24 #include "dialogs/GUIDialogOK.h"
25 #include "GUIUserMessages.h"
26 #include "GUIWindowManager.h"
27 #include "settings/GUISettings.h"
28 #include "ApplicationMessenger.h"
29 #include "utils/md5.h"
32 #include "dialogs/GUIDialogKeyboardGeneric.h"
33 #if defined(TARGET_DARWIN_IOS)
34 #include "osx/ios/IOSKeyboard.h"
37 FILTERING CGUIKeyboardFactory::m_filtering = FILTERING_NONE;
39 CGUIKeyboardFactory::CGUIKeyboardFactory(void)
43 CGUIKeyboardFactory::~CGUIKeyboardFactory(void)
46 void CGUIKeyboardFactory::keyTypedCB(CGUIKeyboard *ref, const std::string &typedString)
50 // send our search message in safe way (only the active window needs it)
51 CGUIMessage message(GUI_MSG_NOTIFY_ALL, ref->GetWindowId(), 0);
54 case FILTERING_SEARCH:
55 message.SetParam1(GUI_MSG_SEARCH_UPDATE);
56 message.SetStringParam(typedString);
57 CApplicationMessenger::Get().SendGUIMessage(message, g_windowManager.GetActiveWindow());
59 case FILTERING_CURRENT:
60 message.SetParam1(GUI_MSG_FILTER_ITEMS);
61 message.SetStringParam(typedString);
62 CApplicationMessenger::Get().SendGUIMessage(message);
68 ref->resetAutoCloseTimer();
71 // Show keyboard with initial value (aTextString) and replace with result string.
72 // Returns: true - successful display and input (empty result may return true or false depending on parameter)
73 // false - unsucessful display of the keyboard or cancelled editing
74 bool CGUIKeyboardFactory::ShowAndGetInput(CStdString& aTextString, const CVariant &heading, bool allowEmptyResult, bool hiddenInput /* = false */, unsigned int autoCloseMs /* = 0 */)
76 bool confirmed = false;
77 CGUIKeyboard *kb = NULL;
78 bool needsFreeing = true;
79 //heading can be a string or a localization id
80 std::string headingStr;
81 if (heading.isString())
82 headingStr = heading.asString();
83 else if (heading.isInteger() && heading.asInteger())
84 headingStr = g_localizeStrings.Get((uint32_t)heading.asInteger());
86 #if defined(TARGET_DARWIN_IOS) && !defined(TARGET_DARWIN_IOS_ATV2)
87 kb = new CIOSKeyboard();
92 kb = (CGUIDialogKeyboardGeneric*)g_windowManager.GetWindow(WINDOW_DIALOG_KEYBOARD);
98 kb->startAutoCloseTimer(autoCloseMs);
99 confirmed = kb->ShowAndGetInput(keyTypedCB, aTextString, aTextString, headingStr, hiddenInput);
106 if (!allowEmptyResult && aTextString.IsEmpty())
113 bool CGUIKeyboardFactory::ShowAndGetInput(CStdString& aTextString, bool allowEmptyResult, unsigned int autoCloseMs /* = 0 */)
115 return ShowAndGetInput(aTextString, "", allowEmptyResult, autoCloseMs) != 0;
118 // Shows keyboard and prompts for a password.
119 // Differs from ShowAndVerifyNewPassword() in that no second verification is necessary.
120 bool CGUIKeyboardFactory::ShowAndGetNewPassword(CStdString& newPassword, const CVariant &heading, bool allowEmpty, unsigned int autoCloseMs /* = 0 */)
122 return ShowAndGetInput(newPassword, heading, allowEmpty, true, autoCloseMs);
125 // Shows keyboard and prompts for a password.
126 // Differs from ShowAndVerifyNewPassword() in that no second verification is necessary.
127 bool CGUIKeyboardFactory::ShowAndGetNewPassword(CStdString& newPassword, unsigned int autoCloseMs /* = 0 */)
129 return ShowAndGetNewPassword(newPassword, 12340, false, autoCloseMs);
132 bool CGUIKeyboardFactory::ShowAndGetFilter(CStdString &filter, bool searching, unsigned int autoCloseMs /* = 0 */)
134 m_filtering = searching ? FILTERING_SEARCH : FILTERING_CURRENT;
135 bool ret = ShowAndGetInput(filter, searching ? 16017 : 16028, true, autoCloseMs);
136 m_filtering = FILTERING_NONE;
141 // \brief Show keyboard twice to get and confirm a user-entered password string.
142 // \param newPassword Overwritten with user input if return=true.
143 // \param heading Heading to display
144 // \param allowEmpty Whether a blank password is valid or not.
145 // \return true if successful display and user input entry/re-entry. false if unsucessful display, no user input, or canceled editing.
146 bool CGUIKeyboardFactory::ShowAndVerifyNewPassword(CStdString& newPassword, const CVariant &heading, bool allowEmpty, unsigned int autoCloseMs /* = 0 */)
148 // Prompt user for password input
149 CStdString userInput = "";
150 if (!ShowAndGetInput(userInput, heading, allowEmpty, true, autoCloseMs))
151 { // user cancelled, or invalid input
154 // success - verify the password
155 CStdString checkInput = "";
156 if (!ShowAndGetInput(checkInput, 12341, allowEmpty, true, autoCloseMs))
157 { // user cancelled, or invalid input
160 // check the password
161 if (checkInput == userInput)
163 XBMC::XBMC_MD5 md5state;
164 md5state.append(userInput);
165 md5state.getDigest(newPassword);
166 newPassword.ToLower();
169 CGUIDialogOK::ShowAndGetInput(12341, 12344, 0, 0);
173 // \brief Show keyboard twice to get and confirm a user-entered password string.
174 // \param strNewPassword Overwritten with user input if return=true.
175 // \return true if successful display and user input entry/re-entry. false if unsucessful display, no user input, or canceled editing.
176 bool CGUIKeyboardFactory::ShowAndVerifyNewPassword(CStdString& newPassword, unsigned int autoCloseMs /* = 0 */)
178 CStdString heading = g_localizeStrings.Get(12340);
179 return ShowAndVerifyNewPassword(newPassword, heading, false, autoCloseMs);
182 // \brief Show keyboard and verify user input against strPassword.
183 // \param strPassword Value to compare against user input.
184 // \param dlgHeading String shown on dialog title. Converts to localized string if contains a positive integer.
185 // \param iRetries If greater than 0, shows "Incorrect password, %d retries left" on dialog line 2, else line 2 is blank.
186 // \return 0 if successful display and user input. 1 if unsucessful input. -1 if no user input or canceled editing.
187 int CGUIKeyboardFactory::ShowAndVerifyPassword(CStdString& strPassword, const CStdString& strHeading, int iRetries, unsigned int autoCloseMs /* = 0 */)
189 CStdString strHeadingTemp;
190 if (1 > iRetries && strHeading.size())
191 strHeadingTemp = strHeading;
193 strHeadingTemp.Format("%s - %i %s", g_localizeStrings.Get(12326).c_str(), g_guiSettings.GetInt("masterlock.maxretries") - iRetries, g_localizeStrings.Get(12343).c_str());
195 CStdString strUserInput = "";
196 if (!ShowAndGetInput(strUserInput, strHeadingTemp, false, true, autoCloseMs)) //bool hiddenInput = false/true ? TODO: GUI Setting to enable disable this feature y/n?
197 return -1; // user canceled out
199 if (!strPassword.IsEmpty())
201 if (strPassword == strUserInput)
204 CStdString md5pword2;
205 XBMC::XBMC_MD5 md5state;
206 md5state.append(strUserInput);
207 md5state.getDigest(md5pword2);
208 if (strPassword.Equals(md5pword2))
209 return 0; // user entered correct password
210 else return 1; // user must have entered an incorrect password
214 if (!strUserInput.IsEmpty())
216 XBMC::XBMC_MD5 md5state;
217 md5state.append(strUserInput);
218 md5state.getDigest(strPassword);
219 strPassword.ToLower();
220 return 0; // user entered correct password