Now About Social Code
aboutsummaryrefslogtreecommitdiff
path: root/src/chat.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/chat.py')
-rw-r--r--src/chat.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/chat.py b/src/chat.py
new file mode 100644
index 0000000..f876854
--- /dev/null
+++ b/src/chat.py
@@ -0,0 +1,120 @@
+# window.py
+#
+# Copyright 2024 Lucas Fryzek
+#
+# 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 <https://www.gnu.org/licenses/>.
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+from gi.repository import Adw
+from gi.repository import Gtk
+from gi.repository import Gio
+from gi.repository import GObject
+
+from .message import WeegtkMessage
+from .color import Color
+from weegtk import config
+
+import json
+
+@Gtk.Template(resource_path='/com/fryzekconcepts/weegtk/gtk/chat.ui')
+class WeegtkChat(Adw.Bin):
+ __gtype_name__ = 'WeegtkChat'
+
+ __gsignals__ = {
+ "buffer_input" : (
+ GObject.SignalFlags.RUN_FIRST,
+ None,
+ (str, str)
+ )
+ }
+
+ window = Gtk.Template.Child()
+ messages = Gtk.Template.Child()
+ text_entry = Gtk.Template.Child()
+
+ def __init__(self, data=None, **kwargs):
+ super().__init__(**kwargs)
+
+ self.data = data or {}
+
+ self.model = Gtk.StringList()
+ self.select = Gtk.NoSelection(model=self.model)
+
+ self.factory = Gtk.SignalListItemFactory()
+ self.factory.connect("setup", self.setup_list_item)
+ self.factory.connect("bind", self.bind_list_item)
+
+ self.messages.set_model(self.select)
+ self.messages.set_factory(self.factory)
+
+ self.color = Color(config.color_options(), False)
+
+ def scroll_bottom(self, *args):
+ n_items = self.model.get_n_items()
+ self.messages.scroll_to(n_items - 1, Gtk.ListScrollFlags.FOCUS)
+
+ def setup_list_item(self, factory, list_item, *user_data):
+ message = WeegtkMessage()
+ list_item.set_child(message)
+ pass
+
+ def bind_list_item(self, factory, list_item, *user_data):
+ text = list_item.get_item().get_string()
+ data = json.loads(text)
+ message = list_item.get_child()
+ message.set_contents(data["username"], data["text"])
+ pass
+
+ def is_chat(self):
+ buf_type = self.data['local_variables']['type'] if 'type' in self.data['local_variables'] else None
+ return buf_type is not None and (buf_type == "private" or buf_type == "channel")
+
+ def pointer(self):
+ """Return pointer on buffer."""
+ return self.data.get('__path', [''])[0]
+
+ def parse_out_colors(self, text):
+ clean_text = self.color.convert(text)
+ return clean_text
+
+ def display(self, time, prefix, text, forcecolor=None):
+ # TODO using JSON here seems a bit ineffcient
+ # See if its possible to make a model that uses
+ # our own custom struct
+ user = self.parse_out_colors(prefix)
+ msg = self.parse_out_colors(text)
+ count = self.model.get_n_items()
+
+ if count != 0:
+ last = self.model.get_string(count - 1)
+ last_data = json.loads(last)
+ if last_data["username"] == user:
+ last_data["text"].append(msg)
+ self.model.splice(count - 1, 1, [json.dumps(last_data)])
+ return
+
+ data = {
+ "username": user,
+ "text": [msg]
+ }
+ self.model.append(json.dumps(data))
+ self.scroll_bottom()
+
+ @Gtk.Template.Callback()
+ def entry_activate(self, *args):
+ entry_buffer = self.text_entry.get_buffer()
+ text = entry_buffer.get_text()
+ entry_buffer.set_text("", 0)
+ self.emit("buffer_input", self.data['full_name'], text)
+