110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Protocol Handler der Erweiterung 'Synopse'."""
|
|
|
|
import traceback
|
|
|
|
import unohelper
|
|
from com.sun.star.frame import XDispatchProvider, XDispatch
|
|
from com.sun.star.lang import XServiceInfo, XInitialization
|
|
|
|
from synopse.redline_model import extract_blocks
|
|
from synopse.builder import build
|
|
from synopse import compare as compare_mod
|
|
from synopse.dialog import show_options, show_compare_options, message
|
|
from synopse.i18n import set_language, t
|
|
|
|
IMPL_NAME = "de.example.synopse.ProtocolHandler"
|
|
PROTOCOL = "de.example.synopse:"
|
|
|
|
|
|
def run(ctx):
|
|
set_language(ctx)
|
|
desktop = ctx.ServiceManager.createInstanceWithContext(
|
|
"com.sun.star.frame.Desktop", ctx)
|
|
doc = desktop.getCurrentComponent()
|
|
if doc is None or not doc.supportsService("com.sun.star.text.TextDocument"):
|
|
message(ctx, t("msg.nodoc"))
|
|
return
|
|
try:
|
|
_blocks, edit_authors, note_authors = extract_blocks(doc)
|
|
if not edit_authors and not note_authors:
|
|
message(ctx, t("msg.nochanges"))
|
|
return
|
|
opts = show_options(ctx, edit_authors, note_authors)
|
|
if opts is None:
|
|
return
|
|
build(ctx, doc, opts)
|
|
except Exception as exc: # pragma: no cover
|
|
message(ctx, t("msg.error.build", exc, traceback.format_exc()))
|
|
|
|
|
|
def run_compare(ctx):
|
|
set_language(ctx)
|
|
desktop = ctx.ServiceManager.createInstanceWithContext(
|
|
"com.sun.star.frame.Desktop", ctx)
|
|
docs = compare_mod.open_documents(ctx)
|
|
if len(docs) < 2:
|
|
message(ctx, t("msg.twodocs"))
|
|
return
|
|
try:
|
|
choice = show_compare_options(ctx, docs, desktop.getCurrentComponent())
|
|
if choice is None:
|
|
return
|
|
source, target, opts = choice
|
|
compare_mod.build(ctx, source, target, opts)
|
|
except Exception as exc: # pragma: no cover
|
|
message(ctx, t("msg.error.compare", exc, traceback.format_exc()))
|
|
|
|
|
|
class SynopseHandler(unohelper.Base, XServiceInfo, XDispatchProvider,
|
|
XDispatch, XInitialization):
|
|
|
|
def __init__(self, ctx):
|
|
self.ctx = ctx
|
|
self.frame = None
|
|
|
|
# XInitialization
|
|
def initialize(self, args):
|
|
if args:
|
|
self.frame = args[0]
|
|
|
|
# XServiceInfo
|
|
def getImplementationName(self):
|
|
return IMPL_NAME
|
|
|
|
def supportsService(self, name):
|
|
return name == "com.sun.star.frame.ProtocolHandler"
|
|
|
|
def getSupportedServiceNames(self):
|
|
return ("com.sun.star.frame.ProtocolHandler",)
|
|
|
|
# XDispatchProvider
|
|
def queryDispatch(self, url, target_frame, search_flags):
|
|
if url.Protocol == PROTOCOL:
|
|
return self
|
|
return None
|
|
|
|
def queryDispatches(self, requests):
|
|
return tuple(self.queryDispatch(r.FeatureURL, r.FrameName,
|
|
r.SearchFlags) for r in requests)
|
|
|
|
# XDispatch
|
|
def dispatch(self, url, args):
|
|
if url.Protocol != PROTOCOL:
|
|
return
|
|
if url.Path == "run":
|
|
run(self.ctx)
|
|
elif url.Path == "compare":
|
|
run_compare(self.ctx)
|
|
|
|
def addStatusListener(self, control, url):
|
|
pass
|
|
|
|
def removeStatusListener(self, control, url):
|
|
pass
|
|
|
|
|
|
g_ImplementationHelper = unohelper.ImplementationHelper()
|
|
g_ImplementationHelper.addImplementation(
|
|
SynopseHandler, IMPL_NAME, ("com.sun.star.frame.ProtocolHandler",))
|