2022-03-25 12:04:29 +00:00
|
|
|
import json
|
|
|
|
import os
|
2023-08-07 05:13:40 +01:00
|
|
|
import subprocess
|
2022-03-25 12:04:29 +00:00
|
|
|
import sqlite3
|
2022-04-09 16:42:40 +01:00
|
|
|
from pathlib import Path
|
2022-03-25 12:04:29 +00:00
|
|
|
from typing import NamedTuple
|
|
|
|
|
|
|
|
import dbus.service
|
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
from gi.repository import GLib
|
|
|
|
|
|
|
|
DBusGMainLoop(set_as_default=True)
|
|
|
|
|
|
|
|
objpath = "/vscode"
|
|
|
|
|
|
|
|
iface = "org.kde.krunner1"
|
|
|
|
|
|
|
|
|
2023-04-29 12:24:19 +01:00
|
|
|
VSCODE_DIRS = [
|
|
|
|
"Code",
|
|
|
|
"Code - OSS",
|
|
|
|
"VSCodium"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-03-25 12:04:29 +00:00
|
|
|
class Match(NamedTuple):
|
|
|
|
data: str
|
|
|
|
display_text: str
|
|
|
|
icon: str
|
|
|
|
type: int
|
|
|
|
relevance: float
|
|
|
|
properties: dict
|
|
|
|
|
|
|
|
|
|
|
|
# Read path_list from database
|
2022-03-28 09:36:38 +01:00
|
|
|
def get_path_list():
|
2023-05-07 17:38:10 +01:00
|
|
|
paths = set()
|
2023-04-29 12:24:19 +01:00
|
|
|
|
|
|
|
for vscode_dir in VSCODE_DIRS:
|
|
|
|
state_file = os.path.join(
|
|
|
|
os.environ["HOME"], ".config", vscode_dir, "User/globalStorage/state.vscdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(state_file):
|
|
|
|
continue
|
|
|
|
|
|
|
|
con = sqlite3.connect(state_file)
|
|
|
|
cur = con.cursor()
|
|
|
|
rows = cur.execute(
|
|
|
|
"SELECT value FROM ItemTable WHERE key = 'history.recentlyOpenedPathsList'"
|
|
|
|
)
|
|
|
|
data = json.loads(rows.fetchone()[0])
|
|
|
|
con.close()
|
|
|
|
paths.update(
|
|
|
|
{
|
|
|
|
"~" + path[len(os.environ["HOME"]) :] if os.environ["HOME"] in path else path
|
|
|
|
for path in [i["folderUri"][7:] for i in data["entries"] if "folderUri" in i]
|
|
|
|
}
|
2022-03-28 09:36:38 +01:00
|
|
|
)
|
2023-05-07 17:38:10 +01:00
|
|
|
return paths
|
2022-03-25 12:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Runner(dbus.service.Object):
|
|
|
|
def __init__(self):
|
|
|
|
dbus.service.Object.__init__(
|
|
|
|
self,
|
|
|
|
dbus.service.BusName(
|
|
|
|
"com.github.j1g5awi.vscode", dbus.SessionBus() # type:ignore
|
|
|
|
),
|
|
|
|
objpath,
|
|
|
|
)
|
|
|
|
|
|
|
|
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
|
|
|
|
def Match(self, query: str):
|
|
|
|
# data, display text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category and urls)
|
|
|
|
return [
|
|
|
|
Match(
|
|
|
|
path,
|
2022-04-09 16:42:40 +01:00
|
|
|
Path(path).name,
|
2022-03-25 12:04:29 +00:00
|
|
|
"com.visualstudio.code.oss",
|
|
|
|
100,
|
|
|
|
1.0,
|
|
|
|
{"subtext": path},
|
|
|
|
)
|
2022-03-28 09:36:38 +01:00
|
|
|
for path in get_path_list()
|
2022-04-09 16:42:40 +01:00
|
|
|
if query.lower() in Path(path).name.lower()
|
2022-03-25 12:04:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
@dbus.service.method(iface, out_signature="a(sss)")
|
|
|
|
def Actions(self):
|
|
|
|
# id, text, icon
|
|
|
|
return [("id", "Open Folder", "document-open-folder")]
|
|
|
|
|
|
|
|
@dbus.service.method(iface, in_signature="ss")
|
|
|
|
def Run(self, data: str, action_id: str):
|
2023-08-07 05:13:40 +01:00
|
|
|
subprocess.run([
|
|
|
|
"code" if not action_id else "xdg-open",
|
2023-08-29 01:49:32 +01:00
|
|
|
str(data)
|
|
|
|
], shell=True)
|
2022-03-25 12:04:29 +00:00
|
|
|
|
|
|
|
runner = Runner()
|
|
|
|
loop = GLib.MainLoop()
|
|
|
|
loop.run()
|