RetroShare/retroshare-qml-app/src/GxsIdentityDelegate.qml

231 lines
4.8 KiB
QML
Raw Normal View History

/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.7
import QtQuick.Controls 2.0
import "." //Needed for ChatCache singleton
Item
{
id: delegateRoot
height: 50
width: parent.width
property var chatId: undefined
property var lastMessageData: ""
2017-06-06 10:06:41 -04:00
property var locale: Qt.locale()
Rectangle {
anchors.fill: parent
color: contactItem.containsMouse ? "lightgrey" : "transparent"
width: parent.width
height: parent.height
MouseArea
{
id: contactItem
anchors.fill: parent
onClicked:
{
console.log("GxsIntentityDelegate onclicked:", model.name,
model.gxs_id)
contactsView.searching = false
if(model.own) contactsView.own_gxs_id = model.gxs_id
else
{
var jsonData = { "own_gxs_hex": contactsView.own_gxs_id,
"remote_gxs_hex": model.gxs_id }
rsApi.request("/chat/initiate_distant_chat",
JSON.stringify(jsonData),
contactsView.startChatCallback)
}
}
onPressAndHold: showDetails()
hoverEnabled: true
}
Rectangle
{
anchors.fill: parent
color: "transparent"
anchors.margins: 5
ColorHash
{
id: colorHash
hash: model.gxs_id
height: parent.height - 4
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 2
MouseArea
{
anchors.fill: parent
onClicked: delegateRoot.showDetails()
}
}
Column
{
height: parent.height
width: parent.width
anchors.left: colorHash.right
anchors.leftMargin: 5
Text
{
id: nickText
color: model.own ? "blue" : "black"
text: model.name
font.bold: true
}
Row
{
id: lastMessageText
width: parent.width
Text
{
id: lastMessageSender
font.italic: true
color: "dodgerblue"
text: (!lastMessageData.incoming && lastMessageData.msg)? "You: " : ""
}
Text
{
id: lastMessageMsg
text: (lastMessageData.msg) ? lastMessageData.msg : ""
font.italic: true
}
}
}
Row
{
anchors.right: parent.right
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
height: parent.height - 10
spacing: 4
Rectangle
{
visible: model.unread_count > 0
color: "cornflowerblue"
antialiasing: true
border.color: "blue"
border.width: 1
height: parent.height - 4
radius: height/2
width: height
anchors.verticalCenter: parent.verticalCenter
Text
{
color: "white"
font.bold: true
text: model.unread_count
anchors.centerIn: parent
}
}
Image
{
source: model.is_contact ?
"qrc:/icons/rating.png" :
"qrc:/icons/rating-unrated.png"
height: parent.height - 4
fillMode: Image.PreserveAspectFit
anchors.verticalCenter: parent.verticalCenter
}
}
}
}
Component.onCompleted: {
if (!chatId){
chatId = getChatIdFromGXS()
}
if (chatId) {
lastMessageData = getChatLastMessage(chatId)
}
}
Connections {
target: ChatCache.lastMessageCache
onLastMessageChanged: {
if (!chatId) {
chatId = getChatIdFromGXS()
}
if (chatId && chatId === chatI){
console.log("New last message received!")
lastMessageData = newLastMessage
}
}
}
function getChatLastMessage (chatId){
return ChatCache.lastMessageCache.getChatLastMessage(chatId)
}
function getChatIdFromGXS (){
var id= ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id)
return ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id)
}
function setTime(){
if (!lastMessageData.recv_time){
return ""
}
var timeFormat = "dd.MM.yyyy";
var recvDate = new Date(lastMessageData.recv_time*1000)
// Check if is today
if ( new Date (lastMessageData.recv_time*1000).setHours(0,0,0,0) == new Date ().setHours(0,0,0,0)) {
timeFormat = "hh:mm"
}
var timeString = Qt.formatDateTime(recvDate, timeFormat)
return timeString
}
function showDetails()
{
console.log("showDetails()", index)
contactsView.searching = false
stackView.push(
"qrc:/ContactDetails.qml",
{md: contactsListView.model.get(index)})
}
}