Skip to content

Commit

Permalink
增加envtools逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
MingYueRuYa committed Nov 11, 2024
1 parent 6b9d018 commit ba48e50
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 39 deletions.
89 changes: 72 additions & 17 deletions env_tools/EnvHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,27 @@
EnvHelper::EnvHelper()
{
this->_init_env();
this->_convert_2_map();
}

EnvHelper::~EnvHelper()
{
}

std::pair<wstring, wstring> EnvHelper::find(const wstring& name) const
{
auto ifind = this->_map_env.find(name);
if (ifind == this->_map_env.end())
{
return {};
}

return { ifind->first, ifind->second };
}

std::map<wstring, wstring> EnvHelper::get_env() const
{
std::map<wstring, wstring> result = {};
for (const wstring& item : _vec_env) {
if (item.empty()) {
continue;
}
if (item[0] == '=') {
continue;
}
std::vector<wstring> values = _split(item, L'=');
if (values.empty())
{
continue;
}
result[values[0]] = values[1];
}
return result;
return this->_map_env;
}

bool EnvHelper::_init_env()
Expand All @@ -58,8 +54,25 @@ bool EnvHelper::_init_env()
return true;
}

void EnvHelper::_convert_2_map()
{
for (const wstring& item : _vec_env) {
if (item.empty()) {
continue;
}
if (item[0] == '=') {
continue;
}
std::vector<wstring> values = split(item, L'=');
if (values.empty())
{
continue;
}
_map_env[values[0]] = values[1];
}
}

std::vector<wstring> EnvHelper::_split(const wstring& str, wchar_t delimiter) const {
std::vector<wstring> EnvHelper::split(const wstring& str, wchar_t delimiter) {
std::vector<wstring> tokens;
size_t start = 0;
size_t end = str.find(delimiter);
Expand All @@ -74,3 +87,45 @@ std::vector<wstring> EnvHelper::_split(const wstring& str, wchar_t delimiter) co
tokens.push_back(str.substr(start));
return tokens;
}

bool EnvHelper::set_key(LPCWSTR varName, LPCWSTR varValue) {
HKEY hKey;
LONG result;

// 打开注册表项
result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _reg_path.c_str(), 0, KEY_SET_VALUE, &hKey);
if (result != ERROR_SUCCESS) {
return false;
}

// 设置环境变量
result = RegSetValueEx(hKey,
varName,
0, REG_SZ, reinterpret_cast<const BYTE*>(varValue), (wcslen(varValue) + 1) * sizeof(wchar_t));
RegCloseKey(hKey);
if (result != ERROR_SUCCESS) {
return false;
}

// 通知系统环境变量已经更改
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, reinterpret_cast<LPARAM>(L"Environment"), SMTO_ABORTIFHUNG, 5000, nullptr);
return true;
}

bool EnvHelper::del_key(const std::wstring& valueName) {
HKEY hKey;
LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _reg_path.c_str(), 0, KEY_SET_VALUE, &hKey);

if (result != ERROR_SUCCESS) {
return false;
}

result = RegDeleteValue(hKey, valueName.c_str());
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
return false;
}

RegCloseKey(hKey);
return true;
}
13 changes: 10 additions & 3 deletions env_tools/EnvHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
#include <map>
#include <string>
#include <vector>
#include <windows.h>

using std::wstring;
using std::vector;

class EnvHelper
{
public:
static std::vector<std::wstring> split(const std::wstring& str, wchar_t delimiter);

public:
EnvHelper();
~EnvHelper();

EnvHelper& operator=(const EnvHelper& left) = delete;
EnvHelper(const EnvHelper& left) = delete;
std::map<wstring, wstring> get_env() const;
std::pair<wstring, wstring> find(const wstring& name) const;
bool set_key(LPCWSTR varName, LPCWSTR varValue);
bool del_key(const std::wstring& valueName);

private:
bool _init_env();
std::vector<std::wstring> _split(const std::wstring& str, wchar_t delimiter) const;
void _convert_2_map();

private:
vector<wstring> _vec_env;


std::map<wstring, wstring> _map_env;
const wstring _reg_path = LR"(SYSTEM\CurrentControlSet\Control\Session Manager\Environment)";
};

49 changes: 49 additions & 0 deletions env_tools/EnvTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ EnvTools::EnvTools(QWidget* parent)
{
ui.setupUi(this);
this->_init_ui();
this->_init_signal();
}

EnvTools::~EnvTools()
Expand All @@ -23,3 +24,51 @@ void EnvTools::_init_ui()
idx++;
}
}

void EnvTools::_init_signal()
{
connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(DoCellClicked(int, int)));
}

void EnvTools::DoCellClicked(int row, int column)
{
ui.listWidget->clear();
this->_clear_list();
QString name = ui.tableWidget->item(row, 0)->text();
auto target_value = _env_helper.find(name.toStdWString());
if (target_value.first == L"")
{
return;
}
else
{
ui.label_key->setText(QString::fromStdWString(target_value.first));
wstring value = target_value.second;
std::vector<wstring> vec_v = EnvHelper::split(value, L';');
for (auto& item : vec_v) {
this->ui.listWidget->addItem(QString::fromStdWString(item));
}
}
}

void EnvTools::on_btn_add_all_clicked()
{

}

void EnvTools::on_btn_del_all_clicked()
{

}

void EnvTools::on_btn_add_sub_clicked()
{

}

void EnvTools::on_btn_del_sub_clicked()
{

}


10 changes: 10 additions & 0 deletions env_tools/EnvTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class EnvTools : public QWidget

private:
void _init_ui();
void _init_signal();
void _clear_list();


protected slots:
void DoCellClicked(int row, int column);
void on_btn_add_all_clicked();
void on_btn_del_all_clicked();
void on_btn_add_sub_clicked();
void on_btn_del_sub_clicked();

private:
Ui::EnvToolsClass ui;
Expand Down
58 changes: 45 additions & 13 deletions env_tools/EnvTools.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,29 @@
</property>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout_5">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Other:</string>
<string>ALL:</string>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableWidget">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="textElideMode">
<enum>Qt::ElideMiddle</enum>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
Expand Down Expand Up @@ -79,14 +88,14 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="btn_add_all">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<widget class="QPushButton" name="btn_delete_all">
<property name="text">
<string>Del</string>
</property>
Expand All @@ -97,21 +106,37 @@
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PATH evn:</string>
<string>Key:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listWidget"/>
<widget class="QLabel" name="label_key">
<property name="styleSheet">
<string notr="true">color: rgb(255, 0, 0);</string>
</property>
<property name="text">
<string>Path</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
Expand All @@ -128,14 +153,14 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<widget class="QPushButton" name="btn_add_sub">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<widget class="QPushButton" name="btn_del_sub">
<property name="text">
<string>Del</string>
</property>
Expand Down Expand Up @@ -170,9 +195,16 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="btn_save">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_restart_explorer">
<property name="text">
<string>重启explorer</string>
<string>restart explorer</string>
</property>
</widget>
</item>
Expand Down
2 changes: 1 addition & 1 deletion env_tools/evn_tools.sln → env_tools/env_tools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32602.291
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "evn_tools", "evn_tools.vcxproj", "{9E8A8CE2-EC9C-4041-8C4A-4D17ED31A1DE}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "env_tools", "env_tools.vcxproj", "{9E8A8CE2-EC9C-4041-8C4A-4D17ED31A1DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 6 additions & 5 deletions env_tools/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "EnvTools.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
EnvTools w;
w.show();
return a.exec();
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
EnvTools w;
w.show();
return a.exec();
}

0 comments on commit ba48e50

Please sign in to comment.