-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemTrayIcon.cpp
178 lines (155 loc) · 7.04 KB
/
SystemTrayIcon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Created by pikachu on 17-7-20.
//
#include "SystemTrayIcon.h"
#include "EditServerWidget.h"
SystemTrayIcon::SystemTrayIcon(Profile* profile,QObject *parent)
: QSystemTrayIcon(parent),
networkInter("com.deepin.daemon.Network", "/com/deepin/daemon/Network", QDBusConnection::sessionBus(), this) {
this->profile=profile;
setIcon(QIcon(":/icons/shadowsocks.ico"));
menu = new QMenu("menu");
startSystemAgentAction = new QAction("启动系统代理", this);
startSystemAgentAction->setCheckable(true);
menu->addAction(startSystemAgentAction);
systemAgentModeMenu = new QMenu("系统代理模式", menu);
QActionGroup* systemAgentModeActionGroup=new QActionGroup(this);
pacModeAction = new QAction("PAC模式", this);
globelModeAction = new QAction("全局模式", this);
systemAgentModeActionGroup->addAction(pacModeAction);
systemAgentModeActionGroup->addAction(globelModeAction);
pacModeAction->setCheckable(true);
globelModeAction->setCheckable(true);
systemAgentModeMenu->addAction(pacModeAction);
systemAgentModeMenu->addAction(globelModeAction);
menu->addMenu(systemAgentModeMenu);
serverMenu = new QMenu("服务器", menu);
loadBalancingAction = new QAction("负载均衡", this);
highAvailabilityAction = new QAction("高可用", this);
accordingToStatisticsAction = new QAction("根据统计", this);
editServerAction = new QAction("编辑服务器...", this);
statisticsConfigurationAction = new QAction("统计配置...", this);
shareServerConfigurationAction = new QAction("分享服务器配置...", this);
scanThe2DCodeOnTheScreenAction = new QAction("扫描屏幕上的二维码...", this);
importTheURLFromTheClipboardAction = new QAction("从剪贴板导入URL...", this);
serverMenu->addAction(loadBalancingAction);
serverMenu->addAction(highAvailabilityAction);
serverMenu->addAction(accordingToStatisticsAction);
serverMenu->addSeparator();
serverMenu->addAction(editServerAction);
serverMenu->addAction(statisticsConfigurationAction);
serverMenu->addSeparator();
serverMenu->addAction(shareServerConfigurationAction);
serverMenu->addAction(scanThe2DCodeOnTheScreenAction);
serverMenu->addAction(importTheURLFromTheClipboardAction);
menu->addMenu(serverMenu);
pacMenu = new QMenu("PAC", menu);
useLocalPacAction = new QAction("使用本地PAC", this);
useOnlinePacAction = new QAction("使用在线PAC", this);
editLocalPacFileAction = new QAction("编辑本地PAC文件...", this);
updateLocalPacFromGFWListAction = new QAction("从GFWList更新本地PAC", this);
editUserRulesForGFWListAction = new QAction("编辑GFWList的用户规则...", this);
protectLocalPacAction = new QAction("保护本地PAC", this);
copyLocalPacUrlAction = new QAction("复制本地PAC网址", this);
editOnlinePacUrlAction = new QAction("编辑在线PAC网址...", this);
pacMenu->addAction(useLocalPacAction);
pacMenu->addAction(useOnlinePacAction);
pacMenu->addSeparator();
pacMenu->addAction(editLocalPacFileAction);
pacMenu->addAction(updateLocalPacFromGFWListAction);
pacMenu->addAction(editUserRulesForGFWListAction);
pacMenu->addAction(protectLocalPacAction);
pacMenu->addAction(copyLocalPacUrlAction);
pacMenu->addAction(editOnlinePacUrlAction);
menu->addMenu(pacMenu);
positiveAgentAction = new QAction("正向代理...", this);
menu->addAction(positiveAgentAction);
menu->addSeparator();
bootAction = new QAction("开机启动", this);
menu->addAction(bootAction);
allowConnectionsFromTheLANAction = new QAction("允许来自局域网的连接", this);
menu->addAction(allowConnectionsFromTheLANAction);
menu->addSeparator();
editShortcutsAction = new QAction("编辑快捷键...", this);
menu->addAction(editShortcutsAction);
helpMenu = new QMenu("帮助", menu);
showLogAction = new QAction("显示日志", this);
detailedLogAction = new QAction("详细记录日志", this);
updateMenu = new QMenu("更新", helpMenu);
checkForUpdateAction = new QAction("检查更新", this);
updateMenu->addAction(checkForUpdateAction);
updateMenu->addSeparator();
checkForUpdatesAtStartupAction = new QAction("启动时检查更新", this);
checkTheBetaUpdateAction = new QAction("检查测试版更新", this);
updateMenu->addAction(checkForUpdatesAtStartupAction);
updateMenu->addAction(checkTheBetaUpdateAction);
helpMenu->addAction(showLogAction);
helpMenu->addAction(detailedLogAction);
helpMenu->addMenu(updateMenu);
menu->addMenu(helpMenu);
aboutAction = new QAction("关于", this);
menu->addAction(aboutAction);
exitAction = new QAction("退出", this);
menu->addAction(exitAction);
setContextMenu(menu);
startSystemAgentAction->setChecked(false);
globelModeAction->setChecked(true);
systemAgentModeMenu->setEnabled(false);
connect(startSystemAgentAction, &QAction::triggered, [this](bool checked) {
if (!checked) {
qDebug() << "取消系统代理";
systemAgentModeMenu->setEnabled(false);
setProxyMethod("none");
} else {
qDebug() << "启动系统代理";
systemAgentModeMenu->setEnabled(true);
if (pacModeAction->isChecked()){
setAutoProxy();
} else{
setManualProxy();
}
}
});
connect(systemAgentModeActionGroup,&QActionGroup::triggered,[this](QAction *action){
if(action==pacModeAction){
setAutoProxy();
} else{
setManualProxy();
}
});
connect(editServerAction,&QAction::triggered,[this](){
EditServerWidget w;
int ret = w.exec();
});
connect(exitAction, &QAction::triggered, []() {
qApp->exit();
});
}
void SystemTrayIcon::setProxyMethod(QString proxyMethod) {
QDBusPendingCallWatcher *w = new QDBusPendingCallWatcher(networkInter.SetProxyMethod(proxyMethod), this);
QObject::connect(w, &QDBusPendingCallWatcher::finished, [=] {
qDebug()<<"success to set proxy method "<<proxyMethod;
});
connect(w, &QDBusPendingCallWatcher::finished, w, &QDBusPendingCallWatcher::deleteLater);
}
void SystemTrayIcon::setManualProxy() {
setProxyMethod("manual");
QString type="socks";
// QString addr="127.0.0.1";
QString addr=profile->local_address;
// QString port="1080";
QString port=QString::number(profile->local_port);
QDBusPendingCallWatcher *w = new QDBusPendingCallWatcher(networkInter.SetProxy(type, addr, port), this);
QObject::connect(w, &QDBusPendingCallWatcher::finished, [=] {
qDebug()<<"set proxy"<<type<<addr<<port;
});
}
void SystemTrayIcon::setAutoProxy() {
setProxyMethod("auto");
// 目前先写死吧
QString proxy="file:///etc/ss/autoproxy.pac";
QDBusPendingCallWatcher *w = new QDBusPendingCallWatcher(networkInter.SetAutoProxy(proxy), this);
QObject::connect(w, &QDBusPendingCallWatcher::finished, [](){
qDebug()<<"finished";
});
}