Refactoring homescreen.
[profile/ivi/ico-uxf-homescreen.git] / lib / misc / state-machine / CicoHistoryState.cpp
1 /*
2  * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Contact: 
5  *
6  * Licensed under the Apache License, Version 2.0 (the License){}
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License. 
17  */
18
19 #include <iostream>
20 #include <string>
21 #include <vector>
22 #include "CicoStateCore.h"
23 #include "CicoHistoryState.h"
24
25 /**
26  * history state object class
27  * @author m.kamoshida
28  * @version 0.1
29  */
30
31
32 /**
33  * @brief CicoHistoryState::CicoHistoryState
34  * @param parent parent object
35  */
36 CicoHistoryState::CicoHistoryState(CicoState* parent):
37     CicoStateCore(EHistoryState, (CicoStateCore*)parent)
38 {
39 }
40
41 /**
42  * @brief CicoHistoryState::CicoHistoryState
43  * @param name Identification name
44  * @param parent parent object
45  */
46 CicoHistoryState::CicoHistoryState(const std::string& name, CicoState* parent):
47     CicoStateCore(EHistoryState, name, (CicoStateCore*)parent)
48 {
49 }
50
51 /**
52  * @brief CicoHistoryState::CicoHistoryState
53  * @param value Identification number
54  * @param parent parent object
55  */
56 CicoHistoryState::CicoHistoryState(int value, CicoState* parent):
57     CicoStateCore(EHistoryState, value, (CicoStateCore*)parent)
58 {
59 }
60
61 /**
62  * @brief CicoHistoryState::CicoHistoryState
63  * @param name Identification name
64  * @param value Identification number
65  * @param parent parent object
66  */
67 CicoHistoryState::CicoHistoryState(const std::string& name, int value,
68                                    CicoState* parent):
69     CicoStateCore(EHistoryState, name, value, (CicoStateCore*)parent)
70 {
71 }
72
73 /**
74  * @brief CicoHistoryState::CicoHistoryState
75  * @param parent parent object
76  */
77 CicoHistoryState::CicoHistoryState(CicoStateMachine* parent):
78     CicoStateCore(EHistoryState, (CicoStateCore*)parent)
79 {
80 }
81
82 /**
83  * @brief CicoHistoryState::CicoHistoryState
84  * @param name Identification name
85  * @param parent parent object
86  */
87 CicoHistoryState::CicoHistoryState(const std::string& name,
88                                    CicoStateMachine* parent):
89     CicoStateCore(EHistoryState, name, (CicoStateCore*)parent)
90 {
91 }
92
93 /**
94  * @brief CicoHistoryState::CicoHistoryState
95  * @param value Identification number
96  * @param parent parent object
97  */
98 CicoHistoryState::CicoHistoryState(int value, CicoStateMachine* parent):
99     CicoStateCore(EHistoryState, value, (CicoStateCore*)parent)
100 {
101 }
102
103 /**
104  * @brief CicoHistoryState::CicoHistoryState
105  * @param name Identification name
106  * @param value Identification number
107  * @param parent parent object
108  */
109 CicoHistoryState::CicoHistoryState(const std::string& name, int value,
110                                    CicoStateMachine* parent):
111     CicoStateCore(EHistoryState, name, value, (CicoStateCore*)parent)
112 {
113 }
114
115 /**
116  * @brief CicoHistoryState::~CicoHistoryState
117  */
118 CicoHistoryState::~CicoHistoryState()
119 {
120     m_vDState.clear();
121     m_vHState.clear();
122 }
123
124 /**
125  * @brief CicoHistoryState::addDefaultState
126  * @param stts default state object
127  */
128 void CicoHistoryState::addDefaultState(std::vector<CicoState*> stts)
129 {
130     int sz = stts.size();
131     for (int i = 0; i < sz; i++) {
132         m_vDState.push_back(stts[i]);
133     }
134 }
135
136 /**
137  * @brief CicoHistoryState::addDefaultState
138  * @param stts default state object
139  */
140 void CicoHistoryState::addDefaultState(CicoState* stt)
141 {
142         m_vDState.push_back(stt);
143 }
144
145 /**
146  * @brief CicoHistoryState::holdHistory
147  */
148 void CicoHistoryState::holdHistory()
149 {
150     bool bR = false;
151     if ((CicoStateCore*)0 != m_parent) { // exist is parent
152         // Discard the previous history
153         m_vHState.clear();
154         std::vector<const CicoState*> t;
155         // Stores the new history
156         if (Deep == m_historyType) {    // deep history ?
157             /*
158             * Deep history
159             */
160             bR = getCurrentStateF(m_parent, t, CicoStateCore::ELvlBttm);
161         }
162         else {
163             /*
164             * Shallow history
165             */
166             bR = getCurrentStateF(m_parent, t, CicoStateCore::ELvlTop);
167         }
168         if (false == bR) {
169             /*
170             * _D_ERROR
171             */
172         }
173         else {
174             int sz = t.size();
175             for (int i = 0; i < sz; i++) {
176                 m_vHState.push_back((CicoState*)t[i]);
177             }
178         }
179     }
180     else {
181         /*
182         * _D_ERROR
183         */
184     }
185 }
186
187 /**
188  * @brief CicoHistoryState::start
189  * @param ev trigger event data
190  * @return
191  */
192 bool CicoHistoryState::start(const CicoEvent& ev, bool)
193 {
194     toActivate(ev);
195
196     toDeactivate(ev);
197
198     int szH = m_vHState.size();
199     int szD = m_vDState.size();
200     if (0 != szH) {
201         for (int i = 0; i < szH; i++) {
202             CicoStateCore* t = (CicoStateCore*)m_vHState[i];
203             startF(t, ev, false);
204         }
205     }
206     else if (0 != szD) {
207         for (int i = 0; i < szD; i++) {
208             CicoStateCore* t =(CicoStateCore*)m_vDState[i];
209             startF(t, ev, true);
210         }
211     }
212     else {
213         /*
214         * _D_ERROR
215         */
216         startF(m_parent, ev, true);
217     }
218     return true;
219 }
220
221 /**
222  * @brief getCurrentStateF
223  * @param stt get top 
224  * @param states store active state objects area
225  * @param policy get poicy
226  * @return true:get success false:get fail
227  */
228 bool getCurrentStateF(CicoStateCore* stt,
229                       std::vector<const CicoState*>& states,
230                       CicoStateCore::E_GetPolicy policy)
231 {
232     return stt->getCurrentState(states, policy);
233 }
234
235 /**
236  * @brief CicoHistoryState::setParent
237  * @param parent parent object
238  */
239 void CicoHistoryState::setParent(CicoState* parent)
240 {
241     CicoStateCore::setParent((CicoStateCore*)parent);
242 }
243
244 /**
245  * @brief CicoHistoryState::setParent
246  * @param parent parent object
247  */
248 void CicoHistoryState::setParent(CicoStateMachine* parent)
249 {
250     CicoStateCore::setParent((CicoStateCore*)parent);
251 }