Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ParameterConnecter.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QLoggingCategory>
4#include <QCryptographicHash>
5#include <QInputDialog>
6
7#include "RabbitCommonEncrypt.h"
8#include "RabbitCommonTools.h"
9#include "DlgInputPassword.h"
10#include "ParameterConnecter.h"
11
12static Q_LOGGING_CATEGORY(log, "Client.Parameter.Connecter")
13
14CParameterConnecter::CParameterConnecter(QObject *parent, const QString &szPrefix)
15 : CParameter(parent, szPrefix),
16 m_Parent(nullptr),
17 m_pParameterClient(nullptr)
18{
19 bool check = false;
20 check = connect(this, SIGNAL(sigSetParameterClient()),
21 this, SLOT(slotSetParameterClient()));
22 Q_ASSERT(check);
23 CParameterConnecter* p = qobject_cast<CParameterConnecter*>(parent);
24 if(p) {
25 m_Parent = p;
26 check = connect(m_Parent, SIGNAL(sigSetParameterClient()),
27 this, SIGNAL(sigSetParameterClient()));
28 Q_ASSERT(check);
29 }
30}
31
37
38int CParameterConnecter::SetParameterClient(CParameterClient *p)
39{
40 if(m_pParameterClient == p) return 0;
42 emit sigSetParameterClient();
43 return 0;
44}
45
50
51QByteArray CParameterConnecter::PasswordSum(const std::string &password,
52 const std::string &key)
53{
54 QCryptographicHash sum(QCryptographicHash::Md5);
55 if(!password.empty())
56 sum.addData(password.c_str(), password.length());
57 std::string pw = "RabbitRemoteControl";
58 sum.addData(pw.c_str(), pw.length());
59 if(!key.empty())
60 sum.addData(key.c_str(), key.length());
61 return sum.result();
62}
63
64int CParameterConnecter::LoadPassword(const QString &szTitle,
65 const QString &szKey,
66 QString &password,
67 QSettings &set)
68{
69 QByteArray sum = set.value(szKey + "_sum").toByteArray();
70 QByteArray pwByte = set.value(szKey).toByteArray();
71 if(pwByte.isEmpty())
72 return 0;
73 RabbitCommon::CEncrypt e;
74
75 std::string key;
77 key = GetParameterClient()->GetEncryptKey().toStdString().c_str();
78 if(!key.empty())
79 e.SetPassword(key.c_str());
80
81 if(!e.Dencode(pwByte, password)
82 && PasswordSum(password.toStdString(), key) == sum)
83 return 0;
84
85 qDebug(log) << "Password don't dencode or sum is error";
86 CDlgInputPassword d(GetParameterClient()->GetViewPassowrd(), szTitle);
87 if(QDialog::Accepted != d.exec())
88 {
89 return -1;
90 }
91
92 CDlgInputPassword::InputType t;
93 int nRet = d.GetValue(t, password);
94 if(nRet) return nRet;
95 if(CDlgInputPassword::InputType::Password == t)
96 return 0;
97 GetParameterClient()->SetEncryptKey(password);
98 return LoadPassword(szTitle, szKey, password, set);
99}
100
101int CParameterConnecter::SavePassword(const QString &szKey,
102 const QString &password,
103 QSettings &set, bool bSave)
104{
105 if(!bSave)
106 {
107 set.remove(szKey);
108 set.remove(szKey + "_sum");
109 return 0;
110 }
111
112 QByteArray encryptPassword;
113 RabbitCommon::CEncrypt e;
114 std::string key = GetParameterClient()->GetEncryptKey().toStdString();
115 if(key.empty())
116 {
117 switch (GetParameterClient()->GetPromptType()) {
118 case CParameterClient::PromptType::First:
119 {
120 int nCount = GetParameterClient()->GetPromptCount();
121 if(nCount >= 1)
122 break;
123 GetParameterClient()->SetPromptCount(nCount + 1);
124 }
125 case CParameterClient::PromptType::Always:
126 {
127 QString szKey;
128 CDlgInputPassword::InputType t = CDlgInputPassword::InputType::Encrypt;
129 CDlgInputPassword dlg(GetParameterClient()->GetViewPassowrd());
130 if(QDialog::Accepted == dlg.exec())
131 dlg.GetValue(t, szKey);
132 if(CDlgInputPassword::InputType::Encrypt == t)
133 GetParameterClient()->SetEncryptKey(szKey);
134 break;
135 }
136 case CParameterClient::PromptType::No:
137 break;
138 }
139 } else
140 e.SetPassword(key.c_str());
141 if(password.isEmpty())
142 return 0;
143 e.Encode(password, encryptPassword);
144 set.setValue(szKey, encryptPassword);
145 set.setValue(szKey + "_sum", PasswordSum(password.toStdString(), key));
146 return 0;
147}
The parameters of client.
the parameters of connecter interface.
CParameterClient * GetParameterClient()
Get CParameterClient.
virtual void slotSetParameterClient()
Call after set CParameterClient.
CParameterClient * m_pParameterClient
Parameter interface.
Definition Parameter.h:169