Skip to content

Commit

Permalink
fixed incorrect handling of urls
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed Jan 12, 2024
1 parent 7c0a7ae commit 80ae826
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/lib/DroppedItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, item, msg, *args: object) -> None:
class DroppedItem():
MAX_PREVIEW_SIZE_MB = 50

def __init__(self, item, drops_dir, dynamic_size=False, is_clipboard=False) -> None:
def __init__(self, item, drops_dir, dynamic_size=False, is_clipboard=False, ignore_urls=False) -> None:
self.DROPS_DIR = drops_dir

self.received_item = item
Expand Down Expand Up @@ -61,7 +61,9 @@ def __init__(self, item, drops_dir, dynamic_size=False, is_clipboard=False) -> N
self.content_is_text = True

self.preview_image = 'font-x-generic-symbolic'
if text_string.startswith('http://') or text_string.startswith('https://'):
if ignore_urls == False and is_clipboard == False and \
(text_string.startswith('http://') or text_string.startswith('https://')):

logging.debug(f'Found http url: {text_string}')
base_filename = 'collected_link_'
self.preview_image = 'chain-link-symbolic'
Expand All @@ -81,7 +83,6 @@ def __init__(self, item, drops_dir, dynamic_size=False, is_clipboard=False) -> N
self.size = len(text_string)

self.set_display_value(text_string)

else:
raise DroppedItemNotSupportedException(msg=f'item of type {type(item)} not supported')

Expand Down Expand Up @@ -156,6 +157,7 @@ def complete_load(self):

self.display_value = self.set_display_value(img_link)
self.size = self.get_size(True)
self.content_is_text = False

self.generate_preview_for_image()

Expand Down
73 changes: 55 additions & 18 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

class CollectorWindow(Adw.ApplicationWindow):


COLLECTOR_COLORS = ["blue", "yellow", "purple", "rose", "orange", "green"]
EMPTY_DROP_TEXT = _('Drop content here')
CAROUSEL_ICONS_PIX_SIZE=50
Expand Down Expand Up @@ -212,15 +213,46 @@ def on_drop_event(self, widget, value, x, y):
def on_drop_event_complete(self, carousel_items: list[CarouselItem]):
new_image = False
for carousel_item in carousel_items:
dropped_item = carousel_item.dropped_item
self.icon_carousel.remove(carousel_item.image)

for i, c in enumerate(self.dropped_items):
if c is carousel_item:
del self.dropped_items[i]
break

if self.settings.get_boolean('collect-text-to-csv') and \
dropped_item.content_is_text:

value = dropped_item.get_text_content()
if self.csvcollector:
self.csvcollector.append_text(value)
else:
self.csvcollector = CsvCollector(self.DROPS_PATH)
self.csvcollector.append_text(value)

dropped_item = DroppedItem(self.csvcollector.get_gfile(),
is_clipboard=True,
drops_dir=self.DROPS_PATH,
dynamic_size=True)

carousel_item = CarouselItem(
item=dropped_item,
image=self.get_new_image_from_dropped_item(dropped_item),
index=0
)

self.icon_carousel.prepend(carousel_item.image)
self.dropped_items.insert(0, carousel_item)
else:
new_image = self.get_new_image_from_dropped_item(dropped_item)
new_image.set_tooltip_text(dropped_item.display_value)

new_image = self.get_new_image_from_dropped_item(carousel_item.dropped_item)
new_image.set_tooltip_text(carousel_item.dropped_item.display_value)

carousel_item.image = new_image
carousel_item.image = new_image

self.icon_carousel.append(new_image)
self.dropped_items[carousel_item.index] = carousel_item
self.icon_carousel.append(new_image)
self.dropped_items.append(carousel_item)
self.dropped_items[carousel_item.index] = carousel_item

if new_image:
self.icon_carousel.scroll_to(new_image, True)
Expand All @@ -229,6 +261,7 @@ def on_drop_event_complete(self, carousel_items: list[CarouselItem]):

def on_drop_event_complete_async(self, carousel_items: list[CarouselItem]):
async_items: list[CarouselItem] = []
GLib.idle_add(lambda: self.update_tot_size_sum(True))

for carousel_item in carousel_items:
if carousel_item.dropped_item.async_load:
Expand Down Expand Up @@ -317,11 +350,11 @@ def on_key_pressed(self, widget, keyval, keycode, state):
if self.dropped_items:
self.carousel_popover.popup()
return True
elif keyval == Gdk.KEY_O:
elif keyval == Gdk.KEY_o:
if self.dropped_items and ctrl_key:
self.on_preview_btn_clicked(None)
return True
if keyval == Gdk.KEY_Delete:
elif keyval == Gdk.KEY_Delete:
if self.dropped_items and not self.is_dragging_away:
self.remove_all_items()
self.carousel_popover.popdown()
Expand Down Expand Up @@ -358,7 +391,7 @@ def drop_value(self, value):
dropped_items.append(d)
elif isinstance(value, str) and self.settings.get_boolean('collect-text-to-csv'):
dropped_item = DroppedItem(value, drops_dir=self.DROPS_PATH)

if dropped_item.async_load:
dropped_items.append(dropped_item)
else:
Expand Down Expand Up @@ -413,20 +446,20 @@ def drop_value(self, value):
carousel_item = CarouselItem(
item=dropped_item,
image=new_image,
index=len(self.dropped_items)
index=0 if dropped_item.is_clipboard else len(self.dropped_items)
)

carousel_items.append(carousel_item)
self.icon_carousel.append(new_image)

self.dropped_items.extend(carousel_items)
if dropped_item.is_clipboard:
self.icon_carousel.prepend(new_image)
else:
self.icon_carousel.append(new_image)

for i, c in enumerate(self.dropped_items):
for c in carousel_items:
if c.dropped_item.is_clipboard:
self.icon_carousel.reorder(c.image, 0)
self.dropped_items.pop(i)
self.dropped_items.insert(0, c)
break
else:
self.dropped_items.append(c)

if any([d.async_load for d in dropped_items]):
threading.Thread(
Expand Down Expand Up @@ -504,7 +537,11 @@ def on_copy_btn_clicked(self, btn=None):
self.clipboard.set_content(content_prov)
self.carousel_popover.popdown()

def update_tot_size_sum(self):
def update_tot_size_sum(self, loading_state=False):
if loading_state:
self.drops_label.set_label('...')
return

tot_size = sum([d.dropped_item.get_size() for d in self.dropped_items])

if tot_size > (1024 * 1024 * 1024):
Expand Down

0 comments on commit 80ae826

Please sign in to comment.