2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"maunium.net/go/gomuks/debug"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages/tstring"
|
2018-04-10 18:31:28 +02:00
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/gomuks/lib/ansimage"
|
|
|
|
"maunium.net/go/tcell"
|
2018-04-10 18:31:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
gob.Register(&UIImageMessage{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type UIImageMessage struct {
|
|
|
|
UITextMessage
|
2018-04-11 16:57:15 +02:00
|
|
|
Path string
|
2018-04-10 18:31:28 +02:00
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewImageMessage creates a new UIImageMessage object with the provided values and the default state.
|
2018-04-11 16:57:15 +02:00
|
|
|
func NewImageMessage(id, sender, msgtype string, path string, data []byte, timestamp time.Time) UIMessage {
|
2018-04-10 18:31:28 +02:00
|
|
|
return &UIImageMessage{
|
|
|
|
UITextMessage{
|
|
|
|
MsgSender: sender,
|
|
|
|
MsgTimestamp: timestamp,
|
|
|
|
MsgSenderColor: widget.GetHashColor(sender),
|
|
|
|
MsgType: msgtype,
|
|
|
|
MsgID: id,
|
|
|
|
prevBufferWidth: 0,
|
|
|
|
MsgState: ifc.MessageStateDefault,
|
|
|
|
MsgIsHighlight: false,
|
|
|
|
MsgIsService: false,
|
|
|
|
},
|
2018-04-11 16:57:15 +02:00
|
|
|
path,
|
2018-04-10 18:31:28 +02:00
|
|
|
data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFrom replaces the content of this message object with the content of the given object.
|
|
|
|
func (msg *UIImageMessage) CopyFrom(from ifc.MessageMeta) {
|
|
|
|
msg.MsgSender = from.Sender()
|
|
|
|
msg.MsgSenderColor = from.SenderColor()
|
|
|
|
|
|
|
|
fromMsg, ok := from.(UIMessage)
|
|
|
|
if ok {
|
|
|
|
msg.MsgSender = fromMsg.RealSender()
|
|
|
|
msg.MsgID = fromMsg.ID()
|
|
|
|
msg.MsgType = fromMsg.Type()
|
|
|
|
msg.MsgTimestamp = fromMsg.Timestamp()
|
|
|
|
msg.MsgState = fromMsg.State()
|
|
|
|
msg.MsgIsService = fromMsg.IsService()
|
|
|
|
msg.MsgIsHighlight = fromMsg.IsHighlight()
|
|
|
|
msg.buffer = nil
|
|
|
|
|
|
|
|
fromImgMsg, ok := from.(*UIImageMessage)
|
|
|
|
if ok {
|
|
|
|
msg.data = fromImgMsg.data
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.RecalculateBuffer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateBuffer generates the internal buffer for this message that consists
|
|
|
|
// of the text of this message split into lines at most as wide as the width
|
|
|
|
// parameter.
|
|
|
|
func (msg *UIImageMessage) CalculateBuffer(width int) {
|
|
|
|
if width < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
image, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, width, color.Black)
|
2018-04-10 18:31:28 +02:00
|
|
|
if err != nil {
|
2018-04-11 16:57:15 +02:00
|
|
|
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
|
2018-04-10 18:31:28 +02:00
|
|
|
debug.Print("Failed to display image:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
msg.buffer = image.Render()
|
2018-04-10 18:31:28 +02:00
|
|
|
msg.prevBufferWidth = width
|
|
|
|
}
|