📄 pybridge.py
字号:
from gettext import gettext as _
from xpcom import components
import ctypes
import os
import shutil
import sys
import traceback
import _winreg
try:
import platformutils
platformutils.initializeLocale()
import gtcache
gtcache.init()
import app
import autoupdate
import eventloop
import config
import folder
import playlist
import prefs
import platformcfg
import singleclick
import frontend
import util
from frontend_implementation import HTMLDisplay
from frontend_implementation.UIBackendDelegate import UIBackendDelegate
from eventloop import asUrgent
from platformutils import getLongPathName
import searchengines
import views
except:
errorOnImport = True
# get a fallback error message in case we can't import util either
import traceback
importErrorMessage = (_("Error importing democracy modules:\n%s") %
traceback.format_exc())
try:
import util
importErrorMessage = util.formatFailedMessage(_("Starting up"),
withExn=True)
except:
pass
# we need to make a fake asUrgent since we probably couldn't import
# eventloop.
def asUrgent(func):
return func
else:
errorOnImport = False
nsIEventQueueService = components.interfaces.nsIEventQueueService
nsIProperties = components.interfaces.nsIProperties
nsIFile = components.interfaces.nsIFile
nsIProxyObjectManager = components.interfaces.nsIProxyObjectManager
pcfIDTVPyBridge = components.interfaces.pcfIDTVPyBridge
pcfIDTVJSBridge = components.interfaces.pcfIDTVJSBridge
pcfIDTVVLCRenderer = components.interfaces.pcfIDTVVLCRenderer
def makeComp(clsid, iid):
"""Helper function to get an XPCOM component"""
return components.classes[clsid].createInstance(iid)
def makeService(clsid, iid):
"""Helper function to get an XPCOM service"""
return components.classes[clsid].getService(iid)
def createProxyObjects():
"""Creates the jsbridge and vlcrenderer xpcom components, then wraps them in
a proxy object, then stores them in the frontend module. By making them
proxy objects, we ensure that the calls to them get made in the xul event
loop.
"""
proxyManager = makeComp("@mozilla.org/xpcomproxy;1",
nsIProxyObjectManager)
eventQueueService = makeService("@mozilla.org/event-queue-service;1",
nsIEventQueueService)
xulEventQueue = eventQueueService.getSpecialEventQueue(
nsIEventQueueService.UI_THREAD_EVENT_QUEUE)
jsBridge = makeService("@participatoryculture.org/dtv/jsbridge;1",
pcfIDTVJSBridge)
frontend.jsBridge = proxyManager.getProxyForObject(xulEventQueue,
pcfIDTVJSBridge, jsBridge, nsIProxyObjectManager.INVOKE_ASYNC |
nsIProxyObjectManager.FORCE_PROXY_CREATION)
vlcRenderer = makeService("@participatoryculture.org/dtv/vlc-renderer;1",
pcfIDTVVLCRenderer)
frontend.vlcRenderer = proxyManager.getProxyForObject(xulEventQueue,
pcfIDTVVLCRenderer, vlcRenderer,
nsIProxyObjectManager.INVOKE_SYNC |
nsIProxyObjectManager.FORCE_PROXY_CREATION)
def initializeProxyObjects(window):
frontend.vlcRenderer.init(window)
frontend.jsBridge.init(window)
def getArgumentList(commandLine):
"""Convert a nsICommandLine component to a list of arguments to pass
to the singleclick module."""
args = [commandLine.getArgument(i) for i in range(commandLine.length)]
# filter out the application.ini that gets included
if len(args) > 0 and args[0].lower().endswith('application.ini'):
args = args[1:]
return [getLongPathName(path) for path in args]
# Copied from resources.py; if you change this function here, change it
# there too.
def appRoot():
klass = components.classes["@mozilla.org/file/directory_service;1"]
service = klass.getService(nsIProperties)
file = service.get("XCurProcD", nsIFile)
return file.path
class PyBridge:
_com_interfaces_ = [pcfIDTVPyBridge]
_reg_clsid_ = "{F87D30FF-C117-401e-9194-DF3877C926D4}"
_reg_contractid_ = "@participatoryculture.org/dtv/pybridge;1"
_reg_desc_ = "Bridge into DTV Python core"
def __init__(self):
self.started = False
self.cursorDisplayCount = 0
if not errorOnImport:
self.delegate = UIBackendDelegate()
def getStartupError(self):
if not errorOnImport:
return ""
else:
return importErrorMessage
def onStartup(self, window):
if self.started:
util.failed(_("Loading window"),
details=_("onStartup called twice"))
return
else:
self.started = True
try:
logFile = config.get(prefs.LOG_PATHNAME)
if logFile is not None:
h = open(logFile, "wt")
sys.stdout = sys.stderr = util.AutoflushingStream(h)
except:
pass
initializeProxyObjects(window)
app.main()
self.initializeSearchEngines()
def onShutdown(self):
frontend.vlcRenderer.stop()
app.controller.onShutdown()
def deleteVLCCache(self):
appDataPath = platformcfg.getSpecialFolder("AppData")
if appDataPath:
vlcCacheDir = os.path.join(appDataPath, "PCF-VLC")
shutil.rmtree(vlcCacheDir, ignore_errors=True)
def shortenDirectoryName(self, path):
"""Shorten a directory name by recognizing well-known nicknames, like
"Desktop", and "My Documents"
"""
tries = [ "My Music", "My Pictures", "My Videos", "My Documents",
"Desktop",
]
for name in tries:
virtualPath = platformcfg.getSpecialFolder(name)
if virtualPath is None:
continue
if path == virtualPath:
return name
elif path.startswith(virtualPath):
relativePath = path[len(virtualPath):]
if relativePath.startswith("\\"):
return name + relativePath
else:
return "%s\\%s" % (name, relativePath)
return path
# Preference setters/getters.
#
# NOTE: these are not in the mail event loop, so we have to be careful in
# accessing data. config.get and config.set are threadsafe though.
#
def getRunAtStartup(self):
return config.get(prefs.RUN_AT_STARTUP)
def setRunAtStartup(self, value):
self.delegate.setRunAtStartup(value)
config.set(prefs.RUN_AT_STARTUP, value)
def getCheckEvery(self):
return config.get(prefs.CHECK_CHANNELS_EVERY_X_MN)
def setCheckEvery(self, value):
return config.set(prefs.CHECK_CHANNELS_EVERY_X_MN, value)
def getMoviesDirectory(self):
return config.get(prefs.MOVIES_DIRECTORY)
def changeMoviesDirectory(self, path, migrate):
app.changeMoviesDirectory(path, migrate)
def getLimitUpstream(self):
return config.get(prefs.LIMIT_UPSTREAM)
def setLimitUpstream(self, value):
config.set(prefs.LIMIT_UPSTREAM, value)
def getLimitUpstreamAmount(self):
return config.get(prefs.UPSTREAM_LIMIT_IN_KBS)
def setLimitUpstreamAmount(self, value):
return config.set(prefs.UPSTREAM_LIMIT_IN_KBS, value)
def getMaxManual(self):
return config.get(prefs.MAX_MANUAL_DOWNLOADS)
def setMaxManual(self, value):
return config.set(prefs.MAX_MANUAL_DOWNLOADS, value)
def updatePrefs(self):
import autodler
autodler.updatePrefs()
def getPreserveDiskSpace(self):
return config.get(prefs.PRESERVE_DISK_SPACE)
def setPreserveDiskSpace(self, value):
config.set(prefs.PRESERVE_DISK_SPACE, value)
def getPreserveDiskSpaceAmount(self):
return config.get(prefs.PRESERVE_X_GB_FREE)
def setPreserveDiskSpaceAmount(self, value):
return config.set(prefs.PRESERVE_X_GB_FREE, value)
def getExpireAfter(self):
return config.get(prefs.EXPIRE_AFTER_X_DAYS)
def setExpireAfter(self, value):
return config.set(prefs.EXPIRE_AFTER_X_DAYS, value)
def getSinglePlayMode(self):
return config.get(prefs.SINGLE_VIDEO_PLAYBACK_MODE)
def setSinglePlayMode(self, value):
return config.set(prefs.SINGLE_VIDEO_PLAYBACK_MODE, value)
def getBTMinPort(self):
return config.get(prefs.BT_MIN_PORT)
def setBTMinPort(self, value):
return config.set(prefs.BT_MIN_PORT, value)
def getBTMaxPort(self):
return config.get(prefs.BT_MAX_PORT)
def setBTMaxPort(self, value):
return config.set(prefs.BT_MAX_PORT, value)
def getStartupTasksDone(self):
return config.get(prefs.STARTUP_TASKS_DONE)
def setStartupTasksDone(self, value):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -