New feature: Attach image straight from clipboard

This commit is contained in:
jeffser 2024-06-30 17:45:03 -06:00
parent 1da347ff2f
commit 4994f6d177

View File

@ -1304,21 +1304,37 @@ Generate a title following these rules:
self.selected_chat_row = self.chat_list_box.get_selected_row() self.selected_chat_row = self.chat_list_box.get_selected_row()
self.chat_actions(action, user_data) self.chat_actions(action, user_data)
def text_received(self, clipboard, result): def cb_text_received(self, clipboard, result):
text = clipboard.read_text_finish(result) try:
#Check if text is a Youtube URL text = clipboard.read_text_finish(result)
youtube_regex = re.compile( #Check if text is a Youtube URL
r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/' youtube_regex = re.compile(
r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})') r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/'
if youtube_regex.match(text): r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
try: if youtube_regex.match(text):
dialogs.youtube_caption(self, text) try:
except Exception as e: dialogs.youtube_caption(self, text)
self.show_toast("error", 10, self.main_overlay) except Exception as e:
self.show_toast("error", 10, self.main_overlay)
except Exception as e: 'huh'
def cb_image_received(self, clipboard, result):
try:
texture = clipboard.read_texture_finish(result)
if texture:
pixbuf = Gdk.pixbuf_get_from_texture(texture)
if not os.path.exists('/tmp/alpaca/images/'):
os.makedirs('/tmp/alpaca/images/')
image_name = self.generate_numbered_name('image.png', os.listdir('/tmp/alpaca/images'))
pixbuf.savev('/tmp/alpaca/images/{}'.format(image_name), "png", [], [])
self.attach_file('/tmp/alpaca/images/{}'.format(image_name), 'image')
except Exception as e: 'huh'
def on_clipboard_paste(self, textview): def on_clipboard_paste(self, textview):
clipboard = Gdk.Display.get_default().get_clipboard() clipboard = Gdk.Display.get_default().get_clipboard()
clipboard.read_text_async(None, self.text_received) clipboard.read_text_async(None, self.cb_text_received)
clipboard.read_texture_async(None, self.cb_image_received)
def on_model_dropdown_setup(self, factory, list_item): def on_model_dropdown_setup(self, factory, list_item):
label = Gtk.Label() label = Gtk.Label()