SMACC2
Loading...
Searching...
No Matches
cb_quick_add.cpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17namespace cl_gcalcli
18{
19
20CbQuickAdd::CbQuickAdd(const std::string & text, const std::string & calendar)
21: text_(text), calendar_(calendar), client_(nullptr)
22{
23}
24
26{
27 RCLCPP_INFO(getLogger(), "[CbQuickAdd] Adding event: %s", text_.c_str());
28
30
31 if (!client_)
32 {
33 RCLCPP_ERROR(getLogger(), "[CbQuickAdd] ClGcalcli client not available");
34 this->postFailureEvent();
35 return;
36 }
37
38 auto * connection = client_->getConnection();
39 if (!connection)
40 {
41 RCLCPP_ERROR(getLogger(), "[CbQuickAdd] CpGcalcliConnection not available");
42 this->postFailureEvent();
43 return;
44 }
45
46 // Build the quick add command
47 std::string args = "quick";
48
49 if (!calendar_.empty())
50 {
51 args += " --calendar \"" + calendar_ + "\"";
52 }
53
54 // Escape the text for shell
55 std::string escaped_text = text_;
56 // Replace double quotes with escaped quotes
57 size_t pos = 0;
58 while ((pos = escaped_text.find('"', pos)) != std::string::npos)
59 {
60 escaped_text.replace(pos, 1, "\\\"");
61 pos += 2;
62 }
63
64 args += " \"" + escaped_text + "\"";
65
66 auto result = connection->executeGcalcli(args, 30000);
67
68 if (result.exit_code == 0 && !result.timed_out)
69 {
70 RCLCPP_INFO(getLogger(), "[CbQuickAdd] Event added successfully");
71 this->postSuccessEvent();
72 }
73 else
74 {
75 RCLCPP_ERROR(getLogger(), "[CbQuickAdd] Failed to add event: %s", result.stdout_output.c_str());
76 this->postFailureEvent();
77 }
78}
79
80} // namespace cl_gcalcli
CbQuickAdd(const std::string &text, const std::string &calendar="")
Construct with event description.
void onEntry() override
CpGcalcliConnection * getConnection()
smacc2::client_core_components::SubprocessResult executeGcalcli(const std::string &args, int timeout_ms=30000)
Execute a gcalcli command.
virtual rclcpp::Logger getLogger() const
void requiresClient(SmaccClientType *&storage)