[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / guilib / GUIKeyboardFactory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
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"
30
31
32 #include "dialogs/GUIDialogKeyboardGeneric.h"
33 #if defined(TARGET_DARWIN_IOS)
34 #include "osx/ios/IOSKeyboard.h"
35 #endif
36
37 FILTERING CGUIKeyboardFactory::m_filtering = FILTERING_NONE;
38
39 CGUIKeyboardFactory::CGUIKeyboardFactory(void)
40 {
41 }
42
43 CGUIKeyboardFactory::~CGUIKeyboardFactory(void)
44 {}
45
46 void CGUIKeyboardFactory::keyTypedCB(CGUIKeyboard *ref, const std::string &typedString)
47 {
48   if(ref)
49   {
50     // send our search message in safe way (only the active window needs it)
51     CGUIMessage message(GUI_MSG_NOTIFY_ALL, ref->GetWindowId(), 0);
52     switch(m_filtering)
53     {
54       case FILTERING_SEARCH:
55         message.SetParam1(GUI_MSG_SEARCH_UPDATE);
56         message.SetStringParam(typedString);
57         CApplicationMessenger::Get().SendGUIMessage(message, g_windowManager.GetActiveWindow());
58         break;
59       case FILTERING_CURRENT:
60         message.SetParam1(GUI_MSG_FILTER_ITEMS);
61         message.SetStringParam(typedString);
62         CApplicationMessenger::Get().SendGUIMessage(message);
63         break;
64       case FILTERING_NONE:
65         break;
66     }
67   }
68   ref->resetAutoCloseTimer();
69 }
70
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 */)
75 {
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());
85
86 #if defined(TARGET_DARWIN_IOS) && !defined(TARGET_DARWIN_IOS_ATV2)
87   kb = new CIOSKeyboard();
88 #endif
89
90   if(!kb)
91   {
92     kb = (CGUIDialogKeyboardGeneric*)g_windowManager.GetWindow(WINDOW_DIALOG_KEYBOARD);
93     needsFreeing = false;
94   }
95
96   if(kb)
97   {
98     kb->startAutoCloseTimer(autoCloseMs);
99     confirmed = kb->ShowAndGetInput(keyTypedCB, aTextString, aTextString, headingStr, hiddenInput);
100     if(needsFreeing)
101       delete kb;
102   }
103
104   if (confirmed)
105   {
106     if (!allowEmptyResult && aTextString.IsEmpty())
107       confirmed = false;
108   }
109
110   return confirmed;
111 }
112
113 bool CGUIKeyboardFactory::ShowAndGetInput(CStdString& aTextString, bool allowEmptyResult, unsigned int autoCloseMs /* = 0 */)
114 {
115   return ShowAndGetInput(aTextString, "", allowEmptyResult, autoCloseMs) != 0;
116 }
117
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 */)
121 {
122   return ShowAndGetInput(newPassword, heading, allowEmpty, true, autoCloseMs);
123 }
124
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 */)
128 {
129   return ShowAndGetNewPassword(newPassword, 12340, false, autoCloseMs);
130 }
131
132 bool CGUIKeyboardFactory::ShowAndGetFilter(CStdString &filter, bool searching, unsigned int autoCloseMs /* = 0 */)
133 {
134   m_filtering = searching ? FILTERING_SEARCH : FILTERING_CURRENT;
135   bool ret = ShowAndGetInput(filter, searching ? 16017 : 16028, true, autoCloseMs);
136   m_filtering = FILTERING_NONE;
137   return ret;
138 }
139
140
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 */)
147 {
148   // Prompt user for password input
149   CStdString userInput = "";
150   if (!ShowAndGetInput(userInput, heading, allowEmpty, true, autoCloseMs))
151   { // user cancelled, or invalid input
152     return false;
153   }
154   // success - verify the password
155   CStdString checkInput = "";
156   if (!ShowAndGetInput(checkInput, 12341, allowEmpty, true, autoCloseMs))
157   { // user cancelled, or invalid input
158     return false;
159   }
160   // check the password
161   if (checkInput == userInput)
162   {
163     XBMC::XBMC_MD5 md5state;
164     md5state.append(userInput);
165     md5state.getDigest(newPassword);
166     newPassword.ToLower();
167     return true;
168   }
169   CGUIDialogOK::ShowAndGetInput(12341, 12344, 0, 0);
170   return false;
171 }
172
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 */)
177 {
178   CStdString heading = g_localizeStrings.Get(12340);
179   return ShowAndVerifyNewPassword(newPassword, heading, false, autoCloseMs);
180 }
181
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 */)
188 {
189   CStdString strHeadingTemp;
190   if (1 > iRetries && strHeading.size())
191     strHeadingTemp = strHeading;
192   else
193     strHeadingTemp.Format("%s - %i %s", g_localizeStrings.Get(12326).c_str(), g_guiSettings.GetInt("masterlock.maxretries") - iRetries, g_localizeStrings.Get(12343).c_str());
194
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
198
199   if (!strPassword.IsEmpty())
200   {
201     if (strPassword == strUserInput)
202       return 0;
203
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
211   }
212   else
213   {
214     if (!strUserInput.IsEmpty())
215     {
216       XBMC::XBMC_MD5 md5state;
217       md5state.append(strUserInput);
218       md5state.getDigest(strPassword);
219       strPassword.ToLower();
220       return 0; // user entered correct password
221     }
222     else return 1;
223   }
224 }
225