1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "GUIDialogYesNo.h"
10 
11 #include "ServiceBroker.h"
12 #include "guilib/GUIComponent.h"
13 #include "guilib/GUIWindowManager.h"
14 #include "input/Key.h"
15 #include "messaging/helpers/DialogHelper.h"
16 
CGUIDialogYesNo(int overrideId)17 CGUIDialogYesNo::CGUIDialogYesNo(int overrideId /* = -1 */)
18     : CGUIDialogBoxBase(overrideId == -1 ? WINDOW_DIALOG_YES_NO : overrideId, "DialogConfirm.xml")
19 {
20   Reset();
21 }
22 
23 CGUIDialogYesNo::~CGUIDialogYesNo() = default;
24 
OnMessage(CGUIMessage & message)25 bool CGUIDialogYesNo::OnMessage(CGUIMessage& message)
26 {
27   switch ( message.GetMessage() )
28   {
29   case GUI_MSG_CLICKED:
30     {
31       int iControl = message.GetSenderId();
32       int iAction = message.GetParam1();
33       if (true || ACTION_SELECT_ITEM == iAction)
34       {
35         if (iControl == CONTROL_NO_BUTTON)
36         {
37           m_bConfirmed = false;
38           Close();
39           return true;
40         }
41         if (iControl == CONTROL_YES_BUTTON)
42         {
43           m_bConfirmed = true;
44           Close();
45           return true;
46         }
47         if (iControl == CONTROL_CUSTOM_BUTTON)
48         {
49           m_bConfirmed = false;
50           m_bCustom = true;
51           Close();
52           return true;
53         }
54       }
55     }
56     break;
57   }
58   return CGUIDialogBoxBase::OnMessage(message);
59 }
60 
OnBack(int actionID)61 bool CGUIDialogYesNo::OnBack(int actionID)
62 {
63   m_bCanceled = true;
64   m_bConfirmed = false;
65   m_bCustom = false;
66   return CGUIDialogBoxBase::OnBack(actionID);
67 }
68 
OnInitWindow()69 void CGUIDialogYesNo::OnInitWindow()
70 {
71   if (!m_strChoices[2].empty())
72     SET_CONTROL_VISIBLE(CONTROL_CUSTOM_BUTTON);
73   else
74     SET_CONTROL_HIDDEN(CONTROL_CUSTOM_BUTTON);
75   SET_CONTROL_HIDDEN(CONTROL_PROGRESS_BAR);
76   SET_CONTROL_FOCUS(CONTROL_NO_BUTTON, 0);
77 
78   CGUIDialogBoxBase::OnInitWindow();
79 }
80 
ShowAndGetInput(const CVariant & heading,const CVariant & line0,const CVariant & line1,const CVariant & line2,bool & bCanceled)81 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
82                                       const CVariant& line0,
83                                       const CVariant& line1,
84                                       const CVariant& line2,
85                                       bool& bCanceled)
86 {
87   return ShowAndGetInput(heading, line0, line1, line2, bCanceled, "", "", NO_TIMEOUT);
88 }
89 
ShowAndGetInput(const CVariant & heading,const CVariant & line0,const CVariant & line1,const CVariant & line2,const CVariant & noLabel,const CVariant & yesLabel)90 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
91                                       const CVariant& line0,
92                                       const CVariant& line1,
93                                       const CVariant& line2,
94                                       const CVariant& noLabel /* = "" */,
95                                       const CVariant& yesLabel /* = "" */)
96 {
97   bool bDummy(false);
98   return ShowAndGetInput(heading, line0, line1, line2, bDummy, noLabel, yesLabel, NO_TIMEOUT);
99 }
100 
ShowAndGetInput(const CVariant & heading,const CVariant & line0,const CVariant & line1,const CVariant & line2,bool & bCanceled,const CVariant & noLabel,const CVariant & yesLabel,unsigned int autoCloseTime)101 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
102                                       const CVariant& line0,
103                                       const CVariant& line1,
104                                       const CVariant& line2,
105                                       bool& bCanceled,
106                                       const CVariant& noLabel,
107                                       const CVariant& yesLabel,
108                                       unsigned int autoCloseTime)
109 {
110   CGUIDialogYesNo *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogYesNo>(WINDOW_DIALOG_YES_NO);
111   if (!dialog)
112     return false;
113 
114   dialog->SetHeading(heading);
115   dialog->SetLine(0, line0);
116   dialog->SetLine(1, line1);
117   dialog->SetLine(2, line2);
118   if (autoCloseTime)
119     dialog->SetAutoClose(autoCloseTime);
120   dialog->SetChoice(0, !noLabel.empty() ? noLabel : 106);
121   dialog->SetChoice(1, !yesLabel.empty() ? yesLabel : 107);
122   dialog->SetChoice(2, "");
123   dialog->m_bCanceled = false;
124   dialog->Open();
125 
126   bCanceled = dialog->m_bCanceled;
127   return (dialog->IsConfirmed()) ? true : false;
128 }
129 
ShowAndGetInput(const CVariant & heading,const CVariant & text)130 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading, const CVariant& text)
131 {
132   bool bDummy(false);
133   return ShowAndGetInput(heading, text, "", "", bDummy);
134 }
135 
ShowAndGetInput(const CVariant & heading,const CVariant & text,bool & bCanceled,const CVariant & noLabel,const CVariant & yesLabel,unsigned int autoCloseTime)136 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
137                                       const CVariant& text,
138                                       bool& bCanceled,
139                                       const CVariant& noLabel /* = "" */,
140                                       const CVariant& yesLabel /* = "" */,
141                                       unsigned int autoCloseTime)
142 {
143   int result = ShowAndGetInput(heading, text, noLabel, yesLabel, "", autoCloseTime);
144 
145   bCanceled = result == -1;
146   return result == 1;
147 }
148 
Reset()149 void CGUIDialogYesNo::Reset()
150 {
151   m_bConfirmed = false;
152   m_bCanceled = false;
153   m_bCustom = false;
154   m_bAutoClosed = false;
155 }
156 
GetResult() const157 int CGUIDialogYesNo::GetResult() const
158 {
159   if (m_bCanceled)
160     return -1;
161   else if (m_bCustom)
162     return 2;
163   else if (IsConfirmed())
164     return 1;
165   else
166     return 0;
167 }
168 
ShowAndGetInput(const CVariant & heading,const CVariant & text,const CVariant & noLabel,const CVariant & yesLabel,const CVariant & customLabel,unsigned int autoCloseTime)169 int CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
170                                      const CVariant& text,
171                                      const CVariant& noLabel,
172                                      const CVariant& yesLabel,
173                                      const CVariant& customLabel,
174                                      unsigned int autoCloseTime)
175 {
176   CGUIDialogYesNo *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogYesNo>(WINDOW_DIALOG_YES_NO);
177   if (!dialog)
178     return false;
179 
180   dialog->SetHeading(heading);
181   dialog->SetText(text);
182   if (autoCloseTime > 0)
183     dialog->SetAutoClose(autoCloseTime);
184   dialog->m_bCanceled = false;
185   dialog->m_bCustom = false;
186   dialog->SetChoice(0, !noLabel.empty() ? noLabel : 106);
187   dialog->SetChoice(1, !yesLabel.empty() ? yesLabel : 107);
188   dialog->SetChoice(2, customLabel);  // Button only visible when label is not empty
189 
190   dialog->Open();
191 
192   return dialog->GetResult();
193 }
194 
ShowAndGetInput(const KODI::MESSAGING::HELPERS::DialogYesNoMessage & options)195 int CGUIDialogYesNo::ShowAndGetInput(const KODI::MESSAGING::HELPERS::DialogYesNoMessage& options)
196 {
197   //Set default yes/no labels, these might be overwritten further down if specified
198   //by the caller
199   SetChoice(0, 106);
200   SetChoice(1, 107);
201   SetChoice(2, "");
202   if (!options.heading.isNull())
203     SetHeading(options.heading);
204   if (!options.text.isNull())
205     SetText(options.text);
206   if (!options.noLabel.isNull())
207     SetChoice(0, options.noLabel);
208   if (!options.yesLabel.isNull())
209     SetChoice(1, options.yesLabel);
210   if (!options.customLabel.isNull())
211     SetChoice(2, options.customLabel);
212   if (options.autoclose > 0)
213     SetAutoClose(options.autoclose);
214   m_bCanceled = false;
215   m_bCustom = false;
216 
217   for (size_t i = 0; i < 3; ++i)
218   {
219     if (!options.lines[i].isNull())
220       SetLine(i, options.lines[i]);
221   }
222 
223   Open();
224 
225   return GetResult();
226 }
227 
GetDefaultLabelID(int controlId) const228 int CGUIDialogYesNo::GetDefaultLabelID(int controlId) const
229 {
230   if (controlId == CONTROL_NO_BUTTON)
231     return 106;
232   else if (controlId == CONTROL_YES_BUTTON)
233     return 107;
234   else if (controlId == CONTROL_CUSTOM_BUTTON)
235     return -1;
236   return CGUIDialogBoxBase::GetDefaultLabelID(controlId);
237 }
238