Compare commits
304 commits
Author | SHA1 | Date | |
---|---|---|---|
c147e02187 | |||
066ab487fc | |||
a8060b393c | |||
321f0b03c8 | |||
d4cec645b5 | |||
a04693e39d | |||
3cce057545 | |||
9a44ae1fa5 | |||
85ff6c25ba | |||
3cc7db8eff | |||
|
ff9a591d6d | ||
8e3f10caff | |||
8bfe88929f | |||
9927bdc38b | |||
|
8083859541 | ||
|
030c374139 | ||
|
2c61b82924 | ||
fb843a1346 | |||
8d82d340a4 | |||
acd60eaba2 | |||
829777935f | |||
0be2648849 | |||
93c5be412e | |||
8f5325b291 | |||
75554c7451 | |||
00af582287 | |||
19835af3cf | |||
e31ef78e71 | |||
be466fbad1 | |||
0a530bfa93 | |||
637eb702a8 | |||
a7d1a28f29 | |||
297e08ba41 | |||
9d688e8596 | |||
23ec80ccba | |||
5fbb03fc46 | |||
81cf0f8d34 | |||
69d797fe51 | |||
4f295fee3c | |||
fffef9876a | |||
283e9ebc4d | |||
21b40a9ccb | |||
76a9c5da0c | |||
8ec0af3205 | |||
4b68da458f | |||
927bdf0dab | |||
5ba97ecc25 | |||
9fff409630 | |||
99fd001292 | |||
2d8f32c257 | |||
77dd28b600 | |||
6f32e99593 | |||
ec362cef55 | |||
e4a2728ef8 | |||
bf11d8a74e | |||
edf1ee60cd | |||
4af16b75bf | |||
bb304ce774 | |||
3c6b611a41 | |||
73d83f55af | |||
b72a837754 | |||
d4bf7e599a | |||
15fb4bbd62 | |||
2aed8a1209 | |||
78ef3664f7 | |||
5aa0f4bca9 | |||
b45a73b723 | |||
758a9d95f3 | |||
dfe72ca36c | |||
db3bc358a7 | |||
0b61b6e928 | |||
820dc845ea | |||
87973b3b67 | |||
b6ba022bff | |||
7b2b7ee5d5 | |||
c50cd1140e | |||
037dabbe06 | |||
2ae75a4b91 | |||
d162494ec8 | |||
7e9eed2075 | |||
7192286aeb | |||
645b92ba51 | |||
80c5e2f2b4 | |||
1f065f23e6 | |||
3c48577eee | |||
0340db7f2f | |||
c3a45ec58c | |||
7ba94e9deb | |||
eac87e713f | |||
d86e2c28a0 | |||
2fcc432aef | |||
e58213b294 | |||
3916aec358 | |||
721d3a1a89 | |||
83cb220175 | |||
18859cb960 | |||
4c20a314f0 | |||
51ac1ac709 | |||
8f949277f6 | |||
ce686e121b | |||
f64e5c2df0 | |||
2c26c7e264 | |||
69e0c88d8d | |||
82d54ba4df | |||
1b66fda318 | |||
9f746d203b | |||
27377e0ec5 | |||
4baa3bccbf | |||
4786388822 | |||
62f02c18d7 | |||
1fcd403dba | |||
5f6691067a | |||
788c6ca556 | |||
bf4a27f35d | |||
0823b35148 | |||
73b1b58a96 | |||
d8b5ccb2da | |||
243edff8bd | |||
da19eb86bb | |||
0ff9f12157 | |||
802e2f11a1 | |||
c708c33a92 | |||
a8a7ce2538 | |||
841e526e59 | |||
6bee149e6b | |||
62a59eb7a1 | |||
296328f12d | |||
4d3ba6b11f | |||
8a2658e4fc | |||
9ac0ca10f3 | |||
7130e674c4 | |||
e27ae1a82f | |||
1aa2b5a539 | |||
43bfaf9b7e | |||
b19dafef33 | |||
aeaa6b1b28 | |||
e47ba603e0 | |||
8fece95aa2 | |||
893ff53aa8 | |||
39f2f3d975 | |||
52551c1ce0 | |||
50d710de04 | |||
332131796c | |||
3a70df21f8 | |||
a24e8382d1 | |||
d20fd84d39 | |||
5862f1552b | |||
ebeb4089eb | |||
a53126d8bc | |||
7db269acb5 | |||
67e5f9744e | |||
d0bdb374a0 | |||
8b3752ef47 | |||
8c6ac1a21d | |||
1c0802c8ca | |||
d84a33e144 | |||
4f6946a5fc | |||
f3153ef1db | |||
41d9fa7a1c | |||
261b34b712 | |||
d1f108e69d | |||
1e37aa762c | |||
a1f3c00a54 | |||
923afe2420 | |||
faa7d396a5 | |||
c55b7c6baf | |||
6764d8ea11 | |||
5fbb96618f | |||
d89c030e66 | |||
5787af8a4f | |||
1706b93b59 | |||
3f09b8f838 | |||
87c216b491 | |||
5f925217fc | |||
3f1fba4de2 | |||
ddfaa63a24 | |||
721f6daa36 | |||
0d584c5aba | |||
4307262f6e | |||
bd07c49755 | |||
8e99cc2969 | |||
a184ecafa3 | |||
7d2688151c | |||
0038aca1f6 | |||
6e06a1d5bc | |||
b7b70bc198 | |||
ce047db787 | |||
f45319de25 | |||
ebf0c64ffb | |||
d514db9c4a | |||
f34289399e | |||
05d6761baa | |||
216dcd29e9 | |||
0973cb2991 | |||
50190f3eac | |||
b44873d587 | |||
4c5efad9dc | |||
8310708c92 | |||
d936c0302d | |||
0e937199b0 | |||
48e498be25 | |||
3a7735b192 | |||
8f914c02a7 | |||
50bb3f5fd7 | |||
a0348b8fd2 | |||
85555da81f | |||
ebe5addfb5 | |||
b3c6860e25 | |||
00ffbac6b0 | |||
ff4124d1f0 | |||
15342f3c53 | |||
270a32db9e | |||
e0ef1ef797 | |||
e1eea2f3a2 | |||
e54cff0f0c | |||
4e6bd04b02 | |||
38159eafeb | |||
ef1a9846bf | |||
3a120c773a | |||
5f64321c2a | |||
a543eb1aef | |||
480c78cf61 | |||
0dcfc5eedc | |||
87426ee20f | |||
20bcae5ab2 | |||
6b65910ded | |||
9ca4aa29d4 | |||
a625ecb47b | |||
55ae5858b5 | |||
9c855553c5 | |||
83a2e6af85 | |||
494afcf2b5 | |||
a8698cc94f | |||
b50ce146b8 | |||
6657dc79ce | |||
bdca0aa6b1 | |||
cb44b12a7e | |||
21c7d65027 | |||
29c7d31c89 | |||
a77dfd191a | |||
b95028e33e | |||
543538fc56 | |||
7ce27d1c11 | |||
95f0d4008a | |||
3477226367 | |||
ddfb3419cc | |||
b309100f99 | |||
c793f56647 | |||
ff2c9831cf | |||
fe1ae8567a | |||
57d6e3adab | |||
91cc851bfc | |||
6d1b83d0f8 | |||
ed56cca2e7 | |||
a4136ff9fe | |||
565449f176 | |||
626227db93 | |||
13a1970047 | |||
ad1977f05f | |||
0a4c4aa042 | |||
5e7c247bb4 | |||
5a59d54b18 | |||
52efc2b1a4 | |||
55703c2007 | |||
efc90e18c3 | |||
3e594c7e13 | |||
0bcfd779b8 | |||
dd62f84acc | |||
f13b43d38b | |||
867c3a18e9 | |||
f367502b8e | |||
fe27955689 | |||
0c33d81c59 | |||
0cb25a25cf | |||
09a946c204 | |||
ae3a1c97e3 | |||
326eef864b | |||
166a7ac83a | |||
a6e48599aa | |||
3f710e26d0 | |||
09749bac51 | |||
0b78470aaa | |||
dfa4d10c36 | |||
2c13f0d77c | |||
c1c1de1b7b | |||
5bbacad84a | |||
0b57e6a77f | |||
9d491e9e93 | |||
c067835b00 | |||
e924715a57 | |||
f3515f1104 | |||
566fc1f2fb | |||
36c71968bc | |||
652381b067 | |||
2a37f36b83 | |||
c4d22c9c14 | |||
e7be046e9f | |||
d050cd82dd | |||
46e74ad5e8 | |||
dc1ec1c9d4 | |||
64e33b6139 | |||
c678a790e5 | |||
323a549eca | |||
7c32056918 |
316 changed files with 36692 additions and 7127 deletions
45
.forgejo/workflows/release.yml
Normal file
45
.forgejo/workflows/release.yml
Normal file
|
@ -0,0 +1,45 @@
|
|||
name: Squawk Release workflow
|
||||
run-name: ${{ gitea.actor }} is running Squawk Release workflow on release ${{ gitea.event.release.tag_name }}
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
Archlinux:
|
||||
runs-on: archlinux
|
||||
steps:
|
||||
- name: Download the release tarball
|
||||
run: curl -sL ${{ gitea.server_url }}/${{ gitea.repository }}/archive/${{ gitea.event.release.tag_name }}.tar.gz --output tarball.tar.gz
|
||||
|
||||
- name: Calculate SHA256 for the tarball
|
||||
run: echo "tbSum=$(sha256sum tarball.tar.gz | cut -d ' ' -f 1)" >> $GITHUB_ENV
|
||||
|
||||
- name: Unarchive tarball
|
||||
run: tar -xvzf tarball.tar.gz
|
||||
|
||||
- name: Clone the AUR repository
|
||||
run: |
|
||||
echo "${{ secrets.DEPLOY_TO_AUR_PRIVATE_KEY }}" > key
|
||||
chmod 600 key
|
||||
GIT_SSH_COMMAND="ssh -i key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git clone ssh://aur@aur.archlinux.org/squawk.git aur
|
||||
chmod 777 -R aur
|
||||
cd aur
|
||||
git config user.name ${{ secrets.DEPLOY_TO_AUR_USER_NAME }}
|
||||
git config user.email ${{ secrets.DEPLOY_TO_AUR_EMAIL }}
|
||||
|
||||
|
||||
- name: Copy PKGBUILD to the directory
|
||||
run: cp squawk/packaging/Archlinux/PKGBUILD aur/
|
||||
|
||||
- name: Put SHA256 sum to PKGBUILD file, and generate .SRCINFO
|
||||
working-directory: aur
|
||||
run: |
|
||||
sed -i "/sha256sums=/c\sha256sums=('${{ env.tbSum }}')" PKGBUILD
|
||||
sudo -u build makepkg --printsrcinfo > .SRCINFO
|
||||
|
||||
- name: Commit package to aur
|
||||
working-directory: aur
|
||||
run: |
|
||||
git add PKGBUILD .SRCINFO
|
||||
git commit -m "${{ gitea.event.release.body//\"/\\\" }}"
|
||||
GIT_SSH_COMMAND="ssh -i ../key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git push
|
5
.gitmodules
vendored
5
.gitmodules
vendored
|
@ -1,3 +1,6 @@
|
|||
[submodule "external/qxmpp"]
|
||||
path = external/qxmpp
|
||||
url = https://github.com/qxmpp-project/qxmpp.git
|
||||
url = https://invent.kde.org/libraries/qxmpp/
|
||||
[submodule "external/lmdbal"]
|
||||
path = external/lmdbal
|
||||
url = https://git.macaw.me/blue/lmdbal
|
||||
|
|
3640
.uncrustify.cfg
Normal file
3640
.uncrustify.cfg
Normal file
File diff suppressed because it is too large
Load diff
181
CHANGELOG.md
Normal file
181
CHANGELOG.md
Normal file
|
@ -0,0 +1,181 @@
|
|||
# Changelog
|
||||
|
||||
## Squawk 0.2.4 (UNRELEASED)
|
||||
### Bug fixes
|
||||
- messages to the mucs get sent once again
|
||||
|
||||
### Improvements
|
||||
- it's possible to build against Qt 6 now, Qt6 is the default
|
||||
- got rid of deprecated SimpleCrypt library
|
||||
- look up for proper stanzaID
|
||||
|
||||
## Squawk 0.2.3 (February 04, 2024)
|
||||
### Bug fixes
|
||||
- "Add contact" and "Join conference" menu are enabled once again (pavavno)!
|
||||
- availability is now read from the same section of config file it was stored
|
||||
- automatic avatars (if a contact doesn't have one) get generated once again
|
||||
|
||||
### Improvements
|
||||
- deactivated accounts now don't appear in combobox of "Add contact" and "Join conference" dialogues
|
||||
- all of the expandable roster items now get saved between launches
|
||||
- settings file on the disk is not rewritten every roster element expansion or collapse
|
||||
- removed unnecessary own vcard request at sturtup (used to do it to discover my own avatar)
|
||||
- vcard window now is Info system and it can display more information
|
||||
- reduced vcard request spam in MUCs
|
||||
|
||||
### New features
|
||||
- now you can enable tray icon from settings!
|
||||
- there is a job queue now, this allowes to spread a bit the spam on the server at connection time
|
||||
- squawk now queries clients of it's peers, you can see what programs other people use
|
||||
|
||||
## Squawk 0.2.2 (May 05, 2022)
|
||||
### Bug fixes
|
||||
- now when you remove an account it actually gets removed
|
||||
- segfault on uninitialized Availability in some rare occasions
|
||||
- fixed crash when you open a dialog with someone that has only error messages in archive
|
||||
- message height is now calculated correctly on Chinese and Japanese paragraphs
|
||||
- the app doesn't crash on SIGINT anymore
|
||||
|
||||
### Improvements
|
||||
- there is a way to disable an account and it wouldn't connect when you change availability
|
||||
- if you cancel password query an account becomes inactive and doesn't annoy you anymore
|
||||
- if you filled password field and chose KWallet as a storage Squawk wouldn't ask you again for the same password
|
||||
- if left the password field empty and chose KWallet as a storage Squawk will try to get that password from KWallet before asking you to input it
|
||||
- accounts now connect to the server asynchronously - if one is stopped on password prompt another is connecting
|
||||
- actualized translations, added English localization file
|
||||
|
||||
### New features
|
||||
- new "About" window with links, license, gratitudes
|
||||
- if the authentication failed Squawk will ask again for your password and login
|
||||
- now there is an amount of unread messages showing on top of Squawk launcher icon
|
||||
- notifications now have buttons to open a conversation or to mark that message as read
|
||||
|
||||
## Squawk 0.2.1 (Apr 02, 2022)
|
||||
### Bug fixes
|
||||
- build in release mode now no longer spams warnings
|
||||
- build now correctly installs all build plugin libs
|
||||
- a bug where the correction message was received, the indication was on but the text didn't actually change
|
||||
- message body now doesn't intecept context menu from the whole message
|
||||
- message input now doesn't increase font when you remove everything from it
|
||||
|
||||
### Improvements
|
||||
- reduced amount of places where platform specific path separator is used
|
||||
- now message input is automatically focused when you open a dialog or a room
|
||||
- what() method on unhandled exception now actually tells what happened
|
||||
|
||||
### New features
|
||||
- the settings are here! You con config different stuff from there
|
||||
- now it's possible to set up different qt styles from settings
|
||||
- if you have KConfig nad KConfigWidgets packages installed - you can choose from global color schemes
|
||||
- it's possible now to choose a folder where squawk is going to store downloaded files
|
||||
- now you can correct your message
|
||||
|
||||
## Squawk 0.2.0 (Jan 10, 2022)
|
||||
### Bug fixes
|
||||
- carbon copies switches on again after reconnection
|
||||
- requesting the history of the current chat after reconnection
|
||||
- global availability (in drop down list) gets restored after reconnection
|
||||
- status icon in active chat changes when presence of the pen pal changes
|
||||
- infinite progress when open the dialogue with something that has no history to show
|
||||
- fallback icons for buttons, when no supported theme is installed (shunf4)
|
||||
- better handling messages with no id (shunf4)
|
||||
- removed dependency: uuid, now it's on Qt (shunf4)
|
||||
- better requesting latest history (shunf4)
|
||||
|
||||
### Improvements
|
||||
- slightly reduced the traffic on the startup by not requesting history of all MUCs
|
||||
- completely rewritten message feed, now it works way faster and looks cooler
|
||||
- OPTIONAL RUNTIME dependency: "KIO Widgets" that is supposed to allow you to open a file in your default file manager
|
||||
- show in folder now is supposed to try it's best to show file in folder, even you don't have KIO installed
|
||||
- once uploaded local files don't get second time uploaded - the remote URL is reused
|
||||
- way better compilation time (vae)
|
||||
|
||||
### New features
|
||||
- pasting images from clipboard to attachment (shunf4)
|
||||
- possible compilation for windows and macOS (shunf4)
|
||||
|
||||
## Squawk 0.1.5 (Jul 29, 2020)
|
||||
### Bug fixes
|
||||
- error with sending attached files to the conference
|
||||
- error with building on lower versions of QXmpp
|
||||
- error on the first access to the conference database
|
||||
- quit now actually quits the app
|
||||
- history in MUC now works properly and requests messages from server
|
||||
- error with handling non lower cased JIDs
|
||||
- some workaround upon reconnection
|
||||
|
||||
|
||||
## Squawk 0.1.4 (Apr 14, 2020)
|
||||
### New features
|
||||
- message line now is in the same window with roster (new window dialog is still able to opened on context menu)
|
||||
- several new ways to manage your account password:
|
||||
- store it in plain text with the config (like it always was)
|
||||
- store it in config jammed (local hashing with the constant seed, not secure at all but might look like it is)
|
||||
- ask the account password on each program launch
|
||||
- store it in KWallet which is dynamically loaded
|
||||
- dragging into conversation now attach files
|
||||
|
||||
### Bug fixes
|
||||
- never updating MUC avatars now get updated
|
||||
- going offline related segfault fix
|
||||
- statuses now behave better: they wrap if they don't fit, you can select them, you can follow links from there
|
||||
- messages and statuses don't loose content if you use < ore > symbols
|
||||
- now avatars of those who are not in the MUC right now but was also display next to the message
|
||||
- fix crash on attempt to attach the same file for the second time
|
||||
|
||||
|
||||
## Squawk 0.1.3 (Mar 31, 2020)
|
||||
### New features
|
||||
- delivery states for the messages
|
||||
- delivery receipts now work for real
|
||||
- avatars in conferences
|
||||
- edited messages now display correctly
|
||||
- restyling to get better look with different desktop themes
|
||||
|
||||
### Bug fixes
|
||||
- clickable links now detects better
|
||||
- fixed lost messages that came with no ID
|
||||
- avatar related fixes
|
||||
- message carbons now get turned on only if the server supports them
|
||||
- progress spinner fix
|
||||
- files in dialog now have better comment
|
||||
|
||||
|
||||
## Squawk 0.1.2 (Dec 25, 2019)
|
||||
### New features
|
||||
- icons in roster for conferences
|
||||
- pal avatar in dialog window
|
||||
- avatars next to every message in dialog windows (not in conferences yet)
|
||||
- roster window position and size now are stored in config
|
||||
- expanded accounts and groups are stored in config
|
||||
- availability (from top combobox) now is stored in config
|
||||
|
||||
### Bug fixes
|
||||
- segfault when sending more then one attached file
|
||||
- wrong path and name of saving file
|
||||
- wrong message syntax when attaching file and writing text in the save message
|
||||
- problem with links highlighting in dialog
|
||||
- project building fixes
|
||||
|
||||
|
||||
## Squawk 0.1.1 (Nov 16, 2019)
|
||||
A lot of bug fixes, memory leaks fixes
|
||||
### New features
|
||||
- exchange files via HTTP File Upload
|
||||
- download VCards of your contacts
|
||||
- upload your VCard with information about your contact phones email addresses, names, career information and avatar
|
||||
- avatars of your contacts in roster and in notifications
|
||||
|
||||
|
||||
## Squawk 0.0.5 (Oct 10, 2019)
|
||||
### Features
|
||||
- chat directly
|
||||
- have multiple accounts
|
||||
- add contacts
|
||||
- remove contacts
|
||||
- assign contact to different groups
|
||||
- chat in MUCs
|
||||
- join MUCs, leave them, keep them subscribed or unsubscribed
|
||||
- download attachmets
|
||||
- local history
|
||||
- desktop notifications of new messages
|
285
CMakeLists.txt
285
CMakeLists.txt
|
@ -1,65 +1,246 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(squawk)
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(squawk VERSION 0.2.4 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
cmake_policy(SET CMP0076 NEW)
|
||||
cmake_policy(SET CMP0077 NEW)
|
||||
cmake_policy(SET CMP0079 NEW)
|
||||
cmake_policy(SET CMP0167 NEW)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
||||
|
||||
find_package(Qt5Widgets CONFIG REQUIRED)
|
||||
find_package(Qt5LinguistTools)
|
||||
|
||||
set(squawk_SRC
|
||||
main.cpp
|
||||
global.cpp
|
||||
exception.cpp
|
||||
signalcatcher.cpp
|
||||
)
|
||||
|
||||
configure_file(resources/images/logo.svg squawk.svg COPYONLY)
|
||||
execute_process(COMMAND convert -background none -size 48x48 squawk.svg squawk48.png WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
execute_process(COMMAND convert -background none -size 64x64 squawk.svg squawk64.png WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
execute_process(COMMAND convert -background none -size 128x128 squawk.svg squawk128.png WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
execute_process(COMMAND convert -background none -size 256x256 squawk.svg squawk256.png WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
configure_file(packaging/squawk.desktop squawk.desktop COPYONLY)
|
||||
|
||||
set(TS_FILES
|
||||
translations/squawk.ru.ts
|
||||
)
|
||||
qt5_add_translation(QM_FILES ${TS_FILES})
|
||||
add_custom_target(translations ALL DEPENDS ${QM_FILES})
|
||||
|
||||
qt5_add_resources(RCC resources/resources.qrc)
|
||||
|
||||
add_executable(squawk ${squawk_SRC} ${RCC})
|
||||
target_link_libraries(squawk Qt5::Widgets)
|
||||
|
||||
add_subdirectory(ui)
|
||||
add_subdirectory(core)
|
||||
|
||||
option(SYSTEM_QXMPP "Use system qxmpp lib" OFF)
|
||||
|
||||
if(NOT SYSTEM_QXMPP)
|
||||
add_subdirectory(external/qxmpp)
|
||||
set(WIN32_FLAG "")
|
||||
set(MACOSX_BUNDLE_FLAG "")
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
if (WIN32)
|
||||
set(WIN32_FLAG WIN32)
|
||||
endif(WIN32)
|
||||
if (APPLE)
|
||||
set(MACOSX_BUNDLE_FLAG MACOSX_BUNDLE)
|
||||
endif(APPLE)
|
||||
endif()
|
||||
|
||||
target_link_libraries(squawk squawkUI)
|
||||
target_link_libraries(squawk squawkCORE)
|
||||
target_link_libraries(squawk uuid)
|
||||
add_executable(squawk ${WIN32_FLAG} ${MACOSX_BUNDLE_FLAG})
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR})
|
||||
|
||||
add_dependencies(${CMAKE_PROJECT_NAME} translations)
|
||||
option(SYSTEM_QXMPP "Use system qxmpp lib" ON)
|
||||
option(SYSTEM_LMDBAL "Use system lmdbal lib" ON)
|
||||
option(WITH_KWALLET "Build KWallet support module" ON)
|
||||
option(WITH_KIO "Build KIO support module" ON)
|
||||
option(WITH_KCONFIG "Build KConfig support module" ON)
|
||||
option(WITH_OMEMO "Build OMEMO support module" OFF) #it should be off by default untill I sort the problems out
|
||||
|
||||
# Install the executable
|
||||
# Dependencies
|
||||
## Qt, detect and prefer Qt6 if available
|
||||
find_package(Qt6 QUIET CONFIG COMPONENTS Widgets DBus Gui Xml Network Core)
|
||||
if (Qt6_FOUND)
|
||||
set(QT_VERSION_MAJOR 6)
|
||||
else()
|
||||
find_package(Qt5 REQUIRED CONFIG COMPONENTS Widgets DBus Gui Xml Network Core)
|
||||
set(QT_VERSION_MAJOR 5)
|
||||
endif()
|
||||
|
||||
message(STATUS "Building against Qt${QT_VERSION_MAJOR}")
|
||||
|
||||
find_package(Boost COMPONENTS)
|
||||
target_include_directories(squawk PRIVATE ${Boost_INCLUDE_DIRS})
|
||||
|
||||
## OMEMO
|
||||
if (WITH_OMEMO)
|
||||
find_package(PkgConfig)
|
||||
if (PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(OMEMO libomemo-c)
|
||||
if (OMEMO_FOUND)
|
||||
target_compile_definitions(squawk PRIVATE WITH_OMEMO)
|
||||
message("Building with support of OMEMO")
|
||||
else ()
|
||||
message("libomemo-c package wasn't found, trying to build without OMEMO support")
|
||||
set(WITH_OMEMO OFF)
|
||||
endif ()
|
||||
else ()
|
||||
message("PKG_CONFIG module wasn't found, can not check libomemo-c support, trying to build without OMEMO support")
|
||||
set(WITH_OMEMO OFF)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
## KIO
|
||||
if (WITH_KIO)
|
||||
find_package(KF${QT_VERSION_MAJOR}KIO CONFIG)
|
||||
|
||||
if (NOT KF${QT_VERSION_MAJOR}KIO_FOUND)
|
||||
set(WITH_KIO OFF)
|
||||
message("KIO package wasn't found, KIO support modules wouldn't be built")
|
||||
else ()
|
||||
target_compile_definitions(squawk PRIVATE WITH_KIO)
|
||||
message("Building with support of KIO")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
## KWallet
|
||||
if (WITH_KWALLET)
|
||||
find_package(KF${QT_VERSION_MAJOR}Wallet CONFIG)
|
||||
|
||||
if (NOT KF${QT_VERSION_MAJOR}Wallet_FOUND)
|
||||
set(WITH_KWALLET OFF)
|
||||
message("KWallet package wasn't found, KWallet support module wouldn't be built")
|
||||
else ()
|
||||
target_compile_definitions(squawk PRIVATE WITH_KWALLET)
|
||||
message("Building with support of KWallet")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
## KConfig
|
||||
if (WITH_KCONFIG)
|
||||
find_package(KF${QT_VERSION_MAJOR}Config CONFIG)
|
||||
if (NOT KF${QT_VERSION_MAJOR}Config_FOUND)
|
||||
set(WITH_KCONFIG OFF)
|
||||
message("KConfig package wasn't found, KConfig support modules wouldn't be built")
|
||||
else()
|
||||
find_package(KF${QT_VERSION_MAJOR}ConfigWidgets CONFIG)
|
||||
if (NOT KF${QT_VERSION_MAJOR}ConfigWidgets_FOUND)
|
||||
set(WITH_KCONFIG OFF)
|
||||
message("KConfigWidgets package wasn't found, KConfigWidgets support modules wouldn't be built")
|
||||
else()
|
||||
target_compile_definitions(squawk PRIVATE WITH_KCONFIG)
|
||||
message("Building with support of KConfig")
|
||||
message("Building with support of KConfigWidgets")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
## QXmpp
|
||||
if (SYSTEM_QXMPP)
|
||||
if (WITH_OMEMO)
|
||||
find_package(QXmppQt${QT_VERSION_MAJOR} CONFIG COMPONENTS Omemo)
|
||||
else ()
|
||||
find_package(QXmppQt${QT_VERSION_MAJOR} CONFIG)
|
||||
endif ()
|
||||
|
||||
if (NOT QXmppQt${QT_VERSION_MAJOR}_FOUND)
|
||||
set(SYSTEM_QXMPP OFF)
|
||||
message("QXmpp package wasn't found, trying to build with bundled QXmpp")
|
||||
else ()
|
||||
message("Building with system QXmpp")
|
||||
endif ()
|
||||
endif () #it's endif() + if() and not else() because I want it to have a fallback behaviour
|
||||
if (NOT SYSTEM_QXMPP) #we can fail finding system QXmpp and this way we'll check bundled before failing completely
|
||||
message("Building with bundled QXmpp")
|
||||
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR}/external/qxmpp/src/base)
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR}/external/qxmpp/src/client)
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/external/qxmpp/src)
|
||||
|
||||
if (WITH_OMEMO)
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR}/external/qxmpp/src/omemo)
|
||||
target_include_directories(squawk PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/external/qxmpp/src/omemo)
|
||||
set(BUILD_OMEMO ON)
|
||||
set(BUILD_TESTS OFF)
|
||||
else ()
|
||||
set(BUILD_OMEMO OFF)
|
||||
endif ()
|
||||
add_subdirectory(external/qxmpp)
|
||||
add_library(QXmpp::QXmpp ALIAS QXmppQt${QT_VERSION_MAJOR})
|
||||
if (WITH_OMEMO)
|
||||
target_include_directories(QXmppOmemoQt${QT_VERSION_MAJOR} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/external/qxmpp/src)
|
||||
add_library(QXmpp::Omemo ALIAS QXmppOmemoQt${QT_VERSION_MAJOR})
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
## LMDBAL
|
||||
if (SYSTEM_LMDBAL)
|
||||
find_package(lmdbalqt${QT_VERSION_MAJOR})
|
||||
if (NOT lmdbalqt${QT_VERSION_MAJOR}_FOUND)
|
||||
set(SYSTEM_LMDBAL OFF)
|
||||
message("LMDBAL package wasn't found, trying to build with bundled LMDBAL")
|
||||
else ()
|
||||
message("Building with system LMDBAL")
|
||||
endif ()
|
||||
else()
|
||||
message("Building with bundled LMDBAL")
|
||||
set(BUILD_STATIC ON)
|
||||
add_subdirectory(external/lmdbal)
|
||||
add_library(LMDBALQT${QT_VERSION_MAJOR}::LMDBALQT${QT_VERSION_MAJOR} ALIAS LMDBAL)
|
||||
endif()
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
## Linking
|
||||
target_link_libraries(squawk
|
||||
PRIVATE
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
Qt${QT_VERSION_MAJOR}::Widgets
|
||||
Qt${QT_VERSION_MAJOR}::DBus
|
||||
Qt${QT_VERSION_MAJOR}::Network
|
||||
Qt${QT_VERSION_MAJOR}::Gui
|
||||
Qt${QT_VERSION_MAJOR}::Xml
|
||||
LMDBALQT${QT_VERSION_MAJOR}::LMDBALQT${QT_VERSION_MAJOR}
|
||||
OpenSSL::Crypto
|
||||
QXmpp::QXmpp
|
||||
)
|
||||
|
||||
if (WITH_OMEMO)
|
||||
target_link_libraries(squawk PRIVATE QXmpp::Omemo)
|
||||
endif ()
|
||||
|
||||
## Link thread libraries on Linux
|
||||
if(UNIX AND NOT APPLE)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(squawk PRIVATE Threads::Threads)
|
||||
endif()
|
||||
|
||||
## Build type
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
endif ()
|
||||
|
||||
message("Build type: ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set (COMPILE_OPTIONS -fno-sized-deallocation) # for eliminating _ZdlPvm
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
list(APPEND COMPILE_OPTIONS -O3)
|
||||
endif()
|
||||
if (CMAKE_BUILD_TYPE STREQUAL Debug)
|
||||
list(APPEND COMPILE_OPTIONS -O0)
|
||||
list(APPEND COMPILE_OPTIONS -g3)
|
||||
list(APPEND COMPILE_OPTIONS -Wall)
|
||||
list(APPEND COMPILE_OPTIONS -Wextra)
|
||||
endif()
|
||||
|
||||
message("Compilation options: " ${COMPILE_OPTIONS})
|
||||
target_compile_options(squawk PRIVATE ${COMPILE_OPTIONS})
|
||||
endif(CMAKE_COMPILER_IS_GNUCXX)
|
||||
|
||||
# I am not really sure about this solution
|
||||
# This should enable plugins to be found in path like /usr/lib/squawk instead of just /usr/lib
|
||||
set(PLUGIN_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/squawk")
|
||||
add_compile_definitions(PLUGIN_PATH="${PLUGIN_PATH}")
|
||||
|
||||
add_subdirectory(main)
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(packaging)
|
||||
add_subdirectory(plugins)
|
||||
add_subdirectory(resources)
|
||||
add_subdirectory(shared)
|
||||
add_subdirectory(translations)
|
||||
add_subdirectory(ui)
|
||||
|
||||
## Install the executable
|
||||
install(TARGETS squawk DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_DATADIR}/Macaw/Squawk/l10n)
|
||||
install(FILES squawk.svg DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps)
|
||||
install(FILES squawk48.png DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps RENAME squawk.png)
|
||||
install(FILES squawk64.png DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/64x64/apps RENAME squawk.png)
|
||||
install(FILES squawk128.png DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/128x128/apps RENAME squawk.png)
|
||||
install(FILES squawk256.png DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/256x256/apps RENAME squawk.png)
|
||||
install(FILES squawk.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
|
||||
install(FILES README.md DESTINATION ${CMAKE_INSTALL_DATADIR}/macaw.me/squawk)
|
||||
install(FILES LICENSE.md DESTINATION ${CMAKE_INSTALL_DATADIR}/macaw.me/squawk)
|
||||
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
if (APPLE)
|
||||
add_custom_command(TARGET squawk POST_BUILD COMMENT "Running macdeployqt..."
|
||||
COMMAND "${Qt${QT_VERSION_MAJOR}Widgets_DIR}/../../../bin/macdeployqt" "${CMAKE_CURRENT_BINARY_DIR}/squawk.app"
|
||||
)
|
||||
endif(APPLE)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -595,17 +595,17 @@ pointer to where the full notice is found.
|
|||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
|
112
README.md
112
README.md
|
@ -1,31 +1,115 @@
|
|||
# Sqwawk
|
||||
# Squawk - a compact XMPP desktop messenger
|
||||
|
||||
A compact XMPP desktop messenger
|
||||
[](https://git.macaw.me/blue/squawk/raw/branch/master/LICENSE.md)
|
||||
[](https://aur.archlinux.org/packages/squawk/)
|
||||
[](https://liberapay.com/macaw.me)
|
||||
|
||||

|
||||
|
||||
### Prerequisites
|
||||
|
||||
- QT 5.12 *(lower versions might work but it wasn't tested)*
|
||||
- uuid _(usually included in some other package, for example it's ***libutil-linux*** in archlinux)_
|
||||
- lmdb
|
||||
- CMake 3.0 or higher
|
||||
- QT 5 or 6
|
||||
- CMake 3.10 or higher
|
||||
- qxmpp 1.1.0 or higher
|
||||
- LMDBAL (my own [library](https://git.macaw.me/blue/lmdbal) for lmdb)
|
||||
- KDE Frameworks: kwallet (optional)
|
||||
- KDE Frameworks: KIO (optional)
|
||||
- KDE Frameworks: KConfig (optional)
|
||||
- KDE Frameworks: KConfigWidgets (optional)
|
||||
- Boost (just one little hpp from there)
|
||||
- Imagemagick (for compilation, to rasterize an SVG logo)
|
||||
|
||||
### Getting
|
||||
|
||||
The easiest way to get the Squawk is to install it from AUR (if you use Archlinux like distribution)
|
||||
|
||||
Here is the [link](https://aur.archlinux.org/packages/squawk/) for the AUR package
|
||||
|
||||
You can also install it from console if you use some AUR wrapper. Here what it's going to look like with *pacaur*
|
||||
|
||||
```
|
||||
$ pacaur -S squawk
|
||||
```
|
||||
|
||||
### Building
|
||||
|
||||
You can also the repo and build it from source
|
||||
|
||||
Squawk requires Qt with SSL enabled. It uses CMake as build system.
|
||||
|
||||
Squawk uses upstream version of QXmpp library so first we need to pull it
|
||||
Please check the prerequisites and install them before installation.
|
||||
|
||||
---
|
||||
|
||||
There are several ways to build Squawk. The one you need depends on whether you have `qxmpp` and `lmdbal` installed in your system.
|
||||
|
||||
#### Building with system dependencies
|
||||
|
||||
This is the easiest way but it requires you to have `qxmpp` and `lmdbal` installed as system packages. Here is what you do:
|
||||
|
||||
```
|
||||
git submodule update --init --recursive
|
||||
$ git clone https://git.macaw.me/blue/squawk
|
||||
$ cd squawk
|
||||
$ mkdir build
|
||||
$ cd build
|
||||
$ cmake ..
|
||||
$ cmake --build .
|
||||
```
|
||||
Then create a folder for the build, go there and build the project using CMake
|
||||
|
||||
|
||||
#### Building with bundled qxmpp
|
||||
|
||||
If you don't have any of `qxmpp` or `lmdbal` (or both) installed the process is abit mor complicated.
|
||||
On the configuration stage you need to enable one or both entries in the square brackets, depending on what package your system lacks.
|
||||
|
||||
Here is what you do
|
||||
|
||||
```
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
$ git clone --recurse-submodules https://git.macaw.me/blue/squawk
|
||||
$ cd squawk
|
||||
$ mkdir build
|
||||
$ cd build
|
||||
$ cmake .. [-D SYSTEM_QXMPP=False] [-D SYSTEM_LMDBAL=False]
|
||||
$ cmake --build .
|
||||
```
|
||||
|
||||
#### For Windows (Mingw-w64) build
|
||||
|
||||
**Building for windows is not mainteined, but was possible in the past, you can try, but it probably won't work**
|
||||
|
||||
You need Qt for mingw64 (MinGW 64-bit) platform when installing Qt.
|
||||
|
||||
The best way to acquire library `lmdb` and `boost` is through Msys2.
|
||||
|
||||
First install Msys2, and then install `mingw-w64-x86_64-lmdb` and `mingw-w64-x86_64-boost` by pacman.
|
||||
|
||||
Then you need to provide the cmake cache entry when calling cmake for configuration:
|
||||
|
||||
`<Msys2 Mingw64 Root Directory>`: e.g. `C:/msys64/mingw64`.
|
||||
|
||||
```
|
||||
$ git clone --recurse-submodules https://git.macaw.me/blue/squawk
|
||||
$ cd squawk
|
||||
$ mkdir build
|
||||
$ cd build
|
||||
$ cmake .. -D SYSTEM_QXMPP=False -D SYSTEM_LMDBAL=False -D LMDB_ROOT_DIR:PATH=<Msys2 Mingw64 Root Directory> -D BOOST_ROOT:PATH=<Msys2 Mingw64 Root Directory>
|
||||
$ cmake --build .
|
||||
```
|
||||
|
||||
You can always refer to `appveyor.yml` to see how AppVeyor build squawk for windows.
|
||||
|
||||
### List of keys
|
||||
|
||||
Here is the list of keys you can pass to configuration phase of `cmake ..`:
|
||||
|
||||
- `CMAKE_BUILD_TYPE` - `Debug` just builds showing all warnings, `Release` builds with no warnings and applies optimizations (default is `Debug`)
|
||||
- `SYSTEM_QXMPP` - `True` tries to link against `qxmpp` installed in the system, `False` builds bundled `qxmpp` library (default is `True`)
|
||||
- `SYSTEM_LMDBAL` - `True` tries to link against `LMDABL` installed in the system, `False` builds bundled `LMDBAL` library (default is `True`)
|
||||
- `WITH_KWALLET` - `True` builds the `KWallet` capability module if `KWallet` is installed and if not goes to `False`. `False` disables `KWallet` support (default is `True`)
|
||||
- `WITH_KIO` - `True` builds the `KIO` capability module if `KIO` is installed and if not goes to `False`. `False` disables `KIO` support (default is `True`)
|
||||
- `WITH_KCONFIG` - `True` builds the `KConfig` and `KConfigWidgets` capability module if such packages are installed and if not goes to `False`. `False` disables `KConfig` and `KConfigWidgets` support (default is `True`)
|
||||
- `WITH_OMEMO` - `True` builds the OMEMO encryption, requires `qxmpp` of version >= 1.5.0 built with OMEMO support. `False` disables OMEMO support (default is `False`)
|
||||
- `QT_VERSION_MAJOR` - `6` builds against Qt 6, `5` builds against Qt 6, corresponding version of lmdbal and qxmpp should be installed (default is `6`)
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the GPLv3 License - see the [LICENSE.md](LICENSE.md) file for details
|
||||
|
|
117
appveyor.yml
Normal file
117
appveyor.yml
Normal file
|
@ -0,0 +1,117 @@
|
|||
image:
|
||||
- Visual Studio 2019
|
||||
- "Previous Ubuntu1804"
|
||||
- macOS-Mojave
|
||||
|
||||
branches:
|
||||
except:
|
||||
- gh-pages
|
||||
|
||||
for:
|
||||
-
|
||||
matrix:
|
||||
only:
|
||||
- image: Visual Studio 2019
|
||||
|
||||
environment:
|
||||
QTDIR: C:\Qt\5.15.2\mingw81_64
|
||||
QTTOOLDIR: C:\Qt\Tools\mingw810_64\bin
|
||||
QTNINJADIR: C:\Qt\Tools\Ninja
|
||||
|
||||
install:
|
||||
- set PATH=%QTTOOLDIR%;%QTNINJADIR%;%QTDIR%\bin;%PATH%
|
||||
- git submodule update --init --recursive
|
||||
|
||||
before_build:
|
||||
- choco install --yes zstandard
|
||||
- choco install --yes --version=7.1.0.2 imagemagick.app
|
||||
- set PATH=C:\Program Files\ImageMagick-7.1.0-Q16-HDRI;%PATH%
|
||||
|
||||
- mkdir lmdb
|
||||
- cd lmdb
|
||||
- curl -OL https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lmdb-0.9.27-1-any.pkg.tar.zst
|
||||
- zstd -d ./mingw-w64-x86_64-lmdb-0.9.27-1-any.pkg.tar.zst
|
||||
- tar -xvf ./mingw-w64-x86_64-lmdb-0.9.27-1-any.pkg.tar
|
||||
- cd ..
|
||||
|
||||
- mkdir boost
|
||||
- cd boost
|
||||
- curl -OL https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.77.0-2-any.pkg.tar.zst
|
||||
- zstd -d ./mingw-w64-x86_64-boost-1.77.0-2-any.pkg.tar.zst
|
||||
- tar -xvf ./mingw-w64-x86_64-boost-1.77.0-2-any.pkg.tar
|
||||
- cd ..
|
||||
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake -GNinja -DCMAKE_BUILD_TYPE:String=Release -DCMAKE_PREFIX_PATH:STRING=%QTDIR% -DLMDB_ROOT_DIR:PATH=C:/projects/squawk/lmdb/mingw64 -DBOOST_ROOT:PATH=C:/projects/squawk/boost/mingw64 ..
|
||||
|
||||
build_script:
|
||||
- cmake --build .
|
||||
- mkdir deploy
|
||||
- cd deploy
|
||||
- copy ..\squawk.exe .\
|
||||
- copy ..\external\qxmpp\src\libqxmpp.dll .\
|
||||
- windeployqt .\squawk.exe
|
||||
- windeployqt .\libqxmpp.dll
|
||||
- cd ..\..
|
||||
|
||||
artifacts:
|
||||
- path: build/deploy/squawk.exe
|
||||
name: Squawk executable (Qt 5.15.2)
|
||||
|
||||
- path: build/deploy
|
||||
name: Squawk deployment with Qt Framework
|
||||
|
||||
-
|
||||
matrix:
|
||||
only:
|
||||
- image: macOS-Mojave
|
||||
|
||||
install:
|
||||
- brew install lmdb imagemagick boost
|
||||
- git submodule update --init --recursive
|
||||
|
||||
before_build:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake -DCMAKE_BUILD_TYPE:String=Release -DCMAKE_PREFIX_PATH:STRING=$HOME/Qt/5.15.2/clang_64 ..
|
||||
|
||||
build_script:
|
||||
- cmake --build .
|
||||
|
||||
after_build:
|
||||
- zip -r squawk.app.zip squawk.app
|
||||
|
||||
artifacts:
|
||||
- path: build/squawk.app/Contents/MacOS/squawk
|
||||
name: Squawk executable (Qt 5.15.2)
|
||||
- path: build/external/qxmpp/src/
|
||||
name: QXMPP
|
||||
- path: build/squawk.app.zip
|
||||
name: Squawk Bundle with Qt Framework (Qt 5.15.2)
|
||||
|
||||
-
|
||||
matrix:
|
||||
only:
|
||||
- image: "Previous Ubuntu1804"
|
||||
|
||||
install:
|
||||
- ls $HOME/Qt
|
||||
- sudo apt update
|
||||
- sudo apt install -y liblmdb-dev liblmdb0 imagemagick mesa-common-dev libglu1-mesa-dev libboost-all-dev
|
||||
- git submodule update --init --recursive
|
||||
|
||||
before_build:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake -DCMAKE_BUILD_TYPE:String=Release -DCMAKE_PREFIX_PATH:STRING=$HOME/Qt/5.12.10/gcc_64 -DCMAKE_BUILD_RPATH="\$ORIGIN" ..
|
||||
|
||||
build_script:
|
||||
- cmake --build .
|
||||
|
||||
after_build:
|
||||
- zip -r squawk.zip squawk -j external/qxmpp/src/libqxmpp*
|
||||
|
||||
artifacts:
|
||||
- path: build/squawk.zip
|
||||
name: Squawk executable and libraries (Qt 5.12)
|
41
cmake/MacOSXBundleInfo.plist.in
Normal file
41
cmake/MacOSXBundleInfo.plist.in
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
|
||||
<key>CFBundleGetInfoString</key>
|
||||
<string>${MACOSX_BUNDLE_INFO_STRING}</string>
|
||||
<key>CFBundleIconFile</key>
|
||||
<string>${MACOSX_BUNDLE_ICON_FILE}</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>${MACOSX_BUNDLE_GUI_IDENTIFIER}</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleLongVersionString</key>
|
||||
<string>${MACOSX_BUNDLE_LONG_VERSION_STRING}</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>${MACOSX_BUNDLE_SHORT_VERSION_STRING}</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>${MACOSX_BUNDLE_BUNDLE_VERSION}</string>
|
||||
<key>CSResourcesFileMapped</key>
|
||||
<true/>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>${MACOSX_BUNDLE_COPYRIGHT}</string>
|
||||
|
||||
<key>NSPrincipalClass</key>
|
||||
<string>NSApplication</string>
|
||||
<key>NSHighResolutionCapable</key>
|
||||
<string>True</string>
|
||||
|
||||
</dict>
|
||||
</plist>
|
|
@ -1,34 +1,34 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(squawkCORE)
|
||||
set(SIGNALCATCHER_SOURCE signalcatcher.cpp)
|
||||
if(WIN32)
|
||||
set(SIGNALCATCHER_SOURCE signalcatcher_win32.cpp)
|
||||
endif(WIN32)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
find_package(Qt5Widgets CONFIG REQUIRED)
|
||||
find_package(Qt5Network CONFIG REQUIRED)
|
||||
|
||||
set(squawkCORE_SRC
|
||||
squawk.cpp
|
||||
set(SOURCE_FILES
|
||||
account.cpp
|
||||
archive.cpp
|
||||
rosteritem.cpp
|
||||
contact.cpp
|
||||
adapterfunctions.cpp
|
||||
conference.cpp
|
||||
storage.cpp
|
||||
networkaccess.cpp
|
||||
contact.cpp
|
||||
rosteritem.cpp
|
||||
${SIGNALCATCHER_SOURCE}
|
||||
squawk.cpp
|
||||
)
|
||||
|
||||
# Tell CMake to create the helloworld executable
|
||||
add_library(squawkCORE ${squawkCORE_SRC})
|
||||
set(HEADER_FILES
|
||||
account.h
|
||||
adapterfunctions.h
|
||||
conference.h
|
||||
contact.h
|
||||
rosteritem.h
|
||||
signalcatcher.h
|
||||
squawk.h
|
||||
)
|
||||
|
||||
if(SYSTEM_QXMPP)
|
||||
find_package(QXmpp CONFIG REQUIRED)
|
||||
get_target_property(QXMPP_INTERFACE_INCLUDE_DIRECTORIES QXmpp::QXmpp INTERFACE_INCLUDE_DIRECTORIES)
|
||||
target_include_directories(squawkCORE PUBLIC ${QXMPP_INTERFACE_INCLUDE_DIRECTORIES})
|
||||
endif()
|
||||
target_sources(squawk PRIVATE ${SOURCE_FILES})
|
||||
|
||||
target_include_directories(squawk PRIVATE ${LMDB_INCLUDE_DIRS})
|
||||
|
||||
# Use the Widgets module from Qt 5.
|
||||
target_link_libraries(squawkCORE Qt5::Core)
|
||||
target_link_libraries(squawkCORE Qt5::Network)
|
||||
target_link_libraries(squawkCORE qxmpp)
|
||||
target_link_libraries(squawkCORE lmdb)
|
||||
add_subdirectory(handlers)
|
||||
add_subdirectory(passwordStorageEngines)
|
||||
add_subdirectory(components)
|
||||
add_subdirectory(delayManager)
|
||||
add_subdirectory(utils)
|
||||
|
|
1516
core/account.cpp
1516
core/account.cpp
File diff suppressed because it is too large
Load diff
218
core/account.h
218
core/account.h
|
@ -15,46 +15,102 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifndef CORE_ACCOUNT_H
|
||||
#define CORE_ACCOUNT_H
|
||||
#include <QObject>
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
#include <QMimeDatabase>
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QTimer>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <list>
|
||||
|
||||
#include <QXmppRosterManager.h>
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
||||
#include <QXmppCarbonManagerV2.h>
|
||||
#else
|
||||
#include <QXmppCarbonManager.h>
|
||||
#endif
|
||||
#include <QXmppDiscoveryManager.h>
|
||||
#include <QXmppMamManager.h>
|
||||
#include <QXmppMucManager.h>
|
||||
#include <QXmppClient.h>
|
||||
#include <QXmppBookmarkManager.h>
|
||||
#include <QXmppBookmarkSet.h>
|
||||
#include "../global.h"
|
||||
#include <QXmppUploadRequestManager.h>
|
||||
#include <QXmppVCardManager.h>
|
||||
#include <QXmppMessageReceiptManager.h>
|
||||
#include <QXmppPubSubManager.h>
|
||||
|
||||
#include <shared/shared.h>
|
||||
#include <shared/identity.h>
|
||||
#include <shared/info.h>
|
||||
#include <shared/clientid.h>
|
||||
#include "contact.h"
|
||||
#include "conference.h"
|
||||
#include <core/components/networkaccess.h>
|
||||
#include <core/delayManager/manager.h>
|
||||
|
||||
namespace Core
|
||||
{
|
||||
#include "handlers/messagehandler.h"
|
||||
#include "handlers/rosterhandler.h"
|
||||
#include "handlers/vcardhandler.h"
|
||||
#include "handlers/discoveryhandler.h"
|
||||
|
||||
class Account : public QObject
|
||||
{
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
||||
#include <QXmppTrustManager.h>
|
||||
#ifdef WITH_OMEMO
|
||||
#include <QXmppOmemoManager.h>
|
||||
#include "handlers/omemohandler.h"
|
||||
#endif
|
||||
#include "handlers/trusthandler.h"
|
||||
#endif
|
||||
|
||||
namespace Core {
|
||||
|
||||
class Account : public QObject {
|
||||
Q_OBJECT
|
||||
friend class MessageHandler;
|
||||
friend class RosterHandler;
|
||||
friend class VCardHandler;
|
||||
friend class DiscoveryHandler;
|
||||
#ifdef WITH_OMEMO
|
||||
friend class OmemoHandler;
|
||||
friend class TrustHandler;
|
||||
#endif
|
||||
public:
|
||||
Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent = 0);
|
||||
enum class Error {
|
||||
authentication,
|
||||
other,
|
||||
none
|
||||
};
|
||||
|
||||
Account(
|
||||
const QString& p_login,
|
||||
const QString& p_server,
|
||||
const QString& p_password,
|
||||
const QString& p_name,
|
||||
bool p_active,
|
||||
NetworkAccess* p_net,
|
||||
QObject* parent = 0);
|
||||
~Account();
|
||||
|
||||
void connect();
|
||||
void disconnect();
|
||||
void reconnect();
|
||||
|
||||
Shared::ConnectionState getState() const;
|
||||
QString getName() const;
|
||||
QString getLogin() const;
|
||||
QString getServer() const;
|
||||
QString getPassword() const;
|
||||
QString getResource() const;
|
||||
QString getAvatarPath() const;
|
||||
QString getBareJid() const;
|
||||
QString getFullJid() const;
|
||||
Shared::Availability getAvailability() const;
|
||||
Shared::AccountPassword getPasswordType() const;
|
||||
Error getLastError() const;
|
||||
bool getActive() const;
|
||||
|
||||
void setName(const QString& p_name);
|
||||
void setLogin(const QString& p_login);
|
||||
|
@ -62,10 +118,10 @@ public:
|
|||
void setPassword(const QString& p_password);
|
||||
void setResource(const QString& p_resource);
|
||||
void setAvailability(Shared::Availability avail);
|
||||
QString getFullJid() const;
|
||||
void setPasswordType(Shared::AccountPassword pt);
|
||||
void sendMessage(const Shared::Message& data);
|
||||
void setActive(bool p_active);
|
||||
void requestArchive(const QString& jid, int count, const QString& before);
|
||||
void setReconnectTimes(unsigned int times);
|
||||
void subscribeToContact(const QString& jid, const QString& reason);
|
||||
void unsubscribeFromContact(const QString& jid, const QString& reason);
|
||||
void removeContactRequest(const QString& jid);
|
||||
|
@ -73,15 +129,30 @@ public:
|
|||
void addContactToGroupRequest(const QString& jid, const QString& groupName);
|
||||
void removeContactFromGroupRequest(const QString& jid, const QString& groupName);
|
||||
void renameContactRequest(const QString& jid, const QString& newName);
|
||||
void requestChangeMessage(const QString& jid, const QString& messageId, const QMap<QString, QVariant>& data);
|
||||
void setContactEncryption(const QString& jid, Shared::EncryptionProtocol value);
|
||||
|
||||
void setRoomJoined(const QString& jid, bool joined);
|
||||
void setRoomAutoJoin(const QString& jid, bool joined);
|
||||
void removeRoomRequest(const QString& jid);
|
||||
void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin);
|
||||
void updateInfo(const Shared::Info& info);
|
||||
void resendMessage(const QString& jid, const QString& id);
|
||||
void replaceMessage(const QString& originalId, const Shared::Message& data);
|
||||
void invalidatePassword();
|
||||
|
||||
void discoverInfo(const QString& address, const QString& node);
|
||||
|
||||
public slots:
|
||||
void connect();
|
||||
void disconnect();
|
||||
void reconnect();
|
||||
void requestInfo(const QString& jid);
|
||||
|
||||
signals:
|
||||
void connectionStateChanged(int);
|
||||
void availabilityChanged(int);
|
||||
void changed(const QMap<QString, QVariant>& data);
|
||||
void connectionStateChanged(Shared::ConnectionState);
|
||||
void availabilityChanged(Shared::Availability);
|
||||
void addGroup(const QString& name);
|
||||
void removeGroup(const QString& name);
|
||||
void addRoom(const QString& jid, const QMap<QString, QVariant>& data);
|
||||
|
@ -94,91 +165,84 @@ signals:
|
|||
void addPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
|
||||
void removePresence(const QString& jid, const QString& name);
|
||||
void message(const Shared::Message& data);
|
||||
void responseArchive(const QString& jid, const std::list<Shared::Message>& list);
|
||||
void changeMessage(const QString& jid, const QString& id, const QMap<QString, QVariant>& data);
|
||||
void responseArchive(const QString& jid, const std::list<Shared::Message>& list, bool last);
|
||||
void error(const QString& text);
|
||||
void addRoomParticipant(const QString& jid, const QString& nickName, const QMap<QString, QVariant>& data);
|
||||
void changeRoomParticipant(const QString& jid, const QString& nickName, const QMap<QString, QVariant>& data);
|
||||
void removeRoomParticipant(const QString& jid, const QString& nickName);
|
||||
void infoReady(const Shared::Info& info);
|
||||
void uploadFile(const QFileInfo& file, const QUrl& set, const QUrl& get, QMap<QString, QString> headers);
|
||||
void uploadFileError(const QString& jid, const QString& messageId, const QString& error);
|
||||
void needPassword();
|
||||
void infoDiscovered(const QString& address, const QString& node, const std::set<Shared::Identity>& identities, const std::set<QString>& features);
|
||||
void pepSupportChanged(Shared::Support support);
|
||||
|
||||
private:
|
||||
QString name;
|
||||
std::map<QString, QString> achiveQueries;
|
||||
std::map<QString, QString> archiveQueries;
|
||||
QXmppClient client;
|
||||
QXmppConfiguration config;
|
||||
QXmppPresence presence;
|
||||
Shared::ConnectionState state;
|
||||
std::map<QString, std::set<QString>> groups;
|
||||
|
||||
MessageHandler* mh;
|
||||
RosterHandler* rh;
|
||||
VCardHandler* vh;
|
||||
DiscoveryHandler* dh;
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
||||
TrustHandler* th;
|
||||
#endif
|
||||
#ifdef WITH_OMEMO
|
||||
OmemoHandler* oh;
|
||||
|
||||
QXmppOmemoManager* om;
|
||||
#endif
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
|
||||
QXmppTrustManager* tm;
|
||||
QXmppCarbonManagerV2* cm;
|
||||
QXmppPubSubManager* psm;
|
||||
#else
|
||||
QXmppCarbonManager* cm;
|
||||
#endif
|
||||
QXmppMamManager* am;
|
||||
QXmppMucManager* mm;
|
||||
QXmppBookmarkManager* bm;
|
||||
std::map<QString, Contact*> contacts;
|
||||
std::map<QString, Conference*> conferences;
|
||||
unsigned int maxReconnectTimes;
|
||||
unsigned int reconnectTimes;
|
||||
QXmppRosterManager* rm;
|
||||
QXmppVCardManager* vm;
|
||||
QXmppUploadRequestManager* um;
|
||||
QXmppDiscoveryManager* dm;
|
||||
QXmppMessageReceiptManager* rcpm;
|
||||
bool reconnectScheduled;
|
||||
QTimer* reconnectTimer;
|
||||
|
||||
std::map<QString, QString> queuedContacts;
|
||||
std::set<QString> outOfRosterContacts;
|
||||
NetworkAccess* network;
|
||||
DelayManager::Manager* delay;
|
||||
Shared::AccountPassword passwordType;
|
||||
Error lastError;
|
||||
Shared::Support pepSupport;
|
||||
bool active;
|
||||
bool notReadyPassword;
|
||||
bool loadingOmemo;
|
||||
|
||||
private slots:
|
||||
void onClientConnected();
|
||||
void onClientDisconnected();
|
||||
void onClientStateChange(QXmppClient::State state);
|
||||
void onClientError(QXmppClient::Error err);
|
||||
|
||||
void onRosterReceived();
|
||||
void onRosterItemAdded(const QString& bareJid);
|
||||
void onRosterItemChanged(const QString& bareJid);
|
||||
void onRosterItemRemoved(const QString& bareJid);
|
||||
void onRosterPresenceChanged(const QString& bareJid, const QString& resource);
|
||||
|
||||
void onPresenceReceived(const QXmppPresence& presence);
|
||||
void onMessageReceived(const QXmppMessage& message);
|
||||
|
||||
void onCarbonMessageReceived(const QXmppMessage& message);
|
||||
void onCarbonMessageSent(const QXmppMessage& message);
|
||||
|
||||
void onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at);
|
||||
|
||||
void onMamMessageReceived(const QString& bareJid, const QXmppMessage& message);
|
||||
void onMamResultsReceived(const QString &queryId, const QXmppResultSetReply &resultSetReply, bool complete);
|
||||
|
||||
void onMucRoomAdded(QXmppMucRoom* room);
|
||||
void onMucJoinedChanged(bool joined);
|
||||
void onMucAutoJoinChanged(bool autoJoin);
|
||||
void onMucNickNameChanged(const QString& nickName);
|
||||
void onMucSubjectChanged(const QString& subject);
|
||||
void onMucAddParticipant(const QString& nickName, const QMap<QString, QVariant>& data);
|
||||
void onMucChangeParticipant(const QString& nickName, const QMap<QString, QVariant>& data);
|
||||
void onMucRemoveParticipant(const QString& nickName);
|
||||
|
||||
void bookmarksReceived(const QXmppBookmarkSet& bookmarks);
|
||||
|
||||
void onContactGroupAdded(const QString& group);
|
||||
void onContactGroupRemoved(const QString& group);
|
||||
void onContactNameChanged(const QString& name);
|
||||
void onContactSubscriptionStateChanged(Shared::SubscriptionState state);
|
||||
void onContactHistoryResponse(const std::list<Shared::Message>& list);
|
||||
void onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at);
|
||||
|
||||
|
||||
void onMamLog(QXmppLogger::MessageType type, const QString &msg);
|
||||
|
||||
void onContactHistoryResponse(const std::list<Shared::Message>& list, bool last);
|
||||
|
||||
private:
|
||||
void addedAccount(const QString &bareJid);
|
||||
void handleNewContact(Contact* contact);
|
||||
void handleNewRosterItem(RosterItem* contact);
|
||||
void handleNewConference(Conference* contact);
|
||||
bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
|
||||
bool handleGroupMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
|
||||
void addNewRoom(const QString& jid, const QString& nick, const QString& roomName, bool autoJoin);
|
||||
void addToGroup(const QString& jid, const QString& group);
|
||||
void removeFromGroup(const QString& jid, const QString& group);
|
||||
void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const;
|
||||
Shared::SubscriptionState castSubscriptionState(QXmppRosterIq::Item::SubscriptionType qs) const;
|
||||
void logMessage(const QXmppMessage& msg, const QString& reason = "Message wasn't handled: ");
|
||||
void storeConferences();
|
||||
void clearConferences();
|
||||
|
||||
void handleDisconnection();
|
||||
void onReconnectTimer();
|
||||
void setPepSupport(Shared::Support support);
|
||||
void runDiscoveryService();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // CORE_ACCOUNT_H
|
||||
|
|
271
core/adapterfunctions.cpp
Normal file
271
core/adapterfunctions.cpp
Normal file
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "adapterfunctions.h"
|
||||
|
||||
void Core::initializeVCard(Shared::VCard& vCard, const QXmppVCardIq& card)
|
||||
{
|
||||
vCard.setFullName(card.fullName());
|
||||
vCard.setFirstName(card.firstName());
|
||||
vCard.setMiddleName(card.middleName());
|
||||
vCard.setLastName(card.lastName());
|
||||
vCard.setBirthday(card.birthday());
|
||||
vCard.setNickName(card.nickName());
|
||||
vCard.setDescription(card.description());
|
||||
vCard.setUrl(card.url());
|
||||
QXmppVCardOrganization org = card.organization();
|
||||
vCard.setOrgName(org.organization());
|
||||
vCard.setOrgRole(org.role());
|
||||
vCard.setOrgUnit(org.unit());
|
||||
vCard.setOrgTitle(org.title());
|
||||
|
||||
QList<QXmppVCardEmail> emails = card.emails();
|
||||
std::deque<Shared::VCard::Email>& myEmails = vCard.getEmails();
|
||||
for (const QXmppVCardEmail& em : emails) {
|
||||
QString addr = em.address();
|
||||
if (addr.size() != 0) {
|
||||
QXmppVCardEmail::Type et = em.type();
|
||||
bool prefered = false;
|
||||
bool accounted = false;
|
||||
if (et & QXmppVCardEmail::Preferred) {
|
||||
prefered = true;
|
||||
}
|
||||
if (et & QXmppVCardEmail::Home) {
|
||||
myEmails.emplace_back(addr, Shared::VCard::Email::home, prefered);
|
||||
accounted = true;
|
||||
}
|
||||
if (et & QXmppVCardEmail::Work) {
|
||||
myEmails.emplace_back(addr, Shared::VCard::Email::work, prefered);
|
||||
accounted = true;
|
||||
}
|
||||
if (!accounted) {
|
||||
myEmails.emplace_back(addr, Shared::VCard::Email::none, prefered);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QList<QXmppVCardPhone> phones = card.phones();
|
||||
std::deque<Shared::VCard::Phone>& myPhones = vCard.getPhones();
|
||||
for (const QXmppVCardPhone& ph : phones) {
|
||||
QString num = ph.number();
|
||||
if (num.size() != 0) {
|
||||
QXmppVCardPhone::Type pt = ph.type();
|
||||
bool prefered = false;
|
||||
bool accounted = false;
|
||||
if (pt & QXmppVCardPhone::Preferred) {
|
||||
prefered = true;
|
||||
}
|
||||
|
||||
bool home = false;
|
||||
bool work = false;
|
||||
|
||||
if (pt & QXmppVCardPhone::Home) {
|
||||
home = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Work) {
|
||||
work = true;
|
||||
}
|
||||
|
||||
|
||||
if (pt & QXmppVCardPhone::Fax) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::fax, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::fax, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::fax, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Voice) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::voice, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::voice, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::voice, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Pager) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::pager, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::pager, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::pager, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Cell) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::cell, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::cell, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::cell, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Video) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::video, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::video, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::video, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (pt & QXmppVCardPhone::Modem) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::modem, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::modem, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::modem, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
accounted = true;
|
||||
}
|
||||
if (!accounted) {
|
||||
if (home || work) {
|
||||
if (home) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::other, Shared::VCard::Phone::home, prefered);
|
||||
}
|
||||
if (work) {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::other, Shared::VCard::Phone::work, prefered);
|
||||
}
|
||||
} else {
|
||||
myPhones.emplace_back(num, Shared::VCard::Phone::other, Shared::VCard::Phone::none, prefered);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::initializeQXmppVCard(QXmppVCardIq& iq, const Shared::VCard& card) {
|
||||
iq.setFullName(card.getFullName());
|
||||
iq.setFirstName(card.getFirstName());
|
||||
iq.setMiddleName(card.getMiddleName());
|
||||
iq.setLastName(card.getLastName());
|
||||
iq.setNickName(card.getNickName());
|
||||
iq.setBirthday(card.getBirthday());
|
||||
iq.setDescription(card.getDescription());
|
||||
iq.setUrl(card.getUrl());
|
||||
QXmppVCardOrganization org;
|
||||
org.setOrganization(card.getOrgName());
|
||||
org.setUnit(card.getOrgUnit());
|
||||
org.setRole(card.getOrgRole());
|
||||
org.setTitle(card.getOrgTitle());
|
||||
iq.setOrganization(org);
|
||||
|
||||
const std::deque<Shared::VCard::Email>& myEmails = card.getEmails();
|
||||
QList<QXmppVCardEmail> emails;
|
||||
for (const Shared::VCard::Email& mEm : myEmails) {
|
||||
QXmppVCardEmail em;
|
||||
QXmppVCardEmail::Type t = QXmppVCardEmail::Internet;
|
||||
if (mEm.prefered) {
|
||||
t = t | QXmppVCardEmail::Preferred;
|
||||
}
|
||||
if (mEm.role == Shared::VCard::Email::home) {
|
||||
t = t | QXmppVCardEmail::Home;
|
||||
} else if (mEm.role == Shared::VCard::Email::work) {
|
||||
t = t | QXmppVCardEmail::Work;
|
||||
}
|
||||
em.setType(t);
|
||||
em.setAddress(mEm.address);
|
||||
|
||||
emails.push_back(em);
|
||||
}
|
||||
|
||||
std::map<QString, QXmppVCardPhone> phones;
|
||||
QList<QXmppVCardPhone> phs;
|
||||
const std::deque<Shared::VCard::Phone>& myPhones = card.getPhones();
|
||||
for (const Shared::VCard::Phone& mPh : myPhones) {
|
||||
std::map<QString, QXmppVCardPhone>::iterator itr = phones.find(mPh.number);
|
||||
if (itr == phones.end()) {
|
||||
itr = phones.emplace(mPh.number, QXmppVCardPhone()).first;
|
||||
}
|
||||
QXmppVCardPhone& phone = itr->second;
|
||||
phone.setNumber(mPh.number);
|
||||
|
||||
switch (mPh.type) {
|
||||
case Shared::VCard::Phone::fax:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Fax);
|
||||
break;
|
||||
case Shared::VCard::Phone::pager:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Pager);
|
||||
break;
|
||||
case Shared::VCard::Phone::voice:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Voice);
|
||||
break;
|
||||
case Shared::VCard::Phone::cell:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Cell);
|
||||
break;
|
||||
case Shared::VCard::Phone::video:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Video);
|
||||
break;
|
||||
case Shared::VCard::Phone::modem:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Modem);
|
||||
break;
|
||||
case Shared::VCard::Phone::other:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::PCS); //loss of information, but I don't even know what the heck is this type of phone!
|
||||
break;
|
||||
}
|
||||
|
||||
switch (mPh.role) {
|
||||
case Shared::VCard::Phone::home:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Home);
|
||||
break;
|
||||
case Shared::VCard::Phone::work:
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Work);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (mPh.prefered) {
|
||||
phone.setType(phone.type() | QXmppVCardPhone::Preferred);
|
||||
}
|
||||
}
|
||||
for (const std::pair<const QString, QXmppVCardPhone>& phone : phones) {
|
||||
phs.push_back(phone.second);
|
||||
}
|
||||
|
||||
iq.setEmails(emails);
|
||||
iq.setPhones(phs);
|
||||
}
|
47
core/adapterfunctions.h
Normal file
47
core/adapterfunctions.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef CORE_ADAPTER_FUNCTIONS_H
|
||||
#define CORE_ADAPTER_FUNCTIONS_H
|
||||
|
||||
#include <QXmppVCardIq.h>
|
||||
#include <QXmppTask.h>
|
||||
#include <QXmppPromise.h>
|
||||
#include <shared/vcard.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
void initializeVCard(Shared::VCard& vCard, const QXmppVCardIq& card);
|
||||
void initializeQXmppVCard(QXmppVCardIq& card, const Shared::VCard& vCard);
|
||||
|
||||
template<typename T>
|
||||
QXmppTask<T> makeReadyTask(T &&value) {
|
||||
QXmppPromise<T> promise;
|
||||
promise.finish(std::move(value));
|
||||
return promise.task();
|
||||
}
|
||||
|
||||
inline QXmppTask<void> makeReadyTask() {
|
||||
QXmppPromise<void> promise;
|
||||
promise.finish();
|
||||
return promise.task();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // CORE_ADAPTER_FUNCTIONS_H
|
510
core/archive.cpp
510
core/archive.cpp
|
@ -1,510 +0,0 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "archive.h"
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <QStandardPaths>
|
||||
#include <QDebug>
|
||||
#include <QDataStream>
|
||||
#include <QDir>
|
||||
|
||||
Core::Archive::Archive(const QString& p_jid, QObject* parent):
|
||||
QObject(parent),
|
||||
jid(p_jid),
|
||||
opened(false),
|
||||
fromTheBeginning(false),
|
||||
environment(),
|
||||
main(),
|
||||
order(),
|
||||
stats()
|
||||
{
|
||||
}
|
||||
|
||||
Core::Archive::~Archive()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void Core::Archive::open(const QString& account)
|
||||
{
|
||||
if (!opened) {
|
||||
mdb_env_create(&environment);
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + account + "/" + jid;
|
||||
QDir cache(path);
|
||||
|
||||
if (!cache.exists()) {
|
||||
bool res = cache.mkpath(path);
|
||||
if (!res) {
|
||||
throw Directory(path.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
mdb_env_set_maxdbs(environment, 4);
|
||||
mdb_env_set_mapsize(environment, 512UL * 1024UL * 1024UL);
|
||||
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
|
||||
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
mdb_dbi_open(txn, "main", MDB_CREATE, &main);
|
||||
mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order);
|
||||
mdb_dbi_open(txn, "stats", MDB_CREATE, &stats);
|
||||
mdb_txn_commit(txn);
|
||||
fromTheBeginning = _isFromTheBeginning();
|
||||
opened = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::close()
|
||||
{
|
||||
if (opened) {
|
||||
mdb_dbi_close(environment, stats);
|
||||
mdb_dbi_close(environment, order);
|
||||
mdb_dbi_close(environment, main);
|
||||
mdb_env_close(environment);
|
||||
opened = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Core::Archive::addElement(const Shared::Message& message)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("addElement", jid.toStdString());
|
||||
}
|
||||
QByteArray ba;
|
||||
QDataStream ds(&ba, QIODevice::WriteOnly);
|
||||
message.serialize(ds);
|
||||
quint64 stamp = message.getTime().toMSecsSinceEpoch();
|
||||
const std::string& id = message.getId().toStdString();
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbData.mv_size = ba.size();
|
||||
lmdbData.mv_data = (uint8_t*)ba.data();
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
int rc;
|
||||
rc = mdb_put(txn, main, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
|
||||
if (rc == 0) {
|
||||
MDB_val orderKey;
|
||||
orderKey.mv_size = 8;
|
||||
orderKey.mv_data = (uint8_t*) &stamp;
|
||||
|
||||
rc = mdb_put(txn, order, &orderKey, &lmdbKey, 0);
|
||||
if (rc) {
|
||||
qDebug() << "An element couldn't be inserted into the index" << mdb_strerror(rc);
|
||||
mdb_txn_abort(txn);
|
||||
return false;
|
||||
} else {
|
||||
rc = mdb_txn_commit(txn);
|
||||
if (rc) {
|
||||
qDebug() << "A transaction error: " << mdb_strerror(rc);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "An element couldn't been added to the archive, skipping" << mdb_strerror(rc);
|
||||
mdb_txn_abort(txn);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::clear()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("clear", jid.toStdString());
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
mdb_drop(txn, main, 0);
|
||||
mdb_drop(txn, order, 0);
|
||||
mdb_drop(txn, stats, 0);
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::getElement(const QString& id)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("getElement", jid.toStdString());
|
||||
}
|
||||
|
||||
std::string strKey = id.toStdString();
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = strKey.size();
|
||||
lmdbKey.mv_data = (char*)strKey.c_str();
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
rc = mdb_get(txn, main, &lmdbKey, &lmdbData);
|
||||
if (rc) {
|
||||
qDebug() <<"Get error: " << mdb_strerror(rc);
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(id.toStdString(), jid.toStdString());
|
||||
} else {
|
||||
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
||||
Shared::Message msg;
|
||||
msg.deserialize(ds);
|
||||
mdb_txn_abort(txn);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::newest()
|
||||
{
|
||||
QString id = newestId();
|
||||
return getElement(id);
|
||||
}
|
||||
|
||||
QString Core::Archive::newestId()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("newestId", jid.toStdString());
|
||||
}
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
MDB_cursor* cursor;
|
||||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
|
||||
if (rc) {
|
||||
qDebug() << "Error geting newestId " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw Empty(jid.toStdString());
|
||||
} else {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
return sId.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::Archive::oldestId()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("oldestId", jid.toStdString());
|
||||
}
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
MDB_cursor* cursor;
|
||||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
||||
if (rc) {
|
||||
qDebug() << "Error geting oldestId " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw Empty(jid.toStdString());
|
||||
} else {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
return sId.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::oldest()
|
||||
{
|
||||
return getElement(oldestId());
|
||||
}
|
||||
|
||||
unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messages)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("addElements", jid.toStdString());
|
||||
}
|
||||
|
||||
int success = 0;
|
||||
int rc = 0;
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
std::list<Shared::Message>::const_iterator itr = messages.begin();
|
||||
while (rc == 0 && itr != messages.end()) {
|
||||
const Shared::Message& message = *itr;
|
||||
|
||||
QByteArray ba;
|
||||
QDataStream ds(&ba, QIODevice::WriteOnly);
|
||||
message.serialize(ds);
|
||||
quint64 stamp = message.getTime().toMSecsSinceEpoch();
|
||||
const std::string& id = message.getId().toStdString();
|
||||
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbData.mv_size = ba.size();
|
||||
lmdbData.mv_data = (uint8_t*)ba.data();
|
||||
|
||||
rc = mdb_put(txn, main, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
|
||||
if (rc == 0) {
|
||||
MDB_val orderKey;
|
||||
orderKey.mv_size = 8;
|
||||
orderKey.mv_data = (uint8_t*) &stamp;
|
||||
|
||||
rc = mdb_put(txn, order, &orderKey, &lmdbKey, 0);
|
||||
if (rc) {
|
||||
qDebug() << "An element couldn't be inserted into the index, aborting the transaction" << mdb_strerror(rc);
|
||||
} else {
|
||||
//qDebug() << "element added with id" << message.getId() << "stamp" << message.getTime();
|
||||
success++;
|
||||
}
|
||||
} else {
|
||||
if (rc == MDB_KEYEXIST) {
|
||||
rc = 0;
|
||||
} else {
|
||||
qDebug() << "An element couldn't been added to the archive, aborting the transaction" << mdb_strerror(rc);
|
||||
}
|
||||
}
|
||||
itr++;
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
success = 0;
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
long unsigned int Core::Archive::size() const
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("size", jid.toStdString());
|
||||
}
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
MDB_stat stat;
|
||||
mdb_stat(txn, order, &stat);
|
||||
mdb_txn_abort(txn);
|
||||
return stat.ms_entries;
|
||||
}
|
||||
|
||||
std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("getBefore", jid.toStdString());
|
||||
}
|
||||
std::list<Shared::Message> res;
|
||||
MDB_cursor* cursor;
|
||||
MDB_txn *txn;
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
if (id == "") {
|
||||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
|
||||
if (rc) {
|
||||
qDebug() << "Error getting before" << mdb_strerror(rc) << ", id:" << id;
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
|
||||
throw Empty(jid.toStdString());
|
||||
}
|
||||
} else {
|
||||
std::string stdId(id.toStdString());
|
||||
lmdbKey.mv_size = stdId.size();
|
||||
lmdbKey.mv_data = (char*)stdId.c_str();
|
||||
rc = mdb_get(txn, main, &lmdbKey, &lmdbData);
|
||||
if (rc) {
|
||||
qDebug() <<"Error getting before: no reference message" << mdb_strerror(rc) << ", id:" << id;
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
} else {
|
||||
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
||||
Shared::Message msg;
|
||||
msg.deserialize(ds);
|
||||
quint64 stamp = msg.getTime().toMSecsSinceEpoch();
|
||||
lmdbKey.mv_data = (quint8*)&stamp;
|
||||
lmdbKey.mv_size = 8;
|
||||
|
||||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET);
|
||||
|
||||
if (rc) {
|
||||
qDebug() << "Error getting before: couldn't set " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
} else {
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_PREV);
|
||||
if (rc) {
|
||||
qDebug() << "Error getting before, couldn't prev " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
MDB_val dKey, dData;
|
||||
dKey.mv_size = lmdbData.mv_size;
|
||||
dKey.mv_data = lmdbData.mv_data;
|
||||
rc = mdb_get(txn, main, &dKey, &dData);
|
||||
if (rc) {
|
||||
qDebug() <<"Get error: " << mdb_strerror(rc);
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(sId, jid.toStdString());
|
||||
} else {
|
||||
QByteArray ba((char*)dData.mv_data, dData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
||||
res.emplace_back();
|
||||
Shared::Message& msg = res.back();
|
||||
msg.deserialize(ds);
|
||||
}
|
||||
|
||||
--count;
|
||||
|
||||
} while (count > 0 && mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_PREV) == 0);
|
||||
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Core::Archive::_isFromTheBeginning()
|
||||
{
|
||||
std::string strKey = "beginning";
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = strKey.size();
|
||||
lmdbKey.mv_data = (char*)strKey.c_str();
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
rc = mdb_get(txn, stats, &lmdbKey, &lmdbData);
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
mdb_txn_abort(txn);
|
||||
return false;
|
||||
} else if (rc) {
|
||||
qDebug() <<"isFromTheBeginning error: " << mdb_strerror(rc);
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(strKey, jid.toStdString());
|
||||
} else {
|
||||
uint8_t value = *(uint8_t*)(lmdbData.mv_data);
|
||||
bool is;
|
||||
if (value == 144) {
|
||||
is = false;
|
||||
} else if (value == 72) {
|
||||
is = true;
|
||||
} else {
|
||||
qDebug() <<"isFromTheBeginning error: stored value doesn't match any magic number, the answer is most probably wrong";
|
||||
}
|
||||
mdb_txn_abort(txn);
|
||||
return is;
|
||||
}
|
||||
}
|
||||
|
||||
bool Core::Archive::isFromTheBeginning()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("isFromTheBeginning", jid.toStdString());
|
||||
}
|
||||
return fromTheBeginning;
|
||||
}
|
||||
|
||||
void Core::Archive::setFromTheBeginning(bool is)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("setFromTheBeginning", jid.toStdString());
|
||||
}
|
||||
if (fromTheBeginning != is) {
|
||||
fromTheBeginning = is;
|
||||
const std::string& id = "beginning";
|
||||
uint8_t value = 144;
|
||||
if (is) {
|
||||
value = 72;
|
||||
}
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbData.mv_size = sizeof value;
|
||||
lmdbData.mv_data = &value;
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
int rc;
|
||||
rc = mdb_put(txn, stats, &lmdbKey, &lmdbData, 0);
|
||||
if (rc != 0) {
|
||||
qDebug() << "Couldn't store beginning key into stat database:" << mdb_strerror(rc);
|
||||
mdb_txn_abort(txn);
|
||||
}
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::printOrder()
|
||||
{
|
||||
qDebug() << "Printing order";
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
MDB_cursor* cursor;
|
||||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
||||
|
||||
do {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
qDebug() << QString(sId.c_str());
|
||||
} while (mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT) == 0);
|
||||
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
}
|
||||
|
||||
void Core::Archive::printKeys()
|
||||
{
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
MDB_cursor* cursor;
|
||||
rc = mdb_cursor_open(txn, main, &cursor);
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
||||
|
||||
do {
|
||||
std::string sId((char*)lmdbKey.mv_data, lmdbKey.mv_size);
|
||||
qDebug() << QString(sId.c_str());
|
||||
} while (mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT) == 0);
|
||||
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
}
|
142
core/archive.h
142
core/archive.h
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CORE_ARCHIVE_H
|
||||
#define CORE_ARCHIVE_H
|
||||
|
||||
#include <QObject>
|
||||
#include "../global.h"
|
||||
#include <lmdb.h>
|
||||
#include "../exception.h"
|
||||
#include <list>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class Archive : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Archive(const QString& jid, QObject* parent = 0);
|
||||
~Archive();
|
||||
|
||||
void open(const QString& account);
|
||||
void close();
|
||||
|
||||
bool addElement(const Shared::Message& message);
|
||||
unsigned int addElements(const std::list<Shared::Message>& messages);
|
||||
Shared::Message getElement(const QString& id);
|
||||
Shared::Message oldest();
|
||||
QString oldestId();
|
||||
Shared::Message newest();
|
||||
QString newestId();
|
||||
void clear();
|
||||
long unsigned int size() const;
|
||||
std::list<Shared::Message> getBefore(int count, const QString& id);
|
||||
bool isFromTheBeginning();
|
||||
void setFromTheBeginning(bool is);
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
|
||||
public:
|
||||
class Directory:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Directory(const std::string& p_path):Exception(), path(p_path){}
|
||||
|
||||
std::string getMessage() const{return "Can't create directory for database at " + path;}
|
||||
private:
|
||||
std::string path;
|
||||
};
|
||||
|
||||
class Closed:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Closed(const std::string& op, const std::string& acc):Exception(), operation(op), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "An attempt to perform operation " + operation + " on closed archive for " + account;}
|
||||
private:
|
||||
std::string operation;
|
||||
std::string account;
|
||||
};
|
||||
|
||||
class NotFound:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
NotFound(const std::string& k, const std::string& acc):Exception(), key(k), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "Element for id " + key + " wasn't found in database " + account;}
|
||||
private:
|
||||
std::string key;
|
||||
std::string account;
|
||||
};
|
||||
|
||||
class Empty:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Empty(const std::string& acc):Exception(), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "An attempt to read ordered elements from database " + account + " but it's empty";}
|
||||
private:
|
||||
std::string account;
|
||||
};
|
||||
|
||||
class Exist:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Exist(const std::string& acc, const std::string& p_key):Exception(), account(acc), key(p_key){}
|
||||
|
||||
std::string getMessage() const{return "An attempt to insert element " + key + " to database " + account + " but it already has an element with given id";}
|
||||
private:
|
||||
std::string account;
|
||||
std::string key;
|
||||
};
|
||||
|
||||
class Unknown:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Unknown(const std::string& acc, const std::string& message):Exception(), account(acc), msg(message){}
|
||||
|
||||
std::string getMessage() const{return "Unknown error on database " + account + ": " + msg;}
|
||||
private:
|
||||
std::string account;
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
private:
|
||||
bool opened;
|
||||
bool fromTheBeginning;
|
||||
MDB_env* environment;
|
||||
MDB_dbi main;
|
||||
MDB_dbi order;
|
||||
MDB_dbi stats;
|
||||
|
||||
bool _isFromTheBeginning();
|
||||
void printOrder();
|
||||
void printKeys();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // CORE_ARCHIVE_H
|
15
core/components/CMakeLists.txt
Normal file
15
core/components/CMakeLists.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
set(SOURCE_FILES
|
||||
networkaccess.cpp
|
||||
clientcache.cpp
|
||||
urlstorage.cpp
|
||||
archive.cpp
|
||||
)
|
||||
|
||||
set(HEADER_FILES
|
||||
networkaccess.h
|
||||
clientcache.h
|
||||
urlstorage.h
|
||||
archive.h
|
||||
)
|
||||
|
||||
target_sources(squawk PRIVATE ${SOURCE_FILES})
|
419
core/components/archive.cpp
Normal file
419
core/components/archive.cpp
Normal file
|
@ -0,0 +1,419 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "archive.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
Core::Archive::Archive(const QString& account, const QString& p_jid, QObject* parent):
|
||||
QObject(parent),
|
||||
jid(p_jid),
|
||||
account(account),
|
||||
opened(false),
|
||||
db(account + "/" + jid),
|
||||
messages(db.addStorage<QString, Shared::Message>("messages")),
|
||||
order(db.addStorage<uint64_t, QString>("order", true)),
|
||||
stats(db.addStorage<QString, QVariant>("stats")),
|
||||
avatars(db.addStorage<QString, AvatarInfo>("avatars")),
|
||||
stanzaIdToId(db.addStorage<QString, QString>("stanzaIdToId")),
|
||||
cursor(order->createCursor())
|
||||
{}
|
||||
|
||||
Core::Archive::~Archive() {
|
||||
close();
|
||||
}
|
||||
|
||||
void Core::Archive::open() {
|
||||
db.open();
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
|
||||
AvatarInfo info;
|
||||
bool hasAvatar = false;
|
||||
try {
|
||||
avatars->getRecord(jid, info, txn);
|
||||
hasAvatar = true;
|
||||
} catch (const LMDBAL::NotFound& e) {}
|
||||
|
||||
if (!hasAvatar)
|
||||
return;
|
||||
|
||||
QFile ava(db.getPath() + "/" + jid + "." + info.type);
|
||||
if (ava.exists())
|
||||
return;
|
||||
|
||||
try {
|
||||
avatars->removeRecord(jid, txn);
|
||||
txn.commit();
|
||||
} catch (const std::exception& e) {
|
||||
qDebug() << e.what();
|
||||
qDebug() << "error opening archive" << jid << "for account" << account
|
||||
<< ". There is supposed to be avatar but the file doesn't exist, couldn't even drop it, it surely will lead to an error";
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::close() {
|
||||
db.close();
|
||||
}
|
||||
|
||||
bool Core::Archive::addElement(const Shared::Message& message) {
|
||||
QString id = message.getId();
|
||||
qDebug() << "Adding message with id " << id;
|
||||
|
||||
try {
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
messages->addRecord(id, message, txn);
|
||||
order->addRecord(message.getTime().toMSecsSinceEpoch(), id, txn);
|
||||
QString stanzaId = message.getStanzaId();
|
||||
if (!stanzaId.isEmpty())
|
||||
stanzaIdToId->addRecord(stanzaId, id, txn);
|
||||
|
||||
txn.commit();
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
qDebug() << "Could not add message with id " + id;
|
||||
qDebug() << e.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Core::Archive::clear() {
|
||||
db.drop();
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::getElement(const QString& id) const {
|
||||
return messages->getRecord(id);
|
||||
}
|
||||
|
||||
bool Core::Archive::hasElement(const QString& id) const {
|
||||
return messages->checkRecord(id);
|
||||
}
|
||||
|
||||
void Core::Archive::changeMessage(const QString& id, const QMap<QString, QVariant>& data) {
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
Shared::Message msg = messages->getRecord(id, txn);
|
||||
|
||||
bool hadStanzaId = !msg.getStanzaId().isEmpty();
|
||||
QDateTime oTime = msg.getTime();
|
||||
bool idChange = msg.change(data);
|
||||
QString newId = msg.getId();
|
||||
QDateTime nTime = msg.getTime();
|
||||
|
||||
bool orderChange = oTime != nTime;
|
||||
if (idChange || orderChange) {
|
||||
if (idChange)
|
||||
messages->removeRecord(id, txn);
|
||||
|
||||
if (orderChange)
|
||||
order->removeRecord(oTime.toMSecsSinceEpoch(), txn);
|
||||
|
||||
order->forceRecord(nTime.toMSecsSinceEpoch(), newId, txn);
|
||||
}
|
||||
|
||||
QString sid = msg.getStanzaId();
|
||||
if (!sid.isEmpty() && (idChange || !hadStanzaId))
|
||||
stanzaIdToId->forceRecord(sid, newId, txn);
|
||||
|
||||
messages->forceRecord(newId, msg, txn);
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::newest() const {
|
||||
LMDBAL::Transaction txn = db.beginReadOnlyTransaction();
|
||||
|
||||
try {
|
||||
cursor.open(txn);
|
||||
while (true) {
|
||||
std::pair<uint64_t, QString> pair = cursor.prev();
|
||||
Shared::Message msg = messages->getRecord(pair.second, txn);
|
||||
if (msg.serverStored()) {
|
||||
cursor.close();
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
cursor.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::Archive::newestId() const {
|
||||
Shared::Message msg = newest();
|
||||
return msg.getId();
|
||||
}
|
||||
|
||||
QString Core::Archive::oldestId() const {
|
||||
Shared::Message msg = oldest();
|
||||
return msg.getId();
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::oldest() const {
|
||||
LMDBAL::Transaction txn = db.beginReadOnlyTransaction();
|
||||
|
||||
try {
|
||||
cursor.open(txn);
|
||||
while (true) {
|
||||
std::pair<uint64_t, QString> pair = cursor.next();
|
||||
Shared::Message msg = messages->getRecord(pair.second, txn);
|
||||
if (msg.serverStored()) {
|
||||
cursor.close();
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
cursor.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messages) {
|
||||
unsigned int success = 0;
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
for (const Shared::Message& message : messages) {
|
||||
QString id = message.getId();
|
||||
bool added = false;
|
||||
try {
|
||||
Core::Archive::messages->addRecord(id, message, txn);
|
||||
added = true;
|
||||
} catch (const LMDBAL::Exist& e) {}
|
||||
|
||||
if (!added)
|
||||
continue;
|
||||
|
||||
order->addRecord(message.getTime().toMSecsSinceEpoch(), id, txn);
|
||||
|
||||
QString sid = message.getStanzaId();
|
||||
if (!sid.isEmpty())
|
||||
stanzaIdToId->addRecord(sid, id, txn);
|
||||
|
||||
++success;
|
||||
}
|
||||
txn.commit();
|
||||
return success;
|
||||
}
|
||||
|
||||
long unsigned int Core::Archive::size() const {
|
||||
return order->count();
|
||||
}
|
||||
|
||||
std::list<Shared::Message> Core::Archive::getBefore(unsigned int count, const QString& id) {
|
||||
LMDBAL::Transaction txn = db.beginReadOnlyTransaction();
|
||||
std::list<Shared::Message> res;
|
||||
try {
|
||||
cursor.open(txn);
|
||||
if (!id.isEmpty()) {
|
||||
Shared::Message reference = messages->getRecord(id, txn);
|
||||
uint64_t stamp = reference.getTime().toMSecsSinceEpoch();
|
||||
cursor.set(stamp);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i) {
|
||||
std::pair<uint64_t, QString> pair;
|
||||
cursor.prev(pair.first, pair.second);
|
||||
|
||||
res.emplace_back();
|
||||
Shared::Message& msg = res.back();
|
||||
messages->getRecord(pair.second, msg, txn);
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
return res;
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
cursor.close();
|
||||
if (res.empty())
|
||||
throw e;
|
||||
else
|
||||
return res;
|
||||
} catch (...) {
|
||||
cursor.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool Core::Archive::isFromTheBeginning() const {
|
||||
try {
|
||||
return stats->getRecord("fromTheBeginning").toBool();
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::setFromTheBeginning(bool is) {
|
||||
stats->forceRecord("fromTheBeginning", is);
|
||||
}
|
||||
|
||||
Shared::EncryptionProtocol Core::Archive::encryption() const {
|
||||
try {
|
||||
return stats->getRecord("encryption").value<Shared::EncryptionProtocol>();
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
return Shared::EncryptionProtocol::none;
|
||||
}
|
||||
}
|
||||
|
||||
bool Core::Archive::setEncryption(Shared::EncryptionProtocol is) {
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
Shared::EncryptionProtocol current = Shared::EncryptionProtocol::none;
|
||||
try {
|
||||
current = stats->getRecord("encryption", txn).value<Shared::EncryptionProtocol>();
|
||||
} catch (const LMDBAL::NotFound& e) {}
|
||||
|
||||
if (is != current) {
|
||||
stats->forceRecord("encryption", static_cast<uint8_t>(is), txn);
|
||||
txn.commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString Core::Archive::idByStanzaId(const QString& stanzaId) const {
|
||||
return stanzaIdToId->getRecord(stanzaId);
|
||||
}
|
||||
|
||||
QString Core::Archive::stanzaIdById(const QString& id) const {
|
||||
try {
|
||||
Shared::Message msg = getElement(id);
|
||||
return msg.getStanzaId();
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
|
||||
bool Core::Archive::setAvatar(const QByteArray& data, AvatarInfo& newInfo, bool generated, const QString& resource) {
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
AvatarInfo oldInfo;
|
||||
bool haveAvatar = false;
|
||||
QString res = resource.isEmpty() ? jid : resource;
|
||||
try {
|
||||
avatars->getRecord(res, oldInfo, txn);
|
||||
haveAvatar = true;
|
||||
} catch (const LMDBAL::NotFound& e) {}
|
||||
|
||||
if (data.size() == 0) {
|
||||
if (!haveAvatar)
|
||||
return false;
|
||||
|
||||
avatars->removeRecord(res, txn);
|
||||
txn.commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
QString currentPath = db.getPath();
|
||||
bool needToRemoveOld = false;
|
||||
QCryptographicHash hash(QCryptographicHash::Sha1);
|
||||
hash.addData(data);
|
||||
QByteArray newHash(hash.result());
|
||||
if (haveAvatar) {
|
||||
if (!generated && !oldInfo.autogenerated && oldInfo.hash == newHash)
|
||||
return false;
|
||||
|
||||
QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type);
|
||||
if (oldAvatar.exists()) {
|
||||
if (oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type + ".bak")) {
|
||||
needToRemoveOld = true;
|
||||
} else {
|
||||
qDebug() << "Can't change avatar: couldn't get rid of the old avatar" << oldAvatar.fileName();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
QMimeDatabase mimedb;
|
||||
QMimeType type = mimedb.mimeTypeForData(data);
|
||||
QString ext = type.preferredSuffix();
|
||||
QFile newAvatar(currentPath + "/" + res + "." + ext);
|
||||
if (!newAvatar.open(QFile::WriteOnly)) {
|
||||
qDebug() << "Can't change avatar: cant open file to write" << newAvatar.fileName() << "rolling back to the previous state";
|
||||
if (needToRemoveOld) {
|
||||
QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak");
|
||||
oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
newAvatar.write(data);
|
||||
newAvatar.close();
|
||||
|
||||
newInfo.type = ext;
|
||||
newInfo.hash = newHash;
|
||||
newInfo.autogenerated = generated;
|
||||
try {
|
||||
avatars->forceRecord(res, newInfo, txn);
|
||||
txn.commit();
|
||||
} catch (...) {
|
||||
qDebug() << "Can't change avatar: couldn't store changes to database for" << newAvatar.fileName() << "rolling back to the previous state";
|
||||
if (needToRemoveOld) {
|
||||
QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak");
|
||||
oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (needToRemoveOld) {
|
||||
QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak");
|
||||
oldAvatar.remove();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Core::Archive::readAvatarInfo(Core::Archive::AvatarInfo& target, const QString& resource) const {
|
||||
try {
|
||||
avatars->getRecord(resource.isEmpty() ? jid : resource, target);
|
||||
return true;
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Archive::readAllResourcesAvatars(std::map<QString, AvatarInfo>& data) const {
|
||||
avatars->readAll(data);
|
||||
}
|
||||
|
||||
Core::Archive::AvatarInfo Core::Archive::getAvatarInfo(const QString& resource) const {
|
||||
return avatars->getRecord(resource);
|
||||
}
|
||||
|
||||
Core::Archive::AvatarInfo::AvatarInfo():
|
||||
type(),
|
||||
hash(),
|
||||
autogenerated(false)
|
||||
{}
|
||||
|
||||
Core::Archive::AvatarInfo::AvatarInfo(const QString& p_type, const QByteArray& p_hash, bool p_autogenerated):
|
||||
type(p_type),
|
||||
hash(p_hash),
|
||||
autogenerated(p_autogenerated)
|
||||
{}
|
||||
|
||||
QDataStream & operator<<(QDataStream& out, const Core::Archive::AvatarInfo& info) {
|
||||
out << info.type;
|
||||
out << info.hash;
|
||||
out << info.autogenerated;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
QDataStream & operator>>(QDataStream& in, Core::Archive::AvatarInfo& info) {
|
||||
in >> info.type;
|
||||
in >> info.hash;
|
||||
in >> info.autogenerated;
|
||||
|
||||
return in;
|
||||
}
|
101
core/components/archive.h
Normal file
101
core/components/archive.h
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QCryptographicHash>
|
||||
#include <QMimeDatabase>
|
||||
#include <QMimeType>
|
||||
#include <QDataStream>
|
||||
|
||||
#include "shared/enums.h"
|
||||
#include "shared/message.h"
|
||||
#include "shared/exception.h"
|
||||
#include <list>
|
||||
|
||||
#include <base.h>
|
||||
#include <storage.h>
|
||||
#include <cursor.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class Archive : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
class AvatarInfo;
|
||||
|
||||
Archive(const QString& account, const QString& jid, QObject* parent = 0);
|
||||
~Archive();
|
||||
|
||||
void open();
|
||||
void close();
|
||||
|
||||
bool addElement(const Shared::Message& message);
|
||||
unsigned int addElements(const std::list<Shared::Message>& messages);
|
||||
Shared::Message getElement(const QString& id) const;
|
||||
bool hasElement(const QString& id) const;
|
||||
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
|
||||
Shared::Message oldest() const;
|
||||
QString oldestId() const;
|
||||
Shared::Message newest() const;
|
||||
QString newestId() const;
|
||||
void clear();
|
||||
long unsigned int size() const;
|
||||
std::list<Shared::Message> getBefore(unsigned int count, const QString& id);
|
||||
bool isFromTheBeginning() const;
|
||||
void setFromTheBeginning(bool is);
|
||||
Shared::EncryptionProtocol encryption() const;
|
||||
bool setEncryption(Shared::EncryptionProtocol value); //returns true if changed, false otherwise
|
||||
bool setAvatar(const QByteArray& data, AvatarInfo& info, bool generated = false, const QString& resource = "");
|
||||
AvatarInfo getAvatarInfo(const QString& resource = "") const;
|
||||
bool readAvatarInfo(AvatarInfo& target, const QString& resource = "") const;
|
||||
void readAllResourcesAvatars(std::map<QString, AvatarInfo>& data) const;
|
||||
QString idByStanzaId(const QString& stanzaId) const;
|
||||
QString stanzaIdById(const QString& id) const;
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
const QString account;
|
||||
|
||||
public:
|
||||
class AvatarInfo {
|
||||
public:
|
||||
AvatarInfo();
|
||||
AvatarInfo(const QString& type, const QByteArray& hash, bool autogenerated);
|
||||
|
||||
QString type;
|
||||
QByteArray hash;
|
||||
bool autogenerated;
|
||||
};
|
||||
|
||||
private:
|
||||
bool opened;
|
||||
LMDBAL::Base db;
|
||||
LMDBAL::Storage<QString, Shared::Message>* messages;
|
||||
LMDBAL::Storage<uint64_t, QString>* order;
|
||||
LMDBAL::Storage<QString, QVariant>* stats;
|
||||
LMDBAL::Storage<QString, AvatarInfo>* avatars;
|
||||
LMDBAL::Storage<QString, QString>* stanzaIdToId;
|
||||
mutable LMDBAL::Cursor<uint64_t, QString> cursor;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
QDataStream& operator << (QDataStream &out, const Core::Archive::AvatarInfo& info);
|
||||
QDataStream& operator >> (QDataStream &in, Core::Archive::AvatarInfo& info);
|
77
core/components/clientcache.cpp
Normal file
77
core/components/clientcache.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "clientcache.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
Core::ClientCache::ClientCache():
|
||||
db("clients"),
|
||||
cache(db.addCache<QString, Shared::ClientInfo>("info")),
|
||||
requested(),
|
||||
specific()
|
||||
{
|
||||
db.open();
|
||||
}
|
||||
|
||||
Core::ClientCache::~ClientCache() {
|
||||
db.close();
|
||||
}
|
||||
|
||||
void Core::ClientCache::open() {
|
||||
db.open();}
|
||||
|
||||
void Core::ClientCache::close() {
|
||||
db.close();}
|
||||
|
||||
bool Core::ClientCache::checkClient(const Shared::ClientId& p_id) {
|
||||
QString id = p_id.getId();
|
||||
if (requested.count(id) == 0 && !cache->checkRecord(id)) {
|
||||
requested.emplace(id, p_id);
|
||||
emit requestClientInfo(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Core::ClientCache::registerClientInfo (
|
||||
const QString& sourceFullJid,
|
||||
const QString& id,
|
||||
const std::set<Shared::Identity>& identities,
|
||||
const std::set<QString>& features)
|
||||
{
|
||||
std::map<QString, Shared::ClientInfo>::iterator itr = requested.find(id);
|
||||
if (itr != requested.end()) {
|
||||
Shared::ClientInfo& info = itr->second;
|
||||
info.identities = identities;
|
||||
info.extensions = features;
|
||||
|
||||
bool valid = info.valid();
|
||||
if (valid) {
|
||||
cache->addRecord(id, info);
|
||||
} else {
|
||||
info.specificPresence = sourceFullJid;
|
||||
specific.insert(std::make_pair(sourceFullJid, info));
|
||||
}
|
||||
requested.erase(id);
|
||||
return valid;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
63
core/components/clientcache.h
Normal file
63
core/components/clientcache.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include <cache.h>
|
||||
|
||||
#include <shared/clientid.h>
|
||||
#include <shared/clientinfo.h>
|
||||
#include <shared/identity.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class ClientCache : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
ClientCache();
|
||||
~ClientCache();
|
||||
|
||||
void open();
|
||||
void close();
|
||||
|
||||
signals:
|
||||
void requestClientInfo(const QString& id);
|
||||
|
||||
public slots:
|
||||
bool checkClient(const Shared::ClientId& id);
|
||||
bool registerClientInfo(
|
||||
const QString& sourceFullJid,
|
||||
const QString& id,
|
||||
const std::set<Shared::Identity>& identities,
|
||||
const std::set<QString>& features
|
||||
);
|
||||
|
||||
private:
|
||||
LMDBAL::Base db;
|
||||
LMDBAL::Cache<QString, Shared::ClientInfo>* cache;
|
||||
std::map<QString, Shared::ClientInfo> requested;
|
||||
std::map<QString, Shared::ClientInfo> specific;
|
||||
};
|
||||
|
||||
}
|
560
core/components/networkaccess.cpp
Normal file
560
core/components/networkaccess.cpp
Normal file
|
@ -0,0 +1,560 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtCore/QDir>
|
||||
|
||||
#include "networkaccess.h"
|
||||
|
||||
Core::NetworkAccess::NetworkAccess(QObject* parent):
|
||||
QObject(parent),
|
||||
running(false),
|
||||
manager(0),
|
||||
storage("fileURLStorage"),
|
||||
downloads(),
|
||||
uploads(),
|
||||
currentPath()
|
||||
{
|
||||
QSettings settings;
|
||||
currentPath = settings.value("downloadsPath").toString();
|
||||
}
|
||||
|
||||
Core::NetworkAccess::~NetworkAccess() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::downladFile(const QString& url) {
|
||||
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
||||
if (itr != downloads.end()) {
|
||||
qDebug() << "NetworkAccess received a request to download a file" << url << ", but the file is currently downloading, skipping";
|
||||
} else {
|
||||
try {
|
||||
std::pair<QString, std::list<Shared::MessageInfo>> p = storage.getPath(url);
|
||||
if (p.first.size() > 0) {
|
||||
QFileInfo info(p.first);
|
||||
if (info.exists() && info.isFile())
|
||||
emit downloadFileComplete(p.second, p.first);
|
||||
else
|
||||
startDownload(p.second, url);
|
||||
} else {
|
||||
startDownload(p.second, url);
|
||||
}
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
qDebug() << "NetworkAccess received a request to download a file" << url << ", but there is now record of which message uses that file, downloading anyway";
|
||||
storage.addFile(url);
|
||||
startDownload(std::list<Shared::MessageInfo>(), url);
|
||||
} catch (const LMDBAL::Unknown& e) {
|
||||
qDebug() << "Error requesting file path:" << e.what();
|
||||
emit loadFileError(std::list<Shared::MessageInfo>(), QString("Database error: ") + e.what(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::start() {
|
||||
if (!running) {
|
||||
manager = new QNetworkAccessManager();
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
||||
manager->setTransferTimeout();
|
||||
#endif
|
||||
storage.open();
|
||||
running = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::stop() {
|
||||
if (running) {
|
||||
storage.close();
|
||||
manager->deleteLater();
|
||||
manager = 0;
|
||||
running = false;
|
||||
|
||||
for (std::map<QString, Transfer*>::const_iterator itr = downloads.begin(), end = downloads.end(); itr != end; ++itr) {
|
||||
itr->second->success = false;
|
||||
itr->second->reply->abort(); //assuming it's gonna call onRequestFinished slot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
||||
if (itr == downloads.end()) {
|
||||
qDebug() << "an error downloading" << url << ": the request had some progress but seems like no one is waiting for it, skipping";
|
||||
} else {
|
||||
Transfer* dwn = itr->second;
|
||||
if (dwn->success) {
|
||||
qreal received = bytesReceived;
|
||||
qreal total = bytesTotal;
|
||||
qreal progress = received/total;
|
||||
dwn->progress = progress;
|
||||
emit loadFileProgress(dwn->messages, progress, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code) {
|
||||
qDebug() << "DEBUG: DOWNLOAD ERROR";
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
qDebug() << rpl->errorString();
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
||||
if (itr == downloads.end()) {
|
||||
qDebug() << "an error downloading" << url << ": the request is reporting an error but seems like no one is waiting for it, skipping";
|
||||
} else {
|
||||
QString errorText = getErrorText(code);
|
||||
//if (errorText.size() > 0) {
|
||||
itr->second->success = false;
|
||||
Transfer* dwn = itr->second;
|
||||
emit loadFileError(dwn->messages, errorText, false);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadSSLError(const QList<QSslError>& errors) {
|
||||
qDebug() << "DEBUG: DOWNLOAD SSL ERRORS";
|
||||
for (const QSslError& err : errors)
|
||||
qDebug() << err.errorString();
|
||||
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
||||
if (itr == downloads.end()) {
|
||||
qDebug() << "an SSL error downloading" << url << ": the request is reporting an error but seems like no one is waiting for it, skipping";
|
||||
} else {
|
||||
//if (errorText.size() > 0) {
|
||||
itr->second->success = false;
|
||||
Transfer* dwn = itr->second;
|
||||
emit loadFileError(dwn->messages, "SSL errors occured", false);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::getErrorText(QNetworkReply::NetworkError code) {
|
||||
QString errorText("");
|
||||
switch (code) {
|
||||
case QNetworkReply::NoError:
|
||||
//this never is supposed to happen
|
||||
break;
|
||||
|
||||
// network layer errors [relating to the destination server] (1-99):
|
||||
case QNetworkReply::ConnectionRefusedError:
|
||||
errorText = "Connection refused";
|
||||
break;
|
||||
case QNetworkReply::RemoteHostClosedError:
|
||||
errorText = "Remote server closed the connection";
|
||||
break;
|
||||
case QNetworkReply::HostNotFoundError:
|
||||
errorText = "Remote host is not found";
|
||||
break;
|
||||
case QNetworkReply::TimeoutError:
|
||||
errorText = "Connection was closed because it timed out";
|
||||
break;
|
||||
case QNetworkReply::OperationCanceledError:
|
||||
//this means I closed it myself by abort() or close()
|
||||
//I don't call them directory, but this is the error code
|
||||
//Qt returns when it can not resume donwload after the network failure
|
||||
//or when the download is canceled by the timout;
|
||||
errorText = "Connection lost";
|
||||
break;
|
||||
case QNetworkReply::SslHandshakeFailedError:
|
||||
errorText = "Security error"; //TODO need to handle sslErrors signal to get a better description here
|
||||
break;
|
||||
case QNetworkReply::TemporaryNetworkFailureError:
|
||||
//this means the connection is lost by opened route, but it's going to be resumed, not sure I need to notify
|
||||
break;
|
||||
case QNetworkReply::NetworkSessionFailedError:
|
||||
errorText = "Outgoing connection problem";
|
||||
break;
|
||||
case QNetworkReply::BackgroundRequestNotAllowedError:
|
||||
errorText = "Background request is not allowed";
|
||||
break;
|
||||
case QNetworkReply::TooManyRedirectsError:
|
||||
errorText = "The request was redirected too many times";
|
||||
break;
|
||||
case QNetworkReply::InsecureRedirectError:
|
||||
errorText = "The request was redirected to insecure connection";
|
||||
break;
|
||||
case QNetworkReply::UnknownNetworkError:
|
||||
errorText = "Unknown network error";
|
||||
break;
|
||||
|
||||
// proxy errors (101-199):
|
||||
case QNetworkReply::ProxyConnectionRefusedError:
|
||||
errorText = "The connection to the proxy server was refused";
|
||||
break;
|
||||
case QNetworkReply::ProxyConnectionClosedError:
|
||||
errorText = "Proxy server closed the connection";
|
||||
break;
|
||||
case QNetworkReply::ProxyNotFoundError:
|
||||
errorText = "Proxy host was not found";
|
||||
break;
|
||||
case QNetworkReply::ProxyTimeoutError:
|
||||
errorText = "Connection to the proxy server was closed because it timed out";
|
||||
break;
|
||||
case QNetworkReply::ProxyAuthenticationRequiredError:
|
||||
errorText = "Couldn't connect to proxy server, authentication is required";
|
||||
break;
|
||||
case QNetworkReply::UnknownProxyError:
|
||||
errorText = "Unknown proxy error";
|
||||
break;
|
||||
|
||||
// content errors (201-299):
|
||||
case QNetworkReply::ContentAccessDenied:
|
||||
errorText = "The access to file is denied";
|
||||
break;
|
||||
case QNetworkReply::ContentOperationNotPermittedError:
|
||||
errorText = "The operation over requesting file is not permitted";
|
||||
break;
|
||||
case QNetworkReply::ContentNotFoundError:
|
||||
errorText = "The file was not found";
|
||||
break;
|
||||
case QNetworkReply::AuthenticationRequiredError:
|
||||
errorText = "Couldn't access the file, authentication is required";
|
||||
break;
|
||||
case QNetworkReply::ContentReSendError:
|
||||
errorText = "Sending error, one more attempt will probably solve this problem";
|
||||
break;
|
||||
case QNetworkReply::ContentConflictError:
|
||||
errorText = "The request could not be completed due to a conflict with the current state of the resource";
|
||||
break;
|
||||
case QNetworkReply::ContentGoneError:
|
||||
errorText = "The requested resource is no longer available at the server";
|
||||
break;
|
||||
case QNetworkReply::UnknownContentError:
|
||||
errorText = "Unknown content error";
|
||||
break;
|
||||
|
||||
// protocol errors
|
||||
case QNetworkReply::ProtocolUnknownError:
|
||||
errorText = "Unknown protocol error";
|
||||
break;
|
||||
case QNetworkReply::ProtocolInvalidOperationError:
|
||||
errorText = "Requested operation is not permitted in this protocol";
|
||||
break;
|
||||
case QNetworkReply::ProtocolFailure:
|
||||
errorText = "Low level protocol error";
|
||||
break;
|
||||
|
||||
// Server side errors (401-499)
|
||||
case QNetworkReply::InternalServerError:
|
||||
errorText = "Internal server error";
|
||||
break;
|
||||
case QNetworkReply::OperationNotImplementedError:
|
||||
errorText = "Server doesn't support requested operation";
|
||||
break;
|
||||
case QNetworkReply::ServiceUnavailableError:
|
||||
errorText = "The server is not available for this operation right now";
|
||||
break;
|
||||
case QNetworkReply::UnknownServerError:
|
||||
errorText = "Unknown server error";
|
||||
break;
|
||||
}
|
||||
return errorText;
|
||||
}
|
||||
|
||||
|
||||
void Core::NetworkAccess::onDownloadFinished() {
|
||||
qDebug() << "DEBUG: DOWNLOAD FINISHED";
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
||||
if (itr == downloads.end()) {
|
||||
qDebug() << "an error downloading" << url << ": the request is done but there is no record of it being downloaded, ignoring";
|
||||
} else {
|
||||
Transfer* dwn = itr->second;
|
||||
if (dwn->success) {
|
||||
qDebug() << "download success for" << url;
|
||||
QString err;
|
||||
QStringList hops = url.split("/");
|
||||
QString fileName = hops.back();
|
||||
QString jid;
|
||||
if (dwn->messages.size() > 0)
|
||||
jid = dwn->messages.front().jid;
|
||||
else
|
||||
qDebug() << "An attempt to save the file but it doesn't seem to belong to any message, download is definately going to be broken";
|
||||
|
||||
QString path = prepareDirectory(jid);
|
||||
if (path.size() > 0) {
|
||||
path = checkFileName(fileName, path);
|
||||
|
||||
QFile file(Shared::resolvePath(path));
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(dwn->reply->readAll());
|
||||
file.close();
|
||||
storage.setPath(url, path);
|
||||
qDebug() << "file" << path << "was successfully downloaded";
|
||||
} else {
|
||||
qDebug() << "couldn't save file" << path;
|
||||
err = "Error opening file to write:" + file.errorString();
|
||||
}
|
||||
} else {
|
||||
err = "Couldn't prepare a directory for file";
|
||||
}
|
||||
|
||||
if (path.size() > 0)
|
||||
emit downloadFileComplete(dwn->messages, path);
|
||||
else
|
||||
emit loadFileError(dwn->messages, "Error saving file " + url + "; " + err, false);
|
||||
}
|
||||
|
||||
dwn->reply->deleteLater();
|
||||
delete dwn;
|
||||
downloads.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url) {
|
||||
Transfer* dwn = new Transfer({msgs, 0, 0, true, "", url, 0});
|
||||
QNetworkRequest req(url);
|
||||
dwn->reply = manager->get(req);
|
||||
connect(dwn->reply, &QNetworkReply::downloadProgress, this, &NetworkAccess::onDownloadProgress);
|
||||
connect(dwn->reply, &QNetworkReply::sslErrors, this, &NetworkAccess::onDownloadSSLError);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
||||
connect(dwn->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &NetworkAccess::onDownloadError);
|
||||
#else
|
||||
connect(dwn->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), this, &NetworkAccess::onDownloadError);
|
||||
#endif
|
||||
connect(dwn->reply, &QNetworkReply::finished, this, &NetworkAccess::onDownloadFinished);
|
||||
downloads.insert(std::make_pair(url, dwn));
|
||||
emit loadFileProgress(dwn->messages, 0, false);
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadError(QNetworkReply::NetworkError code) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
if (itr == uploads.end()) {
|
||||
qDebug() << "an error uploading" << url << ": the request is reporting an error but there is no record of it being uploading, ignoring";
|
||||
} else {
|
||||
QString errorText = getErrorText(code);
|
||||
//if (errorText.size() > 0) {
|
||||
itr->second->success = false;
|
||||
Transfer* upl = itr->second;
|
||||
emit loadFileError(upl->messages, errorText, true);
|
||||
//}
|
||||
|
||||
//TODO deletion?
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadFinished() {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
if (itr == downloads.end()) {
|
||||
qDebug() << "an error uploading" << url << ": the request is done there is no record of it being uploading, ignoring";
|
||||
} else {
|
||||
Transfer* upl = itr->second;
|
||||
if (upl->success) {
|
||||
qDebug() << "upload success for" << url;
|
||||
|
||||
// Copy file to Download folder if it is a temp file. See Conversation::onImagePasted.
|
||||
if (upl->path.startsWith(QDir::tempPath() + QDir::separator() + QStringLiteral("squawk_img_attach_")) && upl->path.endsWith(".png")) {
|
||||
QString err = "";
|
||||
QString downloadDirPath = prepareDirectory(upl->messages.front().jid);
|
||||
if (downloadDirPath.size() > 0) {
|
||||
QString newPath = downloadDirPath + QDir::separator() + upl->path.mid(QDir::tempPath().length() + 1);
|
||||
|
||||
// Copy {TEMPDIR}/squawk_img_attach_XXXXXX.png to Download folder
|
||||
bool copyResult = QFile::copy(upl->path, Shared::resolvePath(newPath));
|
||||
if (copyResult)
|
||||
upl->path = newPath; // Change storage
|
||||
else
|
||||
err = "copying to " + newPath + " failed";
|
||||
} else {
|
||||
err = "Couldn't prepare a directory for file";
|
||||
}
|
||||
|
||||
if (err.size() != 0)
|
||||
qDebug() << "failed to copy temporary upload file " << upl->path << " to download folder:" << err;
|
||||
}
|
||||
|
||||
storage.addFile(upl->messages, upl->url, upl->path);
|
||||
emit uploadFileComplete(upl->messages, upl->url, upl->path);
|
||||
}
|
||||
|
||||
upl->reply->deleteLater();
|
||||
upl->file->close();
|
||||
upl->file->deleteLater();
|
||||
delete upl;
|
||||
uploads.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
if (itr == uploads.end()) {
|
||||
qDebug() << "an error downloading" << url << ": the request had some progress but seems like no one is waiting for it, skipping";
|
||||
} else {
|
||||
Transfer* upl = itr->second;
|
||||
if (upl->success) {
|
||||
qreal received = bytesReceived;
|
||||
qreal total = bytesTotal;
|
||||
qreal progress = received/total;
|
||||
upl->progress = progress;
|
||||
emit loadFileProgress(upl->messages, progress, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::getFileRemoteUrl(const QString& path) {
|
||||
QString p = Shared::squawkifyPath(path);
|
||||
|
||||
try {
|
||||
p = storage.getUrl(p);
|
||||
} catch (const LMDBAL::NotFound& err) {
|
||||
p = "";
|
||||
} catch (...) {
|
||||
throw;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::uploadFile(
|
||||
const Shared::MessageInfo& info,
|
||||
const QString& path,
|
||||
const QUrl& put,
|
||||
const QUrl& get,
|
||||
const QMap<QString, QString> headers
|
||||
) {
|
||||
QFile* file = new QFile(path);
|
||||
Transfer* upl = new Transfer({{info}, 0, 0, true, path, get.toString(), file});
|
||||
QNetworkRequest req(put);
|
||||
for (QMap<QString, QString>::const_iterator itr = headers.begin(), end = headers.end(); itr != end; itr++) {
|
||||
req.setRawHeader(itr.key().toUtf8(), itr.value().toUtf8());
|
||||
}
|
||||
if (file->open(QIODevice::ReadOnly)) {
|
||||
upl->reply = manager->put(req, file);
|
||||
|
||||
connect(upl->reply, &QNetworkReply::uploadProgress, this, &NetworkAccess::onUploadProgress);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
||||
connect(upl->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &NetworkAccess::onUploadError);
|
||||
#else
|
||||
connect(upl->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), this, &NetworkAccess::onUploadError);
|
||||
#endif
|
||||
connect(upl->reply, &QNetworkReply::finished, this, &NetworkAccess::onUploadFinished);
|
||||
uploads.insert(std::make_pair(put.toString(), upl));
|
||||
emit loadFileProgress(upl->messages, 0, true);
|
||||
} else {
|
||||
qDebug() << "couldn't upload file" << path;
|
||||
emit loadFileError(upl->messages, "Error opening file", true);
|
||||
delete file;
|
||||
delete upl;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& account, const QString& jid, const QString& id) {
|
||||
storage.addFile(url, account, jid, id);
|
||||
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
||||
if (itr != downloads.end())
|
||||
itr->second->messages.emplace_back(account, jid, id); //TODO notification is going to happen the next tick, is that okay?
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id) {
|
||||
storage.addFile(url, path, account, jid, id);
|
||||
}
|
||||
|
||||
bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path) {
|
||||
for (const std::pair<const QString, Transfer*>& pair : uploads) {
|
||||
Transfer* info = pair.second;
|
||||
if (pair.second->path == path) {
|
||||
std::list<Shared::MessageInfo>& messages = info->messages;
|
||||
bool dup = false;
|
||||
for (const Shared::MessageInfo& info : messages) {
|
||||
if (info.account == acc && info.jid == jid && info.messageId == id) {
|
||||
dup = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dup) {
|
||||
info->messages.emplace_back(acc, jid, id); //TODO notification is going to happen the next tick, is that okay?
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::prepareDirectory(const QString& jid) {
|
||||
QString path = currentPath;
|
||||
QString addition;
|
||||
if (jid.size() > 0) {
|
||||
addition = jid;
|
||||
path += QDir::separator() + jid;
|
||||
}
|
||||
|
||||
QDir location(path);
|
||||
|
||||
if (!location.exists()) {
|
||||
bool res = location.mkpath(path);
|
||||
if (!res)
|
||||
return "";
|
||||
else
|
||||
return "squawk://" + addition;
|
||||
}
|
||||
return "squawk://" + addition;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::checkFileName(const QString& name, const QString& path) {
|
||||
QStringList parts = name.split(".");
|
||||
QString suffix("");
|
||||
QStringList::const_iterator sItr = parts.begin();
|
||||
QString realName = *sItr;
|
||||
++sItr;
|
||||
for (QStringList::const_iterator sEnd = parts.end(); sItr != sEnd; ++sItr)
|
||||
suffix += "." + (*sItr);
|
||||
|
||||
QString postfix("");
|
||||
QString resolvedPath = Shared::resolvePath(path);
|
||||
QString count("");
|
||||
QFileInfo proposedName(resolvedPath + QDir::separator() + realName + count + suffix);
|
||||
|
||||
int counter = 0;
|
||||
while (proposedName.exists()) {
|
||||
count = QString("(") + std::to_string(++counter).c_str() + ")";
|
||||
proposedName = QFileInfo(resolvedPath + QDir::separator() + realName + count + suffix);
|
||||
}
|
||||
|
||||
return path + QDir::separator() + realName + count + suffix;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id) {
|
||||
return storage.addMessageAndCheckForPath(url, account, jid, id);
|
||||
}
|
||||
|
||||
std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QString& path) {
|
||||
return storage.deletedFile(path);
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath) {
|
||||
QDir dir(currentPath);
|
||||
bool success = true;
|
||||
qDebug() << "moving" << currentPath << "to" << newPath;
|
||||
for (QString fileName : dir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System))
|
||||
success = dir.rename(fileName, newPath + QDir::separator() + fileName) && success;
|
||||
|
||||
if (!success)
|
||||
qDebug() << "couldn't move downloads directory, most probably downloads will be broken";
|
||||
|
||||
currentPath = newPath;
|
||||
}
|
100
core/components/networkaccess.h
Normal file
100
core/components/networkaccess.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QFileInfo>
|
||||
#include <QFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
|
||||
#include <set>
|
||||
|
||||
#include <shared/pathcheck.h>
|
||||
#include "urlstorage.h"
|
||||
|
||||
namespace Core {
|
||||
|
||||
//TODO Need to describe how to get rid of records when file is no longer reachable;
|
||||
class NetworkAccess : public QObject {
|
||||
Q_OBJECT
|
||||
struct Transfer;
|
||||
public:
|
||||
NetworkAccess(QObject* parent = nullptr);
|
||||
virtual ~NetworkAccess();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
QString getFileRemoteUrl(const QString& path);
|
||||
QString addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id);
|
||||
void uploadFile(const Shared::MessageInfo& info, const QString& path, const QUrl& put, const QUrl& get, const QMap<QString, QString> headers);
|
||||
bool checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path);
|
||||
std::list<Shared::MessageInfo> reportPathInvalid(const QString& path);
|
||||
|
||||
signals:
|
||||
void loadFileProgress(const std::list<Shared::MessageInfo>& msgs, qreal value, bool up);
|
||||
void loadFileError(const std::list<Shared::MessageInfo>& msgs, const QString& text, bool up);
|
||||
void uploadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& url, const QString& path);
|
||||
void downloadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& path);
|
||||
|
||||
public slots:
|
||||
void downladFile(const QString& url);
|
||||
void registerFile(const QString& url, const QString& account, const QString& jid, const QString& id);
|
||||
void registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id);
|
||||
void moveFilesDirectory(const QString& newPath);
|
||||
|
||||
private:
|
||||
void startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url);
|
||||
QString getErrorText(QNetworkReply::NetworkError code);
|
||||
QString prepareDirectory(const QString& jid);
|
||||
QString checkFileName(const QString& name, const QString& path);
|
||||
|
||||
private slots:
|
||||
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
||||
void onDownloadError(QNetworkReply::NetworkError code);
|
||||
void onDownloadSSLError(const QList<QSslError> &errors);
|
||||
void onDownloadFinished();
|
||||
void onUploadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
||||
void onUploadError(QNetworkReply::NetworkError code);
|
||||
void onUploadFinished();
|
||||
|
||||
private:
|
||||
bool running;
|
||||
QNetworkAccessManager* manager;
|
||||
UrlStorage storage;
|
||||
std::map<QString, Transfer*> downloads;
|
||||
std::map<QString, Transfer*> uploads;
|
||||
QString currentPath;
|
||||
|
||||
struct Transfer {
|
||||
std::list<Shared::MessageInfo> messages;
|
||||
qreal progress;
|
||||
QNetworkReply* reply;
|
||||
bool success;
|
||||
QString path;
|
||||
QString url;
|
||||
QFile* file;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
252
core/components/urlstorage.cpp
Normal file
252
core/components/urlstorage.cpp
Normal file
|
@ -0,0 +1,252 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
|
||||
#include "urlstorage.h"
|
||||
|
||||
Core::UrlStorage::UrlStorage(const QString& p_name):
|
||||
base(p_name),
|
||||
urlToInfo(base.addStorage<QString, UrlInfo>("urlToInfo")),
|
||||
pathToUrl(base.addStorage<QString, QString>("pathToUrl"))
|
||||
{}
|
||||
|
||||
Core::UrlStorage::~UrlStorage() {
|
||||
close();
|
||||
}
|
||||
|
||||
void Core::UrlStorage::open() {
|
||||
base.open();
|
||||
}
|
||||
|
||||
void Core::UrlStorage::close() {
|
||||
base.close();
|
||||
}
|
||||
|
||||
void Core::UrlStorage::writeInfo(const QString& key, const Core::UrlStorage::UrlInfo& info, bool overwrite) {
|
||||
LMDBAL::WriteTransaction txn = base.beginTransaction();
|
||||
writeInfo(key, info, txn, overwrite);
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
void Core::UrlStorage::writeInfo(const QString& key, const Core::UrlStorage::UrlInfo& info, const LMDBAL::WriteTransaction& txn, bool overwrite) {
|
||||
if (overwrite)
|
||||
urlToInfo->forceRecord(key, info, txn);
|
||||
else
|
||||
urlToInfo->addRecord(key, info, txn);
|
||||
|
||||
if (info.hasPath())
|
||||
pathToUrl->forceRecord(info.getPath(), key, txn);
|
||||
}
|
||||
|
||||
void Core::UrlStorage::addFile(const QString& url) {
|
||||
addToInfo(url, "", "", "");
|
||||
}
|
||||
|
||||
void Core::UrlStorage::addFile(const QString& url, const QString& path) {
|
||||
addToInfo(url, "", "", "", path);
|
||||
}
|
||||
|
||||
void Core::UrlStorage::addFile(const QString& url, const QString& account, const QString& jid, const QString& id) {
|
||||
addToInfo(url, account, jid, id);
|
||||
}
|
||||
|
||||
void Core::UrlStorage::addFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id) {
|
||||
addToInfo(url, account, jid, id, path);
|
||||
}
|
||||
|
||||
void Core::UrlStorage::addFile(const std::list<Shared::MessageInfo>& msgs, const QString& url, const QString& path) {
|
||||
UrlInfo info (path, msgs);
|
||||
writeInfo(url, info, true);
|
||||
}
|
||||
|
||||
QString Core::UrlStorage::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id){
|
||||
return addToInfo(url, account, jid, id).getPath();
|
||||
}
|
||||
|
||||
Core::UrlStorage::UrlInfo Core::UrlStorage::addToInfo(
|
||||
const QString& url,
|
||||
const QString& account,
|
||||
const QString& jid,
|
||||
const QString& id,
|
||||
const QString& path
|
||||
) {
|
||||
UrlInfo info;
|
||||
LMDBAL::WriteTransaction txn = base.beginTransaction();
|
||||
|
||||
try {
|
||||
urlToInfo->getRecord(url, info, txn);
|
||||
} catch (const LMDBAL::NotFound& e) {}
|
||||
|
||||
bool pathChange = false;
|
||||
bool listChange = false;
|
||||
if (path != "-s") {
|
||||
if (info.getPath() != path) {
|
||||
info.setPath(path);
|
||||
pathChange = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (account.size() > 0 && jid.size() > 0 && id.size() > 0)
|
||||
listChange = info.addMessage(account, jid, id);
|
||||
|
||||
if (pathChange || listChange) {
|
||||
writeInfo(url, info, txn, true);
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
std::list<Shared::MessageInfo> Core::UrlStorage::setPath(const QString& url, const QString& path) {
|
||||
std::list<Shared::MessageInfo> list;
|
||||
LMDBAL::WriteTransaction txn = base.beginTransaction();
|
||||
UrlInfo info;
|
||||
|
||||
try {
|
||||
urlToInfo->getRecord(url, info, txn);
|
||||
info.getMessages(list);
|
||||
} catch (const LMDBAL::NotFound& e) {}
|
||||
|
||||
info.setPath(path);
|
||||
writeInfo(url, info, txn, true);
|
||||
txn.commit();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
std::list<Shared::MessageInfo> Core::UrlStorage::removeFile(const QString& url) {
|
||||
std::list<Shared::MessageInfo> list;
|
||||
LMDBAL::WriteTransaction txn = base.beginTransaction();
|
||||
UrlInfo info;
|
||||
|
||||
urlToInfo->getRecord(url, info, txn);
|
||||
urlToInfo->removeRecord(url, txn);
|
||||
info.getMessages(list);
|
||||
|
||||
if (info.hasPath())
|
||||
pathToUrl->removeRecord(info.getPath(), txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
std::list<Shared::MessageInfo> Core::UrlStorage::deletedFile(const QString& path) {
|
||||
std::list<Shared::MessageInfo> list;
|
||||
LMDBAL::WriteTransaction txn = base.beginTransaction();
|
||||
|
||||
QString url = pathToUrl->getRecord(path, txn);
|
||||
pathToUrl->removeRecord(path, txn);
|
||||
|
||||
UrlInfo info = urlToInfo->getRecord(url, txn);
|
||||
info.getMessages(list);
|
||||
info.setPath(QString());
|
||||
urlToInfo->changeRecord(url, info, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
QString Core::UrlStorage::getUrl(const QString& path) {
|
||||
return pathToUrl->getRecord(path);
|
||||
}
|
||||
|
||||
std::pair<QString, std::list<Shared::MessageInfo>> Core::UrlStorage::getPath(const QString& url) {
|
||||
UrlInfo info = urlToInfo->getRecord(url);
|
||||
std::list<Shared::MessageInfo> container;
|
||||
info.getMessages(container);
|
||||
return std::make_pair(info.getPath(), container);
|
||||
}
|
||||
|
||||
Core::UrlStorage::UrlInfo::UrlInfo():
|
||||
localPath(),
|
||||
messages() {}
|
||||
|
||||
Core::UrlStorage::UrlInfo::UrlInfo(const QString& path):
|
||||
localPath(path),
|
||||
messages() {}
|
||||
|
||||
Core::UrlStorage::UrlInfo::UrlInfo(const QString& path, const std::list<Shared::MessageInfo>& msgs):
|
||||
localPath(path),
|
||||
messages(msgs) {}
|
||||
|
||||
Core::UrlStorage::UrlInfo::~UrlInfo() {}
|
||||
|
||||
bool Core::UrlStorage::UrlInfo::addMessage(const QString& acc, const QString& jid, const QString& id) {
|
||||
for (const Shared::MessageInfo& info : messages) {
|
||||
if (info.account == acc && info.jid == jid && info.messageId == id)
|
||||
return false;
|
||||
}
|
||||
messages.emplace_back(acc, jid, id);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Core::UrlStorage::UrlInfo::serialize(QDataStream& data) const {
|
||||
data << localPath;
|
||||
std::list<Shared::MessageInfo>::size_type size = messages.size();
|
||||
data << quint32(size);
|
||||
for (const Shared::MessageInfo& info : messages) {
|
||||
data << info.account;
|
||||
data << info.jid;
|
||||
data << info.messageId;
|
||||
}
|
||||
}
|
||||
|
||||
QDataStream & operator << (QDataStream& in, const Core::UrlStorage::UrlInfo& info) {
|
||||
info.serialize(in);
|
||||
return in;
|
||||
}
|
||||
|
||||
QDataStream & operator >> (QDataStream& out, Core::UrlStorage::UrlInfo& info) {
|
||||
info.deserialize(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void Core::UrlStorage::UrlInfo::deserialize(QDataStream& data) {
|
||||
data >> localPath;
|
||||
quint32 size;
|
||||
data >> size;
|
||||
for (quint32 i = 0; i < size; ++i) {
|
||||
messages.emplace_back();
|
||||
Shared::MessageInfo& info = messages.back();
|
||||
data >> info.account;
|
||||
data >> info.jid;
|
||||
data >> info.messageId;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::UrlStorage::UrlInfo::getMessages(std::list<Shared::MessageInfo>& container) const {
|
||||
for (const Shared::MessageInfo& info : messages)
|
||||
container.emplace_back(info);
|
||||
}
|
||||
|
||||
QString Core::UrlStorage::UrlInfo::getPath() const {
|
||||
return localPath;
|
||||
}
|
||||
|
||||
bool Core::UrlStorage::UrlInfo::hasPath() const {
|
||||
return localPath.size() > 0;
|
||||
}
|
||||
|
||||
void Core::UrlStorage::UrlInfo::setPath(const QString& path) {
|
||||
localPath = path;
|
||||
}
|
94
core/components/urlstorage.h
Normal file
94
core/components/urlstorage.h
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QDataStream>
|
||||
|
||||
#include <list>
|
||||
|
||||
#include <storage.h>
|
||||
|
||||
#include <shared/messageinfo.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class UrlStorage {
|
||||
public:
|
||||
class UrlInfo;
|
||||
|
||||
public:
|
||||
UrlStorage(const QString& name);
|
||||
~UrlStorage();
|
||||
|
||||
void open();
|
||||
void close();
|
||||
|
||||
void addFile(const QString& url);
|
||||
void addFile(const QString& url, const QString& path);
|
||||
void addFile(const QString& url, const QString& account, const QString& jid, const QString& id);
|
||||
void addFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id);
|
||||
void addFile(const std::list<Shared::MessageInfo>& msgs, const QString& url, const QString& path); //this one overwrites all that was
|
||||
std::list<Shared::MessageInfo> removeFile(const QString& url); //removes entry like it never was in the database, returns affected message infos
|
||||
std::list<Shared::MessageInfo> deletedFile(const QString& path); //empties the localPath of the entry, returns affected message infos
|
||||
std::list<Shared::MessageInfo> setPath(const QString& url, const QString& path);
|
||||
QString getUrl(const QString& path);
|
||||
QString addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id);
|
||||
std::pair<QString, std::list<Shared::MessageInfo>> getPath(const QString& url);
|
||||
|
||||
private:
|
||||
LMDBAL::Base base;
|
||||
LMDBAL::Storage<QString, UrlInfo>* urlToInfo;
|
||||
LMDBAL::Storage<QString, QString>* pathToUrl;
|
||||
|
||||
private:
|
||||
void writeInfo(const QString& key, const UrlInfo& info, bool overwrite = false);
|
||||
void writeInfo(const QString& key, const UrlInfo& info, const LMDBAL::WriteTransaction& txn, bool overwrite = false);
|
||||
UrlInfo addToInfo(const QString& url, const QString& account, const QString& jid, const QString& id, const QString& path = "-s");
|
||||
|
||||
public:
|
||||
class UrlInfo {
|
||||
public:
|
||||
UrlInfo(const QString& path);
|
||||
UrlInfo(const QString& path, const std::list<Shared::MessageInfo>& msgs);
|
||||
UrlInfo();
|
||||
~UrlInfo();
|
||||
|
||||
void serialize(QDataStream& data) const;
|
||||
void deserialize(QDataStream& data);
|
||||
|
||||
QString getPath() const;
|
||||
bool hasPath() const;
|
||||
void setPath(const QString& path);
|
||||
|
||||
bool addMessage(const QString& acc, const QString& jid, const QString& id);
|
||||
void getMessages(std::list<Shared::MessageInfo>& container) const;
|
||||
|
||||
private:
|
||||
QString localPath;
|
||||
std::list<Shared::MessageInfo> messages;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
QDataStream& operator >> (QDataStream &in, Core::UrlStorage::UrlInfo& info);
|
||||
QDataStream& operator << (QDataStream &out, const Core::UrlStorage::UrlInfo& info);
|
|
@ -25,71 +25,65 @@ Core::Conference::Conference(const QString& p_jid, const QString& p_account, boo
|
|||
nick(p_nick),
|
||||
room(p_room),
|
||||
joined(false),
|
||||
autoJoin(p_autoJoin)
|
||||
autoJoin(p_autoJoin),
|
||||
exParticipants()
|
||||
{
|
||||
muc = true;
|
||||
name = p_name;
|
||||
|
||||
connect(room, SIGNAL(joined()), this, SLOT(onRoomJoined()));
|
||||
connect(room, SIGNAL(left()), this, SLOT(onRoomLeft()));
|
||||
connect(room, SIGNAL(nameChanged(const QString&)), this, SLOT(onRoomNameChanged(const QString&)));
|
||||
connect(room, SIGNAL(subjectChanged(const QString&)), this, SLOT(onRoomSubjectChanged(const QString&)));
|
||||
connect(room, SIGNAL(participantAdded(const QString&)), this, SLOT(onRoomParticipantAdded(const QString&)));
|
||||
connect(room, SIGNAL(participantChanged(const QString&)), this, SLOT(onRoomParticipantChanged(const QString&)));
|
||||
connect(room, SIGNAL(participantRemoved(const QString&)), this, SLOT(onRoomParticipantRemoved(const QString&)));
|
||||
connect(room, SIGNAL(nickNameChanged(const QString&)), this, SLOT(onRoomNickNameChanged(const QString&)));
|
||||
connect(room, SIGNAL(error(const QXmppStanza::Error&)), this, SLOT(onRoomError(const QXmppStanza::Error&)));
|
||||
connect(room, &QXmppMucRoom::joined, this, &Conference::onRoomJoined);
|
||||
connect(room, &QXmppMucRoom::left, this, &Conference::onRoomLeft);
|
||||
connect(room, &QXmppMucRoom::nameChanged, this, &Conference::onRoomNameChanged);
|
||||
connect(room, &QXmppMucRoom::subjectChanged, this, &Conference::onRoomSubjectChanged);
|
||||
connect(room, &QXmppMucRoom::participantAdded, this, &Conference::onRoomParticipantAdded);
|
||||
connect(room, &QXmppMucRoom::participantChanged, this, &Conference::onRoomParticipantChanged);
|
||||
connect(room, &QXmppMucRoom::participantRemoved, this, &Conference::onRoomParticipantRemoved);
|
||||
connect(room, &QXmppMucRoom::nickNameChanged, this, &Conference::onRoomNickNameChanged);
|
||||
connect(room, &QXmppMucRoom::error, this, &Conference::onRoomError);
|
||||
|
||||
room->setNickName(nick);
|
||||
if (autoJoin) {
|
||||
if (autoJoin)
|
||||
room->join();
|
||||
}
|
||||
|
||||
archive->readAllResourcesAvatars(exParticipants);
|
||||
}
|
||||
|
||||
Core::Conference::~Conference()
|
||||
{
|
||||
if (joined) {
|
||||
Core::Conference::~Conference(){
|
||||
if (joined)
|
||||
room->leave();
|
||||
}
|
||||
|
||||
room->deleteLater();
|
||||
}
|
||||
|
||||
QString Core::Conference::getNick() const
|
||||
{
|
||||
QString Core::Conference::getNick() const {
|
||||
return nick;
|
||||
}
|
||||
|
||||
bool Core::Conference::getAutoJoin()
|
||||
{
|
||||
bool Core::Conference::getAutoJoin() const {
|
||||
return autoJoin;
|
||||
}
|
||||
|
||||
bool Core::Conference::getJoined() const
|
||||
{
|
||||
bool Core::Conference::getJoined() const {
|
||||
return joined;
|
||||
}
|
||||
|
||||
void Core::Conference::setJoined(bool p_joined)
|
||||
{
|
||||
void Core::Conference::setJoined(bool p_joined) {
|
||||
if (joined != p_joined) {
|
||||
if (p_joined) {
|
||||
if (p_joined)
|
||||
room->join();
|
||||
} else {
|
||||
else
|
||||
room->leave();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::setAutoJoin(bool p_autoJoin)
|
||||
{
|
||||
void Core::Conference::setAutoJoin(bool p_autoJoin) {
|
||||
if (autoJoin != p_autoJoin) {
|
||||
autoJoin = p_autoJoin;
|
||||
emit autoJoinChanged(autoJoin);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::setNick(const QString& p_nick)
|
||||
{
|
||||
void Core::Conference::setNick(const QString& p_nick) {
|
||||
if (nick != p_nick) {
|
||||
if (joined) {
|
||||
room->setNickName(p_nick);
|
||||
|
@ -100,86 +94,127 @@ void Core::Conference::setNick(const QString& p_nick)
|
|||
}
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomJoined()
|
||||
{
|
||||
void Core::Conference::onRoomJoined() {
|
||||
joined = true;
|
||||
emit joinedChanged(joined);
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomLeft()
|
||||
{
|
||||
void Core::Conference::onRoomLeft() {
|
||||
joined = false;
|
||||
emit joinedChanged(joined);
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomNameChanged(const QString& p_name)
|
||||
{
|
||||
void Core::Conference::onRoomNameChanged(const QString& p_name) {
|
||||
setName(p_name);
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomNickNameChanged(const QString& p_nick)
|
||||
{
|
||||
void Core::Conference::onRoomNickNameChanged(const QString& p_nick) {
|
||||
if (p_nick != nick) {
|
||||
nick = p_nick;
|
||||
emit nickChanged(nick);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomError(const QXmppStanza::Error& err)
|
||||
{
|
||||
void Core::Conference::onRoomError(const QXmppStanza::Error& err) {
|
||||
qDebug() << "MUC" << jid << "error:" << err.text();
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomParticipantAdded(const QString& p_name)
|
||||
{
|
||||
void Core::Conference::onRoomParticipantAdded(const QString& p_name) {
|
||||
QStringList comps = p_name.split("/");
|
||||
QString resource = comps.back();
|
||||
if (resource == jid) {
|
||||
qDebug() << "Room" << jid << "is reporting of adding itself to the list participants. Not sure what to do with that yet, skipping";
|
||||
} else {
|
||||
QXmppPresence pres = room->participantPresence(p_name);
|
||||
QXmppPresence pres = room->participantPresence(p_name);
|
||||
QXmppMucItem mi = pres.mucItem();
|
||||
if (resource == jid)
|
||||
resource = "";
|
||||
|
||||
std::map<QString, Archive::AvatarInfo>::const_iterator itr = exParticipants.find(resource);
|
||||
bool hasAvatar = itr != exParticipants.end();
|
||||
|
||||
if (resource.size() > 0) {
|
||||
QDateTime lastInteraction = pres.lastUserInteraction();
|
||||
if (!lastInteraction.isValid()) {
|
||||
lastInteraction = QDateTime::currentDateTime();
|
||||
}
|
||||
QXmppMucItem mi = pres.mucItem();
|
||||
if (!lastInteraction.isValid())
|
||||
lastInteraction = QDateTime::currentDateTimeUtc();
|
||||
|
||||
emit addParticipant(resource, {
|
||||
QMap<QString, QVariant> cData = {
|
||||
{"lastActivity", lastInteraction},
|
||||
{"availability", pres.availableStatusType()},
|
||||
{"status", pres.statusText()},
|
||||
{"affiliation", mi.affiliation()},
|
||||
{"role", mi.role()}
|
||||
});
|
||||
{"role", mi.role()},
|
||||
{"client", QVariant::fromValue(
|
||||
Shared::ClientId(
|
||||
pres.capabilityNode(),
|
||||
pres.capabilityVer().toBase64(),
|
||||
pres.capabilityHash())
|
||||
)
|
||||
}
|
||||
};
|
||||
careAboutAvatar(hasAvatar, itr->second, cData, resource, p_name);
|
||||
|
||||
emit addParticipant(resource, cData);
|
||||
|
||||
if (!hasAvatar) // because this way vCard is already requested, no need to handle possible avatar update
|
||||
return;
|
||||
}
|
||||
handlePossibleAvatarUpdate(pres, resource, hasAvatar, itr->second);
|
||||
}
|
||||
|
||||
void Core::Conference::handlePossibleAvatarUpdate (
|
||||
const QXmppPresence& pres,
|
||||
const QString& resource,
|
||||
bool hasAvatar,
|
||||
const Archive::AvatarInfo& info
|
||||
) {
|
||||
switch (pres.vCardUpdateType()) {
|
||||
case QXmppPresence::VCardUpdateNone: //this presence has nothing to do with photo
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateNotReady: //let's say the photo didn't change here
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateNoPhoto: //there is no photo, need to drop if any
|
||||
if (!hasAvatar || !info.autogenerated)
|
||||
setAutoGeneratedAvatar(resource);
|
||||
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateValidPhoto: //there is a photo, need to load
|
||||
if (hasAvatar) {
|
||||
if (info.autogenerated || info.hash != pres.photoHash())
|
||||
emit requestVCard(pres.from());
|
||||
} else {
|
||||
emit requestVCard(pres.from());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomParticipantChanged(const QString& p_name)
|
||||
{
|
||||
void Core::Conference::onRoomParticipantChanged(const QString& p_name) {
|
||||
QStringList comps = p_name.split("/");
|
||||
QString resource = comps.back();
|
||||
if (resource == jid) {
|
||||
qDebug() << "Room" << jid << "is reporting of changing his own presence. Not sure what to do with that yet, skipping";
|
||||
} else {
|
||||
QXmppPresence pres = room->participantPresence(p_name);
|
||||
QXmppPresence pres = room->participantPresence(p_name);
|
||||
QXmppMucItem mi = pres.mucItem();
|
||||
handlePresence(pres);
|
||||
if (resource != jid) {
|
||||
QDateTime lastInteraction = pres.lastUserInteraction();
|
||||
if (!lastInteraction.isValid()) {
|
||||
lastInteraction = QDateTime::currentDateTime();
|
||||
}
|
||||
QXmppMucItem mi = pres.mucItem();
|
||||
if (!lastInteraction.isValid())
|
||||
lastInteraction = QDateTime::currentDateTimeUtc();
|
||||
|
||||
emit changeParticipant(resource, {
|
||||
{"lastActivity", lastInteraction},
|
||||
{"availability", pres.availableStatusType()},
|
||||
{"status", pres.statusText()},
|
||||
{"affiliation", mi.affiliation()},
|
||||
{"role", mi.role()}
|
||||
{"role", mi.role()},
|
||||
{"client", QVariant::fromValue(
|
||||
Shared::ClientId(
|
||||
pres.capabilityNode(),
|
||||
pres.capabilityVer().toBase64(),
|
||||
pres.capabilityHash())
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomParticipantRemoved(const QString& p_name)
|
||||
{
|
||||
void Core::Conference::onRoomParticipantRemoved(const QString& p_name) {
|
||||
QStringList comps = p_name.split("/");
|
||||
QString resource = comps.back();
|
||||
if (resource == jid) {
|
||||
|
@ -189,16 +224,104 @@ void Core::Conference::onRoomParticipantRemoved(const QString& p_name)
|
|||
}
|
||||
}
|
||||
|
||||
QString Core::Conference::getSubject() const
|
||||
{
|
||||
if (joined) {
|
||||
QString Core::Conference::getSubject() const {
|
||||
if (joined)
|
||||
return room->subject();
|
||||
} else {
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Conference::onRoomSubjectChanged(const QString& p_name)
|
||||
{
|
||||
void Core::Conference::onRoomSubjectChanged(const QString& p_name) {
|
||||
emit subjectChanged(p_name);
|
||||
}
|
||||
|
||||
void Core::Conference::handlePresence(const QXmppPresence& pres) {
|
||||
QString id = pres.from();
|
||||
QStringList comps = id.split("/");
|
||||
QString jid = comps.front();
|
||||
QString resource("");
|
||||
if (comps.size() > 1)
|
||||
resource = comps.back();
|
||||
|
||||
Archive::AvatarInfo info;
|
||||
bool hasAvatar = readAvatarInfo(info, resource);
|
||||
handlePossibleAvatarUpdate(pres, resource, hasAvatar, info);
|
||||
}
|
||||
|
||||
bool Core::Conference::setAutoGeneratedAvatar(const QString& resource) {
|
||||
Archive::AvatarInfo newInfo;
|
||||
bool result = RosterItem::setAutoGeneratedAvatar(newInfo, resource);
|
||||
if (result && resource.size() != 0) {
|
||||
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
|
||||
if (itr == exParticipants.end())
|
||||
exParticipants.insert(std::make_pair(resource, newInfo));
|
||||
else
|
||||
itr->second = newInfo;
|
||||
|
||||
emit changeParticipant(resource, {
|
||||
{"avatarState", static_cast<uint>(Shared::Avatar::autocreated)},
|
||||
{"avatarPath", avatarPath(resource) + "." + newInfo.type}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Core::Conference::setAvatar(const QByteArray& data, Archive::AvatarInfo& info, const QString& resource) {
|
||||
bool result = RosterItem::setAvatar(data, info, resource);
|
||||
if (result && resource.size() != 0) {
|
||||
if (data.size() > 0) {
|
||||
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
|
||||
if (itr == exParticipants.end())
|
||||
exParticipants.insert(std::make_pair(resource, info));
|
||||
else
|
||||
itr->second = info;
|
||||
|
||||
emit changeParticipant(resource, {
|
||||
{"avatarState", static_cast<uint>(Shared::Avatar::autocreated)},
|
||||
{"avatarPath", avatarPath(resource) + "." + info.type}
|
||||
});
|
||||
} else {
|
||||
std::map<QString, Archive::AvatarInfo>::iterator itr = exParticipants.find(resource);
|
||||
if (itr != exParticipants.end())
|
||||
exParticipants.erase(itr);
|
||||
|
||||
emit changeParticipant(resource, {
|
||||
{"avatarState", static_cast<uint>(Shared::Avatar::empty)},
|
||||
{"avatarPath", ""}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Core::Conference::handleResponseVCard(const QXmppVCardIq& card, const QString &resource, Shared::VCard& out) {
|
||||
RosterItem::handleResponseVCard(card, resource, out);
|
||||
if (resource.size() > 0)
|
||||
emit changeParticipant(resource, {
|
||||
{"avatarState", static_cast<uint>(out.getAvatarType())},
|
||||
{"avatarPath", out.getAvatarPath()}
|
||||
});
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> Core::Conference::getAllAvatars() const {
|
||||
QMap<QString, QVariant> result;
|
||||
for (const std::pair<const QString, Archive::AvatarInfo>& pair : exParticipants)
|
||||
result.insert(pair.first, avatarPath(pair.first) + "." + pair.second.type);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> Core::Conference::getInfo() const {
|
||||
QMap<QString, QVariant> data = RosterItem::getInfo();
|
||||
|
||||
data.insert("autoJoin", getAutoJoin());
|
||||
data.insert("joined", getJoined());
|
||||
data.insert("nick", getNick());
|
||||
data.insert("avatars", getAllAvatars());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,20 +16,20 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CORE_CONFERENCE_H
|
||||
#define CORE_CONFERENCE_H
|
||||
#pragma once
|
||||
|
||||
#include <QDir>
|
||||
|
||||
#include "rosteritem.h"
|
||||
#include <QXmppMucManager.h>
|
||||
|
||||
namespace Core
|
||||
{
|
||||
#include <set>
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class Conference : public RosterItem
|
||||
{
|
||||
#include "rosteritem.h"
|
||||
#include <shared/global.h>
|
||||
#include <shared/clientid.h>
|
||||
|
||||
namespace Core {
|
||||
class Conference : public RosterItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Conference(const QString& p_jid, const QString& p_account, bool p_autoJoin, const QString& p_name, const QString& p_nick, QXmppMucRoom* p_room);
|
||||
|
@ -42,8 +42,13 @@ public:
|
|||
bool getJoined() const;
|
||||
void setJoined(bool p_joined);
|
||||
|
||||
bool getAutoJoin();
|
||||
bool getAutoJoin() const;
|
||||
void setAutoJoin(bool p_autoJoin);
|
||||
void handlePresence(const QXmppPresence & pres) override;
|
||||
bool setAutoGeneratedAvatar(const QString& resource = "") override;
|
||||
void handleResponseVCard(const QXmppVCardIq & card, const QString &resource, Shared::VCard& out) override;
|
||||
QMap<QString, QVariant> getAllAvatars() const;
|
||||
QMap<QString, QVariant> getInfo() const override;
|
||||
|
||||
signals:
|
||||
void nickChanged(const QString& nick);
|
||||
|
@ -54,11 +59,24 @@ signals:
|
|||
void changeParticipant(const QString& name, const QMap<QString, QVariant>& data);
|
||||
void removeParticipant(const QString& name);
|
||||
|
||||
protected:
|
||||
bool setAvatar(const QByteArray &data, Archive::AvatarInfo& info, const QString &resource = "") override;
|
||||
|
||||
private:
|
||||
void handlePossibleAvatarUpdate(
|
||||
const QXmppPresence& pres,
|
||||
const QString& resource,
|
||||
bool hasAvatar,
|
||||
const Archive::AvatarInfo& info
|
||||
);
|
||||
|
||||
private:
|
||||
QString nick;
|
||||
QXmppMucRoom* room;
|
||||
bool joined;
|
||||
bool autoJoin;
|
||||
std::map<QString, Archive::AvatarInfo> exParticipants;
|
||||
static const std::set<QString> supportedList;
|
||||
|
||||
private slots:
|
||||
void onRoomJoined();
|
||||
|
@ -74,5 +92,3 @@ private slots:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // CORE_CONFERENCE_H
|
||||
|
|
|
@ -22,49 +22,92 @@
|
|||
Core::Contact::Contact(const QString& pJid, const QString& account, QObject* parent):
|
||||
RosterItem(pJid, account, parent),
|
||||
groups(),
|
||||
subscriptionState(Shared::unknown)
|
||||
{
|
||||
}
|
||||
subscriptionState(Shared::SubscriptionState::unknown),
|
||||
pep(Shared::Support::unknown)
|
||||
|
||||
Core::Contact::~Contact()
|
||||
{
|
||||
}
|
||||
#ifdef WITH_OMEMO
|
||||
,omemoBundles(Shared::Possible::unknown)
|
||||
#endif
|
||||
{}
|
||||
|
||||
QSet<QString> Core::Contact::getGroups() const
|
||||
{
|
||||
Core::Contact::~Contact() {}
|
||||
|
||||
QSet<QString> Core::Contact::getGroups() const {
|
||||
return groups;
|
||||
}
|
||||
|
||||
unsigned int Core::Contact::groupsCount() const
|
||||
{
|
||||
unsigned int Core::Contact::groupsCount() const {
|
||||
return groups.size();
|
||||
}
|
||||
|
||||
void Core::Contact::setGroups(const QSet<QString>& set)
|
||||
{
|
||||
void Core::Contact::setGroups(const QSet<QString>& set) {
|
||||
QSet<QString> toRemove = groups - set;
|
||||
QSet<QString> toAdd = set - groups;
|
||||
|
||||
groups = set;
|
||||
|
||||
for (QSet<QString>::iterator itr = toRemove.begin(), end = toRemove.end(); itr != end; ++itr) {
|
||||
emit groupRemoved(*itr);
|
||||
}
|
||||
for (const QString& group : toRemove)
|
||||
emit groupRemoved(group);
|
||||
|
||||
for (QSet<QString>::iterator itr = toAdd.begin(), end = toAdd.end(); itr != end; ++itr) {
|
||||
emit groupAdded(*itr);
|
||||
}
|
||||
for (const QString& group : toAdd)
|
||||
emit groupAdded(group);
|
||||
}
|
||||
|
||||
Shared::SubscriptionState Core::Contact::getSubscriptionState() const
|
||||
{
|
||||
Shared::SubscriptionState Core::Contact::getSubscriptionState() const {
|
||||
return subscriptionState;
|
||||
}
|
||||
|
||||
void Core::Contact::setSubscriptionState(Shared::SubscriptionState state)
|
||||
{
|
||||
void Core::Contact::setSubscriptionState(Shared::SubscriptionState state) {
|
||||
if (subscriptionState != state) {
|
||||
subscriptionState = state;
|
||||
emit subscriptionStateChanged(subscriptionState);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Contact::handlePresence(const QXmppPresence& pres) {
|
||||
switch (pres.vCardUpdateType()) {
|
||||
case QXmppPresence::VCardUpdateNone: //this presence has nothing to do with photo
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateNotReady: //let's say the photo didn't change here
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateNoPhoto: { //there is no photo, need to drop if any
|
||||
Archive::AvatarInfo info;
|
||||
bool hasAvatar = readAvatarInfo(info);
|
||||
if (!hasAvatar || !info.autogenerated)
|
||||
setAutoGeneratedAvatar();
|
||||
|
||||
}
|
||||
break;
|
||||
case QXmppPresence::VCardUpdateValidPhoto:{ //there is a photo, need to load
|
||||
Archive::AvatarInfo info;
|
||||
bool hasAvatar = readAvatarInfo(info);
|
||||
if (hasAvatar) {
|
||||
if (info.autogenerated || info.hash != pres.photoHash())
|
||||
emit requestVCard(jid);
|
||||
} else {
|
||||
emit requestVCard(jid);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Contact::setPepSupport(Shared::Support support) {
|
||||
if (pep != support)
|
||||
pep = support;
|
||||
}
|
||||
|
||||
Shared::Support Core::Contact::getPepSupport() const {
|
||||
return pep;
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> Core::Contact::getInfo() const {
|
||||
QMap<QString, QVariant> data = RosterItem::getInfo();
|
||||
|
||||
data.insert("state", QVariant::fromValue(subscriptionState));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -16,17 +16,18 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CORE_CONTACT_H
|
||||
#define CORE_CONTACT_H
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QSet>
|
||||
|
||||
#include "rosteritem.h"
|
||||
|
||||
#include <shared/enums.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class Contact : public RosterItem
|
||||
{
|
||||
class Contact : public RosterItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Contact(const QString& pJid, const QString& account, QObject* parent = 0);
|
||||
|
@ -38,6 +39,11 @@ public:
|
|||
|
||||
void setSubscriptionState(Shared::SubscriptionState state);
|
||||
Shared::SubscriptionState getSubscriptionState() const;
|
||||
void setPepSupport(Shared::Support support);
|
||||
Shared::Support getPepSupport() const;
|
||||
|
||||
void handlePresence(const QXmppPresence & pres) override;
|
||||
QMap<QString, QVariant> getInfo() const override;
|
||||
|
||||
signals:
|
||||
void groupAdded(const QString& name);
|
||||
|
@ -47,7 +53,11 @@ signals:
|
|||
private:
|
||||
QSet<QString> groups;
|
||||
Shared::SubscriptionState subscriptionState;
|
||||
Shared::Support pep;
|
||||
|
||||
#ifdef WITH_OMEMO
|
||||
public:
|
||||
Shared::Possible omemoBundles;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CORE_CONTACT_H
|
||||
|
|
26
core/delayManager/CMakeLists.txt
Normal file
26
core/delayManager/CMakeLists.txt
Normal file
|
@ -0,0 +1,26 @@
|
|||
set(SOURCE_FILES
|
||||
manager.cpp
|
||||
job.cpp
|
||||
cardinternal.cpp
|
||||
infoforuser.cpp
|
||||
owncardinternal.cpp
|
||||
owninfoforuser.cpp
|
||||
contact.cpp
|
||||
info.cpp
|
||||
)
|
||||
|
||||
set(HEADER_FILES
|
||||
manager.h
|
||||
job.h
|
||||
cardinternal.h
|
||||
infoforuser.h
|
||||
owncardinternal.h
|
||||
owninfoforuser.h
|
||||
contact.h
|
||||
info.h
|
||||
)
|
||||
|
||||
target_sources(squawk PRIVATE
|
||||
${SOURCE_FILES}
|
||||
${HEADER_FILES}
|
||||
)
|
30
core/delayManager/cardinternal.cpp
Normal file
30
core/delayManager/cardinternal.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "cardinternal.h"
|
||||
|
||||
Core::DelayManager::CardInternal::CardInternal(Id p_id, const QString& p_jid) :
|
||||
Job(p_id, Type::cardInternal),
|
||||
Contact(p_id, p_jid, Type::cardInternal)
|
||||
{}
|
||||
|
||||
|
||||
Core::DelayManager::CardInternal::CardInternal(const CardInternal& other) :
|
||||
Job(other),
|
||||
Contact(other)
|
||||
{}
|
35
core/delayManager/cardinternal.h
Normal file
35
core/delayManager/cardinternal.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include "contact.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class CardInternal : public Contact {
|
||||
public:
|
||||
CardInternal(Id id, const QString& jid);
|
||||
CardInternal(const CardInternal& other);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
28
core/delayManager/contact.cpp
Normal file
28
core/delayManager/contact.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "contact.h"
|
||||
|
||||
Core::DelayManager::Contact::Contact(const Contact& other):
|
||||
Job(other),
|
||||
jid(other.jid) {}
|
||||
|
||||
|
||||
Core::DelayManager::Contact::Contact(Id p_id, const QString& p_jid, Type p_type):
|
||||
Job(p_id, p_type),
|
||||
jid(p_jid) {}
|
38
core/delayManager/contact.h
Normal file
38
core/delayManager/contact.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include "job.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class Contact : public virtual Job {
|
||||
protected:
|
||||
Contact(Id id, const QString& jid, Type type);
|
||||
Contact(const Contact& other);
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
61
core/delayManager/info.cpp
Normal file
61
core/delayManager/info.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "info.h"
|
||||
|
||||
Core::DelayManager::Info::Info(Id p_id, Type p_type) :
|
||||
Job(p_id, p_type),
|
||||
stage(Stage::waitingForVCard),
|
||||
info(nullptr)
|
||||
{}
|
||||
|
||||
Core::DelayManager::Info::Info(const Info& other) :
|
||||
Job(other),
|
||||
stage(other.stage),
|
||||
info(nullptr)
|
||||
{}
|
||||
|
||||
Core::DelayManager::Info::~Info() {
|
||||
if (stage == Stage::waitingForBundles) {
|
||||
delete info;
|
||||
}
|
||||
}
|
||||
|
||||
Core::DelayManager::Info::Stage Core::DelayManager::Info::getStage() const {
|
||||
return stage;
|
||||
}
|
||||
|
||||
void Core::DelayManager::Info::receivedVCard(const Shared::VCard& card) {
|
||||
if (stage != Stage::waitingForVCard)
|
||||
throw 245;
|
||||
|
||||
info = new Shared::VCard(card);
|
||||
|
||||
#ifdef WITH_OMEMO
|
||||
stage = Stage::waitingForBundles;
|
||||
#endif
|
||||
}
|
||||
|
||||
Shared::VCard * Core::DelayManager::Info::claim() {
|
||||
if (stage != Stage::waitingForBundles)
|
||||
throw 246;
|
||||
|
||||
Shared::VCard* res = info;
|
||||
info = nullptr;
|
||||
return res;
|
||||
}
|
55
core/delayManager/info.h
Normal file
55
core/delayManager/info.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "job.h"
|
||||
|
||||
#include <shared/vcard.h>
|
||||
#include <shared/info.h>
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class Info : public virtual Job {
|
||||
public:
|
||||
enum class Stage {
|
||||
waitingForVCard,
|
||||
waitingForBundles,
|
||||
finished
|
||||
};
|
||||
|
||||
protected:
|
||||
Info(Id id, Type type);
|
||||
Info(const Info& other);
|
||||
|
||||
public:
|
||||
~Info();
|
||||
|
||||
void receivedVCard(const Shared::VCard& card);
|
||||
Shared::VCard* claim();
|
||||
Stage getStage() const;
|
||||
|
||||
|
||||
private:
|
||||
Stage stage;
|
||||
Shared::VCard* info;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
31
core/delayManager/infoforuser.cpp
Normal file
31
core/delayManager/infoforuser.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "infoforuser.h"
|
||||
|
||||
Core::DelayManager::InfoForUser::InfoForUser(Id p_id, const QString& p_jid) :
|
||||
Job(p_id, Type::infoForUser),
|
||||
Contact(p_id, p_jid, Type::infoForUser),
|
||||
Info(p_id, Type::infoForUser)
|
||||
{}
|
||||
|
||||
Core::DelayManager::InfoForUser::InfoForUser(const InfoForUser& other) :
|
||||
Job(other),
|
||||
Contact(other),
|
||||
Info(other)
|
||||
{}
|
34
core/delayManager/infoforuser.h
Normal file
34
core/delayManager/infoforuser.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "contact.h"
|
||||
#include "info.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class InfoForUser : public Contact, public Info {
|
||||
public:
|
||||
InfoForUser(Id id, const QString& jid);
|
||||
InfoForUser(const InfoForUser& other);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
30
core/delayManager/job.cpp
Normal file
30
core/delayManager/job.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "job.h"
|
||||
|
||||
Core::DelayManager::Job::Job(Id p_id, Type p_type) :
|
||||
id (p_id),
|
||||
type (p_type) {}
|
||||
|
||||
|
||||
Core::DelayManager::Job::Job(const Job& other) :
|
||||
id(other.id),
|
||||
type(other.type) {}
|
||||
|
||||
Core::DelayManager::Job::~Job() {}
|
54
core/delayManager/job.h
Normal file
54
core/delayManager/job.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class Job {
|
||||
public:
|
||||
typedef uint16_t Id;
|
||||
|
||||
enum class Type {
|
||||
cardInternal,
|
||||
ownCardInternal,
|
||||
infoForUser,
|
||||
ownInfoForUser
|
||||
};
|
||||
inline static constexpr const char * const TypeString[] = {
|
||||
"cardInternal",
|
||||
"ownCardInternal",
|
||||
"infoForUser",
|
||||
"ownInfoForUser"
|
||||
};
|
||||
protected:
|
||||
Job(Id id, Type type);
|
||||
Job(const Job& other);
|
||||
|
||||
public:
|
||||
virtual ~Job();
|
||||
|
||||
const Id id;
|
||||
const Type type;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
439
core/delayManager/manager.cpp
Normal file
439
core/delayManager/manager.cpp
Normal file
|
@ -0,0 +1,439 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "manager.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "cardinternal.h"
|
||||
#include "infoforuser.h"
|
||||
#include "owncardinternal.h"
|
||||
#include "owninfoforuser.h"
|
||||
|
||||
Core::DelayManager::Manager::Manager(const QString& poj, Job::Id mpj, QObject* parent) :
|
||||
QObject(parent),
|
||||
maxParallelJobs(mpj),
|
||||
nextJobId(1),
|
||||
scheduledJobs(),
|
||||
scheduledJobsById(scheduledJobs.get<id>()),
|
||||
jobSequence(scheduledJobs.get<sequence>()),
|
||||
runningJobs(),
|
||||
ownVCardJobId(0),
|
||||
ownInfoJobId(0),
|
||||
scheduledVCards(),
|
||||
requestedVCards(),
|
||||
#ifdef WITH_OMEMO
|
||||
requestedBundles(),
|
||||
#endif
|
||||
ownJid(poj)
|
||||
{}
|
||||
|
||||
Core::DelayManager::Manager::~Manager() {
|
||||
for (const std::pair<const Job::Id, Job*>& pair : runningJobs)
|
||||
delete pair.second;
|
||||
|
||||
for (Job* job : jobSequence)
|
||||
delete job;
|
||||
}
|
||||
|
||||
Core::DelayManager::Job::Id Core::DelayManager::Manager::getNextJobId() {
|
||||
Job::Id id = nextJobId++;
|
||||
if (id == 0)
|
||||
id = nextJobId++;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::getInfo(const QString& jid) {
|
||||
if (jid == ownJid)
|
||||
return getOwnInfo();
|
||||
|
||||
Job* job = nullptr;
|
||||
#ifdef WITH_OMEMO
|
||||
std::map<QString, Job::Id>::const_iterator bitr = requestedBundles.find(jid);
|
||||
if (bitr != requestedBundles.end()) {
|
||||
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(bitr->second);
|
||||
if (itr == runningJobs.end())
|
||||
throw JobNotFound(bitr->second);
|
||||
|
||||
job = itr->second;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
job = getVCardJob(jid);
|
||||
|
||||
if (job != nullptr) {
|
||||
if (job->type == Job::Type::cardInternal)
|
||||
replaceJob(new InfoForUser(job->id, jid));
|
||||
} else
|
||||
scheduleJob(new InfoForUser(getNextJobId(), jid));
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::getOwnInfo() {
|
||||
if (ownInfoJobId == 0) {
|
||||
if (ownVCardJobId != 0)
|
||||
replaceJob(new OwnInfoForUser(ownVCardJobId));
|
||||
else
|
||||
scheduleJob(new OwnInfoForUser(getNextJobId()));
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::getVCard(const QString& jid) {
|
||||
Job* job = getVCardJob(jid);
|
||||
if (job == nullptr)
|
||||
scheduleJob(new CardInternal(getNextJobId(), jid));
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::getOwnVCard() {
|
||||
if (ownVCardJobId == 0)
|
||||
scheduleJob(new OwnCardInternal(getNextJobId()));
|
||||
}
|
||||
|
||||
bool Core::DelayManager::Manager::isOwnVCardPending() const {
|
||||
return ownVCardJobId != 0;
|
||||
}
|
||||
|
||||
Core::DelayManager::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) {
|
||||
Job* job = nullptr;
|
||||
std::map<QString, Job::Id>::const_iterator sitr = scheduledVCards.find(jid);
|
||||
if (sitr == scheduledVCards.end()) {
|
||||
std::map<QString, Job::Id>::const_iterator ritr = requestedVCards.find(jid);
|
||||
if (ritr != requestedVCards.end()) {
|
||||
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(ritr->second);
|
||||
if (itr == runningJobs.end())
|
||||
throw JobNotFound(ritr->second, "getVCardJob:1");
|
||||
|
||||
job = itr->second;
|
||||
}
|
||||
} else {
|
||||
StorageById::const_iterator itr = scheduledJobsById.find(sitr->second);
|
||||
if (itr == scheduledJobsById.end())
|
||||
throw JobNotFound(sitr->second, "getVCardJob:2");
|
||||
|
||||
job = *itr;
|
||||
}
|
||||
|
||||
return job;
|
||||
}
|
||||
void Core::DelayManager::Manager::preScheduleJob(Job* job) {
|
||||
switch (job->type) {
|
||||
case Job::Type::cardInternal:
|
||||
scheduledVCards.emplace(dynamic_cast<CardInternal*>(job)->jid, job->id);
|
||||
break;
|
||||
case Job::Type::ownCardInternal:
|
||||
ownVCardJobId = job->id;
|
||||
break;
|
||||
case Job::Type::infoForUser:
|
||||
scheduledVCards.emplace(dynamic_cast<InfoForUser*>(job)->jid, job->id);
|
||||
break;
|
||||
case Job::Type::ownInfoForUser:
|
||||
ownVCardJobId = job->id;
|
||||
ownInfoJobId = job->id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::scheduleJob(Job* job) {
|
||||
preScheduleJob(job);
|
||||
if (runningJobs.size() < maxParallelJobs)
|
||||
executeJob(job);
|
||||
else
|
||||
scheduledJobs.push_back(job);
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::preExecuteJob(Job* job) {
|
||||
switch (job->type) {
|
||||
case Job::Type::cardInternal:
|
||||
case Job::Type::infoForUser: {
|
||||
Contact* cij = dynamic_cast<Contact*>(job);
|
||||
requestedVCards.emplace(cij->jid, job->id);
|
||||
scheduledVCards.erase(cij->jid);
|
||||
}
|
||||
break;
|
||||
case Job::Type::ownInfoForUser:
|
||||
case Job::Type::ownCardInternal:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::executeJob(Job* job) {
|
||||
preExecuteJob(job);
|
||||
runningJobs.emplace(job->id, job);
|
||||
switch (job->type) {
|
||||
case Job::Type::cardInternal:
|
||||
case Job::Type::infoForUser:
|
||||
emit requestVCard(dynamic_cast<Contact*>(job)->jid);
|
||||
break;
|
||||
case Job::Type::ownInfoForUser:
|
||||
case Job::Type::ownCardInternal:
|
||||
emit requestOwnVCard();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::jobIsDone(Job::Id jobId) {
|
||||
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(jobId);
|
||||
if (itr == runningJobs.end())
|
||||
throw JobNotFound(jobId, "jobIsDone");
|
||||
|
||||
Job* job = itr->second;
|
||||
delete job;
|
||||
runningJobs.erase(itr);
|
||||
if (scheduledJobs.size() > 0) {
|
||||
Job* job = scheduledJobs.front();
|
||||
scheduledJobs.pop_front();
|
||||
executeJob(job);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::replaceJob(Job* job) {
|
||||
preScheduleJob(job);
|
||||
std::map<Job::Id, Job*>::iterator itr = runningJobs.find(job->id);
|
||||
if (itr != runningJobs.end()) {
|
||||
preExecuteJob(job);
|
||||
delete itr->second;
|
||||
itr->second = job;
|
||||
} else {
|
||||
StorageById::iterator sitr = scheduledJobsById.find(job->id);
|
||||
if (sitr != scheduledJobsById.end()) {
|
||||
delete *(sitr);
|
||||
scheduledJobsById.replace(sitr, job);
|
||||
} else
|
||||
throw JobNotFound(job->id, "replaceJob");
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) {
|
||||
switch (job->type) {
|
||||
case Job::Type::cardInternal: {
|
||||
CardInternal* jb = dynamic_cast<CardInternal*>(job);
|
||||
if (wasRunning)
|
||||
requestedVCards.erase(jb->jid);
|
||||
else
|
||||
scheduledVCards.erase(jb->jid);
|
||||
|
||||
emit gotVCard(jb->jid, Shared::VCard());
|
||||
}
|
||||
break;
|
||||
case Job::Type::infoForUser: {
|
||||
InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
|
||||
switch (jb->getStage()) {
|
||||
case InfoForUser::Stage::waitingForVCard:
|
||||
if (wasRunning)
|
||||
requestedVCards.erase(jb->jid);
|
||||
else
|
||||
scheduledVCards.erase(jb->jid);
|
||||
|
||||
emit gotVCard(jb->jid, Shared::VCard());
|
||||
break;
|
||||
case InfoForUser::Stage::waitingForBundles:
|
||||
#ifdef WITH_OMEMO
|
||||
requestedBundles.erase(jb->jid);
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
emit gotInfo(Shared::Info(jb->jid));
|
||||
}
|
||||
break;
|
||||
case Job::Type::ownInfoForUser: {
|
||||
OwnInfoForUser* jb = dynamic_cast<OwnInfoForUser*>(job);
|
||||
if (jb->getStage() == OwnInfoForUser::Stage::waitingForVCard) {
|
||||
ownVCardJobId = 0;
|
||||
emit gotOwnVCard(Shared::VCard());
|
||||
}
|
||||
ownInfoJobId = 0;
|
||||
emit gotOwnInfo(Shared::Info (ownJid));
|
||||
}
|
||||
|
||||
break;
|
||||
case Job::Type::ownCardInternal:
|
||||
ownVCardJobId = 0;
|
||||
emit gotOwnVCard(Shared::VCard());
|
||||
break;
|
||||
}
|
||||
|
||||
delete job;
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::disconnected() {
|
||||
for (const std::pair<const Job::Id, Job*> pair : runningJobs)
|
||||
jobIsCanceled(pair.second, true);
|
||||
|
||||
for (Job* job : scheduledJobs)
|
||||
jobIsCanceled(job, false);
|
||||
|
||||
runningJobs.clear();
|
||||
scheduledJobs.clear();
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared::VCard& card) {
|
||||
std::map<QString, Job::Id>::const_iterator cardItr = requestedVCards.find(jid);
|
||||
if (cardItr == requestedVCards.end()) {
|
||||
qDebug() << "received VCard for" << jid << "but it was never requested through manager, ignoring";
|
||||
return;
|
||||
}
|
||||
Job::Id jobId = cardItr->second;
|
||||
requestedVCards.erase(cardItr);
|
||||
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(jobId);
|
||||
if (itr == runningJobs.end())
|
||||
throw JobNotFound(jobId, "receivedVCard");
|
||||
|
||||
Job* job = itr->second;
|
||||
|
||||
switch (job->type) {
|
||||
case Job::Type::cardInternal:
|
||||
jobIsDone(jobId);
|
||||
emit gotVCard(jid, card);
|
||||
break;
|
||||
case Job::Type::infoForUser: {
|
||||
#ifdef WITH_OMEMO
|
||||
requestedBundles.emplace(jid, jobId);
|
||||
InfoForUser* jb = dynamic_cast<InfoForUser*>(job);
|
||||
jb->receivedVCard(card);
|
||||
emit requestBundles(jid);
|
||||
#else
|
||||
Shared::Info info(jid);
|
||||
info.turnIntoContact(card);
|
||||
emit gotInfo(info);
|
||||
jobIsDone(jobId);
|
||||
#endif
|
||||
emit gotVCard(jid, card);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw UnexpectedJobType(job->type, "receivedVCard");
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::receivedOwnVCard(const Shared::VCard& card) {
|
||||
if (ownVCardJobId == 0) {
|
||||
qDebug() << "received own VCard for" << ownJid << "but it was never requested through manager, ignoring";
|
||||
return;
|
||||
}
|
||||
Job::Id jobId = ownVCardJobId;
|
||||
ownVCardJobId = 0;
|
||||
std::map<Job::Id, Job*>::const_iterator itr = runningJobs.find(jobId);
|
||||
if (itr == runningJobs.end())
|
||||
throw JobNotFound(jobId, "receivedOwnVCard");
|
||||
|
||||
Job* job = itr->second;
|
||||
switch (job->type) {
|
||||
case Job::Type::ownCardInternal:
|
||||
jobIsDone(jobId);
|
||||
emit gotOwnVCard(card);
|
||||
break;
|
||||
case Job::Type::ownInfoForUser: {
|
||||
#ifdef WITH_OMEMO
|
||||
OwnInfoForUser* jb = dynamic_cast<OwnInfoForUser*>(job);
|
||||
jb->receivedVCard(card);
|
||||
emit requestOwnBundles();
|
||||
#else
|
||||
Shared::Info info(ownJid);
|
||||
info.turnIntoOwnAccount(card);
|
||||
emit gotOwnInfo(info);
|
||||
jobIsDone(jobId);
|
||||
#endif
|
||||
emit gotOwnVCard(card);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw UnexpectedJobType(job->type, "receivedVCard");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_OMEMO
|
||||
void Core::DelayManager::Manager::receivedBundles(const QString& jid, const std::list<Shared::KeyInfo>& keys) {
|
||||
std::map<QString, Job::Id>::const_iterator itr = requestedBundles.find(jid);
|
||||
if (itr == requestedBundles.end()) {
|
||||
qDebug() << "received bundles for" << jid << "but they were never requested through manager, ignoring";
|
||||
return;
|
||||
}
|
||||
|
||||
Job::Id jobId = itr->second;
|
||||
requestedBundles.erase(itr);
|
||||
std::map<Job::Id, Job*>::const_iterator jitr = runningJobs.find(jobId);
|
||||
if (jitr == runningJobs.end())
|
||||
throw JobNotFound(jobId, "receivedBundles");
|
||||
|
||||
Job* jb = jitr->second;
|
||||
InfoForUser* job = dynamic_cast<InfoForUser*>(jb);
|
||||
|
||||
Shared::Info info(jid);
|
||||
info.turnIntoContact(job->claim(), new std::list<Shared::KeyInfo>(keys));
|
||||
emit gotInfo(info);
|
||||
jobIsDone(jobId);
|
||||
}
|
||||
|
||||
void Core::DelayManager::Manager::receivedOwnBundles(const std::list<Shared::KeyInfo>& keys) {
|
||||
if (ownInfoJobId == 0) {
|
||||
qDebug() << "received own bundles for" << ownJid << "but they were never requested through manager, ignoring";
|
||||
return;
|
||||
}
|
||||
Job::Id jobId = ownInfoJobId;
|
||||
ownInfoJobId = 0;
|
||||
std::map<Job::Id, Job*>::const_iterator jitr = runningJobs.find(jobId);
|
||||
if (jitr == runningJobs.end())
|
||||
throw JobNotFound(jobId, "receivedOwnBundles");
|
||||
|
||||
Job* jb = jitr->second;
|
||||
OwnInfoForUser* job = dynamic_cast<OwnInfoForUser*>(jb);
|
||||
|
||||
Shared::Info info(ownJid);
|
||||
info.turnIntoOwnAccount(job->claim(), new std::list<Shared::KeyInfo>(keys));
|
||||
emit gotOwnInfo(info);
|
||||
jobIsDone(jobId);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Core::DelayManager::Manager::setOwnJid(const QString& jid) {
|
||||
ownJid = jid;
|
||||
}
|
||||
|
||||
|
||||
Core::DelayManager::Manager::UnexpectedJobType::UnexpectedJobType(Job::Type p_type, const std::string& p_method):
|
||||
Exception(),
|
||||
type(p_type),
|
||||
method(p_method)
|
||||
{}
|
||||
|
||||
std::string Core::DelayManager::Manager::UnexpectedJobType::getMessage() const{
|
||||
std::string msg("Unexpected job type: ");
|
||||
msg += Job::TypeString[static_cast<int>(type)];
|
||||
if (method.size() > 0)
|
||||
msg += " in method " + method;
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
Core::DelayManager::Manager::JobNotFound::JobNotFound(Job::Id p_id, const std::string& p_method) :
|
||||
Exception(),
|
||||
id(p_id),
|
||||
method(p_method)
|
||||
{}
|
||||
|
||||
std::string Core::DelayManager::Manager::JobNotFound::getMessage() const {
|
||||
std::string msg("Job with id ");
|
||||
msg += std::to_string(id);
|
||||
msg += " was not found";
|
||||
if (method.size() > 0)
|
||||
msg += " in method " + method;
|
||||
|
||||
return msg;
|
||||
}
|
153
core/delayManager/manager.h
Normal file
153
core/delayManager/manager.h
Normal file
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include <boost/multi_index_container.hpp>
|
||||
#include <boost/multi_index/ordered_index.hpp>
|
||||
#include <boost/multi_index/sequenced_index.hpp>
|
||||
#include <boost/multi_index/member.hpp>
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include <shared/vcard.h>
|
||||
#include <shared/info.h>
|
||||
#include <shared/exception.h>
|
||||
|
||||
#include "job.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class Manager : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Manager(const QString& ownJid, Job::Id maxParallelJobs = 5, QObject* parent = nullptr);
|
||||
~Manager();
|
||||
|
||||
void setOwnJid(const QString& jid);
|
||||
bool isOwnVCardPending() const;
|
||||
|
||||
public slots:
|
||||
void getOwnVCard();
|
||||
void getOwnInfo();
|
||||
void getVCard(const QString& jid);
|
||||
void getInfo(const QString& jid);
|
||||
|
||||
signals:
|
||||
void requestVCard(const QString& jid);
|
||||
void requestOwnVCard();
|
||||
|
||||
#ifdef WITH_OMEMO
|
||||
void requestBundles(const QString& jid);
|
||||
void requestOwnBundles();
|
||||
#endif
|
||||
|
||||
void gotVCard(const QString& jid, const Shared::VCard& info);
|
||||
void gotOwnVCard(const Shared::VCard& info);
|
||||
void gotInfo(const Shared::Info& info);
|
||||
void gotOwnInfo(const Shared::Info& info);
|
||||
|
||||
public slots:
|
||||
void disconnected();
|
||||
void receivedOwnVCard(const Shared::VCard& card);
|
||||
void receivedVCard(const QString& jid, const Shared::VCard& card);
|
||||
|
||||
#ifdef WITH_OMEMO
|
||||
void receivedBundles(const QString& jid, const std::list<Shared::KeyInfo>& keys);
|
||||
void receivedOwnBundles(const std::list<Shared::KeyInfo>& keys);
|
||||
#endif
|
||||
|
||||
private:
|
||||
void preScheduleJob(Job* job);
|
||||
void scheduleJob(Job* job);
|
||||
void preExecuteJob(Job* job);
|
||||
void executeJob(Job* job);
|
||||
void jobIsCanceled(Job* job, bool wasRunning);
|
||||
void jobIsDone(Job::Id jobId);
|
||||
Job::Id getNextJobId();
|
||||
void replaceJob(Job* job);
|
||||
Job* getVCardJob(const QString& jid);
|
||||
|
||||
private:
|
||||
struct id {};
|
||||
struct sequence {};
|
||||
|
||||
typedef boost::multi_index_container<
|
||||
Job*,
|
||||
boost::multi_index::indexed_by<
|
||||
boost::multi_index::sequenced<
|
||||
boost::multi_index::tag<sequence>
|
||||
>,
|
||||
boost::multi_index::ordered_unique<
|
||||
boost::multi_index::tag<id>,
|
||||
boost::multi_index::member<
|
||||
Job,
|
||||
const Job::Id,
|
||||
&Job::id
|
||||
>
|
||||
>
|
||||
>
|
||||
> Storage;
|
||||
|
||||
|
||||
typedef Storage::index<id>::type StorageById;
|
||||
typedef Storage::index<sequence>::type StorageSequence;
|
||||
Job::Id maxParallelJobs;
|
||||
Job::Id nextJobId;
|
||||
|
||||
Storage scheduledJobs;
|
||||
StorageById& scheduledJobsById;
|
||||
StorageSequence& jobSequence;
|
||||
std::map<Job::Id, Job*> runningJobs;
|
||||
|
||||
Job::Id ownVCardJobId;
|
||||
Job::Id ownInfoJobId;
|
||||
std::map<QString, Job::Id> scheduledVCards;
|
||||
std::map<QString, Job::Id> requestedVCards;
|
||||
#ifdef WITH_OMEMO
|
||||
std::map<QString, Job::Id> requestedBundles;
|
||||
#endif
|
||||
QString ownJid;
|
||||
|
||||
public:
|
||||
class UnexpectedJobType: public Utils::Exception {
|
||||
public:
|
||||
UnexpectedJobType(Job::Type p_type, const std::string& p_method = "");
|
||||
std::string getMessage() const override;
|
||||
private:
|
||||
Job::Type type;
|
||||
std::string method;
|
||||
};
|
||||
|
||||
class JobNotFound: public Utils::Exception {
|
||||
public:
|
||||
JobNotFound(Job::Id p_id, const std::string& p_method = "");
|
||||
std::string getMessage() const override;
|
||||
private:
|
||||
Job::Id id;
|
||||
std::string method;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
32
core/delayManager/owncardinternal.cpp
Normal file
32
core/delayManager/owncardinternal.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "owncardinternal.h"
|
||||
|
||||
Core::DelayManager::OwnCardInternal::OwnCardInternal(Id p_id) :
|
||||
Job(p_id, Type::ownCardInternal)
|
||||
{}
|
||||
|
||||
Core::DelayManager::OwnCardInternal::OwnCardInternal(Id p_id, Type p_type) :
|
||||
Job(p_id, p_type)
|
||||
{}
|
||||
|
||||
Core::DelayManager::OwnCardInternal::OwnCardInternal(const OwnCardInternal& other) :
|
||||
Job(other)
|
||||
{}
|
||||
|
36
core/delayManager/owncardinternal.h
Normal file
36
core/delayManager/owncardinternal.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "job.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class OwnCardInternal : public Job {
|
||||
protected:
|
||||
OwnCardInternal(Id id, Type type);
|
||||
|
||||
public:
|
||||
OwnCardInternal(Id id);
|
||||
OwnCardInternal(const OwnCardInternal& other);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
29
core/delayManager/owninfoforuser.cpp
Normal file
29
core/delayManager/owninfoforuser.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "owninfoforuser.h"
|
||||
|
||||
Core::DelayManager::OwnInfoForUser::OwnInfoForUser(Id p_id) :
|
||||
Job(p_id, Type::ownInfoForUser),
|
||||
Info(p_id, Type::ownInfoForUser)
|
||||
{}
|
||||
|
||||
Core::DelayManager::OwnInfoForUser::OwnInfoForUser(const OwnInfoForUser& other) :
|
||||
Job(other),
|
||||
Info(other)
|
||||
{}
|
34
core/delayManager/owninfoforuser.h
Normal file
34
core/delayManager/owninfoforuser.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Squawk messenger.
|
||||
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "contact.h"
|
||||
#include "info.h"
|
||||
|
||||
namespace Core {
|
||||
namespace DelayManager {
|
||||
|
||||
class OwnInfoForUser : public Info {
|
||||
public:
|
||||
OwnInfoForUser(Id id);
|
||||
OwnInfoForUser(const OwnInfoForUser& other);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
22
core/handlers/CMakeLists.txt
Normal file
22
core/handlers/CMakeLists.txt
Normal file
|
@ -0,0 +1,22 @@
|
|||
set(SOURCE_FILES
|
||||
messagehandler.cpp
|
||||
rosterhandler.cpp
|
||||
vcardhandler.cpp
|
||||
discoveryhandler.cpp
|
||||
trusthandler.cpp
|
||||
)
|
||||
|
||||
set(HEADER_FILES
|
||||
messagehandler.h
|
||||
rosterhandler.h
|
||||
vcardhandler.h
|
||||
discoveryhandler.h
|
||||
trusthandler.h
|
||||
)
|
||||
|
||||
if(WITH_OMEMO)
|
||||
list(APPEND SOURCE_FILES omemohandler.cpp)
|
||||
list(APPEND HEADER_FILES omemohandler.h)
|
||||
endif()
|
||||
|
||||
target_sources(squawk PRIVATE ${SOURCE_FILES})
|
155
core/handlers/discoveryhandler.cpp
Normal file
155
core/handlers/discoveryhandler.cpp
Normal file
|
@ -0,0 +1,155 @@
|
|||
// Squawk messenger.
|
||||
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "discoveryhandler.h"
|
||||
#include "core/account.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
Core::DiscoveryHandler::DiscoveryHandler(Core::Account* account):
|
||||
QObject(),
|
||||
acc(account),
|
||||
omemoToCarbonsConnected (false) {}
|
||||
|
||||
Core::DiscoveryHandler::~DiscoveryHandler() {}
|
||||
|
||||
void Core::DiscoveryHandler::initialize()
|
||||
{
|
||||
QObject::connect(acc->dm, &QXmppDiscoveryManager::itemsReceived, this, &DiscoveryHandler::onItemsReceived);
|
||||
QObject::connect(acc->dm, &QXmppDiscoveryManager::infoReceived, this, &DiscoveryHandler::onInfoReceived);
|
||||
|
||||
acc->dm->setClientType("pc");
|
||||
acc->dm->setClientCategory("client");
|
||||
acc->dm->setClientName(qApp->applicationDisplayName() + " " + qApp->applicationVersion());
|
||||
acc->dm->setClientCapabilitiesNode("https://git.macaw.me/blue/squawk");
|
||||
}
|
||||
|
||||
void Core::DiscoveryHandler::onItemsReceived(const QXmppDiscoveryIq& items)
|
||||
{
|
||||
QString server = acc->getServer();
|
||||
if (items.from() == server) {
|
||||
std::set<QString> needToRequest;
|
||||
qDebug() << "Server items list received for account " << acc->getName() << ":";
|
||||
for (QXmppDiscoveryIq::Item item : items.items()) {
|
||||
QString jid = item.jid();
|
||||
if (jid != server) {
|
||||
qDebug() << " Node" << jid;
|
||||
needToRequest.insert(jid);
|
||||
} else {
|
||||
qDebug() << " " << item.node().toStdString().c_str();
|
||||
}
|
||||
}
|
||||
|
||||
for (const QString& jid : needToRequest) {
|
||||
acc->dm->requestInfo(jid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::DiscoveryHandler::onInfoReceived(const QXmppDiscoveryIq& info)
|
||||
{
|
||||
QString from = info.from();
|
||||
QString server = acc->getServer();
|
||||
QString accName = acc->getName();
|
||||
QString bareJid = acc->getBareJid();
|
||||
if (from == server) {
|
||||
bool enableCC = false;
|
||||
qDebug() << "Server info received for account" << accName;
|
||||
QStringList features = info.features();
|
||||
qDebug() << "List of supported features of the server " << server << ":";
|
||||
for (const QString& feature : features) {
|
||||
qDebug() << " " << feature.toStdString().c_str();
|
||||
if (feature == "urn:xmpp:carbons:2") {
|
||||
enableCC = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (enableCC) {
|
||||
qDebug() << "Enabling carbon copies for account" << accName;
|
||||
#if (QXMPP_VERSION) < QT_VERSION_CHECK(1, 5, 0)
|
||||
acc->cm->setCarbonsEnabled(true);
|
||||
#endif
|
||||
#ifdef WITH_OMEMO
|
||||
if (!omemoToCarbonsConnected && acc->oh->hasOwnDevice()) {
|
||||
// connect(this, &QXmppCarbonManager::messageSent, acc->om, &QXmppOmemoManager::handleMessage);
|
||||
// connect(this, &QXmppCarbonManager::messageReceived, acc->om, &QXmppOmemoManager::handleMessage);
|
||||
omemoToCarbonsConnected = true;
|
||||
}
|
||||
} else {
|
||||
if (omemoToCarbonsConnected) {
|
||||
// disconnect(this, &QXmppCarbonManager::messageSent, acc->om, &QXmppOmemoManager::handleMessage);
|
||||
// disconnect(this, &QXmppCarbonManager::messageReceived, acc->om, &QXmppOmemoManager::handleMessage);
|
||||
omemoToCarbonsConnected = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
qDebug() << "Requesting account" << accName << "capabilities";
|
||||
acc->dm->requestInfo(bareJid);
|
||||
} else if (from == bareJid) {
|
||||
qDebug() << "Received capabilities for account" << accName << ":";
|
||||
QList<QXmppDiscoveryIq::Identity> identities = info.identities();
|
||||
bool pepSupported = false;
|
||||
for (const QXmppDiscoveryIq::Identity& identity : identities) {
|
||||
QString type = identity.type();
|
||||
QString category = identity.category();
|
||||
qDebug() << " " << category << type;
|
||||
if (type == "pep" && category == "pubsub") {
|
||||
pepSupported = true;
|
||||
}
|
||||
}
|
||||
acc->setPepSupport(pepSupported ? Shared::Support::supported : Shared::Support::unsupported);
|
||||
} else {
|
||||
QString node = info.queryNode();
|
||||
if (!node.isEmpty()) {
|
||||
qDebug() << "Received features and identities for account" << accName << "about" << from;
|
||||
QStringList feats = info.features();
|
||||
std::set<Shared::Identity> identities;
|
||||
std::set<QString> features(feats.begin(), feats.end());
|
||||
QList<QXmppDiscoveryIq::Identity> idents = info.identities();
|
||||
for (const QXmppDiscoveryIq::Identity& ident : idents) {
|
||||
Shared::Identity identity;
|
||||
identity.category = ident.category();
|
||||
identity.language = ident.language();
|
||||
identity.name = ident.name();
|
||||
identity.type = ident.type();
|
||||
identities.insert(identity);
|
||||
|
||||
qDebug() << " " << identity.name << identity.category << identity.type;
|
||||
}
|
||||
for (const QString& feat : features) {
|
||||
qDebug() << " " << feat;
|
||||
}
|
||||
emit acc->infoDiscovered(from, node, identities, features);
|
||||
} else {
|
||||
Contact* cont = acc->rh->getContact(from);
|
||||
if (cont != nullptr) {
|
||||
qDebug() << "Received info for account" << accName << "about contact" << from;
|
||||
QList<QXmppDiscoveryIq::Identity> identities = info.identities();
|
||||
bool pepSupported = false;
|
||||
for (const QXmppDiscoveryIq::Identity& identity : identities) {
|
||||
QString type = identity.type();
|
||||
QString category = identity.category();
|
||||
qDebug() << " " << category << type;
|
||||
if (type == "pep" && category == "pubsub") {
|
||||
pepSupported = true;
|
||||
}
|
||||
}
|
||||
cont->setPepSupport(pepSupported ? Shared::Support::supported : Shared::Support::unsupported);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
48
core/handlers/discoveryhandler.h
Normal file
48
core/handlers/discoveryhandler.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Squawk messenger.
|
||||
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||