diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-06 00:39:55 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-06 00:39:55 +0100 |
commit | ba4fbab831b09bd66fca8136333db384b44ff6c1 (patch) | |
tree | db385c694fba440063562354741a79cbc9ea5ef7 /src | |
parent | d68dbc893c4b76627b28b189b870aedddcad55bb (diff) |
chat: Add support for file upload
Diffstat (limited to 'src')
-rw-r--r-- | src/chat.py | 18 | ||||
-rw-r--r-- | src/config.py | 3 | ||||
-rw-r--r-- | src/gtk/chat.ui | 18 | ||||
-rw-r--r-- | src/gtk/preferences.ui | 11 | ||||
-rw-r--r-- | src/meson.build | 3 | ||||
-rw-r--r-- | src/message.py | 2 | ||||
-rw-r--r-- | src/preferences.py | 6 | ||||
-rw-r--r-- | src/relay/netfile.py | 25 |
8 files changed, 83 insertions, 3 deletions
diff --git a/src/chat.py b/src/chat.py index 5e52347..bb498c3 100644 --- a/src/chat.py +++ b/src/chat.py @@ -24,6 +24,7 @@ from gi.repository import GObject from .message import WeegtkMessage from .color import Color from weegtk import config +from weegtk import netfile import json @@ -153,3 +154,20 @@ class WeegtkChat(Adw.Bin): entry_buffer.set_text("", 0) self.emit("buffer_input", self.data['full_name'], text) + def open_file_dialog(self, dialog, result, caller): + try: + file = dialog.open_finish(result) + except GLib.GError: + # gtk-dialog-error-quark: Dismissed by user + pass + else: + conf = config.read() + file_url = netfile.upload(file.get_path(), conf["upload"]["url"]) + entry_buffer = self.text_entry.get_buffer() + entry_buffer.insert_text(self.text_entry.get_position(), file_url, -1) + + @Gtk.Template.Callback() + def attach_file(self, *args): + dialog = Gtk.FileDialog() + dialog.set_title("Select file to upload") + dialog.open(self.get_root(), None, self.open_file_dialog, self) diff --git a/src/config.py b/src/config.py index ee9d611..ac52063 100644 --- a/src/config.py +++ b/src/config.py @@ -32,7 +32,7 @@ CONFIG_FILENAME = f"{CONFIG_DIR}/weegtk.conf" CONFIG_DEFAULT_RELAY_LINES = 50 -CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color', 'ssh') +CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color', 'ssh', 'upload') CONFIG_DEFAULT_OPTIONS = (('relay.hostname', ''), ('relay.port', ''), ('relay.ssl', 'off'), @@ -43,6 +43,7 @@ CONFIG_DEFAULT_OPTIONS = (('relay.hostname', ''), ('ssh.port', ''), ('ssh.username', ''), ('ssh.key', ''), + ('upload.url', ''), ('look.debug', 'off'), ('look.statusbar', 'off')) diff --git a/src/gtk/chat.ui b/src/gtk/chat.ui index 233c362..6959d45 100644 --- a/src/gtk/chat.ui +++ b/src/gtk/chat.ui @@ -52,6 +52,15 @@ <property name="margin-top">5</property> <property name="margin-bottom">5</property> <child> + <object class="GtkButton"> + <property name="icon-name">mail-attachment</property> + <style> + <class name="image-button"/> + </style> + <signal name="clicked" handler="attach_file"/> + </object> + </child> + <child> <object class="GtkEntry" id="text_entry"> <property name="halign">fill</property> <property name="hexpand">True</property> @@ -62,6 +71,15 @@ <signal name="activate" handler="entry_activate"/> </object> </child> + <child> + <object class="GtkButton"> + <property name="icon-name">mail-forward</property> + <style> + <class name="image-button"/> + </style> + <signal name="clicked" handler="entry_activate"/> + </object> + </child> </object> </property> </object> diff --git a/src/gtk/preferences.ui b/src/gtk/preferences.ui index 5823d2f..9e4d9fc 100644 --- a/src/gtk/preferences.ui +++ b/src/gtk/preferences.ui @@ -77,6 +77,17 @@ </child> </object> </child> + <child> + <object class="AdwPreferencesGroup"> + <property name="title" translatable="yes">Upload</property> + <child> + <object class="AdwEntryRow" id="upload_url"> + <property name="title" translatable="yes">Upload URL</property> + <property name="use_underline">True</property> + </object> + </child> + </object> + </child> </object> </child> </template> diff --git a/src/meson.build b/src/meson.build index 3225adc..649a19e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -36,7 +36,8 @@ weegtk_sources = [ 'preferences.py', 'relay/network.py', 'relay/protocol.py', - 'relay/color.py' + 'relay/color.py', + 'relay/netfile.py' ] install_data(weegtk_sources, install_dir: moduledir) diff --git a/src/message.py b/src/message.py index 4e37609..27cf817 100644 --- a/src/message.py +++ b/src/message.py @@ -1,4 +1,4 @@ -# window.py +# message.py # # Copyright 2024 Lucas Fryzek # diff --git a/src/preferences.py b/src/preferences.py index d864012..1a87e29 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -39,6 +39,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): ssh_key_path = "" + upload_url = Gtk.Template.Child() + def __init__(self, **kwargs): super().__init__(**kwargs) @@ -53,6 +55,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): self.ssh_username.set_text(self.config["ssh"]["username"]) self.set_ssh_key(self.config["ssh"]["key"]) + self.upload_url.set_text(self.config["upload"]["url"]) + self.install_action("ssh_key.open", None, self.ssh_key_open) def read_preferences(self): @@ -65,6 +69,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): self.config["ssh"]["username"] = self.ssh_username.get_text() self.config["ssh"]["key"] = self.ssh_key_path + self.config["upload"]["url"] = self.upload_url.get_text() + def set_ssh_key(self, key_path): self.ssh_key_path = key_path short_name = os.path.basename(key_path) diff --git a/src/relay/netfile.py b/src/relay/netfile.py new file mode 100644 index 0000000..321b810 --- /dev/null +++ b/src/relay/netfile.py @@ -0,0 +1,25 @@ +# netfile.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 +import requests + +def upload(filepath, upload_url): + files = {'file': open(filepath, 'rb')} + + r = requests.post(upload_url, files=files) + return r.text.strip() |