Skip to content

Commit

Permalink
TgBot++: clientapp: windows: Fix blockingop and sendfile
Browse files Browse the repository at this point in the history
- SendFile's filetype field was being used without being assigned in client, fixed it
- allocmem now ZeroMemory the buffer
- Fix BlockingOperation blocking ui even if no operation is present
  • Loading branch information
Royna2544 committed Jun 4, 2024
1 parent 6144381 commit 6ad0d77
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
6 changes: 3 additions & 3 deletions clientapp/windows/TgBotClient/BlockingOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "UIComponents.h"

bool BlockingOperation::shouldBlockRequest(HWND hDlg) const {
bool _BlockingOperation::shouldBlockRequest(HWND hDlg) const {
if (m_enabled) {
auto &loader = StringLoader::getInstance();
MessageBox(hDlg, loader.getString(IDS_OPERATION_ACTIVE).c_str(),
Expand All @@ -16,12 +16,12 @@ bool BlockingOperation::shouldBlockRequest(HWND hDlg) const {
return false;
}

void BlockingOperation::start() {
void _BlockingOperation::start() {
SetCursor(LoadCursor(NULL, IDC_WAIT));
m_enabled = true;
}

void BlockingOperation::stop() {
void _BlockingOperation::stop() {
SetCursor(LoadCursor(NULL, IDC_ARROW));
m_enabled = false;
}
34 changes: 32 additions & 2 deletions clientapp/windows/TgBotClient/BlockingOperation.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <wtypes.h>
#include <Windows.h>
#include <optional>

class BlockingOperation {
class _BlockingOperation {
public:
bool shouldBlockRequest(HWND hDlg) const;
void start();
Expand All @@ -11,3 +12,32 @@ class BlockingOperation {
private:
bool m_enabled;
};

struct BlockingOperation {
std::optional<_BlockingOperation> _opt;

void init() {
if (_opt) {
_opt.reset();
}
}

bool shouldBlockRequest(HWND hDlg) const {
if (_opt) {
return _opt->shouldBlockRequest(hDlg);
}
return false;
}

void start() {
if (!_opt) {
_opt.emplace();
}
_opt->start();
}
void stop() {
if (_opt) {
_opt->stop();
}
}
};
1 change: 1 addition & 0 deletions clientapp/windows/TgBotClient/DownloadFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ INT_PTR CALLBACK DownloadFileFn(HWND hDlg, UINT message, WPARAM wParam,

// Focus to only one input section
SetFocus(hRemoteFilepath);
blk.init();
return DIALOG_OK;

case WM_COMMAND:
Expand Down
4 changes: 4 additions & 0 deletions clientapp/windows/TgBotClient/SendFileToChat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ INT_PTR CALLBACK SendFileToChat(HWND hDlg, UINT message, WPARAM wParam,

// Focus to only one input section
SetFocus(hChatId);

blk.init();
return DIALOG_OK;

case WM_COMMAND:
Expand Down Expand Up @@ -138,11 +140,13 @@ INT_PTR CALLBACK SendFileToChat(HWND hDlg, UINT message, WPARAM wParam,

if (in) {
blk.start();

in->chatid = chatid;
in->dialog = hDlg;
strncpy(in->filePath, pathbuf.data(),
MAX_PATH_SIZE - 1);
in->filePath[MAX_PATH_SIZE - 1] = 0;
in->fileType = type;
_beginthreadex(NULL, 0, SendFileTask, in, 0, NULL);
} else {
errtext = _T("Failed to allocate memory");
Expand Down
1 change: 1 addition & 0 deletions clientapp/windows/TgBotClient/SendMessageToChat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ INT_PTR CALLBACK SendMsgToChat(HWND hDlg, UINT message, WPARAM wParam,
case WM_INITDIALOG:
hChatId = GetDlgItem(hDlg, IDC_CHATID);
hMsgText = GetDlgItem(hDlg, IDC_MESSAGETXT);
blk.init();
return DIALOG_OK;

case WM_COMMAND:
Expand Down
Binary file modified clientapp/windows/TgBotClient/TgBotClient.rc
Binary file not shown.
6 changes: 5 additions & 1 deletion clientapp/windows/TgBotClient/UIComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ inline std::size_t Length(wchar_t* str) {

template <typename T>
T* allocMem() {
return static_cast<T*>(malloc(sizeof(T)));
T * mem = static_cast<T*>(malloc(sizeof(T)));
if (mem) {
ZeroMemory(mem, sizeof(T));
}
return mem;
}

inline std::wstring charToWstring(const char* charArray) {
Expand Down
4 changes: 3 additions & 1 deletion clientapp/windows/TgBotClient/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#define IDM_WEBSITE 32779
#define ID_MENU_DESTIP 32784
#define ID_MENU_DOWNLOADFILE 32785
#define ID_32786 32786
#define IDC_MENU_DESTIP 32787
#define IDC_STATIC -1

// Next default values for new objects
Expand All @@ -58,7 +60,7 @@
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 133
#define _APS_NEXT_COMMAND_VALUE 32786
#define _APS_NEXT_COMMAND_VALUE 32788
#define _APS_NEXT_CONTROL_VALUE 1020
#define _APS_NEXT_SYMED_VALUE 110
#endif
Expand Down

0 comments on commit 6ad0d77

Please sign in to comment.