Skip to content

Commit

Permalink
Add Button: refresh access toekn
Browse files Browse the repository at this point in the history
  • Loading branch information
numbaa committed Aug 18, 2024
1 parent 9cd0d8b commit bd2240d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
11 changes: 8 additions & 3 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ int App::exec(int argc, char** argv) {
params.enable_run_as_service = std::bind(&App::enableRunAsDaemon, this, std::placeholders::_1);
params.get_history_device_ids = std::bind(&App::getHistoryDeviceIDs, this);
params.get_last_access_token = std::bind(&App::getLastAccessToken, this);
params.refresh_access_token = std::bind(&App::refreshAccessToken, this);
params.clear_last_access_token = std::bind(&App::clearLastAccessToken, this);
params.get_settings = std::bind(&App::getSettings, this);
params.on_user_confirmed_connection = std::bind(&App::onUserConfirmedConnection, this,
Expand Down Expand Up @@ -598,9 +599,7 @@ void App::maybeRefreshAccessToken() {
if (!auto_refresh_access_token_) {
return;
}
access_token_ = generateAccessToken();
settings_->setString("access_token", access_token_);
gui_.setAccessToken(access_token_);
maybeRefreshAccessToken();
}

std::pair<std::string, std::string> App::getLastAccessToken() {
Expand All @@ -622,6 +621,12 @@ std::pair<std::string, std::string> App::getLastAccessToken() {
return {device_id, access_token};
}

void App::refreshAccessToken() {
access_token_ = generateAccessToken();
settings_->setString("access_token", access_token_);
gui_.setAccessToken(access_token_);
}

void App::saveLastAccessToken(const std::string& device_id, const std::string& _access_token) {
// 在本地存储验证码,无论如何都是一件愚蠢的事,只能想办法让它'看上去没那么蠢'
std::string access_token = _access_token;
Expand Down
1 change: 1 addition & 0 deletions src/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class App {
void insertNewestHistoryID(const std::string& device_id);
void maybeRefreshAccessToken();
std::pair<std::string, std::string> getLastAccessToken();
void refreshAccessToken();
void saveLastAccessToken(const std::string& device_id, const std::string& access_token);
void clearLastAccessToken();
void postTask(const std::function<void()>& task);
Expand Down
1 change: 1 addition & 0 deletions src/app/views/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class GUI {
std::function<std::vector<std::string>()> get_history_device_ids;
std::function<std::pair<std::string, std::string>()> get_last_access_token;
std::function<void()> clear_last_access_token;
std::function<void()> refresh_access_token;
std::function<Settings()> get_settings;
std::function<void(bool)> enable_auto_refresh_access_token;
std::function<void(bool)> enable_share_clipboard;
Expand Down
14 changes: 14 additions & 0 deletions src/app/views/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <qmenu.h>
#include <qmimedata.h>
#include <qstringbuilder.h>
#include <qthread.h>

#include <ltlib/logging.h>
#include <ltlib/strings.h>
Expand All @@ -72,6 +73,10 @@
namespace {

void dispatchToUiThread(std::function<void()> callback) {
if (qApp->thread() == QThread::currentThread()) {
callback();
return;
}
// any thread
QTimer* timer = new QTimer();
timer->moveToThread(qApp->thread());
Expand Down Expand Up @@ -521,6 +526,7 @@ void MainWindow::setupOtherCallbacks() {
connect(ui->btnClose, &QPushButton::clicked, [this]() { hide(); });
connect(ui->btnCopy, &QPushButton::pressed, this, &MainWindow::onCopyPressed);
connect(ui->btnShowToken, &QPushButton::pressed, this, &MainWindow::onShowTokenPressed);
connect(ui->btnRefreshToken, &QPushButton::clicked, this, &MainWindow::onRefreshTokenClicked);
connect(ui->btnConnect, &QPushButton::clicked, this, &MainWindow::onConnectBtnClicked);
connect(ui->checkboxService, &QCheckBox::stateChanged,
[this](int) { params_.enable_run_as_service(ui->checkboxService->isChecked()); });
Expand Down Expand Up @@ -969,6 +975,14 @@ void MainWindow::onShowTokenPressed() {
}
}

void MainWindow::onRefreshTokenClicked() {
params_.refresh_access_token();
token_showing_ = true;
token_last_show_time_ms_ = ltlib::steady_now_ms();
ui->labelMyAccessToken->setText(QString::fromStdString(access_token_text_));
QTimer::singleShot(5'100, std::bind(&MainWindow::onTimeoutHideToken, this));
}

void MainWindow::onCopyPressed() {
auto clipboard = QApplication::clipboard();
QString device_id = ui->labelMyDeviceID->text();
Expand Down
2 changes: 2 additions & 0 deletions src/app/views/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class MainWindow : public QMainWindow {

void onShowTokenPressed();

void onRefreshTokenClicked();

void onCopyPressed();

void onUpdateIndicator();
Expand Down
56 changes: 29 additions & 27 deletions src/app/views/mainwindow/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ border-bottom: 3px solid rgb(44, 49, 58);
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -286,7 +286,7 @@ background-color:transparent;
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/cil-description.png</normaloff>:/res/icons/cil-description.png</iconset>
</property>
<property name="iconSize">
Expand All @@ -306,7 +306,7 @@ background-color:transparent;
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/icon_minimize.png</normaloff>:/res/icons/icon_minimize.png</iconset>
</property>
<property name="iconSize">
Expand All @@ -326,7 +326,7 @@ background-color:transparent;
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/icon_close.png</normaloff>:/res/icons/icon_close.png</iconset>
</property>
<property name="iconSize">
Expand Down Expand Up @@ -804,7 +804,7 @@ color: rgb(190, 190, 190);
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/cil-clone.png</normaloff>:/res/icons/cil-clone.png</iconset>
</property>
</widget>
Expand Down Expand Up @@ -890,26 +890,27 @@ color: rgb(190, 190, 190);
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/cil-low-vision.png</normaloff>:/res/icons/cil-low-vision.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<widget class="QPushButton" name="btnRefreshToken">
<property name="maximumSize">
<size>
<width>60</width>
<height>20</height>
<height>16777215</height>
</size>
</property>
</spacer>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/cil-reload.png</normaloff>:/res/icons/cil-reload.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
Expand Down Expand Up @@ -1052,7 +1053,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="icon">
<iconset resource="../../../resources.qrc">
<iconset resource="../../resources.qrc">
<normaloff>:/res/icons/cil-link.png</normaloff>:/res/icons/cil-link.png</iconset>
</property>
</widget>
Expand Down Expand Up @@ -1137,7 +1138,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -1186,7 +1187,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/gamepad_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/gamepad_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -1214,7 +1215,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/mouse_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/mouse_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand All @@ -1236,7 +1237,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/keyboard_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/keyboard_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -1311,7 +1312,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/pc2.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -1360,7 +1361,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/gamepad_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/gamepad_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand All @@ -1382,7 +1383,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/mouse_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/mouse_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand All @@ -1404,7 +1405,7 @@ QPushButton#btnConnect:pressed {
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../resources.qrc">:/res/png_icons/keyboard_gray.png</pixmap>
<pixmap resource="../../resources.qrc">:/res/png_icons/keyboard_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
Expand Down Expand Up @@ -1614,7 +1615,7 @@ QHeaderView::section:vertical
<rect>
<x>0</x>
<y>0</y>
<width>527</width>
<width>459</width>
<height>780</height>
</rect>
</property>
Expand Down Expand Up @@ -2307,7 +2308,8 @@ QHeaderView::section:vertical
</widget>
</widget>
<resources>
<include location="../../../resources.qrc"/>
<include location="../../resources.qrc"/>
<include location="../../resources.qrc"/>
<include location="../../../resources.qrc"/>
</resources>
<connections/>
Expand Down

0 comments on commit bd2240d

Please sign in to comment.