Commit 24da657fc77a4a64ce562749e133269369f68559

Authored by Eric Menezes Noronha
1 parent fb73ff95
Exists in master

Criado metodo para gravar dados em registro (tambem em linux). Installcacic acei…

…tando o parametro uninstall. Criada classe coleta para melhor organização no gercols.
cacic-teste/cacic-teste.pro
... ... @@ -21,8 +21,7 @@ SOURCES += \
21 21 testcacic.cpp \
22 22 ../src/ccacic.cpp \
23 23 ../src/cacic_computer.cpp \
24   - ../src/operatingsystem.cpp \
25   - ../gercols/gercols.cpp
  24 + ../src/operatingsystem.cpp
26 25  
27 26  
28 27 HEADERS += \
... ... @@ -31,8 +30,7 @@ HEADERS += \
31 30 ../src/cacic_computer.h \
32 31 ../src/cacic_comm.h \
33 32 ../src/console.h \
34   - ../src/operatingsystem.h \
35   - ../gercols/gercols.cpp
  33 + ../src/operatingsystem.h
36 34  
37 35 INCLUDEPATH += ../src \
38 36 ../src/crypto++/include/
... ...
cacic-teste/testcacic.cpp
... ... @@ -209,24 +209,20 @@ void CTestCacic::testStartService()
209 209 void CTestCacic::testReadConfig()
210 210 {
211 211  
212   - // Inicializa um arquivo de configuração stub
213   - // que seria parecido com o recebido do Gerente
214   - QJsonObject configJson;
215   - QJsonObject configHardware;
216   - QJsonObject configSoftware;
  212 +// // Inicializa um arquivo de configuração stub
  213 +// // que seria parecido com o recebido do Gerente
  214 +// QJsonObject configJson;
  215 +// QJsonObject configHardware;
217 216  
218   - configHardware["network_interface"] = QJsonValue::fromVariant(QString(""));
219   - configSoftware["operating_system"] = QJsonValue::fromVariant(QString(""));
  217 +// configHardware["computador"] = OCacicComp.toJsonObject();
220 218  
221   - configJson["hardware"] = configHardware;
222   - configJson["software"] = configSoftware;
  219 +// configJson["hardware"] = configHardware;
223 220  
224   - OCacic.setJsonToFile(configJson,"configRequest.json");
  221 +// OCacic.setJsonToFile(configJson,"configRequest.json");
225 222  
226 223 // Leitura do arquivo de configuração
227   - OGercols.readConfig();
228 224  
229   - QVERIFY(OGercols.getConfigJson() == configJson);
  225 + QVERIFY(false);
230 226 }
231 227  
232 228 void CTestCacic::cleanupTestCase()
... ...
cacic-teste/testcacic.h
... ... @@ -27,7 +27,6 @@ private:
27 27 CacicComm OCacicComm;
28 28 CCacic OCacic;
29 29 CACIC_Computer OCacicComp;
30   - Gercols OGercols;
31 30 QString testPath;
32 31 QString testIniPath;
33 32 QJsonObject session;
... ...
gercols/gercols.cpp
1 1 #include "gercols.h"
2 2  
3   -Gercols::Gercols()
  3 +Gercols::Gercols(QObject *parent)
4 4 {
  5 + /*Aqui vamos conectar o slot principal. Acho que algo do tipo:
  6 + * sinal iniciaColeta() chama o slot configuraColetas() que verifica
  7 + * quais coletas serao necessárias e realiza a conexão dos sinais com
  8 + * slots de coletas (coletaHardware, coletaSoftware, etc..)
  9 + */
5 10 }
6 11  
7   -void Gercols::readConfig()
  12 +void Gercols::run()
8 13 {
  14 + QJsonObject coleta;
  15 + /* Pega configurações do Json de configurações localizado
  16 + * na pasta principal do cacic (deverá ser pega do registro,
  17 + * estou tentando implementar isso no installcacic).
  18 + */
9 19  
10   -}
  20 + //Inicializa as classes e seta valores necessários oCacic.setCacicMainFolder(), por exemplo.
