libaurum: apply smart pointer wider and extract impl out
[platform/core/uifw/aurum.git] / libaurum / src / PartialMatch.cc
1 #include "PartialMatch.h"
2
3 #include <iostream>
4 #include <set>
5 #include <regex>
6
7 #include <loguru.hpp>
8
9 bool PartialMatch::checkCriteria(const std::string *textA, const std::string textB)
10 {
11     if (!textA) return false;
12     std::regex re(*textA);
13     return !std::regex_match(textB, re);
14 }
15
16 bool PartialMatch::checkCriteria(const bool *boolA, const bool boolB)
17 {
18     if (!boolA) return false;
19     return *boolA != boolB;
20 }
21
22 void PartialMatch::debugPrint()
23 {
24     if (mSelector->mPkg)
25         LOG_F(INFO, "selector->pkg :%s", mSelector->mPkg->c_str());
26     if (mSelector->mRes)
27         LOG_F(INFO, "selector->pkg :%s", mSelector->mRes->c_str());
28     if (mSelector->mText)
29         LOG_F(INFO, "selector->pkg :%s", mSelector->mText->c_str());
30     if (mSelector->mType)
31         LOG_F(INFO, "selector->pkg :%s", mSelector->mType->c_str());
32     if (mSelector->mStyle)
33         LOG_F(INFO, "selector->pkg :%s", mSelector->mStyle->c_str());
34 }
35
36 bool PartialMatch::checkCriteria(const std::shared_ptr<UiSelector> selector,
37                                  const std::shared_ptr<AccessibleNode> node)
38 {
39     if(checkCriteria(selector->mPkg.get(), node->getPkg())) return false;
40     if(checkCriteria(selector->mRes.get(), node->getRes())) return false;
41     if(checkCriteria(selector->mText.get(), node->getText())) return false;
42     if(checkCriteria(selector->mType.get(), node->getType())) return false;
43     if(checkCriteria(selector->mStyle.get(), node->getStyle())) return false;
44     if(checkCriteria(selector->mStyle.get(), node->getStyle())) return false;
45
46     if(checkCriteria(selector->mIschecked.get(), node->isChecked())) return false;
47     if(checkCriteria(selector->mIscheckable.get(), node->isCheckable())) return false;
48     if(checkCriteria(selector->mIsclickable.get(), node->isClickable())) return false;
49     if(checkCriteria(selector->mIsenabled.get(), node->isEnabled())) return false;
50     if(checkCriteria(selector->mIsfocused.get(), node->isFocused())) return false;
51     if(checkCriteria(selector->mIsfocusable.get(), node->isFocusable())) return false;
52     if(checkCriteria(selector->mIsscrollable.get(), node->isScrollable())) return false;
53     if(checkCriteria(selector->mIsselected.get(), node->isSelected())) return false;
54     if(checkCriteria(selector->mIsshowing.get(), node->isShowing())) return false;
55     if(checkCriteria(selector->mIsactive.get(), node->isActive())) return false;
56
57     return true;
58 }
59
60 PartialMatch::PartialMatch() : mSelector{nullptr}, mDepth{-1}, mPartialMatches{}
61 {
62 }
63
64 PartialMatch::PartialMatch(const std::shared_ptr<UiSelector> selector, const int absDepth)
65     : mSelector{selector}, mDepth{absDepth}, mPartialMatches{}
66 {
67 }
68
69 std::shared_ptr<PartialMatch> PartialMatch::accept(const std::shared_ptr<AccessibleNode> node,
70                                                    const std::shared_ptr<UiSelector> selector,
71                                                    int index, int depth)
72 {
73     return PartialMatch::accept(node, selector, index, depth, depth);
74 }
75
76 std::shared_ptr<PartialMatch> PartialMatch::accept(const std::shared_ptr<AccessibleNode> node,
77                                                    const std::shared_ptr<UiSelector> selector,
78                                                    int index, int absoluteDepth,
79                                                    int relativeDepth)
80 {
81     LOG_SCOPE_F(INFO, "accept checking i:%d a:%d r:%d / %d < %d < %d", index, absoluteDepth, relativeDepth, selector->mMinDepth?*(selector->mMinDepth):-1, relativeDepth, selector->mMaxDepth?*(selector->mMaxDepth):9999999);
82     PartialMatch *match = nullptr;
83
84     if ((selector->mMinDepth && (relativeDepth < *(selector->mMinDepth))) ||
85         (selector->mMaxDepth && (relativeDepth > *(selector->mMaxDepth)))) {
86         LOG_F(INFO, "depth limit overflow %d < %d < %d", selector->mMinDepth?*(selector->mMinDepth):-1, relativeDepth, selector->mMaxDepth?*(selector->mMaxDepth):9999999);
87         return std::shared_ptr<PartialMatch>(nullptr);
88     }
89
90     if (PartialMatch::checkCriteria(selector, node)) {
91         LOG_F(INFO, "New Match found %p %d", selector, absoluteDepth);
92         match = new PartialMatch(selector, absoluteDepth);
93     }
94
95     return std::shared_ptr<PartialMatch>(match);
96 }
97
98 void PartialMatch::update(
99     const std::shared_ptr<AccessibleNode> node, int index, int depth,
100     std::list<std::shared_ptr<PartialMatch>> &partialMatches)
101 {
102     for (auto &childSelector : mSelector->mChild) {
103         auto match = PartialMatch::accept(node, childSelector, index, depth,
104                                           depth - mDepth);
105         if (match) {
106             mPartialMatches.push_back(match);
107             partialMatches.push_front(match);
108         }
109     }
110 }
111
112 bool PartialMatch::finalizeMatch()
113 {
114     std::set<std::shared_ptr<UiSelector>> matches;
115     for (auto &match : mPartialMatches) {
116         if (match->finalizeMatch()) {
117             matches.insert(match->mSelector);
118         }
119     }
120
121     for (auto &sel : mSelector->mChild) {
122         if (!matches.count(sel)) return false;
123     }
124     return true;
125 }