forked from OpenGamers/abaddon
Compare commits
222 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67e924e538 | ||
|
|
c4590f8b23 | ||
|
|
dff93e103a | ||
|
|
02583b8512 | ||
|
|
4740965f4c | ||
|
|
6ff2563e36 | ||
|
|
afaba05293 | ||
|
|
929ebf1a60 | ||
|
|
38c5230a1d | ||
|
|
e2784cd97b | ||
|
|
0471688732 | ||
|
|
f97a6ff266 | ||
|
|
28c3ec417f | ||
|
|
f8f9a907c9 | ||
|
|
cb690b6def | ||
|
|
f751037717 | ||
|
|
64245bf745 | ||
|
|
772598996c | ||
|
|
e888306272 | ||
|
|
848e75f577 | ||
|
|
e2110c22ee | ||
|
|
cf53831b2a | ||
|
|
88f2e63eeb | ||
|
|
ccb82c1676 | ||
|
|
5a3bce7498 | ||
|
|
621beb1344 | ||
|
|
cd900cdfee | ||
|
|
17e7478bb4 | ||
|
|
78a5b9599c | ||
|
|
5588c46d14 | ||
|
|
1767575728 | ||
|
|
c30d17ebb2 | ||
|
|
fd9d1ffb33 | ||
|
|
0a34c04b44 | ||
|
|
7e85168576 | ||
|
|
dfcfe4353a | ||
|
|
9edac78380 | ||
|
|
d2c9985c57 | ||
|
|
92c70bda08 | ||
|
|
9dc2e863e8 | ||
|
|
9394ac9b93 | ||
|
|
05acb8c857 | ||
|
|
d8d9f1b857 | ||
|
|
e08e3106d6 | ||
|
|
3e3afde223 | ||
|
|
0438b11c91 | ||
|
|
f8ae99ee7b | ||
|
|
b735feb901 | ||
|
|
dc127d15fb | ||
|
|
a96d96b3aa | ||
|
|
d57d822aa9 | ||
|
|
a79b2d418e | ||
|
|
0571a05497 | ||
|
|
3027e00905 | ||
|
|
2ecbacc924 | ||
|
|
84eb56d6b1 | ||
|
|
f3e5dcbe65 | ||
|
|
a78fdd386f | ||
|
|
90437de2c0 | ||
|
|
654e225093 | ||
|
|
e93b8715f9 | ||
|
|
b7fffb8691 | ||
|
|
0a04985678 | ||
|
|
9c8d9e54fe | ||
|
|
1f4070e52f | ||
|
|
2e9beaaa30 | ||
|
|
21d640cea3 | ||
|
|
352c0fd1c1 | ||
|
|
12a5fcfcd3 | ||
|
|
f2f8afa368 | ||
|
|
c393cc9707 | ||
|
|
0fa33915da | ||
|
|
634f51fb41 | ||
|
|
348c1cb965 | ||
|
|
32fc7def7c | ||
|
|
14602a7384 | ||
|
|
c683ef9ad9 | ||
|
|
039cc7458d | ||
|
|
d99f16f82d | ||
|
|
243e48e609 | ||
|
|
fac9f1ba58 | ||
|
|
6a5ecb4d95 | ||
|
|
dc28eae95a | ||
|
|
baf96da80c | ||
|
|
31ca6d9fd2 | ||
|
|
04befeb180 | ||
|
|
a4c8a2290d | ||
|
|
96ec5bb665 | ||
|
|
f60cea2216 | ||
|
|
02741f2c1b | ||
|
|
955b9239b9 | ||
|
|
53ac853367 | ||
|
|
1c38671356 | ||
|
|
91527fbd0d | ||
|
|
537d4163c2 | ||
|
|
c0e4a3a988 | ||
|
|
860049fad5 | ||
|
|
344f269414 | ||
|
|
3487353fc7 | ||
|
|
86fc8f4186 | ||
|
|
acb80da387 | ||
|
|
d99d8443ee | ||
|
|
319f9c392c | ||
|
|
a61a630ee6 | ||
|
|
e8260c164f | ||
|
|
4e4986f670 | ||
|
|
3610a2508b | ||
|
|
8396d07d9e | ||
|
|
59acd0f82f | ||
|
|
544ae6f915 | ||
|
|
111399cf4a | ||
|
|
fba5cee519 | ||
|
|
f95d79129e | ||
|
|
02ce353c6d | ||
|
|
241d9a2140 | ||
|
|
849ebf17f1 | ||
|
|
41776fbd02 | ||
|
|
5c7631e713 | ||
|
|
41e2478a6f | ||
|
|
a9d35dcccd | ||
|
|
e87766f106 | ||
|
|
a038f47a25 | ||
|
|
d841a2c862 | ||
|
|
4ee7025ab0 | ||
|
|
d0fa308f6e | ||
|
|
4456c8771d | ||
|
|
caa551a469 | ||
|
|
ccf5afbba9 | ||
|
|
2474ffc2ba | ||
|
|
5cf4d8e160 | ||
|
|
49ff9a249e | ||
|
|
abc448eca0 | ||
|
|
d7177cac97 | ||
|
|
c6182e8923 | ||
|
|
270730d9b3 | ||
|
|
4ec5c1dfcc | ||
|
|
da27c67e6b | ||
|
|
de3b53c676 | ||
|
|
3ac993bae4 | ||
|
|
756de57919 | ||
|
|
d9cf989813 | ||
|
|
ffc69576f2 | ||
|
|
2bed7f161b | ||
|
|
607607ef0a | ||
|
|
b2ba7709df | ||
|
|
8b488a5ca9 | ||
|
|
1d8ef79da6 | ||
|
|
bbf32730cd | ||
|
|
f58ca39e8c | ||
|
|
3b5f4ded31 | ||
|
|
f6fdfeb95f | ||
|
|
2c25319fb8 | ||
|
|
7daa0a250c | ||
|
|
121c2585c8 | ||
|
|
b18b94818a | ||
|
|
63db16a711 | ||
|
|
c30ab91738 | ||
|
|
e8f16292d1 | ||
|
|
db28abaa44 | ||
|
|
bfb2490938 | ||
|
|
b4ab88f708 | ||
|
|
2dab595476 | ||
|
|
a98967fccc | ||
|
|
32e4540464 | ||
|
|
02dc28e89c | ||
|
|
47545d9d32 | ||
|
|
5670dfc1d5 | ||
|
|
34f8af599d | ||
|
|
d36fe4d0e9 | ||
|
|
44317e2d34 | ||
|
|
5b806a2589 | ||
|
|
5a13c7fef7 | ||
|
|
c22a49f64e | ||
|
|
436024b4a0 | ||
|
|
61cde0f7e1 | ||
|
|
a9399873fd | ||
|
|
c2be1d3668 | ||
|
|
1d981d2c5a | ||
|
|
c2b7ca780e | ||
|
|
57e95c8969 | ||
|
|
56a74fb5dd | ||
|
|
49685c3989 | ||
|
|
9767e1e7fd | ||
|
|
a83e9c01a6 | ||
|
|
7b1ceeedf4 | ||
|
|
a0b3c9f8a4 | ||
|
|
a2a45757e9 | ||
|
|
271d21c7bd | ||
|
|
46b88566f1 | ||
|
|
af60bceada | ||
|
|
3583a5d251 | ||
|
|
4503aeabc4 | ||
|
|
7f1d3df4a5 | ||
|
|
17f1289c84 | ||
|
|
fc3d0fddd2 | ||
|
|
481685b3bb | ||
|
|
f31d431517 | ||
|
|
f19dcc0114 | ||
|
|
38a49d172c | ||
|
|
72935b0558 | ||
|
|
c9647f9b33 | ||
|
|
e1703aea3f | ||
|
|
fd53a76bf6 | ||
|
|
b5c1394662 | ||
|
|
66d7cb581c | ||
|
|
8f823420b6 | ||
|
|
36d5e011e8 | ||
|
|
5a4bcbf377 | ||
|
|
0fe007569e | ||
|
|
c49e454ec0 | ||
|
|
8afc8c62ef | ||
|
|
4644adff94 | ||
|
|
5e08083b5a | ||
|
|
95a8da803a | ||
|
|
43a41b34bc | ||
|
|
9c285a09e5 | ||
|
|
1f68da6b77 | ||
|
|
98e0e84d57 | ||
|
|
6ddba4363a | ||
|
|
b5b5c40ecd | ||
|
|
d84fe2b800 | ||
|
|
da561ba4d5 |
151
.github/workflows/ci.yml
vendored
151
.github/workflows/ci.yml
vendored
@@ -1,65 +1,125 @@
|
||||
name: Abaddon CI
|
||||
|
||||
on: [push, pull_request]
|
||||
on: [ push, pull_request ]
|
||||
|
||||
jobs:
|
||||
windows:
|
||||
name: windows-${{ matrix.buildtype }}
|
||||
runs-on: windows-2019
|
||||
msys2:
|
||||
name: msys2-mingw64
|
||||
runs-on: windows-latest
|
||||
strategy:
|
||||
matrix:
|
||||
buildtype: [Debug, RelWithDebInfo, MinSizeRel]
|
||||
buildtype: [ Debug, RelWithDebInfo, MinSizeRel ]
|
||||
mindeps: [ false ]
|
||||
include:
|
||||
- buildtype: RelWithDebInfo
|
||||
mindeps: true
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Fetch CMake
|
||||
uses: lukka/get-cmake@v3.21.2
|
||||
|
||||
- name: Fetch dependencies
|
||||
uses: lukka/run-vcpkg@v7
|
||||
- name: Setup MSYS2 (1)
|
||||
uses: haya14busa/action-cond@v1
|
||||
id: setupmsys
|
||||
with:
|
||||
vcpkgArguments: gtkmm nlohmann-json zlib sqlite3 glibmm openssl ixwebsocket curl
|
||||
vcpkgDirectory: ${{ github.workspace }}/ci/vcpkg/
|
||||
vcpkgTriplet: x64-windows
|
||||
cond: ${{ matrix.mindeps == true }}
|
||||
if_true: >-
|
||||
git
|
||||
make
|
||||
mingw-w64-x86_64-toolchain
|
||||
mingw-w64-x86_64-cmake
|
||||
mingw-w64-x86_64-ninja
|
||||
mingw-w64-x86_64-sqlite3
|
||||
mingw-w64-x86_64-nlohmann-json
|
||||
mingw-w64-x86_64-curl
|
||||
mingw-w64-x86_64-zlib
|
||||
mingw-w64-x86_64-gtkmm3
|
||||
mingw-w64-x86_64-spdlog
|
||||
if_false: >-
|
||||
git
|
||||
make
|
||||
mingw-w64-x86_64-toolchain
|
||||
mingw-w64-x86_64-cmake
|
||||
mingw-w64-x86_64-ninja
|
||||
mingw-w64-x86_64-sqlite3
|
||||
mingw-w64-x86_64-nlohmann-json
|
||||
mingw-w64-x86_64-curl
|
||||
mingw-w64-x86_64-zlib
|
||||
mingw-w64-x86_64-gtkmm3
|
||||
mingw-w64-x86_64-libhandy
|
||||
mingw-w64-x86_64-opus
|
||||
mingw-w64-x86_64-libsodium
|
||||
mingw-w64-x86_64-spdlog
|
||||
|
||||
- name: Build
|
||||
uses: lukka/run-cmake@v3
|
||||
- name: Setup MSYS2 (2)
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
useVcpkgToolchainFile: true
|
||||
vcpkgTriplet: x64-windows
|
||||
buildDirectory: ${{ runner.workspace }}/build
|
||||
cmakeBuildType: ${{ matrix.buildtype }}
|
||||
msystem: mingw64
|
||||
update: true
|
||||
install: ${{ steps.setupmsys.outputs.value }}
|
||||
|
||||
- name: Setup artifact files
|
||||
shell: cmd
|
||||
- name: Build (1)
|
||||
uses: haya14busa/action-cond@v1
|
||||
id: buildcmd
|
||||
with:
|
||||
cond: ${{ matrix.mindeps == true }}
|
||||
if_true: |
|
||||
cmake -GNinja -Bbuild -DUSE_LIBHANDY=OFF -DENABLE_VOICE=OFF -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }}
|
||||
cmake --build build
|
||||
if_false: |
|
||||
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }}
|
||||
cmake --build build
|
||||
|
||||
- name: Build (2)
|
||||
run: ${{ steps.buildcmd.outputs.value }}
|
||||
|
||||
- name: Setup Artifact
|
||||
run: |
|
||||
del /f /s /q "${{ runner.workspace }}\build\CMakeFiles"
|
||||
rmdir /s /q "${{ runner.workspace }}\build\CMakeFiles"
|
||||
del /f /s /q "${{ runner.workspace }}\build\.ninja_deps"
|
||||
del /f /s /q "${{ runner.workspace }}\build\.ninja_log"
|
||||
del /f /s /q "${{ runner.workspace }}\build\abaddon.ilk"
|
||||
del /f /s /q "${{ runner.workspace }}\build\CMakeCache.txt"
|
||||
xcopy /E /I "${{ github.workspace }}\res\css" "${{ runner.workspace }}\build\css"
|
||||
xcopy /E /I "${{ github.workspace }}\res\res" "${{ runner.workspace }}\build\res"
|
||||
xcopy /E /I "${{ github.workspace }}\res\fonts" "${{ runner.workspace }}\build\fonts"
|
||||
mkdir "${{ runner.workspace }}\build\share"
|
||||
xcopy /E /I "${{ github.workspace }}\ci\gtk-for-windows\gtk-nsis-pack\share\glib-2.0" "${{ runner.workspace }}\build\share\glib-2.0"
|
||||
copy "${{ github.workspace }}\ci\vcpkg\installed\x64-windows\tools\glib\gspawn-win64-helper.exe" "${{ runner.workspace }}\build\gspawn-win64-helper.exe"
|
||||
copy "${{ github.workspace }}\ci\vcpkg\installed\x64-windows\tools\glib\gspawn-win64-helper-console.exe" "${{ runner.workspace }}\build\gspawn-win64-helper-console.exe"
|
||||
mkdir -p build/artifactdir/bin build/artifactdir/ssl/certs build/artifactdir/lib build/artifactdir/share/glib-2.0/schemas
|
||||
cd build
|
||||
cp *.exe artifactdir/bin
|
||||
cd ..
|
||||
cp /mingw64/ssl/certs/ca-bundle.crt build/artifactdir/ssl/certs
|
||||
cp -r /mingw64/lib/gdk-pixbuf-2.0 build/artifactdir/lib
|
||||
cp -r res/css res/res res/fonts build/artifactdir/bin
|
||||
cp /mingw64/share/glib-2.0/schemas/gschemas.compiled build/artifactdir/share/glib-2.0/schemas
|
||||
cat "ci/msys-deps.txt" | sed 's/\r$//' | xargs -I % cp /mingw64% build/artifactdir/bin || :
|
||||
cp /usr/bin/msys-ffi-8.dll build/artifactdir/bin/libffi-8.dll
|
||||
mkdir -p build/artifactdir/share/icons/Adwaita
|
||||
cd build/artifactdir/share/icons/Adwaita
|
||||
mkdir -p 16x16/actions 24x24/actions 32x32/actions 48x48/actions 64x64/actions 96x96/actions scalable/actions
|
||||
cd ../../../../../
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/16x16/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/16x16/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/24x24/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/24x24/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/32x32/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/32x32/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/48x48/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/48x48/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/64x64/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/64x64/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/96x96/actions/%.symbolic.png build/artifactdir/share/icons/Adwaita/96x96/actions || :
|
||||
cat "ci/used-icons.txt" | sed 's/\r$//' | xargs -I % cp ci/gtk-for-windows/gtk-nsis-pack/share/icons/Adwaita/scalable/actions/%.svg build/artifactdir/share/icons/Adwaita/scalable/actions || :
|
||||
|
||||
- name: Upload build
|
||||
- name: Upload build (1)
|
||||
uses: haya14busa/action-cond@v1
|
||||
id: buildname
|
||||
with:
|
||||
cond: ${{ matrix.mindeps == true }}
|
||||
if_true: "${{ matrix.buildtype }}-mindeps"
|
||||
if_false: "${{ matrix.buildtype }}"
|
||||
|
||||
- name: Upload build (2)
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: build-windows-${{ matrix.buildtype }}
|
||||
path: ${{ runner.workspace }}/build
|
||||
name: build-windows-msys2-${{ steps.buildname.outputs.value }}
|
||||
path: build/artifactdir
|
||||
|
||||
mac:
|
||||
name: macos-${{ matrix.buildtype }}
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
matrix:
|
||||
buildtype: [Debug, RelWithDebInfo]
|
||||
buildtype: [ Debug, RelWithDebInfo ]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
with:
|
||||
@@ -72,6 +132,11 @@ jobs:
|
||||
run: |
|
||||
brew install gtkmm3
|
||||
brew install nlohmann-json
|
||||
brew install jpeg
|
||||
brew install opus
|
||||
brew install libsodium
|
||||
brew install spdlog
|
||||
brew install libhandy
|
||||
|
||||
- name: Build
|
||||
uses: lukka/run-cmake@v3
|
||||
@@ -94,10 +159,10 @@ jobs:
|
||||
|
||||
linux:
|
||||
name: linux-${{ matrix.buildtype }}
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-22.04
|
||||
strategy:
|
||||
matrix:
|
||||
buildtype: [Debug, RelWithDebInfo, MinSizeRel]
|
||||
buildtype: [ Debug, RelWithDebInfo, MinSizeRel ]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
with:
|
||||
@@ -113,7 +178,7 @@ jobs:
|
||||
cd deps
|
||||
git clone https://github.com/nlohmann/json
|
||||
cd json
|
||||
git checkout db78ac1d7716f56fc9f1b030b715f872f93964e4
|
||||
git checkout bc889afb4c5bf1c0d8ee29ef35eaaf4c8bef8a5d
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
@@ -121,6 +186,10 @@ jobs:
|
||||
sudo make install
|
||||
sudo apt-get install libgtkmm-3.0-dev
|
||||
sudo apt-get install libcurl4-gnutls-dev
|
||||
sudo apt-get install libopus-dev
|
||||
sudo apt-get install libsodium-dev
|
||||
sudo apt-get install libspdlog-dev
|
||||
sudo apt-get install libhandy-1-dev
|
||||
|
||||
- name: Build
|
||||
uses: lukka/run-cmake@v3
|
||||
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -356,3 +356,9 @@ build/
|
||||
out/
|
||||
|
||||
fonts/fonts.conf
|
||||
|
||||
# To make sure no zipped resources are added to the repo
|
||||
*.7z
|
||||
*.zip
|
||||
*.tar.*
|
||||
*.rar
|
||||
|
||||
11
.gitmodules
vendored
11
.gitmodules
vendored
@@ -1,12 +1,9 @@
|
||||
[submodule "vcpkg"]
|
||||
path = ci/vcpkg
|
||||
url = https://github.com/microsoft/vcpkg/
|
||||
[submodule "ci/vcpkg"]
|
||||
path = ci/vcpkg
|
||||
url = https://github.com/microsoft/vcpkg
|
||||
[submodule "ci/gtk-for-windows"]
|
||||
path = ci/gtk-for-windows
|
||||
url = https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer
|
||||
[submodule "subprojects/ixwebsocket"]
|
||||
path = subprojects/ixwebsocket
|
||||
url = https://github.com/machinezone/ixwebsocket
|
||||
url = https://github.com/ouwou/ixwebsocket
|
||||
[submodule "subprojects/miniaudio"]
|
||||
path = subprojects/miniaudio
|
||||
url = https://github.com/mackron/miniaudio
|
||||
|
||||
119
CMakeLists.txt
119
CMakeLists.txt
@@ -7,6 +7,9 @@ set(ABADDON_RESOURCE_DIR "/usr/share/abaddon" CACHE PATH "Fallback directory for
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
|
||||
|
||||
option(USE_LIBHANDY "Enable features that require libhandy (default)" ON)
|
||||
option(ENABLE_VOICE "Enable voice suppport" ON)
|
||||
|
||||
find_package(nlohmann_json REQUIRED)
|
||||
find_package(CURL)
|
||||
find_package(ZLIB REQUIRED)
|
||||
@@ -17,30 +20,33 @@ set(USE_TLS TRUE)
|
||||
set(USE_OPEN_SSL TRUE)
|
||||
find_package(IXWebSocket QUIET)
|
||||
if (NOT IXWebSocket_FOUND)
|
||||
message("ixwebsocket was not found and will be included as a submodule")
|
||||
add_subdirectory(subprojects/ixwebsocket)
|
||||
include_directories(IXWEBSOCKET_INCLUDE_DIRS)
|
||||
endif()
|
||||
message("ixwebsocket was not found and will be included as a submodule")
|
||||
add_subdirectory(subprojects/ixwebsocket)
|
||||
include_directories(IXWEBSOCKET_INCLUDE_DIRS)
|
||||
endif ()
|
||||
|
||||
if(MINGW OR WIN32)
|
||||
link_libraries(ws2_32)
|
||||
endif()
|
||||
if (MINGW OR WIN32)
|
||||
link_libraries(ws2_32)
|
||||
endif ()
|
||||
|
||||
if(WIN32)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_definitions(NOMINMAX)
|
||||
if (WIN32)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_definitions(NOMINMAX)
|
||||
endif ()
|
||||
|
||||
find_package(Fontconfig REQUIRED)
|
||||
link_libraries(${Fontconfig_LIBRARIES})
|
||||
endif()
|
||||
include(TestBigEndian)
|
||||
test_big_endian(IS_BIG_ENDIAN)
|
||||
if (IS_BIG_ENDIAN)
|
||||
add_compile_definitions(ABADDON_IS_BIG_ENDIAN)
|
||||
endif ()
|
||||
|
||||
configure_file(${PROJECT_SOURCE_DIR}/src/config.h.in ${PROJECT_BINARY_DIR}/config.h)
|
||||
|
||||
file(GLOB_RECURSE ABADDON_SOURCES
|
||||
"src/*.h"
|
||||
"src/*.hpp"
|
||||
"src/*.cpp"
|
||||
)
|
||||
"src/*.h"
|
||||
"src/*.hpp"
|
||||
"src/*.cpp"
|
||||
)
|
||||
|
||||
add_executable(abaddon ${ABADDON_SOURCES})
|
||||
target_include_directories(abaddon PUBLIC ${PROJECT_SOURCE_DIR}/src)
|
||||
@@ -51,36 +57,71 @@ target_include_directories(abaddon PUBLIC ${SQLite3_INCLUDE_DIRS})
|
||||
target_include_directories(abaddon PUBLIC ${NLOHMANN_JSON_INCLUDE_DIRS})
|
||||
|
||||
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
|
||||
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
|
||||
((CMAKE_SYSTEM_NAME STREQUAL "Linux") OR (CMAKE_CXX_COMPILER_VERSION LESS 9))))
|
||||
target_link_libraries(abaddon stdc++fs)
|
||||
endif()
|
||||
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
|
||||
((CMAKE_SYSTEM_NAME STREQUAL "Linux") OR (CMAKE_CXX_COMPILER_VERSION LESS 9))))
|
||||
target_link_libraries(abaddon stdc++fs)
|
||||
endif ()
|
||||
|
||||
if (IXWebSocket_LIBRARIES)
|
||||
target_link_libraries(abaddon ${IXWebSocket_LIBRARIES})
|
||||
find_library(MBEDTLS_X509_LIBRARY mbedx509)
|
||||
find_library(MBEDTLS_TLS_LIBRARY mbedtls)
|
||||
find_library(MBEDTLS_CRYPTO_LIBRARY mbedcrypto)
|
||||
if (MBEDTLS_TLS_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_TLS_LIBRARY})
|
||||
endif()
|
||||
if (MBEDTLS_X509_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_X509_LIBRARY})
|
||||
endif()
|
||||
if (MBEDTLS_CRYPTO_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_CRYPTO_LIBRARY})
|
||||
endif()
|
||||
else()
|
||||
target_link_libraries(abaddon $<BUILD_INTERFACE:ixwebsocket>)
|
||||
endif()
|
||||
target_link_libraries(abaddon ${IXWebSocket_LIBRARIES})
|
||||
find_library(MBEDTLS_X509_LIBRARY mbedx509)
|
||||
find_library(MBEDTLS_TLS_LIBRARY mbedtls)
|
||||
find_library(MBEDTLS_CRYPTO_LIBRARY mbedcrypto)
|
||||
if (MBEDTLS_TLS_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_TLS_LIBRARY})
|
||||
endif ()
|
||||
if (MBEDTLS_X509_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_X509_LIBRARY})
|
||||
endif ()
|
||||
if (MBEDTLS_CRYPTO_LIBRARY)
|
||||
target_link_libraries(abaddon ${MBEDTLS_CRYPTO_LIBRARY})
|
||||
endif ()
|
||||
else ()
|
||||
target_link_libraries(abaddon $<BUILD_INTERFACE:ixwebsocket>)
|
||||
endif ()
|
||||
|
||||
find_package(Threads)
|
||||
if (Threads_FOUND)
|
||||
target_link_libraries(abaddon Threads::Threads)
|
||||
endif()
|
||||
target_link_libraries(abaddon Threads::Threads)
|
||||
endif ()
|
||||
|
||||
find_package(Fontconfig QUIET)
|
||||
if (Fontconfig_FOUND)
|
||||
target_link_libraries(abaddon Fontconfig::Fontconfig)
|
||||
endif ()
|
||||
|
||||
find_package(spdlog REQUIRED)
|
||||
target_link_libraries(abaddon spdlog::spdlog)
|
||||
|
||||
target_link_libraries(abaddon ${SQLite3_LIBRARIES})
|
||||
target_link_libraries(abaddon ${GTKMM_LIBRARIES})
|
||||
target_link_libraries(abaddon ${CURL_LIBRARIES})
|
||||
target_link_libraries(abaddon ${ZLIB_LIBRARY})
|
||||
target_link_libraries(abaddon ${NLOHMANN_JSON_LIBRARIES})
|
||||
|
||||
if (USE_LIBHANDY)
|
||||
find_package(libhandy)
|
||||
if (NOT libhandy_FOUND)
|
||||
message("libhandy could not be found. features requiring it have been disabled")
|
||||
set(USE_LIBHANDY OFF)
|
||||
else ()
|
||||
target_include_directories(abaddon PUBLIC ${libhandy_INCLUDE_DIRS})
|
||||
target_link_libraries(abaddon ${libhandy_LIBRARIES})
|
||||
target_compile_definitions(abaddon PRIVATE WITH_LIBHANDY)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (ENABLE_VOICE)
|
||||
target_compile_definitions(abaddon PRIVATE WITH_VOICE)
|
||||
|
||||
find_package(PkgConfig)
|
||||
|
||||
target_include_directories(abaddon PUBLIC subprojects/miniaudio)
|
||||
pkg_check_modules(Opus REQUIRED IMPORTED_TARGET opus)
|
||||
target_link_libraries(abaddon PkgConfig::Opus)
|
||||
|
||||
pkg_check_modules(libsodium REQUIRED IMPORTED_TARGET libsodium)
|
||||
target_link_libraries(abaddon PkgConfig::libsodium)
|
||||
|
||||
target_link_libraries(abaddon ${CMAKE_DL_LIBS})
|
||||
endif ()
|
||||
|
||||
169
README.md
169
README.md
@@ -7,6 +7,7 @@ Alternative Discord client made in C++ with GTK
|
||||
<a href="https://discord.gg/wkCU3vuzG5"><img src="https://discord.com/api/guilds/858156817711890443/widget.png?style=shield"></a>
|
||||
|
||||
Current features:
|
||||
|
||||
* Not Electron
|
||||
* Handles most types of chat messages including embeds, images, and replies
|
||||
* Completely styleable/customizable with CSS (if you have a system GTK theme it won't really use it though)
|
||||
@@ -23,34 +24,58 @@ Current features:
|
||||
* Emojis<sup>2</sup>
|
||||
* Thread support<sup>3</sup>
|
||||
* Animated avatars, server icons, emojis (can be turned off)
|
||||
|
||||
1 - Abaddon tries its best to make Discord think it's a legitimate web client. Some of the things done to do this include: using a browser user agent, sending the same IDENTIFY message that the official web client does, using API v9 endpoints in all cases, and not using endpoints the web client does not normally use. There are still a few smaller inconsistencies, however. For example the web client sends lots of telemetry via the `/science` endpoint (uBlock origin stops this) as well as in the headers of all requests. **In any case,** you should use an official client for joining servers, sending new DMs, or managing your friends list if you are afraid of being caught in Discord's spam filters (unlikely).
|
||||
|
||||
2 - Unicode emojis are substituted manually as opposed to rendered by GTK on non-Windows platforms. This can be changed with the `stock_emojis` setting as shown at the bottom of this README. A CBDT-based font using Twemoji is provided to allow GTK to render emojis natively on Windows.
|
||||
1 - Abaddon tries its best (though is not perfect) to make Discord think it's a legitimate web client. Some of the
|
||||
things done to do this
|
||||
include: using a browser user agent, sending the same IDENTIFY message that the official web client does, using API v9
|
||||
endpoints in all cases, and not using endpoints the web client does not normally use. There are still a few smaller
|
||||
inconsistencies, however. For example the web client sends lots of telemetry via the `/science` endpoint (uBlock origin
|
||||
stops this) as well as in the headers of all requests.<br>
|
||||
|
||||
**See [here](#the-spam-filter)** for things you might want to avoid if you are worried about being caught in the spam
|
||||
filter.
|
||||
|
||||
2 - Unicode emojis are substituted manually as opposed to rendered by GTK on non-Windows platforms. This can be changed
|
||||
with the `stock_emojis` setting as shown at the bottom of this README. A CBDT-based font using Twemoji is provided to
|
||||
allow GTK to render emojis natively on Windows.
|
||||
|
||||
3 - There are some inconsistencies with thread state that might be encountered in some more uncommon cases, but they are
|
||||
the result of fundamental issues with Discord's thread implementation.
|
||||
|
||||
3 - There are some inconsistencies with thread state that might be encountered in some more uncommon cases, but they are the result of fundamental issues with Discord's thread implementation.
|
||||
|
||||
### Building manually (recommended if not on Windows):
|
||||
#### Windows:
|
||||
1. `git clone https://github.com/uowuo/abaddon && cd abaddon`
|
||||
2. `vcpkg install gtkmm:x64-windows nlohmann-json:x64-windows ixwebsocket:x64-windows zlib:x64-windows sqlite3:x64-windows openssl:x64-windows curl:x64-windows`
|
||||
|
||||
#### Windows (with MSYS2):
|
||||
|
||||
1. Install following packages:
|
||||
* mingw-w64-x86_64-cmake
|
||||
* mingw-w64-x86_64-ninja
|
||||
* mingw-w64-x86_64-sqlite3
|
||||
* mingw-w64-x86_64-nlohmann-json
|
||||
* mingw-w64-x86_64-curl
|
||||
* mingw-w64-x86_64-zlib
|
||||
* mingw-w64-x86_64-gtkmm3
|
||||
* mingw-w64-x86_64-libhandy
|
||||
2. `git clone --recurse-submodules="subprojects" https://github.com/uowuo/abaddon && cd abaddon`
|
||||
3. `mkdir build && cd build`
|
||||
4. `cmake -G"Visual Studio 16 2019" -A x64 -DCMAKE_TOOLCHAIN_FILE=c:\path\to\vcpkg\scripts\buildsystems\vcpkg.cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DVCPKG_TARGET_TRIPLET=x64-windows ..`
|
||||
5. Build with Visual Studio
|
||||
|
||||
Or, do steps 1 and 2, and open CMakeLists.txt in Visual Studio if `vcpkg integrate install` was run
|
||||
4. `cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo ..`
|
||||
5. `ninja`
|
||||
|
||||
#### Mac:
|
||||
1. `git clone https://github.com/uowuo/abaddon && cd abaddon`
|
||||
2. `brew install gtkmm3 nlohmann-json`
|
||||
3. `git submodule update --init subprojects`
|
||||
4. `mkdir build && cd build`
|
||||
5. `cmake ..`
|
||||
6. `make`
|
||||
|
||||
1. `git clone https://github.com/uowuo/abaddon --recurse-submodules="subprojects" && cd abaddon`
|
||||
2. `brew install gtkmm3 nlohmann-json libhandy`
|
||||
3. `mkdir build && cd build`
|
||||
4. `cmake ..`
|
||||
5. `make`
|
||||
|
||||
#### Linux:
|
||||
1. Install dependencies: `libgtkmm-3.0-dev`, `libcurl4-gnutls-dev`, and [nlohmann-json](https://github.com/nlohmann/json)
|
||||
2. `git clone https://github.com/uowuo/abaddon && cd abaddon`
|
||||
|
||||
1. Install dependencies
|
||||
* On Ubuntu 20.04 (Focal) and newer:
|
||||
```Shell
|
||||
$ sudo apt install g++ cmake libgtkmm-3.0-dev libcurl4-gnutls-dev libsqlite3-dev libssl-dev nlohmann-json3-dev
|
||||
```
|
||||
2. `git clone https://github.com/uowuo/abaddon --recurse-submodules="subprojects" && cd abaddon`
|
||||
3. `mkdir build && cd build`
|
||||
4. `cmake ..`
|
||||
5. `make`
|
||||
@@ -61,39 +86,60 @@ Latest release version: https://github.com/uowuo/abaddon/releases/latest
|
||||
|
||||
**CI:**
|
||||
|
||||
- Windows: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-windows-RelWithDebInfo.zip)
|
||||
- MacOS: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-macos-RelWithDebInfo.zip) unsigned, unpackaged, requires gtkmm3 (e.g. from homebrew)
|
||||
- Linux: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-linux-MinSizeRel.zip) unpackaged (for now), requires gtkmm3. built on Ubuntu 18.04 + gcc9
|
||||
- Windows: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-windows-msys2-MinSizeRel.zip)
|
||||
- MacOS: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-macos-RelWithDebInfo.zip) unsigned,
|
||||
unpackaged, requires gtkmm3 (e.g. from homebrew)
|
||||
- Linux: [here](https://nightly.link/uowuo/abaddon/workflows/ci/master/build-linux-MinSizeRel.zip) unpackaged (for now),
|
||||
requires gtkmm3. built on Ubuntu 18.04 + gcc9
|
||||
|
||||
⚠️ If you use Windows, make sure to start from the directory containing `css` and `res`
|
||||
⚠️ If you use Windows, make sure to start from the `bin` directory
|
||||
|
||||
On Linux, `css` and `res` can also be loaded from `~/.local/share/abaddon` or `/usr/share/abaddon`
|
||||
|
||||
`abaddon.ini` will also be automatically used if located at `~/.config/abaddon/abaddon.ini` and there is no `abaddon.ini` in the working directory
|
||||
`abaddon.ini` will also be automatically used if located at `~/.config/abaddon/abaddon.ini` and there is
|
||||
no `abaddon.ini` in the working directory
|
||||
|
||||
#### The Spam Filter
|
||||
|
||||
Discord likes disabling accounts/forcing them to reset their passwords if they think the user is a spam bot or
|
||||
potentially had their account compromised. While the official client still often gets users caught in the spam filter,
|
||||
third party clients tend to upset the spam filter more often. If you get caught by it, you can
|
||||
usually [appeal](https://support.discord.com/hc/en-us/requests/new?ticket_form_id=360000029731) it and get it restored.
|
||||
Here are some things you might want to do with the official client instead if you are particularly afraid of evoking the
|
||||
spam filter's wrath:
|
||||
|
||||
* Joining or leaving servers (usually main cause of getting caught)
|
||||
* Frequently disconnecting and reconnecting
|
||||
* Starting new DMs with people
|
||||
* Managing your friends list
|
||||
* Managing your user profile while connected to a third party client
|
||||
|
||||
#### Dependencies:
|
||||
|
||||
#### Dependencies:
|
||||
* [gtkmm](https://www.gtkmm.org/en/)
|
||||
* [JSON for Modern C++](https://github.com/nlohmann/json)
|
||||
* [IXWebSocket](https://github.com/machinezone/IXWebSocket)
|
||||
* [libcurl](https://curl.se/)
|
||||
* [zlib](https://zlib.net/)
|
||||
* [SQLite3](https://www.sqlite.org/index.html)
|
||||
* [libhandy](https://gnome.pages.gitlab.gnome.org/libhandy/) (optional)
|
||||
|
||||
### TODO:
|
||||
|
||||
* Voice support
|
||||
* User activities
|
||||
* Nicknames
|
||||
* More server management stuff
|
||||
* Manage friends
|
||||
* A bunch of other stuff
|
||||
|
||||
### Styling
|
||||
|
||||
#### CSS selectors
|
||||
|
||||
.app-window - Applied to all windows. This means the main window and all popups
|
||||
.app-popup - Additional class for `.app-window`s when the window is not the main window
|
||||
|
||||
.channel-list - Container of the channel list
|
||||
|
||||
.app-popup - Additional class for `.app-window`s when the window is not the main window
|
||||
|
||||
.channel-list - Container of the channel list
|
||||
|
||||
.messages - Container of user messages
|
||||
.message-container - The container which holds a user's messages
|
||||
.message-container-author - The author label for a message container
|
||||
@@ -109,47 +155,45 @@ On Linux, `css` and `res` can also be loaded from `~/.local/share/abaddon` or `/
|
||||
.replying - Extra class for chat input container when a reply is currently being created
|
||||
.reaction-box - Contains a reaction image and the count
|
||||
.reacted - Additional class for reaction-box when the user has reacted with a particular reaction
|
||||
.reaction-count - Contains the count for reaction
|
||||
|
||||
.reaction-count - Contains the count for reaction
|
||||
|
||||
.completer - Container for the message completer
|
||||
.completer-entry - Container for a single entry in the completer
|
||||
.completer-entry-label - Contains the label for an entry in the completer
|
||||
.completer-entry-image - Contains the image for an entry in the completer
|
||||
|
||||
.completer-entry-image - Contains the image for an entry in the completer
|
||||
|
||||
.embed - Container for a message embed
|
||||
.embed-author - The author of an embed
|
||||
.embed-title - The title of an embed
|
||||
.embed-description - The description of an embed
|
||||
.embed-field-title - The title of an embed field
|
||||
.embed-field-value - The value of an embed field
|
||||
.embed-footer - The footer of an embed
|
||||
|
||||
.embed-footer - The footer of an embed
|
||||
|
||||
.members - Container of the member list
|
||||
.members-row - All rows within the members container
|
||||
.members-row-label - All labels in the members container
|
||||
.members-row-member - Rows containing a member
|
||||
.members-row-role - Rows containing a role
|
||||
.members-row-avatar - Contains the avatar for a row in the member list
|
||||
|
||||
.members-row-avatar - Contains the avatar for a row in the member list
|
||||
|
||||
.status-indicator - The status indicator
|
||||
.online - Applied to status indicators when the associated user is online
|
||||
.idle - Applied to status indicators when the associated user is away
|
||||
.dnd - Applied to status indicators when the associated user is on do not disturb
|
||||
.offline - Applied to status indicators when the associated user is offline
|
||||
|
||||
.typing-indicator - The typing indicator (also used for replies)
|
||||
|
||||
.offline - Applied to status indicators when the associated user is offline
|
||||
|
||||
.typing-indicator - The typing indicator (also used for replies)
|
||||
|
||||
Used in reorderable list implementation:
|
||||
.drag-icon
|
||||
.drag-hover-top
|
||||
.drag-hover-bottom
|
||||
.drag-icon .drag-hover-top .drag-hover-bottom
|
||||
|
||||
Used in guild settings popup:
|
||||
.guild-settings-window
|
||||
.guild-members-pane-list - Container for list of members in the members pane
|
||||
.guild-members-pane-info - Container for member info
|
||||
.guild-roles-pane-list - Container for list of roles in the roles pane
|
||||
|
||||
.guild-roles-pane-list - Container for list of roles in the roles pane
|
||||
|
||||
Used in profile popup:
|
||||
.mutual-friend-item - Applied to every item in the mutual friends list
|
||||
.mutual-friend-item-name - Name in mutual friend item
|
||||
@@ -174,11 +218,13 @@ Used in profile popup:
|
||||
.profile-switcher - Buttons used to switch viewed section of profile
|
||||
.profile-stack - Container for profile info that can be switched between
|
||||
.profile-badges - Container for badges
|
||||
.profile-badge
|
||||
|
||||
.profile-badge
|
||||
|
||||
### Settings
|
||||
|
||||
Settings are configured (for now) by editing abaddon.ini
|
||||
The format is similar to the standard Windows ini format **except**:
|
||||
The format is similar to the standard Windows ini format **except**:
|
||||
|
||||
* `#` is used to begin comments as opposed to `;`
|
||||
* Section and key names are case-sensitive
|
||||
|
||||
@@ -187,27 +233,38 @@ This listing is organized by section.
|
||||
For example, memory_db would be set by adding `memory_db = true` under the line `[discord]`
|
||||
|
||||
#### discord
|
||||
|
||||
* gateway (string) - override url for Discord gateway. must be json format and use zlib stream compression
|
||||
* api_base (string) - override base url for Discord API
|
||||
* memory_db (true or false, default false) - if true, Discord data will be kept in memory as opposed to on disk
|
||||
* token (string) - Discord token used to login, this can be set from the menu
|
||||
* prefetch (true or false, default false) - if true, new messages will cause the avatar and image attachments to be automatically downloaded
|
||||
* prefetch (true or false, default false) - if true, new messages will cause the avatar and image attachments to be
|
||||
automatically downloaded
|
||||
|
||||
#### http
|
||||
|
||||
* user_agent (string) - sets the user-agent to use in HTTP requests to the Discord API (not including media/images)
|
||||
* concurrent (int, default 20) - how many images can be concurrently retrieved
|
||||
|
||||
#### gui
|
||||
|
||||
* member_list_discriminator (true or false, default true) - show user discriminators in the member list
|
||||
* stock_emojis (true or false, default true) - allow abaddon to substitute unicode emojis with images from emojis.bin, must be false to allow GTK to render emojis itself
|
||||
* stock_emojis (true or false, default true) - allow abaddon to substitute unicode emojis with images from emojis.bin,
|
||||
must be false to allow GTK to render emojis itself
|
||||
* custom_emojis (true or false, default true) - download and use custom Discord emojis
|
||||
* css (string) - path to the main CSS file
|
||||
* animations (true or false, default true) - use animated images where available (e.g. server icons, emojis, avatars). false means static images will be used
|
||||
* animated_guild_hover_only (true or false, default true) - only animate guild icons when the guild is being hovered over
|
||||
* animations (true or false, default true) - use animated images where available (e.g. server icons, emojis, avatars).
|
||||
false means static images will be used
|
||||
* animated_guild_hover_only (true or false, default true) - only animate guild icons when the guild is being hovered
|
||||
over
|
||||
* owner_crown (true or false, default true) - show a crown next to the owner
|
||||
* unreads (true or false, default true) - show unread indicators and mention badges
|
||||
* save_state (true or false, default true) - save the state of the gui (active channels, tabs, expanded channels)
|
||||
* alt_menu (true or false, default false) - keep the menu hidden unless revealed with alt key
|
||||
* hide_to_tray (true or false, default false) - hide abaddon to the system tray on window close
|
||||
|
||||
#### style
|
||||
|
||||
* linkcolor (string) - color to use for links in messages
|
||||
* expandercolor (string) - color to use for the expander in the channel list
|
||||
* nsfwchannelcolor (string) - color to use for NSFW channels in the channel list
|
||||
|
||||
60
ci/msys-deps.txt
Normal file
60
ci/msys-deps.txt
Normal file
@@ -0,0 +1,60 @@
|
||||
/bin/gdbus.exe
|
||||
/bin/gspawn-win64-helper-console.exe
|
||||
/bin/libatk-1.0-0.dll
|
||||
/bin/libatkmm-1.6-1.dll
|
||||
/bin/libbrotlicommon.dll
|
||||
/bin/libbrotlidec.dll
|
||||
/bin/libbz2-1.dll
|
||||
/bin/libcairo-2.dll
|
||||
/bin/libcairo-gobject-2.dll
|
||||
/bin/libcairomm-1.0-1.dll
|
||||
/bin/libcrypto-1_1-x64.dll
|
||||
/bin/libcurl-4.dll
|
||||
/bin/libdatrie-1.dll
|
||||
/bin/libdeflate.dll
|
||||
/bin/libepoxy-0.dll
|
||||
/bin/libexpat-1.dll
|
||||
/bin/libffi-8.dll
|
||||
/bin/libfontconfig-1.dll
|
||||
/bin/libfreetype-6.dll
|
||||
/bin/libfribidi-0.dll
|
||||
/bin/libgcc_s_seh-1.dll
|
||||
/bin/libgdk-3-0.dll
|
||||
/bin/libgdk_pixbuf-2.0-0.dll
|
||||
/bin/libgdkmm-3.0-1.dll
|
||||
/bin/libgio-2.0-0.dll
|
||||
/bin/libgiomm-2.4-1.dll
|
||||
/bin/libglib-2.0-0.dll
|
||||
/bin/libglibmm-2.4-1.dll
|
||||
/bin/libgmodule-2.0-0.dll
|
||||
/bin/libgobject-2.0-0.dll
|
||||
/bin/libgraphite2.dll
|
||||
/bin/libgtk-3-0.dll
|
||||
/bin/libgtkmm-3.0-1.dll
|
||||
/bin/libhandy-1-0.dll
|
||||
/bin/libharfbuzz-0.dll
|
||||
/bin/libiconv-2.dll
|
||||
/bin/libidn2-0.dll
|
||||
/bin/libintl-8.dll
|
||||
/bin/libnghttp2-14.dll
|
||||
/bin/libpango-1.0-0.dll
|
||||
/bin/libpangocairo-1.0-0.dll
|
||||
/bin/libpangoft2-1.0-0.dll
|
||||
/bin/libpangomm-1.4-1.dll
|
||||
/bin/libpangowin32-1.0-0.dll
|
||||
/bin/libpcre2-8-0.dll
|
||||
/bin/libpixman-1-0.dll
|
||||
/bin/libpng16-16.dll
|
||||
/bin/libpsl-5.dll
|
||||
/bin/libsigc-2.0-0.dll
|
||||
/bin/libspdlog.dll
|
||||
/bin/libsqlite3-0.dll
|
||||
/bin/libssh2-1.dll
|
||||
/bin/libssl-1_1-x64.dll
|
||||
/bin/libstdc++-6.dll
|
||||
/bin/libthai-0.dll
|
||||
/bin/libunistring-2.dll
|
||||
/bin/libwinpthread-1.dll
|
||||
/bin/libzstd.dll
|
||||
/bin/zlib1.dll
|
||||
/../usr/bin/msys-2.0.dll
|
||||
1
ci/used-icons.txt
Normal file
1
ci/used-icons.txt
Normal file
@@ -0,0 +1 @@
|
||||
document-send-symbolic
|
||||
1
ci/vcpkg
1
ci/vcpkg
Submodule ci/vcpkg deleted from a9b27ed5df
37
cmake/Findgdk.cmake
Normal file
37
cmake/Findgdk.cmake
Normal file
@@ -0,0 +1,37 @@
|
||||
find_package(PkgConfig)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PC_gdk QUIET gdk-3.0)
|
||||
set(gdk_DEFINITIONS ${PC_gdk_CFLAGS_OTHER})
|
||||
endif ()
|
||||
|
||||
set(gdk_INCLUDE_HINTS ${PC_gdk_INCLUDEDIR} ${PC_gdk_INCLUDE_DIRS})
|
||||
set(gdk_LIBRARY_HINTS ${PC_gdk_LIBDIR} ${PC_gdk_LIBRARY_DIRS})
|
||||
|
||||
find_path(gdk_INCLUDE_DIR
|
||||
NAMES gdk/gdk.h
|
||||
HINTS ${gdk_INCLUDE_HINTS}
|
||||
/usr/include
|
||||
/usr/local/include
|
||||
/opt/local/include
|
||||
PATH_SUFFIXES gdk-3.0)
|
||||
|
||||
find_library(gdk_LIBRARY
|
||||
NAMES gdk-3.0
|
||||
gdk-3
|
||||
gdk
|
||||
HINTS ${gdk_LIBRARY_HINTS}
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
/opt/local/lib)
|
||||
|
||||
set(gdk_LIBRARIES ${gdk_LIBRARY})
|
||||
set(gdk_INCLUDE_DIRS ${gdk_INCLUDE_DIR})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(gdk
|
||||
REQUIRED_VARS
|
||||
gdk_LIBRARY
|
||||
gdk_INCLUDE_DIR
|
||||
VERSION_VAR gdk_VERSION)
|
||||
|
||||
mark_as_advanced(gdk_INCLUDE_DIR gdk_LIBRARY)
|
||||
@@ -1,48 +1,50 @@
|
||||
set(GDKMM_LIBRARY_NAME gdkmm-3.0)
|
||||
set(gdkmm_LIBRARY_NAME gdkmm-3.0)
|
||||
|
||||
find_package(PkgConfig)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PKGCONFIG_GDKMM QUIET ${GDKMM_LIBRARY_NAME})
|
||||
set(GDKMM_DEFINITIONS ${PKGCONFIG_GDKMM_CFLAGS_OTHER})
|
||||
pkg_check_modules(PKGCONFIG_gdkmm QUIET ${gdkmm_LIBRARY_NAME})
|
||||
set(gdkmm_DEFINITIONS ${PKGCONFIG_gdkmm_CFLAGS_OTHER})
|
||||
endif (PKG_CONFIG_FOUND)
|
||||
|
||||
set(GDKMM_INCLUDE_HINTS ${PKGCONFIG_GDKMM_INCLUDEDIR} ${PKGCONFIG_GDKMM_INCLUDE_DIRS})
|
||||
set(GDKMM_LIBRARY_HINTS ${PKGCONFIG_GDKMM_LIBDIR} ${PKGCONFIG_GDKMM_LIBRARY_DIRS})
|
||||
set(gdkmm_INCLUDE_HINTS ${PKGCONFIG_gdkmm_INCLUDEDIR} ${PKGCONFIG_gdkmm_INCLUDE_DIRS})
|
||||
set(gdkmm_LIBRARY_HINTS ${PKGCONFIG_gdkmm_LIBDIR} ${PKGCONFIG_gdkmm_LIBRARY_DIRS})
|
||||
|
||||
find_path(GDKMM_INCLUDE_DIR
|
||||
find_path(gdkmm_INCLUDE_DIR
|
||||
NAMES gdkmm.h
|
||||
HINTS ${GDKMM_INCLUDE_HINTS}
|
||||
HINTS ${gdkmm_INCLUDE_HINTS}
|
||||
/usr/include
|
||||
/usr/local/include
|
||||
/opt/local/include
|
||||
PATH_SUFFIXES ${GDKMM_LIBRARY_NAME})
|
||||
PATH_SUFFIXES ${gdkmm_LIBRARY_NAME})
|
||||
|
||||
find_path(GDKMM_CONFIG_INCLUDE_DIR
|
||||
find_path(gdkmm_CONFIG_INCLUDE_DIR
|
||||
NAMES gdkmmconfig.h
|
||||
HINTS ${GDKMM_LIBRARY_HINTS}
|
||||
HINTS ${gdkmm_LIBRARY_HINTS}
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
/opt/local/lib
|
||||
PATH_SUFFIXES ${GDKMM_LIBRARY_NAME}/include)
|
||||
PATH_SUFFIXES ${gdkmm_LIBRARY_NAME}/include)
|
||||
|
||||
find_library(GDKMM_LIBRARY
|
||||
NAMES ${GDKMM_LIBRARY_NAME}
|
||||
gdkmm
|
||||
HINTS ${GDKMM_LIBRARY_HINTS}
|
||||
find_library(gdkmm_LIBRARY
|
||||
NAMES ${gdkmm_LIBRARY_NAME}
|
||||
gdkmm
|
||||
HINTS ${gdkmm_LIBRARY_HINTS}
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
/opt/local/lib
|
||||
PATH_SUFFIXES ${GDKMM_LIBRARY_NAME}
|
||||
${GDKMM_LIBRARY_NAME}/include)
|
||||
PATH_SUFFIXES ${gdkmm_LIBRARY_NAME}
|
||||
${gdkmm_LIBRARY_NAME}/include)
|
||||
|
||||
set(GDKMM_LIBRARIES ${GDKMM_LIBRARY})
|
||||
set(GDKMM_INCLUDE_DIRS ${GDKMM_INCLUDE_DIR};${GDKMM_CONFIG_INCLUDE_DIRS};${GDKMM_CONFIG_INCLUDE_DIR})
|
||||
find_package(gdk)
|
||||
|
||||
set(gdkmm_LIBRARIES ${gdkmm_LIBRARY};${gdk_LIBRARIES})
|
||||
set(gdkmm_INCLUDE_DIRS ${gdkmm_INCLUDE_DIR};${gdkmm_CONFIG_INCLUDE_DIRS};${gdkmm_CONFIG_INCLUDE_DIR};${gdk_INCLUDE_DIRS})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(gdkmm
|
||||
REQUIRED_VARS
|
||||
GDKMM_LIBRARY
|
||||
GDKMM_INCLUDE_DIRS
|
||||
VERSION_VAR GDKMM_VERSION)
|
||||
gdkmm_LIBRARY
|
||||
gdkmm_INCLUDE_DIRS
|
||||
VERSION_VAR gdkmm_VERSION)
|
||||
|
||||
mark_as_advanced(GDKMM_INCLUDE_DIR GDKMM_LIBRARY)
|
||||
mark_as_advanced(gdkmm_INCLUDE_DIR gdkmm_LIBRARY)
|
||||
|
||||
@@ -2,56 +2,70 @@ find_package(PkgConfig)
|
||||
pkg_check_modules(PC_GLIB2 QUIET glib-2.0)
|
||||
|
||||
find_path(GLIB_INCLUDE_DIR
|
||||
NAMES glib.h
|
||||
HINTS ${PC_GLIB2_INCLUDEDIR}
|
||||
${PC_GLIB2_INCLUDE_DIRS}
|
||||
$ENV{GLIB2_HOME}/include
|
||||
$ENV{GLIB2_ROOT}/include
|
||||
/usr/local/include
|
||||
/usr/include
|
||||
/glib2/include
|
||||
/glib-2.0/include
|
||||
PATH_SUFFIXES glib2 glib-2.0 glib-2.0/include
|
||||
)
|
||||
NAMES glib.h
|
||||
HINTS ${PC_GLIB2_INCLUDEDIR}
|
||||
${PC_GLIB2_INCLUDE_DIRS}
|
||||
$ENV{GLIB2_HOME}/include
|
||||
$ENV{GLIB2_ROOT}/include
|
||||
/usr/local/include
|
||||
/usr/include
|
||||
/glib2/include
|
||||
/glib-2.0/include
|
||||
PATH_SUFFIXES glib2 glib-2.0 glib-2.0/include
|
||||
)
|
||||
set(GLIB_INCLUDE_DIRS ${GLIB_INCLUDE_DIR})
|
||||
|
||||
find_library(GLIB_LIBRARIES
|
||||
NAMES glib2
|
||||
glib-2.0
|
||||
HINTS ${PC_GLIB2_LIBDIR}
|
||||
${PC_GLIB2_LIBRARY_DIRS}
|
||||
$ENV{GLIB2_HOME}/lib
|
||||
$ENV{GLIB2_ROOT}/lib
|
||||
/usr/local/lib
|
||||
/usr/lib
|
||||
/lib
|
||||
/glib-2.0/lib
|
||||
PATH_SUFFIXES glib2 glib-2.0
|
||||
)
|
||||
NAMES glib2
|
||||
glib-2.0
|
||||
HINTS ${PC_GLIB2_LIBDIR}
|
||||
${PC_GLIB2_LIBRARY_DIRS}
|
||||
$ENV{GLIB2_HOME}/lib
|
||||
$ENV{GLIB2_ROOT}/lib
|
||||
/usr/local/lib
|
||||
/usr/lib
|
||||
/lib
|
||||
/glib-2.0/lib
|
||||
PATH_SUFFIXES glib2 glib-2.0
|
||||
)
|
||||
|
||||
find_library(glib_GOBJECT_LIBRARIES
|
||||
NAMES gobject-2.0
|
||||
HINTS ${PC_GLIB2_LIBDIR}
|
||||
${PC_GLIB2_LIBRARY_DIRS}
|
||||
)
|
||||
|
||||
find_library(glib_GIO_LIBRARIES
|
||||
NAMES gio-2.0
|
||||
HINTS ${PC_GLIB2_LIBDIR}
|
||||
${PC_GLIB2_LIBRARY_DIRS}
|
||||
)
|
||||
|
||||
get_filename_component(_GLIB2_LIB_DIR "${GLIB_LIBRARIES}" PATH)
|
||||
find_path(GLIB_CONFIG_INCLUDE_DIR
|
||||
NAMES glibconfig.h
|
||||
HINTS ${PC_GLIB2_INCLUDEDIR}
|
||||
${PC_GLIB2_INCLUDE_DIRS}
|
||||
$ENV{GLIB2_HOME}/include
|
||||
$ENV{GLIB2_ROOT}/include
|
||||
/usr/local/include
|
||||
/usr/include
|
||||
/glib2/include
|
||||
/glib-2.0/include
|
||||
${_GLIB2_LIB_DIR}
|
||||
${CMAKE_SYSTEM_LIBRARY_PATH}
|
||||
PATH_SUFFIXES glib2 glib-2.0 glib-2.0/include
|
||||
)
|
||||
NAMES glibconfig.h
|
||||
HINTS ${PC_GLIB2_INCLUDEDIR}
|
||||
${PC_GLIB2_INCLUDE_DIRS}
|
||||
$ENV{GLIB2_HOME}/include
|
||||
$ENV{GLIB2_ROOT}/include
|
||||
/usr/local/include
|
||||
/usr/include
|
||||
/glib2/include
|
||||
/glib-2.0/include
|
||||
${_GLIB2_LIB_DIR}
|
||||
${CMAKE_SYSTEM_LIBRARY_PATH}
|
||||
PATH_SUFFIXES glib2 glib-2.0 glib-2.0/include
|
||||
)
|
||||
if (GLIB_CONFIG_INCLUDE_DIR)
|
||||
set(GLIB_INCLUDE_DIRS ${GLIB_INCLUDE_DIRS} ${GLIB_CONFIG_INCLUDE_DIR})
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
set(GLIB_LIBRARIES ${GLIB_LIBRARIES} ${glib_GOBJECT_LIBRARIES} ${glib_GIO_LIBRARIES})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(glib
|
||||
REQUIRED_VARS
|
||||
GLIB_LIBRARIES
|
||||
GLIB_INCLUDE_DIRS
|
||||
VERSION_VAR GLIB_VERSION)
|
||||
mark_as_advanced(GLIB_INCLUDE_DIR GLIB_CONFIG_INCLUDE_DIR)
|
||||
REQUIRED_VARS
|
||||
GLIB_LIBRARIES
|
||||
GLIB_INCLUDE_DIRS
|
||||
VERSION_VAR GLIB_VERSION)
|
||||
mark_as_advanced(GLIB_INCLUDE_DIR GLIB_CONFIG_INCLUDE_DIR glib_GOBJECT_LIBRARIES)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
set(GTKMM_LIBRARY_NAME gtkmm-3.0)
|
||||
set(GDKMM_LIBRARY_NAME gdkmm-3.0)
|
||||
set(GTKMM_LIBRARY_NAME gtkmm-3.0)
|
||||
set(GDKMM_LIBRARY_NAME gdkmm-3.0)
|
||||
|
||||
find_package(PkgConfig)
|
||||
if(PKG_CONFIG_FOUND)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PC_GTKMM QUIET ${GTKMM_LIBRARY_NAME})
|
||||
pkg_check_modules(PC_GDKMM QUIET ${GDKMM_LIBRARY_NAME})
|
||||
pkg_check_modules(PC_PANGOMM QUIET ${PANGOMM_LIBRARY_NAME})
|
||||
set(GTKMM_DEFINITIONS ${PC_GTKMM_CFLAGS_OTHER})
|
||||
endif()
|
||||
set(GTKMM_DEFINITIONS ${PC_GTKMM_CFLAGS_OTHER})
|
||||
endif ()
|
||||
|
||||
find_package(gtk)
|
||||
find_package(glibmm)
|
||||
@@ -46,14 +46,14 @@ find_path(GDKMM_CONFIG_INCLUDE_DIR
|
||||
HINTS ${GDKMM_INCLUDE_HINTS}
|
||||
PATH_SUFFIXES ${GDKMM_LIBRARY_NAME}/include)
|
||||
|
||||
set(GTKMM_LIBRARIES ${GTKMM_LIB};${GDKMM_LIBRARY};${GTK_LIBRARIES};${GLIBMM_LIBRARIES};${PANGOMM_LIBRARIES};${CAIROMM_LIBRARIES};${ATKMM_LIBRARIES};${SIGC++_LIBRARIES})
|
||||
set(GTKMM_INCLUDE_DIRS ${GTKMM_INCLUDE_DIR};${GTKMM_CONFIG_INCLUDE_DIR};${GDKMM_INCLUDE_DIR};${GDKMM_CONFIG_INCLUDE_DIR};${GTK_INCLUDE_DIRS};${GLIBMM_INCLUDE_DIRS};${PANGOMM_INCLUDE_DIRS};${CAIROMM_INCLUDE_DIRS};${ATKMM_INCLUDE_DIRS};${SIGC++_INCLUDE_DIRS})
|
||||
set(GTKMM_LIBRARIES ${GTKMM_LIB};${gdkmm_LIBRARIES};${GTK_LIBRARIES};${GLIBMM_LIBRARIES};${PANGOMM_LIBRARIES};${CAIROMM_LIBRARIES};${ATKMM_LIBRARIES};${SIGC++_LIBRARIES})
|
||||
set(GTKMM_INCLUDE_DIRS ${GTKMM_INCLUDE_DIR};${GTKMM_CONFIG_INCLUDE_DIR};${gdkmm_INCLUDE_DIRS};${gdkmm_CONFIG_INCLUDE_DIR};${GTK_INCLUDE_DIRS};${GLIBMM_INCLUDE_DIRS};${PANGOMM_INCLUDE_DIRS};${CAIROMM_INCLUDE_DIRS};${ATKMM_INCLUDE_DIRS};${SIGC++_INCLUDE_DIRS})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(gtkmm
|
||||
REQUIRED_VARS
|
||||
GTKMM_LIB
|
||||
GTKMM_INCLUDE_DIRS
|
||||
GTKMM_LIB
|
||||
GTKMM_INCLUDE_DIRS
|
||||
VERSION_VAR GTKMM_VERSION)
|
||||
|
||||
mark_as_advanced(GTKMM_INCLUDE_DIR GTKMM_LIBRARY)
|
||||
|
||||
39
cmake/Findlibhandy.cmake
Normal file
39
cmake/Findlibhandy.cmake
Normal file
@@ -0,0 +1,39 @@
|
||||
set(libhandy_LIBRARY_NAME libhandy-1)
|
||||
|
||||
find_package(PkgConfig)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(PC_libhandy QUIET ${libhandy_LIBRARY_NAME})
|
||||
set(libhandy_DEfINITIONS ${PC_libhandy_CFLAGS_OTHER})
|
||||
endif (PKG_CONFIG_FOUND)
|
||||
|
||||
set(libhandy_INCLUDE_HINTS ${PC_libhandy_INCLUDEDIR} ${PC_libhandy_INCLUDE_DIRS})
|
||||
set(libhandy_LIBRARY_HINTS ${PC_libhandy_LIBDIR} ${PC_libhandy_LIBRARY_DIRS})
|
||||
|
||||
find_path(libhandy_INCLUDE_DIR
|
||||
NAMES handy.h
|
||||
HINTS ${libhandy_INCLUDE_HINTS}
|
||||
/usr/include
|
||||
/usr/local/include
|
||||
/opt/local/include
|
||||
PATH_SUFFIXES ${libhandy_LIBRARY_NAME})
|
||||
|
||||
find_library(libhandy_LIBRARY
|
||||
NAMES ${libhandy_LIBRARY_NAME} handy-1
|
||||
HINTS ${libhandy_LIBRARY_HINTS}
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
/opt/local/lib
|
||||
PATH_SUFFIXES ${libhandy_LIBRARY_NAME}
|
||||
${libhandy_LIBRARY_NAME}/include)
|
||||
|
||||
set(libhandy_LIBRARIES ${libhandy_LIBRARY})
|
||||
set(libhandy_INCLUDE_DIRS ${libhandy_INCLUDE_DIR};${libhandy_CONFIG_INCLUDE_DIRS})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(libhandy
|
||||
REQUIRED_VARS
|
||||
libhandy_LIBRARY
|
||||
libhandy_INCLUDE_DIR
|
||||
VERSION_VAR libhandy_VERSION)
|
||||
|
||||
mark_as_advanced(libhandy_INCLUDE_DIR libhandy_LIBRARY)
|
||||
@@ -44,7 +44,7 @@ has to be separate to allow main.css to override certain things
|
||||
background: @secondary_color;
|
||||
}
|
||||
|
||||
.app-popup list {
|
||||
.app-window list, .app-popup list {
|
||||
background: @secondary_color;
|
||||
}
|
||||
|
||||
@@ -87,3 +87,11 @@ has to be separate to allow main.css to override certain things
|
||||
.app-window colorswatch {
|
||||
box-shadow: 0 1px rgba(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
.app-window scale {
|
||||
padding-top: 0px;
|
||||
padding-bottom: 0px;
|
||||
margin-top: 0px;
|
||||
margin-bottom: 0px;
|
||||
color: @text_color;
|
||||
}
|
||||
|
||||
100
res/css/main.css
100
res/css/main.css
@@ -101,17 +101,38 @@
|
||||
.message-input, .message-input textview, .message-input textview text {
|
||||
background-color: #242424;
|
||||
color: #adadad;
|
||||
border-radius: 15px;
|
||||
border-radius: 3px;
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
|
||||
.message-input {
|
||||
border: 1px solid #444444;
|
||||
margin-right: 15px;
|
||||
}
|
||||
|
||||
.message-input.replying {
|
||||
border: 1px solid #026FB9;
|
||||
}
|
||||
|
||||
.message-input {
|
||||
.message-input.bad-input {
|
||||
border: 1px solid #dd3300;
|
||||
}
|
||||
|
||||
.message-input-browse-icon {
|
||||
color: #b9bbbe;
|
||||
margin-left: 5px;
|
||||
margin-top: 11px;
|
||||
}
|
||||
|
||||
/* i dont think theres a way to circumvent having to do this to adjust around the browse icon */
|
||||
.message-input:not(.with-browser-icon) {
|
||||
padding: 0px 0px 0px 5px;
|
||||
}
|
||||
|
||||
.message-input.with-browse-icon {
|
||||
padding: 0px 0px 0px 30px;
|
||||
}
|
||||
|
||||
.members {
|
||||
background-color: @background_color;
|
||||
}
|
||||
@@ -282,3 +303,78 @@
|
||||
.friends-list-row-bot {
|
||||
color: #ff0000;
|
||||
}
|
||||
|
||||
.channel-tab-switcher .box {
|
||||
margin: -7px -1px -7px -1px;
|
||||
background: #2a2a2a;
|
||||
border: 1px solid black;
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab:hover {
|
||||
box-shadow: inset 0 -6px #17633e;
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab:checked {
|
||||
box-shadow: inset 0 -6px #2feb90;
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab {
|
||||
background: #1A1A1A;
|
||||
border: 1px solid #808080;
|
||||
min-height: 35px;
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab.needs-attention:not(:checked) {
|
||||
font-weight: bold;
|
||||
animation: 150ms ease-in;
|
||||
/* background-image: radial-gradient(ellipse at bottom, #FF5370, #1A1A1A 30%); */
|
||||
box-shadow: inset 0 -6px red;
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab > button {
|
||||
border: none;
|
||||
padding: 0;
|
||||
margin: 5px;
|
||||
min-width: 16px;
|
||||
min-height: 16px;
|
||||
color: #FF5370;
|
||||
background-color: rgba(0.21, 0.21, 0.21, 0.5);
|
||||
}
|
||||
|
||||
.channel-tab-switcher tab > button:hover {
|
||||
background-color: alpha(#ff0000, 0.5);
|
||||
}
|
||||
|
||||
.message-progress {
|
||||
border: none;
|
||||
margin-bottom: -8px;
|
||||
}
|
||||
|
||||
.message-progress trough {
|
||||
border: none;
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.message-progress progress {
|
||||
border: none;
|
||||
background-color: #dd3300;
|
||||
margin-left: 1px;
|
||||
}
|
||||
|
||||
.voice-info {
|
||||
background-color: #0B0B0B;
|
||||
padding: 5px;
|
||||
border: 1px solid #202020;
|
||||
}
|
||||
|
||||
.voice-info-disconnect-image {
|
||||
color: #DDDDDD;
|
||||
}
|
||||
|
||||
.voice-info-status {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.voice-info-location {
|
||||
|
||||
}
|
||||
|
||||
469
src/abaddon.cpp
469
src/abaddon.cpp
@@ -1,21 +1,31 @@
|
||||
#include <gtkmm.h>
|
||||
#include <memory>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/cfg/env.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "platform.hpp"
|
||||
#include "audio/manager.hpp"
|
||||
#include "discord/discord.hpp"
|
||||
#include "dialogs/token.hpp"
|
||||
#include "dialogs/editmessage.hpp"
|
||||
#include "dialogs/joinguild.hpp"
|
||||
#include "dialogs/confirm.hpp"
|
||||
#include "dialogs/setstatus.hpp"
|
||||
#include "dialogs/friendpicker.hpp"
|
||||
#include "dialogs/verificationgate.hpp"
|
||||
#include "dialogs/textinput.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "windows/guildsettingswindow.hpp"
|
||||
#include "windows/profilewindow.hpp"
|
||||
#include "windows/pinnedwindow.hpp"
|
||||
#include "windows/threadswindow.hpp"
|
||||
#include "windows/voicewindow.hpp"
|
||||
#include "startup.hpp"
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
#include <handy.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma comment(lib, "crypt32.lib")
|
||||
@@ -31,7 +41,8 @@ Abaddon::Abaddon()
|
||||
std::string ua = GetSettings().UserAgent;
|
||||
m_discord.SetUserAgent(ua);
|
||||
|
||||
m_discord.signal_gateway_ready().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady));
|
||||
// todo rename funcs
|
||||
m_discord.signal_gateway_ready_supplemental().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady));
|
||||
m_discord.signal_message_create().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageCreate));
|
||||
m_discord.signal_message_delete().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageDelete));
|
||||
m_discord.signal_message_update().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageUpdate));
|
||||
@@ -43,6 +54,20 @@ Abaddon::Abaddon()
|
||||
m_discord.signal_thread_update().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnThreadUpdate));
|
||||
m_discord.signal_message_sent().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageSent));
|
||||
m_discord.signal_disconnected().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnDisconnect));
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
m_discord.signal_voice_connected().connect(sigc::mem_fun(*this, &Abaddon::OnVoiceConnected));
|
||||
m_discord.signal_voice_disconnected().connect(sigc::mem_fun(*this, &Abaddon::OnVoiceDisconnected));
|
||||
m_discord.signal_voice_speaking().connect([this](const VoiceSpeakingData &m) {
|
||||
printf("%llu has ssrc %u\n", (uint64_t)m.UserID, m.SSRC);
|
||||
m_audio->AddSSRC(m.SSRC);
|
||||
});
|
||||
#endif
|
||||
|
||||
m_discord.signal_channel_accessibility_changed().connect([this](Snowflake id, bool accessible) {
|
||||
if (!accessible)
|
||||
m_channels_requested.erase(id);
|
||||
});
|
||||
if (GetSettings().Prefetch)
|
||||
m_discord.signal_message_create().connect([this](const Message &message) {
|
||||
if (message.Author.HasAvatar())
|
||||
@@ -59,27 +84,147 @@ Abaddon &Abaddon::Get() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
constexpr static guint BUTTON_BACK = 4;
|
||||
constexpr static guint BUTTON_FORWARD = 5;
|
||||
#else
|
||||
constexpr static guint BUTTON_BACK = 8;
|
||||
constexpr static guint BUTTON_FORWARD = 9;
|
||||
#endif
|
||||
|
||||
static bool HandleButtonEvents(GdkEvent *event, MainWindow *main_window) {
|
||||
if (event->type != GDK_BUTTON_PRESS) return false;
|
||||
|
||||
auto *widget = gtk_get_event_widget(event);
|
||||
if (widget == nullptr) return false;
|
||||
auto *window = gtk_widget_get_toplevel(widget);
|
||||
if (static_cast<void *>(window) != static_cast<void *>(main_window->gobj())) return false; // is this the right way???
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
switch (event->button.button) {
|
||||
case BUTTON_BACK:
|
||||
main_window->GoBack();
|
||||
break;
|
||||
case BUTTON_FORWARD:
|
||||
main_window->GoForward();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool HandleKeyEvents(GdkEvent *event, MainWindow *main_window) {
|
||||
if (event->type != GDK_KEY_PRESS) return false;
|
||||
|
||||
auto *widget = gtk_get_event_widget(event);
|
||||
if (widget == nullptr) return false;
|
||||
auto *window = gtk_widget_get_toplevel(widget);
|
||||
if (static_cast<void *>(window) != static_cast<void *>(main_window->gobj())) return false;
|
||||
|
||||
const bool ctrl = (event->key.state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK;
|
||||
const bool shft = (event->key.state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK;
|
||||
|
||||
constexpr static guint EXCLUDE_STATES = GDK_CONTROL_MASK | GDK_SHIFT_MASK;
|
||||
|
||||
if (!(event->key.state & EXCLUDE_STATES) && event->key.keyval == GDK_KEY_Alt_L) {
|
||||
if (Abaddon::Get().GetSettings().AltMenu) {
|
||||
main_window->ToggleMenuVisibility();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
if (ctrl) {
|
||||
switch (event->key.keyval) {
|
||||
case GDK_KEY_Tab:
|
||||
case GDK_KEY_KP_Tab:
|
||||
case GDK_KEY_ISO_Left_Tab:
|
||||
if (shft)
|
||||
main_window->GoToPreviousTab();
|
||||
else
|
||||
main_window->GoToNextTab();
|
||||
return true;
|
||||
case GDK_KEY_1:
|
||||
case GDK_KEY_2:
|
||||
case GDK_KEY_3:
|
||||
case GDK_KEY_4:
|
||||
case GDK_KEY_5:
|
||||
case GDK_KEY_6:
|
||||
case GDK_KEY_7:
|
||||
case GDK_KEY_8:
|
||||
case GDK_KEY_9:
|
||||
main_window->GoToTab(event->key.keyval - GDK_KEY_1);
|
||||
return true;
|
||||
case GDK_KEY_0:
|
||||
main_window->GoToTab(9);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void MainEventHandler(GdkEvent *event, void *main_window) {
|
||||
if (HandleButtonEvents(event, static_cast<MainWindow *>(main_window))) return;
|
||||
if (HandleKeyEvents(event, static_cast<MainWindow *>(main_window))) return;
|
||||
gtk_main_do_event(event);
|
||||
}
|
||||
|
||||
int Abaddon::StartGTK() {
|
||||
m_gtk_app = Gtk::Application::create("com.github.uowuo.abaddon");
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_gtk_app->signal_activate().connect([] {
|
||||
hdy_init();
|
||||
});
|
||||
#endif
|
||||
|
||||
m_css_provider = Gtk::CssProvider::create();
|
||||
m_css_provider->signal_parsing_error().connect([this](const Glib::RefPtr<const Gtk::CssSection> §ion, const Glib::Error &error) {
|
||||
Gtk::MessageDialog dlg(*m_main_window, "css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
m_css_provider->signal_parsing_error().connect([](const Glib::RefPtr<const Gtk::CssSection> §ion, const Glib::Error &error) {
|
||||
Gtk::MessageDialog dlg("css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
dlg.run();
|
||||
});
|
||||
|
||||
m_css_low_provider = Gtk::CssProvider::create();
|
||||
m_css_low_provider->signal_parsing_error().connect([this](const Glib::RefPtr<const Gtk::CssSection> §ion, const Glib::Error &error) {
|
||||
Gtk::MessageDialog dlg(*m_main_window, "low-priority css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
m_css_low_provider->signal_parsing_error().connect([](const Glib::RefPtr<const Gtk::CssSection> §ion, const Glib::Error &error) {
|
||||
Gtk::MessageDialog dlg("low-priority css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
dlg.run();
|
||||
});
|
||||
|
||||
#ifdef _WIN32
|
||||
bool png_found = false;
|
||||
bool gif_found = false;
|
||||
for (const auto &fmt : Gdk::Pixbuf::get_formats()) {
|
||||
if (fmt.get_name() == "png")
|
||||
png_found = true;
|
||||
else if (fmt.get_name() == "gif")
|
||||
gif_found = true;
|
||||
}
|
||||
|
||||
if (!png_found) {
|
||||
Gtk::MessageDialog dlg("The PNG pixbufloader wasn't detected. Abaddon may not work as a result.", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
dlg.run();
|
||||
}
|
||||
|
||||
if (!gif_found) {
|
||||
Gtk::MessageDialog dlg("The GIF pixbufloader wasn't detected. Animations may not display as a result.", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
dlg.run();
|
||||
}
|
||||
#endif
|
||||
|
||||
m_main_window = std::make_unique<MainWindow>();
|
||||
m_main_window->set_title(APP_TITLE);
|
||||
m_main_window->set_position(Gtk::WIN_POS_CENTER);
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
gdk_event_handler_set(&MainEventHandler, m_main_window.get(), nullptr);
|
||||
#endif
|
||||
|
||||
if (!m_settings.IsValid()) {
|
||||
Gtk::MessageDialog dlg(*m_main_window, "The settings file could not be opened!", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
@@ -99,6 +244,16 @@ int Abaddon::StartGTK() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
m_audio = std::make_unique<AudioManager>();
|
||||
if (!m_audio->OK()) {
|
||||
Gtk::MessageDialog dlg(*m_main_window, "The audio engine could not be initialized!", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
dlg.run();
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
// store must be checked before this can be called
|
||||
m_main_window->UpdateComponents();
|
||||
|
||||
@@ -109,16 +264,20 @@ int Abaddon::StartGTK() {
|
||||
m_main_window->signal_action_disconnect().connect(sigc::mem_fun(*this, &Abaddon::ActionDisconnect));
|
||||
m_main_window->signal_action_set_token().connect(sigc::mem_fun(*this, &Abaddon::ActionSetToken));
|
||||
m_main_window->signal_action_reload_css().connect(sigc::mem_fun(*this, &Abaddon::ActionReloadCSS));
|
||||
m_main_window->signal_action_join_guild().connect(sigc::mem_fun(*this, &Abaddon::ActionJoinGuildDialog));
|
||||
m_main_window->signal_action_set_status().connect(sigc::mem_fun(*this, &Abaddon::ActionSetStatus));
|
||||
m_main_window->signal_action_add_recipient().connect(sigc::mem_fun(*this, &Abaddon::ActionAddRecipient));
|
||||
m_main_window->signal_action_view_pins().connect(sigc::mem_fun(*this, &Abaddon::ActionViewPins));
|
||||
m_main_window->signal_action_view_threads().connect(sigc::mem_fun(*this, &Abaddon::ActionViewThreads));
|
||||
|
||||
m_main_window->GetChannelList()->signal_action_channel_item_select().connect(sigc::mem_fun(*this, &Abaddon::ActionChannelOpened));
|
||||
m_main_window->GetChannelList()->signal_action_channel_item_select().connect(sigc::bind(sigc::mem_fun(*this, &Abaddon::ActionChannelOpened), true));
|
||||
m_main_window->GetChannelList()->signal_action_guild_leave().connect(sigc::mem_fun(*this, &Abaddon::ActionLeaveGuild));
|
||||
m_main_window->GetChannelList()->signal_action_guild_settings().connect(sigc::mem_fun(*this, &Abaddon::ActionGuildSettings));
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
m_main_window->GetChannelList()->signal_action_join_voice_channel().connect(sigc::mem_fun(*this, &Abaddon::ActionJoinVoiceChannel));
|
||||
m_main_window->GetChannelList()->signal_action_disconnect_voice().connect(sigc::mem_fun(*this, &Abaddon::ActionDisconnectVoice));
|
||||
#endif
|
||||
|
||||
m_main_window->GetChatWindow()->signal_action_message_edit().connect(sigc::mem_fun(*this, &Abaddon::ActionChatEditMessage));
|
||||
m_main_window->GetChatWindow()->signal_action_chat_submit().connect(sigc::mem_fun(*this, &Abaddon::ActionChatInputSubmit));
|
||||
m_main_window->GetChatWindow()->signal_action_chat_load_history().connect(sigc::mem_fun(*this, &Abaddon::ActionChatLoadHistory));
|
||||
@@ -128,10 +287,29 @@ int Abaddon::StartGTK() {
|
||||
m_main_window->GetChatWindow()->signal_action_reaction_remove().connect(sigc::mem_fun(*this, &Abaddon::ActionReactionRemove));
|
||||
|
||||
ActionReloadCSS();
|
||||
if (m_settings.GetSettings().HideToTray) {
|
||||
m_tray = Gtk::StatusIcon::create("discord");
|
||||
m_tray->signal_activate().connect(sigc::mem_fun(*this, &Abaddon::on_tray_click));
|
||||
m_tray->signal_popup_menu().connect(sigc::mem_fun(*this, &Abaddon::on_tray_popup_menu));
|
||||
}
|
||||
m_tray_menu = Gtk::make_managed<Gtk::Menu>();
|
||||
m_tray_exit = Gtk::make_managed<Gtk::MenuItem>("Quit", false);
|
||||
|
||||
m_tray_exit->signal_activate().connect(sigc::mem_fun(*this, &Abaddon::on_tray_menu_click));
|
||||
|
||||
m_tray_menu->append(*m_tray_exit);
|
||||
m_tray_menu->show_all();
|
||||
|
||||
m_main_window->signal_hide().connect(sigc::mem_fun(*this, &Abaddon::on_window_hide));
|
||||
m_gtk_app->signal_shutdown().connect(sigc::mem_fun(*this, &Abaddon::OnShutdown), false);
|
||||
|
||||
m_main_window->UpdateMenus();
|
||||
|
||||
m_gtk_app->hold();
|
||||
m_main_window->show();
|
||||
|
||||
RunFirstTimeDiscordStartup();
|
||||
|
||||
return m_gtk_app->run(*m_main_window);
|
||||
}
|
||||
|
||||
@@ -142,7 +320,7 @@ void Abaddon::OnShutdown() {
|
||||
|
||||
void Abaddon::LoadFromSettings() {
|
||||
std::string token = GetSettings().DiscordToken;
|
||||
if (token.size()) {
|
||||
if (!token.empty()) {
|
||||
m_discord_token = token;
|
||||
m_discord.UpdateToken(m_discord_token);
|
||||
}
|
||||
@@ -150,11 +328,13 @@ void Abaddon::LoadFromSettings() {
|
||||
|
||||
void Abaddon::StartDiscord() {
|
||||
m_discord.Start();
|
||||
m_main_window->UpdateMenus();
|
||||
}
|
||||
|
||||
void Abaddon::StopDiscord() {
|
||||
m_discord.Stop();
|
||||
SaveState();
|
||||
if (m_discord.Stop())
|
||||
SaveState();
|
||||
m_main_window->UpdateMenus();
|
||||
}
|
||||
|
||||
bool Abaddon::IsDiscordActive() const {
|
||||
@@ -249,6 +429,64 @@ void Abaddon::DiscordOnThreadUpdate(const ThreadUpdateData &data) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void Abaddon::OnVoiceConnected() {
|
||||
m_audio->StartCaptureDevice();
|
||||
|
||||
auto *wnd = new VoiceWindow(m_discord.GetVoiceChannelID());
|
||||
m_voice_window = wnd;
|
||||
|
||||
wnd->signal_mute().connect([this](bool is_mute) {
|
||||
m_discord.SetVoiceMuted(is_mute);
|
||||
m_audio->SetCapture(!is_mute);
|
||||
});
|
||||
|
||||
wnd->signal_deafen().connect([this](bool is_deaf) {
|
||||
m_discord.SetVoiceDeafened(is_deaf);
|
||||
m_audio->SetPlayback(!is_deaf);
|
||||
});
|
||||
|
||||
wnd->signal_gate().connect([this](double gate) {
|
||||
m_audio->SetCaptureGate(gate);
|
||||
});
|
||||
|
||||
wnd->signal_gain().connect([this](double gain) {
|
||||
m_audio->SetCaptureGain(gain);
|
||||
});
|
||||
|
||||
wnd->signal_mute_user_cs().connect([this](Snowflake id, bool is_mute) {
|
||||
if (const auto ssrc = m_discord.GetSSRCOfUser(id); ssrc.has_value()) {
|
||||
m_audio->SetMuteSSRC(*ssrc, is_mute);
|
||||
}
|
||||
});
|
||||
|
||||
wnd->signal_user_volume_changed().connect([this](Snowflake id, double volume) {
|
||||
if (const auto ssrc = m_discord.GetSSRCOfUser(id); ssrc.has_value()) {
|
||||
m_audio->SetVolumeSSRC(*ssrc, volume);
|
||||
}
|
||||
});
|
||||
|
||||
wnd->set_position(Gtk::WIN_POS_CENTER);
|
||||
|
||||
wnd->show();
|
||||
wnd->signal_hide().connect([this, wnd]() {
|
||||
m_discord.DisconnectFromVoice();
|
||||
m_voice_window = nullptr;
|
||||
delete wnd;
|
||||
delete m_user_menu;
|
||||
SetupUserMenu();
|
||||
});
|
||||
}
|
||||
|
||||
void Abaddon::OnVoiceDisconnected() {
|
||||
m_audio->StopCaptureDevice();
|
||||
m_audio->RemoveAllSSRCs();
|
||||
if (m_voice_window != nullptr) {
|
||||
m_voice_window->close();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
SettingsManager::Settings &Abaddon::GetSettings() {
|
||||
return m_settings.GetSettings();
|
||||
}
|
||||
@@ -267,9 +505,10 @@ void Abaddon::ShowUserMenu(const GdkEvent *event, Snowflake id, Snowflake guild_
|
||||
|
||||
for (const auto child : m_user_menu_roles_submenu->get_children())
|
||||
delete child;
|
||||
|
||||
if (guild.has_value() && user.has_value()) {
|
||||
const auto roles = user->GetSortedRoles();
|
||||
m_user_menu_roles->set_visible(roles.size() > 0);
|
||||
m_user_menu_roles->set_visible(!roles.empty());
|
||||
for (const auto &role : roles) {
|
||||
auto *item = Gtk::manage(new Gtk::MenuItem(role.Name));
|
||||
if (role.Color != 0) {
|
||||
@@ -289,7 +528,7 @@ void Abaddon::ShowUserMenu(const GdkEvent *event, Snowflake id, Snowflake guild_
|
||||
if (me == id) {
|
||||
m_user_menu_ban->set_visible(false);
|
||||
m_user_menu_kick->set_visible(false);
|
||||
m_user_menu_open_dm->set_visible(false);
|
||||
m_user_menu_open_dm->set_sensitive(false);
|
||||
} else {
|
||||
const bool has_kick = m_discord.HasGuildPermission(me, guild_id, Permission::KICK_MEMBERS);
|
||||
const bool has_ban = m_discord.HasGuildPermission(me, guild_id, Permission::BAN_MEMBERS);
|
||||
@@ -297,7 +536,7 @@ void Abaddon::ShowUserMenu(const GdkEvent *event, Snowflake id, Snowflake guild_
|
||||
|
||||
m_user_menu_kick->set_visible(has_kick && can_manage);
|
||||
m_user_menu_ban->set_visible(has_ban && can_manage);
|
||||
m_user_menu_open_dm->set_visible(true);
|
||||
m_user_menu_open_dm->set_sensitive(m_discord.FindDM(id).has_value());
|
||||
}
|
||||
|
||||
m_user_menu_remove_recipient->hide();
|
||||
@@ -311,6 +550,48 @@ void Abaddon::ShowUserMenu(const GdkEvent *event, Snowflake id, Snowflake guild_
|
||||
m_user_menu->popup_at_pointer(event);
|
||||
}
|
||||
|
||||
void Abaddon::RunFirstTimeDiscordStartup() {
|
||||
DiscordStartupDialog dlg(*m_main_window);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
|
||||
std::optional<std::string> cookie;
|
||||
std::optional<uint32_t> build_number;
|
||||
|
||||
dlg.signal_response().connect([&](int response) {
|
||||
if (response == Gtk::RESPONSE_OK) {
|
||||
cookie = dlg.GetCookie();
|
||||
build_number = dlg.GetBuildNumber();
|
||||
}
|
||||
});
|
||||
|
||||
dlg.run();
|
||||
|
||||
Glib::signal_idle().connect_once([this, cookie, build_number]() {
|
||||
if (cookie.has_value()) {
|
||||
m_discord.SetCookie(*cookie);
|
||||
} else {
|
||||
ConfirmDialog confirm(*m_main_window);
|
||||
confirm.SetConfirmText("Cookies could not be fetched. This may increase your chances of being flagged by Discord's anti-spam");
|
||||
confirm.SetAcceptOnly(true);
|
||||
confirm.run();
|
||||
}
|
||||
|
||||
if (build_number.has_value()) {
|
||||
m_discord.SetBuildNumber(*build_number);
|
||||
} else {
|
||||
ConfirmDialog confirm(*m_main_window);
|
||||
confirm.SetConfirmText("Build number could not be fetched. This may increase your chances of being flagged by Discord's anti-spam");
|
||||
confirm.SetAcceptOnly(true);
|
||||
confirm.run();
|
||||
}
|
||||
|
||||
// autoconnect
|
||||
if (cookie.has_value() && build_number.has_value() && GetSettings().Autoconnect && !GetDiscordToken().empty()) {
|
||||
ActionConnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Abaddon::ShowGuildVerificationGateDialog(Snowflake guild_id) {
|
||||
VerificationGateDialog dlg(*m_main_window, guild_id);
|
||||
if (dlg.run() == Gtk::RESPONSE_OK) {
|
||||
@@ -325,13 +606,27 @@ void Abaddon::ShowGuildVerificationGateDialog(Snowflake guild_id) {
|
||||
}
|
||||
}
|
||||
|
||||
void Abaddon::CheckMessagesForMembers(const ChannelData &chan, const std::vector<Message> &msgs) {
|
||||
if (!chan.GuildID.has_value()) return;
|
||||
|
||||
std::vector<Snowflake> unknown;
|
||||
std::transform(msgs.begin(), msgs.end(),
|
||||
std::back_inserter(unknown),
|
||||
[](const Message &msg) -> Snowflake {
|
||||
return msg.Author.ID;
|
||||
});
|
||||
|
||||
const auto fetch = m_discord.FilterUnknownMembersFrom(*chan.GuildID, unknown.begin(), unknown.end());
|
||||
m_discord.RequestMembers(*chan.GuildID, fetch.begin(), fetch.end());
|
||||
}
|
||||
|
||||
void Abaddon::SetupUserMenu() {
|
||||
m_user_menu = Gtk::manage(new Gtk::Menu);
|
||||
m_user_menu_insert_mention = Gtk::manage(new Gtk::MenuItem("Insert Mention"));
|
||||
m_user_menu_ban = Gtk::manage(new Gtk::MenuItem("Ban"));
|
||||
m_user_menu_kick = Gtk::manage(new Gtk::MenuItem("Kick"));
|
||||
m_user_menu_copy_id = Gtk::manage(new Gtk::MenuItem("Copy ID"));
|
||||
m_user_menu_open_dm = Gtk::manage(new Gtk::MenuItem("Open DM"));
|
||||
m_user_menu_open_dm = Gtk::manage(new Gtk::MenuItem("Go to DM"));
|
||||
m_user_menu_roles = Gtk::manage(new Gtk::MenuItem("Roles"));
|
||||
m_user_menu_info = Gtk::manage(new Gtk::MenuItem("View Profile"));
|
||||
m_user_menu_remove_recipient = Gtk::manage(new Gtk::MenuItem("Remove From Group"));
|
||||
@@ -373,6 +668,9 @@ void Abaddon::SaveState() {
|
||||
AbaddonApplicationState state;
|
||||
state.ActiveChannel = m_main_window->GetChatActiveChannel();
|
||||
state.Expansion = m_main_window->GetChannelList()->GetExpansionState();
|
||||
#ifdef WITH_LIBHANDY
|
||||
state.Tabs = m_main_window->GetChatWindow()->GetTabsState();
|
||||
#endif
|
||||
|
||||
const auto path = GetStateCachePath();
|
||||
if (!util::IsFolder(path)) {
|
||||
@@ -380,7 +678,8 @@ void Abaddon::SaveState() {
|
||||
std::filesystem::create_directories(path, ec);
|
||||
}
|
||||
|
||||
auto *fp = std::fopen(GetStateCachePath("/state.json").c_str(), "wb");
|
||||
auto file_name = "/" + std::to_string(m_discord.GetUserData().ID) + ".json";
|
||||
auto *fp = std::fopen(GetStateCachePath(file_name).c_str(), "wb");
|
||||
if (fp == nullptr) return;
|
||||
const auto s = nlohmann::json(state).dump(4);
|
||||
std::fwrite(s.c_str(), 1, s.size(), fp);
|
||||
@@ -394,12 +693,16 @@ void Abaddon::LoadState() {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto data = ReadWholeFile(GetStateCachePath("/state.json"));
|
||||
auto file_name = "/" + std::to_string(m_discord.GetUserData().ID) + ".json";
|
||||
const auto data = ReadWholeFile(GetStateCachePath(file_name));
|
||||
if (data.empty()) return;
|
||||
try {
|
||||
AbaddonApplicationState state = nlohmann::json::parse(data.begin(), data.end());
|
||||
m_main_window->GetChannelList()->UseExpansionState(state.Expansion);
|
||||
ActionChannelOpened(state.ActiveChannel);
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_main_window->GetChatWindow()->UseTabsState(state.Tabs);
|
||||
#endif
|
||||
ActionChannelOpened(state.ActiveChannel, false);
|
||||
} catch (const std::exception &e) {
|
||||
printf("failed to load application state: %s\n", e.what());
|
||||
}
|
||||
@@ -434,18 +737,9 @@ void Abaddon::on_user_menu_copy_id() {
|
||||
|
||||
void Abaddon::on_user_menu_open_dm() {
|
||||
const auto existing = m_discord.FindDM(m_shown_user_menu_id);
|
||||
if (existing.has_value())
|
||||
if (existing.has_value()) {
|
||||
ActionChannelOpened(*existing);
|
||||
else
|
||||
m_discord.CreateDM(m_shown_user_menu_id, [this](DiscordError code, Snowflake channel_id) {
|
||||
if (code == DiscordError::NONE) {
|
||||
// give the gateway a little window to send CHANNEL_CREATE
|
||||
auto cb = [this, channel_id] {
|
||||
ActionChannelOpened(channel_id);
|
||||
};
|
||||
Glib::signal_timeout().connect_once(sigc::track_obj(cb, *this), 200);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Abaddon::on_user_menu_remove_recipient() {
|
||||
@@ -498,24 +792,25 @@ void Abaddon::ActionSetToken() {
|
||||
m_main_window->UpdateComponents();
|
||||
GetSettings().DiscordToken = m_discord_token;
|
||||
}
|
||||
m_main_window->UpdateMenus();
|
||||
}
|
||||
|
||||
void Abaddon::ActionJoinGuildDialog() {
|
||||
JoinGuildDialog dlg(*m_main_window);
|
||||
auto response = dlg.run();
|
||||
if (response == Gtk::RESPONSE_OK) {
|
||||
auto code = dlg.GetCode();
|
||||
m_discord.JoinGuild(code);
|
||||
void Abaddon::ActionChannelOpened(Snowflake id, bool expand_to) {
|
||||
if (!id.IsValid()) {
|
||||
m_discord.SetReferringChannel(Snowflake::Invalid);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Abaddon::ActionChannelOpened(Snowflake id) {
|
||||
if (!id.IsValid() || id == m_main_window->GetChatActiveChannel()) return;
|
||||
if (id == m_main_window->GetChatActiveChannel()) return;
|
||||
|
||||
m_main_window->GetChatWindow()->SetTopic("");
|
||||
|
||||
const auto channel = m_discord.GetChannel(id);
|
||||
if (!channel.has_value()) return;
|
||||
if (!channel.has_value()) {
|
||||
m_discord.SetReferringChannel(Snowflake::Invalid);
|
||||
m_main_window->UpdateChatActiveChannel(Snowflake::Invalid, false);
|
||||
m_main_window->UpdateChatWindowContents();
|
||||
return;
|
||||
}
|
||||
|
||||
const bool can_access = channel->IsDM() || m_discord.HasChannelPermission(m_discord.GetUserData().ID, id, Permission::VIEW_CHANNEL);
|
||||
|
||||
@@ -532,11 +827,12 @@ void Abaddon::ActionChannelOpened(Snowflake id) {
|
||||
display = "Empty group";
|
||||
m_main_window->set_title(std::string(APP_TITLE) + " - " + display);
|
||||
}
|
||||
m_main_window->UpdateChatActiveChannel(id);
|
||||
m_main_window->UpdateChatActiveChannel(id, expand_to);
|
||||
if (m_channels_requested.find(id) == m_channels_requested.end()) {
|
||||
// dont fire requests we know will fail
|
||||
if (can_access) {
|
||||
m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Message> &msgs) {
|
||||
m_discord.FetchMessagesInChannel(id, [channel, this, id](const std::vector<Message> &msgs) {
|
||||
CheckMessagesForMembers(*channel, msgs);
|
||||
m_main_window->UpdateChatWindowContents();
|
||||
m_channels_requested.insert(id);
|
||||
});
|
||||
@@ -557,6 +853,9 @@ void Abaddon::ActionChannelOpened(Snowflake id) {
|
||||
ShowGuildVerificationGateDialog(*channel->GuildID);
|
||||
}
|
||||
}
|
||||
|
||||
m_main_window->UpdateMenus();
|
||||
m_discord.SetReferringChannel(id);
|
||||
}
|
||||
|
||||
void Abaddon::ActionChatLoadHistory(Snowflake id) {
|
||||
@@ -579,7 +878,11 @@ void Abaddon::ActionChatLoadHistory(Snowflake id) {
|
||||
m_discord.FetchMessagesInChannelBefore(id, before_id, [this, id](const std::vector<Message> &msgs) {
|
||||
m_channels_history_loading.erase(id);
|
||||
|
||||
if (msgs.size() == 0) {
|
||||
const auto channel = m_discord.GetChannel(id);
|
||||
if (channel.has_value())
|
||||
CheckMessagesForMembers(*channel, msgs);
|
||||
|
||||
if (msgs.empty()) {
|
||||
m_channels_history_loaded.insert(id);
|
||||
} else {
|
||||
m_main_window->UpdateChatPrependHistory(msgs);
|
||||
@@ -587,13 +890,13 @@ void Abaddon::ActionChatLoadHistory(Snowflake id) {
|
||||
});
|
||||
}
|
||||
|
||||
void Abaddon::ActionChatInputSubmit(std::string msg, Snowflake channel, Snowflake referenced_message) {
|
||||
if (msg.substr(0, 7) == "/shrug " || msg == "/shrug")
|
||||
msg = msg.substr(6) + "\xC2\xAF\x5C\x5F\x28\xE3\x83\x84\x29\x5F\x2F\xC2\xAF"; // this is important
|
||||
if (referenced_message.IsValid())
|
||||
m_discord.SendChatMessage(msg, channel, referenced_message);
|
||||
else
|
||||
m_discord.SendChatMessage(msg, channel);
|
||||
void Abaddon::ActionChatInputSubmit(ChatSubmitParams data) {
|
||||
if (data.Message.substr(0, 7) == "/shrug " || data.Message == "/shrug")
|
||||
data.Message = data.Message.substr(6) + "\xC2\xAF\x5C\x5F\x28\xE3\x83\x84\x29\x5F\x2F\xC2\xAF"; // this is important
|
||||
|
||||
if (!m_discord.HasChannelPermission(m_discord.GetUserData().ID, data.ChannelID, Permission::VIEW_CHANNEL)) return;
|
||||
|
||||
m_discord.SendChatMessage(data, NOOP_CALLBACK);
|
||||
}
|
||||
|
||||
void Abaddon::ActionChatEditMessage(Snowflake channel_id, Snowflake id) {
|
||||
@@ -649,7 +952,7 @@ void Abaddon::ActionSetStatus() {
|
||||
const auto status = dlg.GetStatusType();
|
||||
const auto activity_type = dlg.GetActivityType();
|
||||
const auto activity_name = dlg.GetActivityName();
|
||||
if (activity_name == "") {
|
||||
if (activity_name.empty()) {
|
||||
m_discord.UpdateStatus(status, false);
|
||||
} else {
|
||||
ActivityData activity;
|
||||
@@ -702,6 +1005,25 @@ void Abaddon::ActionViewThreads(Snowflake channel_id) {
|
||||
window->show();
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void Abaddon::ActionJoinVoiceChannel(Snowflake channel_id) {
|
||||
m_discord.ConnectToVoice(channel_id);
|
||||
}
|
||||
|
||||
void Abaddon::ActionDisconnectVoice() {
|
||||
m_discord.DisconnectFromVoice();
|
||||
}
|
||||
#endif
|
||||
|
||||
std::optional<Glib::ustring> Abaddon::ShowTextPrompt(const Glib::ustring &prompt, const Glib::ustring &title, const Glib::ustring &placeholder, Gtk::Window *window) {
|
||||
TextInputDialog dlg(prompt, title, placeholder, window != nullptr ? *window : *m_main_window);
|
||||
const auto code = dlg.run();
|
||||
if (code == Gtk::RESPONSE_OK)
|
||||
return dlg.GetInput();
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
bool Abaddon::ShowConfirm(const Glib::ustring &prompt, Gtk::Window *window) {
|
||||
ConfirmDialog dlg(window != nullptr ? *window : *m_main_window);
|
||||
dlg.SetConfirmText(prompt);
|
||||
@@ -732,15 +1054,60 @@ EmojiResource &Abaddon::GetEmojis() {
|
||||
return m_emojis;
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
AudioManager &Abaddon::GetAudio() {
|
||||
return *m_audio;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Abaddon::on_tray_click() {
|
||||
m_main_window->set_visible(!m_main_window->is_visible());
|
||||
}
|
||||
|
||||
void Abaddon::on_tray_menu_click() {
|
||||
m_gtk_app->quit();
|
||||
}
|
||||
|
||||
void Abaddon::on_tray_popup_menu(int button, int activate_time) {
|
||||
m_tray->popup_menu_at_position(*m_tray_menu, button, activate_time);
|
||||
}
|
||||
|
||||
void Abaddon::on_window_hide() {
|
||||
if (!m_settings.GetSettings().HideToTray) {
|
||||
m_gtk_app->quit();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (std::getenv("ABADDON_NO_FC") == nullptr)
|
||||
Platform::SetupFonts();
|
||||
|
||||
char *systemLocale = std::setlocale(LC_ALL, "");
|
||||
try {
|
||||
if (systemLocale != nullptr) {
|
||||
std::locale::global(std::locale(systemLocale));
|
||||
}
|
||||
} catch (...) {
|
||||
try {
|
||||
std::locale::global(std::locale::classic());
|
||||
if (systemLocale != nullptr) {
|
||||
std::setlocale(LC_ALL, systemLocale);
|
||||
}
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && defined(_MSC_VER)
|
||||
TCHAR buf[2] { 0 };
|
||||
GetEnvironmentVariableA("GTK_CSD", buf, sizeof(buf));
|
||||
if (buf[0] != '1')
|
||||
SetEnvironmentVariableA("GTK_CSD", "0");
|
||||
#endif
|
||||
|
||||
spdlog::cfg::load_env_levels();
|
||||
auto log_audio = spdlog::stdout_color_mt("audio");
|
||||
auto log_voice = spdlog::stdout_color_mt("voice");
|
||||
auto log_discord = spdlog::stdout_color_mt("discord");
|
||||
|
||||
Gtk::Main::init_gtkmm_internals(); // why???
|
||||
return Abaddon::Get().StartGTK();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <gtkmm.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@@ -11,17 +12,20 @@
|
||||
|
||||
#define APP_TITLE "Abaddon"
|
||||
|
||||
class AudioManager;
|
||||
|
||||
class Abaddon {
|
||||
private:
|
||||
Abaddon();
|
||||
|
||||
public:
|
||||
static Abaddon &Get();
|
||||
|
||||
Abaddon(const Abaddon &) = delete;
|
||||
Abaddon &operator=(const Abaddon &) = delete;
|
||||
Abaddon(Abaddon &&) = delete;
|
||||
Abaddon &operator=(Abaddon &&) = delete;
|
||||
|
||||
public:
|
||||
static Abaddon &Get();
|
||||
|
||||
int StartGTK();
|
||||
void OnShutdown();
|
||||
|
||||
@@ -34,8 +38,8 @@ public:
|
||||
void ActionDisconnect();
|
||||
void ActionSetToken();
|
||||
void ActionJoinGuildDialog();
|
||||
void ActionChannelOpened(Snowflake id);
|
||||
void ActionChatInputSubmit(std::string msg, Snowflake channel, Snowflake referenced_message);
|
||||
void ActionChannelOpened(Snowflake id, bool expand_to = true);
|
||||
void ActionChatInputSubmit(ChatSubmitParams data);
|
||||
void ActionChatLoadHistory(Snowflake id);
|
||||
void ActionChatEditMessage(Snowflake channel_id, Snowflake id);
|
||||
void ActionInsertMention(Snowflake id);
|
||||
@@ -50,6 +54,12 @@ public:
|
||||
void ActionViewPins(Snowflake channel_id);
|
||||
void ActionViewThreads(Snowflake channel_id);
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void ActionJoinVoiceChannel(Snowflake channel_id);
|
||||
void ActionDisconnectVoice();
|
||||
#endif
|
||||
|
||||
std::optional<Glib::ustring> ShowTextPrompt(const Glib::ustring &prompt, const Glib::ustring &title, const Glib::ustring &placeholder = "", Gtk::Window *window = nullptr);
|
||||
bool ShowConfirm(const Glib::ustring &prompt, Gtk::Window *window = nullptr);
|
||||
|
||||
void ActionReloadCSS();
|
||||
@@ -57,6 +67,10 @@ public:
|
||||
ImageManager &GetImageManager();
|
||||
EmojiResource &GetEmojis();
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
AudioManager &GetAudio();
|
||||
#endif
|
||||
|
||||
std::string GetDiscordToken() const;
|
||||
bool IsDiscordActive() const;
|
||||
|
||||
@@ -75,6 +89,11 @@ public:
|
||||
void DiscordOnDisconnect(bool is_reconnecting, GatewayCloseCode close_code);
|
||||
void DiscordOnThreadUpdate(const ThreadUpdateData &data);
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void OnVoiceConnected();
|
||||
void OnVoiceDisconnected();
|
||||
#endif
|
||||
|
||||
SettingsManager::Settings &GetSettings();
|
||||
|
||||
Glib::RefPtr<Gtk::CssProvider> GetStyleProvider();
|
||||
@@ -91,8 +110,12 @@ public:
|
||||
static std::string GetStateCachePath(const std::string &path);
|
||||
|
||||
protected:
|
||||
void RunFirstTimeDiscordStartup();
|
||||
|
||||
void ShowGuildVerificationGateDialog(Snowflake guild_id);
|
||||
|
||||
void CheckMessagesForMembers(const ChannelData &chan, const std::vector<Message> &msgs);
|
||||
|
||||
void SetupUserMenu();
|
||||
void SaveState();
|
||||
void LoadState();
|
||||
@@ -110,6 +133,8 @@ protected:
|
||||
Gtk::MenuItem *m_user_menu_roles;
|
||||
Gtk::MenuItem *m_user_menu_remove_recipient;
|
||||
Gtk::Menu *m_user_menu_roles_submenu;
|
||||
Gtk::Menu *m_tray_menu;
|
||||
Gtk::MenuItem *m_tray_exit;
|
||||
|
||||
void on_user_menu_insert_mention();
|
||||
void on_user_menu_ban();
|
||||
@@ -117,6 +142,10 @@ protected:
|
||||
void on_user_menu_copy_id();
|
||||
void on_user_menu_open_dm();
|
||||
void on_user_menu_remove_recipient();
|
||||
void on_tray_click();
|
||||
void on_tray_popup_menu(int button, int activate_time);
|
||||
void on_tray_menu_click();
|
||||
void on_window_hide();
|
||||
|
||||
private:
|
||||
SettingsManager m_settings;
|
||||
@@ -131,9 +160,15 @@ private:
|
||||
ImageManager m_img_mgr;
|
||||
EmojiResource m_emojis;
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
std::unique_ptr<AudioManager> m_audio;
|
||||
Gtk::Window *m_voice_window = nullptr;
|
||||
#endif
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
Glib::RefPtr<Gtk::Application> m_gtk_app;
|
||||
Glib::RefPtr<Gtk::CssProvider> m_css_provider;
|
||||
Glib::RefPtr<Gtk::CssProvider> m_css_low_provider; // registered with a lower priority to allow better customization
|
||||
std::unique_ptr<MainWindow> m_main_window; // wah wah cant create a gtkstylecontext fuck you
|
||||
Glib::RefPtr<Gtk::StatusIcon> m_tray;
|
||||
std::unique_ptr<MainWindow> m_main_window; // wah wah cant create a gtkstylecontext fuck you
|
||||
};
|
||||
|
||||
121
src/audio/devices.cpp
Normal file
121
src/audio/devices.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifdef WITH_VOICE
|
||||
|
||||
// clang-format off
|
||||
|
||||
#include "devices.hpp"
|
||||
#include <cstring>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
// clang-format on
|
||||
|
||||
AudioDevices::AudioDevices()
|
||||
: m_playback(Gtk::ListStore::create(m_playback_columns))
|
||||
, m_capture(Gtk::ListStore::create(m_capture_columns)) {
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> AudioDevices::GetPlaybackDeviceModel() {
|
||||
return m_playback;
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> AudioDevices::GetCaptureDeviceModel() {
|
||||
return m_capture;
|
||||
}
|
||||
|
||||
void AudioDevices::SetDevices(ma_device_info *pPlayback, ma_uint32 playback_count, ma_device_info *pCapture, ma_uint32 capture_count) {
|
||||
m_playback->clear();
|
||||
|
||||
for (ma_uint32 i = 0; i < playback_count; i++) {
|
||||
auto &d = pPlayback[i];
|
||||
|
||||
auto row = *m_playback->append();
|
||||
row[m_playback_columns.Name] = d.name;
|
||||
row[m_playback_columns.DeviceID] = d.id;
|
||||
|
||||
if (d.isDefault) {
|
||||
m_default_playback_iter = row;
|
||||
SetActivePlaybackDevice(row);
|
||||
}
|
||||
}
|
||||
|
||||
m_capture->clear();
|
||||
|
||||
for (ma_uint32 i = 0; i < capture_count; i++) {
|
||||
auto &d = pCapture[i];
|
||||
|
||||
auto row = *m_capture->append();
|
||||
row[m_capture_columns.Name] = d.name;
|
||||
row[m_capture_columns.DeviceID] = d.id;
|
||||
|
||||
if (d.isDefault) {
|
||||
m_default_capture_iter = row;
|
||||
SetActiveCaptureDevice(row);
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_default_playback_iter) {
|
||||
spdlog::get("audio")->warn("No default playback device found");
|
||||
}
|
||||
|
||||
if (!m_default_capture_iter) {
|
||||
spdlog::get("audio")->warn("No default capture device found");
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetPlaybackDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const {
|
||||
if (iter) {
|
||||
return static_cast<ma_device_id>((*iter)[m_playback_columns.DeviceID]);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetCaptureDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const {
|
||||
if (iter) {
|
||||
return static_cast<ma_device_id>((*iter)[m_capture_columns.DeviceID]);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetDefaultPlayback() const {
|
||||
if (m_default_playback_iter) {
|
||||
return static_cast<ma_device_id>((*m_default_playback_iter)[m_playback_columns.DeviceID]);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetDefaultCapture() const {
|
||||
if (m_default_capture_iter) {
|
||||
return static_cast<ma_device_id>((*m_default_capture_iter)[m_capture_columns.DeviceID]);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void AudioDevices::SetActivePlaybackDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
m_active_playback_iter = iter;
|
||||
}
|
||||
|
||||
void AudioDevices::SetActiveCaptureDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
m_active_capture_iter = iter;
|
||||
}
|
||||
|
||||
Gtk::TreeModel::iterator AudioDevices::GetActivePlaybackDevice() {
|
||||
return m_active_playback_iter;
|
||||
}
|
||||
|
||||
Gtk::TreeModel::iterator AudioDevices::GetActiveCaptureDevice() {
|
||||
return m_active_capture_iter;
|
||||
}
|
||||
|
||||
AudioDevices::PlaybackColumns::PlaybackColumns() {
|
||||
add(Name);
|
||||
add(DeviceID);
|
||||
}
|
||||
|
||||
AudioDevices::CaptureColumns::CaptureColumns() {
|
||||
add(Name);
|
||||
add(DeviceID);
|
||||
}
|
||||
#endif
|
||||
58
src/audio/devices.hpp
Normal file
58
src/audio/devices.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#ifdef WITH_VOICE
|
||||
|
||||
// clang-format off
|
||||
|
||||
#include <gtkmm/liststore.h>
|
||||
#include <miniaudio.h>
|
||||
#include <optional>
|
||||
|
||||
// clang-format on
|
||||
|
||||
class AudioDevices {
|
||||
public:
|
||||
AudioDevices();
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> GetPlaybackDeviceModel();
|
||||
Glib::RefPtr<Gtk::ListStore> GetCaptureDeviceModel();
|
||||
|
||||
void SetDevices(ma_device_info *pPlayback, ma_uint32 playback_count, ma_device_info *pCapture, ma_uint32 capture_count);
|
||||
|
||||
[[nodiscard]] std::optional<ma_device_id> GetPlaybackDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const;
|
||||
[[nodiscard]] std::optional<ma_device_id> GetCaptureDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const;
|
||||
|
||||
[[nodiscard]] std::optional<ma_device_id> GetDefaultPlayback() const;
|
||||
[[nodiscard]] std::optional<ma_device_id> GetDefaultCapture() const;
|
||||
|
||||
void SetActivePlaybackDevice(const Gtk::TreeModel::iterator &iter);
|
||||
void SetActiveCaptureDevice(const Gtk::TreeModel::iterator &iter);
|
||||
|
||||
Gtk::TreeModel::iterator GetActivePlaybackDevice();
|
||||
Gtk::TreeModel::iterator GetActiveCaptureDevice();
|
||||
|
||||
private:
|
||||
class PlaybackColumns : public Gtk::TreeModel::ColumnRecord {
|
||||
public:
|
||||
PlaybackColumns();
|
||||
|
||||
Gtk::TreeModelColumn<Glib::ustring> Name;
|
||||
Gtk::TreeModelColumn<ma_device_id> DeviceID;
|
||||
};
|
||||
PlaybackColumns m_playback_columns;
|
||||
Glib::RefPtr<Gtk::ListStore> m_playback;
|
||||
Gtk::TreeModel::iterator m_active_playback_iter;
|
||||
Gtk::TreeModel::iterator m_default_playback_iter;
|
||||
|
||||
class CaptureColumns : public Gtk::TreeModel::ColumnRecord {
|
||||
public:
|
||||
CaptureColumns();
|
||||
|
||||
Gtk::TreeModelColumn<Glib::ustring> Name;
|
||||
Gtk::TreeModelColumn<ma_device_id> DeviceID;
|
||||
};
|
||||
CaptureColumns m_capture_columns;
|
||||
Glib::RefPtr<Gtk::ListStore> m_capture;
|
||||
Gtk::TreeModel::iterator m_active_capture_iter;
|
||||
Gtk::TreeModel::iterator m_default_capture_iter;
|
||||
};
|
||||
#endif
|
||||
466
src/audio/manager.cpp
Normal file
466
src/audio/manager.cpp
Normal file
@@ -0,0 +1,466 @@
|
||||
#ifdef WITH_VOICE
|
||||
// clang-format off
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#include "manager.hpp"
|
||||
#include <array>
|
||||
#include <glibmm/main.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#define MINIAUDIO_IMPLEMENTATION
|
||||
#include <miniaudio.h>
|
||||
#include <opus.h>
|
||||
#include <cstring>
|
||||
// clang-format on
|
||||
|
||||
const uint8_t *StripRTPExtensionHeader(const uint8_t *buf, int num_bytes, size_t &outlen) {
|
||||
if (buf[0] == 0xbe && buf[1] == 0xde && num_bytes > 4) {
|
||||
uint64_t offset = 4 + 4 * ((buf[2] << 8) | buf[3]);
|
||||
|
||||
outlen = num_bytes - offset;
|
||||
return buf + offset;
|
||||
}
|
||||
outlen = num_bytes;
|
||||
return buf;
|
||||
}
|
||||
|
||||
void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) {
|
||||
AudioManager *mgr = reinterpret_cast<AudioManager *>(pDevice->pUserData);
|
||||
if (mgr == nullptr) return;
|
||||
std::lock_guard<std::mutex> _(mgr->m_mutex);
|
||||
|
||||
auto *pOutputF32 = static_cast<float *>(pOutput);
|
||||
for (auto &[ssrc, pair] : mgr->m_sources) {
|
||||
double volume = 1.0;
|
||||
if (const auto vol_it = mgr->m_volume_ssrc.find(ssrc); vol_it != mgr->m_volume_ssrc.end()) {
|
||||
volume = vol_it->second;
|
||||
}
|
||||
auto &buf = pair.first;
|
||||
const size_t n = std::min(static_cast<size_t>(buf.size()), static_cast<size_t>(frameCount * 2ULL));
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
pOutputF32[i] += volume * buf[i] / 32768.F;
|
||||
}
|
||||
buf.erase(buf.begin(), buf.begin() + n);
|
||||
}
|
||||
}
|
||||
|
||||
void capture_data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) {
|
||||
auto *mgr = reinterpret_cast<AudioManager *>(pDevice->pUserData);
|
||||
if (mgr == nullptr) return;
|
||||
|
||||
mgr->OnCapturedPCM(static_cast<const int16_t *>(pInput), frameCount);
|
||||
}
|
||||
|
||||
AudioManager::AudioManager() {
|
||||
m_ok = true;
|
||||
|
||||
int err;
|
||||
m_encoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
|
||||
if (err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("failed to initialize opus encoder: {}", err);
|
||||
m_ok = false;
|
||||
return;
|
||||
}
|
||||
opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(64000));
|
||||
|
||||
if (ma_context_init(nullptr, 0, nullptr, &m_context) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("failed to initialize context");
|
||||
m_ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("audio")->info("Audio backend: {}", ma_get_backend_name(m_context.backend));
|
||||
|
||||
Enumerate();
|
||||
|
||||
m_playback_config = ma_device_config_init(ma_device_type_playback);
|
||||
m_playback_config.playback.format = ma_format_f32;
|
||||
m_playback_config.playback.channels = 2;
|
||||
m_playback_config.sampleRate = 48000;
|
||||
m_playback_config.dataCallback = data_callback;
|
||||
m_playback_config.pUserData = this;
|
||||
|
||||
if (const auto playback_id = m_devices.GetDefaultPlayback(); playback_id.has_value()) {
|
||||
m_playback_id = *playback_id;
|
||||
m_playback_config.playback.pDeviceID = &m_playback_id;
|
||||
}
|
||||
|
||||
if (ma_device_init(&m_context, &m_playback_config, &m_playback_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("failed to initialize playback device");
|
||||
m_ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ma_device_start(&m_playback_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("failed to start playback");
|
||||
ma_device_uninit(&m_playback_device);
|
||||
m_ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
m_capture_config = ma_device_config_init(ma_device_type_capture);
|
||||
m_capture_config.capture.format = ma_format_s16;
|
||||
m_capture_config.capture.channels = 2;
|
||||
m_capture_config.sampleRate = 48000;
|
||||
m_capture_config.periodSizeInFrames = 480;
|
||||
m_capture_config.dataCallback = capture_data_callback;
|
||||
m_capture_config.pUserData = this;
|
||||
|
||||
if (const auto capture_id = m_devices.GetDefaultCapture(); capture_id.has_value()) {
|
||||
m_capture_id = *capture_id;
|
||||
m_capture_config.capture.pDeviceID = &m_capture_id;
|
||||
}
|
||||
|
||||
if (ma_device_init(&m_context, &m_capture_config, &m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("failed to initialize capture device");
|
||||
m_ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
char playback_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1];
|
||||
ma_device_get_name(&m_playback_device, ma_device_type_playback, playback_device_name, sizeof(playback_device_name), nullptr);
|
||||
spdlog::get("audio")->info("using {} as playback device", playback_device_name);
|
||||
|
||||
char capture_device_name[MA_MAX_DEVICE_NAME_LENGTH + 1];
|
||||
ma_device_get_name(&m_capture_device, ma_device_type_capture, capture_device_name, sizeof(capture_device_name), nullptr);
|
||||
spdlog::get("audio")->info("using {} as capture device", capture_device_name);
|
||||
|
||||
Glib::signal_timeout().connect(sigc::mem_fun(*this, &AudioManager::DecayVolumeMeters), 40);
|
||||
}
|
||||
|
||||
AudioManager::~AudioManager() {
|
||||
ma_device_uninit(&m_playback_device);
|
||||
ma_device_uninit(&m_capture_device);
|
||||
ma_context_uninit(&m_context);
|
||||
RemoveAllSSRCs();
|
||||
}
|
||||
|
||||
void AudioManager::AddSSRC(uint32_t ssrc) {
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
int error;
|
||||
if (m_sources.find(ssrc) == m_sources.end()) {
|
||||
auto *decoder = opus_decoder_create(48000, 2, &error);
|
||||
m_sources.insert(std::make_pair(ssrc, std::make_pair(std::deque<int16_t> {}, decoder)));
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::RemoveSSRC(uint32_t ssrc) {
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
if (auto it = m_sources.find(ssrc); it != m_sources.end()) {
|
||||
opus_decoder_destroy(it->second.second);
|
||||
m_sources.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::RemoveAllSSRCs() {
|
||||
spdlog::get("audio")->info("removing all ssrc");
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
for (auto &[ssrc, pair] : m_sources) {
|
||||
opus_decoder_destroy(pair.second);
|
||||
}
|
||||
m_sources.clear();
|
||||
}
|
||||
|
||||
void AudioManager::SetOpusBuffer(uint8_t *ptr) {
|
||||
m_opus_buffer = ptr;
|
||||
}
|
||||
|
||||
void AudioManager::FeedMeOpus(uint32_t ssrc, const std::vector<uint8_t> &data) {
|
||||
if (!m_should_playback) return;
|
||||
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
if (m_muted_ssrcs.find(ssrc) != m_muted_ssrcs.end()) return;
|
||||
|
||||
size_t payload_size = 0;
|
||||
const auto *opus_encoded = StripRTPExtensionHeader(data.data(), static_cast<int>(data.size()), payload_size);
|
||||
static std::array<opus_int16, 120 * 48 * 2> pcm;
|
||||
if (auto it = m_sources.find(ssrc); it != m_sources.end()) {
|
||||
int decoded = opus_decode(it->second.second, opus_encoded, static_cast<opus_int32>(payload_size), pcm.data(), 120 * 48, 0);
|
||||
if (decoded <= 0) {
|
||||
} else {
|
||||
UpdateReceiveVolume(ssrc, pcm.data(), decoded);
|
||||
auto &buf = it->second.first;
|
||||
buf.insert(buf.end(), pcm.begin(), pcm.begin() + decoded * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::StartCaptureDevice() {
|
||||
if (ma_device_start(&m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to start capture device");
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::StopCaptureDevice() {
|
||||
if (ma_device_stop(&m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to stop capture device");
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetPlaybackDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
spdlog::get("audio")->debug("Setting new playback device");
|
||||
|
||||
const auto device_id = m_devices.GetPlaybackDeviceIDFromModel(iter);
|
||||
if (!device_id) {
|
||||
spdlog::get("audio")->error("Requested ID from iterator is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
m_devices.SetActivePlaybackDevice(iter);
|
||||
|
||||
m_playback_id = *device_id;
|
||||
|
||||
ma_device_uninit(&m_playback_device);
|
||||
|
||||
m_playback_config = ma_device_config_init(ma_device_type_playback);
|
||||
m_playback_config.playback.format = ma_format_f32;
|
||||
m_playback_config.playback.channels = 2;
|
||||
m_playback_config.playback.pDeviceID = &m_playback_id;
|
||||
m_playback_config.sampleRate = 48000;
|
||||
m_playback_config.dataCallback = data_callback;
|
||||
m_playback_config.pUserData = this;
|
||||
|
||||
if (ma_device_init(&m_context, &m_playback_config, &m_playback_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to initialize new device");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ma_device_start(&m_playback_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to start new device");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetCaptureDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
spdlog::get("audio")->debug("Setting new capture device");
|
||||
|
||||
const auto device_id = m_devices.GetCaptureDeviceIDFromModel(iter);
|
||||
if (!device_id) {
|
||||
spdlog::get("audio")->error("Requested ID from iterator is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
m_devices.SetActiveCaptureDevice(iter);
|
||||
|
||||
m_capture_id = *device_id;
|
||||
|
||||
ma_device_uninit(&m_capture_device);
|
||||
|
||||
m_capture_config = ma_device_config_init(ma_device_type_capture);
|
||||
m_capture_config.capture.format = ma_format_s16;
|
||||
m_capture_config.capture.channels = 2;
|
||||
m_capture_config.capture.pDeviceID = &m_capture_id;
|
||||
m_capture_config.sampleRate = 48000;
|
||||
m_capture_config.periodSizeInFrames = 480;
|
||||
m_capture_config.dataCallback = capture_data_callback;
|
||||
m_capture_config.pUserData = this;
|
||||
|
||||
if (ma_device_init(&m_context, &m_capture_config, &m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to initialize new device");
|
||||
return;
|
||||
}
|
||||
|
||||
// technically this should probably try and check old state but if you are in the window to change it then you are connected
|
||||
if (ma_device_start(&m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to start new device");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetCapture(bool capture) {
|
||||
m_should_capture = capture;
|
||||
}
|
||||
|
||||
void AudioManager::SetPlayback(bool playback) {
|
||||
m_should_playback = playback;
|
||||
}
|
||||
|
||||
void AudioManager::SetCaptureGate(double gate) {
|
||||
m_capture_gate = gate * 0.01;
|
||||
}
|
||||
|
||||
void AudioManager::SetCaptureGain(double gain) {
|
||||
m_capture_gain = gain;
|
||||
}
|
||||
|
||||
void AudioManager::SetMuteSSRC(uint32_t ssrc, bool mute) {
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
if (mute) {
|
||||
m_muted_ssrcs.insert(ssrc);
|
||||
} else {
|
||||
m_muted_ssrcs.erase(ssrc);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetVolumeSSRC(uint32_t ssrc, double volume) {
|
||||
std::lock_guard<std::mutex> _(m_mutex);
|
||||
volume *= 0.01;
|
||||
constexpr const double E = 2.71828182845904523536;
|
||||
m_volume_ssrc[ssrc] = (std::exp(volume) - 1) / (E - 1);
|
||||
}
|
||||
|
||||
void AudioManager::SetEncodingApplication(int application) {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
int prev_bitrate = 64000;
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_GET_BITRATE(&prev_bitrate)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("Failed to get old bitrate when reinitializing: {}", err);
|
||||
}
|
||||
opus_encoder_destroy(m_encoder);
|
||||
int err = 0;
|
||||
m_encoder = opus_encoder_create(48000, 2, application, &err);
|
||||
if (err != OPUS_OK) {
|
||||
spdlog::get("audio")->critical("opus_encoder_create failed: {}", err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(prev_bitrate)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("Failed to set bitrate when reinitializing: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
int AudioManager::GetEncodingApplication() {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
int temp = OPUS_APPLICATION_VOIP;
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_GET_APPLICATION(&temp)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("opus_encoder_ctl(OPUS_GET_APPLICATION) failed: {}", err);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void AudioManager::SetSignalHint(int signal) {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_SET_SIGNAL(signal)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("opus_encoder_ctl(OPUS_SET_SIGNAL) failed: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
int AudioManager::GetSignalHint() {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
int temp = OPUS_AUTO;
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_GET_SIGNAL(&temp)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("opus_encoder_ctl(OPUS_GET_SIGNAL) failed: {}", err);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void AudioManager::SetBitrate(int bitrate) {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(bitrate)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("opus_encoder_ctl(OPUS_SET_BITRATE) failed: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
int AudioManager::GetBitrate() {
|
||||
std::lock_guard<std::mutex> _(m_enc_mutex);
|
||||
int temp = 64000;
|
||||
if (int err = opus_encoder_ctl(m_encoder, OPUS_GET_BITRATE(&temp)); err != OPUS_OK) {
|
||||
spdlog::get("audio")->error("opus_encoder_ctl(OPUS_GET_BITRATE) failed: {}", err);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void AudioManager::Enumerate() {
|
||||
ma_device_info *pPlaybackDeviceInfo;
|
||||
ma_uint32 playbackDeviceCount;
|
||||
ma_device_info *pCaptureDeviceInfo;
|
||||
ma_uint32 captureDeviceCount;
|
||||
|
||||
spdlog::get("audio")->debug("Enumerating devices");
|
||||
|
||||
if (ma_context_get_devices(
|
||||
&m_context,
|
||||
&pPlaybackDeviceInfo,
|
||||
&playbackDeviceCount,
|
||||
&pCaptureDeviceInfo,
|
||||
&captureDeviceCount) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to enumerate devices");
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("audio")->debug("Found {} playback devices and {} capture devices", playbackDeviceCount, captureDeviceCount);
|
||||
|
||||
m_devices.SetDevices(pPlaybackDeviceInfo, playbackDeviceCount, pCaptureDeviceInfo, captureDeviceCount);
|
||||
}
|
||||
|
||||
void AudioManager::OnCapturedPCM(const int16_t *pcm, ma_uint32 frames) {
|
||||
if (m_opus_buffer == nullptr || !m_should_capture) return;
|
||||
|
||||
const double gain = m_capture_gain;
|
||||
// i have a suspicion i can cast the const away... but i wont
|
||||
std::vector<int16_t> new_pcm(pcm, pcm + frames * 2);
|
||||
for (auto &val : new_pcm) {
|
||||
const int32_t unclamped = static_cast<int32_t>(val * gain);
|
||||
val = std::clamp(unclamped, INT16_MIN, INT16_MAX);
|
||||
}
|
||||
|
||||
UpdateCaptureVolume(new_pcm.data(), frames);
|
||||
|
||||
if (m_capture_peak_meter / 32768.0 < m_capture_gate) return;
|
||||
|
||||
m_enc_mutex.lock();
|
||||
int payload_len = opus_encode(m_encoder, new_pcm.data(), 480, static_cast<unsigned char *>(m_opus_buffer), 1275);
|
||||
m_enc_mutex.unlock();
|
||||
if (payload_len < 0) {
|
||||
spdlog::get("audio")->error("encoding error: {}", payload_len);
|
||||
} else {
|
||||
m_signal_opus_packet.emit(payload_len);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::UpdateReceiveVolume(uint32_t ssrc, const int16_t *pcm, int frames) {
|
||||
std::lock_guard<std::mutex> _(m_vol_mtx);
|
||||
|
||||
auto &meter = m_volumes[ssrc];
|
||||
for (int i = 0; i < frames * 2; i += 2) {
|
||||
const int amp = std::abs(pcm[i]);
|
||||
meter = std::max(meter, std::abs(amp) / 32768.0);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::UpdateCaptureVolume(const int16_t *pcm, ma_uint32 frames) {
|
||||
for (ma_uint32 i = 0; i < frames * 2; i += 2) {
|
||||
const int amp = std::abs(pcm[i]);
|
||||
m_capture_peak_meter = std::max(m_capture_peak_meter.load(std::memory_order_relaxed), amp);
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioManager::DecayVolumeMeters() {
|
||||
m_capture_peak_meter -= 600;
|
||||
if (m_capture_peak_meter < 0) m_capture_peak_meter = 0;
|
||||
|
||||
std::lock_guard<std::mutex> _(m_vol_mtx);
|
||||
|
||||
for (auto &[ssrc, meter] : m_volumes) {
|
||||
meter -= 0.01;
|
||||
if (meter < 0.0) meter = 0.0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioManager::OK() const {
|
||||
return m_ok;
|
||||
}
|
||||
|
||||
double AudioManager::GetCaptureVolumeLevel() const noexcept {
|
||||
return m_capture_peak_meter / 32768.0;
|
||||
}
|
||||
|
||||
double AudioManager::GetSSRCVolumeLevel(uint32_t ssrc) const noexcept {
|
||||
std::lock_guard<std::mutex> _(m_vol_mtx);
|
||||
if (const auto it = m_volumes.find(ssrc); it != m_volumes.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
AudioDevices &AudioManager::GetDevices() {
|
||||
return m_devices;
|
||||
}
|
||||
|
||||
AudioManager::type_signal_opus_packet AudioManager::signal_opus_packet() {
|
||||
return m_signal_opus_packet;
|
||||
}
|
||||
#endif
|
||||
120
src/audio/manager.hpp
Normal file
120
src/audio/manager.hpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
#ifdef WITH_VOICE
|
||||
// clang-format off
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <deque>
|
||||
#include <gtkmm/treemodel.h>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <miniaudio.h>
|
||||
#include <opus.h>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include "devices.hpp"
|
||||
// clang-format on
|
||||
|
||||
class AudioManager {
|
||||
public:
|
||||
AudioManager();
|
||||
~AudioManager();
|
||||
|
||||
void AddSSRC(uint32_t ssrc);
|
||||
void RemoveSSRC(uint32_t ssrc);
|
||||
void RemoveAllSSRCs();
|
||||
|
||||
void SetOpusBuffer(uint8_t *ptr);
|
||||
void FeedMeOpus(uint32_t ssrc, const std::vector<uint8_t> &data);
|
||||
|
||||
void StartCaptureDevice();
|
||||
void StopCaptureDevice();
|
||||
|
||||
void SetPlaybackDevice(const Gtk::TreeModel::iterator &iter);
|
||||
void SetCaptureDevice(const Gtk::TreeModel::iterator &iter);
|
||||
|
||||
void SetCapture(bool capture);
|
||||
void SetPlayback(bool playback);
|
||||
|
||||
void SetCaptureGate(double gate);
|
||||
void SetCaptureGain(double gain);
|
||||
|
||||
void SetMuteSSRC(uint32_t ssrc, bool mute);
|
||||
void SetVolumeSSRC(uint32_t ssrc, double volume);
|
||||
|
||||
void SetEncodingApplication(int application);
|
||||
[[nodiscard]] int GetEncodingApplication();
|
||||
void SetSignalHint(int signal);
|
||||
[[nodiscard]] int GetSignalHint();
|
||||
void SetBitrate(int bitrate);
|
||||
[[nodiscard]] int GetBitrate();
|
||||
|
||||
void Enumerate();
|
||||
|
||||
[[nodiscard]] bool OK() const;
|
||||
|
||||
[[nodiscard]] double GetCaptureVolumeLevel() const noexcept;
|
||||
[[nodiscard]] double GetSSRCVolumeLevel(uint32_t ssrc) const noexcept;
|
||||
|
||||
[[nodiscard]] AudioDevices &GetDevices();
|
||||
|
||||
private:
|
||||
void OnCapturedPCM(const int16_t *pcm, ma_uint32 frames);
|
||||
|
||||
void UpdateReceiveVolume(uint32_t ssrc, const int16_t *pcm, int frames);
|
||||
void UpdateCaptureVolume(const int16_t *pcm, ma_uint32 frames);
|
||||
std::atomic<int> m_capture_peak_meter = 0;
|
||||
|
||||
bool DecayVolumeMeters();
|
||||
|
||||
friend void data_callback(ma_device *, void *, const void *, ma_uint32);
|
||||
friend void capture_data_callback(ma_device *, void *, const void *, ma_uint32);
|
||||
|
||||
std::thread m_thread;
|
||||
|
||||
bool m_ok;
|
||||
|
||||
// playback
|
||||
ma_device m_playback_device;
|
||||
ma_device_config m_playback_config;
|
||||
ma_device_id m_playback_id;
|
||||
// capture
|
||||
ma_device m_capture_device;
|
||||
ma_device_config m_capture_config;
|
||||
ma_device_id m_capture_id;
|
||||
|
||||
ma_context m_context;
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
mutable std::mutex m_enc_mutex;
|
||||
|
||||
std::unordered_map<uint32_t, std::pair<std::deque<int16_t>, OpusDecoder *>> m_sources;
|
||||
|
||||
OpusEncoder *m_encoder;
|
||||
|
||||
uint8_t *m_opus_buffer = nullptr;
|
||||
|
||||
std::atomic<bool> m_should_capture = true;
|
||||
std::atomic<bool> m_should_playback = true;
|
||||
|
||||
std::atomic<double> m_capture_gate = 0.0;
|
||||
std::atomic<double> m_capture_gain = 1.0;
|
||||
|
||||
std::unordered_set<uint32_t> m_muted_ssrcs;
|
||||
std::unordered_map<uint32_t, double> m_volume_ssrc;
|
||||
|
||||
mutable std::mutex m_vol_mtx;
|
||||
std::unordered_map<uint32_t, double> m_volumes;
|
||||
|
||||
AudioDevices m_devices;
|
||||
|
||||
public:
|
||||
using type_signal_opus_packet = sigc::signal<void(int payload_size)>;
|
||||
type_signal_opus_packet signal_opus_packet();
|
||||
|
||||
private:
|
||||
type_signal_opus_packet m_signal_opus_packet;
|
||||
};
|
||||
#endif
|
||||
@@ -10,8 +10,6 @@ CellRendererPixbufAnimation::CellRendererPixbufAnimation()
|
||||
property_ypad() = 2;
|
||||
}
|
||||
|
||||
CellRendererPixbufAnimation::~CellRendererPixbufAnimation() {}
|
||||
|
||||
Glib::PropertyProxy<Glib::RefPtr<Gdk::Pixbuf>> CellRendererPixbufAnimation::property_pixbuf() {
|
||||
return m_property_pixbuf.get_proxy();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
class CellRendererPixbufAnimation : public Gtk::CellRenderer {
|
||||
public:
|
||||
CellRendererPixbufAnimation();
|
||||
virtual ~CellRendererPixbufAnimation();
|
||||
~CellRendererPixbufAnimation() override = default;
|
||||
|
||||
Glib::PropertyProxy<Glib::RefPtr<Gdk::Pixbuf>> property_pixbuf();
|
||||
Glib::PropertyProxy<Glib::RefPtr<Gdk::PixbufAnimation>> property_pixbuf_animation();
|
||||
|
||||
@@ -17,8 +17,20 @@ ChannelList::ChannelList()
|
||||
, m_menu_category_copy_id("_Copy ID", true)
|
||||
, m_menu_channel_copy_id("_Copy ID", true)
|
||||
, m_menu_channel_mark_as_read("Mark as _Read", true)
|
||||
#ifdef WITH_LIBHANDY
|
||||
, m_menu_channel_open_tab("Open in New _Tab", true)
|
||||
, m_menu_dm_open_tab("Open in New _Tab", true)
|
||||
#endif
|
||||
#ifdef WITH_VOICE
|
||||
, m_menu_voice_channel_join("_Join", true)
|
||||
, m_menu_voice_channel_disconnect("_Disconnect", true)
|
||||
#endif
|
||||
, m_menu_dm_copy_id("_Copy ID", true)
|
||||
, m_menu_dm_close("") // changes depending on if group or not
|
||||
#ifdef WITH_VOICE
|
||||
, m_menu_dm_join_voice("Join _Voice", true)
|
||||
, m_menu_dm_disconnect_voice("_Disconnect Voice", true)
|
||||
#endif
|
||||
, m_menu_thread_copy_id("_Copy ID", true)
|
||||
, m_menu_thread_leave("_Leave", true)
|
||||
, m_menu_thread_archive("_Archive", true)
|
||||
@@ -32,7 +44,11 @@ ChannelList::ChannelList()
|
||||
const auto type = row[m_columns.m_type];
|
||||
// text channels should not be allowed to be collapsed
|
||||
// maybe they should be but it seems a little difficult to handle expansion to permit this
|
||||
#ifdef WITH_VOICE
|
||||
if (type != RenderType::TextChannel && type != RenderType::VoiceChannel) {
|
||||
#else
|
||||
if (type != RenderType::TextChannel) {
|
||||
#endif
|
||||
if (row[m_columns.m_expanded]) {
|
||||
m_view.collapse_row(path);
|
||||
row[m_columns.m_expanded] = false;
|
||||
@@ -42,7 +58,11 @@ ChannelList::ChannelList()
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
if (type == RenderType::TextChannel || type == RenderType::DM || type == RenderType::Thread || type == RenderType::VoiceChannel) {
|
||||
#else
|
||||
if (type == RenderType::TextChannel || type == RenderType::DM || type == RenderType::Thread) {
|
||||
#endif
|
||||
const auto id = static_cast<Snowflake>(row[m_columns.m_id]);
|
||||
m_signal_action_channel_item_select.emit(id);
|
||||
Abaddon::Get().GetDiscordClient().MarkChannelAsRead(id, [](...) {});
|
||||
@@ -143,11 +163,35 @@ ChannelList::ChannelList()
|
||||
else
|
||||
discord.MuteChannel(id, NOOP_CALLBACK);
|
||||
});
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_menu_channel_open_tab.signal_activate().connect([this] {
|
||||
const auto id = static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]);
|
||||
m_signal_action_open_new_tab.emit(id);
|
||||
});
|
||||
m_menu_channel.append(m_menu_channel_open_tab);
|
||||
#endif
|
||||
|
||||
m_menu_channel.append(m_menu_channel_mark_as_read);
|
||||
m_menu_channel.append(m_menu_channel_toggle_mute);
|
||||
m_menu_channel.append(m_menu_channel_copy_id);
|
||||
m_menu_channel.show_all();
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
m_menu_voice_channel_join.signal_activate().connect([this]() {
|
||||
const auto id = static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]);
|
||||
m_signal_action_join_voice_channel.emit(id);
|
||||
});
|
||||
|
||||
m_menu_voice_channel_disconnect.signal_activate().connect([this]() {
|
||||
m_signal_action_disconnect_voice.emit();
|
||||
});
|
||||
|
||||
m_menu_voice_channel.append(m_menu_voice_channel_join);
|
||||
m_menu_voice_channel.append(m_menu_voice_channel_disconnect);
|
||||
m_menu_voice_channel.show_all();
|
||||
#endif
|
||||
|
||||
m_menu_dm_copy_id.signal_activate().connect([this] {
|
||||
Gtk::Clipboard::get()->set_text(std::to_string((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]));
|
||||
});
|
||||
@@ -170,8 +214,26 @@ ChannelList::ChannelList()
|
||||
else
|
||||
discord.MuteChannel(id, NOOP_CALLBACK);
|
||||
});
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_menu_dm_open_tab.signal_activate().connect([this] {
|
||||
const auto id = static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]);
|
||||
m_signal_action_open_new_tab.emit(id);
|
||||
});
|
||||
m_menu_dm.append(m_menu_dm_open_tab);
|
||||
#endif
|
||||
m_menu_dm.append(m_menu_dm_toggle_mute);
|
||||
m_menu_dm.append(m_menu_dm_close);
|
||||
#ifdef WITH_VOICE
|
||||
m_menu_dm_join_voice.signal_activate().connect([this]() {
|
||||
const auto id = static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]);
|
||||
m_signal_action_join_voice_channel.emit(id);
|
||||
});
|
||||
m_menu_dm_disconnect_voice.signal_activate().connect([this]() {
|
||||
m_signal_action_disconnect_voice.emit();
|
||||
});
|
||||
m_menu_dm.append(m_menu_dm_join_voice);
|
||||
m_menu_dm.append(m_menu_dm_disconnect_voice);
|
||||
#endif
|
||||
m_menu_dm.append(m_menu_dm_copy_id);
|
||||
m_menu_dm.show_all();
|
||||
|
||||
@@ -207,12 +269,6 @@ ChannelList::ChannelList()
|
||||
m_menu_thread.append(m_menu_thread_copy_id);
|
||||
m_menu_thread.show_all();
|
||||
|
||||
m_menu_guild.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnGuildSubmenuPopup));
|
||||
m_menu_category.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnCategorySubmenuPopup));
|
||||
m_menu_channel.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnChannelSubmenuPopup));
|
||||
m_menu_dm.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnDMSubmenuPopup));
|
||||
m_menu_thread.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnThreadSubmenuPopup));
|
||||
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
discord.signal_message_create().connect(sigc::mem_fun(*this, &ChannelList::OnMessageCreate));
|
||||
discord.signal_guild_create().connect(sigc::mem_fun(*this, &ChannelList::UpdateNewGuild));
|
||||
@@ -291,7 +347,7 @@ void ChannelList::UpdateChannel(Snowflake id) {
|
||||
auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id);
|
||||
if (!iter || !channel.has_value()) return;
|
||||
if (channel->Type == ChannelType::GUILD_CATEGORY) return UpdateChannelCategory(*channel);
|
||||
if (!IsTextChannel(channel->Type)) return;
|
||||
if (!channel->IsText()) return;
|
||||
|
||||
// refresh stuff that might have changed
|
||||
const bool is_orphan_TMP = !channel->ParentID.has_value();
|
||||
@@ -399,7 +455,7 @@ void ChannelList::OnThreadListSync(const ThreadListSyncData &data) {
|
||||
queue.pop();
|
||||
if ((*item)[m_columns.m_type] == RenderType::Thread)
|
||||
threads.push_back(static_cast<Snowflake>((*item)[m_columns.m_id]));
|
||||
for (auto child : item->children())
|
||||
for (const auto &child : item->children())
|
||||
queue.push(child);
|
||||
}
|
||||
|
||||
@@ -448,7 +504,7 @@ void ChannelList::OnGuildUnmute(Snowflake id) {
|
||||
|
||||
// create a temporary channel row for non-joined threads
|
||||
// and delete them when the active channel switches off of them if still not joined
|
||||
void ChannelList::SetActiveChannel(Snowflake id) {
|
||||
void ChannelList::SetActiveChannel(Snowflake id, bool expand_to) {
|
||||
// mark channel as read when switching off
|
||||
if (m_active_channel.IsValid())
|
||||
Abaddon::Get().GetDiscordClient().MarkChannelAsRead(m_active_channel, [](...) {});
|
||||
@@ -465,11 +521,12 @@ void ChannelList::SetActiveChannel(Snowflake id) {
|
||||
|
||||
const auto channel_iter = GetIteratorForChannelFromID(id);
|
||||
if (channel_iter) {
|
||||
m_view.expand_to_path(m_model->get_path(channel_iter));
|
||||
if (expand_to) {
|
||||
m_view.expand_to_path(m_model->get_path(channel_iter));
|
||||
}
|
||||
m_view.get_selection()->select(channel_iter);
|
||||
} else {
|
||||
m_view.get_selection()->unselect_all();
|
||||
// SetActiveChannel should probably just take the channel object
|
||||
const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id);
|
||||
if (!channel.has_value() || !channel->IsThread()) return;
|
||||
auto parent_iter = GetIteratorForChannelFromID(*channel->ParentID);
|
||||
@@ -564,7 +621,11 @@ Gtk::TreeModel::iterator ChannelList::AddGuild(const GuildData &guild) {
|
||||
for (const auto &channel_ : *guild.Channels) {
|
||||
const auto channel = discord.GetChannel(channel_.ID);
|
||||
if (!channel.has_value()) continue;
|
||||
#ifdef WITH_VOICE
|
||||
if (channel->Type == ChannelType::GUILD_TEXT || channel->Type == ChannelType::GUILD_NEWS || channel->Type == ChannelType::GUILD_VOICE) {
|
||||
#else
|
||||
if (channel->Type == ChannelType::GUILD_TEXT || channel->Type == ChannelType::GUILD_NEWS) {
|
||||
#endif
|
||||
if (channel->ParentID.has_value())
|
||||
categories[*channel->ParentID].push_back(*channel);
|
||||
else
|
||||
@@ -580,7 +641,7 @@ Gtk::TreeModel::iterator ChannelList::AddGuild(const GuildData &guild) {
|
||||
if (thread.has_value())
|
||||
threads[*thread->ParentID].push_back(*thread);
|
||||
}
|
||||
const auto add_threads = [&](const ChannelData &channel, Gtk::TreeRow row) {
|
||||
const auto add_threads = [&](const ChannelData &channel, const Gtk::TreeRow &row) {
|
||||
row[m_columns.m_expanded] = true;
|
||||
|
||||
const auto it = threads.find(channel.ID);
|
||||
@@ -590,9 +651,31 @@ Gtk::TreeModel::iterator ChannelList::AddGuild(const GuildData &guild) {
|
||||
m_tmp_channel_map[thread.ID] = CreateThreadRow(row.children(), thread);
|
||||
};
|
||||
|
||||
auto add_voice_participants = [this, &discord](const ChannelData &channel, const Gtk::TreeNodeChildren &root) {
|
||||
for (auto user_id : discord.GetUsersInVoiceChannel(channel.ID)) {
|
||||
const auto user = discord.GetUser(user_id);
|
||||
|
||||
auto user_row = *m_model->append(root);
|
||||
user_row[m_columns.m_type] = RenderType::VoiceParticipant;
|
||||
user_row[m_columns.m_id] = user_id;
|
||||
if (user.has_value()) {
|
||||
user_row[m_columns.m_name] = user->GetEscapedName();
|
||||
} else {
|
||||
user_row[m_columns.m_name] = "<i>Unknown</i>";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const auto &channel : orphan_channels) {
|
||||
auto channel_row = *m_model->append(guild_row.children());
|
||||
channel_row[m_columns.m_type] = RenderType::TextChannel;
|
||||
if (IsTextChannel(channel.Type))
|
||||
channel_row[m_columns.m_type] = RenderType::TextChannel;
|
||||
#ifdef WITH_VOICE
|
||||
else {
|
||||
channel_row[m_columns.m_type] = RenderType::VoiceChannel;
|
||||
add_voice_participants(channel, channel_row->children());
|
||||
}
|
||||
#endif
|
||||
channel_row[m_columns.m_id] = channel.ID;
|
||||
channel_row[m_columns.m_name] = "#" + Glib::Markup::escape_text(*channel.Name);
|
||||
channel_row[m_columns.m_sort] = *channel.Position + OrphanChannelSortOffset;
|
||||
@@ -615,7 +698,14 @@ Gtk::TreeModel::iterator ChannelList::AddGuild(const GuildData &guild) {
|
||||
|
||||
for (const auto &channel : channels) {
|
||||
auto channel_row = *m_model->append(cat_row.children());
|
||||
channel_row[m_columns.m_type] = RenderType::TextChannel;
|
||||
if (IsTextChannel(channel.Type))
|
||||
channel_row[m_columns.m_type] = RenderType::TextChannel;
|
||||
#ifdef WITH_VOICE
|
||||
else {
|
||||
channel_row[m_columns.m_type] = RenderType::VoiceChannel;
|
||||
add_voice_participants(channel, channel_row->children());
|
||||
}
|
||||
#endif
|
||||
channel_row[m_columns.m_id] = channel.ID;
|
||||
channel_row[m_columns.m_name] = "#" + Glib::Markup::escape_text(*channel.Name);
|
||||
channel_row[m_columns.m_sort] = *channel.Position;
|
||||
@@ -648,7 +738,7 @@ Gtk::TreeModel::iterator ChannelList::CreateThreadRow(const Gtk::TreeNodeChildre
|
||||
thread_row[m_columns.m_type] = RenderType::Thread;
|
||||
thread_row[m_columns.m_id] = channel.ID;
|
||||
thread_row[m_columns.m_name] = "- " + Glib::Markup::escape_text(*channel.Name);
|
||||
thread_row[m_columns.m_sort] = channel.ID;
|
||||
thread_row[m_columns.m_sort] = static_cast<int64_t>(channel.ID);
|
||||
thread_row[m_columns.m_nsfw] = false;
|
||||
|
||||
return thread_iter;
|
||||
@@ -692,7 +782,7 @@ bool ChannelList::IsTextChannel(ChannelType type) {
|
||||
}
|
||||
|
||||
// this should be unncessary but something is behaving strange so its just in case
|
||||
void ChannelList::OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) {
|
||||
void ChannelList::OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) const {
|
||||
(*iter)[m_columns.m_expanded] = false;
|
||||
}
|
||||
|
||||
@@ -717,7 +807,11 @@ bool ChannelList::SelectionFunc(const Glib::RefPtr<Gtk::TreeModel> &model, const
|
||||
m_last_selected = m_model->get_path(row);
|
||||
|
||||
auto type = (*m_model->get_iter(path))[m_columns.m_type];
|
||||
#ifdef WITH_VOICE
|
||||
return type == RenderType::TextChannel || type == RenderType::DM || type == RenderType::Thread || type == RenderType::VoiceChannel;
|
||||
#else
|
||||
return type == RenderType::TextChannel || type == RenderType::DM || type == RenderType::Thread;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ChannelList::AddPrivateChannels() {
|
||||
@@ -737,14 +831,14 @@ void ChannelList::AddPrivateChannels() {
|
||||
|
||||
std::optional<UserData> top_recipient;
|
||||
const auto recipients = dm->GetDMRecipients();
|
||||
if (recipients.size() > 0)
|
||||
if (!recipients.empty())
|
||||
top_recipient = recipients[0];
|
||||
|
||||
auto iter = m_model->append(header_row->children());
|
||||
auto row = *iter;
|
||||
row[m_columns.m_type] = RenderType::DM;
|
||||
row[m_columns.m_id] = dm_id;
|
||||
row[m_columns.m_sort] = -(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id);
|
||||
row[m_columns.m_sort] = static_cast<int64_t>(-(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id));
|
||||
row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize);
|
||||
|
||||
if (dm->Type == ChannelType::DM && top_recipient.has_value())
|
||||
@@ -781,7 +875,7 @@ void ChannelList::UpdateCreateDMChannel(const ChannelData &dm) {
|
||||
auto row = *iter;
|
||||
row[m_columns.m_type] = RenderType::DM;
|
||||
row[m_columns.m_id] = dm.ID;
|
||||
row[m_columns.m_sort] = -(dm.LastMessageID.has_value() ? *dm.LastMessageID : dm.ID);
|
||||
row[m_columns.m_sort] = static_cast<int64_t>(-(dm.LastMessageID.has_value() ? *dm.LastMessageID : dm.ID));
|
||||
row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize);
|
||||
|
||||
if (dm.Type == ChannelType::DM && top_recipient.has_value())
|
||||
@@ -817,7 +911,7 @@ void ChannelList::OnMessageCreate(const Message &msg) {
|
||||
if (!channel.has_value()) return;
|
||||
if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM) {
|
||||
if (iter)
|
||||
(*iter)[m_columns.m_sort] = -msg.ID;
|
||||
(*iter)[m_columns.m_sort] = static_cast<int64_t>(-msg.ID);
|
||||
}
|
||||
if (channel->GuildID.has_value())
|
||||
if ((iter = GetIteratorForGuildFromID(*channel->GuildID)))
|
||||
@@ -826,19 +920,29 @@ void ChannelList::OnMessageCreate(const Message &msg) {
|
||||
|
||||
bool ChannelList::OnButtonPressEvent(GdkEventButton *ev) {
|
||||
if (ev->button == GDK_BUTTON_SECONDARY && ev->type == GDK_BUTTON_PRESS) {
|
||||
if (m_view.get_path_at_pos(ev->x, ev->y, m_path_for_menu)) {
|
||||
if (m_view.get_path_at_pos(static_cast<int>(ev->x), static_cast<int>(ev->y), m_path_for_menu)) {
|
||||
auto row = (*m_model->get_iter(m_path_for_menu));
|
||||
switch (static_cast<RenderType>(row[m_columns.m_type])) {
|
||||
case RenderType::Guild:
|
||||
OnGuildSubmenuPopup();
|
||||
m_menu_guild.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
break;
|
||||
case RenderType::Category:
|
||||
OnCategorySubmenuPopup();
|
||||
m_menu_category.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
break;
|
||||
case RenderType::TextChannel:
|
||||
OnChannelSubmenuPopup();
|
||||
m_menu_channel.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
break;
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
OnVoiceChannelSubmenuPopup();
|
||||
m_menu_voice_channel.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
break;
|
||||
#endif
|
||||
case RenderType::DM: {
|
||||
OnDMSubmenuPopup();
|
||||
const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(static_cast<Snowflake>(row[m_columns.m_id]));
|
||||
if (channel.has_value()) {
|
||||
m_menu_dm_close.set_label(channel->Type == ChannelType::DM ? "Close" : "Leave");
|
||||
@@ -848,6 +952,7 @@ bool ChannelList::OnButtonPressEvent(GdkEventButton *ev) {
|
||||
m_menu_dm.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
} break;
|
||||
case RenderType::Thread: {
|
||||
OnThreadSubmenuPopup();
|
||||
m_menu_thread.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
break;
|
||||
} break;
|
||||
@@ -887,17 +992,22 @@ void ChannelList::MoveRow(const Gtk::TreeModel::iterator &iter, const Gtk::TreeM
|
||||
m_model->erase(iter);
|
||||
}
|
||||
|
||||
void ChannelList::OnGuildSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
|
||||
void ChannelList::OnGuildSubmenuPopup() {
|
||||
const auto iter = m_model->get_iter(m_path_for_menu);
|
||||
if (!iter) return;
|
||||
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
|
||||
if (Abaddon::Get().GetDiscordClient().IsGuildMuted(id))
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
if (discord.IsGuildMuted(id))
|
||||
m_menu_guild_toggle_mute.set_label("Unmute");
|
||||
else
|
||||
m_menu_guild_toggle_mute.set_label("Mute");
|
||||
|
||||
const auto guild = discord.GetGuild(id);
|
||||
const auto self_id = discord.GetUserData().ID;
|
||||
m_menu_guild_leave.set_sensitive(!(guild.has_value() && guild->OwnerID == self_id));
|
||||
}
|
||||
|
||||
void ChannelList::OnCategorySubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
|
||||
void ChannelList::OnCategorySubmenuPopup() {
|
||||
const auto iter = m_model->get_iter(m_path_for_menu);
|
||||
if (!iter) return;
|
||||
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
|
||||
@@ -907,27 +1017,59 @@ void ChannelList::OnCategorySubmenuPopup(const Gdk::Rectangle *flipped_rect, con
|
||||
m_menu_category_toggle_mute.set_label("Mute");
|
||||
}
|
||||
|
||||
void ChannelList::OnChannelSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
|
||||
void ChannelList::OnChannelSubmenuPopup() {
|
||||
const auto iter = m_model->get_iter(m_path_for_menu);
|
||||
if (!iter) return;
|
||||
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
|
||||
if (Abaddon::Get().GetDiscordClient().IsChannelMuted(id))
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
#ifdef WITH_LIBHANDY
|
||||
const auto perms = discord.HasChannelPermission(discord.GetUserData().ID, id, Permission::VIEW_CHANNEL);
|
||||
m_menu_channel_open_tab.set_sensitive(perms);
|
||||
#endif
|
||||
if (discord.IsChannelMuted(id))
|
||||
m_menu_channel_toggle_mute.set_label("Unmute");
|
||||
else
|
||||
m_menu_channel_toggle_mute.set_label("Mute");
|
||||
}
|
||||
|
||||
void ChannelList::OnDMSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
|
||||
#ifdef WITH_VOICE
|
||||
void ChannelList::OnVoiceChannelSubmenuPopup() {
|
||||
const auto iter = m_model->get_iter(m_path_for_menu);
|
||||
if (!iter) return;
|
||||
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
if (discord.IsVoiceConnected() || discord.IsVoiceConnecting()) {
|
||||
m_menu_voice_channel_join.set_sensitive(false);
|
||||
m_menu_voice_channel_disconnect.set_sensitive(discord.GetVoiceChannelID() == id);
|
||||
} else {
|
||||
m_menu_voice_channel_join.set_sensitive(true);
|
||||
m_menu_voice_channel_disconnect.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void ChannelList::OnDMSubmenuPopup() {
|
||||
auto iter = m_model->get_iter(m_path_for_menu);
|
||||
if (!iter) return;
|
||||
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
|
||||
if (Abaddon::Get().GetDiscordClient().IsChannelMuted(id))
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
if (discord.IsChannelMuted(id))
|
||||
m_menu_dm_toggle_mute.set_label("Unmute");
|
||||
else
|
||||
m_menu_dm_toggle_mute.set_label("Mute");
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
if (discord.IsVoiceConnected() || discord.IsVoiceConnecting()) {
|
||||
m_menu_dm_join_voice.set_sensitive(false);
|
||||
m_menu_dm_disconnect_voice.set_sensitive(discord.GetVoiceChannelID() == id);
|
||||
} else {
|
||||
m_menu_dm_join_voice.set_sensitive(true);
|
||||
m_menu_dm_disconnect_voice.set_sensitive(false);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void ChannelList::OnThreadSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
|
||||
void ChannelList::OnThreadSubmenuPopup() {
|
||||
m_menu_thread_archive.set_visible(false);
|
||||
m_menu_thread_unarchive.set_visible(false);
|
||||
|
||||
@@ -961,6 +1103,22 @@ ChannelList::type_signal_action_guild_settings ChannelList::signal_action_guild_
|
||||
return m_signal_action_guild_settings;
|
||||
}
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
ChannelList::type_signal_action_open_new_tab ChannelList::signal_action_open_new_tab() {
|
||||
return m_signal_action_open_new_tab;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
ChannelList::type_signal_action_join_voice_channel ChannelList::signal_action_join_voice_channel() {
|
||||
return m_signal_action_join_voice_channel;
|
||||
}
|
||||
|
||||
ChannelList::type_signal_action_disconnect_voice ChannelList::signal_action_disconnect_voice() {
|
||||
return m_signal_action_disconnect_voice;
|
||||
}
|
||||
#endif
|
||||
|
||||
ChannelList::ModelColumns::ModelColumns() {
|
||||
add(m_type);
|
||||
add(m_id);
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
ChannelList();
|
||||
|
||||
void UpdateListing();
|
||||
void SetActiveChannel(Snowflake id);
|
||||
void SetActiveChannel(Snowflake id, bool expand_to);
|
||||
|
||||
// channel list should be populated when this is called
|
||||
void UseExpansionState(const ExpansionStateRoot &state);
|
||||
@@ -84,7 +84,7 @@ protected:
|
||||
|
||||
bool IsTextChannel(ChannelType type);
|
||||
|
||||
void OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path);
|
||||
void OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) const;
|
||||
void OnRowExpanded(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path);
|
||||
bool SelectionFunc(const Glib::RefPtr<Gtk::TreeModel> &model, const Gtk::TreeModel::Path &path, bool is_currently_selected);
|
||||
bool OnButtonPressEvent(GdkEventButton *ev);
|
||||
@@ -121,10 +121,28 @@ protected:
|
||||
Gtk::MenuItem m_menu_channel_mark_as_read;
|
||||
Gtk::MenuItem m_menu_channel_toggle_mute;
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
Gtk::MenuItem m_menu_channel_open_tab;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
Gtk::Menu m_menu_voice_channel;
|
||||
Gtk::MenuItem m_menu_voice_channel_join;
|
||||
Gtk::MenuItem m_menu_voice_channel_disconnect;
|
||||
#endif
|
||||
|
||||
Gtk::Menu m_menu_dm;
|
||||
Gtk::MenuItem m_menu_dm_copy_id;
|
||||
Gtk::MenuItem m_menu_dm_close;
|
||||
Gtk::MenuItem m_menu_dm_toggle_mute;
|
||||
#ifdef WITH_VOICE
|
||||
Gtk::MenuItem m_menu_dm_join_voice;
|
||||
Gtk::MenuItem m_menu_dm_disconnect_voice;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
Gtk::MenuItem m_menu_dm_open_tab;
|
||||
#endif
|
||||
|
||||
Gtk::Menu m_menu_thread;
|
||||
Gtk::MenuItem m_menu_thread_copy_id;
|
||||
@@ -134,11 +152,15 @@ protected:
|
||||
Gtk::MenuItem m_menu_thread_mark_as_read;
|
||||
Gtk::MenuItem m_menu_thread_toggle_mute;
|
||||
|
||||
void OnGuildSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
|
||||
void OnCategorySubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
|
||||
void OnChannelSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
|
||||
void OnDMSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
|
||||
void OnThreadSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
|
||||
void OnGuildSubmenuPopup();
|
||||
void OnCategorySubmenuPopup();
|
||||
void OnChannelSubmenuPopup();
|
||||
void OnDMSubmenuPopup();
|
||||
void OnThreadSubmenuPopup();
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void OnVoiceChannelSubmenuPopup();
|
||||
#endif
|
||||
|
||||
bool m_updating_listing = false;
|
||||
|
||||
@@ -149,16 +171,38 @@ protected:
|
||||
std::unordered_map<Snowflake, Gtk::TreeModel::iterator> m_tmp_channel_map;
|
||||
|
||||
public:
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_channel_item_select;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_guild_leave;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_guild_settings;
|
||||
using type_signal_action_channel_item_select = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_guild_leave = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_guild_settings = sigc::signal<void, Snowflake>;
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
using type_signal_action_open_new_tab = sigc::signal<void, Snowflake>;
|
||||
type_signal_action_open_new_tab signal_action_open_new_tab();
|
||||
#endif
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
using type_signal_action_join_voice_channel = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_disconnect_voice = sigc::signal<void>;
|
||||
|
||||
type_signal_action_join_voice_channel signal_action_join_voice_channel();
|
||||
type_signal_action_disconnect_voice signal_action_disconnect_voice();
|
||||
#endif
|
||||
|
||||
type_signal_action_channel_item_select signal_action_channel_item_select();
|
||||
type_signal_action_guild_leave signal_action_guild_leave();
|
||||
type_signal_action_guild_settings signal_action_guild_settings();
|
||||
|
||||
protected:
|
||||
private:
|
||||
type_signal_action_channel_item_select m_signal_action_channel_item_select;
|
||||
type_signal_action_guild_leave m_signal_action_guild_leave;
|
||||
type_signal_action_guild_settings m_signal_action_guild_settings;
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
type_signal_action_open_new_tab m_signal_action_open_new_tab;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
type_signal_action_join_voice_channel m_signal_action_join_voice_channel;
|
||||
type_signal_action_disconnect_voice m_signal_action_disconnect_voice;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -27,9 +27,6 @@ CellRendererChannels::CellRendererChannels()
|
||||
});
|
||||
}
|
||||
|
||||
CellRendererChannels::~CellRendererChannels() {
|
||||
}
|
||||
|
||||
Glib::PropertyProxy<RenderType> CellRendererChannels::property_type() {
|
||||
return m_property_type.get_proxy();
|
||||
}
|
||||
@@ -68,6 +65,12 @@ void CellRendererChannels::get_preferred_width_vfunc(Gtk::Widget &widget, int &m
|
||||
return get_preferred_width_vfunc_channel(widget, minimum_width, natural_width);
|
||||
case RenderType::Thread:
|
||||
return get_preferred_width_vfunc_thread(widget, minimum_width, natural_width);
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
return get_preferred_width_vfunc_voice_channel(widget, minimum_width, natural_width);
|
||||
case RenderType::VoiceParticipant:
|
||||
return get_preferred_width_vfunc_voice_participant(widget, minimum_width, natural_width);
|
||||
#endif
|
||||
case RenderType::DMHeader:
|
||||
return get_preferred_width_vfunc_dmheader(widget, minimum_width, natural_width);
|
||||
case RenderType::DM:
|
||||
@@ -85,6 +88,12 @@ void CellRendererChannels::get_preferred_width_for_height_vfunc(Gtk::Widget &wid
|
||||
return get_preferred_width_for_height_vfunc_channel(widget, height, minimum_width, natural_width);
|
||||
case RenderType::Thread:
|
||||
return get_preferred_width_for_height_vfunc_thread(widget, height, minimum_width, natural_width);
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
return get_preferred_width_for_height_vfunc_voice_channel(widget, height, minimum_width, natural_width);
|
||||
case RenderType::VoiceParticipant:
|
||||
return get_preferred_width_for_height_vfunc_voice_participant(widget, height, minimum_width, natural_width);
|
||||
#endif
|
||||
case RenderType::DMHeader:
|
||||
return get_preferred_width_for_height_vfunc_dmheader(widget, height, minimum_width, natural_width);
|
||||
case RenderType::DM:
|
||||
@@ -102,6 +111,12 @@ void CellRendererChannels::get_preferred_height_vfunc(Gtk::Widget &widget, int &
|
||||
return get_preferred_height_vfunc_channel(widget, minimum_height, natural_height);
|
||||
case RenderType::Thread:
|
||||
return get_preferred_height_vfunc_thread(widget, minimum_height, natural_height);
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
return get_preferred_height_vfunc_voice_channel(widget, minimum_height, natural_height);
|
||||
case RenderType::VoiceParticipant:
|
||||
return get_preferred_height_vfunc_voice_participant(widget, minimum_height, natural_height);
|
||||
#endif
|
||||
case RenderType::DMHeader:
|
||||
return get_preferred_height_vfunc_dmheader(widget, minimum_height, natural_height);
|
||||
case RenderType::DM:
|
||||
@@ -119,6 +134,12 @@ void CellRendererChannels::get_preferred_height_for_width_vfunc(Gtk::Widget &wid
|
||||
return get_preferred_height_for_width_vfunc_channel(widget, width, minimum_height, natural_height);
|
||||
case RenderType::Thread:
|
||||
return get_preferred_height_for_width_vfunc_thread(widget, width, minimum_height, natural_height);
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
return get_preferred_height_for_width_vfunc_voice_channel(widget, width, minimum_height, natural_height);
|
||||
case RenderType::VoiceParticipant:
|
||||
return get_preferred_height_for_width_vfunc_voice_participant(widget, width, minimum_height, natural_height);
|
||||
#endif
|
||||
case RenderType::DMHeader:
|
||||
return get_preferred_height_for_width_vfunc_dmheader(widget, width, minimum_height, natural_height);
|
||||
case RenderType::DM:
|
||||
@@ -136,6 +157,12 @@ void CellRendererChannels::render_vfunc(const Cairo::RefPtr<Cairo::Context> &cr,
|
||||
return render_vfunc_channel(cr, widget, background_area, cell_area, flags);
|
||||
case RenderType::Thread:
|
||||
return render_vfunc_thread(cr, widget, background_area, cell_area, flags);
|
||||
#ifdef WITH_VOICE
|
||||
case RenderType::VoiceChannel:
|
||||
return render_vfunc_voice_channel(cr, widget, background_area, cell_area, flags);
|
||||
case RenderType::VoiceParticipant:
|
||||
return render_vfunc_voice_participant(cr, widget, background_area, cell_area, flags);
|
||||
#endif
|
||||
case RenderType::DMHeader:
|
||||
return render_vfunc_dmheader(cr, widget, background_area, cell_area, flags);
|
||||
case RenderType::DM:
|
||||
@@ -212,7 +239,10 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtr<Cairo::Context
|
||||
const double text_w = text_natural.width;
|
||||
const double text_h = text_natural.height;
|
||||
|
||||
Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h);
|
||||
Gdk::Rectangle text_cell_area(static_cast<int>(text_x),
|
||||
static_cast<int>(text_y),
|
||||
static_cast<int>(text_w),
|
||||
static_cast<int>(text_h));
|
||||
|
||||
static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().ChannelColor);
|
||||
m_renderer_text.property_foreground_rgba() = color;
|
||||
@@ -231,7 +261,11 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtr<Cairo::Context
|
||||
|
||||
const auto cb = [this, &widget, anim, icon_x, icon_y, icon_w, icon_h] {
|
||||
if (m_pixbuf_anim_iters.at(anim)->advance())
|
||||
widget.queue_draw_area(icon_x, icon_y, icon_w, icon_h);
|
||||
widget.queue_draw_area(
|
||||
static_cast<int>(icon_x),
|
||||
static_cast<int>(icon_y),
|
||||
static_cast<int>(icon_w),
|
||||
static_cast<int>(icon_h));
|
||||
};
|
||||
|
||||
if ((hover_only && is_hovered) || !hover_only)
|
||||
@@ -264,12 +298,12 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtr<Cairo::Context
|
||||
const auto y = background_area.get_y();
|
||||
const auto w = background_area.get_width();
|
||||
const auto h = background_area.get_height();
|
||||
cr->rectangle(x, y + h / 2 - 24 / 2, 3, 24);
|
||||
cr->rectangle(x, y + h / 2.0 - 24.0 / 2.0, 3.0, 24.0);
|
||||
cr->fill();
|
||||
}
|
||||
|
||||
if (total_mentions < 1) return;
|
||||
auto *paned = static_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
auto *paned = dynamic_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
if (paned != nullptr) {
|
||||
const auto edge = std::min(paned->get_position(), background_area.get_width());
|
||||
|
||||
@@ -415,7 +449,7 @@ void CellRendererChannels::render_vfunc_channel(const Cairo::RefPtr<Cairo::Conte
|
||||
}
|
||||
|
||||
if (unread_state < 1) return;
|
||||
auto *paned = static_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
auto *paned = dynamic_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
if (paned != nullptr) {
|
||||
const auto edge = std::min(paned->get_position(), cell_area.get_width());
|
||||
|
||||
@@ -487,7 +521,7 @@ void CellRendererChannels::render_vfunc_thread(const Cairo::RefPtr<Cairo::Contex
|
||||
}
|
||||
|
||||
if (unread_state < 1) return;
|
||||
auto *paned = static_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
auto *paned = dynamic_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
if (paned != nullptr) {
|
||||
const auto edge = std::min(paned->get_position(), cell_area.get_width());
|
||||
|
||||
@@ -495,6 +529,76 @@ void CellRendererChannels::render_vfunc_thread(const Cairo::RefPtr<Cairo::Contex
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
|
||||
// voice channel
|
||||
|
||||
void CellRendererChannels::get_preferred_width_vfunc_voice_channel(Gtk::Widget &widget, int &minimum_width, int &natural_width) const {
|
||||
m_renderer_text.get_preferred_width(widget, minimum_width, natural_width);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_width_for_height_vfunc_voice_channel(Gtk::Widget &widget, int height, int &minimum_width, int &natural_width) const {
|
||||
m_renderer_text.get_preferred_width_for_height(widget, height, minimum_width, natural_width);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_height_vfunc_voice_channel(Gtk::Widget &widget, int &minimum_height, int &natural_height) const {
|
||||
m_renderer_text.get_preferred_height(widget, minimum_height, natural_height);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_height_for_width_vfunc_voice_channel(Gtk::Widget &widget, int width, int &minimum_height, int &natural_height) const {
|
||||
m_renderer_text.get_preferred_height_for_width(widget, width, minimum_height, natural_height);
|
||||
}
|
||||
|
||||
void CellRendererChannels::render_vfunc_voice_channel(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) {
|
||||
Gtk::Requisition minimum_size, natural_size;
|
||||
m_renderer_text.get_preferred_size(widget, minimum_size, natural_size);
|
||||
|
||||
const int text_x = background_area.get_x() + 21;
|
||||
const int text_y = background_area.get_y() + background_area.get_height() / 2 - natural_size.height / 2;
|
||||
const int text_w = natural_size.width;
|
||||
const int text_h = natural_size.height;
|
||||
|
||||
Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h);
|
||||
m_renderer_text.property_foreground_rgba() = Gdk::RGBA("#0f0");
|
||||
m_renderer_text.render(cr, widget, background_area, text_cell_area, flags);
|
||||
m_renderer_text.property_foreground_set() = false;
|
||||
}
|
||||
|
||||
// voice participant
|
||||
|
||||
void CellRendererChannels::get_preferred_width_vfunc_voice_participant(Gtk::Widget &widget, int &minimum_width, int &natural_width) const {
|
||||
m_renderer_text.get_preferred_width(widget, minimum_width, natural_width);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_width_for_height_vfunc_voice_participant(Gtk::Widget &widget, int height, int &minimum_width, int &natural_width) const {
|
||||
m_renderer_text.get_preferred_width_for_height(widget, height, minimum_width, natural_width);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_height_vfunc_voice_participant(Gtk::Widget &widget, int &minimum_height, int &natural_height) const {
|
||||
m_renderer_text.get_preferred_height(widget, minimum_height, natural_height);
|
||||
}
|
||||
|
||||
void CellRendererChannels::get_preferred_height_for_width_vfunc_voice_participant(Gtk::Widget &widget, int width, int &minimum_height, int &natural_height) const {
|
||||
m_renderer_text.get_preferred_height_for_width(widget, width, minimum_height, natural_height);
|
||||
}
|
||||
|
||||
void CellRendererChannels::render_vfunc_voice_participant(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) {
|
||||
Gtk::Requisition minimum_size, natural_size;
|
||||
m_renderer_text.get_preferred_size(widget, minimum_size, natural_size);
|
||||
|
||||
const int text_x = background_area.get_x() + 27;
|
||||
const int text_y = background_area.get_y() + background_area.get_height() / 2 - natural_size.height / 2;
|
||||
const int text_w = natural_size.width;
|
||||
const int text_h = natural_size.height;
|
||||
|
||||
Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h);
|
||||
m_renderer_text.property_foreground_rgba() = Gdk::RGBA("#f00");
|
||||
m_renderer_text.render(cr, widget, background_area, text_cell_area, flags);
|
||||
m_renderer_text.property_foreground_set() = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// dm header
|
||||
|
||||
void CellRendererChannels::get_preferred_width_vfunc_dmheader(Gtk::Widget &widget, int &minimum_width, int &natural_width) const {
|
||||
@@ -522,7 +626,7 @@ void CellRendererChannels::render_vfunc_dmheader(const Cairo::RefPtr<Cairo::Cont
|
||||
|
||||
if (!Abaddon::Get().GetSettings().Unreads) return;
|
||||
|
||||
auto *paned = static_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
auto *paned = dynamic_cast<Gtk::Paned *>(widget.get_ancestor(Gtk::Paned::get_type()));
|
||||
if (paned != nullptr) {
|
||||
const auto edge = std::min(paned->get_position(), background_area.get_width());
|
||||
if (const auto unread = Abaddon::Get().GetDiscordClient().GetUnreadDMsCount(); unread > 0)
|
||||
@@ -587,7 +691,10 @@ void CellRendererChannels::render_vfunc_dm(const Cairo::RefPtr<Cairo::Context> &
|
||||
const double text_w = text_natural.width;
|
||||
const double text_h = text_natural.height;
|
||||
|
||||
Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h);
|
||||
Gdk::Rectangle text_cell_area(static_cast<int>(text_x),
|
||||
static_cast<int>(text_y),
|
||||
static_cast<int>(text_w),
|
||||
static_cast<int>(text_h));
|
||||
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
const auto id = m_property_id.get_value();
|
||||
@@ -642,7 +749,7 @@ void CellRendererChannels::cairo_path_rounded_rect(const Cairo::RefPtr<Cairo::Co
|
||||
void CellRendererChannels::unread_render_mentions(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, int mentions, int edge, const Gdk::Rectangle &cell_area) {
|
||||
Pango::FontDescription font;
|
||||
font.set_family("sans 14");
|
||||
//font.set_weight(Pango::WEIGHT_BOLD);
|
||||
// font.set_weight(Pango::WEIGHT_BOLD);
|
||||
|
||||
auto layout = widget.create_pango_layout(std::to_string(mentions));
|
||||
layout->set_font_description(font);
|
||||
|
||||
@@ -11,6 +11,12 @@ enum class RenderType : uint8_t {
|
||||
TextChannel,
|
||||
Thread,
|
||||
|
||||
// TODO: maybe enable anyways but without ability to join if no voice support
|
||||
#ifdef WITH_VOICE
|
||||
VoiceChannel,
|
||||
VoiceParticipant,
|
||||
#endif
|
||||
|
||||
DMHeader,
|
||||
DM,
|
||||
};
|
||||
@@ -18,7 +24,7 @@ enum class RenderType : uint8_t {
|
||||
class CellRendererChannels : public Gtk::CellRenderer {
|
||||
public:
|
||||
CellRendererChannels();
|
||||
virtual ~CellRendererChannels();
|
||||
~CellRendererChannels() override = default;
|
||||
|
||||
Glib::PropertyProxy<RenderType> property_type();
|
||||
Glib::PropertyProxy<uint64_t> property_id();
|
||||
@@ -83,6 +89,30 @@ protected:
|
||||
const Gdk::Rectangle &cell_area,
|
||||
Gtk::CellRendererState flags);
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
// voice channel
|
||||
void get_preferred_width_vfunc_voice_channel(Gtk::Widget &widget, int &minimum_width, int &natural_width) const;
|
||||
void get_preferred_width_for_height_vfunc_voice_channel(Gtk::Widget &widget, int height, int &minimum_width, int &natural_width) const;
|
||||
void get_preferred_height_vfunc_voice_channel(Gtk::Widget &widget, int &minimum_height, int &natural_height) const;
|
||||
void get_preferred_height_for_width_vfunc_voice_channel(Gtk::Widget &widget, int width, int &minimum_height, int &natural_height) const;
|
||||
void render_vfunc_voice_channel(const Cairo::RefPtr<Cairo::Context> &cr,
|
||||
Gtk::Widget &widget,
|
||||
const Gdk::Rectangle &background_area,
|
||||
const Gdk::Rectangle &cell_area,
|
||||
Gtk::CellRendererState flags);
|
||||
|
||||
// voice channel
|
||||
void get_preferred_width_vfunc_voice_participant(Gtk::Widget &widget, int &minimum_width, int &natural_width) const;
|
||||
void get_preferred_width_for_height_vfunc_voice_participant(Gtk::Widget &widget, int height, int &minimum_width, int &natural_width) const;
|
||||
void get_preferred_height_vfunc_voice_participant(Gtk::Widget &widget, int &minimum_height, int &natural_height) const;
|
||||
void get_preferred_height_for_width_vfunc_voice_participant(Gtk::Widget &widget, int width, int &minimum_height, int &natural_height) const;
|
||||
void render_vfunc_voice_participant(const Cairo::RefPtr<Cairo::Context> &cr,
|
||||
Gtk::Widget &widget,
|
||||
const Gdk::Rectangle &background_area,
|
||||
const Gdk::Rectangle &cell_area,
|
||||
Gtk::CellRendererState flags);
|
||||
#endif
|
||||
|
||||
// dm header
|
||||
void get_preferred_width_vfunc_dmheader(Gtk::Widget &widget, int &minimum_width, int &natural_width) const;
|
||||
void get_preferred_width_for_height_vfunc_dmheader(Gtk::Widget &widget, int height, int &minimum_width, int &natural_width) const;
|
||||
@@ -106,7 +136,7 @@ protected:
|
||||
Gtk::CellRendererState flags);
|
||||
|
||||
static void cairo_path_rounded_rect(const Cairo::RefPtr<Cairo::Context> &cr, double x, double y, double w, double h, double r);
|
||||
void unread_render_mentions(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, int mentions, int edge, const Gdk::Rectangle &cell_area);
|
||||
static void unread_render_mentions(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, int mentions, int edge, const Gdk::Rectangle &cell_area);
|
||||
|
||||
private:
|
||||
Gtk::CellRendererText m_renderer_text;
|
||||
|
||||
246
src/components/channeltabswitcherhandy.cpp
Normal file
246
src/components/channeltabswitcherhandy.cpp
Normal file
@@ -0,0 +1,246 @@
|
||||
#ifdef WITH_LIBHANDY
|
||||
|
||||
#include "channeltabswitcherhandy.hpp"
|
||||
#include "abaddon.hpp"
|
||||
|
||||
void selected_page_notify_cb(HdyTabView *view, GParamSpec *pspec, ChannelTabSwitcherHandy *switcher) {
|
||||
auto *page = hdy_tab_view_get_selected_page(view);
|
||||
if (auto it = switcher->m_pages_rev.find(page); it != switcher->m_pages_rev.end()) {
|
||||
switcher->m_signal_channel_switched_to.emit(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
gboolean close_page_cb(HdyTabView *view, HdyTabPage *page, ChannelTabSwitcherHandy *switcher) {
|
||||
switcher->ClearPage(page);
|
||||
hdy_tab_view_close_page_finish(view, page, true);
|
||||
return GDK_EVENT_STOP;
|
||||
}
|
||||
|
||||
ChannelTabSwitcherHandy::ChannelTabSwitcherHandy() {
|
||||
m_tab_bar = hdy_tab_bar_new();
|
||||
m_tab_bar_wrapped = Glib::wrap(GTK_WIDGET(m_tab_bar));
|
||||
m_tab_view = hdy_tab_view_new();
|
||||
m_tab_view_wrapped = Glib::wrap(GTK_WIDGET(m_tab_view));
|
||||
|
||||
m_tab_bar_wrapped->get_style_context()->add_class("channel-tab-switcher");
|
||||
|
||||
g_signal_connect(m_tab_view, "notify::selected-page", G_CALLBACK(selected_page_notify_cb), this);
|
||||
g_signal_connect(m_tab_view, "close-page", G_CALLBACK(close_page_cb), this);
|
||||
|
||||
hdy_tab_bar_set_view(m_tab_bar, m_tab_view);
|
||||
add(*m_tab_bar_wrapped);
|
||||
m_tab_bar_wrapped->show();
|
||||
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
discord.signal_message_create().connect([this](const Message &data) {
|
||||
CheckUnread(data.ChannelID);
|
||||
});
|
||||
|
||||
discord.signal_message_ack().connect([this](const MessageAckData &data) {
|
||||
CheckUnread(data.ChannelID);
|
||||
});
|
||||
|
||||
discord.signal_channel_accessibility_changed().connect(sigc::mem_fun(*this, &ChannelTabSwitcherHandy::OnChannelAccessibilityChanged));
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::AddChannelTab(Snowflake id) {
|
||||
if (m_pages.find(id) != m_pages.end()) return;
|
||||
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
const auto channel = discord.GetChannel(id);
|
||||
if (!channel.has_value()) return;
|
||||
|
||||
auto *dummy = Gtk::make_managed<Gtk::Box>(); // minimal
|
||||
auto *page = hdy_tab_view_append(m_tab_view, GTK_WIDGET(dummy->gobj()));
|
||||
|
||||
hdy_tab_page_set_title(page, channel->GetDisplayName().c_str());
|
||||
hdy_tab_page_set_tooltip(page, nullptr);
|
||||
|
||||
m_pages[id] = page;
|
||||
m_pages_rev[page] = id;
|
||||
|
||||
CheckUnread(id);
|
||||
CheckPageIcon(page, *channel);
|
||||
AppendPageHistory(page, id);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::ReplaceActiveTab(Snowflake id) {
|
||||
auto *page = hdy_tab_view_get_selected_page(m_tab_view);
|
||||
if (page == nullptr) {
|
||||
AddChannelTab(id);
|
||||
} else if (auto it = m_pages.find(id); it != m_pages.end()) {
|
||||
hdy_tab_view_set_selected_page(m_tab_view, it->second);
|
||||
} else {
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
const auto channel = discord.GetChannel(id);
|
||||
if (!channel.has_value()) return;
|
||||
|
||||
hdy_tab_page_set_title(page, channel->GetDisplayName().c_str());
|
||||
|
||||
ClearPage(page);
|
||||
m_pages[id] = page;
|
||||
m_pages_rev[page] = id;
|
||||
|
||||
CheckUnread(id);
|
||||
CheckPageIcon(page, *channel);
|
||||
AppendPageHistory(page, id);
|
||||
}
|
||||
}
|
||||
|
||||
TabsState ChannelTabSwitcherHandy::GetTabsState() {
|
||||
TabsState state;
|
||||
|
||||
const gint num_pages = hdy_tab_view_get_n_pages(m_tab_view);
|
||||
for (gint i = 0; i < num_pages; i++) {
|
||||
auto *page = hdy_tab_view_get_nth_page(m_tab_view, i);
|
||||
if (page != nullptr) {
|
||||
if (const auto it = m_pages_rev.find(page); it != m_pages_rev.end()) {
|
||||
state.Channels.push_back(it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::UseTabsState(const TabsState &state) {
|
||||
for (auto id : state.Channels) {
|
||||
AddChannelTab(id);
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::GoBackOnCurrent() {
|
||||
AdvanceOnCurrent(-1);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::GoForwardOnCurrent() {
|
||||
AdvanceOnCurrent(1);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::GoToPreviousTab() {
|
||||
if (!hdy_tab_view_select_previous_page(m_tab_view)) {
|
||||
if (const auto num_pages = hdy_tab_view_get_n_pages(m_tab_view); num_pages > 1) {
|
||||
hdy_tab_view_set_selected_page(m_tab_view, hdy_tab_view_get_nth_page(m_tab_view, num_pages - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::GoToNextTab() {
|
||||
if (!hdy_tab_view_select_next_page(m_tab_view)) {
|
||||
if (hdy_tab_view_get_n_pages(m_tab_view) > 1) {
|
||||
hdy_tab_view_set_selected_page(m_tab_view, hdy_tab_view_get_nth_page(m_tab_view, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::GoToTab(int idx) {
|
||||
if (hdy_tab_view_get_n_pages(m_tab_view) >= idx + 1)
|
||||
hdy_tab_view_set_selected_page(m_tab_view, hdy_tab_view_get_nth_page(m_tab_view, idx));
|
||||
}
|
||||
|
||||
int ChannelTabSwitcherHandy::GetNumberOfTabs() const {
|
||||
return hdy_tab_view_get_n_pages(m_tab_view);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::CheckUnread(Snowflake id) {
|
||||
if (auto it = m_pages.find(id); it != m_pages.end()) {
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
const bool has_unreads = discord.GetUnreadStateForChannel(id) > -1;
|
||||
const bool show_indicator = has_unreads && !discord.IsChannelMuted(id);
|
||||
hdy_tab_page_set_needs_attention(it->second, show_indicator);
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::ClearPage(HdyTabPage *page) {
|
||||
if (auto it = m_pages_rev.find(page); it != m_pages_rev.end()) {
|
||||
m_pages.erase(it->second);
|
||||
}
|
||||
m_pages_rev.erase(page);
|
||||
m_page_icons.erase(page);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::OnPageIconLoad(HdyTabPage *page, const Glib::RefPtr<Gdk::Pixbuf> &pb) {
|
||||
auto new_pb = pb->scale_simple(16, 16, Gdk::INTERP_BILINEAR);
|
||||
m_page_icons[page] = new_pb;
|
||||
hdy_tab_page_set_icon(page, G_ICON(new_pb->gobj()));
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::CheckPageIcon(HdyTabPage *page, const ChannelData &data) {
|
||||
std::optional<std::string> icon_url;
|
||||
|
||||
if (data.GuildID.has_value()) {
|
||||
if (const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(*data.GuildID); guild.has_value() && guild->HasIcon()) {
|
||||
icon_url = guild->GetIconURL("png", "16");
|
||||
}
|
||||
} else if (data.IsDM()) {
|
||||
icon_url = data.GetIconURL();
|
||||
}
|
||||
|
||||
if (icon_url.has_value()) {
|
||||
auto *child_widget = hdy_tab_page_get_child(page);
|
||||
if (child_widget == nullptr) return; // probably wont happen :---)
|
||||
// i think this works???
|
||||
auto *trackable = Glib::wrap(GTK_WIDGET(child_widget));
|
||||
Abaddon::Get().GetImageManager().LoadFromURL(
|
||||
*icon_url,
|
||||
sigc::track_obj([this, page](const Glib::RefPtr<Gdk::Pixbuf> &pb) { OnPageIconLoad(page, pb); },
|
||||
*trackable));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
hdy_tab_page_set_icon(page, nullptr);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::AppendPageHistory(HdyTabPage *page, Snowflake channel) {
|
||||
auto it = m_page_history.find(page);
|
||||
if (it == m_page_history.end()) {
|
||||
m_page_history[page] = PageHistory { { channel }, 0 };
|
||||
return;
|
||||
}
|
||||
|
||||
// drop everything beyond current position
|
||||
it->second.Visited.resize(++it->second.CurrentVisitedIndex);
|
||||
it->second.Visited.push_back(channel);
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::AdvanceOnCurrent(size_t by) {
|
||||
auto *current = hdy_tab_view_get_selected_page(m_tab_view);
|
||||
if (current == nullptr) return;
|
||||
auto history = m_page_history.find(current);
|
||||
if (history == m_page_history.end()) return;
|
||||
if (by + history->second.CurrentVisitedIndex < 0 || by + history->second.CurrentVisitedIndex >= history->second.Visited.size()) return;
|
||||
|
||||
history->second.CurrentVisitedIndex += by;
|
||||
const auto to_id = history->second.Visited.at(history->second.CurrentVisitedIndex);
|
||||
|
||||
// temporarily point current index to the end so that it doesnt fuck up the history
|
||||
// remove it immediately after cuz the emit will call ReplaceActiveTab
|
||||
const auto real = history->second.CurrentVisitedIndex;
|
||||
history->second.CurrentVisitedIndex = history->second.Visited.size() - 1;
|
||||
m_signal_channel_switched_to.emit(to_id);
|
||||
// iterator might not be valid
|
||||
history = m_page_history.find(current);
|
||||
if (history != m_page_history.end()) {
|
||||
history->second.Visited.pop_back();
|
||||
}
|
||||
history->second.CurrentVisitedIndex = real;
|
||||
}
|
||||
|
||||
void ChannelTabSwitcherHandy::OnChannelAccessibilityChanged(Snowflake id, bool accessibility) {
|
||||
if (accessibility) return;
|
||||
if (auto it = m_pages.find(id); it != m_pages.end()) {
|
||||
if (hdy_tab_page_get_selected(it->second))
|
||||
if (!hdy_tab_view_select_previous_page(m_tab_view))
|
||||
hdy_tab_view_select_next_page(m_tab_view);
|
||||
|
||||
hdy_tab_view_close_page(m_tab_view, it->second);
|
||||
ClearPage(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
ChannelTabSwitcherHandy::type_signal_channel_switched_to ChannelTabSwitcherHandy::signal_channel_switched_to() {
|
||||
return m_signal_channel_switched_to;
|
||||
}
|
||||
|
||||
#endif
|
||||
71
src/components/channeltabswitcherhandy.hpp
Normal file
71
src/components/channeltabswitcherhandy.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
// perhaps this should be conditionally included within cmakelists?
|
||||
#ifdef WITH_LIBHANDY
|
||||
#include <gtkmm/box.h>
|
||||
#include <unordered_map>
|
||||
#include <handy.h>
|
||||
#include "discord/snowflake.hpp"
|
||||
#include "state.hpp"
|
||||
|
||||
class ChannelData;
|
||||
|
||||
// thin wrapper over c api
|
||||
// HdyTabBar + invisible HdyTabView since it needs one
|
||||
class ChannelTabSwitcherHandy : public Gtk::Box {
|
||||
public:
|
||||
ChannelTabSwitcherHandy();
|
||||
|
||||
// no-op if already added
|
||||
void AddChannelTab(Snowflake id);
|
||||
// switches to existing tab if it exists
|
||||
void ReplaceActiveTab(Snowflake id);
|
||||
TabsState GetTabsState();
|
||||
void UseTabsState(const TabsState &state);
|
||||
|
||||
void GoBackOnCurrent();
|
||||
void GoForwardOnCurrent();
|
||||
void GoToPreviousTab();
|
||||
void GoToNextTab();
|
||||
void GoToTab(int idx);
|
||||
|
||||
[[nodiscard]] int GetNumberOfTabs() const;
|
||||
|
||||
private:
|
||||
void CheckUnread(Snowflake id);
|
||||
void ClearPage(HdyTabPage *page);
|
||||
void OnPageIconLoad(HdyTabPage *page, const Glib::RefPtr<Gdk::Pixbuf> &pb);
|
||||
void CheckPageIcon(HdyTabPage *page, const ChannelData &data);
|
||||
void AppendPageHistory(HdyTabPage *page, Snowflake channel);
|
||||
void AdvanceOnCurrent(size_t by);
|
||||
|
||||
void OnChannelAccessibilityChanged(Snowflake id, bool accessibility);
|
||||
|
||||
HdyTabBar *m_tab_bar;
|
||||
Gtk::Widget *m_tab_bar_wrapped;
|
||||
HdyTabView *m_tab_view;
|
||||
Gtk::Widget *m_tab_view_wrapped;
|
||||
|
||||
std::unordered_map<Snowflake, HdyTabPage *> m_pages;
|
||||
std::unordered_map<HdyTabPage *, Snowflake> m_pages_rev;
|
||||
// need to hold a reference to the pixbuf data
|
||||
std::unordered_map<HdyTabPage *, Glib::RefPtr<Gdk::Pixbuf>> m_page_icons;
|
||||
|
||||
struct PageHistory {
|
||||
std::vector<Snowflake> Visited;
|
||||
size_t CurrentVisitedIndex;
|
||||
};
|
||||
|
||||
std::unordered_map<HdyTabPage *, PageHistory> m_page_history;
|
||||
|
||||
friend void selected_page_notify_cb(HdyTabView *, GParamSpec *, ChannelTabSwitcherHandy *);
|
||||
friend gboolean close_page_cb(HdyTabView *, HdyTabPage *, ChannelTabSwitcherHandy *);
|
||||
|
||||
public:
|
||||
using type_signal_channel_switched_to = sigc::signal<void, Snowflake>;
|
||||
|
||||
type_signal_channel_switched_to signal_channel_switched_to();
|
||||
|
||||
private:
|
||||
type_signal_channel_switched_to m_signal_channel_switched_to;
|
||||
};
|
||||
#endif
|
||||
@@ -1,11 +1,14 @@
|
||||
#include "chatinput.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
ChatInput::ChatInput() {
|
||||
ChatInputText::ChatInputText() {
|
||||
get_style_context()->add_class("message-input");
|
||||
set_propagate_natural_height(true);
|
||||
set_min_content_height(20);
|
||||
set_max_content_height(250);
|
||||
set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
|
||||
set_policy(Gtk::POLICY_EXTERNAL, Gtk::POLICY_AUTOMATIC);
|
||||
|
||||
// hack
|
||||
auto cb = [this](GdkEventKey *e) -> bool {
|
||||
@@ -20,22 +23,26 @@ ChatInput::ChatInput() {
|
||||
add(m_textview);
|
||||
}
|
||||
|
||||
void ChatInput::InsertText(const Glib::ustring &text) {
|
||||
void ChatInputText::InsertText(const Glib::ustring &text) {
|
||||
GetBuffer()->insert_at_cursor(text);
|
||||
m_textview.grab_focus();
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::TextBuffer> ChatInput::GetBuffer() {
|
||||
Glib::RefPtr<Gtk::TextBuffer> ChatInputText::GetBuffer() {
|
||||
return m_textview.get_buffer();
|
||||
}
|
||||
|
||||
// this isnt connected directly so that the chat window can handle stuff like the completer first
|
||||
bool ChatInput::ProcessKeyPress(GdkEventKey *event) {
|
||||
bool ChatInputText::ProcessKeyPress(GdkEventKey *event) {
|
||||
if (event->keyval == GDK_KEY_Escape) {
|
||||
m_signal_escape.emit();
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((event->state & GDK_CONTROL_MASK) && event->keyval == GDK_KEY_v) {
|
||||
return CheckHandleClipboardPaste();
|
||||
}
|
||||
|
||||
if (event->keyval == GDK_KEY_Return) {
|
||||
if (event->state & GDK_SHIFT_MASK)
|
||||
return false;
|
||||
@@ -53,10 +60,514 @@ bool ChatInput::ProcessKeyPress(GdkEventKey *event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ChatInput::on_grab_focus() {
|
||||
void ChatInputText::on_grab_focus() {
|
||||
m_textview.grab_focus();
|
||||
}
|
||||
|
||||
bool ChatInputText::CheckHandleClipboardPaste() {
|
||||
auto clip = Gtk::Clipboard::get();
|
||||
|
||||
if (!clip->wait_is_image_available()) return false;
|
||||
|
||||
const auto pb = clip->wait_for_image();
|
||||
if (pb) {
|
||||
m_signal_image_paste.emit(pb);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ChatInputText::type_signal_submit ChatInputText::signal_submit() {
|
||||
return m_signal_submit;
|
||||
}
|
||||
|
||||
ChatInputText::type_signal_escape ChatInputText::signal_escape() {
|
||||
return m_signal_escape;
|
||||
}
|
||||
|
||||
ChatInputText::type_signal_image_paste ChatInputText::signal_image_paste() {
|
||||
return m_signal_image_paste;
|
||||
}
|
||||
|
||||
ChatInputTextContainer::ChatInputTextContainer() {
|
||||
// triple hack !!!
|
||||
auto cb = [this](GdkEventKey *e) -> bool {
|
||||
return event(reinterpret_cast<GdkEvent *>(e));
|
||||
};
|
||||
m_input.signal_key_press_event().connect(cb, false);
|
||||
|
||||
m_upload_img.property_icon_name() = "document-send-symbolic";
|
||||
m_upload_img.property_icon_size() = Gtk::ICON_SIZE_LARGE_TOOLBAR;
|
||||
m_upload_img.get_style_context()->add_class("message-input-browse-icon");
|
||||
|
||||
AddPointerCursor(m_upload_ev);
|
||||
|
||||
m_upload_ev.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
||||
if (ev->button == GDK_BUTTON_PRIMARY) {
|
||||
ShowFileChooser();
|
||||
// return focus
|
||||
m_input.grab_focus();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
m_upload_ev.add(m_upload_img);
|
||||
add_overlay(m_upload_ev);
|
||||
add(m_input);
|
||||
|
||||
show_all_children();
|
||||
|
||||
// stop the overlay from using (start) padding
|
||||
signal_get_child_position().connect(sigc::mem_fun(*this, &ChatInputTextContainer::GetChildPosition), false);
|
||||
}
|
||||
|
||||
void ChatInputTextContainer::ShowFileChooser() {
|
||||
auto dlg = Gtk::FileChooserNative::create("Choose file", Gtk::FILE_CHOOSER_ACTION_OPEN);
|
||||
dlg->set_select_multiple(true);
|
||||
dlg->set_modal(true);
|
||||
|
||||
dlg->signal_response().connect([this, dlg](int response) {
|
||||
if (response == Gtk::RESPONSE_ACCEPT) {
|
||||
for (const auto &file : dlg->get_files()) {
|
||||
m_signal_add_attachment.emit(file);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
auto filter_all = Gtk::FileFilter::create();
|
||||
filter_all->set_name("All files (*.*)");
|
||||
filter_all->add_pattern("*.*");
|
||||
dlg->add_filter(filter_all);
|
||||
|
||||
dlg->run();
|
||||
}
|
||||
|
||||
ChatInputText &ChatInputTextContainer::Get() {
|
||||
return m_input;
|
||||
}
|
||||
|
||||
void ChatInputTextContainer::ShowChooserIcon() {
|
||||
m_upload_ev.show();
|
||||
}
|
||||
|
||||
void ChatInputTextContainer::HideChooserIcon() {
|
||||
m_upload_ev.hide();
|
||||
}
|
||||
|
||||
bool ChatInputTextContainer::GetChildPosition(Gtk::Widget *child, Gdk::Rectangle &pos) {
|
||||
Gtk::Allocation main_alloc;
|
||||
{
|
||||
auto *grandchild = m_input.get_child();
|
||||
int x, y;
|
||||
if (grandchild->translate_coordinates(m_input, 0, 0, x, y)) {
|
||||
main_alloc.set_x(x);
|
||||
main_alloc.set_y(y);
|
||||
} else {
|
||||
main_alloc.set_x(0);
|
||||
main_alloc.set_y(0);
|
||||
}
|
||||
main_alloc.set_width(grandchild->get_allocated_width());
|
||||
main_alloc.set_height(grandchild->get_allocated_height());
|
||||
}
|
||||
|
||||
Gtk::Requisition min, req;
|
||||
child->get_preferred_size(min, req);
|
||||
|
||||
// let css move it around
|
||||
pos.set_x(0);
|
||||
pos.set_y(0);
|
||||
pos.set_width(std::max(min.width, std::min(main_alloc.get_width(), req.width)));
|
||||
pos.set_height(std::max(min.height, std::min(main_alloc.get_height(), req.height)));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ChatInputTextContainer::type_signal_add_attachment ChatInputTextContainer::signal_add_attachment() {
|
||||
return m_signal_add_attachment;
|
||||
}
|
||||
|
||||
ChatInputAttachmentContainer::ChatInputAttachmentContainer()
|
||||
: m_box(Gtk::ORIENTATION_HORIZONTAL) {
|
||||
get_style_context()->add_class("attachment-container");
|
||||
|
||||
m_box.set_halign(Gtk::ALIGN_START);
|
||||
|
||||
add(m_box);
|
||||
m_box.show();
|
||||
|
||||
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER);
|
||||
set_vexpand(true);
|
||||
set_size_request(-1, AttachmentItemSize + 10);
|
||||
}
|
||||
|
||||
void ChatInputAttachmentContainer::Clear() {
|
||||
for (auto *item : m_attachments) {
|
||||
item->RemoveIfTemp();
|
||||
delete item;
|
||||
}
|
||||
m_attachments.clear();
|
||||
}
|
||||
|
||||
void ChatInputAttachmentContainer::ClearNoPurge() {
|
||||
for (auto *item : m_attachments) {
|
||||
delete item;
|
||||
}
|
||||
m_attachments.clear();
|
||||
}
|
||||
|
||||
bool ChatInputAttachmentContainer::AddImage(const Glib::RefPtr<Gdk::Pixbuf> &pb) {
|
||||
if (m_attachments.size() == 10) return false;
|
||||
|
||||
static unsigned go_up = 0;
|
||||
std::string dest_name = "pasted-image-" + std::to_string(go_up++);
|
||||
const auto path = (std::filesystem::temp_directory_path() / "abaddon-cache" / dest_name).string();
|
||||
|
||||
try {
|
||||
pb->save(path, "png");
|
||||
} catch (...) {
|
||||
fprintf(stderr, "pasted image save error\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto *item = Gtk::make_managed<ChatInputAttachmentItem>(Gio::File::create_for_path(path), pb);
|
||||
item->set_valign(Gtk::ALIGN_FILL);
|
||||
item->set_vexpand(true);
|
||||
item->set_margin_bottom(5);
|
||||
item->show();
|
||||
m_box.add(*item);
|
||||
|
||||
m_attachments.push_back(item);
|
||||
|
||||
item->signal_item_removed().connect([this, item] {
|
||||
item->RemoveIfTemp();
|
||||
if (auto it = std::find(m_attachments.begin(), m_attachments.end(), item); it != m_attachments.end())
|
||||
m_attachments.erase(it);
|
||||
delete item;
|
||||
if (m_attachments.empty())
|
||||
m_signal_emptied.emit();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ChatInputAttachmentContainer::AddFile(const Glib::RefPtr<Gio::File> &file, Glib::RefPtr<Gdk::Pixbuf> pb) {
|
||||
if (m_attachments.size() == 10) return false;
|
||||
|
||||
ChatInputAttachmentItem *item;
|
||||
if (pb)
|
||||
item = Gtk::make_managed<ChatInputAttachmentItem>(file, pb, true);
|
||||
else
|
||||
item = Gtk::make_managed<ChatInputAttachmentItem>(file);
|
||||
item->set_valign(Gtk::ALIGN_FILL);
|
||||
item->set_vexpand(true);
|
||||
item->set_margin_bottom(5);
|
||||
item->show();
|
||||
m_box.add(*item);
|
||||
|
||||
m_attachments.push_back(item);
|
||||
|
||||
item->signal_item_removed().connect([this, item] {
|
||||
if (auto it = std::find(m_attachments.begin(), m_attachments.end(), item); it != m_attachments.end())
|
||||
m_attachments.erase(it);
|
||||
delete item;
|
||||
if (m_attachments.empty())
|
||||
m_signal_emptied.emit();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<ChatSubmitParams::Attachment> ChatInputAttachmentContainer::GetAttachments() const {
|
||||
std::vector<ChatSubmitParams::Attachment> ret;
|
||||
for (auto *x : m_attachments) {
|
||||
if (!x->GetFile()->query_exists())
|
||||
puts("bad!");
|
||||
ret.push_back({ x->GetFile(), x->GetType(), x->GetFilename() });
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ChatInputAttachmentContainer::type_signal_emptied ChatInputAttachmentContainer::signal_emptied() {
|
||||
return m_signal_emptied;
|
||||
}
|
||||
|
||||
ChatInputAttachmentItem::ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file)
|
||||
: m_file(file)
|
||||
, m_img(Gtk::make_managed<Gtk::Image>())
|
||||
, m_type(ChatSubmitParams::ExtantFile)
|
||||
, m_box(Gtk::ORIENTATION_VERTICAL) {
|
||||
get_style_context()->add_class("attachment-item");
|
||||
|
||||
set_size_request(AttachmentItemSize, AttachmentItemSize);
|
||||
set_halign(Gtk::ALIGN_START);
|
||||
m_box.set_hexpand(true);
|
||||
m_box.set_vexpand(true);
|
||||
m_box.set_halign(Gtk::ALIGN_FILL);
|
||||
m_box.set_valign(Gtk::ALIGN_FILL);
|
||||
m_box.add(*m_img);
|
||||
m_box.add(m_label);
|
||||
add(m_box);
|
||||
show_all_children();
|
||||
|
||||
m_label.set_valign(Gtk::ALIGN_END);
|
||||
m_label.set_max_width_chars(0); // will constrain to given size
|
||||
m_label.set_ellipsize(Pango::ELLIPSIZE_MIDDLE);
|
||||
m_label.set_margin_start(7);
|
||||
m_label.set_margin_end(7);
|
||||
|
||||
m_img->set_vexpand(true);
|
||||
m_img->property_icon_name() = "document-send-symbolic";
|
||||
m_img->property_icon_size() = Gtk::ICON_SIZE_DIALOG; // todo figure out how to not use this weird property??? i dont know how icons work (screw your theme)
|
||||
|
||||
SetFilenameFromFile();
|
||||
|
||||
SetupMenu();
|
||||
UpdateTooltip();
|
||||
}
|
||||
|
||||
ChatInputAttachmentItem::ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file, const Glib::RefPtr<Gdk::Pixbuf> &pb, bool is_extant)
|
||||
: m_file(file)
|
||||
, m_img(Gtk::make_managed<Gtk::Image>())
|
||||
, m_type(is_extant ? ChatSubmitParams::ExtantFile : ChatSubmitParams::PastedImage)
|
||||
, m_filename("unknown.png")
|
||||
, m_label("unknown.png")
|
||||
, m_box(Gtk::ORIENTATION_VERTICAL) {
|
||||
get_style_context()->add_class("attachment-item");
|
||||
|
||||
int outw, outh;
|
||||
GetImageDimensions(pb->get_width(), pb->get_height(), outw, outh, AttachmentItemSize, AttachmentItemSize);
|
||||
m_img->property_pixbuf() = pb->scale_simple(outw, outh, Gdk::INTERP_BILINEAR);
|
||||
|
||||
set_size_request(AttachmentItemSize, AttachmentItemSize);
|
||||
set_halign(Gtk::ALIGN_START);
|
||||
m_box.set_hexpand(true);
|
||||
m_box.set_vexpand(true);
|
||||
m_box.set_halign(Gtk::ALIGN_FILL);
|
||||
m_box.set_valign(Gtk::ALIGN_FILL);
|
||||
m_box.add(*m_img);
|
||||
m_box.add(m_label);
|
||||
add(m_box);
|
||||
show_all_children();
|
||||
|
||||
m_label.set_valign(Gtk::ALIGN_END);
|
||||
m_label.set_max_width_chars(0); // will constrain to given size
|
||||
m_label.set_ellipsize(Pango::ELLIPSIZE_MIDDLE);
|
||||
m_label.set_margin_start(7);
|
||||
m_label.set_margin_end(7);
|
||||
|
||||
m_img->set_vexpand(true);
|
||||
|
||||
if (is_extant)
|
||||
SetFilenameFromFile();
|
||||
|
||||
SetupMenu();
|
||||
UpdateTooltip();
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gio::File> ChatInputAttachmentItem::GetFile() const {
|
||||
return m_file;
|
||||
}
|
||||
|
||||
ChatSubmitParams::AttachmentType ChatInputAttachmentItem::GetType() const {
|
||||
return m_type;
|
||||
}
|
||||
|
||||
std::string ChatInputAttachmentItem::GetFilename() const {
|
||||
return m_filename;
|
||||
}
|
||||
|
||||
bool ChatInputAttachmentItem::IsTemp() const noexcept {
|
||||
return m_type == ChatSubmitParams::PastedImage;
|
||||
}
|
||||
|
||||
void ChatInputAttachmentItem::RemoveIfTemp() {
|
||||
if (IsTemp())
|
||||
m_file->remove();
|
||||
}
|
||||
|
||||
void ChatInputAttachmentItem::SetFilenameFromFile() {
|
||||
auto info = m_file->query_info(G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
|
||||
m_filename = info->get_attribute_string(G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
|
||||
m_label.set_text(m_filename);
|
||||
}
|
||||
|
||||
void ChatInputAttachmentItem::SetupMenu() {
|
||||
m_menu_remove.set_label("Remove");
|
||||
m_menu_remove.signal_activate().connect([this] {
|
||||
m_signal_item_removed.emit();
|
||||
});
|
||||
|
||||
m_menu_set_filename.set_label("Change Filename");
|
||||
m_menu_set_filename.signal_activate().connect([this] {
|
||||
const auto name = Abaddon::Get().ShowTextPrompt("Enter new filename for attachment", "Enter filename", m_filename);
|
||||
if (name.has_value()) {
|
||||
m_filename = *name;
|
||||
m_label.set_text(m_filename);
|
||||
UpdateTooltip();
|
||||
}
|
||||
});
|
||||
|
||||
m_menu.add(m_menu_set_filename);
|
||||
m_menu.add(m_menu_remove);
|
||||
m_menu.show_all();
|
||||
|
||||
signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
||||
if (ev->button == GDK_BUTTON_SECONDARY) {
|
||||
m_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
void ChatInputAttachmentItem::UpdateTooltip() {
|
||||
set_tooltip_text(m_filename);
|
||||
}
|
||||
|
||||
ChatInputAttachmentItem::type_signal_item_removed ChatInputAttachmentItem::signal_item_removed() {
|
||||
return m_signal_item_removed;
|
||||
}
|
||||
|
||||
ChatInput::ChatInput()
|
||||
: Gtk::Box(Gtk::ORIENTATION_VERTICAL) {
|
||||
m_input.signal_add_attachment().connect(sigc::mem_fun(*this, &ChatInput::AddAttachment));
|
||||
|
||||
m_input.Get().signal_escape().connect([this] {
|
||||
m_attachments.Clear();
|
||||
m_attachments_revealer.set_reveal_child(false);
|
||||
m_signal_escape.emit();
|
||||
});
|
||||
|
||||
m_input.Get().signal_submit().connect([this](const Glib::ustring &input) -> bool {
|
||||
ChatSubmitParams data;
|
||||
data.Message = input;
|
||||
data.Attachments = m_attachments.GetAttachments();
|
||||
|
||||
bool b = m_signal_submit.emit(data);
|
||||
if (b) {
|
||||
m_attachments_revealer.set_reveal_child(false);
|
||||
m_attachments.ClearNoPurge();
|
||||
}
|
||||
return b;
|
||||
});
|
||||
|
||||
m_attachments.set_vexpand(false);
|
||||
|
||||
m_attachments_revealer.set_transition_type(Gtk::REVEALER_TRANSITION_TYPE_SLIDE_UP);
|
||||
m_attachments_revealer.add(m_attachments);
|
||||
add(m_attachments_revealer);
|
||||
add(m_input);
|
||||
show_all_children();
|
||||
|
||||
m_input.Get().signal_image_paste().connect([this](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
|
||||
if (CanAttachFiles() && m_attachments.AddImage(pb))
|
||||
m_attachments_revealer.set_reveal_child(true);
|
||||
});
|
||||
|
||||
// double hack !
|
||||
auto cb = [this](GdkEventKey *e) -> bool {
|
||||
return event(reinterpret_cast<GdkEvent *>(e));
|
||||
};
|
||||
m_input.signal_key_press_event().connect(cb, false);
|
||||
|
||||
m_attachments.signal_emptied().connect([this] {
|
||||
m_attachments_revealer.set_reveal_child(false);
|
||||
});
|
||||
|
||||
SetActiveChannel(Snowflake::Invalid);
|
||||
}
|
||||
|
||||
void ChatInput::InsertText(const Glib::ustring &text) {
|
||||
m_input.Get().InsertText(text);
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::TextBuffer> ChatInput::GetBuffer() {
|
||||
return m_input.Get().GetBuffer();
|
||||
}
|
||||
|
||||
bool ChatInput::ProcessKeyPress(GdkEventKey *event) {
|
||||
return m_input.Get().ProcessKeyPress(event);
|
||||
}
|
||||
|
||||
void ChatInput::AddAttachment(const Glib::RefPtr<Gio::File> &file) {
|
||||
if (!CanAttachFiles()) return;
|
||||
|
||||
std::string content_type;
|
||||
|
||||
try {
|
||||
const auto info = file->query_info(G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
|
||||
content_type = info->get_attribute_string(G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
|
||||
} catch (const Gio::Error &err) {
|
||||
printf("io error: %s\n", err.what().c_str());
|
||||
return;
|
||||
} catch (...) {
|
||||
puts("attachment query exception");
|
||||
return;
|
||||
}
|
||||
|
||||
static const std::unordered_set<std::string> image_exts {
|
||||
".png",
|
||||
".jpg",
|
||||
};
|
||||
|
||||
if (image_exts.find(content_type) != image_exts.end()) {
|
||||
if (AddFileAsImageAttachment(file)) {
|
||||
m_attachments_revealer.set_reveal_child(true);
|
||||
m_input.Get().grab_focus();
|
||||
}
|
||||
} else if (m_attachments.AddFile(file)) {
|
||||
m_attachments_revealer.set_reveal_child(true);
|
||||
m_input.Get().grab_focus();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatInput::IndicateTooLarge() {
|
||||
m_input.Get().get_style_context()->add_class("bad-input");
|
||||
const auto cb = [this] {
|
||||
m_input.Get().get_style_context()->remove_class("bad-input");
|
||||
};
|
||||
Glib::signal_timeout().connect_seconds_once(sigc::track_obj(cb, *this), 2);
|
||||
}
|
||||
|
||||
void ChatInput::SetActiveChannel(Snowflake id) {
|
||||
m_active_channel = id;
|
||||
if (CanAttachFiles()) {
|
||||
m_input.Get().get_style_context()->add_class("with-browse-icon");
|
||||
m_input.ShowChooserIcon();
|
||||
} else {
|
||||
m_input.Get().get_style_context()->remove_class("with-browse-icon");
|
||||
m_input.HideChooserIcon();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatInput::StartReplying() {
|
||||
m_input.Get().grab_focus();
|
||||
m_input.Get().get_style_context()->add_class("replying");
|
||||
}
|
||||
|
||||
void ChatInput::StopReplying() {
|
||||
m_input.Get().get_style_context()->remove_class("replying");
|
||||
}
|
||||
|
||||
bool ChatInput::AddFileAsImageAttachment(const Glib::RefPtr<Gio::File> &file) {
|
||||
try {
|
||||
const auto read_stream = file->read();
|
||||
if (!read_stream) return false;
|
||||
const auto pb = Gdk::Pixbuf::create_from_stream(read_stream);
|
||||
return m_attachments.AddFile(file, pb);
|
||||
} catch (...) {
|
||||
return m_attachments.AddFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
bool ChatInput::CanAttachFiles() {
|
||||
return Abaddon::Get().GetDiscordClient().HasSelfChannelPermission(m_active_channel, Permission::ATTACH_FILES | Permission::SEND_MESSAGES);
|
||||
}
|
||||
|
||||
ChatInput::type_signal_submit ChatInput::signal_submit() {
|
||||
return m_signal_submit;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,72 @@
|
||||
#pragma once
|
||||
#include <gtkmm.h>
|
||||
#include "discord/chatsubmitparams.hpp"
|
||||
#include "discord/permissions.hpp"
|
||||
|
||||
class ChatInput : public Gtk::ScrolledWindow {
|
||||
class ChatInputAttachmentItem : public Gtk::EventBox {
|
||||
public:
|
||||
ChatInput();
|
||||
ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file);
|
||||
ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file, const Glib::RefPtr<Gdk::Pixbuf> &pb, bool is_extant = false);
|
||||
|
||||
[[nodiscard]] Glib::RefPtr<Gio::File> GetFile() const;
|
||||
[[nodiscard]] ChatSubmitParams::AttachmentType GetType() const;
|
||||
[[nodiscard]] std::string GetFilename() const;
|
||||
[[nodiscard]] bool IsTemp() const noexcept;
|
||||
void RemoveIfTemp();
|
||||
|
||||
private:
|
||||
void SetFilenameFromFile();
|
||||
void SetupMenu();
|
||||
void UpdateTooltip();
|
||||
|
||||
Gtk::Menu m_menu;
|
||||
Gtk::MenuItem m_menu_remove;
|
||||
Gtk::MenuItem m_menu_set_filename;
|
||||
|
||||
Gtk::Box m_box;
|
||||
Gtk::Label m_label;
|
||||
Gtk::Image *m_img = nullptr;
|
||||
|
||||
Glib::RefPtr<Gio::File> m_file;
|
||||
ChatSubmitParams::AttachmentType m_type;
|
||||
std::string m_filename;
|
||||
|
||||
private:
|
||||
using type_signal_item_removed = sigc::signal<void>;
|
||||
|
||||
type_signal_item_removed m_signal_item_removed;
|
||||
|
||||
public:
|
||||
type_signal_item_removed signal_item_removed();
|
||||
};
|
||||
|
||||
class ChatInputAttachmentContainer : public Gtk::ScrolledWindow {
|
||||
public:
|
||||
ChatInputAttachmentContainer();
|
||||
|
||||
void Clear();
|
||||
void ClearNoPurge();
|
||||
bool AddImage(const Glib::RefPtr<Gdk::Pixbuf> &pb);
|
||||
bool AddFile(const Glib::RefPtr<Gio::File> &file, Glib::RefPtr<Gdk::Pixbuf> pb = {});
|
||||
[[nodiscard]] std::vector<ChatSubmitParams::Attachment> GetAttachments() const;
|
||||
|
||||
private:
|
||||
std::vector<ChatInputAttachmentItem *> m_attachments;
|
||||
|
||||
Gtk::Box m_box;
|
||||
|
||||
private:
|
||||
using type_signal_emptied = sigc::signal<void>;
|
||||
|
||||
type_signal_emptied m_signal_emptied;
|
||||
|
||||
public:
|
||||
type_signal_emptied signal_emptied();
|
||||
};
|
||||
|
||||
class ChatInputText : public Gtk::ScrolledWindow {
|
||||
public:
|
||||
ChatInputText();
|
||||
|
||||
void InsertText(const Glib::ustring &text);
|
||||
Glib::RefPtr<Gtk::TextBuffer> GetBuffer();
|
||||
@@ -15,9 +78,78 @@ protected:
|
||||
private:
|
||||
Gtk::TextView m_textview;
|
||||
|
||||
bool CheckHandleClipboardPaste();
|
||||
|
||||
public:
|
||||
typedef sigc::signal<bool, Glib::ustring> type_signal_submit;
|
||||
typedef sigc::signal<void> type_signal_escape;
|
||||
using type_signal_submit = sigc::signal<bool, Glib::ustring>;
|
||||
using type_signal_escape = sigc::signal<void>;
|
||||
using type_signal_image_paste = sigc::signal<void, Glib::RefPtr<Gdk::Pixbuf>>;
|
||||
|
||||
type_signal_submit signal_submit();
|
||||
type_signal_escape signal_escape();
|
||||
type_signal_image_paste signal_image_paste();
|
||||
|
||||
private:
|
||||
type_signal_submit m_signal_submit;
|
||||
type_signal_escape m_signal_escape;
|
||||
type_signal_image_paste m_signal_image_paste;
|
||||
};
|
||||
|
||||
// file upload, text
|
||||
class ChatInputTextContainer : public Gtk::Overlay {
|
||||
public:
|
||||
ChatInputTextContainer();
|
||||
|
||||
// not proxying everythign lol!!
|
||||
ChatInputText &Get();
|
||||
|
||||
void ShowChooserIcon();
|
||||
void HideChooserIcon();
|
||||
|
||||
private:
|
||||
void ShowFileChooser();
|
||||
bool GetChildPosition(Gtk::Widget *child, Gdk::Rectangle &pos);
|
||||
|
||||
Gtk::EventBox m_upload_ev;
|
||||
Gtk::Image m_upload_img;
|
||||
ChatInputText m_input;
|
||||
|
||||
public:
|
||||
using type_signal_add_attachment = sigc::signal<void, Glib::RefPtr<Gio::File>>;
|
||||
type_signal_add_attachment signal_add_attachment();
|
||||
|
||||
private:
|
||||
type_signal_add_attachment m_signal_add_attachment;
|
||||
};
|
||||
|
||||
class ChatInput : public Gtk::Box {
|
||||
public:
|
||||
ChatInput();
|
||||
|
||||
void InsertText(const Glib::ustring &text);
|
||||
Glib::RefPtr<Gtk::TextBuffer> GetBuffer();
|
||||
bool ProcessKeyPress(GdkEventKey *event);
|
||||
void AddAttachment(const Glib::RefPtr<Gio::File> &file);
|
||||
void IndicateTooLarge();
|
||||
|
||||
void SetActiveChannel(Snowflake id);
|
||||
|
||||
void StartReplying();
|
||||
void StopReplying();
|
||||
|
||||
private:
|
||||
bool AddFileAsImageAttachment(const Glib::RefPtr<Gio::File> &file);
|
||||
bool CanAttachFiles();
|
||||
|
||||
Gtk::Revealer m_attachments_revealer;
|
||||
ChatInputAttachmentContainer m_attachments;
|
||||
ChatInputTextContainer m_input;
|
||||
|
||||
Snowflake m_active_channel;
|
||||
|
||||
public:
|
||||
using type_signal_submit = sigc::signal<bool, ChatSubmitParams>;
|
||||
using type_signal_escape = sigc::signal<void>;
|
||||
|
||||
type_signal_submit signal_submit();
|
||||
type_signal_escape signal_escape();
|
||||
|
||||
@@ -25,16 +25,16 @@ ChatInputIndicator::ChatInputIndicator()
|
||||
if (!std::filesystem::exists(path)) return;
|
||||
auto gif_data = ReadWholeFile(path);
|
||||
auto loader = Gdk::PixbufLoader::create();
|
||||
loader->signal_size_prepared().connect([&](int inw, int inh) {
|
||||
int w, h;
|
||||
GetImageDimensions(inw, inh, w, h, 20, 10);
|
||||
loader->set_size(w, h);
|
||||
});
|
||||
loader->write(gif_data.data(), gif_data.size());
|
||||
try {
|
||||
loader->signal_size_prepared().connect([&](int inw, int inh) {
|
||||
int w, h;
|
||||
GetImageDimensions(inw, inh, w, h, 20, 10);
|
||||
loader->set_size(w, h);
|
||||
});
|
||||
loader->write(gif_data.data(), gif_data.size());
|
||||
loader->close();
|
||||
m_img.property_pixbuf_animation() = loader->get_animation();
|
||||
} catch (const std::exception &) {}
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
void ChatInputIndicator::AddUser(Snowflake channel_id, const UserData &user, int timeout) {
|
||||
@@ -84,14 +84,14 @@ void ChatInputIndicator::OnMessageCreate(const Message &message) {
|
||||
|
||||
void ChatInputIndicator::SetTypingString(const Glib::ustring &str) {
|
||||
m_label.set_text(str);
|
||||
if (str == "")
|
||||
if (str.empty())
|
||||
m_img.hide();
|
||||
else if (m_img.property_pixbuf_animation().get_value())
|
||||
m_img.show();
|
||||
}
|
||||
|
||||
void ChatInputIndicator::ComputeTypingString() {
|
||||
if (m_custom_markup != "") {
|
||||
if (!m_custom_markup.empty()) {
|
||||
m_label.set_markup(m_custom_markup);
|
||||
m_img.hide();
|
||||
return;
|
||||
@@ -104,7 +104,7 @@ void ChatInputIndicator::ComputeTypingString() {
|
||||
if (user.has_value())
|
||||
typers.push_back(*user);
|
||||
}
|
||||
if (typers.size() == 0) {
|
||||
if (typers.empty()) {
|
||||
SetTypingString("");
|
||||
} else if (typers.size() == 1) {
|
||||
SetTypingString(typers[0].Username + " is typing...");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "chatmessage.hpp"
|
||||
#include "chatlist.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "chatmessage.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
ChatList::ChatList() {
|
||||
@@ -8,17 +8,11 @@ ChatList::ChatList() {
|
||||
|
||||
set_can_focus(false);
|
||||
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
|
||||
signal_edge_reached().connect(sigc::mem_fun(*this, &ChatList::OnScrollEdgeOvershot));
|
||||
|
||||
auto v = get_vadjustment();
|
||||
v->signal_value_changed().connect([this, v] {
|
||||
m_should_scroll_to_bottom = v->get_upper() - v->get_page_size() <= v->get_value();
|
||||
});
|
||||
get_vadjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &ChatList::OnVAdjustmentValueChanged));
|
||||
get_vadjustment()->property_upper().signal_changed().connect(sigc::mem_fun(*this, &ChatList::OnVAdjustmentUpperChanged));
|
||||
|
||||
m_list.signal_size_allocate().connect([this](Gtk::Allocation &) {
|
||||
if (m_should_scroll_to_bottom)
|
||||
ScrollToBottom();
|
||||
});
|
||||
m_list.signal_size_allocate().connect(sigc::mem_fun(*this, &ChatList::OnListSizeAllocate));
|
||||
|
||||
m_list.set_focus_hadjustment(get_hadjustment());
|
||||
m_list.set_focus_vadjustment(get_vadjustment());
|
||||
@@ -30,56 +24,7 @@ ChatList::ChatList() {
|
||||
|
||||
m_list.show();
|
||||
|
||||
m_menu_copy_id = Gtk::manage(new Gtk::MenuItem("Copy ID"));
|
||||
m_menu_copy_id->signal_activate().connect([this] {
|
||||
Gtk::Clipboard::get()->set_text(std::to_string(m_menu_selected_message));
|
||||
});
|
||||
m_menu_copy_id->show();
|
||||
m_menu.append(*m_menu_copy_id);
|
||||
|
||||
m_menu_delete_message = Gtk::manage(new Gtk::MenuItem("Delete Message"));
|
||||
m_menu_delete_message->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().DeleteMessage(m_active_channel, m_menu_selected_message);
|
||||
});
|
||||
m_menu_delete_message->show();
|
||||
m_menu.append(*m_menu_delete_message);
|
||||
|
||||
m_menu_edit_message = Gtk::manage(new Gtk::MenuItem("Edit Message"));
|
||||
m_menu_edit_message->signal_activate().connect([this] {
|
||||
m_signal_action_message_edit.emit(m_active_channel, m_menu_selected_message);
|
||||
});
|
||||
m_menu_edit_message->show();
|
||||
m_menu.append(*m_menu_edit_message);
|
||||
|
||||
m_menu_copy_content = Gtk::manage(new Gtk::MenuItem("Copy Content"));
|
||||
m_menu_copy_content->signal_activate().connect([this] {
|
||||
const auto msg = Abaddon::Get().GetDiscordClient().GetMessage(m_menu_selected_message);
|
||||
if (msg.has_value())
|
||||
Gtk::Clipboard::get()->set_text(msg->Content);
|
||||
});
|
||||
m_menu_copy_content->show();
|
||||
m_menu.append(*m_menu_copy_content);
|
||||
|
||||
m_menu_reply_to = Gtk::manage(new Gtk::MenuItem("Reply To"));
|
||||
m_menu_reply_to->signal_activate().connect([this] {
|
||||
m_signal_action_reply_to.emit(m_menu_selected_message);
|
||||
});
|
||||
m_menu_reply_to->show();
|
||||
m_menu.append(*m_menu_reply_to);
|
||||
|
||||
m_menu_unpin = Gtk::manage(new Gtk::MenuItem("Unpin"));
|
||||
m_menu_unpin->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().Unpin(m_active_channel, m_menu_selected_message, [](...) {});
|
||||
});
|
||||
m_menu.append(*m_menu_unpin);
|
||||
|
||||
m_menu_pin = Gtk::manage(new Gtk::MenuItem("Pin"));
|
||||
m_menu_pin->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().Pin(m_active_channel, m_menu_selected_message, [](...) {});
|
||||
});
|
||||
m_menu.append(*m_menu_pin);
|
||||
|
||||
m_menu.show();
|
||||
SetupMenu();
|
||||
}
|
||||
|
||||
void ChatList::Clear() {
|
||||
@@ -89,6 +34,9 @@ void ChatList::Clear() {
|
||||
delete *it;
|
||||
it++;
|
||||
}
|
||||
m_id_to_widget.clear();
|
||||
m_num_messages = 0;
|
||||
m_num_rows = 0;
|
||||
}
|
||||
|
||||
void ChatList::SetActiveChannel(Snowflake id) {
|
||||
@@ -98,6 +46,7 @@ void ChatList::SetActiveChannel(Snowflake id) {
|
||||
void ChatList::ProcessNewMessage(const Message &data, bool prepend) {
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
if (!discord.IsStarted()) return;
|
||||
if (!prepend) m_ignore_next_upper = true;
|
||||
|
||||
// delete preview message when gateway sends it back
|
||||
if (!data.IsPending && data.Nonce.has_value() && data.Author.ID == discord.GetUserData().ID) {
|
||||
@@ -243,7 +192,7 @@ void ChatList::RefetchMessage(Snowflake id) {
|
||||
}
|
||||
|
||||
Snowflake ChatList::GetOldestListedMessage() {
|
||||
if (m_id_to_widget.size() > 0)
|
||||
if (!m_id_to_widget.empty())
|
||||
return m_id_to_widget.begin()->first;
|
||||
else
|
||||
return Snowflake::Invalid;
|
||||
@@ -306,14 +255,85 @@ void ChatList::ActuallyRemoveMessage(Snowflake id) {
|
||||
RemoveMessageAndHeader(it->second);
|
||||
}
|
||||
|
||||
void ChatList::OnScrollEdgeOvershot(Gtk::PositionType pos) {
|
||||
if (pos == Gtk::POS_TOP)
|
||||
m_signal_action_chat_load_history.emit(m_active_channel);
|
||||
void ChatList::SetupMenu() {
|
||||
m_menu_copy_id = Gtk::manage(new Gtk::MenuItem("Copy ID"));
|
||||
m_menu_copy_id->signal_activate().connect([this] {
|
||||
Gtk::Clipboard::get()->set_text(std::to_string(m_menu_selected_message));
|
||||
});
|
||||
m_menu_copy_id->show();
|
||||
m_menu.append(*m_menu_copy_id);
|
||||
|
||||
m_menu_delete_message = Gtk::manage(new Gtk::MenuItem("Delete Message"));
|
||||
m_menu_delete_message->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().DeleteMessage(m_active_channel, m_menu_selected_message);
|
||||
});
|
||||
m_menu_delete_message->show();
|
||||
m_menu.append(*m_menu_delete_message);
|
||||
|
||||
m_menu_edit_message = Gtk::manage(new Gtk::MenuItem("Edit Message"));
|
||||
m_menu_edit_message->signal_activate().connect([this] {
|
||||
m_signal_action_message_edit.emit(m_active_channel, m_menu_selected_message);
|
||||
});
|
||||
m_menu_edit_message->show();
|
||||
m_menu.append(*m_menu_edit_message);
|
||||
|
||||
m_menu_copy_content = Gtk::manage(new Gtk::MenuItem("Copy Content"));
|
||||
m_menu_copy_content->signal_activate().connect([this] {
|
||||
const auto msg = Abaddon::Get().GetDiscordClient().GetMessage(m_menu_selected_message);
|
||||
if (msg.has_value())
|
||||
Gtk::Clipboard::get()->set_text(msg->Content);
|
||||
});
|
||||
m_menu_copy_content->show();
|
||||
m_menu.append(*m_menu_copy_content);
|
||||
|
||||
m_menu_reply_to = Gtk::manage(new Gtk::MenuItem("Reply To"));
|
||||
m_menu_reply_to->signal_activate().connect([this] {
|
||||
m_signal_action_reply_to.emit(m_menu_selected_message);
|
||||
});
|
||||
m_menu_reply_to->show();
|
||||
m_menu.append(*m_menu_reply_to);
|
||||
|
||||
m_menu_unpin = Gtk::manage(new Gtk::MenuItem("Unpin"));
|
||||
m_menu_unpin->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().Unpin(m_active_channel, m_menu_selected_message, [](...) {});
|
||||
});
|
||||
m_menu.append(*m_menu_unpin);
|
||||
|
||||
m_menu_pin = Gtk::manage(new Gtk::MenuItem("Pin"));
|
||||
m_menu_pin->signal_activate().connect([this] {
|
||||
Abaddon::Get().GetDiscordClient().Pin(m_active_channel, m_menu_selected_message, [](...) {});
|
||||
});
|
||||
m_menu.append(*m_menu_pin);
|
||||
|
||||
m_menu.show();
|
||||
}
|
||||
|
||||
void ChatList::ScrollToBottom() {
|
||||
auto x = get_vadjustment();
|
||||
x->set_value(x->get_upper());
|
||||
auto v = get_vadjustment();
|
||||
v->set_value(v->get_upper());
|
||||
}
|
||||
|
||||
void ChatList::OnVAdjustmentValueChanged() {
|
||||
auto v = get_vadjustment();
|
||||
if (m_history_timer.elapsed() > 1 && v->get_value() < 500) {
|
||||
m_history_timer.start();
|
||||
m_signal_action_chat_load_history.emit(m_active_channel);
|
||||
}
|
||||
m_should_scroll_to_bottom = v->get_upper() - v->get_page_size() <= v->get_value();
|
||||
}
|
||||
|
||||
void ChatList::OnVAdjustmentUpperChanged() {
|
||||
auto v = get_vadjustment();
|
||||
if (!m_ignore_next_upper && !m_should_scroll_to_bottom && m_old_upper > -1.0) {
|
||||
const auto inc = v->get_upper() - m_old_upper;
|
||||
v->set_value(v->get_value() + inc);
|
||||
}
|
||||
m_ignore_next_upper = false;
|
||||
m_old_upper = v->get_upper();
|
||||
}
|
||||
|
||||
void ChatList::OnListSizeAllocate(Gtk::Allocation &allocation) {
|
||||
if (m_should_scroll_to_bottom) ScrollToBottom();
|
||||
}
|
||||
|
||||
void ChatList::RemoveMessageAndHeader(Gtk::Widget *widget) {
|
||||
@@ -335,10 +355,6 @@ ChatList::type_signal_action_message_edit ChatList::signal_action_message_edit()
|
||||
return m_signal_action_message_edit;
|
||||
}
|
||||
|
||||
ChatList::type_signal_action_chat_submit ChatList::signal_action_chat_submit() {
|
||||
return m_signal_action_chat_submit;
|
||||
}
|
||||
|
||||
ChatList::type_signal_action_chat_load_history ChatList::signal_action_chat_load_history() {
|
||||
return m_signal_action_chat_load_history;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <gtkmm.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "discord/message.hpp"
|
||||
#include "discord/snowflake.hpp"
|
||||
|
||||
class ChatList : public Gtk::ScrolledWindow {
|
||||
@@ -25,8 +26,11 @@ public:
|
||||
void ActuallyRemoveMessage(Snowflake id); // perhaps not the best method name
|
||||
|
||||
private:
|
||||
void OnScrollEdgeOvershot(Gtk::PositionType pos);
|
||||
void SetupMenu();
|
||||
void ScrollToBottom();
|
||||
void OnVAdjustmentValueChanged();
|
||||
void OnVAdjustmentUpperChanged();
|
||||
void OnListSizeAllocate(Gtk::Allocation &allocation);
|
||||
void RemoveMessageAndHeader(Gtk::Widget *widget);
|
||||
|
||||
bool m_use_pinned_menu = false;
|
||||
@@ -47,15 +51,18 @@ private:
|
||||
int m_num_rows = 0;
|
||||
std::map<Snowflake, Gtk::Widget *> m_id_to_widget;
|
||||
|
||||
bool m_ignore_next_upper = false;
|
||||
double m_old_upper = -1.0;
|
||||
bool m_should_scroll_to_bottom = true;
|
||||
Gtk::ListBox m_list;
|
||||
|
||||
bool m_separate_all = false;
|
||||
|
||||
Glib::Timer m_history_timer;
|
||||
|
||||
public:
|
||||
// these are all forwarded by the parent
|
||||
using type_signal_action_message_edit = sigc::signal<void, Snowflake, Snowflake>;
|
||||
using type_signal_action_chat_submit = sigc::signal<void, std::string, Snowflake, Snowflake>;
|
||||
using type_signal_action_chat_load_history = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_channel_click = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_insert_mention = sigc::signal<void, Snowflake>;
|
||||
@@ -65,7 +72,6 @@ public:
|
||||
using type_signal_action_reply_to = sigc::signal<void, Snowflake>;
|
||||
|
||||
type_signal_action_message_edit signal_action_message_edit();
|
||||
type_signal_action_chat_submit signal_action_chat_submit();
|
||||
type_signal_action_chat_load_history signal_action_chat_load_history();
|
||||
type_signal_action_channel_click signal_action_channel_click();
|
||||
type_signal_action_insert_mention signal_action_insert_mention();
|
||||
@@ -76,7 +82,6 @@ public:
|
||||
|
||||
private:
|
||||
type_signal_action_message_edit m_signal_action_message_edit;
|
||||
type_signal_action_chat_submit m_signal_action_chat_submit;
|
||||
type_signal_action_chat_load_history m_signal_action_chat_load_history;
|
||||
type_signal_action_channel_click m_signal_action_channel_click;
|
||||
type_signal_action_insert_mention m_signal_action_insert_mention;
|
||||
@@ -101,15 +106,6 @@ inline void ChatList::SetMessages(Iter begin, Iter end) {
|
||||
|
||||
template<typename Iter>
|
||||
inline void ChatList::PrependMessages(Iter begin, Iter end) {
|
||||
const auto old_upper = get_vadjustment()->get_upper();
|
||||
const auto old_value = get_vadjustment()->get_value();
|
||||
for (Iter it = begin; it != end; it++)
|
||||
ProcessNewMessage(*it, true);
|
||||
// force everything to process before getting new values
|
||||
while (Gtk::Main::events_pending())
|
||||
Gtk::Main::iteration();
|
||||
const auto new_upper = get_vadjustment()->get_upper();
|
||||
if (old_value == 0.0 && (new_upper - old_upper) > 0.0)
|
||||
get_vadjustment()->set_value(new_upper - old_upper);
|
||||
// this isn't ideal
|
||||
}
|
||||
|
||||
@@ -30,9 +30,8 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d
|
||||
if (data.Nonce.has_value())
|
||||
container->Nonce = *data.Nonce;
|
||||
|
||||
if (data.Content.size() > 0 || data.Type != MessageType::DEFAULT) {
|
||||
if (!data.Content.empty() || data.Type != MessageType::DEFAULT) {
|
||||
container->m_text_component = container->CreateTextComponent(data);
|
||||
container->AttachEventHandlers(*container->m_text_component);
|
||||
container->m_main.add(*container->m_text_component);
|
||||
}
|
||||
|
||||
@@ -78,7 +77,7 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d
|
||||
container->m_main.add(*widget);
|
||||
}
|
||||
|
||||
if (data.Reactions.has_value() && data.Reactions->size() > 0) {
|
||||
if (data.Reactions.has_value() && !data.Reactions->empty()) {
|
||||
container->m_reactions_component = container->CreateReactionsComponent(data);
|
||||
container->m_main.add(*container->m_reactions_component);
|
||||
}
|
||||
@@ -101,7 +100,6 @@ void ChatMessageItemContainer::UpdateContent() {
|
||||
|
||||
if (!data->Embeds.empty()) {
|
||||
m_embed_component = CreateEmbedsComponent(data->Embeds);
|
||||
AttachEventHandlers(*m_embed_component);
|
||||
m_main.add(*m_embed_component);
|
||||
m_embed_component->show_all();
|
||||
}
|
||||
@@ -114,7 +112,7 @@ void ChatMessageItemContainer::UpdateReactions() {
|
||||
}
|
||||
|
||||
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
||||
if (data->Reactions.has_value() && data->Reactions->size() > 0) {
|
||||
if (data->Reactions.has_value() && !data->Reactions->empty()) {
|
||||
m_reactions_component = CreateReactionsComponent(*data);
|
||||
m_reactions_component->show_all();
|
||||
m_main.add(*m_reactions_component);
|
||||
@@ -150,14 +148,14 @@ void ChatMessageItemContainer::UpdateAttributes() {
|
||||
m_attrib_label->set_markup("<span color='#999999'>[edited]</span>");
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, std::string url) {
|
||||
void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, const std::string &url) {
|
||||
// clang-format off
|
||||
widget->signal_button_press_event().connect([url](GdkEventButton *event) -> bool {
|
||||
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
|
||||
widget->signal_button_release_event().connect([url](GdkEventButton *event) -> bool {
|
||||
if (event->type == GDK_BUTTON_RELEASE && event->button == GDK_BUTTON_PRIMARY) {
|
||||
LaunchBrowser(url);
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}, false);
|
||||
// clang-format on
|
||||
}
|
||||
@@ -174,6 +172,8 @@ Gtk::TextView *ChatMessageItemContainer::CreateTextComponent(const Message &data
|
||||
tv->set_halign(Gtk::ALIGN_FILL);
|
||||
tv->set_hexpand(true);
|
||||
|
||||
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnTextViewButtonPress), false);
|
||||
|
||||
UpdateTextComponent(tv);
|
||||
|
||||
return tv;
|
||||
@@ -224,13 +224,13 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
|
||||
}
|
||||
} break;
|
||||
case MessageType::RECIPIENT_ADD: {
|
||||
if (data->Mentions.size() == 0) break;
|
||||
if (data->Mentions.empty()) break;
|
||||
const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID);
|
||||
const auto &added = data->Mentions[0];
|
||||
b->insert_markup(s, "<i><span color='#999999'><span color='#eeeeee'>" + adder->Username + "</span> added <span color='#eeeeee'>" + added.Username + "</span></span></i>");
|
||||
} break;
|
||||
case MessageType::RECIPIENT_REMOVE: {
|
||||
if (data->Mentions.size() == 0) break;
|
||||
if (data->Mentions.empty()) break;
|
||||
const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID);
|
||||
const auto &added = data->Mentions[0];
|
||||
if (adder->ID == added.ID)
|
||||
@@ -281,8 +281,6 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
|
||||
tag->property_weight() = Pango::WEIGHT_BOLD;
|
||||
m_channel_tagmap[tag] = *data->MessageReference->ChannelID;
|
||||
b->insert_with_tag(iter, data->Content, tag);
|
||||
|
||||
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnClickChannel), false);
|
||||
} else {
|
||||
b->insert_markup(s, "<i><span color='#999999'>" + author->GetEscapedBoldName() + " started a thread: </span><b>" + Glib::Markup::escape_text(data->Content) + "</b></i>");
|
||||
}
|
||||
@@ -297,12 +295,10 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedsComponent(const std::vector<E
|
||||
if (IsEmbedImageOnly(embed)) {
|
||||
auto *widget = CreateImageComponent(*embed.Thumbnail->ProxyURL, *embed.Thumbnail->URL, *embed.Thumbnail->Width, *embed.Thumbnail->Height);
|
||||
widget->show();
|
||||
AttachEventHandlers(*widget);
|
||||
box->add(*widget);
|
||||
} else {
|
||||
auto *widget = CreateEmbedComponent(embed);
|
||||
widget->show();
|
||||
AttachEventHandlers(*widget);
|
||||
box->add(*widget);
|
||||
}
|
||||
}
|
||||
@@ -361,8 +357,8 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &emb
|
||||
if (embed.URL.has_value()) {
|
||||
AddPointerCursor(*title_ev);
|
||||
auto url = *embed.URL;
|
||||
title_ev->signal_button_press_event().connect([this, url = std::move(url)](GdkEventButton *event) -> bool {
|
||||
if (event->button == GDK_BUTTON_PRIMARY) {
|
||||
title_ev->signal_button_release_event().connect([url = std::move(url)](GdkEventButton *event) -> bool {
|
||||
if (event->type == GDK_BUTTON_RELEASE && event->button == GDK_BUTTON_PRIMARY) {
|
||||
LaunchBrowser(url);
|
||||
return true;
|
||||
}
|
||||
@@ -389,7 +385,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &emb
|
||||
}
|
||||
|
||||
// todo: handle inline fields
|
||||
if (embed.Fields.has_value() && embed.Fields->size() > 0) {
|
||||
if (embed.Fields.has_value() && !embed.Fields->empty()) {
|
||||
auto *flow = Gtk::manage(new Gtk::FlowBox);
|
||||
flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
|
||||
flow->set_min_children_per_line(3);
|
||||
@@ -493,12 +489,22 @@ Gtk::Widget *ChatMessageItemContainer::CreateImageComponent(const std::string &p
|
||||
Gtk::EventBox *ev = Gtk::manage(new Gtk::EventBox);
|
||||
Gtk::Image *widget = Gtk::manage(new LazyImage(proxy_url, w, h, false));
|
||||
ev->add(*widget);
|
||||
ev->set_halign(Gtk::ALIGN_START);
|
||||
widget->set_halign(Gtk::ALIGN_START);
|
||||
widget->set_size_request(w, h);
|
||||
|
||||
AttachEventHandlers(*ev);
|
||||
AddClickHandler(ev, url);
|
||||
|
||||
const auto on_button_press_event = [this, url](GdkEventButton *e) -> bool {
|
||||
if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) {
|
||||
m_selected_link = url;
|
||||
m_link_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(e));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
ev->signal_button_press_event().connect(on_button_press_event, false);
|
||||
|
||||
return ev;
|
||||
}
|
||||
|
||||
@@ -510,29 +516,21 @@ Gtk::Widget *ChatMessageItemContainer::CreateAttachmentComponent(const Attachmen
|
||||
ev->get_style_context()->add_class("message-attachment-box");
|
||||
ev->add(*btn);
|
||||
|
||||
AttachEventHandlers(*ev);
|
||||
AddClickHandler(ev, data.URL);
|
||||
|
||||
const auto on_button_press_event = [this, url = data.URL](GdkEventButton *e) -> bool {
|
||||
if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) {
|
||||
m_selected_link = url;
|
||||
m_link_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(e));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
ev->signal_button_press_event().connect(on_button_press_event, false);
|
||||
|
||||
return ev;
|
||||
}
|
||||
|
||||
Gtk::Widget *ChatMessageItemContainer::CreateStickerComponentDeprecated(const StickerData &data) {
|
||||
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||
auto *imgw = Gtk::manage(new Gtk::Image);
|
||||
box->add(*imgw);
|
||||
auto &img = Abaddon::Get().GetImageManager();
|
||||
|
||||
if (data.FormatType == StickerFormatType::PNG || data.FormatType == StickerFormatType::APNG) {
|
||||
auto cb = [this, imgw](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
||||
imgw->property_pixbuf() = pixbuf;
|
||||
};
|
||||
img.LoadFromURL(data.GetURL(), sigc::track_obj(cb, *imgw));
|
||||
}
|
||||
|
||||
AttachEventHandlers(*box);
|
||||
return box;
|
||||
}
|
||||
|
||||
Gtk::Widget *ChatMessageItemContainer::CreateStickersComponent(const std::vector<StickerItem> &data) {
|
||||
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||
|
||||
@@ -541,6 +539,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateStickersComponent(const std::vector
|
||||
if (sticker.FormatType != StickerFormatType::PNG && sticker.FormatType != StickerFormatType::APNG) continue;
|
||||
auto *ev = Gtk::manage(new Gtk::EventBox);
|
||||
auto *img = Gtk::manage(new LazyImage(sticker.GetURL(), StickerComponentSize, StickerComponentSize, false));
|
||||
img->set_halign(Gtk::ALIGN_START);
|
||||
img->set_size_request(StickerComponentSize, StickerComponentSize); // should this go in LazyImage ?
|
||||
img->show();
|
||||
ev->show();
|
||||
@@ -550,7 +549,6 @@ Gtk::Widget *ChatMessageItemContainer::CreateStickersComponent(const std::vector
|
||||
|
||||
box->show();
|
||||
|
||||
AttachEventHandlers(*box);
|
||||
return box;
|
||||
}
|
||||
|
||||
@@ -607,7 +605,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateReactionsComponent(const Message &d
|
||||
// image
|
||||
if (is_stock) { // unicode/stock
|
||||
const auto shortcode = emojis.GetShortCodeForPattern(reaction.Emoji.Name);
|
||||
if (shortcode != "")
|
||||
if (!shortcode.empty())
|
||||
ev->set_tooltip_text(shortcode);
|
||||
|
||||
const auto &pb = emojis.GetPixBuf(reaction.Emoji.Name);
|
||||
@@ -657,13 +655,19 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data)
|
||||
const auto role = discord.GetRole(role_id);
|
||||
if (role.has_value()) {
|
||||
const auto author = discord.GetUser(author_id);
|
||||
return "<b><span color=\"#" + IntToCSSColor(role->Color) + "\">" + author->GetEscapedString() + "</span></b>";
|
||||
if (author.has_value()) {
|
||||
return "<b><span color=\"#" + IntToCSSColor(role->Color) + "\">" + author->GetEscapedString() + "</span></b>";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto author = discord.GetUser(author_id);
|
||||
return author->GetEscapedBoldString<false>();
|
||||
if (author.has_value()) {
|
||||
return author->GetEscapedBoldString<false>();
|
||||
}
|
||||
|
||||
return "<b>Unknown User</b>";
|
||||
};
|
||||
|
||||
// if the message wasnt fetched from store it might have an un-fetched reference
|
||||
@@ -675,15 +679,15 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data)
|
||||
}
|
||||
|
||||
if (data.Interaction.has_value()) {
|
||||
const auto user = *discord.GetUser(data.Interaction->User.ID);
|
||||
|
||||
if (data.GuildID.has_value()) {
|
||||
lbl->set_markup(get_author_markup(user.ID, *data.GuildID) +
|
||||
lbl->set_markup(get_author_markup(data.Interaction->User.ID, *data.GuildID) +
|
||||
" used <span color='#697ec4'>/" +
|
||||
Glib::Markup::escape_text(data.Interaction->Name) +
|
||||
"</span>");
|
||||
} else if (const auto user = discord.GetUser(data.Interaction->User.ID); user.has_value()) {
|
||||
lbl->set_markup(user->GetEscapedBoldString<false>());
|
||||
} else {
|
||||
lbl->set_markup(user.GetEscapedBoldString<false>());
|
||||
lbl->set_markup("<b>Unknown User</b>");
|
||||
}
|
||||
} else if (referenced_message.has_value()) {
|
||||
if (referenced_message.value() == nullptr) {
|
||||
@@ -783,7 +787,7 @@ void ChatMessageItemContainer::HandleRoleMentions(const Glib::RefPtr<Gtk::TextBu
|
||||
}
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::HandleUserMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
|
||||
void ChatMessageItemContainer::HandleUserMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) const {
|
||||
constexpr static const auto mentions_regex = R"(<@!?(\d+)>)";
|
||||
|
||||
static auto rgx = Glib::Regex::create(mentions_regex);
|
||||
@@ -860,7 +864,7 @@ void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) {
|
||||
const auto mark_start = buf->create_mark(start_it, false);
|
||||
end_it.backward_char();
|
||||
const auto mark_end = buf->create_mark(end_it, false);
|
||||
const auto cb = [this, &tv, buf, mark_start, mark_end](const Glib::RefPtr<Gdk::PixbufAnimation> &pixbuf) {
|
||||
const auto cb = [&tv, buf, mark_start, mark_end](const Glib::RefPtr<Gdk::PixbufAnimation> &pixbuf) {
|
||||
auto start_it = mark_start->get_iter();
|
||||
auto end_it = mark_end->get_iter();
|
||||
end_it.forward_char();
|
||||
@@ -878,7 +882,7 @@ void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) {
|
||||
const auto mark_start = buf->create_mark(start_it, false);
|
||||
end_it.backward_char();
|
||||
const auto mark_end = buf->create_mark(end_it, false);
|
||||
const auto cb = [this, buf, mark_start, mark_end](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
||||
const auto cb = [buf, mark_start, mark_end](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
||||
auto start_it = mark_start->get_iter();
|
||||
auto end_it = mark_end->get_iter();
|
||||
end_it.forward_char();
|
||||
@@ -901,7 +905,7 @@ void ChatMessageItemContainer::HandleEmojis(Gtk::TextView &tv) {
|
||||
if (Abaddon::Get().GetSettings().ShowCustomEmojis) HandleCustomEmojis(tv);
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr<Gtk::TextBuffer> buf) {
|
||||
void ChatMessageItemContainer::CleanupEmojis(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
|
||||
static auto rgx = Glib::Regex::create(R"(<a?:([\w\d_]+):(\d+)>)");
|
||||
|
||||
auto text = GetText(buf);
|
||||
@@ -921,9 +925,9 @@ void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr<Gtk::TextBuffer> buf)
|
||||
|
||||
startpos = mend;
|
||||
const auto it = buf->erase(start_it, end_it);
|
||||
const int alen = text.size();
|
||||
const int alen = static_cast<int>(text.size());
|
||||
text = GetText(buf);
|
||||
const int blen = text.size();
|
||||
const int blen = static_cast<int>(text.size());
|
||||
startpos -= (alen - blen);
|
||||
|
||||
buf->insert(it, new_term);
|
||||
@@ -932,7 +936,7 @@ void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr<Gtk::TextBuffer> buf)
|
||||
}
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::HandleChannelMentions(Glib::RefPtr<Gtk::TextBuffer> buf) {
|
||||
void ChatMessageItemContainer::HandleChannelMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
|
||||
static auto rgx = Glib::Regex::create(R"(<#(\d+)>)");
|
||||
|
||||
Glib::ustring text = GetText(buf);
|
||||
@@ -972,7 +976,6 @@ void ChatMessageItemContainer::HandleChannelMentions(Glib::RefPtr<Gtk::TextBuffe
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::HandleChannelMentions(Gtk::TextView *tv) {
|
||||
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnClickChannel), false);
|
||||
HandleChannelMentions(tv->get_buffer());
|
||||
}
|
||||
|
||||
@@ -989,12 +992,12 @@ bool ChatMessageItemContainer::OnClickChannel(GdkEventButton *ev) {
|
||||
return false;
|
||||
|
||||
int x, y;
|
||||
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, ev->y, x, y);
|
||||
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, static_cast<int>(ev->x), static_cast<int>(ev->y), x, y);
|
||||
Gtk::TextBuffer::iterator iter;
|
||||
m_text_component->get_iter_at_location(iter, x, y);
|
||||
|
||||
const auto tags = iter.get_tags();
|
||||
for (auto tag : tags) {
|
||||
for (const auto &tag : tags) {
|
||||
const auto it = m_channel_tagmap.find(tag);
|
||||
if (it != m_channel_tagmap.end()) {
|
||||
m_signal_action_channel_click.emit(it->second);
|
||||
@@ -1006,6 +1009,20 @@ bool ChatMessageItemContainer::OnClickChannel(GdkEventButton *ev) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ChatMessageItemContainer::OnTextViewButtonPress(GdkEventButton *ev) {
|
||||
// run all button press handlers and propagate if none return true
|
||||
if (OnLinkClick(ev)) return true;
|
||||
if (OnClickChannel(ev)) return true;
|
||||
|
||||
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
|
||||
// send the event upward skipping TextView's handler because we dont want it
|
||||
gtk_propagate_event(GTK_WIDGET(m_main.gobj()), reinterpret_cast<GdkEvent *>(ev));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::on_link_menu_copy() {
|
||||
Gtk::Clipboard::get()->set_text(m_selected_link);
|
||||
}
|
||||
@@ -1013,8 +1030,6 @@ void ChatMessageItemContainer::on_link_menu_copy() {
|
||||
void ChatMessageItemContainer::HandleLinks(Gtk::TextView &tv) {
|
||||
const auto rgx = Glib::Regex::create(R"(\bhttps?:\/\/[^\s]+\.[^\s]+\b)");
|
||||
|
||||
tv.signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnLinkClick), false);
|
||||
|
||||
auto buf = tv.get_buffer();
|
||||
Glib::ustring text = GetText(buf);
|
||||
|
||||
@@ -1052,12 +1067,12 @@ bool ChatMessageItemContainer::OnLinkClick(GdkEventButton *ev) {
|
||||
return false;
|
||||
|
||||
int x, y;
|
||||
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, ev->y, x, y);
|
||||
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, static_cast<int>(ev->x), static_cast<int>(ev->y), x, y);
|
||||
Gtk::TextBuffer::iterator iter;
|
||||
m_text_component->get_iter_at_location(iter, x, y);
|
||||
|
||||
const auto tags = iter.get_tags();
|
||||
for (auto tag : tags) {
|
||||
for (const auto &tag : tags) {
|
||||
const auto it = m_link_tagmap.find(tag);
|
||||
if (it != m_link_tagmap.end()) {
|
||||
if (ev->button == GDK_BUTTON_PRIMARY) {
|
||||
@@ -1086,18 +1101,6 @@ ChatMessageItemContainer::type_signal_action_reaction_remove ChatMessageItemCont
|
||||
return m_signal_action_reaction_remove;
|
||||
}
|
||||
|
||||
void ChatMessageItemContainer::AttachEventHandlers(Gtk::Widget &widget) {
|
||||
const auto on_button_press_event = [this](GdkEventButton *e) -> bool {
|
||||
if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) {
|
||||
event(reinterpret_cast<GdkEvent *>(e)); // illegal ooooooh
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
widget.signal_button_press_event().connect(on_button_press_event, false);
|
||||
}
|
||||
|
||||
ChatMessageHeader::ChatMessageHeader(const Message &data)
|
||||
: m_main_box(Gtk::ORIENTATION_HORIZONTAL)
|
||||
, m_content_box(Gtk::ORIENTATION_VERTICAL)
|
||||
@@ -1130,16 +1133,15 @@ ChatMessageHeader::ChatMessageHeader(const Message &data)
|
||||
m_avatar.set_valign(Gtk::ALIGN_START);
|
||||
m_avatar.set_margin_right(10);
|
||||
|
||||
m_author.set_markup(data.Author.GetEscapedBoldName());
|
||||
m_author.set_single_line_mode(true);
|
||||
m_author.set_line_wrap(false);
|
||||
m_author.set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
m_author.set_xalign(0.f);
|
||||
m_author.set_xalign(0.0F);
|
||||
m_author.set_can_focus(false);
|
||||
|
||||
m_meta_ev.signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageHeader::on_author_button_press));
|
||||
|
||||
if (author->IsBot || data.WebhookID.has_value()) {
|
||||
if (author->IsABot() || data.WebhookID.has_value()) {
|
||||
m_extra = Gtk::manage(new Gtk::Label);
|
||||
m_extra->get_style_context()->add_class("message-container-extra");
|
||||
m_extra->set_single_line_mode(true);
|
||||
@@ -1147,7 +1149,7 @@ ChatMessageHeader::ChatMessageHeader(const Message &data)
|
||||
m_extra->set_can_focus(false);
|
||||
m_extra->set_use_markup(true);
|
||||
}
|
||||
if (author->IsBot)
|
||||
if (author->IsABot())
|
||||
m_extra->set_markup("<b>BOT</b>");
|
||||
else if (data.WebhookID.has_value())
|
||||
m_extra->set_markup("<b>Webhook</b>");
|
||||
@@ -1211,30 +1213,32 @@ ChatMessageHeader::ChatMessageHeader(const Message &data)
|
||||
show_all();
|
||||
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
auto role_update_cb = [this](...) { UpdateNameColor(); };
|
||||
auto role_update_cb = [this](...) { UpdateName(); };
|
||||
discord.signal_role_update().connect(sigc::track_obj(role_update_cb, *this));
|
||||
auto guild_member_update_cb = [this](const auto &, const auto &) { UpdateNameColor(); };
|
||||
auto guild_member_update_cb = [this](const auto &, const auto &) { UpdateName(); };
|
||||
discord.signal_guild_member_update().connect(sigc::track_obj(guild_member_update_cb, *this));
|
||||
UpdateNameColor();
|
||||
UpdateName();
|
||||
AttachUserMenuHandler(m_meta_ev);
|
||||
AttachUserMenuHandler(m_avatar_ev);
|
||||
}
|
||||
|
||||
void ChatMessageHeader::UpdateNameColor() {
|
||||
void ChatMessageHeader::UpdateName() {
|
||||
const auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
const auto user = discord.GetUser(UserID);
|
||||
if (!user.has_value()) return;
|
||||
const auto chan = discord.GetChannel(ChannelID);
|
||||
bool is_guild = chan.has_value() && chan->GuildID.has_value();
|
||||
if (is_guild) {
|
||||
const auto member = discord.GetMember(UserID, *chan->GuildID);
|
||||
const auto role_id = discord.GetMemberHoistedRole(*chan->GuildID, UserID, true);
|
||||
const auto role = discord.GetRole(role_id);
|
||||
const auto name = GetEscapedDisplayName(*user, member);
|
||||
|
||||
std::string md;
|
||||
if (role.has_value())
|
||||
m_author.set_markup("<span weight='bold' color='#" + IntToCSSColor(role->Color) + "'>" + user->GetEscapedName() + "</span>");
|
||||
m_author.set_markup("<span weight='bold' color='#" + IntToCSSColor(role->Color) + "'>" + name + "</span>");
|
||||
else
|
||||
m_author.set_markup("<span weight='bold'>" + user->GetEscapedName() + "</span>");
|
||||
m_author.set_markup("<span weight='bold'>" + name + "</span>");
|
||||
} else
|
||||
m_author.set_markup("<span weight='bold'>" + user->GetEscapedName() + "</span>");
|
||||
}
|
||||
@@ -1258,6 +1262,13 @@ void ChatMessageHeader::AttachUserMenuHandler(Gtk::Widget &widget) {
|
||||
});
|
||||
}
|
||||
|
||||
Glib::ustring ChatMessageHeader::GetEscapedDisplayName(const UserData &user, const std::optional<GuildMember> &member) {
|
||||
if (member.has_value() && !member->Nickname.empty())
|
||||
return Glib::Markup::escape_text(member->Nickname);
|
||||
else
|
||||
return Glib::Markup::escape_text(user.GetEscapedName());
|
||||
}
|
||||
|
||||
bool ChatMessageHeader::on_author_button_press(GdkEventButton *ev) {
|
||||
if (ev->button == GDK_BUTTON_PRIMARY && (ev->state & GDK_SHIFT_MASK)) {
|
||||
m_signal_action_insert_mention.emit();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <gtkmm.h>
|
||||
#include "discord/discord.hpp"
|
||||
|
||||
class ChatMessageItemContainer : public Gtk::Box {
|
||||
class ChatMessageItemContainer : public Gtk::EventBox {
|
||||
public:
|
||||
Snowflake ID;
|
||||
Snowflake ChannelID;
|
||||
@@ -19,14 +19,13 @@ public:
|
||||
void SetFailed();
|
||||
|
||||
protected:
|
||||
void AddClickHandler(Gtk::Widget *widget, std::string);
|
||||
static void AddClickHandler(Gtk::Widget *widget, const std::string &);
|
||||
Gtk::TextView *CreateTextComponent(const Message &data); // Message.Content
|
||||
void UpdateTextComponent(Gtk::TextView *tv);
|
||||
Gtk::Widget *CreateEmbedsComponent(const std::vector<EmbedData> &embeds);
|
||||
Gtk::Widget *CreateEmbedComponent(const EmbedData &data); // Message.Embeds[0]
|
||||
static Gtk::Widget *CreateEmbedComponent(const EmbedData &data); // Message.Embeds[0]
|
||||
Gtk::Widget *CreateImageComponent(const std::string &proxy_url, const std::string &url, int inw, int inh);
|
||||
Gtk::Widget *CreateAttachmentComponent(const AttachmentData &data); // non-image attachments
|
||||
Gtk::Widget *CreateStickerComponentDeprecated(const StickerData &data);
|
||||
Gtk::Widget *CreateStickersComponent(const std::vector<StickerItem> &data);
|
||||
Gtk::Widget *CreateReactionsComponent(const Message &data);
|
||||
Gtk::Widget *CreateReplyComponent(const Message &data);
|
||||
@@ -35,16 +34,17 @@ protected:
|
||||
|
||||
static bool IsEmbedImageOnly(const EmbedData &data);
|
||||
|
||||
void HandleRoleMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf);
|
||||
void HandleUserMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf);
|
||||
void HandleStockEmojis(Gtk::TextView &tv);
|
||||
void HandleCustomEmojis(Gtk::TextView &tv);
|
||||
void HandleEmojis(Gtk::TextView &tv);
|
||||
void CleanupEmojis(Glib::RefPtr<Gtk::TextBuffer> buf);
|
||||
static void HandleRoleMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf);
|
||||
void HandleUserMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) const;
|
||||
static void HandleStockEmojis(Gtk::TextView &tv);
|
||||
static void HandleCustomEmojis(Gtk::TextView &tv);
|
||||
static void HandleEmojis(Gtk::TextView &tv);
|
||||
static void CleanupEmojis(const Glib::RefPtr<Gtk::TextBuffer> &buf);
|
||||
|
||||
void HandleChannelMentions(Glib::RefPtr<Gtk::TextBuffer> buf);
|
||||
void HandleChannelMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf);
|
||||
void HandleChannelMentions(Gtk::TextView *tv);
|
||||
bool OnClickChannel(GdkEventButton *ev);
|
||||
bool OnTextViewButtonPress(GdkEventButton *ev);
|
||||
|
||||
// reused for images and links
|
||||
Gtk::Menu m_link_menu;
|
||||
@@ -58,8 +58,6 @@ protected:
|
||||
std::map<Glib::RefPtr<Gtk::TextTag>, std::string> m_link_tagmap;
|
||||
std::map<Glib::RefPtr<Gtk::TextTag>, Snowflake> m_channel_tagmap;
|
||||
|
||||
void AttachEventHandlers(Gtk::Widget &widget);
|
||||
|
||||
Gtk::EventBox *_ev;
|
||||
Gtk::Box m_main;
|
||||
Gtk::Label *m_attrib_label = nullptr;
|
||||
@@ -91,11 +89,12 @@ public:
|
||||
|
||||
ChatMessageHeader(const Message &data);
|
||||
void AddContent(Gtk::Widget *widget, bool prepend);
|
||||
void UpdateNameColor();
|
||||
void UpdateName();
|
||||
std::vector<Gtk::Widget *> GetChildContent();
|
||||
|
||||
protected:
|
||||
void AttachUserMenuHandler(Gtk::Widget &widget);
|
||||
static Glib::ustring GetEscapedDisplayName(const UserData &user, const std::optional<GuildMember> &member);
|
||||
|
||||
bool on_author_button_press(GdkEventButton *ev);
|
||||
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#include "chatwindow.hpp"
|
||||
#include "chatmessage.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "chatinputindicator.hpp"
|
||||
#include "ratelimitindicator.hpp"
|
||||
#include "chatinput.hpp"
|
||||
#include "chatlist.hpp"
|
||||
#include "constants.hpp"
|
||||
#ifdef WITH_LIBHANDY
|
||||
#include "channeltabswitcherhandy.hpp"
|
||||
#endif
|
||||
|
||||
ChatWindow::ChatWindow() {
|
||||
Abaddon::Get().GetDiscordClient().signal_message_send_fail().connect(sigc::mem_fun(*this, &ChatWindow::OnMessageSendFail));
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
discord.signal_message_send_fail().connect(sigc::mem_fun(*this, &ChatWindow::OnMessageSendFail));
|
||||
|
||||
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||
m_chat = Gtk::manage(new ChatList);
|
||||
@@ -16,6 +20,13 @@ ChatWindow::ChatWindow() {
|
||||
m_rate_limit_indicator = Gtk::manage(new RateLimitIndicator);
|
||||
m_meta = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_tab_switcher = Gtk::make_managed<ChannelTabSwitcherHandy>();
|
||||
m_tab_switcher->signal_channel_switched_to().connect([this](Snowflake id) {
|
||||
m_signal_action_channel_click.emit(id, false);
|
||||
});
|
||||
#endif
|
||||
|
||||
m_rate_limit_indicator->set_margin_end(5);
|
||||
m_rate_limit_indicator->set_hexpand(true);
|
||||
m_rate_limit_indicator->set_halign(Gtk::ALIGN_END);
|
||||
@@ -36,6 +47,8 @@ ChatWindow::ChatWindow() {
|
||||
m_topic_text.set_halign(Gtk::ALIGN_START);
|
||||
m_topic_text.show();
|
||||
|
||||
m_input->set_valign(Gtk::ALIGN_END);
|
||||
|
||||
m_input->signal_submit().connect(sigc::mem_fun(*this, &ChatWindow::OnInputSubmit));
|
||||
m_input->signal_escape().connect([this]() {
|
||||
if (m_is_replying)
|
||||
@@ -45,25 +58,22 @@ ChatWindow::ChatWindow() {
|
||||
m_input->show();
|
||||
|
||||
m_completer.SetBuffer(m_input->GetBuffer());
|
||||
m_completer.SetGetChannelID([this]() -> auto {
|
||||
m_completer.SetGetChannelID([this]() {
|
||||
return m_active_channel;
|
||||
});
|
||||
|
||||
m_completer.SetGetRecentAuthors([this]() -> auto {
|
||||
m_completer.SetGetRecentAuthors([this]() {
|
||||
return m_chat->GetRecentAuthors();
|
||||
});
|
||||
|
||||
m_completer.show();
|
||||
|
||||
m_chat->signal_action_channel_click().connect([this](Snowflake id) {
|
||||
m_signal_action_channel_click.emit(id);
|
||||
m_signal_action_channel_click.emit(id, true);
|
||||
});
|
||||
m_chat->signal_action_chat_load_history().connect([this](Snowflake id) {
|
||||
m_signal_action_chat_load_history.emit(id);
|
||||
});
|
||||
m_chat->signal_action_chat_submit().connect([this](const std::string &str, Snowflake channel_id, Snowflake referenced_id) {
|
||||
m_signal_action_chat_submit.emit(str, channel_id, referenced_id);
|
||||
});
|
||||
m_chat->signal_action_insert_mention().connect([this](Snowflake id) {
|
||||
// lowkey gross
|
||||
m_signal_action_insert_mention.emit(id);
|
||||
@@ -88,12 +98,20 @@ ChatWindow::ChatWindow() {
|
||||
|
||||
m_meta->add(*m_input_indicator);
|
||||
m_meta->add(*m_rate_limit_indicator);
|
||||
//m_scroll->add(*m_list);
|
||||
// m_scroll->add(*m_list);
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_main->add(*m_tab_switcher);
|
||||
m_tab_switcher->show();
|
||||
#endif
|
||||
m_main->add(m_topic);
|
||||
m_main->add(*m_chat);
|
||||
m_main->add(m_completer);
|
||||
m_main->add(*m_input);
|
||||
m_main->add(*m_meta);
|
||||
m_main->add(m_progress);
|
||||
|
||||
m_progress.show();
|
||||
|
||||
m_main->show();
|
||||
}
|
||||
|
||||
@@ -112,10 +130,15 @@ void ChatWindow::SetMessages(const std::vector<Message> &msgs) {
|
||||
void ChatWindow::SetActiveChannel(Snowflake id) {
|
||||
m_active_channel = id;
|
||||
m_chat->SetActiveChannel(id);
|
||||
m_input->SetActiveChannel(id);
|
||||
m_input_indicator->SetActiveChannel(id);
|
||||
m_rate_limit_indicator->SetActiveChannel(id);
|
||||
if (m_is_replying)
|
||||
StopReplying();
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
m_tab_switcher->ReplaceActiveTab(id);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ChatWindow::AddNewMessage(const Message &data) {
|
||||
@@ -134,7 +157,7 @@ void ChatWindow::AddNewHistory(const std::vector<Message> &msgs) {
|
||||
m_chat->PrependMessages(msgs.crbegin(), msgs.crend());
|
||||
}
|
||||
|
||||
void ChatWindow::InsertChatInput(std::string text) {
|
||||
void ChatWindow::InsertChatInput(const std::string &text) {
|
||||
m_input->InsertText(text);
|
||||
}
|
||||
|
||||
@@ -151,19 +174,110 @@ void ChatWindow::SetTopic(const std::string &text) {
|
||||
m_topic.set_visible(text.length() > 0);
|
||||
}
|
||||
|
||||
void ChatWindow::AddAttachment(const Glib::RefPtr<Gio::File> &file) {
|
||||
m_input->AddAttachment(file);
|
||||
}
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
void ChatWindow::OpenNewTab(Snowflake id) {
|
||||
// open if its the first tab (in which case it really isnt a tab but whatever)
|
||||
if (m_tab_switcher->GetNumberOfTabs() == 0) {
|
||||
m_signal_action_channel_click.emit(id, false);
|
||||
}
|
||||
m_tab_switcher->AddChannelTab(id);
|
||||
}
|
||||
|
||||
TabsState ChatWindow::GetTabsState() {
|
||||
return m_tab_switcher->GetTabsState();
|
||||
}
|
||||
|
||||
void ChatWindow::UseTabsState(const TabsState &state) {
|
||||
m_tab_switcher->UseTabsState(state);
|
||||
}
|
||||
|
||||
void ChatWindow::GoBack() {
|
||||
m_tab_switcher->GoBackOnCurrent();
|
||||
}
|
||||
|
||||
void ChatWindow::GoForward() {
|
||||
m_tab_switcher->GoForwardOnCurrent();
|
||||
}
|
||||
|
||||
void ChatWindow::GoToPreviousTab() {
|
||||
m_tab_switcher->GoToPreviousTab();
|
||||
}
|
||||
|
||||
void ChatWindow::GoToNextTab() {
|
||||
m_tab_switcher->GoToNextTab();
|
||||
}
|
||||
|
||||
void ChatWindow::GoToTab(int idx) {
|
||||
m_tab_switcher->GoToTab(idx);
|
||||
}
|
||||
#endif
|
||||
|
||||
Snowflake ChatWindow::GetActiveChannel() const {
|
||||
return m_active_channel;
|
||||
}
|
||||
|
||||
bool ChatWindow::OnInputSubmit(const Glib::ustring &text) {
|
||||
bool ChatWindow::OnInputSubmit(ChatSubmitParams data) {
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
if (!discord.HasSelfChannelPermission(m_active_channel, Permission::SEND_MESSAGES)) return false;
|
||||
if (!data.Attachments.empty() && !discord.HasSelfChannelPermission(m_active_channel, Permission::ATTACH_FILES)) return false;
|
||||
|
||||
int nitro_restriction = BaseAttachmentSizeLimit;
|
||||
const auto nitro = discord.GetUserData().PremiumType;
|
||||
if (!nitro.has_value() || nitro == EPremiumType::None) {
|
||||
nitro_restriction = BaseAttachmentSizeLimit;
|
||||
} else if (nitro == EPremiumType::NitroClassic) {
|
||||
nitro_restriction = NitroClassicAttachmentSizeLimit;
|
||||
} else if (nitro == EPremiumType::Nitro) {
|
||||
nitro_restriction = NitroAttachmentSizeLimit;
|
||||
}
|
||||
|
||||
int guild_restriction = BaseAttachmentSizeLimit;
|
||||
if (const auto channel = discord.GetChannel(m_active_channel); channel.has_value() && channel->GuildID.has_value()) {
|
||||
if (const auto guild = discord.GetGuild(*channel->GuildID); guild.has_value()) {
|
||||
if (!guild->PremiumTier.has_value() || guild->PremiumTier == GuildPremiumTier::NONE || guild->PremiumTier == GuildPremiumTier::TIER_1) {
|
||||
guild_restriction = BaseAttachmentSizeLimit;
|
||||
} else if (guild->PremiumTier == GuildPremiumTier::TIER_2) {
|
||||
guild_restriction = BoostLevel2AttachmentSizeLimit;
|
||||
} else if (guild->PremiumTier == GuildPremiumTier::TIER_3) {
|
||||
guild_restriction = BoostLevel3AttachmentSizeLimit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int restriction = std::max(nitro_restriction, guild_restriction);
|
||||
|
||||
goffset total_size = 0;
|
||||
for (const auto &attachment : data.Attachments) {
|
||||
const auto info = attachment.File->query_info();
|
||||
if (info) {
|
||||
const auto size = info->get_size();
|
||||
if (size > restriction) {
|
||||
m_input->IndicateTooLarge();
|
||||
return false;
|
||||
}
|
||||
total_size += size;
|
||||
if (total_size > MaxMessagePayloadSize) {
|
||||
m_input->IndicateTooLarge();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_rate_limit_indicator->CanSpeak())
|
||||
return false;
|
||||
|
||||
if (text.size() == 0)
|
||||
if (data.Message.empty() && data.Attachments.empty())
|
||||
return false;
|
||||
|
||||
data.ChannelID = m_active_channel;
|
||||
data.InReplyToID = m_replying_to;
|
||||
|
||||
if (m_active_channel.IsValid())
|
||||
m_signal_action_chat_submit.emit(text, m_active_channel, m_replying_to); // m_replying_to is checked for invalid in the handler
|
||||
m_signal_action_chat_submit.emit(data); // m_replying_to is checked for invalid in the handler
|
||||
if (m_is_replying)
|
||||
StopReplying();
|
||||
|
||||
@@ -186,8 +300,7 @@ void ChatWindow::StartReplying(Snowflake message_id) {
|
||||
const auto author = discord.GetUser(message.Author.ID);
|
||||
m_replying_to = message_id;
|
||||
m_is_replying = true;
|
||||
m_input->grab_focus();
|
||||
m_input->get_style_context()->add_class("replying");
|
||||
m_input->StartReplying();
|
||||
if (author.has_value())
|
||||
m_input_indicator->SetCustomMarkup("Replying to " + author->GetEscapedBoldString<false>());
|
||||
else
|
||||
@@ -197,7 +310,7 @@ void ChatWindow::StartReplying(Snowflake message_id) {
|
||||
void ChatWindow::StopReplying() {
|
||||
m_is_replying = false;
|
||||
m_replying_to = Snowflake::Invalid;
|
||||
m_input->get_style_context()->remove_class("replying");
|
||||
m_input->StopReplying();
|
||||
m_input_indicator->ClearCustom();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,14 @@
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include "discord/discord.hpp"
|
||||
#include "discord/chatsubmitparams.hpp"
|
||||
#include "completer.hpp"
|
||||
#include "state.hpp"
|
||||
#include "progressbar.hpp"
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
class ChannelTabSwitcherHandy;
|
||||
#endif
|
||||
|
||||
class ChatMessageHeader;
|
||||
class ChatMessageItemContainer;
|
||||
@@ -25,10 +32,22 @@ public:
|
||||
void DeleteMessage(Snowflake id); // add [deleted] indicator
|
||||
void UpdateMessage(Snowflake id); // add [edited] indicator
|
||||
void AddNewHistory(const std::vector<Message> &msgs); // prepend messages
|
||||
void InsertChatInput(std::string text);
|
||||
void InsertChatInput(const std::string &text);
|
||||
Snowflake GetOldestListedMessage(); // oldest message that is currently in the ListBox
|
||||
void UpdateReactions(Snowflake id);
|
||||
void SetTopic(const std::string &text);
|
||||
void AddAttachment(const Glib::RefPtr<Gio::File> &file);
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
void OpenNewTab(Snowflake id);
|
||||
TabsState GetTabsState();
|
||||
void UseTabsState(const TabsState &state);
|
||||
void GoBack();
|
||||
void GoForward();
|
||||
void GoToPreviousTab();
|
||||
void GoToNextTab();
|
||||
void GoToTab(int idx);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
bool m_is_replying = false;
|
||||
@@ -39,7 +58,7 @@ protected:
|
||||
|
||||
Snowflake m_active_channel;
|
||||
|
||||
bool OnInputSubmit(const Glib::ustring &text);
|
||||
bool OnInputSubmit(ChatSubmitParams data);
|
||||
|
||||
bool OnKeyPressEvent(GdkEventKey *e);
|
||||
void OnScrollEdgeOvershot(Gtk::PositionType pos);
|
||||
@@ -47,8 +66,8 @@ protected:
|
||||
void OnMessageSendFail(const std::string &nonce, float retry_after);
|
||||
|
||||
Gtk::Box *m_main;
|
||||
//Gtk::ListBox *m_list;
|
||||
//Gtk::ScrolledWindow *m_scroll;
|
||||
// Gtk::ListBox *m_list;
|
||||
// Gtk::ScrolledWindow *m_scroll;
|
||||
|
||||
Gtk::EventBox m_topic; // todo probably make everything else go on the stack
|
||||
Gtk::Label m_topic_text;
|
||||
@@ -61,15 +80,20 @@ protected:
|
||||
ChatInputIndicator *m_input_indicator;
|
||||
RateLimitIndicator *m_rate_limit_indicator;
|
||||
Gtk::Box *m_meta;
|
||||
MessageUploadProgressBar m_progress;
|
||||
|
||||
#ifdef WITH_LIBHANDY
|
||||
ChannelTabSwitcherHandy *m_tab_switcher;
|
||||
#endif
|
||||
|
||||
public:
|
||||
typedef sigc::signal<void, Snowflake, Snowflake> type_signal_action_message_edit;
|
||||
typedef sigc::signal<void, std::string, Snowflake, Snowflake> type_signal_action_chat_submit;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_chat_load_history;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_channel_click;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_insert_mention;
|
||||
typedef sigc::signal<void, Snowflake, Glib::ustring> type_signal_action_reaction_add;
|
||||
typedef sigc::signal<void, Snowflake, Glib::ustring> type_signal_action_reaction_remove;
|
||||
using type_signal_action_message_edit = sigc::signal<void, Snowflake, Snowflake>;
|
||||
using type_signal_action_chat_submit = sigc::signal<void, ChatSubmitParams>;
|
||||
using type_signal_action_chat_load_history = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_channel_click = sigc::signal<void, Snowflake, bool>;
|
||||
using type_signal_action_insert_mention = sigc::signal<void, Snowflake>;
|
||||
using type_signal_action_reaction_add = sigc::signal<void, Snowflake, Glib::ustring>;
|
||||
using type_signal_action_reaction_remove = sigc::signal<void, Snowflake, Glib::ustring>;
|
||||
|
||||
type_signal_action_message_edit signal_action_message_edit();
|
||||
type_signal_action_chat_submit signal_action_chat_submit();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include "completer.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "util.hpp"
|
||||
@@ -46,7 +47,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) {
|
||||
|
||||
switch (e->keyval) {
|
||||
case GDK_KEY_Down: {
|
||||
if (m_entries.size() == 0) return true;
|
||||
if (m_entries.empty()) return true;
|
||||
const auto index = static_cast<size_t>(m_list.get_selected_row()->get_index());
|
||||
if (index >= m_entries.size() - 1) return true;
|
||||
m_list.select_row(*m_entries[index + 1]);
|
||||
@@ -54,7 +55,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) {
|
||||
}
|
||||
return true;
|
||||
case GDK_KEY_Up: {
|
||||
if (m_entries.size() == 0) return true;
|
||||
if (m_entries.empty()) return true;
|
||||
const auto index = static_cast<size_t>(m_list.get_selected_row()->get_index());
|
||||
if (index == 0) return true;
|
||||
m_list.select_row(*m_entries[index - 1]);
|
||||
@@ -62,7 +63,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) {
|
||||
}
|
||||
return true;
|
||||
case GDK_KEY_Return: {
|
||||
if (m_entries.size() == 0) return true;
|
||||
if (m_entries.empty()) return true;
|
||||
DoCompletion(m_list.get_selected_row());
|
||||
}
|
||||
return true;
|
||||
@@ -74,11 +75,11 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) {
|
||||
}
|
||||
|
||||
void Completer::SetGetRecentAuthors(get_recent_authors_cb cb) {
|
||||
m_recent_authors_cb = cb;
|
||||
m_recent_authors_cb = std::move(cb);
|
||||
}
|
||||
|
||||
void Completer::SetGetChannelID(get_channel_id_cb cb) {
|
||||
m_channel_id_cb = cb;
|
||||
m_channel_id_cb = std::move(cb);
|
||||
}
|
||||
|
||||
bool Completer::IsShown() const {
|
||||
@@ -86,7 +87,7 @@ bool Completer::IsShown() const {
|
||||
}
|
||||
|
||||
CompleterEntry *Completer::CreateEntry(const Glib::ustring &completion) {
|
||||
auto entry = Gtk::manage(new CompleterEntry(completion, m_entries.size()));
|
||||
auto entry = Gtk::manage(new CompleterEntry(completion, static_cast<int>(m_entries.size())));
|
||||
m_entries.push_back(entry);
|
||||
entry->show_all();
|
||||
m_list.add(*entry);
|
||||
@@ -152,7 +153,7 @@ void Completer::CompleteEmojis(const Glib::ustring &term) {
|
||||
const auto make_entry = [&](const Glib::ustring &name, const Glib::ustring &completion, const Glib::ustring &url = "", bool animated = false) -> CompleterEntry * {
|
||||
const auto entry = CreateEntry(completion);
|
||||
entry->SetText(name);
|
||||
if (url == "") return entry;
|
||||
if (url.empty()) return entry;
|
||||
if (animated)
|
||||
entry->SetAnimation(url);
|
||||
else
|
||||
@@ -173,8 +174,8 @@ void Completer::CompleteEmojis(const Glib::ustring &term) {
|
||||
const auto emoji = *discord.GetEmoji(tmp.ID);
|
||||
if (emoji.IsAnimated.has_value() && *emoji.IsAnimated) continue;
|
||||
if (emoji.IsAvailable.has_value() && !*emoji.IsAvailable) continue;
|
||||
if (emoji.Roles.has_value() && emoji.Roles->size() > 0) continue;
|
||||
if (term.size() > 0)
|
||||
if (emoji.Roles.has_value() && !emoji.Roles->empty()) continue;
|
||||
if (!term.empty())
|
||||
if (!StringContainsCaseless(emoji.Name, term)) continue;
|
||||
|
||||
if (i++ > MaxCompleterEntries) break;
|
||||
@@ -190,8 +191,8 @@ void Completer::CompleteEmojis(const Glib::ustring &term) {
|
||||
const auto emoji = *discord.GetEmoji(tmp.ID);
|
||||
const bool is_animated = emoji.IsAnimated.has_value() && *emoji.IsAnimated;
|
||||
if (emoji.IsAvailable.has_value() && !*emoji.IsAvailable) continue;
|
||||
if (emoji.Roles.has_value() && emoji.Roles->size() > 0) continue;
|
||||
if (term.size() > 0)
|
||||
if (emoji.Roles.has_value() && !emoji.Roles->empty()) continue;
|
||||
if (!term.empty())
|
||||
if (!StringContainsCaseless(emoji.Name, term)) continue;
|
||||
|
||||
if (i++ > MaxCompleterEntries) goto done;
|
||||
@@ -275,7 +276,7 @@ void Completer::OnTextBufferChanged() {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (m_entries.size() > 0) {
|
||||
if (!m_entries.empty()) {
|
||||
m_list.select_row(*m_entries[0]);
|
||||
set_reveal_child(true);
|
||||
} else {
|
||||
@@ -329,8 +330,8 @@ Glib::ustring Completer::GetTerm() {
|
||||
return m_start.get_text(m_end);
|
||||
}
|
||||
|
||||
CompleterEntry::CompleterEntry(const Glib::ustring &completion, int index)
|
||||
: m_completion(completion)
|
||||
CompleterEntry::CompleterEntry(Glib::ustring completion, int index)
|
||||
: m_completion(std::move(completion))
|
||||
, m_index(index)
|
||||
, m_box(Gtk::ORIENTATION_HORIZONTAL) {
|
||||
set_halign(Gtk::ALIGN_START);
|
||||
|
||||
@@ -8,7 +8,7 @@ constexpr static int CompleterImageSize = 24;
|
||||
|
||||
class CompleterEntry : public Gtk::ListBoxRow {
|
||||
public:
|
||||
CompleterEntry(const Glib::ustring &completion, int index);
|
||||
CompleterEntry(Glib::ustring completion, int index);
|
||||
void SetTextColor(int color); // SetText will reset
|
||||
void SetText(const Glib::ustring &text);
|
||||
void SetImage(const Glib::RefPtr<Gdk::Pixbuf> &pb);
|
||||
|
||||
@@ -102,7 +102,7 @@ bool DragListBox::scroll() {
|
||||
}
|
||||
|
||||
void DragListBox::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext> &context, int x, int y, const Gtk::SelectionData &selection_data, guint info, guint time) {
|
||||
int index = 0;
|
||||
int index;
|
||||
if (m_hover_row != nullptr) {
|
||||
if (m_top) {
|
||||
index = m_hover_row->get_index() - 1;
|
||||
@@ -130,7 +130,7 @@ void DragListBox::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext> &co
|
||||
void DragListBox::add_draggable(Gtk::ListBoxRow *widget) {
|
||||
widget->drag_source_set(m_entries, Gdk::BUTTON1_MASK, Gdk::ACTION_MOVE);
|
||||
widget->signal_drag_begin().connect(sigc::bind<0>(sigc::mem_fun(*this, &DragListBox::row_drag_begin), widget));
|
||||
widget->signal_drag_data_get().connect([this, widget](const Glib::RefPtr<Gdk::DragContext> &context, Gtk::SelectionData &selection_data, guint info, guint time) {
|
||||
widget->signal_drag_data_get().connect([widget](const Glib::RefPtr<Gdk::DragContext> &context, Gtk::SelectionData &selection_data, guint info, guint time) {
|
||||
selection_data.set("GTK_LIST_BOX_ROW", 32, reinterpret_cast<const guint8 *>(&widget), sizeof(&widget));
|
||||
});
|
||||
add(*widget);
|
||||
|
||||
@@ -133,7 +133,7 @@ void FriendsList::OnActionRemove(Snowflake id) {
|
||||
break;
|
||||
}
|
||||
if (Abaddon::Get().ShowConfirm(str, window)) {
|
||||
const auto cb = [this, window](DiscordError code) {
|
||||
const auto cb = [window](DiscordError code) {
|
||||
if (code == DiscordError::NONE) return;
|
||||
Gtk::MessageDialog dlg(*window, "Failed to remove user", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
|
||||
dlg.set_position(Gtk::WIN_POS_CENTER);
|
||||
@@ -215,7 +215,7 @@ void FriendsListAddComponent::Submit() {
|
||||
if (hashpos == Glib::ustring::npos) return;
|
||||
const auto username = text.substr(0, hashpos);
|
||||
const auto discriminator = text.substr(hashpos + 1);
|
||||
if (username.size() == 0 || discriminator.size() != 4) return;
|
||||
if (username.empty() || discriminator.size() != 4) return;
|
||||
if (discriminator.find_first_not_of("0123456789") != Glib::ustring::npos) return;
|
||||
|
||||
m_requesting = true;
|
||||
@@ -229,7 +229,9 @@ void FriendsListAddComponent::Submit() {
|
||||
m_label.set_text("Failed: "s + GetDiscordErrorDisplayString(code));
|
||||
}
|
||||
};
|
||||
Abaddon::Get().GetDiscordClient().SendFriendRequest(username, std::stoul(discriminator), sigc::track_obj(cb, *this));
|
||||
Abaddon::Get().GetDiscordClient().SendFriendRequest(username,
|
||||
static_cast<int>(std::stoul(discriminator)),
|
||||
sigc::track_obj(cb, *this));
|
||||
}
|
||||
|
||||
bool FriendsListAddComponent::OnKeyPress(GdkEventKey *e) {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "lazyimage.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include "abaddon.hpp"
|
||||
|
||||
LazyImage::LazyImage(int w, int h, bool use_placeholder)
|
||||
@@ -9,8 +11,8 @@ LazyImage::LazyImage(int w, int h, bool use_placeholder)
|
||||
signal_draw().connect(sigc::mem_fun(*this, &LazyImage::OnDraw));
|
||||
}
|
||||
|
||||
LazyImage::LazyImage(const std::string &url, int w, int h, bool use_placeholder)
|
||||
: m_url(url)
|
||||
LazyImage::LazyImage(std::string url, int w, int h, bool use_placeholder)
|
||||
: m_url(std::move(url))
|
||||
, m_width(w)
|
||||
, m_height(h) {
|
||||
if (use_placeholder)
|
||||
@@ -27,7 +29,7 @@ void LazyImage::SetURL(const std::string &url) {
|
||||
}
|
||||
|
||||
bool LazyImage::OnDraw(const Cairo::RefPtr<Cairo::Context> &context) {
|
||||
if (!m_needs_request || m_url == "") return false;
|
||||
if (!m_needs_request || m_url.empty()) return false;
|
||||
m_needs_request = false;
|
||||
|
||||
if (m_animated) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
class LazyImage : public Gtk::Image {
|
||||
public:
|
||||
LazyImage(int w, int h, bool use_placeholder = true);
|
||||
LazyImage(const std::string &url, int w, int h, bool use_placeholder = true);
|
||||
LazyImage(std::string url, int w, int h, bool use_placeholder = true);
|
||||
|
||||
void SetAnimated(bool is_animated);
|
||||
void SetURL(const std::string &url);
|
||||
|
||||
@@ -151,7 +151,7 @@ void MemberList::UpdateMemberList() {
|
||||
if (!pos_role.has_value()) {
|
||||
roleless_users.push_back(id);
|
||||
continue;
|
||||
};
|
||||
}
|
||||
|
||||
pos_to_role[pos_role->Position] = *pos_role;
|
||||
pos_to_users[pos_role->Position].push_back(std::move(*user));
|
||||
@@ -160,17 +160,18 @@ void MemberList::UpdateMemberList() {
|
||||
}
|
||||
|
||||
int num_rows = 0;
|
||||
const auto guild = *discord.GetGuild(m_guild_id);
|
||||
auto add_user = [this, &user_to_color, &num_rows, guild](const UserData &data) -> bool {
|
||||
const auto guild = discord.GetGuild(m_guild_id);
|
||||
if (!guild.has_value()) return;
|
||||
auto add_user = [this, &num_rows, guild](const UserData &data) -> bool {
|
||||
if (num_rows++ > MaxMemberListRows) return false;
|
||||
auto *row = Gtk::manage(new MemberListUserRow(guild, data));
|
||||
auto *row = Gtk::manage(new MemberListUserRow(*guild, data));
|
||||
m_id_to_row[data.ID] = row;
|
||||
AttachUserMenuHandler(row, data.ID);
|
||||
m_listbox->add(*row);
|
||||
return true;
|
||||
};
|
||||
|
||||
auto add_role = [this](std::string name) {
|
||||
auto add_role = [this](const std::string &name) {
|
||||
auto *role_row = Gtk::manage(new Gtk::ListBoxRow);
|
||||
auto *role_lbl = Gtk::manage(new Gtk::Label);
|
||||
|
||||
@@ -215,7 +216,7 @@ void MemberList::UpdateMemberList() {
|
||||
}
|
||||
|
||||
void MemberList::AttachUserMenuHandler(Gtk::ListBoxRow *row, Snowflake id) {
|
||||
row->signal_button_press_event().connect([this, row, id](GdkEventButton *e) -> bool {
|
||||
row->signal_button_press_event().connect([this, id](GdkEventButton *e) -> bool {
|
||||
if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) {
|
||||
Abaddon::Get().ShowUserMenu(reinterpret_cast<const GdkEvent *>(e), id, m_guild_id);
|
||||
return true;
|
||||
|
||||
24
src/components/progressbar.cpp
Normal file
24
src/components/progressbar.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "progressbar.hpp"
|
||||
#include "abaddon.hpp"
|
||||
|
||||
MessageUploadProgressBar::MessageUploadProgressBar() {
|
||||
get_style_context()->add_class("message-progress");
|
||||
auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
discord.signal_message_progress().connect([this](const std::string &nonce, float percent) {
|
||||
if (nonce == m_last_nonce) {
|
||||
set_fraction(percent);
|
||||
}
|
||||
});
|
||||
discord.signal_message_send_fail().connect([this](const std::string &nonce, float) {
|
||||
if (nonce == m_last_nonce)
|
||||
set_fraction(0.0);
|
||||
});
|
||||
discord.signal_message_create().connect([this](const Message &msg) {
|
||||
if (msg.IsPending) {
|
||||
m_last_nonce = *msg.Nonce;
|
||||
} else if (msg.Nonce.has_value() && (*msg.Nonce == m_last_nonce)) {
|
||||
m_last_nonce = "";
|
||||
set_fraction(0.0);
|
||||
}
|
||||
});
|
||||
}
|
||||
11
src/components/progressbar.hpp
Normal file
11
src/components/progressbar.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <gtkmm/progressbar.h>
|
||||
#include <string>
|
||||
|
||||
class MessageUploadProgressBar : public Gtk::ProgressBar {
|
||||
public:
|
||||
MessageUploadProgressBar();
|
||||
|
||||
private:
|
||||
std::string m_last_nonce;
|
||||
};
|
||||
@@ -66,7 +66,7 @@ int RateLimitIndicator::GetTimeLeft() const {
|
||||
if (sec_diff <= 0)
|
||||
return 0;
|
||||
else
|
||||
return sec_diff;
|
||||
return static_cast<int>(sec_diff);
|
||||
}
|
||||
|
||||
int RateLimitIndicator::GetRateLimit() const {
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
#include "abaddon.hpp"
|
||||
|
||||
static const constexpr int Diameter = 8;
|
||||
static const auto OnlineColor = Gdk::RGBA("#43B581");
|
||||
static const auto IdleColor = Gdk::RGBA("#FAA61A");
|
||||
static const auto DNDColor = Gdk::RGBA("#982929");
|
||||
static const auto OfflineColor = Gdk::RGBA("#808080");
|
||||
|
||||
StatusIndicator::StatusIndicator(Snowflake user_id)
|
||||
: Glib::ObjectBase("statusindicator")
|
||||
@@ -26,9 +22,6 @@ StatusIndicator::StatusIndicator(Snowflake user_id)
|
||||
CheckStatus();
|
||||
}
|
||||
|
||||
StatusIndicator::~StatusIndicator() {
|
||||
}
|
||||
|
||||
void StatusIndicator::CheckStatus() {
|
||||
const auto status = Abaddon::Get().GetDiscordClient().GetUserStatus(m_id);
|
||||
const auto last_status = m_status;
|
||||
@@ -121,7 +114,7 @@ bool StatusIndicator::on_draw(const Cairo::RefPtr<Cairo::Context> &cr) {
|
||||
const auto color = get_style_context()->get_color(Gtk::STATE_FLAG_NORMAL);
|
||||
|
||||
cr->set_source_rgb(color.get_red(), color.get_green(), color.get_blue());
|
||||
cr->arc(width / 2, height / 2, width / 3, 0.0, 2 * (4 * std::atan(1)));
|
||||
cr->arc(width / 2.0, height / 2.0, width / 3.0, 0.0, 2 * (4 * std::atan(1)));
|
||||
cr->close_path();
|
||||
cr->fill_preserve();
|
||||
cr->stroke();
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
class StatusIndicator : public Gtk::Widget {
|
||||
public:
|
||||
StatusIndicator(Snowflake user_id);
|
||||
virtual ~StatusIndicator();
|
||||
~StatusIndicator() override = default;
|
||||
|
||||
protected:
|
||||
Gtk::SizeRequestMode get_request_mode_vfunc() const override;
|
||||
void get_preferred_width_vfunc(int &minimum_width, int &natural_width) const override;
|
||||
void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override;
|
||||
void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override;
|
||||
void get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const override;
|
||||
void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override;
|
||||
void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override;
|
||||
void on_size_allocate(Gtk::Allocation &allocation) override;
|
||||
void on_map() override;
|
||||
void on_unmap() override;
|
||||
|
||||
89
src/components/voiceinfobox.cpp
Normal file
89
src/components/voiceinfobox.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "voiceinfobox.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
VoiceInfoBox::VoiceInfoBox()
|
||||
: Gtk::Box(Gtk::ORIENTATION_HORIZONTAL)
|
||||
, m_left(Gtk::ORIENTATION_VERTICAL) {
|
||||
m_disconnect_ev.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
||||
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_PRIMARY) {
|
||||
spdlog::get("discord")->debug("Request disconnect from info box");
|
||||
Abaddon::Get().GetDiscordClient().DisconnectFromVoice();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
AddPointerCursor(m_disconnect_ev);
|
||||
|
||||
get_style_context()->add_class("voice-info");
|
||||
m_status.get_style_context()->add_class("voice-info-status");
|
||||
m_location.get_style_context()->add_class("voice-info-location");
|
||||
m_disconnect_img.get_style_context()->add_class("voice-info-disconnect-image");
|
||||
|
||||
m_status.set_label("You shouldn't see me");
|
||||
m_location.set_label("You shouldn't see me");
|
||||
|
||||
Abaddon::Get().GetDiscordClient().signal_voice_requested_connect().connect([this](Snowflake channel_id) {
|
||||
show();
|
||||
|
||||
if (const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(channel_id); channel.has_value() && channel->Name.has_value()) {
|
||||
if (channel->GuildID.has_value()) {
|
||||
if (const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(*channel->GuildID); guild.has_value()) {
|
||||
m_location.set_label(*channel->Name + " / " + guild->Name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_location.set_label(*channel->Name);
|
||||
return;
|
||||
}
|
||||
|
||||
m_location.set_label("Unknown");
|
||||
});
|
||||
|
||||
Abaddon::Get().GetDiscordClient().signal_voice_requested_disconnect().connect([this]() {
|
||||
hide();
|
||||
});
|
||||
|
||||
Abaddon::Get().GetDiscordClient().signal_voice_client_state_update().connect([this](DiscordVoiceClient::State state) {
|
||||
Glib::ustring label;
|
||||
switch (state) {
|
||||
case DiscordVoiceClient::State::ConnectingToWebsocket:
|
||||
label = "Connecting";
|
||||
break;
|
||||
case DiscordVoiceClient::State::EstablishingConnection:
|
||||
label = "Establishing connection";
|
||||
break;
|
||||
case DiscordVoiceClient::State::Connected:
|
||||
label = "Connected";
|
||||
break;
|
||||
case DiscordVoiceClient::State::DisconnectedByServer:
|
||||
case DiscordVoiceClient::State::DisconnectedByClient:
|
||||
label = "Disconnected";
|
||||
break;
|
||||
default:
|
||||
label = "Unknown";
|
||||
break;
|
||||
}
|
||||
m_status.set_label(label);
|
||||
});
|
||||
|
||||
m_status.set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
m_location.set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
|
||||
m_disconnect_ev.add(m_disconnect_img);
|
||||
m_disconnect_img.property_icon_name() = "call-stop-symbolic";
|
||||
m_disconnect_img.property_icon_size() = 5;
|
||||
m_disconnect_img.set_hexpand(true);
|
||||
m_disconnect_img.set_halign(Gtk::ALIGN_END);
|
||||
|
||||
m_left.add(m_status);
|
||||
m_left.add(m_location);
|
||||
|
||||
add(m_left);
|
||||
add(m_disconnect_ev);
|
||||
|
||||
show_all_children();
|
||||
}
|
||||
19
src/components/voiceinfobox.hpp
Normal file
19
src/components/voiceinfobox.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <gtkmm/box.h>
|
||||
#include <gtkmm/eventbox.h>
|
||||
#include <gtkmm/image.h>
|
||||
#include <gtkmm/label.h>
|
||||
|
||||
class VoiceInfoBox : public Gtk::Box {
|
||||
public:
|
||||
VoiceInfoBox();
|
||||
|
||||
private:
|
||||
Gtk::Box m_left;
|
||||
Gtk::Label m_status;
|
||||
Gtk::Label m_location;
|
||||
|
||||
Gtk::EventBox m_disconnect_ev;
|
||||
Gtk::Image m_disconnect_img;
|
||||
};
|
||||
125
src/components/volumemeter.cpp
Normal file
125
src/components/volumemeter.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "volumemeter.hpp"
|
||||
#include <cstring>
|
||||
|
||||
VolumeMeter::VolumeMeter()
|
||||
: Glib::ObjectBase("volumemeter")
|
||||
, Gtk::Widget() {
|
||||
set_has_window(true);
|
||||
}
|
||||
|
||||
void VolumeMeter::SetVolume(double fraction) {
|
||||
m_fraction = fraction;
|
||||
queue_draw();
|
||||
}
|
||||
|
||||
void VolumeMeter::SetTick(double fraction) {
|
||||
m_tick = fraction;
|
||||
queue_draw();
|
||||
}
|
||||
|
||||
void VolumeMeter::SetShowTick(bool show) {
|
||||
m_show_tick = show;
|
||||
}
|
||||
|
||||
Gtk::SizeRequestMode VolumeMeter::get_request_mode_vfunc() const {
|
||||
return Gtk::Widget::get_request_mode_vfunc();
|
||||
}
|
||||
|
||||
void VolumeMeter::get_preferred_width_vfunc(int &minimum_width, int &natural_width) const {
|
||||
const int width = get_allocated_width();
|
||||
minimum_width = natural_width = width;
|
||||
}
|
||||
|
||||
void VolumeMeter::get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const {
|
||||
get_preferred_width_vfunc(minimum_width, natural_width);
|
||||
}
|
||||
|
||||
void VolumeMeter::get_preferred_height_vfunc(int &minimum_height, int &natural_height) const {
|
||||
// blehhh :PPP
|
||||
const int height = get_allocated_height();
|
||||
minimum_height = natural_height = 4;
|
||||
}
|
||||
|
||||
void VolumeMeter::get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const {
|
||||
get_preferred_height_vfunc(minimum_height, natural_height);
|
||||
}
|
||||
|
||||
void VolumeMeter::on_size_allocate(Gtk::Allocation &allocation) {
|
||||
set_allocation(allocation);
|
||||
|
||||
if (m_window)
|
||||
m_window->move_resize(allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height());
|
||||
}
|
||||
|
||||
void VolumeMeter::on_map() {
|
||||
Gtk::Widget::on_map();
|
||||
}
|
||||
|
||||
void VolumeMeter::on_unmap() {
|
||||
Gtk::Widget::on_unmap();
|
||||
}
|
||||
|
||||
void VolumeMeter::on_realize() {
|
||||
set_realized(true);
|
||||
|
||||
if (!m_window) {
|
||||
GdkWindowAttr attributes;
|
||||
std::memset(&attributes, 0, sizeof(attributes));
|
||||
|
||||
auto allocation = get_allocation();
|
||||
|
||||
attributes.x = allocation.get_x();
|
||||
attributes.y = allocation.get_y();
|
||||
attributes.width = allocation.get_width();
|
||||
attributes.height = allocation.get_height();
|
||||
|
||||
attributes.event_mask = get_events() | Gdk::EXPOSURE_MASK;
|
||||
attributes.window_type = GDK_WINDOW_CHILD;
|
||||
attributes.wclass = GDK_INPUT_OUTPUT;
|
||||
|
||||
m_window = Gdk::Window::create(get_parent_window(), &attributes, GDK_WA_X | GDK_WA_Y);
|
||||
set_window(m_window);
|
||||
|
||||
m_window->set_user_data(gobj());
|
||||
}
|
||||
}
|
||||
|
||||
void VolumeMeter::on_unrealize() {
|
||||
m_window.reset();
|
||||
|
||||
Gtk::Widget::on_unrealize();
|
||||
}
|
||||
|
||||
bool VolumeMeter::on_draw(const Cairo::RefPtr<Cairo::Context> &cr) {
|
||||
const auto allocation = get_allocation();
|
||||
const auto width = allocation.get_width();
|
||||
const auto height = allocation.get_height();
|
||||
|
||||
const double LOW_MAX = 0.7 * width;
|
||||
const double MID_MAX = 0.85 * width;
|
||||
const double desired_width = width * m_fraction;
|
||||
|
||||
const double draw_low = std::min(desired_width, LOW_MAX);
|
||||
const double draw_mid = std::min(desired_width, MID_MAX);
|
||||
const double draw_hi = desired_width;
|
||||
|
||||
cr->set_source_rgb(1.0, 0.0, 0.0);
|
||||
cr->rectangle(0.0, 0.0, draw_hi, height);
|
||||
cr->fill();
|
||||
cr->set_source_rgb(1.0, 0.5, 0.0);
|
||||
cr->rectangle(0.0, 0.0, draw_mid, height);
|
||||
cr->fill();
|
||||
cr->set_source_rgb(.0, 1.0, 0.0);
|
||||
cr->rectangle(0.0, 0.0, draw_low, height);
|
||||
cr->fill();
|
||||
|
||||
if (m_show_tick) {
|
||||
const double tick_base = width * m_tick;
|
||||
|
||||
cr->set_source_rgb(0.8, 0.8, 0.8);
|
||||
cr->rectangle(tick_base, 0, 4, height);
|
||||
cr->fill();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
31
src/components/volumemeter.hpp
Normal file
31
src/components/volumemeter.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <gtkmm/widget.h>
|
||||
|
||||
class VolumeMeter : public Gtk::Widget {
|
||||
public:
|
||||
VolumeMeter();
|
||||
|
||||
void SetVolume(double fraction);
|
||||
void SetTick(double fraction);
|
||||
void SetShowTick(bool show);
|
||||
|
||||
protected:
|
||||
Gtk::SizeRequestMode get_request_mode_vfunc() const override;
|
||||
void get_preferred_width_vfunc(int &minimum_width, int &natural_width) const override;
|
||||
void get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const override;
|
||||
void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override;
|
||||
void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override;
|
||||
void on_size_allocate(Gtk::Allocation &allocation) override;
|
||||
void on_map() override;
|
||||
void on_unmap() override;
|
||||
void on_realize() override;
|
||||
void on_unrealize() override;
|
||||
bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) override;
|
||||
|
||||
private:
|
||||
Glib::RefPtr<Gdk::Window> m_window;
|
||||
|
||||
double m_fraction = 0.0;
|
||||
double m_tick = 0.0;
|
||||
bool m_show_tick = false;
|
||||
};
|
||||
@@ -1,4 +1,12 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
constexpr static uint64_t SnowflakeSplitDifference = 600;
|
||||
constexpr static int MaxMessagesForChatCull = 50; // this has to be 50 (for now) cuz that magic number is used in a couple other places and i dont feel like replacing them
|
||||
constexpr static int AttachmentItemSize = 120;
|
||||
constexpr static int BaseAttachmentSizeLimit = 8 * 1024 * 1024;
|
||||
constexpr static int NitroClassicAttachmentSizeLimit = 50 * 1024 * 1024;
|
||||
constexpr static int NitroAttachmentSizeLimit = 100 * 1024 * 1024;
|
||||
constexpr static int BoostLevel2AttachmentSizeLimit = 50 * 1024 * 1024;
|
||||
constexpr static int BoostLevel3AttachmentSizeLimit = 100 * 1024 * 1024;
|
||||
constexpr static int MaxMessagePayloadSize = 199 * 1024 * 1024;
|
||||
|
||||
@@ -34,3 +34,7 @@ ConfirmDialog::ConfirmDialog(Gtk::Window &parent)
|
||||
void ConfirmDialog::SetConfirmText(const Glib::ustring &text) {
|
||||
m_label.set_text(text);
|
||||
}
|
||||
|
||||
void ConfirmDialog::SetAcceptOnly(bool accept_only) {
|
||||
m_cancel.set_visible(!accept_only);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ class ConfirmDialog : public Gtk::Dialog {
|
||||
public:
|
||||
ConfirmDialog(Gtk::Window &parent);
|
||||
void SetConfirmText(const Glib::ustring &text);
|
||||
void SetAcceptOnly(bool accept_only);
|
||||
|
||||
protected:
|
||||
Gtk::Label m_label;
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
#include "joinguild.hpp"
|
||||
#include "abaddon.hpp"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
|
||||
JoinGuildDialog::JoinGuildDialog(Gtk::Window &parent)
|
||||
: Gtk::Dialog("Join Server", parent, true)
|
||||
, m_layout(Gtk::ORIENTATION_VERTICAL)
|
||||
, m_ok("OK")
|
||||
, m_cancel("Cancel")
|
||||
, m_info("Enter code") {
|
||||
set_default_size(300, 50);
|
||||
get_style_context()->add_class("app-window");
|
||||
get_style_context()->add_class("app-popup");
|
||||
|
||||
Glib::signal_idle().connect(sigc::mem_fun(*this, &JoinGuildDialog::on_idle_slot));
|
||||
|
||||
m_entry.signal_changed().connect(sigc::mem_fun(*this, &JoinGuildDialog::on_entry_changed));
|
||||
|
||||
m_ok.set_sensitive(false);
|
||||
|
||||
m_ok.signal_clicked().connect([&]() {
|
||||
response(Gtk::RESPONSE_OK);
|
||||
});
|
||||
|
||||
m_cancel.signal_clicked().connect([&]() {
|
||||
response(Gtk::RESPONSE_CANCEL);
|
||||
});
|
||||
|
||||
m_entry.set_hexpand(true);
|
||||
m_layout.add(m_entry);
|
||||
m_lower.set_hexpand(true);
|
||||
m_lower.pack_start(m_info);
|
||||
m_info.set_halign(Gtk::ALIGN_START);
|
||||
m_lower.pack_start(m_ok, Gtk::PACK_SHRINK);
|
||||
m_lower.pack_start(m_cancel, Gtk::PACK_SHRINK);
|
||||
m_ok.set_halign(Gtk::ALIGN_END);
|
||||
m_cancel.set_halign(Gtk::ALIGN_END);
|
||||
m_layout.add(m_lower);
|
||||
get_content_area()->add(m_layout);
|
||||
|
||||
show_all_children();
|
||||
}
|
||||
|
||||
void JoinGuildDialog::on_entry_changed() {
|
||||
std::string s = m_entry.get_text();
|
||||
std::regex invite_regex(R"((https?:\/\/)?discord\.(gg(\/invite)?\/|com\/invite\/)([A-Za-z0-9\-]+))", std::regex_constants::ECMAScript);
|
||||
std::smatch match;
|
||||
bool full_url = std::regex_search(s, match, invite_regex);
|
||||
if (full_url || IsCode(s)) {
|
||||
m_code = full_url ? match[4].str() : s;
|
||||
m_needs_request = true;
|
||||
m_ok.set_sensitive(false);
|
||||
} else {
|
||||
m_ok.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
|
||||
void JoinGuildDialog::CheckCode() {
|
||||
auto cb = [this](const std::optional<InviteData> &invite) {
|
||||
if (invite.has_value()) {
|
||||
m_ok.set_sensitive(true);
|
||||
if (invite->Guild.has_value()) {
|
||||
if (invite->MemberCount.has_value())
|
||||
m_info.set_text(invite->Guild->Name + " (" + std::to_string(*invite->MemberCount) + " members)");
|
||||
else
|
||||
m_info.set_text(invite->Guild->Name);
|
||||
} else {
|
||||
m_info.set_text("Group DM (" + std::to_string(*invite->MemberCount) + " members)");
|
||||
}
|
||||
} else {
|
||||
m_ok.set_sensitive(false);
|
||||
m_info.set_text("Invalid invite");
|
||||
}
|
||||
};
|
||||
Abaddon::Get().GetDiscordClient().FetchInvite(m_code, sigc::track_obj(cb, *this));
|
||||
}
|
||||
|
||||
bool JoinGuildDialog::IsCode(std::string str) {
|
||||
return str.length() >= 2 && std::all_of(str.begin(), str.end(), [](char c) -> bool { return std::isalnum(c) || c == '-'; });
|
||||
}
|
||||
|
||||
std::string JoinGuildDialog::GetCode() {
|
||||
return m_code;
|
||||
}
|
||||
|
||||
static const constexpr int RateLimitMS = 1500;
|
||||
bool JoinGuildDialog::on_idle_slot() {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (m_needs_request && ((now - m_last_req_time) > std::chrono::milliseconds(RateLimitMS))) {
|
||||
m_needs_request = false;
|
||||
m_last_req_time = now;
|
||||
CheckCode();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
#include <gtkmm.h>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
class JoinGuildDialog : public Gtk::Dialog {
|
||||
public:
|
||||
JoinGuildDialog(Gtk::Window &parent);
|
||||
std::string GetCode();
|
||||
|
||||
protected:
|
||||
void on_entry_changed();
|
||||
bool IsCode(std::string str);
|
||||
|
||||
Gtk::Box m_layout;
|
||||
Gtk::Button m_ok;
|
||||
Gtk::Button m_cancel;
|
||||
Gtk::Box m_lower;
|
||||
Gtk::Label m_info;
|
||||
Gtk::Entry m_entry;
|
||||
|
||||
void CheckCode();
|
||||
|
||||
// needs a rate limit cuz if u hit it u get ip banned from /invites for a long time :(
|
||||
bool m_needs_request = false;
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_last_req_time;
|
||||
bool on_idle_slot();
|
||||
|
||||
private:
|
||||
std::string m_code;
|
||||
};
|
||||
26
src/dialogs/textinput.cpp
Normal file
26
src/dialogs/textinput.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "textinput.hpp"
|
||||
|
||||
TextInputDialog::TextInputDialog(const Glib::ustring &prompt, const Glib::ustring &title, const Glib::ustring &placeholder, Gtk::Window &parent)
|
||||
: Gtk::Dialog(title, parent, true)
|
||||
, m_label(prompt) {
|
||||
get_style_context()->add_class("app-window");
|
||||
get_style_context()->add_class("app-popup");
|
||||
|
||||
auto ok = add_button("OK", Gtk::RESPONSE_OK);
|
||||
auto cancel = add_button("Cancel", Gtk::RESPONSE_CANCEL);
|
||||
|
||||
get_content_area()->add(m_label);
|
||||
get_content_area()->add(m_entry);
|
||||
|
||||
m_entry.set_text(placeholder);
|
||||
|
||||
m_entry.set_activates_default(true);
|
||||
ok->set_can_default(true);
|
||||
ok->grab_default();
|
||||
|
||||
show_all_children();
|
||||
}
|
||||
|
||||
Glib::ustring TextInputDialog::GetInput() const {
|
||||
return m_entry.get_text();
|
||||
}
|
||||
14
src/dialogs/textinput.hpp
Normal file
14
src/dialogs/textinput.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <gtkmm/dialog.h>
|
||||
#include <gtkmm/entry.h>
|
||||
|
||||
class TextInputDialog : public Gtk::Dialog {
|
||||
public:
|
||||
TextInputDialog(const Glib::ustring &prompt, const Glib::ustring &title, const Glib::ustring &placeholder, Gtk::Window &parent);
|
||||
|
||||
Glib::ustring GetInput() const;
|
||||
|
||||
private:
|
||||
Gtk::Label m_label;
|
||||
Gtk::Entry m_entry;
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "token.hpp"
|
||||
|
||||
std::string trim(const std::string& str) {
|
||||
std::string trim(const std::string &str) {
|
||||
const auto first = str.find_first_not_of(' ');
|
||||
if (first == std::string::npos) return str;
|
||||
const auto last = str.find_last_not_of(' ');
|
||||
@@ -30,6 +30,8 @@ TokenDialog::TokenDialog(Gtk::Window &parent)
|
||||
m_bbox.pack_start(m_cancel, Gtk::PACK_SHRINK);
|
||||
m_bbox.set_layout(Gtk::BUTTONBOX_END);
|
||||
|
||||
m_entry.set_input_purpose(Gtk::INPUT_PURPOSE_PASSWORD);
|
||||
m_entry.set_visibility(false);
|
||||
m_entry.set_hexpand(true);
|
||||
m_layout.add(m_entry);
|
||||
m_layout.add(m_bbox);
|
||||
|
||||
@@ -26,7 +26,7 @@ constexpr inline const char *GetPresenceString(PresenceStatus s) {
|
||||
return "";
|
||||
}
|
||||
|
||||
constexpr inline const char* GetPresenceDisplayString(PresenceStatus s) {
|
||||
constexpr inline const char *GetPresenceDisplayString(PresenceStatus s) {
|
||||
switch (s) {
|
||||
case PresenceStatus::Online:
|
||||
return "Online";
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
struct BanData {
|
||||
std::string Reason; // null
|
||||
UserData User; // access id
|
||||
UserData User; // access id
|
||||
|
||||
friend void from_json(const nlohmann::json &j, BanData &m);
|
||||
};
|
||||
|
||||
@@ -89,7 +89,28 @@ bool ChannelData::HasIcon() const noexcept {
|
||||
}
|
||||
|
||||
std::string ChannelData::GetIconURL() const {
|
||||
return "https://cdn.discordapp.com/channel-icons/" + std::to_string(ID) + "/" + *Icon + ".png";
|
||||
if (HasIcon()) {
|
||||
return "https://cdn.discordapp.com/channel-icons/" + std::to_string(ID) + "/" + *Icon + ".png";
|
||||
} else {
|
||||
const auto recipients = GetDMRecipients();
|
||||
if (!recipients.empty())
|
||||
return recipients[0].GetAvatarURL("png", "32");
|
||||
else
|
||||
return "https://cdn.discordapp.com/embed/avatars/0.png";
|
||||
}
|
||||
}
|
||||
|
||||
std::string ChannelData::GetDisplayName() const {
|
||||
if (Name.has_value()) {
|
||||
return "#" + *Name;
|
||||
} else {
|
||||
const auto recipients = GetDMRecipients();
|
||||
if (Type == ChannelType::DM && !recipients.empty())
|
||||
return recipients[0].Username;
|
||||
else if (Type == ChannelType::GROUP_DM)
|
||||
return std::to_string(recipients.size()) + " members";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
std::vector<Snowflake> ChannelData::GetChildIDs() const {
|
||||
@@ -116,5 +137,8 @@ std::vector<UserData> ChannelData::GetDMRecipients() const {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return std::vector<UserData>();
|
||||
return {};
|
||||
}
|
||||
bool ChannelData::IsText() const noexcept {
|
||||
return Type == ChannelType::GUILD_TEXT || Type == ChannelType::GUILD_NEWS;
|
||||
}
|
||||
|
||||
@@ -94,14 +94,16 @@ struct ChannelData {
|
||||
friend void from_json(const nlohmann::json &j, ChannelData &m);
|
||||
void update_from_json(const nlohmann::json &j);
|
||||
|
||||
bool NSFW() const;
|
||||
bool IsDM() const noexcept;
|
||||
bool IsThread() const noexcept;
|
||||
bool IsJoinedThread() const;
|
||||
bool IsCategory() const noexcept;
|
||||
bool HasIcon() const noexcept;
|
||||
std::string GetIconURL() const;
|
||||
std::vector<Snowflake> GetChildIDs() const;
|
||||
std::optional<PermissionOverwrite> GetOverwrite(Snowflake id) const;
|
||||
std::vector<UserData> GetDMRecipients() const;
|
||||
[[nodiscard]] bool NSFW() const;
|
||||
[[nodiscard]] bool IsDM() const noexcept;
|
||||
[[nodiscard]] bool IsThread() const noexcept;
|
||||
[[nodiscard]] bool IsJoinedThread() const;
|
||||
[[nodiscard]] bool IsCategory() const noexcept;
|
||||
[[nodiscard]] bool IsText() const noexcept;
|
||||
[[nodiscard]] bool HasIcon() const noexcept;
|
||||
[[nodiscard]] std::string GetIconURL() const;
|
||||
[[nodiscard]] std::string GetDisplayName() const;
|
||||
[[nodiscard]] std::vector<Snowflake> GetChildIDs() const;
|
||||
[[nodiscard]] std::optional<PermissionOverwrite> GetOverwrite(Snowflake id) const;
|
||||
[[nodiscard]] std::vector<UserData> GetDMRecipients() const;
|
||||
};
|
||||
|
||||
24
src/discord/chatsubmitparams.hpp
Normal file
24
src/discord/chatsubmitparams.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <glibmm/ustring.h>
|
||||
#include <giomm/file.h>
|
||||
#include "discord/snowflake.hpp"
|
||||
|
||||
struct ChatSubmitParams {
|
||||
enum AttachmentType {
|
||||
PastedImage,
|
||||
ExtantFile,
|
||||
};
|
||||
|
||||
struct Attachment {
|
||||
Glib::RefPtr<Gio::File> File;
|
||||
AttachmentType Type;
|
||||
std::string Filename;
|
||||
};
|
||||
|
||||
Snowflake ChannelID;
|
||||
Snowflake InReplyToID;
|
||||
Glib::ustring Message;
|
||||
std::vector<Attachment> Attachments;
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
#include "websocket.hpp"
|
||||
#include "chatsubmitparams.hpp"
|
||||
#include "waiter.hpp"
|
||||
#include "httpclient.hpp"
|
||||
#include "objects.hpp"
|
||||
#include "store.hpp"
|
||||
#include "voiceclient.hpp"
|
||||
#include "websocket.hpp"
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <thread>
|
||||
@@ -17,31 +20,6 @@
|
||||
#undef GetMessage
|
||||
#endif
|
||||
|
||||
class HeartbeatWaiter {
|
||||
public:
|
||||
template<class R, class P>
|
||||
bool wait_for(std::chrono::duration<R, P> const &time) const {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
return !cv.wait_for(lock, time, [&] { return terminate; });
|
||||
}
|
||||
|
||||
void kill() {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
terminate = true;
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
void revive() {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
terminate = false;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::condition_variable cv;
|
||||
mutable std::mutex m;
|
||||
bool terminate = false;
|
||||
};
|
||||
|
||||
class Abaddon;
|
||||
class DiscordClient {
|
||||
friend class Abaddon;
|
||||
@@ -49,13 +27,12 @@ class DiscordClient {
|
||||
public:
|
||||
DiscordClient(bool mem_store = false);
|
||||
void Start();
|
||||
void Stop();
|
||||
bool Stop();
|
||||
bool IsStarted() const;
|
||||
bool IsStoreValid() const;
|
||||
|
||||
std::unordered_set<Snowflake> GetGuilds() const;
|
||||
const UserData &GetUserData() const;
|
||||
const UserSettings &GetUserSettings() const;
|
||||
std::vector<Snowflake> GetUserSortedGuilds() const;
|
||||
std::vector<Message> GetMessagesForChannel(Snowflake id, size_t limit = 50) const;
|
||||
std::vector<Message> GetMessagesBefore(Snowflake channel_id, Snowflake message_id, size_t limit = 50) const;
|
||||
@@ -63,8 +40,8 @@ public:
|
||||
|
||||
EPremiumType GetSelfPremiumType() const;
|
||||
|
||||
void FetchMessagesInChannel(Snowflake id, sigc::slot<void(const std::vector<Message> &)> cb);
|
||||
void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, sigc::slot<void(const std::vector<Message> &)> cb);
|
||||
void FetchMessagesInChannel(Snowflake id, const sigc::slot<void(const std::vector<Message> &)> &cb);
|
||||
void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, const sigc::slot<void(const std::vector<Message> &)> &cb);
|
||||
std::optional<Message> GetMessage(Snowflake id) const;
|
||||
std::optional<ChannelData> GetChannel(Snowflake id) const;
|
||||
std::optional<EmojiData> GetEmoji(Snowflake id) const;
|
||||
@@ -73,88 +50,103 @@ public:
|
||||
std::optional<RoleData> GetRole(Snowflake id) const;
|
||||
std::optional<GuildData> GetGuild(Snowflake id) const;
|
||||
std::optional<GuildMember> GetMember(Snowflake user_id, Snowflake guild_id) const;
|
||||
std::optional<BanData> GetBan(Snowflake guild_id, Snowflake user_id) const;
|
||||
Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const;
|
||||
std::optional<RoleData> GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const;
|
||||
std::set<Snowflake> GetUsersInGuild(Snowflake id) const;
|
||||
std::set<Snowflake> GetChannelsInGuild(Snowflake id) const;
|
||||
std::vector<Snowflake> GetUsersInThread(Snowflake id) const;
|
||||
std::vector<ChannelData> GetActiveThreads(Snowflake channel_id) const;
|
||||
void GetArchivedPublicThreads(Snowflake channel_id, sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> callback);
|
||||
void GetArchivedPrivateThreads(Snowflake channel_id, sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> callback);
|
||||
void GetArchivedPublicThreads(Snowflake channel_id, const sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> &callback);
|
||||
void GetArchivedPrivateThreads(Snowflake channel_id, const sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> &callback);
|
||||
std::vector<Snowflake> GetChildChannelIDs(Snowflake parent_id) const;
|
||||
|
||||
// get ids of given list of members for who we do not have the member data
|
||||
template<typename Iter>
|
||||
std::unordered_set<Snowflake> FilterUnknownMembersFrom(Snowflake guild_id, Iter begin, Iter end) {
|
||||
std::unordered_set<Snowflake> ret;
|
||||
const auto known = m_store.GetMembersInGuild(guild_id);
|
||||
for (auto iter = begin; iter != end; iter++)
|
||||
if (known.find(*iter) == known.end())
|
||||
ret.insert(*iter);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool IsThreadJoined(Snowflake thread_id) const;
|
||||
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
|
||||
|
||||
bool HasSelfChannelPermission(Snowflake channel_id, Permission perm) const;
|
||||
bool HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
|
||||
bool HasChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
|
||||
Permission ComputePermissions(Snowflake member_id, Snowflake guild_id) const;
|
||||
Permission ComputeOverwrites(Permission base, Snowflake member_id, Snowflake channel_id) const;
|
||||
bool CanManageMember(Snowflake guild_id, Snowflake actor, Snowflake target) const; // kick, ban, edit nickname (cant think of a better name)
|
||||
|
||||
void ChatMessageCallback(std::string nonce, const http::response_type &response);
|
||||
void ChatMessageCallback(const std::string &nonce, const http::response_type &response, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void SendChatMessageNoAttachments(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void SendChatMessageAttachments(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError code)> &callback);
|
||||
|
||||
void SendChatMessage(const std::string &content, Snowflake channel);
|
||||
void SendChatMessage(const std::string &content, Snowflake channel, Snowflake referenced_message);
|
||||
void SendChatMessage(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void DeleteMessage(Snowflake channel_id, Snowflake id);
|
||||
void EditMessage(Snowflake channel_id, Snowflake id, std::string content);
|
||||
void SendLazyLoad(Snowflake id);
|
||||
void SendThreadLazyLoad(Snowflake id);
|
||||
void JoinGuild(std::string code);
|
||||
void LeaveGuild(Snowflake id);
|
||||
void KickUser(Snowflake user_id, Snowflake guild_id);
|
||||
void BanUser(Snowflake user_id, Snowflake guild_id); // todo: reason, delete messages
|
||||
void UpdateStatus(PresenceStatus status, bool is_afk);
|
||||
void UpdateStatus(PresenceStatus status, bool is_afk, const ActivityData &obj);
|
||||
void CreateDM(Snowflake user_id);
|
||||
void CreateDM(Snowflake user_id, sigc::slot<void(DiscordError code, Snowflake channel_id)> callback);
|
||||
void CloseDM(Snowflake channel_id);
|
||||
std::optional<Snowflake> FindDM(Snowflake user_id); // wont find group dms
|
||||
void AddReaction(Snowflake id, Glib::ustring param);
|
||||
void RemoveReaction(Snowflake id, Glib::ustring param);
|
||||
void SetGuildName(Snowflake id, const Glib::ustring &name);
|
||||
void SetGuildName(Snowflake id, const Glib::ustring &name, sigc::slot<void(DiscordError code)> callback);
|
||||
void SetGuildIcon(Snowflake id, const std::string &data);
|
||||
void SetGuildIcon(Snowflake id, const std::string &data, sigc::slot<void(DiscordError code)> callback);
|
||||
void UnbanUser(Snowflake guild_id, Snowflake user_id);
|
||||
void UnbanUser(Snowflake guild_id, Snowflake user_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void DeleteInvite(const std::string &code);
|
||||
void DeleteInvite(const std::string &code, sigc::slot<void(DiscordError code)> callback);
|
||||
void SetGuildName(Snowflake id, const Glib::ustring &name, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void SetGuildIcon(Snowflake id, const std::string &data, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void UnbanUser(Snowflake guild_id, Snowflake user_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void DeleteInvite(const std::string &code, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void AddGroupDMRecipient(Snowflake channel_id, Snowflake user_id);
|
||||
void RemoveGroupDMRecipient(Snowflake channel_id, Snowflake user_id);
|
||||
void ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, Permission permissions, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, Gdk::RGBA color, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, sigc::slot<void(DiscordError code)> callback);
|
||||
void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::slot<void(DiscordError code)> callback);
|
||||
std::optional<GuildApplicationData> GetGuildApplication(Snowflake guild_id) const;
|
||||
void RemoveRelationship(Snowflake id, sigc::slot<void(DiscordError code)> callback);
|
||||
void SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot<void(DiscordError code)> callback);
|
||||
void PutRelationship(Snowflake id, sigc::slot<void(DiscordError code)> callback); // send fr by id, accept incoming
|
||||
void Pin(Snowflake channel_id, Snowflake message_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void Unpin(Snowflake channel_id, Snowflake message_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void LeaveThread(Snowflake channel_id, const std::string &location, sigc::slot<void(DiscordError code)> callback);
|
||||
void ArchiveThread(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void UnArchiveThread(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void MarkChannelAsRead(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void MarkGuildAsRead(Snowflake guild_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void MuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void UnmuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
|
||||
void MarkAllAsRead(sigc::slot<void(DiscordError code)> callback);
|
||||
void MuteGuild(Snowflake id, sigc::slot<void(DiscordError code)> callback);
|
||||
void UnmuteGuild(Snowflake id, sigc::slot<void(DiscordError code)> callback);
|
||||
void MuteThread(Snowflake id, sigc::slot<void(DiscordError code)> callback);
|
||||
void UnmuteThread(Snowflake id, sigc::slot<void(DiscordError code)> callback);
|
||||
void ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, Permission permissions, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, const Gdk::RGBA &color, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void RemoveRelationship(Snowflake id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void SendFriendRequest(const Glib::ustring &username, int discriminator, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void PutRelationship(Snowflake id, const sigc::slot<void(DiscordError code)> &callback); // send fr by id, accept incoming
|
||||
void Pin(Snowflake channel_id, Snowflake message_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void Unpin(Snowflake channel_id, Snowflake message_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void LeaveThread(Snowflake channel_id, const std::string &location, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void ArchiveThread(Snowflake channel_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void UnArchiveThread(Snowflake channel_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void MarkChannelAsRead(Snowflake channel_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void MarkGuildAsRead(Snowflake guild_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void MuteChannel(Snowflake channel_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void UnmuteChannel(Snowflake channel_id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void MuteGuild(Snowflake id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void UnmuteGuild(Snowflake id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void MuteThread(Snowflake id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void UnmuteThread(Snowflake id, const sigc::slot<void(DiscordError code)> &callback);
|
||||
|
||||
bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const;
|
||||
bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const;
|
||||
|
||||
// send op 8 to get member data for unknown members
|
||||
template<typename Iter>
|
||||
void RequestMembers(Snowflake guild_id, Iter begin, Iter end) {
|
||||
if (std::distance(begin, end) == 0) return;
|
||||
|
||||
RequestGuildMembersMessage obj;
|
||||
obj.GuildID = guild_id;
|
||||
obj.Presences = false;
|
||||
obj.UserIDs = { begin, end };
|
||||
m_websocket.Send(obj);
|
||||
}
|
||||
|
||||
// real client doesn't seem to use the single role endpoints so neither do we
|
||||
template<typename Iter>
|
||||
auto SetMemberRoles(Snowflake guild_id, Snowflake user_id, Iter begin, Iter end, sigc::slot<void(DiscordError code)> callback) {
|
||||
auto SetMemberRoles(Snowflake guild_id, Snowflake user_id, Iter begin, Iter end, const sigc::slot<void(DiscordError code)> &callback) {
|
||||
ModifyGuildMemberObject obj;
|
||||
obj.Roles = { begin, end };
|
||||
m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/members/" + std::to_string(user_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) {
|
||||
@@ -167,30 +159,49 @@ public:
|
||||
|
||||
// FetchGuildBans fetches all bans+reasons via api, this func fetches stored bans (so usually just GUILD_BAN_ADD data)
|
||||
std::vector<BanData> GetBansInGuild(Snowflake guild_id);
|
||||
void FetchGuildBan(Snowflake guild_id, Snowflake user_id, sigc::slot<void(BanData)> callback);
|
||||
void FetchGuildBans(Snowflake guild_id, sigc::slot<void(std::vector<BanData>)> callback);
|
||||
void FetchGuildBan(Snowflake guild_id, Snowflake user_id, const sigc::slot<void(BanData)> &callback);
|
||||
void FetchGuildBans(Snowflake guild_id, const sigc::slot<void(std::vector<BanData>)> &callback);
|
||||
|
||||
void FetchInvite(std::string code, sigc::slot<void(std::optional<InviteData>)> callback);
|
||||
void FetchGuildInvites(Snowflake guild_id, sigc::slot<void(std::vector<InviteData>)> callback);
|
||||
void FetchInvite(const std::string &code, const sigc::slot<void(std::optional<InviteData>)> &callback);
|
||||
void FetchGuildInvites(Snowflake guild_id, const sigc::slot<void(std::vector<InviteData>)> &callback);
|
||||
|
||||
void FetchAuditLog(Snowflake guild_id, sigc::slot<void(AuditLogData)> callback);
|
||||
void FetchAuditLog(Snowflake guild_id, const sigc::slot<void(AuditLogData)> &callback);
|
||||
|
||||
void FetchGuildEmojis(Snowflake guild_id, sigc::slot<void(std::vector<EmojiData>)> callback);
|
||||
void FetchGuildEmojis(Snowflake guild_id, const sigc::slot<void(std::vector<EmojiData>)> &callback);
|
||||
|
||||
void FetchUserProfile(Snowflake user_id, sigc::slot<void(UserProfileData)> callback);
|
||||
void FetchUserNote(Snowflake user_id, sigc::slot<void(std::string note)> callback);
|
||||
void SetUserNote(Snowflake user_id, std::string note);
|
||||
void SetUserNote(Snowflake user_id, std::string note, sigc::slot<void(DiscordError code)> callback);
|
||||
void FetchUserRelationships(Snowflake user_id, sigc::slot<void(std::vector<UserData>)> callback);
|
||||
void FetchUserProfile(Snowflake user_id, const sigc::slot<void(UserProfileData)> &callback);
|
||||
void FetchUserNote(Snowflake user_id, const sigc::slot<void(std::string note)> &callback);
|
||||
void SetUserNote(Snowflake user_id, std::string note, const sigc::slot<void(DiscordError code)> &callback);
|
||||
void FetchUserRelationships(Snowflake user_id, const sigc::slot<void(std::vector<UserData>)> &callback);
|
||||
|
||||
void FetchPinned(Snowflake id, sigc::slot<void(std::vector<Message>, DiscordError code)> callback);
|
||||
void FetchPinned(Snowflake id, const sigc::slot<void(std::vector<Message>, DiscordError code)> &callback);
|
||||
|
||||
bool IsVerificationRequired(Snowflake guild_id);
|
||||
void GetVerificationGateInfo(Snowflake guild_id, sigc::slot<void(std::optional<VerificationGateInfoObject>)> callback);
|
||||
void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, sigc::slot<void(DiscordError code)> callback);
|
||||
bool IsVerificationRequired(Snowflake guild_id) const;
|
||||
void GetVerificationGateInfo(Snowflake guild_id, const sigc::slot<void(std::optional<VerificationGateInfoObject>)> &callback);
|
||||
void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, const sigc::slot<void(DiscordError code)> &callback);
|
||||
|
||||
void UpdateToken(std::string token);
|
||||
void SetUserAgent(std::string agent);
|
||||
#ifdef WITH_VOICE
|
||||
void ConnectToVoice(Snowflake channel_id);
|
||||
void DisconnectFromVoice();
|
||||
// Is fully connected
|
||||
[[nodiscard]] bool IsVoiceConnected() const noexcept;
|
||||
[[nodiscard]] bool IsVoiceConnecting() const noexcept;
|
||||
[[nodiscard]] Snowflake GetVoiceChannelID() const noexcept;
|
||||
[[nodiscard]] std::unordered_set<Snowflake> GetUsersInVoiceChannel(Snowflake channel_id);
|
||||
[[nodiscard]] std::optional<uint32_t> GetSSRCOfUser(Snowflake id) const;
|
||||
[[nodiscard]] std::optional<Snowflake> GetVoiceState(Snowflake user_id) const;
|
||||
|
||||
void SetVoiceMuted(bool is_mute);
|
||||
void SetVoiceDeafened(bool is_deaf);
|
||||
#endif
|
||||
|
||||
void SetReferringChannel(Snowflake id);
|
||||
|
||||
void SetBuildNumber(uint32_t build_number);
|
||||
void SetCookie(std::string_view cookie);
|
||||
|
||||
void UpdateToken(const std::string &token);
|
||||
void SetUserAgent(const std::string &agent);
|
||||
|
||||
bool IsChannelMuted(Snowflake id) const noexcept;
|
||||
bool IsGuildMuted(Snowflake id) const noexcept;
|
||||
@@ -210,8 +221,8 @@ private:
|
||||
std::vector<uint8_t> m_decompress_buf;
|
||||
z_stream m_zstream;
|
||||
|
||||
std::string GetAPIURL();
|
||||
std::string GetGatewayURL();
|
||||
static std::string GetAPIURL();
|
||||
static std::string GetGatewayURL();
|
||||
|
||||
static DiscordError GetCodeFromResponse(const http::response_type &response);
|
||||
|
||||
@@ -262,28 +273,38 @@ private:
|
||||
void HandleGatewayThreadMemberListUpdate(const GatewayMessage &msg);
|
||||
void HandleGatewayMessageAck(const GatewayMessage &msg);
|
||||
void HandleGatewayUserGuildSettingsUpdate(const GatewayMessage &msg);
|
||||
void HandleGatewayGuildMembersChunk(const GatewayMessage &msg);
|
||||
void HandleGatewayReadySupplemental(const GatewayMessage &msg);
|
||||
void HandleGatewayReconnect(const GatewayMessage &msg);
|
||||
void HandleGatewayInvalidSession(const GatewayMessage &msg);
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void HandleGatewayVoiceStateUpdate(const GatewayMessage &msg);
|
||||
void HandleGatewayVoiceServerUpdate(const GatewayMessage &msg);
|
||||
#endif
|
||||
|
||||
void HeartbeatThread();
|
||||
void SendIdentify();
|
||||
void SendResume();
|
||||
|
||||
void HandleSocketOpen();
|
||||
void HandleSocketClose(uint16_t code);
|
||||
void SetHeaders();
|
||||
void SetSuperPropertiesFromIdentity(const IdentifyMessage &identity);
|
||||
|
||||
bool CheckCode(const http::response_type &r);
|
||||
bool CheckCode(const http::response_type &r, int expected);
|
||||
void HandleSocketOpen();
|
||||
void HandleSocketClose(const ix::WebSocketCloseInfo &info);
|
||||
|
||||
static bool CheckCode(const http::response_type &r);
|
||||
static bool CheckCode(const http::response_type &r, int expected);
|
||||
|
||||
void StoreMessageData(Message &msg);
|
||||
|
||||
void HandleReadyReadState(const ReadyEventData &data);
|
||||
void HandleReadyGuildSettings(const ReadyEventData &data);
|
||||
|
||||
void HandleUserGuildSettingsUpdateForDMs(const UserGuildSettingsUpdateData &data);
|
||||
|
||||
std::string m_token;
|
||||
|
||||
uint32_t m_build_number = 142000;
|
||||
|
||||
void AddUserToGuild(Snowflake user_id, Snowflake guild_id);
|
||||
std::map<Snowflake, std::set<Snowflake>> m_guild_to_users;
|
||||
std::map<Snowflake, std::set<Snowflake>> m_guild_to_channels;
|
||||
@@ -314,13 +335,33 @@ private:
|
||||
std::thread m_heartbeat_thread;
|
||||
std::atomic<int> m_last_sequence = -1;
|
||||
std::atomic<int> m_heartbeat_msec = 0;
|
||||
HeartbeatWaiter m_heartbeat_waiter;
|
||||
Waiter m_heartbeat_waiter;
|
||||
std::atomic<bool> m_heartbeat_acked = true;
|
||||
|
||||
bool m_reconnecting = false; // reconnecting either to resume or reidentify
|
||||
bool m_wants_resume = false; // reconnecting specifically to resume
|
||||
std::string m_session_id;
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
DiscordVoiceClient m_voice;
|
||||
|
||||
bool m_mute_requested = false;
|
||||
bool m_deaf_requested = false;
|
||||
|
||||
Snowflake m_voice_channel_id;
|
||||
// todo sql i guess
|
||||
std::unordered_map<Snowflake, Snowflake> m_voice_state_user_channel;
|
||||
std::unordered_map<Snowflake, std::unordered_set<Snowflake>> m_voice_state_channel_users;
|
||||
|
||||
void SendVoiceStateUpdate();
|
||||
|
||||
void SetVoiceState(Snowflake user_id, Snowflake channel_id);
|
||||
void ClearVoiceState(Snowflake user_id);
|
||||
|
||||
void OnVoiceConnected();
|
||||
void OnVoiceDisconnected();
|
||||
#endif
|
||||
|
||||
mutable std::mutex m_msg_mutex;
|
||||
Glib::Dispatcher m_msg_dispatch;
|
||||
std::queue<std::string> m_msg_queue;
|
||||
@@ -330,12 +371,15 @@ private:
|
||||
Glib::Dispatcher m_generic_dispatch;
|
||||
std::queue<std::function<void()>> m_generic_queue;
|
||||
|
||||
Glib::Timer m_progress_cb_timer;
|
||||
|
||||
std::set<Snowflake> m_channels_pinned_requested;
|
||||
std::set<Snowflake> m_channels_lazy_loaded;
|
||||
|
||||
// signals
|
||||
public:
|
||||
typedef sigc::signal<void> type_signal_gateway_ready;
|
||||
typedef sigc::signal<void> type_signal_gateway_ready_supplemental;
|
||||
typedef sigc::signal<void, Message> type_signal_message_create;
|
||||
typedef sigc::signal<void, Snowflake, Snowflake> type_signal_message_delete;
|
||||
typedef sigc::signal<void, Snowflake, Snowflake> type_signal_message_update;
|
||||
@@ -372,6 +416,7 @@ public:
|
||||
typedef sigc::signal<void, ThreadUpdateData> type_signal_thread_update;
|
||||
typedef sigc::signal<void, ThreadMemberListUpdateData> type_signal_thread_member_list_update;
|
||||
typedef sigc::signal<void, MessageAckData> type_signal_message_ack;
|
||||
typedef sigc::signal<void, GuildMembersChunkData> type_signal_guild_members_chunk;
|
||||
|
||||
// not discord dispatch events
|
||||
typedef sigc::signal<void, Snowflake> type_signal_added_to_thread;
|
||||
@@ -383,12 +428,26 @@ public:
|
||||
typedef sigc::signal<void, Snowflake> type_signal_channel_unmuted;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_guild_muted;
|
||||
typedef sigc::signal<void, Snowflake> type_signal_guild_unmuted;
|
||||
typedef sigc::signal<void, Snowflake, bool> type_signal_channel_accessibility_changed;
|
||||
|
||||
typedef sigc::signal<void, std::string /* nonce */, float /* retry_after */> type_signal_message_send_fail; // retry after param will be 0 if it failed for a reason that isnt slowmode
|
||||
typedef sigc::signal<void, bool, GatewayCloseCode> type_signal_disconnected; // bool true if reconnecting
|
||||
typedef sigc::signal<void> type_signal_connected;
|
||||
typedef sigc::signal<void, std::string, float> type_signal_message_progress;
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
using type_signal_voice_connected = sigc::signal<void()>;
|
||||
using type_signal_voice_disconnected = sigc::signal<void()>;
|
||||
using type_signal_voice_speaking = sigc::signal<void(VoiceSpeakingData)>;
|
||||
using type_signal_voice_user_disconnect = sigc::signal<void(Snowflake, Snowflake)>;
|
||||
using type_signal_voice_user_connect = sigc::signal<void(Snowflake, Snowflake)>;
|
||||
using type_signal_voice_requested_connect = sigc::signal<void(Snowflake)>;
|
||||
using type_signal_voice_requested_disconnect = sigc::signal<void()>;
|
||||
using type_signal_voice_client_state_update = sigc::signal<void(DiscordVoiceClient::State)>;
|
||||
#endif
|
||||
|
||||
type_signal_gateway_ready signal_gateway_ready();
|
||||
type_signal_gateway_ready_supplemental signal_gateway_ready_supplemental();
|
||||
type_signal_message_create signal_message_create();
|
||||
type_signal_message_delete signal_message_delete();
|
||||
type_signal_message_update signal_message_update();
|
||||
@@ -427,6 +486,7 @@ public:
|
||||
type_signal_thread_update signal_thread_update();
|
||||
type_signal_thread_member_list_update signal_thread_member_list_update();
|
||||
type_signal_message_ack signal_message_ack();
|
||||
type_signal_guild_members_chunk signal_guild_members_chunk();
|
||||
|
||||
type_signal_added_to_thread signal_added_to_thread();
|
||||
type_signal_removed_from_thread signal_removed_from_thread();
|
||||
@@ -435,12 +495,26 @@ public:
|
||||
type_signal_channel_unmuted signal_channel_unmuted();
|
||||
type_signal_guild_muted signal_guild_muted();
|
||||
type_signal_guild_unmuted signal_guild_unmuted();
|
||||
type_signal_channel_accessibility_changed signal_channel_accessibility_changed();
|
||||
type_signal_message_send_fail signal_message_send_fail();
|
||||
type_signal_disconnected signal_disconnected();
|
||||
type_signal_connected signal_connected();
|
||||
type_signal_message_progress signal_message_progress();
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
type_signal_voice_connected signal_voice_connected();
|
||||
type_signal_voice_disconnected signal_voice_disconnected();
|
||||
type_signal_voice_speaking signal_voice_speaking();
|
||||
type_signal_voice_user_disconnect signal_voice_user_disconnect();
|
||||
type_signal_voice_user_connect signal_voice_user_connect();
|
||||
type_signal_voice_requested_connect signal_voice_requested_connect();
|
||||
type_signal_voice_requested_disconnect signal_voice_requested_disconnect();
|
||||
type_signal_voice_client_state_update signal_voice_client_state_update();
|
||||
#endif
|
||||
|
||||
protected:
|
||||
type_signal_gateway_ready m_signal_gateway_ready;
|
||||
type_signal_gateway_ready_supplemental m_signal_gateway_ready_supplemental;
|
||||
type_signal_message_create m_signal_message_create;
|
||||
type_signal_message_delete m_signal_message_delete;
|
||||
type_signal_message_update m_signal_message_update;
|
||||
@@ -479,6 +553,7 @@ protected:
|
||||
type_signal_thread_update m_signal_thread_update;
|
||||
type_signal_thread_member_list_update m_signal_thread_member_list_update;
|
||||
type_signal_message_ack m_signal_message_ack;
|
||||
type_signal_guild_members_chunk m_signal_guild_members_chunk;
|
||||
|
||||
type_signal_removed_from_thread m_signal_removed_from_thread;
|
||||
type_signal_added_to_thread m_signal_added_to_thread;
|
||||
@@ -487,7 +562,20 @@ protected:
|
||||
type_signal_channel_unmuted m_signal_channel_unmuted;
|
||||
type_signal_guild_muted m_signal_guild_muted;
|
||||
type_signal_guild_unmuted m_signal_guild_unmuted;
|
||||
type_signal_channel_accessibility_changed m_signal_channel_accessibility_changed;
|
||||
type_signal_message_send_fail m_signal_message_send_fail;
|
||||
type_signal_disconnected m_signal_disconnected;
|
||||
type_signal_connected m_signal_connected;
|
||||
type_signal_message_progress m_signal_message_progress;
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
type_signal_voice_connected m_signal_voice_connected;
|
||||
type_signal_voice_disconnected m_signal_voice_disconnected;
|
||||
type_signal_voice_speaking m_signal_voice_speaking;
|
||||
type_signal_voice_user_disconnect m_signal_voice_user_disconnect;
|
||||
type_signal_voice_user_connect m_signal_voice_user_connect;
|
||||
type_signal_voice_requested_connect m_signal_voice_requested_connect;
|
||||
type_signal_voice_requested_disconnect m_signal_voice_requested_disconnect;
|
||||
type_signal_voice_client_state_update m_signal_voice_client_state_update;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -16,7 +16,7 @@ void to_json(nlohmann::json &j, const EmojiData &m) {
|
||||
j["id"] = m.ID;
|
||||
else
|
||||
j["id"] = nullptr;
|
||||
if (m.Name != "")
|
||||
if (!m.Name.empty())
|
||||
j["name"] = m.Name;
|
||||
else
|
||||
j["name"] = nullptr;
|
||||
|
||||
@@ -77,7 +77,7 @@ void GuildData::update_from_json(const nlohmann::json &j) {
|
||||
JS_RD("owner_id", OwnerID);
|
||||
std::string tmp;
|
||||
JS_RD("permissions", tmp);
|
||||
if (tmp != "")
|
||||
if (!tmp.empty())
|
||||
Permissions = std::stoull(tmp);
|
||||
JS_RD("region", VoiceRegion);
|
||||
JS_RD("afk_channel_id", AFKChannelID);
|
||||
@@ -119,7 +119,7 @@ void GuildData::update_from_json(const nlohmann::json &j) {
|
||||
JS_RD("approximate_presence_count", ApproximatePresenceCount);
|
||||
}
|
||||
|
||||
bool GuildData::HasFeature(const std::string &search_feature) {
|
||||
bool GuildData::HasFeature(const std::string &search_feature) const {
|
||||
if (!Features.has_value()) return false;
|
||||
for (const auto &feature : *Features)
|
||||
if (search_feature == feature)
|
||||
@@ -128,66 +128,17 @@ bool GuildData::HasFeature(const std::string &search_feature) {
|
||||
}
|
||||
|
||||
bool GuildData::HasIcon() const {
|
||||
return Icon != "";
|
||||
return !Icon.empty();
|
||||
}
|
||||
|
||||
bool GuildData::HasAnimatedIcon() const {
|
||||
return HasIcon() && Icon[0] == 'a' && Icon[1] == '_';
|
||||
}
|
||||
|
||||
std::string GuildData::GetIconURL(std::string ext, std::string size) const {
|
||||
std::string GuildData::GetIconURL(const std::string &ext, const std::string &size) const {
|
||||
return "https://cdn.discordapp.com/icons/" + std::to_string(ID) + "/" + Icon + "." + ext + "?size=" + size;
|
||||
}
|
||||
|
||||
std::vector<Snowflake> GuildData::GetSortedChannels(Snowflake ignore) const {
|
||||
std::vector<Snowflake> ret;
|
||||
|
||||
const auto &discord = Abaddon::Get().GetDiscordClient();
|
||||
auto channels = discord.GetChannelsInGuild(ID);
|
||||
|
||||
std::unordered_map<Snowflake, std::vector<ChannelData>> category_to_channels;
|
||||
std::map<int, std::vector<ChannelData>> position_to_categories;
|
||||
std::map<int, std::vector<ChannelData>> orphan_channels;
|
||||
for (const auto &channel_id : channels) {
|
||||
const auto data = discord.GetChannel(channel_id);
|
||||
if (!data->ParentID.has_value() && (data->Type == ChannelType::GUILD_TEXT || data->Type == ChannelType::GUILD_NEWS))
|
||||
orphan_channels[*data->Position].push_back(*data);
|
||||
else if (data->ParentID.has_value() && (data->Type == ChannelType::GUILD_TEXT || data->Type == ChannelType::GUILD_NEWS))
|
||||
category_to_channels[*data->ParentID].push_back(*data);
|
||||
else if (data->Type == ChannelType::GUILD_CATEGORY)
|
||||
position_to_categories[*data->Position].push_back(*data);
|
||||
}
|
||||
|
||||
for (auto &[pos, channels] : orphan_channels) {
|
||||
std::sort(channels.begin(), channels.end(), [&](const ChannelData &a, const ChannelData &b) -> bool {
|
||||
return a.ID < b.ID;
|
||||
});
|
||||
for (const auto &chan : channels)
|
||||
ret.push_back(chan.ID);
|
||||
}
|
||||
|
||||
for (auto &[pos, categories] : position_to_categories) {
|
||||
std::sort(categories.begin(), categories.end(), [&](const ChannelData &a, const ChannelData &b) -> bool {
|
||||
return a.ID < b.ID;
|
||||
});
|
||||
for (const auto &category : categories) {
|
||||
ret.push_back(category.ID);
|
||||
if (ignore == category.ID) continue; // stupid hack to save me some time
|
||||
auto it = category_to_channels.find(category.ID);
|
||||
if (it == category_to_channels.end()) continue;
|
||||
auto &channels = it->second;
|
||||
std::sort(channels.begin(), channels.end(), [&](const ChannelData &a, const ChannelData &b) -> bool {
|
||||
return a.Position < b.Position;
|
||||
});
|
||||
for (auto &channel : channels) {
|
||||
ret.push_back(channel.ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, GuildApplicationData &m) {
|
||||
JS_D("user_id", m.UserID);
|
||||
JS_D("guild_id", m.GuildID);
|
||||
|
||||
@@ -16,6 +16,13 @@ enum class GuildApplicationStatus {
|
||||
UNKNOWN,
|
||||
};
|
||||
|
||||
enum class GuildPremiumTier {
|
||||
NONE = 0,
|
||||
TIER_1 = 1,
|
||||
TIER_2 = 2,
|
||||
TIER_3 = 3,
|
||||
};
|
||||
|
||||
struct GuildApplicationData {
|
||||
Snowflake UserID;
|
||||
Snowflake GuildID;
|
||||
@@ -73,7 +80,7 @@ struct GuildData {
|
||||
std::optional<std::string> VanityURL; // null
|
||||
std::optional<std::string> Description; // null
|
||||
std::optional<std::string> BannerHash; // null
|
||||
std::optional<int> PremiumTier;
|
||||
std::optional<GuildPremiumTier> PremiumTier;
|
||||
std::optional<int> PremiumSubscriptionCount;
|
||||
std::optional<std::string> PreferredLocale;
|
||||
std::optional<Snowflake> PublicUpdatesChannelID; // null
|
||||
@@ -91,9 +98,8 @@ struct GuildData {
|
||||
friend void from_json(const nlohmann::json &j, GuildData &m);
|
||||
void update_from_json(const nlohmann::json &j);
|
||||
|
||||
bool HasFeature(const std::string &feature);
|
||||
bool HasFeature(const std::string &feature) const;
|
||||
bool HasIcon() const;
|
||||
bool HasAnimatedIcon() const;
|
||||
std::string GetIconURL(std::string ext = "png", std::string size = "32") const;
|
||||
std::vector<Snowflake> GetSortedChannels(Snowflake ignore = Snowflake::Invalid) const;
|
||||
std::string GetIconURL(const std::string &ext = "png", const std::string &size = "32") const;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "httpclient.hpp"
|
||||
|
||||
//#define USE_LOCAL_PROXY
|
||||
#include <utility>
|
||||
|
||||
HTTPClient::HTTPClient() {
|
||||
m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks));
|
||||
}
|
||||
@@ -10,23 +11,29 @@ void HTTPClient::SetBase(const std::string &url) {
|
||||
}
|
||||
|
||||
void HTTPClient::SetUserAgent(std::string agent) {
|
||||
m_agent = agent;
|
||||
m_agent = std::move(agent);
|
||||
}
|
||||
|
||||
void HTTPClient::SetAuth(std::string auth) {
|
||||
m_authorization = auth;
|
||||
m_authorization = std::move(auth);
|
||||
}
|
||||
|
||||
void HTTPClient::MakeDELETE(const std::string &path, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::SetPersistentHeader(std::string name, std::string value) {
|
||||
m_headers.insert_or_assign(std::move(name), std::move(value));
|
||||
}
|
||||
|
||||
void HTTPClient::SetCookie(std::string_view cookie) {
|
||||
m_cookie = cookie;
|
||||
}
|
||||
|
||||
void HTTPClient::MakeDELETE(const std::string &path, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("DELETE %s\n", path.c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, path, cb] {
|
||||
http::request req(http::REQUEST_DELETE, m_api_base + path);
|
||||
AddHeaders(req);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
|
||||
#ifdef USE_LOCAL_PROXY
|
||||
req.set_proxy("http://127.0.0.1:8888");
|
||||
req.set_verify_ssl(false);
|
||||
#endif
|
||||
req.set_header("Origin", "https://discord.com");
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
|
||||
auto res = req.execute();
|
||||
|
||||
@@ -34,18 +41,16 @@ void HTTPClient::MakeDELETE(const std::string &path, std::function<void(http::re
|
||||
}));
|
||||
}
|
||||
|
||||
void HTTPClient::MakePATCH(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::MakePATCH(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("PATCH %s\n", path.c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
|
||||
http::request req(http::REQUEST_PATCH, m_api_base + path);
|
||||
AddHeaders(req);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
req.set_header("Content-Type", "application/json");
|
||||
req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
|
||||
req.set_header("Origin", "https://discord.com");
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
req.set_body(payload);
|
||||
#ifdef USE_LOCAL_PROXY
|
||||
req.set_proxy("http://127.0.0.1:8888");
|
||||
req.set_verify_ssl(false);
|
||||
#endif
|
||||
|
||||
auto res = req.execute();
|
||||
|
||||
@@ -53,18 +58,16 @@ void HTTPClient::MakePATCH(const std::string &path, const std::string &payload,
|
||||
}));
|
||||
}
|
||||
|
||||
void HTTPClient::MakePOST(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::MakePOST(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("POST %s\n", path.c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
|
||||
http::request req(http::REQUEST_POST, m_api_base + path);
|
||||
AddHeaders(req);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
req.set_header("Content-Type", "application/json");
|
||||
req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
|
||||
req.set_header("Origin", "https://discord.com");
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
req.set_body(payload);
|
||||
#ifdef USE_LOCAL_PROXY
|
||||
req.set_proxy("http://127.0.0.1:8888");
|
||||
req.set_verify_ssl(false);
|
||||
#endif
|
||||
|
||||
auto res = req.execute();
|
||||
|
||||
@@ -72,19 +75,17 @@ void HTTPClient::MakePOST(const std::string &path, const std::string &payload, s
|
||||
}));
|
||||
}
|
||||
|
||||
void HTTPClient::MakePUT(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::MakePUT(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("PUT %s\n", path.c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] {
|
||||
http::request req(http::REQUEST_PUT, m_api_base + path);
|
||||
AddHeaders(req);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
if (payload != "")
|
||||
req.set_header("Origin", "https://discord.com");
|
||||
if (!payload.empty())
|
||||
req.set_header("Content-Type", "application/json");
|
||||
req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
req.set_body(payload);
|
||||
#ifdef USE_LOCAL_PROXY
|
||||
req.set_proxy("http://127.0.0.1:8888");
|
||||
req.set_verify_ssl(false);
|
||||
#endif
|
||||
|
||||
auto res = req.execute();
|
||||
|
||||
@@ -92,17 +93,13 @@ void HTTPClient::MakePUT(const std::string &path, const std::string &payload, st
|
||||
}));
|
||||
}
|
||||
|
||||
void HTTPClient::MakeGET(const std::string &path, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::MakeGET(const std::string &path, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("GET %s\n", path.c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, path, cb] {
|
||||
http::request req(http::REQUEST_GET, m_api_base + path);
|
||||
AddHeaders(req);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
req.set_header("Content-Type", "application/json");
|
||||
req.set_user_agent(m_agent != "" ? m_agent : "Abaddon");
|
||||
#ifdef USE_LOCAL_PROXY
|
||||
req.set_proxy("http://127.0.0.1:8888");
|
||||
req.set_verify_ssl(false);
|
||||
#endif
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
|
||||
auto res = req.execute();
|
||||
|
||||
@@ -110,6 +107,21 @@ void HTTPClient::MakeGET(const std::string &path, std::function<void(http::respo
|
||||
}));
|
||||
}
|
||||
|
||||
http::request HTTPClient::CreateRequest(http::EMethod method, std::string path) {
|
||||
http::request req(method, m_api_base + path);
|
||||
req.set_header("Authorization", m_authorization);
|
||||
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
|
||||
return req;
|
||||
}
|
||||
|
||||
void HTTPClient::Execute(http::request &&req, const std::function<void(http::response_type r)> &cb) {
|
||||
printf("%s %s\n", req.get_method(), req.get_url().c_str());
|
||||
m_futures.push_back(std::async(std::launch::async, [this, cb, req = std::move(req)]() mutable {
|
||||
auto res = req.execute();
|
||||
OnResponse(res, cb);
|
||||
}));
|
||||
}
|
||||
|
||||
void HTTPClient::CleanupFutures() {
|
||||
for (auto it = m_futures.begin(); it != m_futures.end();) {
|
||||
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
||||
@@ -126,11 +138,19 @@ void HTTPClient::RunCallbacks() {
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
void HTTPClient::OnResponse(const http::response_type &r, std::function<void(http::response_type r)> cb) {
|
||||
void HTTPClient::AddHeaders(http::request &r) {
|
||||
for (const auto &[name, val] : m_headers) {
|
||||
r.set_header(name, val);
|
||||
}
|
||||
curl_easy_setopt(r.get_curl(), CURLOPT_COOKIE, m_cookie.c_str());
|
||||
curl_easy_setopt(r.get_curl(), CURLOPT_ACCEPT_ENCODING, "");
|
||||
}
|
||||
|
||||
void HTTPClient::OnResponse(const http::response_type &r, const std::function<void(http::response_type r)> &cb) {
|
||||
CleanupFutures();
|
||||
try {
|
||||
m_mutex.lock();
|
||||
m_queue.push([this, r, cb] { cb(r); });
|
||||
m_queue.push([r, cb] { cb(r); });
|
||||
m_dispatcher.emit();
|
||||
m_mutex.unlock();
|
||||
} catch (const std::exception &e) {
|
||||
|
||||
@@ -17,14 +17,22 @@ public:
|
||||
|
||||
void SetUserAgent(std::string agent);
|
||||
void SetAuth(std::string auth);
|
||||
void MakeDELETE(const std::string &path, std::function<void(http::response_type r)> cb);
|
||||
void MakeGET(const std::string &path, std::function<void(http::response_type r)> cb);
|
||||
void MakePATCH(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb);
|
||||
void MakePOST(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb);
|
||||
void MakePUT(const std::string &path, const std::string &payload, std::function<void(http::response_type r)> cb);
|
||||
void SetPersistentHeader(std::string name, std::string value);
|
||||
void SetCookie(std::string_view cookie);
|
||||
|
||||
void MakeDELETE(const std::string &path, const std::function<void(http::response_type r)> &cb);
|
||||
void MakeGET(const std::string &path, const std::function<void(http::response_type r)> &cb);
|
||||
void MakePATCH(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
|
||||
void MakePOST(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
|
||||
void MakePUT(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
|
||||
|
||||
[[nodiscard]] http::request CreateRequest(http::EMethod method, std::string path);
|
||||
void Execute(http::request &&req, const std::function<void(http::response_type r)> &cb);
|
||||
|
||||
private:
|
||||
void OnResponse(const http::response_type &r, std::function<void(http::response_type r)> cb);
|
||||
void AddHeaders(http::request &r);
|
||||
|
||||
void OnResponse(const http::response_type &r, const std::function<void(http::response_type r)> &cb);
|
||||
void CleanupFutures();
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
@@ -36,4 +44,6 @@ private:
|
||||
std::string m_api_base;
|
||||
std::string m_authorization;
|
||||
std::string m_agent;
|
||||
std::unordered_map<std::string, std::string> m_headers;
|
||||
std::string m_cookie;
|
||||
};
|
||||
|
||||
@@ -14,10 +14,10 @@ enum class InteractionType {
|
||||
};
|
||||
|
||||
struct MessageInteractionData {
|
||||
Snowflake ID; // id of the interaction
|
||||
InteractionType Type; // the type of interaction
|
||||
std::string Name; // the name of the ApplicationCommand
|
||||
UserData User; // the user who invoked the interaction
|
||||
Snowflake ID; // id of the interaction
|
||||
InteractionType Type; // the type of interaction
|
||||
std::string Name; // the name of the ApplicationCommand
|
||||
UserData User; // the user who invoked the interaction
|
||||
// undocumented???
|
||||
std::optional<GuildMember> Member; // the member who invoked the interaction (in a guild)
|
||||
|
||||
|
||||
@@ -141,8 +141,8 @@ inline void json_update_optional_nullable_default(const ::nlohmann::json &j, con
|
||||
} while (0)
|
||||
|
||||
// set a json value from a std::optional only if it has a value
|
||||
#define JS_IF(k, v) \
|
||||
do { \
|
||||
if (v.has_value()) \
|
||||
j[k] = *v; \
|
||||
#define JS_IF(k, v) \
|
||||
do { \
|
||||
if ((v).has_value()) \
|
||||
j[k] = *(v); \
|
||||
} while (0)
|
||||
|
||||
@@ -19,7 +19,7 @@ std::vector<RoleData> GuildMember::GetSortedRoles() const {
|
||||
for (const auto role_id : Roles) {
|
||||
const auto role = Abaddon::Get().GetDiscordClient().GetRole(role_id);
|
||||
if (!role.has_value()) continue;
|
||||
roles.push_back(std::move(*role));
|
||||
roles.push_back(*role);
|
||||
}
|
||||
|
||||
std::sort(roles.begin(), roles.end(), [](const RoleData &a, const RoleData &b) {
|
||||
|
||||
@@ -20,7 +20,7 @@ struct GuildMember {
|
||||
// undocuemtned moment !!!1
|
||||
std::optional<std::string> Avatar;
|
||||
|
||||
std::vector<RoleData> GetSortedRoles() const;
|
||||
[[nodiscard]] std::vector<RoleData> GetSortedRoles() const;
|
||||
|
||||
void update_from_json(const nlohmann::json &j);
|
||||
friend void from_json(const nlohmann::json &j, GuildMember &m);
|
||||
|
||||
@@ -176,7 +176,7 @@ void to_json(nlohmann::json &j, const MessageApplicationData &m) {
|
||||
j["id"] = m.ID;
|
||||
JS_IF("cover_image", m.CoverImage);
|
||||
j["description"] = m.Description;
|
||||
if (m.Icon == "")
|
||||
if (m.Icon.empty())
|
||||
j["icon"] = nullptr;
|
||||
else
|
||||
j["icon"] = m.Icon;
|
||||
@@ -230,7 +230,7 @@ void Message::from_json_edited(const nlohmann::json &j) {
|
||||
JS_O("content", Content);
|
||||
JS_O("timestamp", Timestamp);
|
||||
JS_ON("edited_timestamp", EditedTimestamp);
|
||||
if (EditedTimestamp.size() > 0)
|
||||
if (!EditedTimestamp.empty())
|
||||
SetEdited();
|
||||
JS_O("tts", IsTTS);
|
||||
JS_O("mention_everyone", DoesMentionEveryone);
|
||||
|
||||
@@ -209,10 +209,10 @@ struct Message {
|
||||
|
||||
void SetDeleted();
|
||||
void SetEdited();
|
||||
bool IsDeleted() const;
|
||||
bool IsEdited() const;
|
||||
[[nodiscard]] bool IsDeleted() const;
|
||||
[[nodiscard]] bool IsEdited() const;
|
||||
|
||||
bool DoesMention(Snowflake id) const noexcept;
|
||||
[[nodiscard]] bool DoesMention(Snowflake id) const noexcept;
|
||||
|
||||
private:
|
||||
bool m_deleted = false;
|
||||
|
||||
@@ -41,7 +41,7 @@ void from_json(const nlohmann::json &j, GuildMemberListUpdateMessage::MemberItem
|
||||
JS_D("mute", m.IsMuted);
|
||||
JS_D("joined_at", m.JoinedAt);
|
||||
JS_D("deaf", m.IsDefeaned);
|
||||
JS_N("hoisted_role", m.HoistedRole);
|
||||
JS_ON("hoisted_role", m.HoistedRole);
|
||||
JS_ON("premium_since", m.PremiumSince);
|
||||
JS_ON("nick", m.Nickname);
|
||||
JS_ON("presence", m.Presence);
|
||||
@@ -77,7 +77,7 @@ void from_json(const nlohmann::json &j, GuildMemberListUpdateMessage &m) {
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const LazyLoadRequestMessage &m) {
|
||||
j["op"] = GatewayOp::LazyLoadRequest;
|
||||
j["op"] = GatewayOp::GuildSubscriptions;
|
||||
j["d"] = nlohmann::json::object();
|
||||
j["d"]["guild_id"] = m.GuildID;
|
||||
if (m.Channels.has_value()) {
|
||||
@@ -98,7 +98,7 @@ void to_json(nlohmann::json &j, const LazyLoadRequestMessage &m) {
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const UpdateStatusMessage &m) {
|
||||
j["op"] = GatewayOp::UpdateStatus;
|
||||
j["op"] = GatewayOp::PresenceUpdate;
|
||||
j["d"] = nlohmann::json::object();
|
||||
j["d"]["since"] = m.Since;
|
||||
j["d"]["activities"] = m.Activities;
|
||||
@@ -119,6 +119,14 @@ void to_json(nlohmann::json &j, const UpdateStatusMessage &m) {
|
||||
}
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const RequestGuildMembersMessage &m) {
|
||||
j["op"] = GatewayOp::RequestGuildMembers;
|
||||
j["d"] = nlohmann::json::object();
|
||||
j["d"]["guild_id"] = m.GuildID;
|
||||
j["d"]["presences"] = m.Presences;
|
||||
j["d"]["user_ids"] = m.UserIDs;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, ReadStateEntry &m) {
|
||||
JS_ON("mention_count", m.MentionCount);
|
||||
JS_ON("last_message_id", m.LastMessageID);
|
||||
@@ -225,8 +233,14 @@ void from_json(const nlohmann::json &j, SupplementalMergedPresencesData &m) {
|
||||
JS_D("friends", m.Friends);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, SupplementalGuildEntry &m) {
|
||||
JS_D("id", m.ID);
|
||||
JS_D("voice_states", m.VoiceStates);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, ReadySupplementalData &m) {
|
||||
JS_D("merged_presences", m.MergedPresences);
|
||||
JS_D("guilds", m.Guilds);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const IdentifyProperties &m) {
|
||||
@@ -243,7 +257,7 @@ void to_json(nlohmann::json &j, const IdentifyProperties &m) {
|
||||
j["referring_domain_current"] = m.ReferringDomainCurrent;
|
||||
j["release_channel"] = m.ReleaseChannel;
|
||||
j["client_build_number"] = m.ClientBuildNumber;
|
||||
if (m.ClientEventSource == "")
|
||||
if (m.ClientEventSource.empty())
|
||||
j["client_event_source"] = nullptr;
|
||||
else
|
||||
j["client_event_source"] = m.ClientEventSource;
|
||||
@@ -254,6 +268,7 @@ void to_json(nlohmann::json &j, const ClientStateProperties &m) {
|
||||
j["highest_last_message_id"] = m.HighestLastMessageID;
|
||||
j["read_state_version"] = m.ReadStateVersion;
|
||||
j["user_guild_settings_version"] = m.UserGuildSettingsVersion;
|
||||
j["user_settings_version"] = m.UserSettingsVersion;
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const IdentifyMessage &m) {
|
||||
@@ -282,7 +297,7 @@ void to_json(nlohmann::json &j, const CreateMessageObject &m) {
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const MessageEditObject &m) {
|
||||
if (m.Content.size() > 0)
|
||||
if (!m.Content.empty())
|
||||
j["content"] = m.Content;
|
||||
|
||||
// todo EmbedData to_json
|
||||
@@ -626,3 +641,48 @@ void to_json(nlohmann::json &j, const AckBulkData &m) {
|
||||
void from_json(const nlohmann::json &j, UserGuildSettingsUpdateData &m) {
|
||||
m.Settings = j;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, GuildMembersChunkData &m) {
|
||||
JS_D("members", m.Members);
|
||||
JS_D("guild_id", m.GuildID);
|
||||
}
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
void to_json(nlohmann::json &j, const VoiceStateUpdateMessage &m) {
|
||||
j["op"] = GatewayOp::VoiceStateUpdate;
|
||||
if (m.GuildID.has_value())
|
||||
j["d"]["guild_id"] = *m.GuildID;
|
||||
else
|
||||
j["d"]["guild_id"] = nullptr;
|
||||
if (m.ChannelID.has_value())
|
||||
j["d"]["channel_id"] = *m.ChannelID;
|
||||
else
|
||||
j["d"]["channel_id"] = nullptr;
|
||||
j["d"]["self_mute"] = m.SelfMute;
|
||||
j["d"]["self_deaf"] = m.SelfDeaf;
|
||||
j["d"]["self_video"] = m.SelfVideo;
|
||||
// j["d"]["preferred_region"] = m.PreferredRegion;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceServerUpdateData &m) {
|
||||
JS_D("token", m.Token);
|
||||
JS_D("endpoint", m.Endpoint);
|
||||
JS_ON("guild_id", m.GuildID);
|
||||
JS_ON("channel_id", m.ChannelID);
|
||||
}
|
||||
#endif
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceState &m) {
|
||||
JS_ON("guild_id", m.GuildID);
|
||||
JS_N("channel_id", m.ChannelID);
|
||||
JS_D("deaf", m.IsDeafened);
|
||||
JS_D("mute", m.IsMuted);
|
||||
JS_D("self_deaf", m.IsSelfDeafened);
|
||||
JS_D("self_mute", m.IsSelfMuted);
|
||||
JS_D("self_video", m.IsSelfVideo);
|
||||
JS_O("self_stream", m.IsSelfStream);
|
||||
JS_D("suppress", m.IsSuppressed);
|
||||
JS_D("user_id", m.UserID);
|
||||
JS_ON("member", m.Member);
|
||||
JS_D("session_id", m.SessionID);
|
||||
}
|
||||
|
||||
@@ -24,16 +24,35 @@
|
||||
// most stuff below should just be objects that get processed and thrown away immediately
|
||||
|
||||
enum class GatewayOp : int {
|
||||
Event = 0,
|
||||
Dispatch = 0,
|
||||
Heartbeat = 1,
|
||||
Identify = 2,
|
||||
UpdateStatus = 3,
|
||||
PresenceUpdate = 3,
|
||||
VoiceStateUpdate = 4,
|
||||
VoiceServerPing = 5,
|
||||
Resume = 6,
|
||||
Reconnect = 7,
|
||||
RequestGuildMembers = 8,
|
||||
InvalidSession = 9,
|
||||
Hello = 10,
|
||||
HeartbeatAck = 11,
|
||||
LazyLoadRequest = 14,
|
||||
// 12 unused
|
||||
CallConnect = 13,
|
||||
GuildSubscriptions = 14,
|
||||
LobbyConnect = 15,
|
||||
LobbyDisconnect = 16,
|
||||
LobbyVoiceStatesUpdate = 17,
|
||||
StreamCreate = 18,
|
||||
StreamDelete = 19,
|
||||
StreamWatch = 20,
|
||||
StreamPing = 21,
|
||||
StreamSetPaused = 22,
|
||||
// 23 unused
|
||||
RequestGuildApplicationCommands = 24,
|
||||
EmbeddedActivityLaunch = 25,
|
||||
EmbeddedActivityClose = 26,
|
||||
EmbeddedActivityUpdate = 27,
|
||||
RequestForumUnreads = 28,
|
||||
};
|
||||
|
||||
enum class GatewayEvent : int {
|
||||
@@ -80,6 +99,9 @@ enum class GatewayEvent : int {
|
||||
THREAD_MEMBER_LIST_UPDATE,
|
||||
MESSAGE_ACK,
|
||||
USER_GUILD_SETTINGS_UPDATE,
|
||||
GUILD_MEMBERS_CHUNK,
|
||||
VOICE_STATE_UPDATE,
|
||||
VOICE_SERVER_UPDATE,
|
||||
};
|
||||
|
||||
enum class GatewayCloseCode : uint16_t {
|
||||
@@ -177,7 +199,7 @@ struct GuildMemberListUpdateMessage {
|
||||
std::string HoistedRole; // null
|
||||
bool IsDefeaned;
|
||||
|
||||
GuildMember GetAsMemberData() const;
|
||||
[[nodiscard]] GuildMember GetAsMemberData() const;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, MemberItem &m);
|
||||
|
||||
@@ -226,6 +248,14 @@ struct UpdateStatusMessage {
|
||||
friend void to_json(nlohmann::json &j, const UpdateStatusMessage &m);
|
||||
};
|
||||
|
||||
struct RequestGuildMembersMessage {
|
||||
Snowflake GuildID;
|
||||
bool Presences;
|
||||
std::vector<Snowflake> UserIDs;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const RequestGuildMembersMessage &m);
|
||||
};
|
||||
|
||||
struct ReadStateEntry {
|
||||
int MentionCount;
|
||||
Snowflake LastMessageID;
|
||||
@@ -324,8 +354,18 @@ struct SupplementalMergedPresencesData {
|
||||
friend void from_json(const nlohmann::json &j, SupplementalMergedPresencesData &m);
|
||||
};
|
||||
|
||||
struct VoiceState;
|
||||
struct SupplementalGuildEntry {
|
||||
// std::vector<?> EmbeddedActivities;
|
||||
Snowflake ID;
|
||||
std::vector<VoiceState> VoiceStates;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, SupplementalGuildEntry &m);
|
||||
};
|
||||
|
||||
struct ReadySupplementalData {
|
||||
SupplementalMergedPresencesData MergedPresences;
|
||||
std::vector<SupplementalGuildEntry> Guilds;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, ReadySupplementalData &m);
|
||||
};
|
||||
@@ -354,6 +394,7 @@ struct ClientStateProperties {
|
||||
std::string HighestLastMessageID = "0";
|
||||
int ReadStateVersion = 0;
|
||||
int UserGuildSettingsVersion = -1;
|
||||
int UserSettingsVersion = -1;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const ClientStateProperties &m);
|
||||
};
|
||||
@@ -822,3 +863,55 @@ struct UserGuildSettingsUpdateData {
|
||||
|
||||
friend void from_json(const nlohmann::json &j, UserGuildSettingsUpdateData &m);
|
||||
};
|
||||
|
||||
struct GuildMembersChunkData {
|
||||
/*
|
||||
not needed so not deserialized
|
||||
int ChunkCount;
|
||||
int ChunkIndex;
|
||||
std::vector<?> NotFound;
|
||||
*/
|
||||
Snowflake GuildID;
|
||||
std::vector<GuildMember> Members;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, GuildMembersChunkData &m);
|
||||
};
|
||||
|
||||
#ifdef WITH_VOICE
|
||||
struct VoiceStateUpdateMessage {
|
||||
std::optional<Snowflake> GuildID;
|
||||
std::optional<Snowflake> ChannelID;
|
||||
bool SelfMute = false;
|
||||
bool SelfDeaf = false;
|
||||
bool SelfVideo = false;
|
||||
std::string PreferredRegion;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const VoiceStateUpdateMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceServerUpdateData {
|
||||
std::string Token;
|
||||
std::string Endpoint;
|
||||
std::optional<Snowflake> GuildID;
|
||||
std::optional<Snowflake> ChannelID;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceServerUpdateData &m);
|
||||
};
|
||||
#endif
|
||||
|
||||
struct VoiceState {
|
||||
std::optional<Snowflake> ChannelID;
|
||||
bool IsDeafened;
|
||||
bool IsMuted;
|
||||
std::optional<Snowflake> GuildID;
|
||||
std::optional<GuildMember> Member;
|
||||
bool IsSelfDeafened;
|
||||
bool IsSelfMuted;
|
||||
bool IsSelfVideo;
|
||||
bool IsSelfStream = false;
|
||||
std::string SessionID;
|
||||
bool IsSuppressed;
|
||||
Snowflake UserID;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceState &m);
|
||||
};
|
||||
|
||||
@@ -3,19 +3,19 @@
|
||||
#include "user.hpp"
|
||||
|
||||
enum class RelationshipType {
|
||||
None = 0,
|
||||
Friend = 1,
|
||||
Blocked = 2,
|
||||
PendingIncoming = 3,
|
||||
PendingOutgoing = 4,
|
||||
Implicit = 5,
|
||||
None = 0,
|
||||
Friend = 1,
|
||||
Blocked = 2,
|
||||
PendingIncoming = 3,
|
||||
PendingOutgoing = 4,
|
||||
Implicit = 5,
|
||||
};
|
||||
|
||||
struct RelationshipData {
|
||||
// Snowflake UserID; this is the same as ID apparently but it looks new so i wont touch it
|
||||
RelationshipType Type;
|
||||
Snowflake ID;
|
||||
// Unknown Nickname; // null
|
||||
// Unknown Nickname; // null
|
||||
|
||||
friend void from_json(const nlohmann::json &j, RelationshipData &m);
|
||||
friend void from_json(const nlohmann::json &j, RelationshipData &m);
|
||||
};
|
||||
|
||||
@@ -16,8 +16,8 @@ struct RoleData {
|
||||
bool IsManaged;
|
||||
bool IsMentionable;
|
||||
|
||||
bool HasColor() const noexcept;
|
||||
Glib::ustring GetEscapedName() const;
|
||||
[[nodiscard]] bool HasColor() const noexcept;
|
||||
[[nodiscard]] Glib::ustring GetEscapedName() const;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, RoleData &m);
|
||||
};
|
||||
|
||||
@@ -25,7 +25,7 @@ Snowflake::Snowflake(const Glib::ustring &str) {
|
||||
m_num = std::strtoull(str.c_str(), nullptr, 10);
|
||||
else
|
||||
m_num = Invalid;
|
||||
};
|
||||
}
|
||||
|
||||
Snowflake Snowflake::FromNow() {
|
||||
using namespace std::chrono;
|
||||
@@ -53,14 +53,12 @@ bool Snowflake::IsValid() const {
|
||||
return m_num != Invalid;
|
||||
}
|
||||
|
||||
std::string Snowflake::GetLocalTimestamp() const {
|
||||
Glib::ustring Snowflake::GetLocalTimestamp() const {
|
||||
const time_t secs_since_epoch = (m_num / SecondsInterval) + DiscordEpochSeconds;
|
||||
const std::tm tm = *localtime(&secs_since_epoch);
|
||||
std::stringstream ss;
|
||||
const static std::locale locale("");
|
||||
ss.imbue(locale);
|
||||
ss << std::put_time(&tm, "%X %x");
|
||||
return ss.str();
|
||||
std::array<char, 256> tmp {};
|
||||
std::strftime(tmp.data(), sizeof(tmp), "%X %x", &tm);
|
||||
return tmp.data();
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, Snowflake &s) {
|
||||
|
||||
@@ -12,8 +12,8 @@ struct Snowflake {
|
||||
static Snowflake FromNow(); // not thread safe
|
||||
static Snowflake FromISO8601(std::string_view ts);
|
||||
|
||||
bool IsValid() const;
|
||||
std::string GetLocalTimestamp() const;
|
||||
[[nodiscard]] bool IsValid() const;
|
||||
[[nodiscard]] Glib::ustring GetLocalTimestamp() const;
|
||||
|
||||
bool operator==(const Snowflake &s) const noexcept {
|
||||
return m_num == s.m_num;
|
||||
|
||||
@@ -22,15 +22,6 @@ void from_json(const nlohmann::json &j, StickerData &m) {
|
||||
JS_D("format_type", m.FormatType);
|
||||
}
|
||||
|
||||
std::string StickerData::GetURL() const {
|
||||
if (!AssetHash.has_value()) return "";
|
||||
if (FormatType == StickerFormatType::PNG || FormatType == StickerFormatType::APNG)
|
||||
return "https://media.discordapp.net/stickers/" + std::to_string(ID) + "/" + *AssetHash + ".png?size=256";
|
||||
else if (FormatType == StickerFormatType::LOTTIE)
|
||||
return "https://media.discordapp.net/stickers/" + std::to_string(ID) + "/" + *AssetHash + ".json";
|
||||
return "";
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const StickerItem &m) {
|
||||
j["id"] = m.ID;
|
||||
j["name"] = m.Name;
|
||||
|
||||
@@ -24,8 +24,6 @@ struct StickerData {
|
||||
|
||||
friend void to_json(nlohmann::json &j, const StickerData &m);
|
||||
friend void from_json(const nlohmann::json &j, StickerData &m);
|
||||
|
||||
std::string GetURL() const;
|
||||
};
|
||||
|
||||
struct StickerItem {
|
||||
@@ -36,5 +34,5 @@ struct StickerItem {
|
||||
friend void to_json(nlohmann::json &j, const StickerItem &m);
|
||||
friend void from_json(const nlohmann::json &j, StickerItem &m);
|
||||
|
||||
std::string GetURL() const;
|
||||
[[nodiscard]] std::string GetURL() const;
|
||||
};
|
||||
|
||||
@@ -253,6 +253,14 @@ void Store::SetGuildMember(Snowflake guild_id, Snowflake user_id, const GuildMem
|
||||
|
||||
s->Reset();
|
||||
|
||||
{
|
||||
auto &s = m_stmt_clr_member_roles;
|
||||
s->Bind(1, user_id);
|
||||
s->Bind(2, guild_id);
|
||||
s->Step();
|
||||
s->Reset();
|
||||
}
|
||||
|
||||
{
|
||||
auto &s = m_stmt_set_member_roles;
|
||||
|
||||
@@ -576,6 +584,23 @@ std::vector<Snowflake> Store::GetChannelIDsWithParentID(Snowflake channel_id) co
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unordered_set<Snowflake> Store::GetMembersInGuild(Snowflake guild_id) const {
|
||||
auto &s = m_stmt_get_guild_member_ids;
|
||||
|
||||
s->Bind(1, guild_id);
|
||||
|
||||
std::unordered_set<Snowflake> ret;
|
||||
while (s->FetchOne()) {
|
||||
Snowflake x;
|
||||
s->Get(0, x);
|
||||
ret.insert(x);
|
||||
}
|
||||
|
||||
s->Reset();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Store::AddReaction(const MessageReactionAddObject &data, bool byself) {
|
||||
auto &s = m_stmt_add_reaction;
|
||||
|
||||
@@ -721,6 +746,7 @@ std::optional<GuildData> Store::GetGuild(Snowflake id) const {
|
||||
s->Get(2, r.Icon);
|
||||
s->Get(5, r.OwnerID);
|
||||
s->Get(20, r.IsUnavailable);
|
||||
s->Get(27, r.PremiumTier);
|
||||
|
||||
s->Reset();
|
||||
|
||||
@@ -801,8 +827,8 @@ std::optional<GuildMember> Store::GetGuildMember(Snowflake guild_id, Snowflake u
|
||||
s->Get(2, r.Nickname);
|
||||
s->Get(3, r.JoinedAt);
|
||||
s->Get(4, r.PremiumSince);
|
||||
//s->Get(5, r.IsDeafened);
|
||||
//s->Get(6, r.IsMuted);
|
||||
// s->Get(5, r.IsDeafened);
|
||||
// s->Get(6, r.IsMuted);
|
||||
s->Get(7, r.Avatar);
|
||||
s->Get(8, r.IsPending);
|
||||
|
||||
@@ -862,8 +888,8 @@ Message Store::GetMessageBound(std::unique_ptr<Statement> &s) const {
|
||||
s->Get(4, r.Content);
|
||||
s->Get(5, r.Timestamp);
|
||||
s->Get(6, r.EditedTimestamp);
|
||||
//s->Get(7, r.IsTTS);
|
||||
//s->Get(8, r.DoesMentionEveryone);
|
||||
// s->Get(7, r.IsTTS);
|
||||
// s->Get(8, r.DoesMentionEveryone);
|
||||
s->GetJSON(9, r.Embeds);
|
||||
s->Get(10, r.IsPinned);
|
||||
s->Get(11, r.WebhookID);
|
||||
@@ -889,21 +915,28 @@ Message Store::GetMessageBound(std::unique_ptr<Statement> &s) const {
|
||||
}
|
||||
|
||||
if (!s->IsNull(25)) {
|
||||
auto &a = r.Attachments.emplace_back();
|
||||
s->Get(25, a.ID);
|
||||
s->Get(26, a.Filename);
|
||||
s->Get(27, a.Bytes);
|
||||
s->Get(28, a.URL);
|
||||
s->Get(29, a.ProxyURL);
|
||||
s->Get(30, a.Height);
|
||||
s->Get(31, a.Width);
|
||||
auto &q = r.MessageReference.emplace();
|
||||
s->Get(25, q.MessageID);
|
||||
s->Get(26, q.ChannelID);
|
||||
s->Get(27, q.GuildID);
|
||||
}
|
||||
|
||||
if (!s->IsNull(32)) {
|
||||
auto &q = r.MessageReference.emplace();
|
||||
s->Get(32, q.MessageID);
|
||||
s->Get(33, q.ChannelID);
|
||||
s->Get(34, q.GuildID);
|
||||
int num_attachments;
|
||||
s->Get(28, num_attachments);
|
||||
if (num_attachments > 0) {
|
||||
auto &s = m_stmt_get_attachments;
|
||||
s->Bind(1, r.ID);
|
||||
while (s->FetchOne()) {
|
||||
auto &q = r.Attachments.emplace_back();
|
||||
s->Get(1, q.ID);
|
||||
s->Get(2, q.Filename);
|
||||
s->Get(3, q.Bytes);
|
||||
s->Get(4, q.URL);
|
||||
s->Get(5, q.ProxyURL);
|
||||
s->Get(6, q.Height);
|
||||
s->Get(7, q.Width);
|
||||
}
|
||||
s->Reset();
|
||||
}
|
||||
|
||||
{
|
||||
@@ -984,11 +1017,11 @@ std::optional<RoleData> Store::GetRole(Snowflake id) const {
|
||||
return role;
|
||||
}
|
||||
|
||||
RoleData Store::GetRoleBound(std::unique_ptr<Statement> &s) const {
|
||||
RoleData Store::GetRoleBound(std::unique_ptr<Statement> &s) {
|
||||
RoleData r;
|
||||
|
||||
s->Get(0, r.ID);
|
||||
//s->Get(1, guild id);
|
||||
// s->Get(1, guild id);
|
||||
s->Get(2, r.Name);
|
||||
s->Get(3, r.Color);
|
||||
s->Get(4, r.IsHoisted);
|
||||
@@ -1061,6 +1094,14 @@ void Store::ClearRecipient(Snowflake channel_id, Snowflake user_id) {
|
||||
s->Reset();
|
||||
}
|
||||
|
||||
void Store::ClearRole(Snowflake id) {
|
||||
auto &s = m_stmt_clr_role;
|
||||
|
||||
s->Bind(1, id);
|
||||
s->Step();
|
||||
s->Reset();
|
||||
}
|
||||
|
||||
std::unordered_set<Snowflake> Store::GetChannels() const {
|
||||
auto &s = m_stmt_get_chan_ids;
|
||||
std::unordered_set<Snowflake> r;
|
||||
@@ -1505,6 +1546,16 @@ bool Store::CreateTables() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_db.Execute(R"(
|
||||
CREATE TRIGGER remove_deleted_roles AFTER DELETE ON roles
|
||||
BEGIN
|
||||
DELETE FROM member_roles WHERE role = old.id;
|
||||
END
|
||||
)") != SQLITE_OK) {
|
||||
fprintf(stderr, "failed to create roles trigger: %s\n", m_db.ErrStr());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1594,55 +1645,43 @@ bool Store::CreateStatements() {
|
||||
message_interactions.name,
|
||||
message_interactions.type,
|
||||
message_interactions.user_id,
|
||||
attachments.id,
|
||||
attachments.filename,
|
||||
attachments.size,
|
||||
attachments.url,
|
||||
attachments.proxy,
|
||||
attachments.height,
|
||||
attachments.width,
|
||||
message_references.message,
|
||||
message_references.channel,
|
||||
message_references.guild
|
||||
message_references.guild,
|
||||
COUNT(attachments.id)
|
||||
FROM messages
|
||||
LEFT OUTER JOIN
|
||||
message_interactions
|
||||
ON messages.id = message_interactions.message_id
|
||||
LEFT OUTER JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
LEFT OUTER JOIN
|
||||
message_references
|
||||
ON messages.id = message_references.id
|
||||
WHERE messages.id = ?1
|
||||
LEFT JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
WHERE messages.id = ?1 GROUP BY messages.id
|
||||
UNION ALL
|
||||
SELECT messages.*,
|
||||
message_interactions.interaction_id,
|
||||
message_interactions.name,
|
||||
message_interactions.type,
|
||||
message_interactions.user_id,
|
||||
attachments.id,
|
||||
attachments.filename,
|
||||
attachments.size,
|
||||
attachments.url,
|
||||
attachments.proxy,
|
||||
attachments.height,
|
||||
attachments.width,
|
||||
message_references.message,
|
||||
message_references.channel,
|
||||
message_references.guild
|
||||
message_references.guild,
|
||||
COUNT(attachments.id)
|
||||
FROM messages
|
||||
LEFT OUTER JOIN
|
||||
message_interactions
|
||||
ON messages.id = message_interactions.message_id
|
||||
LEFT OUTER JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
LEFT OUTER JOIN
|
||||
message_references
|
||||
ON messages.id = message_references.id
|
||||
LEFT JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
WHERE messages.id = (SELECT message FROM message_references WHERE id = ?1)
|
||||
ORDER BY messages.id DESC
|
||||
GROUP BY messages.id ORDER BY messages.id DESC
|
||||
)");
|
||||
if (!m_stmt_get_msg->OK()) {
|
||||
fprintf(stderr, "failed to prepare get message statement: %s\n", m_db.ErrStr());
|
||||
@@ -1666,27 +1705,21 @@ bool Store::CreateStatements() {
|
||||
message_interactions.name,
|
||||
message_interactions.type,
|
||||
message_interactions.user_id,
|
||||
attachments.id,
|
||||
attachments.filename,
|
||||
attachments.size,
|
||||
attachments.url,
|
||||
attachments.proxy,
|
||||
attachments.height,
|
||||
attachments.width,
|
||||
message_references.message,
|
||||
message_references.channel,
|
||||
message_references.guild
|
||||
message_references.guild,
|
||||
COUNT(attachments.id)
|
||||
FROM messages
|
||||
LEFT OUTER JOIN
|
||||
message_interactions
|
||||
ON messages.id = message_interactions.message_id
|
||||
LEFT OUTER JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
LEFT OUTER JOIN
|
||||
message_references
|
||||
ON messages.id = message_references.id
|
||||
WHERE channel_id = ? AND pending = 0 ORDER BY id DESC LIMIT ?
|
||||
LEFT JOIN
|
||||
attachments
|
||||
ON messages.id = attachments.message
|
||||
WHERE channel_id = ? AND pending = 0 GROUP BY messages.id ORDER BY id DESC LIMIT ?
|
||||
) ORDER BY id ASC
|
||||
)");
|
||||
if (!m_stmt_get_last_msgs->OK()) {
|
||||
@@ -1857,6 +1890,20 @@ bool Store::CreateStatements() {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_stmt_clr_member_roles = std::make_unique<Statement>(m_db, R"(
|
||||
DELETE FROM member_roles
|
||||
WHERE user = ? AND
|
||||
EXISTS (
|
||||
SELECT 1 FROM roles
|
||||
WHERE member_roles.role = roles.id
|
||||
AND roles.guild = ?
|
||||
)
|
||||
)");
|
||||
if (!m_stmt_clr_member_roles->OK()) {
|
||||
fprintf(stderr, "failed to prepare clear member roles statement: %s\n", m_db.ErrStr());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_stmt_set_guild_emoji = std::make_unique<Statement>(m_db, R"(
|
||||
REPLACE INTO guild_emojis VALUES (
|
||||
?, ?
|
||||
@@ -2134,6 +2181,23 @@ bool Store::CreateStatements() {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_stmt_get_guild_member_ids = std::make_unique<Statement>(m_db, R"(
|
||||
SELECT user_id FROM members WHERE guild_id = ?
|
||||
)");
|
||||
if (!m_stmt_get_guild_member_ids->OK()) {
|
||||
fprintf(stderr, "failed to prepare get guild member ids statement: %s\n", m_db.ErrStr());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_stmt_clr_role = std::make_unique<Statement>(m_db, R"(
|
||||
DELETE FROM roles
|
||||
WHERE id = ?1;
|
||||
)");
|
||||
if (!m_stmt_clr_role->OK()) {
|
||||
fprintf(stderr, "failed to prepare clear role statement: %s\n", m_db.ErrStr());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2224,11 +2288,11 @@ int Store::Statement::Bind(int index, Snowflake id) {
|
||||
|
||||
int Store::Statement::Bind(int index, const char *str, size_t len) {
|
||||
if (len == -1) len = strlen(str);
|
||||
return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str, len, SQLITE_TRANSIENT));
|
||||
return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str, static_cast<int>(len), SQLITE_TRANSIENT));
|
||||
}
|
||||
|
||||
int Store::Statement::Bind(int index, const std::string &str) {
|
||||
return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str.c_str(), str.size(), SQLITE_TRANSIENT));
|
||||
return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str.c_str(), static_cast<int>(str.size()), SQLITE_TRANSIENT));
|
||||
}
|
||||
|
||||
int Store::Statement::Bind(int index) {
|
||||
|
||||
@@ -44,6 +44,8 @@ public:
|
||||
std::vector<Message> GetPinnedMessages(Snowflake channel_id) const;
|
||||
std::vector<ChannelData> GetActiveThreads(Snowflake channel_id) const; // public
|
||||
std::vector<Snowflake> GetChannelIDsWithParentID(Snowflake channel_id) const;
|
||||
std::unordered_set<Snowflake> GetMembersInGuild(Snowflake guild_id) const;
|
||||
// ^ not the same as GetUsersInGuild since users in a guild may include users who do not have retrieved member data
|
||||
|
||||
void AddReaction(const MessageReactionAddObject &data, bool byself);
|
||||
void RemoveReaction(const MessageReactionRemoveObject &data, bool byself);
|
||||
@@ -52,6 +54,7 @@ public:
|
||||
void ClearChannel(Snowflake id);
|
||||
void ClearBan(Snowflake guild_id, Snowflake user_id);
|
||||
void ClearRecipient(Snowflake channel_id, Snowflake user_id);
|
||||
void ClearRole(Snowflake id);
|
||||
|
||||
std::unordered_set<Snowflake> GetChannels() const;
|
||||
std::unordered_set<Snowflake> GetGuilds() const;
|
||||
@@ -99,7 +102,7 @@ private:
|
||||
~Statement();
|
||||
Statement &operator=(Statement &other) = delete;
|
||||
|
||||
bool OK() const;
|
||||
[[nodiscard]] bool OK() const;
|
||||
|
||||
int Bind(int index, Snowflake id);
|
||||
int Bind(int index, const char *str, size_t len = -1);
|
||||
@@ -222,7 +225,7 @@ private:
|
||||
*first++ = id.get<T>();
|
||||
}
|
||||
|
||||
bool IsNull(int index) const;
|
||||
[[nodiscard]] bool IsNull(int index) const;
|
||||
int Step();
|
||||
bool Insert();
|
||||
bool FetchOne();
|
||||
@@ -236,7 +239,7 @@ private:
|
||||
};
|
||||
|
||||
Message GetMessageBound(std::unique_ptr<Statement> &stmt) const;
|
||||
RoleData GetRoleBound(std::unique_ptr<Statement> &stmt) const;
|
||||
static RoleData GetRoleBound(std::unique_ptr<Statement> &stmt);
|
||||
|
||||
void SetMessageInteractionPair(Snowflake message_id, const MessageInteractionData &interaction);
|
||||
|
||||
@@ -278,6 +281,7 @@ private:
|
||||
STMT(set_interaction);
|
||||
STMT(set_member_roles);
|
||||
STMT(get_member_roles);
|
||||
STMT(clr_member_roles);
|
||||
STMT(set_guild_emoji);
|
||||
STMT(get_guild_emojis);
|
||||
STMT(clr_guild_emoji);
|
||||
@@ -302,5 +306,7 @@ private:
|
||||
STMT(sub_reaction);
|
||||
STMT(get_reactions);
|
||||
STMT(get_chan_ids_parent);
|
||||
STMT(get_guild_member_ids);
|
||||
STMT(clr_role);
|
||||
#undef STMT
|
||||
};
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#include "user.hpp"
|
||||
#include "abaddon.hpp"
|
||||
|
||||
bool UserData::IsABot() const noexcept {
|
||||
return IsBot.has_value() && *IsBot;
|
||||
}
|
||||
|
||||
bool UserData::IsDeleted() const {
|
||||
return Discriminator == "0000";
|
||||
}
|
||||
@@ -17,7 +21,7 @@ bool UserData::HasAnimatedAvatar(Snowflake guild_id) const {
|
||||
const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id);
|
||||
if (member.has_value() && member->Avatar.has_value() && member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_')
|
||||
return true;
|
||||
else if (!member->Avatar.has_value())
|
||||
else if (member.has_value() && !member->Avatar.has_value())
|
||||
return HasAnimatedAvatar();
|
||||
return false;
|
||||
}
|
||||
@@ -29,7 +33,7 @@ bool UserData::HasAnimatedAvatar(const std::optional<Snowflake> &guild_id) const
|
||||
return HasAnimatedAvatar();
|
||||
}
|
||||
|
||||
std::string UserData::GetAvatarURL(Snowflake guild_id, std::string ext, std::string size) const {
|
||||
std::string UserData::GetAvatarURL(Snowflake guild_id, const std::string &ext, std::string size) const {
|
||||
const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id);
|
||||
if (member.has_value() && member->Avatar.has_value()) {
|
||||
if (ext == "gif" && !(member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_'))
|
||||
@@ -43,14 +47,14 @@ std::string UserData::GetAvatarURL(Snowflake guild_id, std::string ext, std::str
|
||||
}
|
||||
}
|
||||
|
||||
std::string UserData::GetAvatarURL(const std::optional<Snowflake> &guild_id, std::string ext, std::string size) const {
|
||||
std::string UserData::GetAvatarURL(const std::optional<Snowflake> &guild_id, const std::string &ext, std::string size) const {
|
||||
if (guild_id.has_value())
|
||||
return GetAvatarURL(*guild_id, ext, size);
|
||||
else
|
||||
return GetAvatarURL(ext, size);
|
||||
}
|
||||
|
||||
std::string UserData::GetAvatarURL(std::string ext, std::string size) const {
|
||||
std::string UserData::GetAvatarURL(const std::string &ext, std::string size) const {
|
||||
if (HasAvatar())
|
||||
return "https://cdn.discordapp.com/avatars/" + std::to_string(ID) + "/" + Avatar + "." + ext + "?size=" + size;
|
||||
else
|
||||
@@ -107,7 +111,7 @@ void to_json(nlohmann::json &j, const UserData &m) {
|
||||
j["id"] = m.ID;
|
||||
j["username"] = m.Username;
|
||||
j["discriminator"] = m.Discriminator;
|
||||
if (m.Avatar == "")
|
||||
if (m.Avatar.empty())
|
||||
j["avatar"] = nullptr;
|
||||
else
|
||||
j["avatar"] = m.Avatar;
|
||||
|
||||
@@ -60,22 +60,23 @@ struct UserData {
|
||||
friend void to_json(nlohmann::json &j, const UserData &m);
|
||||
void update_from_json(const nlohmann::json &j);
|
||||
|
||||
bool IsDeleted() const;
|
||||
bool HasAvatar() const;
|
||||
bool HasAnimatedAvatar() const noexcept;
|
||||
bool HasAnimatedAvatar(Snowflake guild_id) const;
|
||||
bool HasAnimatedAvatar(const std::optional<Snowflake> &guild_id) const;
|
||||
std::string GetAvatarURL(Snowflake guild_id, std::string ext = "png", std::string size = "32") const;
|
||||
std::string GetAvatarURL(const std::optional<Snowflake> &guild_id, std::string ext = "png", std::string size = "32") const;
|
||||
std::string GetAvatarURL(std::string ext = "png", std::string size = "32") const;
|
||||
std::string GetDefaultAvatarURL() const;
|
||||
Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const;
|
||||
std::string GetMention() const;
|
||||
std::string GetEscapedName() const;
|
||||
std::string GetEscapedBoldName() const;
|
||||
std::string GetEscapedString() const;
|
||||
[[nodiscard]] bool IsABot() const noexcept;
|
||||
[[nodiscard]] bool IsDeleted() const;
|
||||
[[nodiscard]] bool HasAvatar() const;
|
||||
[[nodiscard]] bool HasAnimatedAvatar() const noexcept;
|
||||
[[nodiscard]] bool HasAnimatedAvatar(Snowflake guild_id) const;
|
||||
[[nodiscard]] bool HasAnimatedAvatar(const std::optional<Snowflake> &guild_id) const;
|
||||
[[nodiscard]] std::string GetAvatarURL(Snowflake guild_id, const std::string &ext = "png", std::string size = "32") const;
|
||||
[[nodiscard]] std::string GetAvatarURL(const std::optional<Snowflake> &guild_id, const std::string &ext = "png", std::string size = "32") const;
|
||||
[[nodiscard]] std::string GetAvatarURL(const std::string &ext = "png", std::string size = "32") const;
|
||||
[[nodiscard]] std::string GetDefaultAvatarURL() const;
|
||||
[[nodiscard]] Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const;
|
||||
[[nodiscard]] std::string GetMention() const;
|
||||
[[nodiscard]] std::string GetEscapedName() const;
|
||||
[[nodiscard]] std::string GetEscapedBoldName() const;
|
||||
[[nodiscard]] std::string GetEscapedString() const;
|
||||
template<bool with_at>
|
||||
inline std::string GetEscapedBoldString() const {
|
||||
[[nodiscard]] inline std::string GetEscapedBoldString() const {
|
||||
if constexpr (with_at)
|
||||
return "<b>@" + Glib::Markup::escape_text(Username) + "</b>#" + Discriminator;
|
||||
else
|
||||
|
||||
@@ -8,33 +8,5 @@ void from_json(const nlohmann::json &j, UserSettingsGuildFoldersEntry &m) {
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, UserSettings &m) {
|
||||
JS_D("timezone_offset", m.TimezoneOffset);
|
||||
JS_D("theme", m.Theme);
|
||||
JS_D("stream_notifications_enabled", m.AreStreamNotificationsEnabled);
|
||||
JS_D("status", m.Status);
|
||||
JS_D("show_current_game", m.ShouldShowCurrentGame);
|
||||
// JS_D("restricted_guilds", m.RestrictedGuilds);
|
||||
JS_D("render_reactions", m.ShouldRenderReactions);
|
||||
JS_D("render_embeds", m.ShouldRenderEmbeds);
|
||||
JS_D("native_phone_integration_enabled", m.IsNativePhoneIntegrationEnabled);
|
||||
JS_D("message_display_compact", m.ShouldMessageDisplayCompact);
|
||||
JS_D("locale", m.Locale);
|
||||
JS_D("inline_embed_media", m.ShouldInlineEmbedMedia);
|
||||
JS_D("inline_attachment_media", m.ShouldInlineAttachmentMedia);
|
||||
JS_D("guild_positions", m.GuildPositions);
|
||||
JS_D("guild_folders", m.GuildFolders);
|
||||
JS_D("gif_auto_play", m.ShouldGIFAutoplay);
|
||||
// JS_D("friend_source_flags", m.FriendSourceFlags);
|
||||
JS_D("explicit_content_filter", m.ExplicitContentFilter);
|
||||
JS_D("enable_tts_command", m.IsTTSCommandEnabled);
|
||||
JS_D("disable_games_tab", m.ShouldDisableGamesTab);
|
||||
JS_D("developer_mode", m.DeveloperMode);
|
||||
JS_D("detect_platform_accounts", m.ShouldDetectPlatformAccounts);
|
||||
JS_D("default_guilds_restricted", m.AreDefaultGuildsRestricted);
|
||||
// JS_N("custom_status", m.CustomStatus);
|
||||
JS_D("convert_emoticons", m.ShouldConvertEmoticons);
|
||||
JS_D("contact_sync_enabled", m.IsContactSyncEnabled);
|
||||
JS_D("animate_emoji", m.ShouldAnimateEmojis);
|
||||
JS_D("allow_accessibility_detection", m.IsAccessibilityDetectionAllowed);
|
||||
JS_D("afk_timeout", m.AFKTimeout);
|
||||
}
|
||||
|
||||
@@ -6,42 +6,43 @@
|
||||
struct UserSettingsGuildFoldersEntry {
|
||||
int Color = -1; // null
|
||||
std::vector<Snowflake> GuildIDs;
|
||||
Snowflake ID; // null (this can be a snowflake as a string or an int that isnt a snowflake lol)
|
||||
Snowflake ID; // null (this can be a snowflake as a string or an int that isnt a snowflake lol)
|
||||
std::string Name; // null
|
||||
|
||||
friend void from_json(const nlohmann::json &j, UserSettingsGuildFoldersEntry &m);
|
||||
};
|
||||
|
||||
struct UserSettings {
|
||||
int TimezoneOffset; //
|
||||
std::string Theme; //
|
||||
bool AreStreamNotificationsEnabled; //
|
||||
std::string Status; //
|
||||
bool ShouldShowCurrentGame; //
|
||||
// std::vector<Unknown> RestrictedGuilds; //
|
||||
bool ShouldRenderReactions; //
|
||||
bool ShouldRenderEmbeds; //
|
||||
bool IsNativePhoneIntegrationEnabled; //
|
||||
bool ShouldMessageDisplayCompact; //
|
||||
std::string Locale; //
|
||||
bool ShouldInlineEmbedMedia; //
|
||||
bool ShouldInlineAttachmentMedia; //
|
||||
std::vector<UserSettingsGuildFoldersEntry> GuildFolders;
|
||||
/*
|
||||
int TimezoneOffset;
|
||||
std::string Theme;
|
||||
bool AreStreamNotificationsEnabled;
|
||||
std::string Status;
|
||||
bool ShouldShowCurrentGame;
|
||||
// std::vector<Unknown> RestrictedGuilds;
|
||||
bool ShouldRenderReactions;
|
||||
bool ShouldRenderEmbeds;
|
||||
bool IsNativePhoneIntegrationEnabled;
|
||||
bool ShouldMessageDisplayCompact;
|
||||
std::string Locale;
|
||||
bool ShouldInlineEmbedMedia;
|
||||
bool ShouldInlineAttachmentMedia;
|
||||
std::vector<Snowflake> GuildPositions; // deprecated?
|
||||
std::vector<UserSettingsGuildFoldersEntry> GuildFolders; //
|
||||
bool ShouldGIFAutoplay; //
|
||||
// Unknown FriendSourceFlags; //
|
||||
int ExplicitContentFilter; //
|
||||
bool IsTTSCommandEnabled; //
|
||||
bool ShouldDisableGamesTab; //
|
||||
bool DeveloperMode; //
|
||||
bool ShouldDetectPlatformAccounts; //
|
||||
bool AreDefaultGuildsRestricted; //
|
||||
bool ShouldGIFAutoplay;
|
||||
// Unknown FriendSourceFlags;
|
||||
int ExplicitContentFilter;
|
||||
bool IsTTSCommandEnabled;
|
||||
bool ShouldDisableGamesTab;
|
||||
bool DeveloperMode;
|
||||
bool ShouldDetectPlatformAccounts;
|
||||
bool AreDefaultGuildsRestricted;
|
||||
// Unknown CustomStatus; // null
|
||||
bool ShouldConvertEmoticons; //
|
||||
bool IsContactSyncEnabled; //
|
||||
bool ShouldAnimateEmojis; //
|
||||
bool IsAccessibilityDetectionAllowed; //
|
||||
int AFKTimeout;
|
||||
bool ShouldConvertEmoticons;
|
||||
bool IsContactSyncEnabled;
|
||||
bool ShouldAnimateEmojis;
|
||||
bool IsAccessibilityDetectionAllowed;
|
||||
int AFKTimeout;*/
|
||||
|
||||
friend void from_json(const nlohmann::json &j, UserSettings &m);
|
||||
};
|
||||
|
||||
544
src/discord/voiceclient.cpp
Normal file
544
src/discord/voiceclient.cpp
Normal file
@@ -0,0 +1,544 @@
|
||||
#ifdef WITH_VOICE
|
||||
// clang-format off
|
||||
|
||||
#include "voiceclient.hpp"
|
||||
#include "json.hpp"
|
||||
#include <sodium.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/fmt/bin_to_hex.h>
|
||||
#include "abaddon.hpp"
|
||||
#include "audio/manager.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define S_ADDR(var) (var).sin_addr.S_un.S_addr
|
||||
#define socklen_t int
|
||||
#else
|
||||
#define S_ADDR(var) (var).sin_addr.s_addr
|
||||
#endif
|
||||
// clang-format on
|
||||
|
||||
UDPSocket::UDPSocket()
|
||||
: m_socket(INVALID_SOCKET) {
|
||||
}
|
||||
|
||||
UDPSocket::~UDPSocket() {
|
||||
Stop();
|
||||
}
|
||||
|
||||
void UDPSocket::Connect(std::string_view ip, uint16_t port) {
|
||||
std::memset(&m_server, 0, sizeof(m_server));
|
||||
m_server.sin_family = AF_INET;
|
||||
S_ADDR(m_server) = inet_addr(ip.data());
|
||||
m_server.sin_port = htons(port);
|
||||
m_socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
bind(m_socket, reinterpret_cast<sockaddr *>(&m_server), sizeof(m_server));
|
||||
}
|
||||
|
||||
void UDPSocket::Run() {
|
||||
m_running = true;
|
||||
m_thread = std::thread(&UDPSocket::ReadThread, this);
|
||||
}
|
||||
|
||||
void UDPSocket::SetSecretKey(std::array<uint8_t, 32> key) {
|
||||
m_secret_key = key;
|
||||
}
|
||||
|
||||
void UDPSocket::SetSSRC(uint32_t ssrc) {
|
||||
m_ssrc = ssrc;
|
||||
}
|
||||
|
||||
void UDPSocket::SendEncrypted(const uint8_t *data, size_t len) {
|
||||
m_sequence++;
|
||||
m_timestamp += 480; // this is important
|
||||
|
||||
std::vector<uint8_t> rtp(12 + len + crypto_secretbox_MACBYTES, 0);
|
||||
rtp[0] = 0x80; // ver 2
|
||||
rtp[1] = 0x78; // payload type 0x78
|
||||
rtp[2] = (m_sequence >> 8) & 0xFF;
|
||||
rtp[3] = (m_sequence >> 0) & 0xFF;
|
||||
rtp[4] = (m_timestamp >> 24) & 0xFF;
|
||||
rtp[5] = (m_timestamp >> 16) & 0xFF;
|
||||
rtp[6] = (m_timestamp >> 8) & 0xFF;
|
||||
rtp[7] = (m_timestamp >> 0) & 0xFF;
|
||||
rtp[8] = (m_ssrc >> 24) & 0xFF;
|
||||
rtp[9] = (m_ssrc >> 16) & 0xFF;
|
||||
rtp[10] = (m_ssrc >> 8) & 0xFF;
|
||||
rtp[11] = (m_ssrc >> 0) & 0xFF;
|
||||
|
||||
static std::array<uint8_t, 24> nonce = {};
|
||||
std::memcpy(nonce.data(), rtp.data(), 12);
|
||||
crypto_secretbox_easy(rtp.data() + 12, data, len, nonce.data(), m_secret_key.data());
|
||||
|
||||
Send(rtp.data(), rtp.size());
|
||||
}
|
||||
|
||||
void UDPSocket::SendEncrypted(const std::vector<uint8_t> &data) {
|
||||
SendEncrypted(data.data(), data.size());
|
||||
}
|
||||
|
||||
void UDPSocket::Send(const uint8_t *data, size_t len) {
|
||||
sendto(m_socket, reinterpret_cast<const char *>(data), static_cast<int>(len), 0, reinterpret_cast<sockaddr *>(&m_server), sizeof(m_server));
|
||||
}
|
||||
|
||||
std::vector<uint8_t> UDPSocket::Receive() {
|
||||
while (true) {
|
||||
sockaddr_in from;
|
||||
socklen_t fromlen = sizeof(from);
|
||||
static std::array<uint8_t, 4096> buf;
|
||||
int n = recvfrom(m_socket, reinterpret_cast<char *>(buf.data()), sizeof(buf), 0, reinterpret_cast<sockaddr *>(&from), &fromlen);
|
||||
if (n < 0) {
|
||||
return {};
|
||||
} else if (S_ADDR(from) == S_ADDR(m_server) && from.sin_port == m_server.sin_port) {
|
||||
return { buf.begin(), buf.begin() + n };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UDPSocket::Stop() {
|
||||
#ifdef _WIN32
|
||||
closesocket(m_socket);
|
||||
#else
|
||||
close(m_socket);
|
||||
#endif
|
||||
m_running = false;
|
||||
if (m_thread.joinable()) m_thread.join();
|
||||
}
|
||||
|
||||
void UDPSocket::ReadThread() {
|
||||
timeval tv;
|
||||
while (m_running) {
|
||||
static std::array<uint8_t, 4096> buf;
|
||||
sockaddr_in from;
|
||||
socklen_t addrlen = sizeof(from);
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 1000000;
|
||||
|
||||
fd_set read_fds;
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(m_socket, &read_fds);
|
||||
|
||||
if (select(m_socket + 1, &read_fds, nullptr, nullptr, &tv) > 0) {
|
||||
int n = recvfrom(m_socket, reinterpret_cast<char *>(buf.data()), sizeof(buf), 0, reinterpret_cast<sockaddr *>(&from), &addrlen);
|
||||
if (n > 0 && S_ADDR(from) == S_ADDR(m_server) && from.sin_port == m_server.sin_port) {
|
||||
m_signal_data.emit({ buf.begin(), buf.begin() + n });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UDPSocket::type_signal_data UDPSocket::signal_data() {
|
||||
return m_signal_data;
|
||||
}
|
||||
|
||||
DiscordVoiceClient::DiscordVoiceClient()
|
||||
: m_state(State::DisconnectedByClient)
|
||||
, m_ws("voice-ws")
|
||||
, m_log(spdlog::get("voice")) {
|
||||
if (sodium_init() == -1) {
|
||||
m_log->critical("sodium_init() failed");
|
||||
}
|
||||
|
||||
m_udp.signal_data().connect([this](const std::vector<uint8_t> &data) {
|
||||
OnUDPData(data);
|
||||
});
|
||||
|
||||
m_ws.signal_open().connect(sigc::mem_fun(*this, &DiscordVoiceClient::OnWebsocketOpen));
|
||||
m_ws.signal_close().connect(sigc::mem_fun(*this, &DiscordVoiceClient::OnWebsocketClose));
|
||||
m_ws.signal_message().connect(sigc::mem_fun(*this, &DiscordVoiceClient::OnWebsocketMessage));
|
||||
|
||||
m_dispatcher.connect(sigc::mem_fun(*this, &DiscordVoiceClient::OnDispatch));
|
||||
|
||||
// idle or else singleton deadlock
|
||||
Glib::signal_idle().connect_once([this]() {
|
||||
auto &audio = Abaddon::Get().GetAudio();
|
||||
audio.SetOpusBuffer(m_opus_buffer.data());
|
||||
audio.signal_opus_packet().connect([this](int payload_size) {
|
||||
if (IsConnected()) {
|
||||
m_udp.SendEncrypted(m_opus_buffer.data(), payload_size);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
DiscordVoiceClient::~DiscordVoiceClient() {
|
||||
if (IsConnected() || IsConnecting()) Stop();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::Start() {
|
||||
SetState(State::ConnectingToWebsocket);
|
||||
m_heartbeat_waiter.revive();
|
||||
m_keepalive_waiter.revive();
|
||||
m_ws.StartConnection("wss://" + m_endpoint + "/?v=7");
|
||||
|
||||
m_signal_connected.emit();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::Stop() {
|
||||
if (!IsConnected() && !IsConnecting()) {
|
||||
m_log->warn("Requested stop while not connected (from {})", GetStateName(m_state));
|
||||
return;
|
||||
}
|
||||
|
||||
SetState(State::DisconnectedByClient);
|
||||
m_ws.Stop(4014);
|
||||
m_udp.Stop();
|
||||
m_heartbeat_waiter.kill();
|
||||
if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
|
||||
m_keepalive_waiter.kill();
|
||||
if (m_keepalive_thread.joinable()) m_keepalive_thread.join();
|
||||
|
||||
m_signal_disconnected.emit();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetSessionID(std::string_view session_id) {
|
||||
m_session_id = session_id;
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetEndpoint(std::string_view endpoint) {
|
||||
m_endpoint = endpoint;
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetToken(std::string_view token) {
|
||||
m_token = token;
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetServerID(Snowflake id) {
|
||||
m_server_id = id;
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetUserID(Snowflake id) {
|
||||
m_user_id = id;
|
||||
}
|
||||
|
||||
std::optional<uint32_t> DiscordVoiceClient::GetSSRCOfUser(Snowflake id) const {
|
||||
if (const auto it = m_ssrc_map.find(id); it != m_ssrc_map.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool DiscordVoiceClient::IsConnected() const noexcept {
|
||||
return m_state == State::Connected;
|
||||
}
|
||||
|
||||
bool DiscordVoiceClient::IsConnecting() const noexcept {
|
||||
return m_state == State::ConnectingToWebsocket || m_state == State::EstablishingConnection;
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnGatewayMessage(const std::string &str) {
|
||||
VoiceGatewayMessage msg = nlohmann::json::parse(str);
|
||||
switch (msg.Opcode) {
|
||||
case VoiceGatewayOp::Hello:
|
||||
HandleGatewayHello(msg);
|
||||
break;
|
||||
case VoiceGatewayOp::Ready:
|
||||
HandleGatewayReady(msg);
|
||||
break;
|
||||
case VoiceGatewayOp::SessionDescription:
|
||||
HandleGatewaySessionDescription(msg);
|
||||
break;
|
||||
case VoiceGatewayOp::Speaking:
|
||||
HandleGatewaySpeaking(msg);
|
||||
break;
|
||||
default:
|
||||
m_log->warn("Unhandled opcode: {}", static_cast<int>(msg.Opcode));
|
||||
}
|
||||
}
|
||||
|
||||
const char *DiscordVoiceClient::GetStateName(State state) {
|
||||
switch (state) {
|
||||
case State::DisconnectedByClient:
|
||||
return "DisconnectedByClient";
|
||||
case State::DisconnectedByServer:
|
||||
return "DisconnectedByServer";
|
||||
case State::ConnectingToWebsocket:
|
||||
return "ConnectingToWebsocket";
|
||||
case State::EstablishingConnection:
|
||||
return "EstablishingConnection";
|
||||
case State::Connected:
|
||||
return "Connected";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::HandleGatewayHello(const VoiceGatewayMessage &m) {
|
||||
VoiceHelloData d = m.Data;
|
||||
|
||||
m_log->debug("Received hello: {}ms", d.HeartbeatInterval);
|
||||
|
||||
m_heartbeat_msec = d.HeartbeatInterval;
|
||||
m_heartbeat_thread = std::thread(&DiscordVoiceClient::HeartbeatThread, this);
|
||||
|
||||
Identify();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::HandleGatewayReady(const VoiceGatewayMessage &m) {
|
||||
VoiceReadyData d = m.Data;
|
||||
|
||||
m_log->debug("Received ready: {}:{} (ssrc: {})", d.IP, d.Port, d.SSRC);
|
||||
|
||||
m_ip = d.IP;
|
||||
m_port = d.Port;
|
||||
m_ssrc = d.SSRC;
|
||||
|
||||
if (std::find(d.Modes.begin(), d.Modes.end(), "xsalsa20_poly1305") == d.Modes.end()) {
|
||||
m_log->warn("xsalsa20_poly1305 not in modes");
|
||||
}
|
||||
|
||||
m_udp.Connect(m_ip, m_port);
|
||||
m_keepalive_thread = std::thread(&DiscordVoiceClient::KeepaliveThread, this);
|
||||
|
||||
Discovery();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::HandleGatewaySessionDescription(const VoiceGatewayMessage &m) {
|
||||
VoiceSessionDescriptionData d = m.Data;
|
||||
|
||||
m_log->debug("Received session description (mode: {}) (key: {:ns}) ", d.Mode, spdlog::to_hex(d.SecretKey.begin(), d.SecretKey.end()));
|
||||
|
||||
VoiceSpeakingMessage msg;
|
||||
msg.Delay = 0;
|
||||
msg.SSRC = m_ssrc;
|
||||
msg.Speaking = VoiceSpeakingType::Microphone;
|
||||
m_ws.Send(msg);
|
||||
|
||||
m_secret_key = d.SecretKey;
|
||||
m_udp.SetSSRC(m_ssrc);
|
||||
m_udp.SetSecretKey(m_secret_key);
|
||||
m_udp.SendEncrypted({ 0xF8, 0xFF, 0xFE });
|
||||
m_udp.Run();
|
||||
|
||||
SetState(State::Connected);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::HandleGatewaySpeaking(const VoiceGatewayMessage &m) {
|
||||
VoiceSpeakingData d = m.Data;
|
||||
m_ssrc_map[d.UserID] = d.SSRC;
|
||||
m_signal_speaking.emit(d);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::Identify() {
|
||||
VoiceIdentifyMessage msg;
|
||||
msg.ServerID = m_server_id;
|
||||
msg.UserID = m_user_id;
|
||||
msg.SessionID = m_session_id;
|
||||
msg.Token = m_token;
|
||||
msg.Video = true;
|
||||
|
||||
m_ws.Send(msg);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::Discovery() {
|
||||
std::vector<uint8_t> payload;
|
||||
// request
|
||||
payload.push_back(0x00);
|
||||
payload.push_back(0x01);
|
||||
// payload length (70)
|
||||
payload.push_back(0x00);
|
||||
payload.push_back(0x46);
|
||||
// ssrc
|
||||
payload.push_back((m_ssrc >> 24) & 0xFF);
|
||||
payload.push_back((m_ssrc >> 16) & 0xFF);
|
||||
payload.push_back((m_ssrc >> 8) & 0xFF);
|
||||
payload.push_back((m_ssrc >> 0) & 0xFF);
|
||||
// space for address and port
|
||||
for (int i = 0; i < 66; i++) payload.push_back(0x00);
|
||||
|
||||
m_udp.Send(payload.data(), payload.size());
|
||||
|
||||
constexpr int MAX_TRIES = 100;
|
||||
for (int i = 0; i < MAX_TRIES; i++) {
|
||||
const auto response = m_udp.Receive();
|
||||
if (response.size() >= 74 && response[0] == 0x00 && response[1] == 0x02) {
|
||||
const char *ip = reinterpret_cast<const char *>(response.data() + 8);
|
||||
uint16_t port = (response[73] << 8) | response[74];
|
||||
m_log->info("Discovered IP and port: {}:{}", ip, port);
|
||||
SelectProtocol(ip, port);
|
||||
break;
|
||||
} else {
|
||||
m_log->error("Received non-discovery packet after sending request (try {}/{})", i + 1, MAX_TRIES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SelectProtocol(const char *ip, uint16_t port) {
|
||||
VoiceSelectProtocolMessage msg;
|
||||
msg.Mode = "xsalsa20_poly1305";
|
||||
msg.Address = ip;
|
||||
msg.Port = port;
|
||||
msg.Protocol = "udp";
|
||||
|
||||
m_ws.Send(msg);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnWebsocketOpen() {
|
||||
m_log->info("Websocket opened");
|
||||
SetState(State::EstablishingConnection);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnWebsocketClose(const ix::WebSocketCloseInfo &info) {
|
||||
if (info.remote) {
|
||||
m_log->debug("Websocket closed (remote): {} ({})", info.code, info.reason);
|
||||
} else {
|
||||
m_log->debug("Websocket closed (local): {} ({})", info.code, info.reason);
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnWebsocketMessage(const std::string &data) {
|
||||
m_dispatch_mutex.lock();
|
||||
m_dispatch_queue.push(data);
|
||||
m_dispatcher.emit();
|
||||
m_dispatch_mutex.unlock();
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::HeartbeatThread() {
|
||||
while (true) {
|
||||
if (!m_heartbeat_waiter.wait_for(std::chrono::milliseconds(m_heartbeat_msec))) break;
|
||||
|
||||
const auto ms = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
|
||||
m_log->trace("Heartbeat: {}", ms);
|
||||
|
||||
VoiceHeartbeatMessage msg;
|
||||
msg.Nonce = ms;
|
||||
m_ws.Send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::KeepaliveThread() {
|
||||
while (true) {
|
||||
if (!m_heartbeat_waiter.wait_for(std::chrono::seconds(10))) break;
|
||||
|
||||
if (IsConnected()) {
|
||||
const static uint8_t KEEPALIVE[] = { 0x13, 0x37 };
|
||||
m_udp.Send(KEEPALIVE, sizeof(KEEPALIVE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::SetState(State state) {
|
||||
m_log->debug("Changing state to {}", GetStateName(state));
|
||||
m_state = state;
|
||||
m_signal_state_update.emit(state);
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnUDPData(std::vector<uint8_t> data) {
|
||||
uint8_t *payload = data.data() + 12;
|
||||
uint32_t ssrc = (data[8] << 24) |
|
||||
(data[9] << 16) |
|
||||
(data[10] << 8) |
|
||||
(data[11] << 0);
|
||||
static std::array<uint8_t, 24> nonce = {};
|
||||
std::memcpy(nonce.data(), data.data(), 12);
|
||||
if (crypto_secretbox_open_easy(payload, payload, data.size() - 12, nonce.data(), m_secret_key.data())) {
|
||||
// spdlog::get("voice")->trace("UDP payload decryption failure");
|
||||
} else {
|
||||
Abaddon::Get().GetAudio().FeedMeOpus(ssrc, { payload, payload + data.size() - 12 - crypto_box_MACBYTES });
|
||||
}
|
||||
}
|
||||
|
||||
void DiscordVoiceClient::OnDispatch() {
|
||||
m_dispatch_mutex.lock();
|
||||
if (m_dispatch_queue.empty()) {
|
||||
m_dispatch_mutex.unlock();
|
||||
return;
|
||||
}
|
||||
auto msg = std::move(m_dispatch_queue.front());
|
||||
m_dispatch_queue.pop();
|
||||
m_dispatch_mutex.unlock();
|
||||
OnGatewayMessage(msg);
|
||||
}
|
||||
|
||||
DiscordVoiceClient::type_signal_disconnected DiscordVoiceClient::signal_connected() {
|
||||
return m_signal_connected;
|
||||
}
|
||||
|
||||
DiscordVoiceClient::type_signal_disconnected DiscordVoiceClient::signal_disconnected() {
|
||||
return m_signal_disconnected;
|
||||
}
|
||||
|
||||
DiscordVoiceClient::type_signal_speaking DiscordVoiceClient::signal_speaking() {
|
||||
return m_signal_speaking;
|
||||
}
|
||||
|
||||
DiscordVoiceClient::type_signal_state_update DiscordVoiceClient::signal_state_update() {
|
||||
return m_signal_state_update;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceGatewayMessage &m) {
|
||||
JS_D("op", m.Opcode);
|
||||
m.Data = j.at("d");
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceHelloData &m) {
|
||||
JS_D("heartbeat_interval", m.HeartbeatInterval);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const VoiceHeartbeatMessage &m) {
|
||||
j["op"] = VoiceGatewayOp::Heartbeat;
|
||||
j["d"] = m.Nonce;
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const VoiceIdentifyMessage &m) {
|
||||
j["op"] = VoiceGatewayOp::Identify;
|
||||
j["d"]["server_id"] = m.ServerID;
|
||||
j["d"]["user_id"] = m.UserID;
|
||||
j["d"]["session_id"] = m.SessionID;
|
||||
j["d"]["token"] = m.Token;
|
||||
j["d"]["video"] = m.Video;
|
||||
j["d"]["streams"][0]["type"] = "video";
|
||||
j["d"]["streams"][0]["rid"] = "100";
|
||||
j["d"]["streams"][0]["quality"] = 100;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceReadyData::VoiceStream &m) {
|
||||
JS_D("active", m.IsActive);
|
||||
JS_D("quality", m.Quality);
|
||||
JS_D("rid", m.RID);
|
||||
JS_D("rtx_ssrc", m.RTXSSRC);
|
||||
JS_D("ssrc", m.SSRC);
|
||||
JS_D("type", m.Type);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceReadyData &m) {
|
||||
JS_ON("experiments", m.Experiments);
|
||||
JS_D("ip", m.IP);
|
||||
JS_D("modes", m.Modes);
|
||||
JS_D("port", m.Port);
|
||||
JS_D("ssrc", m.SSRC);
|
||||
JS_ON("streams", m.Streams);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const VoiceSelectProtocolMessage &m) {
|
||||
j["op"] = VoiceGatewayOp::SelectProtocol;
|
||||
j["d"]["address"] = m.Address;
|
||||
j["d"]["port"] = m.Port;
|
||||
j["d"]["protocol"] = m.Protocol;
|
||||
j["d"]["mode"] = m.Mode;
|
||||
j["d"]["data"]["address"] = m.Address;
|
||||
j["d"]["data"]["port"] = m.Port;
|
||||
j["d"]["data"]["mode"] = m.Mode;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceSessionDescriptionData &m) {
|
||||
JS_D("mode", m.Mode);
|
||||
JS_D("secret_key", m.SecretKey);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const VoiceSpeakingMessage &m) {
|
||||
j["op"] = VoiceGatewayOp::Speaking;
|
||||
j["d"]["speaking"] = m.Speaking;
|
||||
j["d"]["delay"] = m.Delay;
|
||||
j["d"]["ssrc"] = m.SSRC;
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, VoiceSpeakingData &m) {
|
||||
JS_D("user_id", m.UserID);
|
||||
JS_D("ssrc", m.SSRC);
|
||||
JS_D("speaking", m.Speaking);
|
||||
}
|
||||
#endif
|
||||
288
src/discord/voiceclient.hpp
Normal file
288
src/discord/voiceclient.hpp
Normal file
@@ -0,0 +1,288 @@
|
||||
#pragma once
|
||||
#ifdef WITH_VOICE
|
||||
// clang-format off
|
||||
|
||||
#include "snowflake.hpp"
|
||||
#include "waiter.hpp"
|
||||
#include "websocket.hpp"
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <glibmm/dispatcher.h>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <spdlog/logger.h>
|
||||
#include <unordered_map>
|
||||
// clang-format on
|
||||
|
||||
enum class VoiceGatewayCloseCode : uint16_t {
|
||||
Normal = 4000,
|
||||
UnknownOpcode = 4001,
|
||||
InvalidPayload = 4002,
|
||||
NotAuthenticated = 4003,
|
||||
AuthenticationFailed = 4004,
|
||||
AlreadyAuthenticated = 4005,
|
||||
SessionInvalid = 4006,
|
||||
SessionTimedOut = 4009,
|
||||
ServerNotFound = 4011,
|
||||
UnknownProtocol = 4012,
|
||||
Disconnected = 4014,
|
||||
ServerCrashed = 4015,
|
||||
UnknownEncryption = 4016,
|
||||
};
|
||||
|
||||
enum class VoiceGatewayOp : int {
|
||||
Identify = 0,
|
||||
SelectProtocol = 1,
|
||||
Ready = 2,
|
||||
Heartbeat = 3,
|
||||
SessionDescription = 4,
|
||||
Speaking = 5,
|
||||
HeartbeatAck = 6,
|
||||
Resume = 7,
|
||||
Hello = 8,
|
||||
Resumed = 9,
|
||||
ClientDisconnect = 13,
|
||||
};
|
||||
|
||||
struct VoiceGatewayMessage {
|
||||
VoiceGatewayOp Opcode;
|
||||
nlohmann::json Data;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceGatewayMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceHelloData {
|
||||
int HeartbeatInterval;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceHelloData &m);
|
||||
};
|
||||
|
||||
struct VoiceHeartbeatMessage {
|
||||
uint64_t Nonce;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const VoiceHeartbeatMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceIdentifyMessage {
|
||||
Snowflake ServerID;
|
||||
Snowflake UserID;
|
||||
std::string SessionID;
|
||||
std::string Token;
|
||||
bool Video;
|
||||
// todo streams i guess?
|
||||
|
||||
friend void to_json(nlohmann::json &j, const VoiceIdentifyMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceReadyData {
|
||||
struct VoiceStream {
|
||||
bool IsActive;
|
||||
int Quality;
|
||||
std::string RID;
|
||||
int RTXSSRC;
|
||||
int SSRC;
|
||||
std::string Type;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceStream &m);
|
||||
};
|
||||
|
||||
std::vector<std::string> Experiments;
|
||||
std::string IP;
|
||||
std::vector<std::string> Modes;
|
||||
uint16_t Port;
|
||||
uint32_t SSRC;
|
||||
std::vector<VoiceStream> Streams;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceReadyData &m);
|
||||
};
|
||||
|
||||
struct VoiceSelectProtocolMessage {
|
||||
std::string Address;
|
||||
uint16_t Port;
|
||||
std::string Mode;
|
||||
std::string Protocol;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const VoiceSelectProtocolMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceSessionDescriptionData {
|
||||
// std::string AudioCodec;
|
||||
// std::string VideoCodec;
|
||||
// std::string MediaSessionID;
|
||||
std::string Mode;
|
||||
std::array<uint8_t, 32> SecretKey;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceSessionDescriptionData &m);
|
||||
};
|
||||
|
||||
enum class VoiceSpeakingType {
|
||||
Microphone = 1 << 0,
|
||||
Soundshare = 1 << 1,
|
||||
Priority = 1 << 2,
|
||||
};
|
||||
|
||||
struct VoiceSpeakingMessage {
|
||||
VoiceSpeakingType Speaking;
|
||||
int Delay;
|
||||
uint32_t SSRC;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const VoiceSpeakingMessage &m);
|
||||
};
|
||||
|
||||
struct VoiceSpeakingData {
|
||||
Snowflake UserID;
|
||||
uint32_t SSRC;
|
||||
VoiceSpeakingType Speaking;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, VoiceSpeakingData &m);
|
||||
};
|
||||
|
||||
class UDPSocket {
|
||||
public:
|
||||
UDPSocket();
|
||||
~UDPSocket();
|
||||
|
||||
void Connect(std::string_view ip, uint16_t port);
|
||||
void Run();
|
||||
void SetSecretKey(std::array<uint8_t, 32> key);
|
||||
void SetSSRC(uint32_t ssrc);
|
||||
void SendEncrypted(const uint8_t *data, size_t len);
|
||||
void SendEncrypted(const std::vector<uint8_t> &data);
|
||||
void Send(const uint8_t *data, size_t len);
|
||||
std::vector<uint8_t> Receive();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
void ReadThread();
|
||||
|
||||
#ifdef _WIN32
|
||||
SOCKET m_socket;
|
||||
#else
|
||||
int m_socket;
|
||||
#endif
|
||||
sockaddr_in m_server;
|
||||
|
||||
std::atomic<bool> m_running = false;
|
||||
|
||||
std::thread m_thread;
|
||||
|
||||
std::array<uint8_t, 32> m_secret_key;
|
||||
uint32_t m_ssrc;
|
||||
|
||||
uint16_t m_sequence = 0;
|
||||
uint32_t m_timestamp = 0;
|
||||
|
||||
public:
|
||||
using type_signal_data = sigc::signal<void, std::vector<uint8_t>>;
|
||||
type_signal_data signal_data();
|
||||
|
||||
private:
|
||||
type_signal_data m_signal_data;
|
||||
};
|
||||
|
||||
class DiscordVoiceClient {
|
||||
public:
|
||||
DiscordVoiceClient();
|
||||
~DiscordVoiceClient();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
|
||||
void SetSessionID(std::string_view session_id);
|
||||
void SetEndpoint(std::string_view endpoint);
|
||||
void SetToken(std::string_view token);
|
||||
void SetServerID(Snowflake id);
|
||||
void SetUserID(Snowflake id);
|
||||
|
||||
[[nodiscard]] std::optional<uint32_t> GetSSRCOfUser(Snowflake id) const;
|
||||
|
||||
// Is a websocket and udp connection fully established
|
||||
[[nodiscard]] bool IsConnected() const noexcept;
|
||||
[[nodiscard]] bool IsConnecting() const noexcept;
|
||||
|
||||
enum class State {
|
||||
ConnectingToWebsocket,
|
||||
EstablishingConnection,
|
||||
Connected,
|
||||
DisconnectedByClient,
|
||||
DisconnectedByServer,
|
||||
};
|
||||
|
||||
private:
|
||||
static const char *GetStateName(State state);
|
||||
|
||||
void OnGatewayMessage(const std::string &msg);
|
||||
void HandleGatewayHello(const VoiceGatewayMessage &m);
|
||||
void HandleGatewayReady(const VoiceGatewayMessage &m);
|
||||
void HandleGatewaySessionDescription(const VoiceGatewayMessage &m);
|
||||
void HandleGatewaySpeaking(const VoiceGatewayMessage &m);
|
||||
|
||||
void Identify();
|
||||
void Discovery();
|
||||
void SelectProtocol(const char *ip, uint16_t port);
|
||||
|
||||
void OnWebsocketOpen();
|
||||
void OnWebsocketClose(const ix::WebSocketCloseInfo &info);
|
||||
void OnWebsocketMessage(const std::string &str);
|
||||
|
||||
void HeartbeatThread();
|
||||
void KeepaliveThread();
|
||||
|
||||
void SetState(State state);
|
||||
|
||||
void OnUDPData(std::vector<uint8_t> data);
|
||||
|
||||
std::string m_session_id;
|
||||
std::string m_endpoint;
|
||||
std::string m_token;
|
||||
Snowflake m_server_id;
|
||||
Snowflake m_channel_id;
|
||||
Snowflake m_user_id;
|
||||
|
||||
std::unordered_map<Snowflake, uint32_t> m_ssrc_map;
|
||||
|
||||
std::array<uint8_t, 32> m_secret_key;
|
||||
|
||||
std::string m_ip;
|
||||
uint16_t m_port;
|
||||
uint32_t m_ssrc;
|
||||
|
||||
int m_heartbeat_msec;
|
||||
Waiter m_heartbeat_waiter;
|
||||
std::thread m_heartbeat_thread;
|
||||
|
||||
Waiter m_keepalive_waiter;
|
||||
std::thread m_keepalive_thread;
|
||||
|
||||
Websocket m_ws;
|
||||
UDPSocket m_udp;
|
||||
|
||||
Glib::Dispatcher m_dispatcher;
|
||||
std::queue<std::string> m_dispatch_queue;
|
||||
std::mutex m_dispatch_mutex;
|
||||
|
||||
void OnDispatch();
|
||||
|
||||
std::array<uint8_t, 1275> m_opus_buffer;
|
||||
|
||||
std::shared_ptr<spdlog::logger> m_log;
|
||||
|
||||
std::atomic<State> m_state;
|
||||
|
||||
using type_signal_connected = sigc::signal<void()>;
|
||||
using type_signal_disconnected = sigc::signal<void()>;
|
||||
using type_signal_speaking = sigc::signal<void(VoiceSpeakingData)>;
|
||||
using type_signal_state_update = sigc::signal<void(State)>;
|
||||
type_signal_connected m_signal_connected;
|
||||
type_signal_disconnected m_signal_disconnected;
|
||||
type_signal_speaking m_signal_speaking;
|
||||
type_signal_state_update m_signal_state_update;
|
||||
|
||||
public:
|
||||
type_signal_connected signal_connected();
|
||||
type_signal_disconnected signal_disconnected();
|
||||
type_signal_speaking signal_speaking();
|
||||
type_signal_state_update signal_state_update();
|
||||
};
|
||||
#endif
|
||||
29
src/discord/waiter.hpp
Normal file
29
src/discord/waiter.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
class Waiter {
|
||||
public:
|
||||
template<class R, class P>
|
||||
bool wait_for(std::chrono::duration<R, P> const &time) const {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
return !cv.wait_for(lock, time, [&] { return terminate; });
|
||||
}
|
||||
|
||||
void kill() {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
terminate = true;
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
void revive() {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
terminate = false;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::condition_variable cv;
|
||||
mutable std::mutex m;
|
||||
bool terminate = false;
|
||||
};
|
||||
@@ -1,18 +1,37 @@
|
||||
#include "websocket.hpp"
|
||||
#include <functional>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <utility>
|
||||
|
||||
Websocket::Websocket() {}
|
||||
Websocket::Websocket(const std::string &id)
|
||||
: m_close_info { 1000, "Normal", false } {
|
||||
if (m_log = spdlog::get(id); !m_log) {
|
||||
m_log = spdlog::stdout_color_mt(id);
|
||||
}
|
||||
|
||||
void Websocket::StartConnection(std::string url) {
|
||||
m_websocket.disableAutomaticReconnection();
|
||||
m_websocket.setUrl(url);
|
||||
m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1));
|
||||
m_websocket.setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works
|
||||
m_websocket.start();
|
||||
m_open_dispatcher.connect([this]() {
|
||||
m_signal_open.emit();
|
||||
});
|
||||
|
||||
m_close_dispatcher.connect([this]() {
|
||||
Stop();
|
||||
m_signal_close.emit(m_close_info);
|
||||
});
|
||||
}
|
||||
|
||||
void Websocket::StartConnection(const std::string &url) {
|
||||
m_log->debug("Starting connection to {}", url);
|
||||
|
||||
m_websocket = std::make_unique<ix::WebSocket>();
|
||||
|
||||
m_websocket->disableAutomaticReconnection();
|
||||
m_websocket->setUrl(url);
|
||||
m_websocket->setOnMessageCallback([this](auto &&msg) { OnMessage(std::forward<decltype(msg)>(msg)); });
|
||||
m_websocket->setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works
|
||||
m_websocket->start();
|
||||
}
|
||||
|
||||
void Websocket::SetUserAgent(std::string agent) {
|
||||
m_agent = agent;
|
||||
m_agent = std::move(agent);
|
||||
}
|
||||
|
||||
bool Websocket::GetPrintMessages() const noexcept {
|
||||
@@ -24,22 +43,19 @@ void Websocket::SetPrintMessages(bool show) noexcept {
|
||||
}
|
||||
|
||||
void Websocket::Stop() {
|
||||
m_log->debug("Stopping with default close code");
|
||||
Stop(ix::WebSocketCloseConstants::kNormalClosureCode);
|
||||
}
|
||||
|
||||
void Websocket::Stop(uint16_t code) {
|
||||
m_websocket.stop(code);
|
||||
}
|
||||
|
||||
bool Websocket::IsOpen() const {
|
||||
auto state = m_websocket.getReadyState();
|
||||
return state == ix::ReadyState::Open;
|
||||
m_log->debug("Stopping with close code {}", code);
|
||||
m_websocket-> stop(code);
|
||||
}
|
||||
|
||||
void Websocket::Send(const std::string &str) {
|
||||
if (m_print_messages)
|
||||
printf("sending %s\n", str.c_str());
|
||||
m_websocket.sendText(str);
|
||||
m_log->trace("Send: {}", str);
|
||||
m_websocket->sendText(str);
|
||||
}
|
||||
|
||||
void Websocket::Send(const nlohmann::json &j) {
|
||||
@@ -49,10 +65,13 @@ void Websocket::Send(const nlohmann::json &j) {
|
||||
void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) {
|
||||
switch (msg->type) {
|
||||
case ix::WebSocketMessageType::Open: {
|
||||
m_signal_open.emit();
|
||||
m_log->debug("Received open frame, dispatching");
|
||||
m_open_dispatcher.emit();
|
||||
} break;
|
||||
case ix::WebSocketMessageType::Close: {
|
||||
m_signal_close.emit(msg->closeInfo.code);
|
||||
m_log->debug("Received close frame, dispatching. {} ({}){}", msg->closeInfo.code, msg->closeInfo.reason, msg->closeInfo.remote ? " Remote" : "");
|
||||
m_close_info = msg->closeInfo;
|
||||
m_close_dispatcher.emit();
|
||||
} break;
|
||||
case ix::WebSocketMessageType::Message: {
|
||||
m_signal_message.emit(msg->str);
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
#include <ixwebsocket/IXWebSocket.h>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <glibmm.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
class Websocket {
|
||||
public:
|
||||
Websocket();
|
||||
void StartConnection(std::string url);
|
||||
Websocket(const std::string &id);
|
||||
void StartConnection(const std::string &url);
|
||||
|
||||
void SetUserAgent(std::string agent);
|
||||
|
||||
@@ -20,17 +22,16 @@ public:
|
||||
void Send(const nlohmann::json &j);
|
||||
void Stop();
|
||||
void Stop(uint16_t code);
|
||||
bool IsOpen() const;
|
||||
|
||||
private:
|
||||
void OnMessage(const ix::WebSocketMessagePtr &msg);
|
||||
|
||||
ix::WebSocket m_websocket;
|
||||
std::unique_ptr<ix::WebSocket> m_websocket;
|
||||
std::string m_agent;
|
||||
|
||||
public:
|
||||
using type_signal_open = sigc::signal<void>;
|
||||
using type_signal_close = sigc::signal<void, uint16_t>;
|
||||
using type_signal_close = sigc::signal<void, ix::WebSocketCloseInfo>;
|
||||
using type_signal_message = sigc::signal<void, std::string>;
|
||||
|
||||
type_signal_open signal_open();
|
||||
@@ -43,4 +44,10 @@ private:
|
||||
type_signal_message m_signal_message;
|
||||
|
||||
bool m_print_messages = true;
|
||||
|
||||
Glib::Dispatcher m_open_dispatcher;
|
||||
Glib::Dispatcher m_close_dispatcher;
|
||||
ix::WebSocketCloseInfo m_close_info;
|
||||
|
||||
std::shared_ptr<spdlog::logger> m_log;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
#include "emojis.hpp"
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#ifdef ABADDON_IS_BIG_ENDIAN
|
||||
/* Allows processing emojis.bin correctly on big-endian systems. */
|
||||
int emojis_int32_correct_endian(int little_endian_in) {
|
||||
/* this does the same thing as __bswap_32() but can be done without
|
||||
non-standard headers. */
|
||||
return ((little_endian_in >> 24) & 0xff) | // move byte 3 to byte 0
|
||||
((little_endian_in << 8) & 0xff0000) | // move byte 1 to byte 2
|
||||
((little_endian_in >> 8) & 0xff00) | // move byte 2 to byte 1
|
||||
((little_endian_in << 24) & 0xff000000); // byte 0 to byte 3
|
||||
}
|
||||
#else
|
||||
int emojis_int32_correct_endian(int little_endian_in) {
|
||||
return little_endian_in;
|
||||
}
|
||||
#endif
|
||||
|
||||
EmojiResource::EmojiResource(std::string filepath)
|
||||
: m_filepath(filepath) {}
|
||||
: m_filepath(std::move(filepath)) {}
|
||||
|
||||
bool EmojiResource::Load() {
|
||||
m_fp = std::fopen(m_filepath.c_str(), "rb");
|
||||
@@ -10,18 +27,22 @@ bool EmojiResource::Load() {
|
||||
|
||||
int index_offset;
|
||||
std::fread(&index_offset, 4, 1, m_fp);
|
||||
index_offset = emojis_int32_correct_endian(index_offset);
|
||||
std::fseek(m_fp, index_offset, SEEK_SET);
|
||||
|
||||
int emojis_count;
|
||||
std::fread(&emojis_count, 4, 1, m_fp);
|
||||
emojis_count = emojis_int32_correct_endian(emojis_count);
|
||||
for (int i = 0; i < emojis_count; i++) {
|
||||
std::vector<std::string> shortcodes;
|
||||
|
||||
int shortcodes_count;
|
||||
std::fread(&shortcodes_count, 4, 1, m_fp);
|
||||
shortcodes_count = emojis_int32_correct_endian(shortcodes_count);
|
||||
for (int j = 0; j < shortcodes_count; j++) {
|
||||
int shortcode_length;
|
||||
std::fread(&shortcode_length, 4, 1, m_fp);
|
||||
shortcode_length = emojis_int32_correct_endian(shortcode_length);
|
||||
std::string shortcode(shortcode_length, '\0');
|
||||
std::fread(shortcode.data(), shortcode_length, 1, m_fp);
|
||||
shortcodes.push_back(std::move(shortcode));
|
||||
@@ -29,13 +50,16 @@ bool EmojiResource::Load() {
|
||||
|
||||
int surrogates_count;
|
||||
std::fread(&surrogates_count, 4, 1, m_fp);
|
||||
surrogates_count = emojis_int32_correct_endian(surrogates_count);
|
||||
std::string surrogates(surrogates_count, '\0');
|
||||
std::fread(surrogates.data(), surrogates_count, 1, m_fp);
|
||||
m_patterns.push_back(surrogates);
|
||||
m_patterns.emplace_back(surrogates);
|
||||
|
||||
int data_size, data_offset;
|
||||
std::fread(&data_size, 4, 1, m_fp);
|
||||
data_size = emojis_int32_correct_endian(data_size);
|
||||
std::fread(&data_offset, 4, 1, m_fp);
|
||||
data_offset = emojis_int32_correct_endian(data_offset);
|
||||
m_index[surrogates] = { data_offset, data_size };
|
||||
|
||||
for (const auto &shortcode : shortcodes)
|
||||
@@ -52,7 +76,7 @@ bool EmojiResource::Load() {
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> EmojiResource::GetPixBuf(const Glib::ustring &pattern) {
|
||||
const auto it = m_index.find(pattern);
|
||||
if (it == m_index.end()) return Glib::RefPtr<Gdk::Pixbuf>();
|
||||
if (it == m_index.end()) return {};
|
||||
const int pos = it->second.first;
|
||||
const int len = it->second.second;
|
||||
std::fseek(m_fp, pos, SEEK_SET);
|
||||
@@ -86,17 +110,17 @@ void EmojiResource::ReplaceEmojis(Glib::RefPtr<Gtk::TextBuffer> buf, int size) {
|
||||
else
|
||||
break;
|
||||
}
|
||||
searchpos = r + pattern.size();
|
||||
searchpos = static_cast<int>(r + pattern.size());
|
||||
|
||||
const auto start_it = buf->get_iter_at_offset(r);
|
||||
const auto end_it = buf->get_iter_at_offset(r + pattern.size());
|
||||
const auto start_it = buf->get_iter_at_offset(static_cast<int>(r));
|
||||
const auto end_it = buf->get_iter_at_offset(static_cast<int>(r + pattern.size()));
|
||||
|
||||
auto it = buf->erase(start_it, end_it);
|
||||
buf->insert_pixbuf(it, pixbuf);
|
||||
|
||||
int alen = text.size();
|
||||
int alen = static_cast<int>(text.size());
|
||||
text = get_text();
|
||||
int blen = text.size();
|
||||
int blen = static_cast<int>(text.size());
|
||||
searchpos -= (alen - blen);
|
||||
}
|
||||
}
|
||||
@@ -109,10 +133,6 @@ std::string EmojiResource::GetShortCodeForPattern(const Glib::ustring &pattern)
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::vector<Glib::ustring> &EmojiResource::GetPatterns() const {
|
||||
return m_patterns;
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> &EmojiResource::GetShortCodes() const {
|
||||
return m_shortcode_index;
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user