11 21  
12   -QJsonObject Gercols::getConfigJson()
13   -{
14   - return QJsonObject();
  22 + //emite sinal para começar a coleta
  23 +
  24 + //aguarda todas as coletas serem realizadas
  25 +
  26 + //salva coleta em json
  27 + coleta["computador"] = oColeta.getOComputer().toJsonObject();
  28 + coleta["software"] = QJsonValue::fromVariant(QString("Aqui vai coleta de software"));
  29 + coleta["hardware"] = QJsonValue::fromVariant(QString("Aqui vai coleta de hardware"));
  30 + qDebug() << coleta;
  31 + //salva json em arquivo
  32 + oCacic.setJsonToFile(coleta, "coleta.json");
  33 + //emite sinal "finished" pra finalizar a aplicação
  34 + emit finished();
15 35 }
... ...
gercols/gercols.h
1 1 #ifndef GERCOLS_H
2 2 #define GERCOLS_H
  3 +#include <QtCore>
  4 +#include <ccoleta.h>
  5 +#include <ccacic.h>
3 6  
4   -#include <QJsonObject>
5   -
6   -class Gercols
  7 +class Gercols : public QObject
7 8 {
  9 + Q_OBJECT
  10 +private:
  11 + CCacic oCacic;
  12 + CColeta oColeta;
8 13 public:
9   - Gercols();
  14 + explicit Gercols(QObject *parent = 0);
  15 +
  16 +public slots:
  17 + void run();
10 18  
11   - void readConfig();
12   - QJsonObject getConfigJson();
  19 +signals:
  20 + void finished();
  21 + void fimColeta();
13 22 };
14 23  
15 24 #endif // GERCOLS_H
... ...
gercols/gercols.pro
... ... @@ -25,6 +25,7 @@ SOURCES += \
25 25 ../src/ccacic.cpp \
26 26 ../src/cacic_computer.cpp \
27 27 ../src/operatingsystem.cpp \
  28 + ../src/ccoleta.cpp \
28 29 ./gercols.cpp
29 30  
30 31  
... ... @@ -33,6 +34,7 @@ HEADERS += \
33 34 ../src/cacic_computer.h \
34 35 ../src/operatingsystem.h \
35 36 ../src/console.h \
  37 + ../src/ccoleta.h \
36 38 ./gercols.h
37 39  
38 40 INCLUDEPATH += ../src \
... ...
gercols/main.cpp
1 1 #include <QCoreApplication>
  2 +#include <gercols.h>
2 3  
3 4 int main(int argc, char *argv[])
4 5 {
5 6 QCoreApplication a(argc, argv);
6   -
  7 + //crio o objeto que será responsável pela execução
  8 + Gercols *oGercols = new Gercols(&a);
  9 + //conecto o sinal "finished" do objeto oGercols ao slot "quit" do objeto a(QCoreApplication)
  10 + //ou seja, quando o finished for chamado, a aplicacao é encerrada.
  11 + a.connect(oGercols, SIGNAL(finished()), &a, SLOT(quit()));
  12 + //invoco o metodo "run" do objeto oGercols assim que o loop (a.exec()) comecar
  13 + QMetaObject::invokeMethod(oGercols, "run", Qt::QueuedConnection);
7 14 return a.exec();
8 15 }
... ...
install-cacic/install-cacic.pro.user
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE QtCreatorProject>
3   -<!-- Written by QtCreator 3.1.2, 2014-08-15T13:57:58. -->
  3 +<!-- Written by QtCreator 3.1.2, 2014-08-15T17:35:14. -->
