Skip to content

Commit

Permalink
refactored some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshan-R committed Jul 10, 2021
1 parent 5ce7834 commit ca0e9a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 43 deletions.
76 changes: 34 additions & 42 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import validators
import io


from urllib.parse import unquote
import magic
import requests
Expand All @@ -35,7 +34,6 @@

import threading

#(TARGET_ENTRY_URI, TARGET_ENTRY_TEXT, TARGET_ENTRY_MOZ_URL) = range(3)
(TARGET_OCTECT_STREAM, TARGET_URI_LIST, TARGET_PLAIN) = range(3)


Expand All @@ -59,28 +57,22 @@ class PydropWindow(Handy.Window):
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):

print(info)

# TODO : handle this section better

# Application/Octect stream
# TODO: better printing debug information

# Application/Octect stream : Image from Chromuim browsers
if info == TARGET_OCTECT_STREAM:
print("Got an Image from browser")
image = Image.open(io.BytesIO(data.get_data()))
format = image.format.lower()
image.save(f"/tmp/pydrop/{self.count}.{format}")
x = self.icon.get_pixel_size() + 50
pixbuf = Pixbuf.new_from_file_at_scale(f"/tmp/pydrop/{self.count}.{format}", x, x, True)
print("This in an image")
self.link_stack.append(f"file:///tmp/pydrop/{self.count}.{format}")
self.icon.set_from_pixbuf(pixbuf)
self.count += 1
a = "special"

# plain

#if info == TARGET_PLAIN:
# print("Got some good old plain text")
# print(data.get_text())

if info == TARGET_URI_LIST:
print(data.get_uris())
for uri in data.get_uris():
Expand Down Expand Up @@ -110,19 +102,13 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):

elif self.link_is_image(link):
self.download_image(link)
# trying out multithreading so ui does not get blocked
# while loading image

#download_thread = threading.Thread(target=self.download_image, args=(link,))
#download_thread.start()
#download_thread.join()
a = "special"
else:
# TODO: handle link better, preferably make a file that contains the link?
# investigate on which filetype to use
self.link_stack.append(link)
a = "text/html"

else:
print(" Got text ")
file_name = f'{text.split()[0]}.txt'
Expand All @@ -138,13 +124,19 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
print(a)

if "special" not in a:
if "image" in a:
x = self.icon.get_pixel_size() + 50

pixbuf = Pixbuf.new_from_file_at_scale(uri[6:].replace('%20', ' '), x, x, True)
print("This in an image")
if a in self.image_formats:

# TODO : add preview support for more mime types
# - gifs
# - videos
# - pdfs

pixbuf = Pixbuf.new_from_file_at_scale(
uri[6:].replace('%20', ' '),
self.image_size,
self.image_size,
True)
self.icon.set_from_pixbuf(pixbuf)

else:
gicon = Gio.content_type_get_icon(a)
self.icon.set_from_gicon(gicon, 512)
Expand All @@ -153,13 +145,13 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):

if self.initial == 1:
source_targets = [
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
]
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
]

self.eventbox.drag_source_set(
Gdk.ModifierType.BUTTON1_MASK, source_targets, Gdk.DragAction.COPY
)
Gdk.ModifierType.BUTTON1_MASK, source_targets, Gdk.DragAction.COPY
)
self.eventbox.connect("drag-begin", self.hello)
self.eventbox.connect("drag-data-get", self.on_drag_data_get)
self.eventbox.connect("drag-end", self.end)
Expand All @@ -184,12 +176,12 @@ def link_is_image(self, link):
if r.headers["content-type"] in self.image_formats:
return True
return False


def download_image(self, link):

# TODO: make the download another thread

print(link)
print("Starting download...")
r = requests.get(link)
Expand Down Expand Up @@ -217,7 +209,7 @@ def hello(self, widget, data):

if self.initial != 1:
pass
#self.icon.clear()
#self.icon.clear()
print("Hello world")

def hello_source(self, widget, drag_context, x, y, data):
Expand Down Expand Up @@ -248,8 +240,9 @@ def drop_source(self, widget, drag_context, x, y, time, data):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.google_re = re.compile(
"[http|https]:\/\/www.google.com\/imgres\?imgurl=(.*)\&imgrefurl"
)
"[http|https]:\/\/www.google.com\/imgres\?imgurl=(.*)\&imgrefurl"
)
self.image_size = self.icon.get_pixel_size() + 50
#print(self.iconview.get_model())
self.button.hide()
self.image_formats = ("image/png", "image/jpeg", "image/jpg")
Expand All @@ -266,17 +259,16 @@ def __init__(self, **kwargs):
# drop destination stuff

enforce_target = [
Gtk.TargetEntry.new("application/octet-stream", Gtk.TargetFlags(4), TARGET_OCTECT_STREAM),
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
]
Gtk.TargetEntry.new("application/octet-stream", Gtk.TargetFlags(4), TARGET_OCTECT_STREAM),
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
]

self.droparea.drag_dest_set(
Gtk.DestDefaults.ALL, enforce_target, Gdk.DragAction.COPY
)
Gtk.DestDefaults.ALL, enforce_target, Gdk.DragAction.COPY
)
self.droparea.connect("drag-data-received", self.on_drag_data_received)
self.droparea.connect("drag-drop", self.hello_source)

self.eventbox.connect("enter-notify-event", self.change_cursor)
self.eventbox.connect("leave-notify-event", self.revert_cursor)

2 changes: 1 addition & 1 deletion src/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</object>
<packing>
<property name="expand">True</property>
<property name="fill">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
Expand Down

0 comments on commit ca0e9a1

Please sign in to comment.