-
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathmainwindow.h
More file actions
516 lines (347 loc) · 13 KB
/
Copy pathmainwindow.h
File metadata and controls
516 lines (347 loc) · 13 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#pragma once
#include <QMainWindow>
#include <include/global/HTTPRequestHelper.hpp>
#ifndef Q_MOC_RUN
#include <core/server/gen/libcore.pb.h>
#endif
#include "include/global/Configs.hpp"
#include "include/stats/connections/connectionLister.hpp"
#include "3rdparty/qv2ray/v2/ui/widgets/speedchart/SpeedWidget.hpp"
#include "include/database/entities/Profile.h"
#ifdef Q_OS_LINUX
#include <QtDBus>
#endif
#ifndef MW_INTERFACE
#include <optional>
#include <QKeyEvent>
#include <QSystemTrayIcon>
#include <QPointer>
#include <QTimer>
#include <QElapsedTimer>
#include <QQueue>
#include <QWaitCondition>
#include <QProcess>
#include <QTextDocument>
#include <QShortcut>
#include <QKeySequence>
#include <QSet>
#include <QHash>
#include <QCheckBox>
#include <QSemaphore>
#include <QMutex>
#include <QThreadPool>
#include <QLocalServer>
#include <QLocalSocket>
#include "group/GroupSort.hpp"
#include "include/global/GuiUtils.hpp"
#include "include/ui/setting/Icon.hpp"
#include "include/ui/utils/DataViewHtmlGenerator.h"
#include "include/ui/utils/ProfilesFilterProxyModel.h"
#include "include/ui/utils/ProfilesTableModel.h"
#include "ui_mainwindow.h"
#endif
namespace Configs_sys {
class CoreProcess;
}
class TrayProfileSelector;
class TrayOtpCodes;
class TestRunner;
class DialogVpnAuth;
struct VpnAuthChallenge;
namespace Qv2ray::ui { class SyntaxHighlighter; }
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
enum class RefreshAnchor {
KeepPlace,
Removal,
};
enum class ExitReason {
None,
RunUpdater,
Restart,
RestartWithTun,
RestartWithDns,
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
qint64 GetCorePid();
QString GetRunningConfigName();
// Blocking RPC; never call from the UI thread.
static QString liveVpnConnectOkText();
// Blocking RPC; never call from the UI thread. Empty unless the exit hop is an endpoint.
static QString liveVpnStateText(bool *connected = nullptr);
void prepare_exit();
void refresh_proxy_list(const QList<int> &ids = {}, bool mayNeedReset = false,
RefreshAnchor anchor = RefreshAnchor::KeepPlace);
void show_group(int gid);
void refresh_groups();
void refresh_status(const QString &traffic_update = "");
void update_traffic_graph(int proxyDl, int proxyUp, int directDl, int directUp);
void profile_start(int _id = -1);
void profile_stop(bool crash = false, bool block = false, bool manual = false);
int get_profile_to_start();
void set_spmode_system_proxy(bool enable, bool save = true);
void toggle_system_proxy();
void set_spmode_vpn(bool enable, bool save = true);
bool get_elevated_permissions(ExitReason reason = ExitReason::RestartWithTun);
void start_select_mode(QObject *context, const std::function<void(int)> &callback);
void RegisterHotkey(bool unregister);
bool StopVPNProcess();
void UpdateConnectionList(const QMap<QString, Stats::ConnectionMetadata>& toUpdate, const QMap<QString, Stats::ConnectionMetadata>& toAdd);
void UpdateConnectionListWithRecreate(const QList<Stats::ConnectionMetadata>& connections);
void UpdateDataView(bool force = false);
void refresh_auto_selector_view();
class DialogAutoSelector *m_autoSelectorDialog = nullptr;
void setDownloadReport(const DownloadProgressReport& report, bool show);
signals:
void profile_selected(int id);
public slots:
void on_commitDataRequest();
void on_menu_exit_triggered();
#ifndef MW_INTERFACE
private slots:
void on_masterLogBrowser_customContextMenuRequested(const QPoint &pos);
void on_menu_basic_settings_triggered();
void on_menu_routing_settings_triggered();
void on_menu_vpn_settings_triggered();
void on_menu_preset_settings_triggered();
void on_menu_otp_manager_triggered();
void on_menu_hotkey_settings_triggered();
void on_menu_add_from_input_triggered();
void on_menu_add_from_clipboard_triggered();
void on_menu_clone_triggered();
void on_menu_delete_repeat_triggered();
void on_menu_delete_triggered();
void on_menu_reset_traffic_triggered();
void on_menu_copy_links_triggered();
void on_menu_copy_links_nkr_triggered();
void on_menu_export_config_triggered();
void display_qr_link(bool nkrFormat = false);
void on_menu_scan_qr_triggered();
void on_menu_clear_test_result_triggered();
void on_menu_manage_groups_triggered();
void on_menu_select_all_triggered();
void on_menu_remove_unavailable_triggered();
void on_menu_remove_invalid_triggered();
void on_menu_remove_insecure_triggered();
void on_menu_resolve_selected_triggered();
void on_menu_resolve_domain_triggered();
void on_menu_update_subscription_triggered();
void on_profilesTableView_doubleClicked(const QModelIndex &index);
void on_profilesTableView_customContextMenuRequested(const QPoint &pos);
void on_tabWidget_currentChanged(int index);
void on_tabWidget_customContextMenuRequested(const QPoint& p);
private:
Ui::MainWindow *ui;
QElapsedTimer sinceWindowDeactivated;
ProfilesTableModel *profilesTableModel = nullptr;
ProfilesFilterProxyModel *profilesFilterModel = nullptr;
QSystemTrayIcon *tray;
QMenu *trayMenu = nullptr; // tray context menu
QPointer<TrayProfileSelector> traySelector;
void openTraySelector(bool routing);
QPointer<TrayOtpCodes> trayOtpCodes;
void openTrayOtpCodes();
QShortcut *shortcut_esc = new QShortcut(QKeySequence::Cancel, this);
//
QThreadPool *parallelCoreCallPool = new QThreadPool(this);
std::unique_ptr<TestRunner> testRunner;
//
Configs_sys::CoreProcess *core_process = nullptr;
QMutex coreProcessMutex;
QLocalServer *core_server = nullptr;
bool rpc_started = false;
qint64 vpn_pid = 0;
//
QTextDocument *qvLogDocument = new QTextDocument(this);
//
QString title_error;
std::optional<Icon::TrayIconStatus> icon_status;
std::shared_ptr<Configs::Profile> running;
int last_running_profile_id = -1;
bool m_profileConnecting = false;
bool m_profileDisconnecting = false;
bool m_xrayGeoAssetBusy = false;
QString traffic_update_cache;
qint64 last_test_time = 0;
//
int proxy_last_order = -1;
bool select_mode = false;
QMutex mu_starting;
QMutex mu_stopping;
QMutex mu_exit;
ExitReason exit_reason = ExitReason::None;
//
QMutex mu_download_update;
QMutex mu_download_dashboard;
//
QMutex connectionListMu;
//
int toolTipID;
//
SpeedWidget *speedChartWidget;
//
// for data view
std::atomic<qint64> lastUpdatedMs = QDateTime::currentMSecsSinceEpoch();
DataViewHtmlGenerator dataViewHtmlGenerator_;
// shortcuts
QList<QShortcut*> hiddenMenuShortcuts;
// search
QString addressFilterString;
QString nameFilterString;
QString typeFilterString;
QString countryFilterString;
QTimer *m_filterRefreshDebounce = nullptr;
bool m_profilesTableHadFocus = false;
int m_profilesScrollValue = 0;
// log
QStringList includeKeywords;
QStringList excludeKeywords;
QRegularExpression includeCombined;
QRegularExpression excludeCombined;
QMutex logMutex;
QQueue<QString> logQueue;
QWaitCondition logWaiter;
Qv2ray::ui::SyntaxHighlighter *logHighlighter = nullptr;
QMutex logPendingMutex;
QString logPendingText;
bool logFlushScheduled = false;
struct LogFilter {
bool enableInclude = false;
bool enableExclude = false;
QStringList includeKeywords;
QStringList excludeKeywords;
QRegularExpression includeCombined;
QRegularExpression excludeCombined;
};
void append_log(const QString &log);
void log_process_loop();
// UI thread only.
void flush_log_batch();
bool should_print_log(const QString &log, const LogFilter &filter);
void updateLogFilterFields();
void setLogHighlighter(bool darkMode);
void applyProfileFilters();
QList<int> get_now_selected_list();
void refresh_startstop_button();
QList<int> get_selected_or_group();
void set_system_proxy(bool enable);
void saveProfileFocusState();
void restoreProfileFocusState(RefreshAnchor anchor);
void selectProfileRows(const QList<int> &rows);
void focusProfilesTable(bool selectFirst);
void clearUnavailableProfiles(bool confirm = true, QList<int> profileIDs = {});
void dialog_message_impl(MwMessage cmd, const QStringList &args);
void handle_deeplink_impl(const QString &url);
void handle_addsub(const QString &url, const QString &name);
void handle_import_route(const QString &url);
void handle_add_remote_routes(const QString &url);
void import_or_handle_deeplink(const QString &text);
void refresh_proxy_list_column_size();
void refresh_proxy_list_impl(const QList<int> &ids = {}, bool mayNeedReset = false);
void refresh_proxy_list_impl_refresh_data(const QList<int>& ids = {}, bool mayNeedReset = false);
void parseQrImage(const QPixmap *image);
void importFromFiles(const QStringList &paths);
void trayClickEvent();
void keyPressEvent(QKeyEvent *event) override;
void closeEvent(QCloseEvent *event) override;
void changeEvent(QEvent *event) override;
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void syncConnectionViewState();
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent* event) override;
void applyLogBrowserFont();
void applyTopBarMetrics();
QSize designMinimumSize;
QTimer *m_proxyListRefreshDebounce = nullptr;
void scheduleProxyListRefresh();
bool m_adjustingColumns = false;
//
void HotkeyEvent(const QString &key);
void RegisterHiddenMenuShortcuts(bool unregister = false);
void registerMenuShortcuts(QMenu *menu, QSet<QKeySequence> &claimed);
void collectMenuShortcuts(QMenu *menu, QSet<QKeySequence> &out);
void setActionsData();
QList<QAction*> getActionsForShortcut();
void loadShortcuts();
// rpc
void setup_rpc(QLocalSocket *socket);
bool verify_core_pid(QLocalSocket *socket);
void rank_auto_selector(const std::shared_ptr<Configs::Profile>& ent, const QList<int>& stale = {});
void on_auto_selector_exhausted(int profileID);
void on_subscription_group_changed(int gid, const QList<int>& disturbed);
bool auto_selector_ranked = false;
bool handleXrayGeoAssetError(const QString& error, const QString& contextName);
void url_test_current();
// vpn endpoints
// The hop the "proxy" tag lands on, or null when it is not an endpoint.
static std::shared_ptr<Configs::Profile> vpn_exit_endpoint(const std::shared_ptr<Configs::Profile> &ent);
static QString vpn_state_text(const QString &state, const QString &error);
void start_vpn_challenge_poll();
void stop_vpn_challenge_poll();
void poll_vpn_challenges();
void show_vpn_challenge(const VpnAuthChallenge &challenge);
// No challenge is pending: the core never prompts for config-supplied credentials.
void show_vpn_auth_failure(const QString &endpointTag, const QString &error);
void clear_vpn_credential_overrides();
QTimer *m_vpnChallengeTimer = nullptr;
std::atomic<bool> m_vpnChallengeBusy{false};
QSet<QString> m_vpnChallengeSeen;
QPointer<DialogVpnAuth> m_vpnAuthDialog;
QString m_vpnEndpointState;
// Survives the restart the recovery itself triggers, so a rejected retry cannot loop.
QHash<int, int> m_vpnAuthPrompted;
int m_vpnAuthRestartID = -1;
bool set_system_dns(bool set, bool save_set = true);
void CheckUpdate();
void OpenDashboard();
void SeedDashboard();
void setupConnectionList();
void setupConnectionSortMenu();
friend class TestRunner;
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
#endif // MW_INTERFACE
};
inline MainWindow *GetMainWindow() {
return qobject_cast<MainWindow *>(mainwindow);
}
void UI_InitMainWindow();
#ifdef Q_OS_LINUX
/*
* Proxy class for interface org.freedesktop.portal.Request
*/
class OrgFreedesktopPortalRequestInterface : public QDBusAbstractInterface
{
Q_OBJECT
public:
OrgFreedesktopPortalRequestInterface(const QString& service,
const QString& path,
const QDBusConnection& connection,
QObject* parent = nullptr);
~OrgFreedesktopPortalRequestInterface();
public Q_SLOTS:
inline QDBusPendingReply<> Close()
{
QList<QVariant> argumentList;
return asyncCallWithArgumentList(QStringLiteral("Close"), argumentList);
}
Q_SIGNALS: // SIGNALS
void Response(uint response, QVariantMap results);
};
namespace org {
namespace freedesktop {
namespace portal {
typedef ::OrgFreedesktopPortalRequestInterface Request;
}
}
}
#endif