4 4 <qtcreator>
5 5 <data>
6 6 <variable>ProjectExplorer.Project.ActiveTarget</variable>
... ...
install-cacic/installcacic.cpp
... ... @@ -9,7 +9,7 @@ InstallCacic::InstallCacic(QObject *parent) :
9 9 void InstallCacic::run(QStringList argv, int argc) {
10 10 bool ok;
11 11 //valida os parametros repassados
12   - validaParametros(argv, argc, &ok);
  12 + QMap<QString, QString> param = validaParametros(argv, argc, &ok);
13 13 //se tiver usuario, senha e url
14 14 if (ok){
15 15 oCacicComm.setUrlGerente(this->argumentos["host"]);
... ... @@ -21,13 +21,11 @@ void InstallCacic::run(QStringList argv, int argc) {
21 21 oCacic.setChaveCrypt(jsonLogin["reply"].toObject()["chavecrip"].toString());
22 22 oCacic.setCacicMainFolder("c:/cacic");
23 23 oCacic.createFolder(oCacic.getCacicMainFolder());
24   - //grava chave em arquivo json;
25   - QJsonObject configJson = oCacic.getJsonFromFile(oCacic.getCacicMainFolder() + "/cacicTeste.json");
26   - QJsonObject configToSave = configJson["configs"].toObject();
27   - configToSave["chaveCrypt"] = QJsonValue::fromVariant(oCacic.getChaveCrypt());
28   - configJson["configs"] = configToSave;
29   - oCacic.setJsonToFile(configJson, oCacic.getCacicMainFolder() + "/cacicTeste.json");
  24 + //grava chave em registro;
30 25  
  26 + QVariantMap registro;
  27 + registro["key"] = oCacic.getChaveCrypt();
  28 + oCacic.setValueToRegistry("Lightbase", "Cacic", registro);
31 29 //starta o processo do cacic.
32 30 #ifdef Q_OS_WIN
33 31 QString exitStatus = oCacic.startProcess("cacic.exe", true, &ok);
... ... @@ -39,10 +37,13 @@ void InstallCacic::run(QStringList argv, int argc) {
39 37 if (!ok)
40 38 qDebug() << "Erro ao iniciar o processo.";
41 39 #endif
42   - }
43   - else
  40 + } else
44 41 std::cout << "Nao foi possivel realizar o login.\n "
45 42 << jsonLogin["error"].toString().toStdString();
  43 + } else if ((param.contains("default")) && (param["default"] == "uninstall")){
  44 + oCacic.deleteFolder("c:/cacic");
  45 + oCacic.removeRegistry("Lightbase", "Cacic");
  46 +
46 47 } else {
47 48 std::cout << "\nInstalador do Agente Cacic.\n\n"
48 49 << "Parametros incorretos. (<obrigatorios> [opcional])\n\n"
... ... @@ -64,6 +65,8 @@ QMap&lt;QString, QString&gt; InstallCacic::validaParametros(QStringList argv, int argc
64 65 QStringList auxList = aux.split("=");
65 66 if ((auxList.at(0).at(0) == '-') && (auxList.size() > 1))
66 67 map[auxList.at(0).mid(1)] = auxList.at(1);
  68 + else if (aux.at(0)== '-')
  69 + map["default"] = aux.mid(1);
67 70 }
68 71 *ok = (bool) map.contains("host") && map.contains("user") && map.contains("password");
69 72 if (*ok){
... ...
install-cacic/installcacic.h
... ... @@ -19,6 +19,7 @@ public:
19 19 QMap<QString, QString> validaParametros(QStringList argv, int argc, bool *ok);
20 20 bool startProcess(QString pathprogram, bool wait, QStringList arguments);
21 21  
  22 + void registro();
22 23 private:
23 24 QMap<QString, QString> argumentos;
24 25 CacicComm oCacicComm;
... ...
src/cacic_computer.cpp
... ... @@ -20,11 +20,9 @@ QList&lt;QVariantMap&gt; CACIC_Computer::networkInterfacesRunning(){
20 20  
21 21 foreach (QNetworkInterface in, interface.allInterfaces()) {
22 22 if (!(bool)(in.flags() & QNetworkInterface::IsLoopBack) &&
23   - !(bool)(in.flags() & QNetworkInterface::IsPointToPoint) &&
24   - (bool)(in.flags() & QNetworkInterface::IsRunning)){
25   - // qDebug() << in.humanReadableName() << "\n";
  23 + !(bool)(in.flags() & QNetworkInterface::IsPointToPoint) &&
  24 + (bool)(in.flags() & QNetworkInterface::IsRunning)){
26 25 mapInterface["nome"] = in.humanReadableName();
27   - // qDebug() << in.hardwareAddress() << "\n";
28 26 mapInterface["mac"] = in.hardwareAddress();
29 27 foreach (QNetworkAddressEntry ae, in.addressEntries()){
30 28 if (ae.ip().scopeId() == ""){
... ... @@ -34,18 +32,11 @@ QList&lt;QVariantMap&gt; CACIC_Computer::networkInterfacesRunning(){
34 32 mapInterface["ipv6"] = ae.ip().toString();
35 33 mapInterface["netmask_ipv6"] = ae.netmask().toString();
36 34 }
37   - // qDebug() << ae.ip().toString() << "\n";
38 35 }
39 36 listaMap.append(mapInterface);
40 37 mapInterface.clear();
41 38 }
42 39 }
43   -// foreach (QVariantMap each, listaMap){
44   -
45   -// foreach (QVariant eachValue, each.values())
46   -// qDebug() << eachValue.toString();
47   -// }
48   -
49 40 return listaMap;
50 41 }
51 42  
... ... @@ -61,7 +52,6 @@ QJsonObject CACIC_Computer::toJsonObject()
61 52 count++;
62 53 }
63 54 json["networkDevices"] = network;
64   -// qDebug() << json;
65 55 return json;
66 56 }
67 57  
... ...
src/ccacic.cpp
... ... @@ -204,6 +204,21 @@ QString CCacic::startProcess(QString pathprogram, bool wait, bool *ok, QStringLi
204 204 return process.errorString();
205 205 }
206 206  
  207 +void CCacic::setValueToRegistry(QString organization, QString application, QVariantMap values)
  208 +{
  209 + QSettings registry(organization, application);
  210 + for (QVariantMap::const_iterator i = values.constBegin(); i != values.constEnd(); i++)
  211 + registry.setValue(i.key(), i.value());
  212 + registry.sync();
  213 +}
  214 +
  215 +void CCacic::removeRegistry(QString organization, QString application)
  216 +{
  217 + QSettings registry(organization, application);
  218 + registry.clear();
  219 + registry.sync();
  220 +}
  221 +
207 222 /*Getters/Setters
208 223 * Begin:
209 224 */
... ...
src/ccacic.h
... ... @@ -36,6 +36,9 @@ public:
36 36 bool setJsonToFile(QJsonObject json, QString filepath);
37 37 QJsonObject getJsonFromFile(QString filepath);
38 38 QString startProcess(QString pathprogram, bool wait, bool *ok, QStringList arguments = QStringList());
  39 + void setValueToRegistry(QString organization, QString application, QVariantMap values);
  40 + void removeRegistry(QString organization, QString application);
  41 +
39 42  
40 43 //Geters/seters:
41 44  
... ...
src/ccoleta.cpp
... ... @@ -3,3 +3,9 @@
3 3 CColeta::CColeta()
4 4 {
5 5 }
  6 +
  7 +
  8 +CACIC_Computer CColeta::getOComputer() const
  9 +{
  10 + return oComputer;
  11 +}
... ...
src/ccoleta.h
1 1 #ifndef CCOLETA_H
2 2 #define CCOLETA_H
  3 +#include <cacic_computer.h>
3 4  
4 5 class CColeta
5 6 {
  7 +private:
  8 + CACIC_Computer oComputer;
6 9 public:
7 10 CColeta();
  11 + CACIC_Computer getOComputer() const;
8 12 };
9 13  
10 14 #endif // CCOLETA_H
... ...
src/operatingsystem.cpp
... ... @@ -2,8 +2,8 @@
2 2  
3 3 OperatingSystem::OperatingSystem()
4 4 {
5   - this->idOs = this->coletaIdOs();
6 5 this->nomeOs = this->coletaNomeOs();
  6 + this->idOs = this->coletaIdOs();
7 7 }
8 8  
9 9  
... ... @@ -47,6 +47,7 @@ QString OperatingSystem::coletaNomeOs()
47 47 QString text;
48 48 QStringList environment = QProcessEnvironment::systemEnvironment().toStringList();
49 49 foreach (text, environment) {
  50 +// qDebug() << text;
50 51 if (text.contains("OS=" , Qt::CaseInsensitive) ||
51 52 text.contains("SESSION=", Qt::CaseInsensitive) ){
52 53 QStringList split = text.split("=");
... ...