Rabbit Remote Control 0.0.37
Loading...
Searching...
No Matches
ParameterOperate.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 "ParameterOperate.h"
11
12static Q_LOGGING_CATEGORY(log, "Parameter.Operate")
13
14CParameterOperate::CParameterOperate(QObject *parent, const QString &szPrefix)
15 : CParameter(parent, szPrefix)
16 , m_Parent(nullptr)
17 , m_pParameterPlugin(nullptr)
18{
19 bool check = false;
20 check = connect(this, SIGNAL(sigSetGlobalParameters()),
21 this, SLOT(slotSetGlobalParameters()));
22 Q_ASSERT(check);
23 CParameterOperate* p = qobject_cast<CParameterOperate*>(parent);
24 if(p) {
25 m_Parent = p;
26 check = connect(m_Parent, SIGNAL(sigSetGlobalParameters()),
27 this, SIGNAL(sigSetGlobalParameters()));
28 Q_ASSERT(check);
29 }
30}
31
37
38int CParameterOperate::SetGlobalParameters(CParameterPlugin *p)
39{
40 if(m_pParameterPlugin == p) return 0;
43 return 0;
44}
45
50
51QByteArray CParameterOperate::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 CParameterOperate::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 = GetGlobalParameters()->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(GetGlobalParameters()->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 GetGlobalParameters()->SetEncryptKey(password);
107 return LoadPassword(szTitle, szKey, password, set);
108}
109
110int CParameterOperate::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 = GetGlobalParameters()->GetEncryptKey().toStdString();
124 if(key.empty())
125 {
126 switch (GetGlobalParameters()->GetPromptType()) {
127 case CParameterPlugin::PromptType::First:
128 {
129 int nCount = GetGlobalParameters()->GetPromptCount();
130 if(nCount >= 1)
131 break;
132 GetGlobalParameters()->SetPromptCount(nCount + 1);
133 }
134 case CParameterPlugin::PromptType::Always:
135 {
136 QString szKey;
137 CDlgInputPassword::InputType t = CDlgInputPassword::InputType::Encrypt;
138 CDlgInputPassword dlg(GetGlobalParameters()->GetViewPassowrd());
139 if(QDialog::Accepted == dlg.exec())
140 dlg.GetValue(t, szKey);
141 if(CDlgInputPassword::InputType::Encrypt == t)
142 GetGlobalParameters()->SetEncryptKey(szKey);
143 break;
144 }
145 case CParameterPlugin::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}
Operational parameter interface.
void sigSetGlobalParameters()
Only used by this class.
virtual void slotSetGlobalParameters()
Call after set CParameterPlugin.
CParameterPlugin * GetGlobalParameters()
Get CParameterPlugin.
CParameterPlugin * m_pParameterPlugin
Global parameters of plugins.
Parameter interface.
Definition Parameter.h:209