Skip to content

Commit ca0e9a1

Browse files
committed
refactored some code
1 parent 5ce7834 commit ca0e9a1

2 files changed

Lines changed: 35 additions & 43 deletions

File tree

src/window.py

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import validators
2626
import io
2727

28-
2928
from urllib.parse import unquote
3029
import magic
3130
import requests
@@ -35,7 +34,6 @@
3534

3635
import threading
3736

38-
#(TARGET_ENTRY_URI, TARGET_ENTRY_TEXT, TARGET_ENTRY_MOZ_URL) = range(3)
3937
(TARGET_OCTECT_STREAM, TARGET_URI_LIST, TARGET_PLAIN) = range(3)
4038

4139

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

6159
print(info)
62-
63-
# TODO : handle this section better
6460

65-
# Application/Octect stream
61+
# TODO: better printing debug information
62+
63+
# Application/Octect stream : Image from Chromuim browsers
6664
if info == TARGET_OCTECT_STREAM:
65+
print("Got an Image from browser")
6766
image = Image.open(io.BytesIO(data.get_data()))
6867
format = image.format.lower()
6968
image.save(f"/tmp/pydrop/{self.count}.{format}")
7069
x = self.icon.get_pixel_size() + 50
7170
pixbuf = Pixbuf.new_from_file_at_scale(f"/tmp/pydrop/{self.count}.{format}", x, x, True)
72-
print("This in an image")
7371
self.link_stack.append(f"file:///tmp/pydrop/{self.count}.{format}")
7472
self.icon.set_from_pixbuf(pixbuf)
7573
self.count += 1
7674
a = "special"
7775

78-
# plain
79-
80-
#if info == TARGET_PLAIN:
81-
# print("Got some good old plain text")
82-
# print(data.get_text())
83-
8476
if info == TARGET_URI_LIST:
8577
print(data.get_uris())
8678
for uri in data.get_uris():
@@ -110,19 +102,13 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
110102

111103
elif self.link_is_image(link):
112104
self.download_image(link)
113-
# trying out multithreading so ui does not get blocked
114-
# while loading image
115-
116-
#download_thread = threading.Thread(target=self.download_image, args=(link,))
117-
#download_thread.start()
118-
#download_thread.join()
119105
a = "special"
120106
else:
121107
# TODO: handle link better, preferably make a file that contains the link?
122108
# investigate on which filetype to use
123109
self.link_stack.append(link)
124110
a = "text/html"
125-
111+
126112
else:
127113
print(" Got text ")
128114
file_name = f'{text.split()[0]}.txt'
@@ -138,13 +124,19 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
138124
print(a)
139125

140126
if "special" not in a:
141-
if "image" in a:
142-
x = self.icon.get_pixel_size() + 50
143-
144-
pixbuf = Pixbuf.new_from_file_at_scale(uri[6:].replace('%20', ' '), x, x, True)
145-
print("This in an image")
127+
if a in self.image_formats:
128+
129+
# TODO : add preview support for more mime types
130+
# - gifs
131+
# - videos
132+
# - pdfs
133+
134+
pixbuf = Pixbuf.new_from_file_at_scale(
135+
uri[6:].replace('%20', ' '),
136+
self.image_size,
137+
self.image_size,
138+
True)
146139
self.icon.set_from_pixbuf(pixbuf)
147-
148140
else:
149141
gicon = Gio.content_type_get_icon(a)
150142
self.icon.set_from_gicon(gicon, 512)
@@ -153,13 +145,13 @@ def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
153145

154146
if self.initial == 1:
155147
source_targets = [
156-
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
157-
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
158-
]
148+
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
149+
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
150+
]
159151

160152
self.eventbox.drag_source_set(
161-
Gdk.ModifierType.BUTTON1_MASK, source_targets, Gdk.DragAction.COPY
162-
)
153+
Gdk.ModifierType.BUTTON1_MASK, source_targets, Gdk.DragAction.COPY
154+
)
163155
self.eventbox.connect("drag-begin", self.hello)
164156
self.eventbox.connect("drag-data-get", self.on_drag_data_get)
165157
self.eventbox.connect("drag-end", self.end)
@@ -184,12 +176,12 @@ def link_is_image(self, link):
184176
if r.headers["content-type"] in self.image_formats:
185177
return True
186178
return False
187-
179+
188180

189181
def download_image(self, link):
190182

191183
# TODO: make the download another thread
192-
184+
193185
print(link)
194186
print("Starting download...")
195187
r = requests.get(link)
@@ -217,7 +209,7 @@ def hello(self, widget, data):
217209

218210
if self.initial != 1:
219211
pass
220-
#self.icon.clear()
212+
#self.icon.clear()
221213
print("Hello world")
222214

223215
def hello_source(self, widget, drag_context, x, y, data):
@@ -248,8 +240,9 @@ def drop_source(self, widget, drag_context, x, y, time, data):
248240
def __init__(self, **kwargs):
249241
super().__init__(**kwargs)
250242
self.google_re = re.compile(
251-
"[http|https]:\/\/www.google.com\/imgres\?imgurl=(.*)\&imgrefurl"
252-
)
243+
"[http|https]:\/\/www.google.com\/imgres\?imgurl=(.*)\&imgrefurl"
244+
)
245+
self.image_size = self.icon.get_pixel_size() + 50
253246
#print(self.iconview.get_model())
254247
self.button.hide()
255248
self.image_formats = ("image/png", "image/jpeg", "image/jpg")
@@ -266,17 +259,16 @@ def __init__(self, **kwargs):
266259
# drop destination stuff
267260

268261
enforce_target = [
269-
Gtk.TargetEntry.new("application/octet-stream", Gtk.TargetFlags(4), TARGET_OCTECT_STREAM),
270-
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
271-
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
272-
]
262+
Gtk.TargetEntry.new("application/octet-stream", Gtk.TargetFlags(4), TARGET_OCTECT_STREAM),
263+
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags(4), TARGET_URI_LIST),
264+
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags(4), TARGET_PLAIN),
265+
]
273266

274267
self.droparea.drag_dest_set(
275-
Gtk.DestDefaults.ALL, enforce_target, Gdk.DragAction.COPY
276-
)
268+
Gtk.DestDefaults.ALL, enforce_target, Gdk.DragAction.COPY
269+
)
277270
self.droparea.connect("drag-data-received", self.on_drag_data_received)
278271
self.droparea.connect("drag-drop", self.hello_source)
279272

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

src/window.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
</object>
6464
<packing>
6565
<property name="expand">True</property>
66-
<property name="fill">False</property>
66+
<property name="fill">True</property>
6767
<property name="position">1</property>
6868
</packing>
6969
</child>

0 commit comments

Comments
 (0)