forked from yvie-k/nautilus-kdeconnect
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkdeconnect-ext.py
69 lines (57 loc) · 2.24 KB
/
kdeconnect-ext.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
import importlib
from subprocess import check_output, call
from gi.repository import GObject
from kdeconnect import send_files, get_available_devices
import zipfile
import urllib
import urllib.parse as urlparse
TARGET = "%%TARGET%%".title()
Nautilus = importlib.import_module("gi.repository.{}".format(TARGET))
class KDEConnectExtension(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
GObject.Object.__init__(self)
def zipdir(self, path, ziph):
# ziph is zipfile handle
import os
for root, dirs, files in os.walk(path):
for file in files:
print(dirs + [file])
ziph.write(os.path.join(root, file), os.path.join(*(dirs + [file])))
def menu_activate_cb(self, menu, files, device_id, device_name, folder_list):
import zipfile
import os
for folder_name in folder_list:
zip_name = urllib.url2pathname(urlparse.urlparse(folder_name + '.zip').path)
zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
self.zipdir(zip_name[:-4] + '/', zipf)
zipf.close()
zipfile = Nautilus.FileInfo.create_for_uri(folder_name + '.zip')
files.append(zipfile)
send_files(files, device_id, device_name)
def get_file_items(self, window, files):
try:
devices = get_available_devices()
except Exception as e:
raise Exception("Failed to get available devices")
files_new = []
folder_list = []
for i in range(len(files)):
if (files[i].is_directory() == False):
files_new.append(files[i])
else:
folder_list.append(files[i].get_uri())
files = files_new
print(files)
print(folder_list)
if (len(files_new) + len(folder_list) < 1):
return []
items = []
for device in devices:
item = Nautilus.MenuItem(
name="KDEConnectExtension::Send_File",
label="Send to %s" % device["name"]
)
item.connect('activate', self.menu_activate_cb,
files, device["id"], device["name"], folder_list)
items.append(item)
return items