openCom-Companion/sbapp/ui/conversations.py

299 lines
12 KiB
Python
Raw Normal View History

2022-04-07 21:03:53 +02:00
import RNS
import time
2022-04-07 21:03:53 +02:00
from kivy.metrics import dp,sp
2022-04-07 21:03:53 +02:00
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, BooleanProperty
from kivymd.uix.list import MDList, IconLeftWidget, IconRightWidget, OneLineAvatarIconListItem
from kivymd.uix.menu import MDDropdownMenu
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.core.clipboard import Clipboard
2022-10-18 19:17:55 +02:00
from kivy.effects.scroll import ScrollEffect
2022-04-07 21:03:53 +02:00
from kivymd.uix.button import MDRectangleFlatButton
2022-04-07 21:03:53 +02:00
from kivymd.uix.dialog import MDDialog
class NewConv(BoxLayout):
pass
class MsgSync(BoxLayout):
pass
class ConvSettings(BoxLayout):
disp_name = StringProperty()
context_dest = StringProperty()
2022-04-07 21:03:53 +02:00
trusted = BooleanProperty()
class Conversations():
def __init__(self, app):
self.app = app
self.context_dests = []
self.added_item_dests = []
self.list = None
self.conversation_dropdown = None
self.delete_dialog = None
self.clear_dialog = None
2022-04-07 21:03:53 +02:00
self.update()
def reload(self):
self.clear_list()
self.update()
def clear_list(self):
if self.list != None:
self.list.clear_widgets()
self.context_dests = []
self.added_item_dests = []
def update(self):
# if self.app.sideband.getstate("app.flags.unread_conversations"):
# self.clear_list()
2022-04-07 21:03:53 +02:00
self.context_dests = self.app.sideband.list_conversations()
self.update_widget()
self.app.sideband.setstate("app.flags.unread_conversations", False)
self.app.sideband.setstate("app.flags.new_conversations", False)
2022-10-12 16:33:05 +02:00
self.app.sideband.setstate("wants.viewupdate.conversations", False)
2022-04-07 21:03:53 +02:00
def trust_icon(self, context_dest, unread):
trust_icon = "account-question"
if self.app.sideband.is_trusted(context_dest):
if unread:
trust_icon = "email-seal"
else:
trust_icon = "account-check"
else:
if unread:
trust_icon = "email"
else:
trust_icon = "account-question"
return trust_icon
2022-04-07 21:03:53 +02:00
def update_widget(self):
us = time.time()
RNS.log("Updating conversation list widgets", RNS.LOG_DEBUG)
2022-04-07 21:03:53 +02:00
if self.list == None:
self.list = MDList()
remove_widgets = []
for w in self.list.children:
if not w.sb_uid in [e["dest"] for e in self.context_dests]:
RNS.log("Should remove "+RNS.prettyhexrep(w.sb_uid)+" from list")
remove_widgets.append(w)
self.added_item_dests.remove(w.sb_uid)
for w in remove_widgets:
RNS.log("Removing "+str(w))
self.list.remove_widget(w)
2022-04-07 21:03:53 +02:00
for conv in self.context_dests:
context_dest = conv["dest"]
unread = conv["unread"]
2023-09-20 21:41:26 +02:00
last_activity = conv["last_activity"]
2022-04-07 21:03:53 +02:00
if not context_dest in self.added_item_dests:
iconl = IconLeftWidget(icon=self.trust_icon(context_dest, unread), on_release=self.app.conversation_action)
2022-04-07 21:03:53 +02:00
item = OneLineAvatarIconListItem(text=self.app.sideband.peer_display_name(context_dest), on_release=self.app.conversation_action)
item.add_widget(iconl)
2023-09-20 21:41:26 +02:00
item.last_activity = last_activity
item.iconl = iconl
2022-04-07 21:03:53 +02:00
item.sb_uid = context_dest
item.sb_unread = unread
2022-10-02 12:45:06 +02:00
iconl.sb_uid = context_dest
2022-04-07 21:03:53 +02:00
def gen_edit(item):
2022-04-07 21:03:53 +02:00
def x():
t_s = time.time()
dest = self.conversation_dropdown.context_dest
2022-04-07 21:03:53 +02:00
try:
2023-09-21 01:59:06 +02:00
disp_name = self.app.sideband.raw_display_name(dest)
2022-04-07 21:03:53 +02:00
is_trusted = self.app.sideband.is_trusted(dest)
2022-10-13 22:12:39 +02:00
yes_button = MDRectangleFlatButton(text="Save",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_accept, text_color=self.app.color_accept)
no_button = MDRectangleFlatButton(text="Cancel",font_size=dp(18))
dialog_content = ConvSettings(disp_name=disp_name, context_dest=RNS.hexrep(dest, delimit=False), trusted=is_trusted)
2022-04-07 21:03:53 +02:00
dialog = MDDialog(
title="Edit Conversation",
text= "With "+RNS.prettyhexrep(dest),
2022-04-07 21:03:53 +02:00
type="custom",
content_cls=dialog_content,
buttons=[ yes_button, no_button ],
2022-10-03 00:56:39 +02:00
# elevation=0,
2022-04-07 21:03:53 +02:00
)
dialog.d_content = dialog_content
def dl_yes(s):
try:
name = dialog.d_content.ids["name_field"].text
trusted = dialog.d_content.ids["trusted_switch"].active
if trusted:
RNS.log("Setting Trusted "+str(trusted))
self.app.sideband.trusted_conversation(dest)
else:
RNS.log("Setting Untrusted "+str(trusted))
self.app.sideband.untrusted_conversation(dest)
RNS.log("Name="+name)
self.app.sideband.named_conversation(name, dest)
except Exception as e:
RNS.log("Error while saving conversation settings: "+str(e), RNS.LOG_ERROR)
dialog.dismiss()
def cb(dt):
self.update()
Clock.schedule_once(cb, 0.2)
2022-04-07 21:03:53 +02:00
def dl_no(s):
dialog.dismiss()
yes_button.bind(on_release=dl_yes)
no_button.bind(on_release=dl_no)
item.dmenu.dismiss()
dialog.open()
RNS.log("Generated edit dialog in "+str(RNS.prettytime(time.time()-t_s)))
2022-04-07 21:03:53 +02:00
except Exception as e:
RNS.log("Error while creating conversation settings: "+str(e), RNS.LOG_ERROR)
return x
def gen_clear(item):
2022-04-07 21:03:53 +02:00
def x():
if self.clear_dialog == None:
yes_button = MDRectangleFlatButton(text="Yes",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_reject, text_color=self.app.color_reject)
no_button = MDRectangleFlatButton(text="No",font_size=dp(18))
self.clear_dialog = MDDialog(
title="Clear all messages in conversation?",
buttons=[ yes_button, no_button ],
# elevation=0,
)
def dl_yes(s):
self.clear_dialog.dismiss()
self.app.sideband.clear_conversation(self.conversation_dropdown.context_dest)
def dl_no(s):
self.clear_dialog.dismiss()
yes_button.bind(on_release=dl_yes)
no_button.bind(on_release=dl_no)
2022-04-07 21:03:53 +02:00
item.dmenu.dismiss()
self.clear_dialog.open()
2022-04-07 21:03:53 +02:00
return x
def gen_del(item):
2022-04-07 21:03:53 +02:00
def x():
if self.delete_dialog == None:
yes_button = MDRectangleFlatButton(text="Yes",font_size=dp(18), theme_text_color="Custom", line_color=self.app.color_reject, text_color=self.app.color_reject)
no_button = MDRectangleFlatButton(text="No",font_size=dp(18))
self.delete_dialog = MDDialog(
title="Delete conversation?",
buttons=[ yes_button, no_button ],
# elevation=0,
)
def dl_yes(s):
self.delete_dialog.dismiss()
self.app.sideband.delete_conversation(self.conversation_dropdown.context_dest)
def cb(dt):
self.update()
Clock.schedule_once(cb, 0.2)
def dl_no(s):
self.delete_dialog.dismiss()
yes_button.bind(on_release=dl_yes)
no_button.bind(on_release=dl_no)
2022-04-07 21:03:53 +02:00
item.dmenu.dismiss()
self.delete_dialog.open()
2022-04-07 21:03:53 +02:00
return x
def gen_copy_addr(item):
def x():
Clipboard.copy(RNS.hexrep(self.conversation_dropdown.context_dest, delimit=False))
item.dmenu.dismiss()
return x
2023-07-10 02:49:58 +02:00
item.iconr = IconRightWidget(icon="dots-vertical");
if self.conversation_dropdown == None:
dmi_h = 40
dm_items = [
{
"viewclass": "OneLineListItem",
"text": "Edit",
"height": dp(dmi_h),
"on_release": gen_edit(item)
},
{
"text": "Copy Address",
"viewclass": "OneLineListItem",
"height": dp(dmi_h),
"on_release": gen_copy_addr(item)
},
{
"text": "Clear Messages",
"viewclass": "OneLineListItem",
"height": dp(dmi_h),
"on_release": gen_clear(item)
},
{
"text": "Delete Conversation",
"viewclass": "OneLineListItem",
"height": dp(dmi_h),
"on_release": gen_del(item)
}
]
self.conversation_dropdown = MDDropdownMenu(
2023-07-10 02:49:58 +02:00
caller=item.iconr,
items=dm_items,
position="auto",
2023-07-10 19:20:24 +02:00
width=dp(256),
elevation=0,
radius=dp(3),
)
2022-10-18 19:17:55 +02:00
self.conversation_dropdown.effect_cls = ScrollEffect
2022-04-07 21:03:53 +02:00
item.dmenu = self.conversation_dropdown
def callback_factory(ref, dest):
2022-04-07 21:03:53 +02:00
def x(sender):
self.conversation_dropdown.context_dest = dest
ref.dmenu.caller = ref.iconr
2022-04-07 21:03:53 +02:00
ref.dmenu.open()
return x
item.iconr.bind(on_release=callback_factory(item, context_dest))
2022-04-07 21:03:53 +02:00
item.add_widget(item.iconr)
self.added_item_dests.append(context_dest)
self.list.add_widget(item)
else:
for w in self.list.children:
if w.sb_uid == context_dest:
disp_name = self.app.sideband.peer_display_name(context_dest)
trust_icon = self.trust_icon(context_dest, unread)
2023-09-20 21:41:26 +02:00
w.last_activity = last_activity
if w.iconl.icon != trust_icon:
w.iconl.icon = trust_icon
w.sb_unread = unread
if w.text != disp_name:
w.text = disp_name
2023-09-20 21:41:26 +02:00
self.list.children.sort(key=lambda w: w.last_activity)
RNS.log("Updated conversation list widgets in "+RNS.prettytime(time.time()-us), RNS.LOG_DEBUG)
2022-04-07 21:03:53 +02:00
def get_widget(self):
return self.list