2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
2022-08-06 01:37:52 +02:00
|
|
|
import sys
|
2022-08-05 22:49:48 +02:00
|
|
|
import webbrowser
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2022-08-06 01:37:52 +02:00
|
|
|
from fastapi import FastAPI
|
2021-11-27 16:49:33 +01:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-08-06 14:18:28 +02:00
|
|
|
from PyQt6 import QtGui, QtWidgets
|
|
|
|
from PyQt6.QtCore import Qt, QThread
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2022-08-06 02:20:04 +02:00
|
|
|
from src.configure import configure_server
|
2022-08-06 01:37:52 +02:00
|
|
|
from src.router import router
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils import constants
|
2022-08-09 16:05:27 +02:00
|
|
|
from src.utils.cli import cli
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2022-01-12 15:06:32 +01:00
|
|
|
|
2022-08-06 01:37:52 +02:00
|
|
|
# Initialize the Application Server
|
|
|
|
app = FastAPI()
|
|
|
|
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
|
|
|
|
app.include_router(router)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run():
|
2022-08-09 16:05:27 +02:00
|
|
|
# Setup Base GUI
|
2022-08-05 22:49:48 +02:00
|
|
|
gui = QtWidgets.QApplication([])
|
|
|
|
gui.setQuitOnLastWindowClosed(False)
|
2022-08-06 14:18:28 +02:00
|
|
|
window = ConfigureWindow()
|
2022-08-09 16:05:27 +02:00
|
|
|
tray = create_system_tray(gui, window)
|
|
|
|
tray.show()
|
|
|
|
|
|
|
|
# Load config from CLI
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
|
|
|
|
# Trigger First Run Experience, if required
|
|
|
|
if args.config is None:
|
|
|
|
window.show()
|
|
|
|
gui.exec()
|
|
|
|
|
|
|
|
# Reload config after first run
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
# Quit if app still not configured
|
|
|
|
if args.config is None:
|
|
|
|
print('Exiting as Khoj is not configured. Configure the application to use it.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Setup Application Server
|
|
|
|
host, port, socket = configure_server(args)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
# Start Application Server
|
2022-08-05 22:49:48 +02:00
|
|
|
server = ServerThread(app, host, port, socket)
|
|
|
|
server.start()
|
|
|
|
gui.aboutToQuit.connect(server.terminate)
|
|
|
|
|
|
|
|
# Start the GUI
|
|
|
|
gui.exec()
|
|
|
|
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
class ServerThread(QThread):
|
2022-08-05 22:49:48 +02:00
|
|
|
def __init__(self, app, host=None, port=None, socket=None):
|
|
|
|
super(ServerThread, self).__init__()
|
|
|
|
self.app = app
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.socket = socket
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.wait()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=self.socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=self.host, port=self.port)
|
|
|
|
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
class ConfigureWindow(QtWidgets.QMainWindow):
|
2022-08-09 15:53:07 +02:00
|
|
|
"""Create Window to Configure Khoj
|
|
|
|
Allow user to
|
|
|
|
1. Enable/Disable search on 1. org-mode, 2. markdown, 3. beancount or 4. image content types
|
|
|
|
2. Configure the server host and port
|
|
|
|
3. Save the configuration to khoj.yml and start the server
|
|
|
|
"""
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2022-08-09 19:35:39 +02:00
|
|
|
# Initialize Configure Window
|
2022-08-06 14:18:28 +02:00
|
|
|
self.setWindowTitle("Khoj - Configure")
|
|
|
|
self.layout = QtWidgets.QVBoxLayout()
|
|
|
|
|
2022-08-09 19:35:39 +02:00
|
|
|
# Org Mode Settings
|
|
|
|
orgmode_settings = QtWidgets.QWidget()
|
|
|
|
self.orgmode_layout = QtWidgets.QVBoxLayout(orgmode_settings)
|
|
|
|
enable_orgmode_search = QtWidgets.QCheckBox(
|
|
|
|
"Search Org-Mode Files",
|
|
|
|
stateChanged = self.show_orgmode_search_options)
|
|
|
|
self.orgmode_layout.addWidget(enable_orgmode_search)
|
|
|
|
self.layout.addWidget(orgmode_settings)
|
|
|
|
|
|
|
|
# Ledger Settings
|
|
|
|
ledger_settings = QtWidgets.QWidget()
|
|
|
|
self.ledger_layout = QtWidgets.QVBoxLayout(ledger_settings)
|
|
|
|
enable_ledger_search = QtWidgets.QCheckBox(
|
|
|
|
"Search Beancount Files",
|
|
|
|
state_changed=self.show_ledger_search_options)
|
|
|
|
self.ledger_layout.addWidget(enable_ledger_search)
|
|
|
|
self.layout.addWidget(ledger_settings)
|
2022-08-06 14:18:28 +02:00
|
|
|
|
2022-08-09 19:39:49 +02:00
|
|
|
# Button to Save Settings
|
|
|
|
action_bar = QtWidgets.QWidget()
|
|
|
|
action_bar_layout = QtWidgets.QHBoxLayout(action_bar)
|
|
|
|
save_button = QtWidgets.QPushButton("Start", clicked=self.save_settings)
|
|
|
|
action_bar_layout.addWidget(save_button)
|
|
|
|
self.layout.addWidget(action_bar)
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
# Set the central widget of the Window. Widget will expand
|
|
|
|
# to take up all the space in the window by default.
|
|
|
|
self.config_window = QtWidgets.QWidget()
|
|
|
|
self.config_window.setLayout(self.layout)
|
|
|
|
|
|
|
|
self.setCentralWidget(self.config_window)
|
|
|
|
|
2022-08-09 19:39:49 +02:00
|
|
|
def save_settings(self, s):
|
|
|
|
# Save the settings to khoj.yml
|
|
|
|
pass
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
def show_orgmode_search_options(self, s):
|
|
|
|
if Qt.CheckState(s) == Qt.CheckState.Checked:
|
2022-08-09 19:35:39 +02:00
|
|
|
self.orgmode_layout.layout().addWidget(QtWidgets.QLabel("Search Org-Mode Files"))
|
|
|
|
self.orgmode_layout.layout().addWidget(QtWidgets.QLineEdit())
|
2022-08-06 14:18:28 +02:00
|
|
|
else:
|
2022-08-09 19:35:39 +02:00
|
|
|
self.orgmode_layout.layout().removeWidget(self.orgmode_layout.layout().itemAt(1).widget())
|
|
|
|
self.orgmode_layout.layout().removeWidget(self.orgmode_layout.layout().itemAt(1).widget())
|
2022-08-06 14:18:28 +02:00
|
|
|
|
|
|
|
def show_ledger_search_options(self, s):
|
|
|
|
if Qt.CheckState(s) == Qt.CheckState.Checked:
|
2022-08-09 19:35:39 +02:00
|
|
|
self.ledger_layout.layout().addWidget(QtWidgets.QLabel("Search Ledger Files"))
|
|
|
|
self.ledger_layout.layout().addWidget(QtWidgets.QLineEdit())
|
2022-08-06 14:18:28 +02:00
|
|
|
else:
|
2022-08-09 19:35:39 +02:00
|
|
|
self.ledger_layout.layout().removeWidget(self.ledger_layout.layout().itemAt(1).widget())
|
|
|
|
self.ledger_layout.layout().removeWidget(self.ledger_layout.layout().itemAt(1).widget())
|
2022-08-06 14:18:28 +02:00
|
|
|
|
|
|
|
|
2022-08-09 16:05:27 +02:00
|
|
|
def create_system_tray(gui: QtWidgets.QApplication, window: QtWidgets.QMainWindow):
|
2022-08-05 22:49:48 +02:00
|
|
|
"""Create System Tray with Menu
|
|
|
|
Menu Actions should contain
|
|
|
|
1. option to open search page at localhost:8000/
|
|
|
|
2. option to open config page at localhost:8000/config
|
|
|
|
3. to quit
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create the system tray with icon
|
2022-08-06 01:37:52 +02:00
|
|
|
icon_path = constants.web_directory / 'assets/icons/favicon-144x144.png'
|
2022-08-05 22:49:48 +02:00
|
|
|
icon = QtGui.QIcon(f'{icon_path.absolute()}')
|
|
|
|
tray = QtWidgets.QSystemTrayIcon(icon)
|
|
|
|
tray.setVisible(True)
|
|
|
|
|
|
|
|
# Create the menu and menu actions
|
|
|
|
menu = QtWidgets.QMenu()
|
|
|
|
menu_actions = [
|
|
|
|
('Search', lambda: webbrowser.open('http://localhost:8000/')),
|
2022-08-09 16:05:27 +02:00
|
|
|
('Configure', window.show),
|
2022-08-08 22:49:26 +02:00
|
|
|
('Quit', gui.quit),
|
2022-08-05 22:49:48 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
# Add the menu actions to the menu
|
|
|
|
for action_text, action_function in menu_actions:
|
|
|
|
menu_action = QtGui.QAction(action_text, menu)
|
|
|
|
menu_action.triggered.connect(action_function)
|
|
|
|
menu.addAction(menu_action)
|
|
|
|
|
|
|
|
# Add the menu to the system tray
|
|
|
|
tray.setContextMenu(menu)
|
|
|
|
|
|
|
|
return tray
|
2022-08-02 19:13:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-08-09 15:53:07 +02:00
|
|
|
run()
|