summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/gui/DialogNumeric.h
blob: 8b5c592ba1240e22918a84083c426adf4e994676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once
/*
 *      Copyright (C) 2005-2017 Team KODI
 *      http://kodi.tv
 *
 *  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 KODI; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "definitions.h"
#include "../AddonBase.h"

namespace kodi
{
namespace gui
{

  //============================================================================
  ///
  /// \defgroup cpp_kodi_gui_DialogNumeric Dialog Numeric
  /// \ingroup cpp_kodi_gui
  /// @{
  /// @brief \cpp_namespace{ kodi::gui::DialogNumeric }
  /// **Numeric dialogs**
  ///
  /// The functions listed below have to be permitted by the user for the
  /// representation of a numeric keyboard around an input.
  ///
  /// The class supports several kinds, from an easy number choice up to the
  /// passport Word production and their confirmation for add-on.
  ///
  /// It has the header \ref DialogNumeric.h "#include <kodi/gui/DialogNumeric.h>"
  /// be included to enjoy it.
  ///
  namespace DialogNumeric
  {
    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to get numeric new password
    ///
    /// @param[out] newPassword        String to preload into the keyboard
    ///                                accumulator. Overwritten with user input
    ///                                if return=true. Returned in MD5 format.
    /// @return                        true if successful display and user
    ///                                input entry/re-entry.
    ///                                false if unsuccessful display, no user
    ///                                input, or canceled editing.
    ///
    inline bool ShowAndVerifyNewPassword(std::string& newPassword)
    {
      using namespace ::kodi::addon;
      char* pw = nullptr;
      bool ret = CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_verify_new_password(CAddonBase::m_interface->toKodi->kodiBase, &pw);
      if (pw != nullptr)
      {
        if (std::strlen(pw))
          newPassword = pw;
        CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, pw);
      }
      return ret;
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to verify numeric password.
    ///
    /// @param[in] password             Password to compare with user input, need
    ///                                 in MD5 format.
    /// @param[in] heading              Heading to display
    /// @param[in] retries              If greater than 0, shows "Incorrect
    ///                                 password, %d retries left" on dialog
    ///                                 line 2, else line 2 is blank.
    /// @return                         Possible values:
    ///                                 - 0 if successful display and user input.
    ///                                 - 1 if unsuccessful input.
    ///                                 - -1 if no user input or canceled editing.
    ///
    ///
    ///-------------------------------------------------------------------------
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.cpp}
    /// #include <stdio.h>      /* fprintf */
    /// #include <kodi/General.h>
    /// #include <kodi/gui/DialogNumeric.h>
    ///
    /// /*
    ///  * The example below shows the complete use of keyboard dialog for password
    ///  * check. If only one check from add-on needed can be function with retries
    ///  * set to '0' called alone.
    ///  *
    ///  * The use of MD5 translated password is always required for the check on Kodi!
    ///  */
    ///
    /// int maxretries = 3;
    ///
    /// /*
    ///  * Password names need to be send as md5 sum to kodi.
    ///  */
    /// std::string password = kodi::GetMD5("1234");
    ///
    /// /*
    ///  * To the loop about password checks.
    ///  */
    /// int ret;
    /// for (unsigned int i = 0; i < maxretries; i++)
    /// {
    ///   /*
    ///    * Ask the user about the password.
    ///    */
    ///   ret = kodi::gui::DialogNumeric::ShowAndVerifyPassword(password, "Demo numeric password call for PW '1234'", i);
    ///   if (ret == 0)
    ///   {
    ///     fprintf(stderr, "Numeric password successfull confirmed after '%i' tries\n", i+1);
    ///     break;
    ///   }
    ///   else if (ret < 0)
    ///   {
    ///     fprintf(stderr, "Canceled editing on try '%i'\n", i+1);
    ///     break;
    ///   }
    ///   else /* if (ret > 0) */
    ///   {
    ///     fprintf(stderr, "Wrong numeric password entered on try '%i'\n", i+1);
    ///   }
    /// }
    /// ~~~~~~~~~~~~~
    ///
    inline int ShowAndVerifyPassword(const std::string& password, const std::string& heading, int retries)
    {
      using namespace ::kodi::addon;
      return CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_verify_password(CAddonBase::m_interface->toKodi->kodiBase,
                                                                                                password.c_str(), heading.c_str(), retries);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to verify numeric password
    ///
    /// @param[in,out] toVerify         Value to compare against user input.
    /// @param[in] heading              Heading to display
    /// @param[in] verifyInput          If set as true we verify the users input
    ///                                 versus toVerify.
    /// @return                         true if successful display and user
    ///                                 input. false if unsuccessful display, no
    ///                                 user input, or canceled editing.
    ///
    inline bool ShowAndVerifyInput(std::string& toVerify, const std::string& heading, bool verifyInput)
    {
      using namespace ::kodi::addon;
      char* retString = nullptr;
      bool ret = CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_verify_input(CAddonBase::m_interface->toKodi->kodiBase,
                                                                                                 toVerify.c_str(), &retString, heading.c_str(), verifyInput);
      if (retString != nullptr)
      {
        if (std::strlen(retString))
          toVerify = retString;
        CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, retString);
      }
      return ret;
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to get time value.
    ///
    /// @param[out] time                Overwritten with user input if
    ///                                 return=true and time inserted.
    /// @param[in] heading              Heading to display.
    /// @return                         true if successful display and user
    ///                                 input. false if unsuccessful display, no
    ///                                 user input, or canceled editing.
    ///
    ///
    ///-------------------------------------------------------------------------
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.cpp}
    /// #include <stdio.h>      /* printf */
    /// #include <time.h>       /* time_t, struct tm, time, localtime, strftime */
    /// #include <kodi/gui/DialogNumeric.h>
    ///
    /// time_t rawtime;
    /// struct tm * timeinfo;
    /// char buffer [10];
    ///
    /// time (&rawtime);
    /// timeinfo = localtime(&rawtime);
    /// bool bRet = kodi::gui::DialogNumeric::ShowAndGetTime(*timeinfo, "Selected time test call");
    /// strftime(buffer, sizeof(buffer), "%H:%M.", timeinfo);
    /// printf("Selected time it's %s and was on Dialog %s\n", buffer, bRet ? "OK" : "Canceled");
    /// ~~~~~~~~~~~~~
    ///
    inline bool ShowAndGetTime(tm& time, const std::string& heading)
    {
      using namespace ::kodi::addon;
      return CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_get_time(CAddonBase::m_interface->toKodi->kodiBase, &time, heading.c_str());
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to get date value.
    ///
    /// @param[in,out] date             Overwritten with user input if
    ///                                 return=true and date inserted.
    /// @param[in] heading              Heading to display
    /// @return                         true if successful display and user
    ///                                 input. false if unsuccessful display, no
    ///                                 user input, or canceled editing.
    ///
    ///
    ///-------------------------------------------------------------------------
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.cpp}
    /// #include <stdio.h>      /* printf */
    /// #include <time.h>       /* time_t, struct tm, time, localtime, strftime */
    /// #include <kodi/gui/DialogNumeric.h>
    ///
    /// time_t rawtime;
    /// struct tm * timeinfo;
    /// char buffer [20];
    ///
    /// time (&rawtime);
    /// timeinfo = localtime(&rawtime);
    /// bool bRet = kodi::gui::DialogNumeric::ShowAndGetDate(*timeinfo, "Selected date test call");
    /// strftime(buffer, sizeof(buffer), "%Y-%m-%d", timeinfo);
    /// printf("Selected date it's %s and was on Dialog %s\n", buffer, bRet ? "OK" : "Canceled");
    /// ~~~~~~~~~~~~~
    ///
    inline bool ShowAndGetDate(tm& date, const std::string& heading)
    {
      using namespace ::kodi::addon;
      return CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_get_date(CAddonBase::m_interface->toKodi->kodiBase, &date, heading.c_str());
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to get a IP
    ///
    /// @param[in,out] ipAddress        Overwritten with user input if
    ///                                 return=true and IP address inserted.
    /// @param[in] heading              Heading to display.
    /// @return                         true if successful display and
    ///                                 user input. false if unsuccessful
    ///                                 display, no user input, or canceled
    ///                                 editing.
    ///
    inline bool ShowAndGetIPAddress(std::string& ipAddress, const std::string& heading)
    {
      using namespace ::kodi::addon;
      char* retString = nullptr;
      bool ret = CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_get_ip_address(CAddonBase::m_interface->toKodi->kodiBase,
                                                                                                   ipAddress.c_str(), &retString, heading.c_str());
      if (retString != nullptr)
      {
        if (std::strlen(retString))
          ipAddress = retString;
        CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, retString);
      }
      return ret;
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Use dialog to get normal number.
    ///
    /// @param[in,out] input            Overwritten with user input if
    ///                                 return=true and time in seconds inserted
    /// @param[in] heading              Heading to display
    /// @param[in] autoCloseTimeoutMs   To close the dialog after a specified
    ///                                 time, in milliseconds, default is 0
    ///                                 which keeps the dialog open
    ///                                 indefinitely.
    /// @return                         true if successful display and user
    ///                                 input. false if unsuccessful display, no
    ///                                 user input, or canceled editing.
    ///
    ///
    ///-------------------------------------------------------------------------
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.cpp}
    ///   #include <stdio.h>      /* printf */
    ///   #include <stdlib.h>     /* strtoull (C++11) */
    ///   #include <kodi/gui/DialogNumeric.h>
    ///
    ///   std::string number;
    ///   bool bRet = kodi::gui::DialogNumeric::ShowAndGetNumber(number, "Number test call");
    ///   printf("Written number input is : %llu and was %s\n",
    ///                  strtoull(number.c_str(), nullptr, 0), bRet ? "OK" : "Canceled");
    /// ~~~~~~~~~~~~~
    ///
    inline bool ShowAndGetNumber(std::string& input, const std::string& heading, unsigned int autoCloseTimeoutMs = 0)
    {
      using namespace ::kodi::addon;
      char* retString = nullptr;
      bool ret = CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_get_number(CAddonBase::m_interface->toKodi->kodiBase,
                                                                                               input.c_str(), &retString, heading.c_str(), autoCloseTimeoutMs);
      if (retString != nullptr)
      {
        if (std::strlen(retString))
          input = retString;
        CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, retString);
      }
      return ret;
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    ///
    /// \ingroup cpp_kodi_gui_DialogNumeric
    /// @brief Show numeric keypad to get seconds.
    ///
    /// @param[in,out] time     Overwritten with user input if return=true and
    ///                         time in seconds inserted.
    /// @param[in] heading      Heading to display
    /// @return                 true if successful display and user input. false
    ///                         if unsuccessful display, no user input, or
    ///                         canceled editing.
    ///
    inline bool ShowAndGetSeconds(std::string& time, const std::string& heading)
    {
      using namespace ::kodi::addon;
      char* retString = nullptr;
      bool ret = CAddonBase::m_interface->toKodi->kodi_gui->dialogNumeric->show_and_get_seconds(CAddonBase::m_interface->toKodi->kodiBase,
                                                                                                time.c_str(), &retString, heading.c_str());
      if (retString != nullptr)
      {
        if (std::strlen(retString))
          time = retString;
        CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, retString);
      }
      return ret;
    }
    //--------------------------------------------------------------------------
  };
  /// @}

} /* namespace gui */
} /* namespace kodi */