Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nheko
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
nheko
Commits
56ee290b
Commit
56ee290b
authored
6 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Add the ability to change the room avatar
fixes #418
parent
db9c37d3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/dialogs/RoomSettings.cpp
+133
-0
133 additions, 0 deletions
src/dialogs/RoomSettings.cpp
src/dialogs/RoomSettings.h
+47
-8
47 additions, 8 deletions
src/dialogs/RoomSettings.h
src/timeline/TimelineItem.cpp
+1
-2
1 addition, 2 deletions
src/timeline/TimelineItem.cpp
with
181 additions
and
10 deletions
src/dialogs/RoomSettings.cpp
+
133
−
0
View file @
56ee290b
#include
<QApplication>
#include
<QComboBox>
#include
<QFileDialog>
#include
<QImageReader>
#include
<QLabel>
#include
<QMessageBox>
#include
<QMimeDatabase>
#include
<QPainter>
#include
<QPixmap>
#include
<QShowEvent>
...
...
@@ -356,6 +359,103 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
else
avatar_
->
setImage
(
avatarImg_
);
if
(
canChangeAvatar
(
room_id_
.
toStdString
(),
utils
::
localUser
().
toStdString
()))
{
auto
filter
=
new
ClickableFilter
(
this
);
avatar_
->
installEventFilter
(
filter
);
avatar_
->
setCursor
(
Qt
::
PointingHandCursor
);
connect
(
filter
,
&
ClickableFilter
::
clicked
,
this
,
[
this
]()
{
const
auto
fileName
=
QFileDialog
::
getOpenFileName
(
this
,
tr
(
"Select an avatar"
),
""
,
tr
(
"All Files (*)"
));
if
(
fileName
.
isEmpty
())
return
;
QMimeDatabase
db
;
QMimeType
mime
=
db
.
mimeTypeForFile
(
fileName
,
QMimeDatabase
::
MatchContent
);
const
auto
format
=
mime
.
name
().
split
(
"/"
)[
0
];
QFile
file
{
fileName
,
this
};
if
(
format
!=
"image"
)
{
displayErrorMessage
(
tr
(
"The selected media is not an image"
));
return
;
}
if
(
!
file
.
open
(
QIODevice
::
ReadOnly
))
{
displayErrorMessage
(
tr
(
"Error while reading media: %1"
).
arg
(
file
.
errorString
()));
return
;
}
if
(
spinner_
)
{
startLoadingSpinner
();
resetErrorLabel
();
}
// Events emitted from the http callbacks (different threads) will
// be queued back into the UI thread through this proxy object.
auto
proxy
=
std
::
make_shared
<
ThreadProxy
>
();
connect
(
proxy
.
get
(),
&
ThreadProxy
::
error
,
this
,
&
RoomSettings
::
displayErrorMessage
);
connect
(
proxy
.
get
(),
&
ThreadProxy
::
avatarChanged
,
this
,
&
RoomSettings
::
setAvatar
);
const
auto
bin
=
file
.
peek
(
file
.
size
());
const
auto
payload
=
std
::
string
(
bin
.
data
(),
bin
.
size
());
const
auto
dimensions
=
QImageReader
(
&
file
).
size
();
// First we need to create a new mxc URI
// (i.e upload media to the Matrix content repository) for the new avatar.
http
::
client
()
->
upload
(
payload
,
mime
.
name
().
toStdString
(),
QFileInfo
(
fileName
).
fileName
().
toStdString
(),
[
proxy
=
std
::
move
(
proxy
),
dimensions
,
payload
,
mimetype
=
mime
.
name
().
toStdString
(),
size
=
payload
.
size
(),
room_id
=
room_id_
.
toStdString
(),
content
=
std
::
move
(
bin
)](
const
mtx
::
responses
::
ContentURI
&
res
,
mtx
::
http
::
RequestErr
err
)
{
if
(
err
)
{
emit
proxy
->
error
(
tr
(
"Failed to upload image: %s"
)
.
arg
(
QString
::
fromStdString
(
err
->
matrix_error
.
error
)));
return
;
}
using
namespace
mtx
::
events
;
state
::
Avatar
avatar_event
;
avatar_event
.
image_info
.
w
=
dimensions
.
width
();
avatar_event
.
image_info
.
h
=
dimensions
.
height
();
avatar_event
.
image_info
.
mimetype
=
mimetype
;
avatar_event
.
image_info
.
size
=
size
;
avatar_event
.
url
=
res
.
content_uri
;
http
::
client
()
->
send_state_event
<
state
::
Avatar
,
EventType
::
RoomAvatar
>
(
room_id
,
avatar_event
,
[
content
=
std
::
move
(
content
),
proxy
=
std
::
move
(
proxy
)](
const
mtx
::
responses
::
EventId
&
,
mtx
::
http
::
RequestErr
err
)
{
if
(
err
)
{
emit
proxy
->
error
(
tr
(
"Failed to upload image: %s"
)
.
arg
(
QString
::
fromStdString
(
err
->
matrix_error
.
error
)));
return
;
}
emit
proxy
->
avatarChanged
(
QImage
::
fromData
(
content
));
});
});
});
}
auto
roomNameLabel
=
new
QLabel
(
QString
::
fromStdString
(
info_
.
name
),
this
);
roomNameLabel
->
setFont
(
doubleFont
);
...
...
@@ -537,6 +637,19 @@ RoomSettings::canChangeNameAndTopic(const std::string &room_id, const std::strin
return
false
;
}
bool
RoomSettings
::
canChangeAvatar
(
const
std
::
string
&
room_id
,
const
std
::
string
&
user_id
)
const
{
try
{
return
cache
::
client
()
->
hasEnoughPowerLevel
(
{
EventType
::
RoomAvatar
},
room_id
,
user_id
);
}
catch
(
const
lmdb
::
error
&
e
)
{
nhlog
::
db
()
->
warn
(
"lmdb error: {}"
,
e
.
what
());
}
return
false
;
}
void
RoomSettings
::
updateAccessRules
(
const
std
::
string
&
room_id
,
const
mtx
::
events
::
state
::
JoinRules
&
join_rule
,
...
...
@@ -596,6 +709,26 @@ RoomSettings::startLoadingSpinner()
}
}
void
RoomSettings
::
displayErrorMessage
(
const
QString
&
msg
)
{
stopLoadingSpinner
();
errorLabel_
->
show
();
errorLabel_
->
setText
(
msg
);
}
void
RoomSettings
::
setAvatar
(
const
QImage
&
img
)
{
stopLoadingSpinner
();
avatarImg_
=
img
;
if
(
avatar_
)
avatar_
->
setImage
(
img
);
}
void
RoomSettings
::
resetErrorLabel
()
{
...
...
This diff is collapsed.
Click to expand it.
src/dialogs/RoomSettings.h
+
47
−
8
View file @
56ee290b
#pragma once
#include
<QEvent>
#include
<QFrame>
#include
<QImage>
...
...
@@ -19,6 +20,41 @@ class TextField;
class
TextField
;
class
Toggle
;
class
ClickableFilter
:
public
QObject
{
Q_OBJECT
public:
explicit
ClickableFilter
(
QWidget
*
parent
)
:
QObject
(
parent
)
{}
signals
:
void
clicked
();
protected
:
bool
eventFilter
(
QObject
*
obj
,
QEvent
*
event
)
{
if
(
event
->
type
()
==
QEvent
::
MouseButtonRelease
)
{
emit
clicked
();
return
true
;
}
return
QObject
::
eventFilter
(
obj
,
event
);
}
};
/// Convenience class which connects events emmited from threads
/// outside of main with the UI code.
class
ThreadProxy
:
public
QObject
{
Q_OBJECT
signals:
void
error
(
const
QString
&
msg
);
void
avatarChanged
(
const
QImage
&
img
);
};
class
EditModal
:
public
QWidget
{
Q_OBJECT
...
...
@@ -71,36 +107,39 @@ private:
bool
canChangeJoinRules
(
const
std
::
string
&
room_id
,
const
std
::
string
&
user_id
)
const
;
//! Whether the user has enough power level to send m.room.name & m.room.topic events.
bool
canChangeNameAndTopic
(
const
std
::
string
&
room_id
,
const
std
::
string
&
user_id
)
const
;
//! Whether the user has enough power level to send m.room.avatar event.
bool
canChangeAvatar
(
const
std
::
string
&
room_id
,
const
std
::
string
&
user_id
)
const
;
void
updateAccessRules
(
const
std
::
string
&
room_id
,
const
mtx
::
events
::
state
::
JoinRules
&
,
const
mtx
::
events
::
state
::
GuestAccess
&
);
void
stopLoadingSpinner
();
void
startLoadingSpinner
();
void
resetErrorLabel
();
void
displayErrorMessage
(
const
QString
&
msg
);
void
setAvatar
(
const
QImage
&
img
)
{
avatarImg_
=
img
;
}
void
setAvatar
(
const
QImage
&
img
)
;
void
setupEditButton
();
//! Retrieve the current room information from cache.
void
retrieveRoomInfo
();
void
enableEncryption
();
Avatar
*
avatar_
;
Avatar
*
avatar_
=
nullptr
;
bool
usesEncryption_
=
false
;
QHBoxLayout
*
btnLayout_
;
FlatButton
*
editFieldsBtn_
;
FlatButton
*
editFieldsBtn_
=
nullptr
;
RoomInfo
info_
;
QString
room_id_
;
QImage
avatarImg_
;
QLabel
*
errorLabel_
;
LoadingIndicator
*
spinner_
;
QLabel
*
errorLabel_
=
nullptr
;
LoadingIndicator
*
spinner_
=
nullptr
;
QComboBox
*
accessCombo
;
Toggle
*
encryptionToggle_
;
Toggle
*
keyRequestsToggle_
;
QComboBox
*
accessCombo
=
nullptr
;
Toggle
*
encryptionToggle_
=
nullptr
;
Toggle
*
keyRequestsToggle_
=
nullptr
;
};
}
// dialogs
This diff is collapsed.
Click to expand it.
src/timeline/TimelineItem.cpp
+
1
−
2
View file @
56ee290b
...
...
@@ -639,18 +639,17 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam
auto
filter
=
new
UserProfileFilter
(
user_id
,
userName_
);
userName_
->
installEventFilter
(
filter
);
userName_
->
setCursor
(
Qt
::
PointingHandCursor
);
connect
(
filter
,
&
UserProfileFilter
::
hoverOn
,
this
,
[
this
]()
{
QFont
f
=
userName_
->
font
();
f
.
setUnderline
(
true
);
userName_
->
setCursor
(
Qt
::
PointingHandCursor
);
userName_
->
setFont
(
f
);
});
connect
(
filter
,
&
UserProfileFilter
::
hoverOff
,
this
,
[
this
]()
{
QFont
f
=
userName_
->
font
();
f
.
setUnderline
(
false
);
userName_
->
setCursor
(
Qt
::
ArrowCursor
);
userName_
->
setFont
(
f
);
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment