3bac4382e3d30aa88ba05f280142fc368f8bc3bf
[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->mDesc)
31         LOG_F(INFO, "selector->pkg :%s", mSelector->mDesc->c_str());
32     if (mSelector->mType)
33         LOG_F(INFO, "selector->pkg :%s", mSelector->mType->c_str());
34     if (mSelector->mStyle)
35         LOG_F(INFO, "selector->pkg :%s", mSelector->mStyle->c_str());
36 }
37
38 bool PartialMatch::checkCriteria(const std::shared_ptr<UiSelector> selector,
39                                  const AccessibleNode *node)
40 {
41     if(checkCriteria(selector->mPkg.get(), node->getPkg())) return false;
42     if(checkCriteria(selector->mRes.get(), node->getRes())) return false;
43     if(checkCriteria(selector->mText.get(), node->getText())) return false;
44     if(checkCriteria(selector->mDesc.get(), node->getDesc())) return false;
45     if(checkCriteria(selector->mType.get(), node->getType())) return false;
46     if(checkCriteria(selector->mStyle.get(), node->getStyle())) return false;
47     if(checkCriteria(selector->mStyle.get(), node->getStyle())) return false;
48
49     if(checkCriteria(selector->mIschecked.get(), node->isChecked())) return false;
50     if(checkCriteria(selector->mIscheckable.get(), node->isCheckable())) return false;
51     if(checkCriteria(selector->mIsclickable.get(), node->isClickable())) return false;
52     if(checkCriteria(selector->mIsenabled.get(), node->isEnabled())) return false;
53     if(checkCriteria(selector->mIsfocused.get(), node->isFocused())) return false;
54     if(checkCriteria(selector->mIsfocusable.get(), node->isFocusable())) return false;
55     if(checkCriteria(selector->mIsscrollable.get(), node->isScrollable())) return false;
56     if(checkCriteria(selector->mIsselected.get(), node->isSelected())) return false;
57     if(checkCriteria(selector->mIsshowing.get(), node->isShowing())) return false;
58     if(checkCriteria(selector->mIsactive.get(), node->isActive())) return false;
59
60     return true;
61 }
62
63 PartialMatch::PartialMatch() : mSelector{nullptr}, mDepth{-1}, mPartialMatches{}
64 {
65 }
66
67 PartialMatch::PartialMatch(const std::shared_ptr<UiSelector> selector, const int absDepth)
68     : mSelector{selector}, mDepth{absDepth}, mPartialMatches{}
69 {
70 }
71
72 std::shared_ptr<PartialMatch> PartialMatch::accept(const AccessibleNode *node,
73                                                    const std::shared_ptr<UiSelector> selector,
74                                                    int index, int depth)
75 {
76     return PartialMatch::accept(node, selector, index, depth, depth);
77 }
78
79 std::shared_ptr<PartialMatch> PartialMatch::accept(const AccessibleNode *node,
80                                                    const std::shared_ptr<UiSelector> selector,
81                                                    int index, int absoluteDepth,
82                                                    int relativeDepth)
83 {
84     PartialMatch *match = nullptr;
85
86     if (PartialMatch::checkCriteria(selector, node)) {
87         LOG_SCOPE_F(INFO, "New Match found %p %d", selector, absoluteDepth);
88         match = new PartialMatch(selector, absoluteDepth);
89     }
90
91     return std::shared_ptr<PartialMatch>(match);
92 }
93
94 void PartialMatch::update(
95     const AccessibleNode *node, int index, int depth,
96     std::list<std::shared_ptr<PartialMatch>> &partialMatches)
97 {
98     for (auto childSelector : mSelector->mChild) {
99         auto match = PartialMatch::accept(node, childSelector, index, depth,
100                                           depth - mDepth);
101         if (match) {
102             mPartialMatches.push_back(match);
103             partialMatches.push_front(match);
104         }
105     }
106 }
107
108 bool PartialMatch::finalizeMatch()
109 {
110     std::set<std::shared_ptr<UiSelector>> matches;
111     for (auto match : mPartialMatches) {
112         if (match->finalizeMatch()) {
113             matches.insert(match->mSelector);
114         }
115     }
116
117     for (auto sel : mSelector->mChild) {
118         if (!matches.count(sel)) return false;
119     }
120     return true;
121 }