Rabbit Remote Control 0.0.36
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 std::string pw = "RabbitRemoteControl";
55 QCryptographicHash sum(QCryptographicHash::Md5);
56
57#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
58 if(!password.empty())
59 sum.addData(password.c_str(), password.length());
60 sum.addData(pw.c_str(), pw.length());
61 if(!key.empty())
62 sum.addData(key.c_str(), key.length());
63#else
64 if(!password.empty())
65 sum.addData(QByteArrayView(password.c_str(), password.length()));
66 sum.addData(QByteArrayView(pw.c_str(), pw.length()));
67 if(!key.empty())
68 sum.addData(QByteArrayView(key.c_str(), key.length()));
69#endif
70 return sum.result();
71}
72
73int CParameterConnecter::LoadPassword(const QString &szTitle,
74 const QString &szKey,
75 QString &password,
76 QSettings &set)
77{
78 QByteArray sum = set.value(szKey + "_sum").toByteArray();
79 QByteArray pwByte = set.value(szKey).toByteArray();
80 if(pwByte.isEmpty())
81 return 0;
82 RabbitCommon::CEncrypt e;
83
84 std::string key;
86 key = GetParameterClient()->GetEncryptKey().toStdString().c_str();
87 if(!key.empty())
88 e.SetPassword(key.c_str());
89
90 if(!e.Dencode(pwByte, password)
91 && PasswordSum(password.toStdString(), key) == sum)
92 return 0;
93
94 qDebug(log) << "Password don't dencode or sum is error";
95 CDlgInputPassword d(GetParameterClient()->GetViewPassowrd(), szTitle);
96 if(QDialog::Accepted != d.exec())
97 {
98 return -1;
99 }
100
101 CDlgInputPassword::InputType t;
102 int nRet = d.GetValue(t, password);
103 if(nRet) return nRet;
104 if(CDlgInputPassword::InputType::Password == t)
105 return 0;
106 GetParameterClient()->SetEncryptKey(password);
107 return LoadPassword(szTitle, szKey, password, set);
108}
109
110int CParameterConnecter::SavePassword(const QString &szKey,
111 const QString &password,
112 QSettings &set, bool bSave)
113{
114 if(!bSave)
115 {
116 set.remove(szKey);
117 set.remove(szKey + "_sum");
118 return 0;
119 }
120
121 QByteArray encryptPassword;
122 RabbitCommon::CEncrypt e;
123 std::string key = GetParameterClient()->GetEncryptKey().toStdString();
124 if(key.empty())
125 {
126 switch (GetParameterClient()->GetPromptType()) {
127 case CParameterClient::PromptType::First:
128 {
129 int nCount = GetParameterClient()->GetPromptCount();
130 if(nCount >= 1)
131 break;
132 GetParameterClient()->SetPromptCount(nCount + 1);
133 }
134 case CParameterClient::PromptType::Always:
135 {
136 QString szKey;
137 CDlgInputPassword::InputType t = CDlgInputPassword::InputType::Encrypt;
138 CDlgInputPassword dlg(GetParameterClient()->GetViewPassowrd());
139 if(QDialog::Accepted == dlg.exec())
140 dlg.GetValue(t, szKey);
141 if(CDlgInputPassword::InputType::Encrypt == t)
142 GetParameterClient()->SetEncryptKey(szKey);
143 break;
144 }
145 case CParameterClient::PromptType::No:
146 break;
147 }
148 } else
149 e.SetPassword(key.c_str());
150 if(password.isEmpty())
151 return 0;
152 e.Encode(password, encryptPassword);
153 set.setValue(szKey, encryptPassword);
154 set.setValue(szKey + "_sum", PasswordSum(password.toStdString(), key));
155 return 0;
156}
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