libaurum: Fix indentation and clean code up
[platform/core/uifw/aurum.git] / libaurum / src / Comparer.cc
1 #include "Aurum.h"
2
3 Comparer::Comparer(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
4                    const bool &earlyReturn)
5     : mDevice(device), mSelector(selector), mEarlyReturn(earlyReturn)
6 {
7 }
8
9 Comparer::~Comparer() {}
10
11 std::shared_ptr<AccessibleNode> Comparer::findObject(const std::shared_ptr<UiDevice> device,
12                                      const std::shared_ptr<UiSelector> selector,
13                                      const std::shared_ptr<AccessibleNode> root)
14 {
15     std::vector<std::shared_ptr<AccessibleNode>> ret = findObjects(device, selector, root, true);
16     if (ret.size() > 0)
17         return std::move(ret[0]);
18     else
19         return nullptr;
20 }
21
22 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<UiDevice> device,
23                                                     const std::shared_ptr<UiSelector> selector,
24                                                     const std::shared_ptr<AccessibleNode> root, bool earlyReturn)
25 {
26     Comparer comparer(device, selector, earlyReturn);
27
28     LOGI("findObjects selector(%s) from (type:%s style:%s, role:%s, text:%s) earlyReturn:%d", selector->description().c_str(), root->getType().c_str(),  root->getStyle().c_str(),  root->getRole().c_str(),  root->getText().c_str(), earlyReturn);
29
30     if (selector->mParent) {
31         auto ret = Comparer::findObjects(device, selector->mParent, root);
32         std::vector<std::shared_ptr<AccessibleNode>> merged{};
33
34         for (const auto &node : ret) {
35             auto tmp = comparer.findObjects(node);
36             std::move(std::begin(tmp), std::end(tmp), std::back_inserter(merged));
37         }
38         return merged;
39     }
40
41     return comparer.findObjects(root);
42 }
43
44 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<AccessibleNode> root)
45 {
46     std::list<std::shared_ptr<PartialMatch>> partialList{};
47     std::vector<std::shared_ptr<AccessibleNode>> ret = findObjects(root, 0, 0, partialList);
48     LOGI("%d object(s) found", (int)ret.size());
49     return ret;
50 }
51
52 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(
53     const std::shared_ptr<AccessibleNode> root, const int &index, const int &depth,
54     std::list<std::shared_ptr<PartialMatch>> &partialMatches)
55 {
56     std::vector<std::shared_ptr<AccessibleNode>> ret;
57
58     root->refresh();
59     for (auto &match : partialMatches)
60         match->update(root, index, depth, partialMatches);
61
62     std::shared_ptr<PartialMatch> currentMatch =
63         PartialMatch::accept(root, mSelector, index, depth);
64     if (currentMatch) partialMatches.push_front(currentMatch);
65
66     if (!(mSelector->mMaxDepth && (depth+1 > *(mSelector->mMaxDepth)))) {
67         int childCnt = root->getChildCount();
68         for (int i = 0; i < childCnt; i++) {
69             std::shared_ptr<AccessibleNode> childNode = root->getChildAt(i);
70             if (childNode == nullptr) continue;
71
72             std::vector<std::shared_ptr<AccessibleNode>> childret =
73                 findObjects(childNode, i, depth + 1, partialMatches);
74             std::move(std::begin(childret), std::end(childret), std::back_inserter(ret));
75
76             if (!ret.empty() && mEarlyReturn) {
77                 LOGI("Object found and earlyReturn");
78                 return ret;
79             }
80         }
81     } else {
82         LOGI("Abort searching! No need to search children(maxDepth limit overflow, %d < %d < %d)", mSelector->mMinDepth? * (mSelector->mMinDepth): -1, depth, mSelector->mMaxDepth?*(mSelector->mMaxDepth):9999999);
83     }
84
85     if (currentMatch && currentMatch->finalizeMatch()){
86         LOGI("Found matched = %s with criteria %s", root->description().c_str(), currentMatch->debugPrint().c_str());
87         ret.push_back(root);
88     }
89
90     return ret;
91 }