-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_window.py
374 lines (317 loc) · 18.9 KB
/
search_window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#Class to spawn a window to search files and folders within the drive.
from datetime import datetime
from threading import Thread
import tkinter as tk
from tkinter import ttk
from unidecode import unidecode
import settings
import context_functions
class searchWindow():
def __init__(self, rootID, w, h, x, y): # get all existing data to search into and display
super().__init__()
settings.rootID = rootID
global search_window_is_open
search_window_is_open = True
self.searchIsRunning = False
self.searchWindow(w, h, x, y)
self.stop = False
def abort(self):
if self.searchIsRunning:
self.searchIsRunning = False
self.statusbar.configure(
text=" Search aborted. Displayed results may be incomplete. Ready to search again.",
background='yellow', foreground='black')
self.statusbar.update()
else:
global search_window_is_open
search_window_is_open = None
self.mainWindow.destroy()
def showContextMenu(self,
event): # show a contextual menu. If nothing is selected already, or if another single item is selected,
try:
if self.contextMenu:
self.contextMenu.destroy()
except:
pass
self.contextMenu = tk.Menu(self.mainWindow, tearoff=0)
self.contextMenu.add_command(label="Download", command=lambda: self.downloadSelected())
self.contextMenu.add_command(label="Copy/Move", command=lambda: self.copyMoveSelected())
self.contextMenu.add_command(label="Remove", command=lambda: self.removeRestore(flag=True))
self.contextMenu.add_command(label="Star/Unstar", command=lambda: self.starUnstar())
self.contextMenu.bind("<FocusOut>", lambda event: self.contextMenu.destroy())
try:
# automatically select the item below the mouse. Otherwise multiple items are selected, keep it that way.
if len(self.searchview.selection()) > 1:
if self.searchview.item(self.searchview.selection()[0])['values'][-1:][0] == (
'True' or True): # the value item for trashed is the last element of [values]
self.contextMenu.delete(2) # delete the 'Remove' option and replace with a 'Restore' one
self.contextMenu.add_command(label="Restore", command=lambda: self.removeRestore(flag=False))
else:
item = self.searchview.identify_row(event.y)
self.searchview.selection_set(item)
if self.searchview.item(self.searchview.selection()[0])['values'][-1:][0] == (
'True' or True): # the value item for trashed is the last element of [values]
self.contextMenu.delete(2) # delete the 'Remove' option and replace with a 'Restore' one
self.contextMenu.add_command(label="Restore", command=lambda: self.removeRestore(flag=False))
self.contextMenu.tk_popup(event.x_root, event.y_root)
except:
pass
finally:
self.contextMenu.grab_release()
def starUnstar(self):
data = []
items = self.searchview.selection()
for i in items:
i = self.searchview.item(i)
i['text'] = [j['name'] for j in settings.files + settings.folders if i['tags'][1] == j['id']][
0] # search in both lists at once.
data.append(i)
Thread(target=context_functions.contextFunctions().starUnstar, args=[data]).start()
def removeRestore(self, flag):
if not (
settings.downloadIsRunning or settings.moveInProgress or settings.copyInProgress or settings.retrievingFiles or settings.loadingData):
data = []
items = self.searchview.selection()
for i in items:
i = self.searchview.item(i)
i['text'] = [j['name'] for j in settings.files + settings.folders if i['tags'][1] == j['id']][
0] # search in both lists at once.
data.append(i)
Thread(target=context_functions.contextFunctions().removeRestore, args=(data, flag)).start()
else:
tk.messagebox.showinfo(None, "Please wait for the current operation to complete.")
def downloadSelected(self):
if not settings.downloadIsRunning:
data = [] # create list to store the item(s) to process.
try:
items = self.searchview.selection()
for i in items: # item names contain their full path. Need to keep the file/folder name only. It may contain a '/' so can't just split on this.
# get the id and search in settings.files/folders ?
i = self.searchview.item(i)
i['text'] = [j['name'] for j in settings.files + settings.folders if i['tags'][1] == j['id']][
0] # search in both lists at once.
data.append(i)
self.download = Thread(target=context_functions.contextFunctions().download, args=[data])
self.download.start()
# contextFunctions().download(data) #send
except Exception as e:
print({e})
def searchWindow(self, w, h, x, y):
# create the new window on top of the existing one.
self.mainWindow = tk.Toplevel()
self.mainWindow.geometry("%dx%d+%d+%d" % (w, h, x, y))
self.mainWindow.title("Search Engine")
global appIcon
appIcon = tk.PhotoImage(file="./resources/GoogleDrive.png")
self.mainWindow.iconphoto(False, appIcon)
self.mainWindow.rowconfigure(0, weight=0)
self.mainWindow.rowconfigure(1, weight=1)
self.mainWindow.rowconfigure(2, weight=1)
self.mainWindow.rowconfigure(3, weight=1)
self.mainWindow.columnconfigure(0, weight=1)
self.toolbar = ttk.Frame(self.mainWindow)
self.toolbar.columnconfigure(0, weight=1)
self.toolbar.columnconfigure(1, weight=1)
self.toolbar.grid(row=0, sticky="we", padx=2, pady=2)
self.searchEntry = ttk.Entry(self.toolbar)
self.searchEntry.config(background='lightgrey', foreground='black')
self.searchButton = ttk.Button(self.toolbar, text='Search', command=self.search)
self.searchEntry.grid(column=0, row=0, sticky='e', padx=2, pady=2)
self.searchButton.grid(column=1, row=0, sticky='w', padx=2, pady=2)
self.statusbar = ttk.Label(self.mainWindow, text="", anchor=tk.W)
# self.statusbar.pack(side=tk.BOTTOM, fill=tk.X)
self.statusbar.grid(row=4, sticky="we", padx=2, pady=2)
self.searchEntry.bind('<Return>', self.search) # launch search by pressing Enter
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview", highlightthickness=0, bd=0, font=('Calibri', 11), background="#e0e0e0",
foreground="black", rowheight=25) # Modify the font of the body
style.configure("Treeview.Heading", font=('Calibri', 11, 'bold')) # Modify the font of the headings
style.layout("Treeview", [('Treeview', {'sticky': 'nswe'})], ) # Remove the borders
style.map("Treeview", foreground=[('selected', '#000000')],
background=[('selected', '#ffcc00'), (['invalid', '!disabled'], '#e0e0e0'),
(['invalid', 'disabled'], '#e0e0e0')])
self.searchview = ttk.Treeview(self.mainWindow, style="Treeview", columns=("#1", "#2", "#3", "#4", "#5", "#6"),
selectmode='extended')
self.searchview.grid(column=0, row=1, rowspan=3, sticky="nsew", padx=2)
self.searchview.heading('#0', text="Path")
self.searchview.heading('#1', text="Type")
self.searchview.heading('#2', text="Size")
self.searchview.heading('#3', text="Modified (UTC)")
self.searchview.heading('#4', text="Owner")
self.searchview.heading('#5', text="Shared")
self.searchview.heading('#6', text="Trashed")
self.searchview.columnconfigure(0, weight=3)
self.searchview.rowconfigure(0, weight=1)
self.searchview.column('#0#', stretch=True)
self.searchview.column('#1#', width=int(w / 8), stretch=False)
self.searchview.column('#2#', width=int(w / 16), stretch=False)
self.searchview.column('#3#', width=int(w / 8), stretch=False)
self.searchview.column('#4#', width=int(w / 7), stretch=False)
self.searchview.column('#5#', width=int(w / 15), stretch=False)
self.searchview.column('#6#', width=int(w / 15), stretch=False)
# add a scrollbar
self.scrollbar = ttk.Scrollbar(self.mainWindow, orient="vertical", command=self.searchview.yview)
# self.scrollbar.place(relx=0.978, rely=0.175, relheight=0.713, relwidth=0.020)
self.searchview.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.grid(column=0, row=1, rowspan=3, sticky="sne", padx=2, pady=50)
self.statusbar.configure(text=" Ready to search. Press Esc to close the window.", background='green',
foreground='black')
# make a contextual menu on item selection
self.contextMenu = tk.Menu(self.mainWindow, tearoff=0)
self.contextMenu.add_command(label="Download", command=lambda: self.downloadSelected())
self.contextMenu.add_command(label="Copy/Move", command=lambda: self.copyMoveSelected())
self.contextMenu.add_command(label="Star/Unstar", command=lambda: self.starUnstar())
self.mainWindow.bind("<Button-3>", lambda event: self.showContextMenu(event))
self.mainWindow.protocol("WM_DELETE_WINDOW", self.abort)
self.mainWindow.bind('<Escape>', lambda e: self.abort())
self.mainWindow.mainloop()
self.statusbar.update()
def search(self,
*args): # a 2d argument is passed when starting the search by hitting Enter (the event itself), but not when clicking on the button.
# variable arguments on the function to prevent an exception.
self.searchview.delete(*self.searchview.get_children()) # clear the view upon a new search
if not self.searchEntry.get() == "":
self.statusbar.configure(text=" Searching...", background='blue', foreground='orange')
self.statusbar.update()
self.searchIsRunning = True
query = unidecode(self.searchEntry.get()).lower()
results = [i for i in settings.files + settings.folders if query in str(i).lower()]
self.statusbar.configure(
text=" Found {0} results. Displaying... Press escape to cancel.".format(len(results)),
background='blue', foreground='orange')
self.statusbar.update()
for i in range(len(results)):
if self.searchIsRunning: # press on Escape switches this to False and interrupts the search results display.
# try:
try:
attributes = context_functions.contextFunctions().getMimeAndSize(results[i]['mimeType'],
results[i]['size'])
except KeyError:
attributes = context_functions.contextFunctions().getMimeAndSize(results[i]['mimeType'], None)
# START BUILDING PATH FOR EACH RESULT#
try:
parent = results[i]['parents'][0]
except:
parent = ''
path = ''
y = results[i]
while not parent == 'None':
try:
z = [x for x in settings.folders if x['id'] == parent][0]
y = z
parent = y['id']
path = y['name'] + '/' + path
parent = settings.treeview.item(settings.directory[parent])['tags'][0]
# print("parent:", parent)
except Exception as e:
# print(e) #a shared file has a parent, which is present in the metadata but not shared, generates an exception.
break
if not y['ownedByMe']:
treeBasis = 'Shared With Me/'
else:
try:
test = y['parents']
treeBasis = 'Root/'
except:
treeBasis = 'Orphaned Data/'
pass
if y['trashed']:
treeBasis = 'Trash/'
try:
fileparent = results[i]['parents'][0]
except:
fileparent = None
pass
text = ' ' + treeBasis + path + results[i]['name']
modified = datetime.strftime(datetime.strptime(results[i]['modifiedTime'], '%Y-%m-%dT%H:%M:%S.%fZ'),
'%d/%m/%y %H:%M')
self.searchview.insert('', 'end', image=attributes[2], text=text,
values=(attributes[0], attributes[1],
modified, results[i]['owners'][0]['displayName'],
results[i]['shared'], results[i]['trashed']),
tags=(fileparent, results[i]['id']))
# first tag is "None" instead of being the parent ID. Won't be needed, but having something avoids additional processing when downloading.
self.mainWindow.update()
if self.searchIsRunning: # search and result display weren't interrupted by user input
self.statusbar.configure(text=" Ready to search.", background='green', foreground='black')
self.statusbar.update()
self.searchIsRunning = False
# if searchIsRunning is already false, do nothing. Status bar was updated by the abort function.
def copyMoveSelected(self):
if not (
settings.downloadIsRunning or settings.moveInProgress or settings.copyInProgress or settings.retrievingFiles or settings.loadingData):
self.data = []
items = self.searchview.selection()
for i in items:
i = self.searchview.item(i)
i['text'] = [j['name'] for j in settings.files + settings.folders if i['tags'][1] == j['id']][
0] # search in both lists at once.
self.data.append(i)
# need to get the target folder. Open another window showing the folder tree and select there.
# Just test the first entry, if there are multiple.
w = self.mainWindow.winfo_width()
h = self.mainWindow.winfo_height()
x = self.mainWindow.winfo_x()
y = self.mainWindow.winfo_y() # pass main window position and dimensions to place window appropriately.
self.spawnCopyMoveWindow(2 * w / 6, h - h / 6, x + w / 6, y + h / 12)
else:
tk.messagebox.showinfo(None, "Please wait for the current operation to complete.")
def copySelected(self):
destination = self.selectview.item(self.selectview.selection())
self.selectWindow.destroy()
Thread(target=context_functions.contextFunctions().copy, args=(self.data, destination)).start()
def moveSelected(self):
destination = self.selectview.item(self.selectview.selection())
self.selectWindow.destroy()
Thread(target=context_functions.contextFunctions().move, args=(self.data, destination)).start()
def spawnCopyMoveWindow(self, w, h, x, y):
self.selectWindow = tk.Toplevel()
self.selectWindow.geometry("%dx%d+%d+%d" % (w, h, x, y))
self.selectWindow.title("Select destination folder")
self.selectWindow.rowconfigure(0, weight=0)
self.selectWindow.rowconfigure(1, weight=1)
self.selectWindow.columnconfigure(0, weight=1)
self.selectview = ttk.Treeview(self.selectWindow, style="Treeview", selectmode='browse')
self.selectview.columnconfigure(0, weight=1)
self.selectview.rowconfigure(0, weight=1)
self.selectview.heading('#0', text="Folder Structure") # , anchor=tk.W) # shame
self.selectview.column('#0#', stretch=True, minwidth=400)
self.selectview.grid(column=0, row=0, rowspan=2, sticky="nsew", padx=2)
self.selectWindow.bind('<Escape>', lambda e: self.selectWindow.destroy())
self.toolbar = ttk.Frame(self.selectWindow)
self.copyButton = ttk.Button(self.toolbar, text='Copy', command=self.copySelected)
self.moveButton = ttk.Button(self.toolbar, text='Move', command=self.moveSelected)
self.cancelButton = ttk.Button(self.toolbar, text='Cancel', command=self.selectWindow.destroy)
self.cancelButton.pack(side=tk.RIGHT, padx=2, pady=2)
self.moveButton.pack(side=tk.RIGHT, padx=2, pady=2)
self.copyButton.pack(side=tk.RIGHT, padx=2, pady=2)
self.toolbar.grid(column=0, row=2, sticky="swe", padx=2, pady=2)
for child in settings.treeview.get_children(): # duplicate folder tree in the new window
# remove Starred, Orphaned and Trash from the selectview. Can't copy or move there
if not (settings.treeview.item(child)['text'] == ' Starred' or settings.treeview.item(child)[
'text'] == ' Orphaned Data' or settings.treeview.item(child)['text'] == ' Trash'):
try:
item = self.selectview.insert('', 'end', image=settings.treeview.item(child)['image'],
text=settings.treeview.item(child)['text'],
values=settings.treeview.item(child)['values'],
tags=settings.treeview.item(child)['tags'])
self.getsubchildren(item, child)
except Exception as e:
print(e)
print(settings.treeview.item(child))
pass
self.selectWindow.update()
def getsubchildren(self, item, child):
self.selectWindow.update()
try:
for child in settings.treeview.get_children(child):
i = self.selectview.insert(item, 'end', image=settings.folderIcon,
text=settings.treeview.item(child)['text'],
values=settings.treeview.item(child)['values'],
tags=settings.treeview.item(child)['tags'])
if settings.treeview.get_children(child):
self.getsubchildren(i, child)
except Exception as e:
print({e})
pass