Skip to content

Fix syncthing plugin #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 84 additions & 67 deletions syncthing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

class Plugin(PluginInstance, GlobalQueryHandler):

config_key = 'syncthing_api_key'
config_key = 'api_key'

def __init__(self):
PluginInstance.__init__(self)
GlobalQueryHandler.__init__(self)

self.iconUrls = ["xdg:syncthing", f"file:{Path(__file__).parent}/syncthing.svg"]
self._api_key = self.readConfig(self.config_key, str)
if self._api_key is None:
self._api_key = ''
if self._api_key:
self.st = Syncthing(self._api_key)

Expand All @@ -39,6 +41,7 @@ def defaultTrigger(self):
@property
def api_key(self) -> str:
return self._api_key
# return '1234'

@api_key.setter
def api_key(self, value: str):
Expand All @@ -65,76 +68,90 @@ def configWidget(self):
def handleGlobalQuery(self, query):

results = []

if self.st:

config = self.st.system.config()

devices = dict()
for d in config['devices']:
if not d['name']:
d['name'] = d['deviceID']
d['_shared_folders'] = {}
devices[d['deviceID']] = d

folders = dict()
for f in config['folders']:
if not f['label']:
f['label'] = f['id']
for d in f['devices']:
devices[d['deviceID']]['_shared_folders'][f['id']] = f
folders[f['id']] = f

matcher = Matcher(query.string)

# create device items
for device_id, d in devices.items():
device_name = d['name']

if match := matcher.match(device_name):
device_folders = ", ".join([f['label'] for f in d['_shared_folders'].values()])

actions = []
if d['paused']:
actions.append(
Action("resume", "Resume synchronization",
lambda did=device_id: self.st.system.resume(did))
)
else:
actions.append(
Action("pause", "Pause synchronization",
lambda did=device_id: self.st.system.pause(did))
try:
if self.st:
config = self.st.system.config()

devices = dict()
for d in config['devices']:
if not d['name']:
d['name'] = d['deviceID']
d['_shared_folders'] = {}
devices[d['deviceID']] = d

folders = dict()
for f in config['folders']:
if not f['label']:
f['label'] = f['id']
for d in f['devices']:
devices[d['deviceID']]['_shared_folders'][f['id']] = f
folders[f['id']] = f

matcher = Matcher(query.string)

# create device items
for device_id, d in devices.items():
device_name = d['name']

if match := matcher.match(device_name):
device_folders = ", ".join([f['label'] for f in d['_shared_folders'].values()])

actions = []
if d['paused']:
actions.append(
Action("resume", "Resume synchronization",
lambda did=device_id: self.st.system.resume(did))
)
else:
actions.append(
Action("pause", "Pause synchronization",
lambda did=device_id: self.st.system.pause(did))
)

item = StandardItem(
id=device_id,
text=f"{device_name}",
subtext=f"{'Paused ' if d['paused'] else ''}Syncthing device. "
f"Shared: {device_folders if device_folders else 'Nothing'}.",
iconUrls=self.iconUrls,
actions=actions
)

results.append(RankItem(item, match))

# create folder items
for folder_id, f in folders.items():
folder_name = f['label']
if match := matcher.match(folder_name):
folders_devices = ", ".join([devices[d['deviceID']]['name'] for d in f['devices']])
item = StandardItem(
id=folder_id,
text=folder_name,
subtext=f"Syncthing folder {f['path']}. "
f"Shared with {folders_devices if folders_devices else 'nobody'}.",
iconUrls=self.iconUrls,
actions=[
Action("scan", "Scan the folder",
lambda fid=folder_id: self.st.database.scan(fid)),
Action("open", "Open this folder in file browser",
lambda p=f['path']: openFile(p))
]
)
results.append(RankItem(item, match))
except:
if self._api_key == '':
item = StandardItem(
id=device_id,
text=f"{device_name}",
subtext=f"{'Paused ' if d['paused'] else ''}Syncthing device. "
f"Shared: {device_folders if device_folders else 'Nothing'}.",
iconUrls=self.iconUrls,
actions=actions
id="no_key",
text="Please enter your Syncthing API key in settings",
subtext="You can find your api key on your Syncthing web dashboard.",
iconUrls=self.iconUrls
)

results.append(RankItem(item, match))

# create folder items
for folder_id, f in folders.items():
folder_name = f['label']
if match := matcher.match(folder_name):
folders_devices = ", ".join([devices[d['deviceID']]['name'] for d in f['devices']])
else:
item = StandardItem(
id=folder_id,
text=folder_name,
subtext=f"Syncthing folder {f['path']}. "
f"Shared with {folders_devices if folders_devices else 'nobody'}.",
iconUrls=self.iconUrls,
actions=[
Action("scan", "Scan the folder",
lambda fid=folder_id: self.st.database.scan(fid)),
Action("open", "Open this folder in file browser",
lambda p=f['path']: openFile(p))
]
id="invalid_key",
text='Invalid API Key',
subtext=f"API Key {self._api_key} is invalid. Please try entering your API key again in settings.",
iconUrls=self.iconUrls
)
results.append(RankItem(item, match))

results.append(RankItem(item, 0))
return results