SecretService 0.13.2d47dfe
SecretService is base back end library for your c++ Qt projects.
secretservice.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2024-2024 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8#include "secretservice.h"
9#include <QASecret.h>
10#include <SecretDB.h>
11#include <QASecret/keystorage.h>
12
13SecretService::SecretService(int argc, char **argv):
14 Patronum::Service<QCoreApplication>(argc, argv) {
15
16}
17
19 // call on server started
21
22 return true;
23}
24
26 // call on server stoped
27 QASecret::KeyStorage::deinitService();
28
29}
30
31void SecretService::handleReceiveData(const QHash<QString, Patronum::Feature> &data) {
32 auto storage = QASecret::KeyStorage::instance();
33
34 if (!storage) {
35 sendResuylt(QuasarAppUtils::Locales::tr("Service is stoped. Please use the start command."));
36 return;
37 }
38
39 bool fAdd = data.contains("add");
40 bool fRemove = data.contains("remove");
41 bool fGet = data.contains("get");
42
43 int actionsCount = fAdd + fRemove + fGet;
44 if (actionsCount > 1) {
45 sendResuylt(QuasarAppUtils::Locales::tr("You are try to run multiple commands. please choose some of pleas choose one."));
46 return;
47 } else if (data.isEmpty()) {
48 sendResuylt(QuasarAppUtils::Locales::tr("please choose one of available actions add/remove/get"));
49 return;
50 };
51
52 if (fAdd) {
53 const auto&& dataVal = data.value("data").arg();
54 const auto&& aliasVal = data.value("alias").arg();
55
56 if (dataVal.isEmpty()) {
57 sendResuylt(QuasarAppUtils::Locales::tr("You forget a data. please use the next command add -data yourDataString"));
58 return;
59 }
60
61 sendRawResuylt(storage->add(dataVal.toLatin1(), aliasVal));
62
63 } else if ( fGet) {
64
65 const auto&& hashVal = data.value("hash").arg();
66 const auto&& aliasVal = data.value("alias").arg();
67
68 if (hashVal.isEmpty() && aliasVal.isEmpty()) {
69 sendResuylt(QuasarAppUtils::Locales::tr("You forget a hash key of alias of getting data. "
70 "Please use the next command get -hash yourHash or "
71 "get -alias yourAlias"));
72 return;
73 }
74
75 if (hashVal.size()) {
76 sendRawResuylt(storage->get(hashVal.toLatin1()));
77 } else if (aliasVal.size()) {
78 sendRawResuylt(storage->get(aliasVal));
79 }
80
81 } else if (fRemove) {
82
83 const auto&& hashVal = data.value("hash").arg();
84 const auto&& aliasVal = data.value("alias").arg();
85
86 if (hashVal.isEmpty() && aliasVal.isEmpty()) {
87 sendResuylt(QuasarAppUtils::Locales::tr("You forget a hash key of alias of removable data. "
88 "Please use the next command remove -hash yourHash or "
89 "remove -alias yourAlias"));
90 return;
91 }
92
93 if (hashVal.size()) {
94 storage->remove(hashVal.toLatin1());
95 } else if (aliasVal.size()) {
96 storage->remove(aliasVal);
97 }
98
99 } else {
100 Patronum::Service<QCoreApplication>::handleReceiveData(data);
101 }
102}
103
104bool SecretService::handleReceive(const Patronum::Feature &data) {
105
106 if (data.cmd() == "ping") {
107 sendResuylt("Pong");
108 } else if (data.cmd() == "state") {
109 auto storage = QASecret::KeyStorage::instance();
110
111 if (storage) {
112 sendResuylt("application status: Active");
113 } else {
114 sendResuylt("application status: Stoped");
115 }
116 }
117
118 return true;
119}
120
121QSet<Patronum::Feature> SecretService::supportedFeatures() {
122 QSet<Patronum::Feature> data;
123
124 data << Patronum::Feature("ping", {}, QuasarAppUtils::Locales::tr("This is description of the ping command"));
125 data << Patronum::Feature("state", {}, QuasarAppUtils::Locales::tr("Returns state"));
126 data << Patronum::Feature("add",
127 {},
128 QuasarAppUtils::Locales::tr("Adds new value into seret storage, and return a hash key after adding."),
129 "QASecret add -data mysecretString -alias myCustomKey ");
130 data << Patronum::Feature("remove",
131 {},
132 QuasarAppUtils::Locales::tr("Remove data from storage by hash key or alias."),
133 "QASecret remove -hash hashKey or QASecret remove -alias myCustomKey");
134 data << Patronum::Feature("get",
135 {},
136 QuasarAppUtils::Locales::tr("return secret from database"),
137 "QASecret get -hash hashKey or QASecret get -alias myCustomKey ");
138 data << Patronum::Feature("-alias", {"aliasName"}, QuasarAppUtils::Locales::tr("Alias name"));
139 data << Patronum::Feature("-data", {"data"}, QuasarAppUtils::Locales::tr("Data that will be saved"));
140 data << Patronum::Feature("-hash", {"hash"}, QuasarAppUtils::Locales::tr("Hash kay of data"));
141
142 return data;
143}
void handleReceiveData(const QHash< QString, Patronum::Feature > &data) override
SecretService(int argc, char **argv)
void onStop() override
bool onStart() override
QSet< Patronum::Feature > supportedFeatures() override
bool handleReceive(const Patronum::Feature &data) override
bool init()
init main initialize method of The SecretService library
Definition QASecret.cpp:15