forked from quodlibet/quodlibet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
692 lines (520 loc) · 19.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# -*- coding: utf-8 -*-
# Copyright 2012 Christoph Reiter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
import __builtin__
import gettext
import locale
import os
import sys
import warnings
from quodlibet.util import set_process_title, environ, cached_func
from quodlibet.util import windows, is_osx
from quodlibet.util.path import mkdir, unexpand
from quodlibet.util.i18n import GlibTranslations, set_i18n_envvars, \
fixup_i18n_envvars
from quodlibet.util.dprint import print_, print_d, print_w, print_e
from quodlibet import const
from quodlibet.const import MinVersions, Version
PLUGIN_DIRS = ["editing", "events", "playorder", "songsmenu", "playlist",
"gstreamer", "covers"]
GlibTranslations().install(unicode=True)
class Application(object):
"""A main application class for controlling the application as a whole
and accessing sub-modules.
window - The main window which is present as long as QL is running
library - The main library (see library.SongFileLibrary)
librarian - The main (and atm only) librarian (see library.SongLibrarian)
player - see player._base
quit() - Quit the application
"""
window = None
library = None
player = None
cover_manager = None
name = None
"""The application name e.g. 'Quod Libet'"""
id = None
"""The application ID e.g. 'quodlibet'"""
@property
def librarian(self):
return self.library.librarian
@property
def browser(self):
return self.window.browser
def quit(self):
from gi.repository import GLib
def idle_quit():
if self.window:
self.window.destroy()
# so this can be called from a signal handler and before
# the main loop starts
GLib.idle_add(idle_quit, priority=GLib.PRIORITY_HIGH)
def show(self):
from quodlibet.qltk import Window
for window in Window.windows:
window.show()
def present(self):
# deiconify is needed if the window is on another workspace
from quodlibet.qltk import Window
for window in Window.windows:
window.deiconify()
window.present()
def hide(self):
from quodlibet.qltk import Window
for window in Window.windows:
window.hide()
app = Application()
def is_release():
"""Returns whether the running version is a stable release or under
development.
"""
return const.VERSION_TUPLE[-1] != -1
@cached_func
def get_base_dir():
"""The path to the quodlibet package"""
file_path = __file__
if os.name == "nt":
file_path = file_path.decode(sys.getfilesystemencoding())
return os.path.dirname(os.path.realpath(file_path))
@cached_func
def get_image_dir():
"""The path to the image directory in the quodlibet package"""
return os.path.join(get_base_dir(), "images")
@cached_func
def get_user_dir():
"""Place where QL saves its state, database, config etc."""
if os.name == "nt":
USERDIR = os.path.join(windows.get_appdate_dir(), "Quod Libet")
else:
USERDIR = os.path.join(os.path.expanduser("~"), ".quodlibet")
if 'QUODLIBET_USERDIR' in environ:
USERDIR = environ['QUODLIBET_USERDIR']
# XXX: Exec conf.py in this directory, used to override const globals
# e.g. for setting USERDIR for the Windows portable version
# Note: execfile doesn't handle unicode paths on windows, so encode.
# (this doesn't use the old win api in case of str compared to os.*)
_CONF_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "conf.py")
try:
execfile(_CONF_PATH)
except IOError:
pass
return USERDIR
def _fix_gst_leaks():
"""gst_element_add_pad and gst_bin_add are wrongly annotated and lead
to PyGObject refing the passed element.
Work around by adding a wrapper that unrefs afterwards.
Can be called multiple times.
https://bugzilla.gnome.org/show_bug.cgi?id=741390
https://bugzilla.gnome.org/show_bug.cgi?id=702960
"""
from gi.repository import Gst
assert Gst.is_initialized()
def do_wrap(func):
def wrap(self, obj):
result = func(self, obj)
obj.unref()
return result
return wrap
parent = Gst.Bin()
elm = Gst.Bin()
parent.add(elm)
if elm.__grefcount__ == 3:
elm.unref()
Gst.Bin.add = do_wrap(Gst.Bin.add)
pad = Gst.Pad.new("foo", Gst.PadDirection.SRC)
parent.add_pad(pad)
if pad.__grefcount__ == 3:
pad.unref()
Gst.Element.add_pad = do_wrap(Gst.Element.add_pad)
def _gtk_init():
"""Call before using Gtk/Gdk"""
import gi
# disable for consistency and trigger events seem a bit flaky here
if is_osx():
os.environ["GTK_OVERLAY_SCROLLING"] = "0"
# make sure GdkX11 doesn't get used under Windows
if os.name == "nt":
sys.modules["gi.repository.GdkX11"] = None
try:
# not sure if this is available under Windows
gi.require_version("GdkX11", "3.0")
from gi.repository import GdkX11
GdkX11
except (ValueError, ImportError):
pass
gi.require_version("GLib", "2.0")
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GObject", "2.0")
gi.require_version("Pango", "1.0")
gi.require_version("GdkPixbuf", "2.0")
gi.require_version("Gio", "2.0")
from gi.repository import Gtk, GObject, Gdk, GdkPixbuf
# add Gtk.TreePath.__getitem__/__len__ for PyGObject 3.2
try:
Gtk.TreePath()[0]
except TypeError:
Gtk.TreePath.__getitem__ = lambda self, index: list(self)[index]
Gtk.TreePath.__len__ = lambda self: self.get_depth()
# GTK+ 3.4+ constants
if not hasattr(Gdk, "BUTTON_PRIMARY"):
Gdk.BUTTON_PRIMARY = 1
Gdk.BUTTON_MIDDLE = 2
Gdk.BUTTON_SECONDARY = 3
if not hasattr(Gdk, "EVENT_PROPAGATE"):
Gdk.EVENT_PROPAGATE = 0
Gdk.EVENT_STOP = 1
# On windows the default variants only do ANSI paths, so replace them.
# In some typelibs they are replaced by default, in some don't..
if os.name == "nt":
for name in ["new_from_file_at_scale", "new_from_file_at_size",
"new_from_file"]:
cls = GdkPixbuf.Pixbuf
setattr(cls, name, getattr(cls, name + "_utf8", name))
# https://bugzilla.gnome.org/show_bug.cgi?id=670372
if not hasattr(GdkPixbuf.Pixbuf, "savev"):
GdkPixbuf.Pixbuf.savev = GdkPixbuf.Pixbuf.save
# Force menu/button image related settings. We might show too many atm
# but this makes sure we don't miss cases where we forgot to force them
# per widget.
# https://bugzilla.gnome.org/show_bug.cgi?id=708676
warnings.filterwarnings('ignore', '.*g_value_get_int.*', Warning)
# some day... but not now
warnings.filterwarnings(
'ignore', '.*Stock items are deprecated.*', Warning)
warnings.filterwarnings(
'ignore', '.*:use-stock.*', Warning)
warnings.filterwarnings(
'ignore', '.*The property GtkAlignment:[^\s]+ is deprecated.*',
Warning)
# Newer glib is noisy regarding deprecated signals/properties
# even with stable releases.
if is_release():
warnings.filterwarnings(
'ignore', '.* It will be removed in a future version.',
Warning)
settings = Gtk.Settings.get_default()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
settings.set_property("gtk-button-images", True)
settings.set_property("gtk-menu-images", True)
if hasattr(settings.props, "gtk_primary_button_warps_slider"):
settings.set_property("gtk-primary-button-warps-slider", True)
# Make sure PyGObject includes support for foreign cairo structs
try:
gi.require_foreign("cairo")
except AttributeError:
# older pygobject
pass
except ImportError:
print_e("PyGObject is missing cairo support")
exit(1)
# CSS overrides
if os.name == "nt":
# somehow borders are missing under Windows & Gtk+3.14
style_provider = Gtk.CssProvider()
style_provider.load_from_data("""
.menu {
border: 1px solid @borders;
}
""")
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
if sys.platform == "darwin":
# fix duplicated shadows for popups with Gtk+3.14
style_provider = Gtk.CssProvider()
style_provider.load_from_data("""
GtkWindow {
box-shadow: none;
}
.tooltip {
border-radius: 0;
padding: 0;
}
.tooltip.background {
background-clip: border-box;
}
""")
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
# https://bugzilla.gnome.org/show_bug.cgi?id=708676
warnings.filterwarnings('ignore', '.*g_value_get_int.*', Warning)
# blacklist some modules, simply loading can cause segfaults
sys.modules["gtk"] = None
sys.modules["gpod"] = None
sys.modules["glib"] = None
sys.modules["gobject"] = None
sys.modules["gnome"] = None
from quodlibet.qltk import pygobject_version
if pygobject_version < (3, 9):
GObject.threads_init()
def _gst_init():
"""Call once before importing GStreamer"""
assert "gi.repository.Gst" not in sys.modules
import gi
# We don't want python-gst, it changes API..
assert "gi.overrides.Gst" not in sys.modules
sys.modules["gi.overrides.Gst"] = None
# blacklist some modules, simply loading can cause segfaults
sys.modules["gst"] = None
# We don't depend on Gst overrides, so make sure it's initialized.
try:
gi.require_version("Gst", "1.0")
from gi.repository import Gst
except (ValueError, ImportError):
return
if Gst.is_initialized():
return
from gi.repository import GLib
try:
ok, argv = Gst.init_check(sys.argv)
except GLib.GError:
print_e("Failed to initialize GStreamer")
# Uninited Gst segfaults: make sure no one can use it
sys.modules["gi.repository.Gst"] = None
else:
sys.argv = argv
# monkey patching ahead
_fix_gst_leaks()
# https://bugzilla.gnome.org/show_bug.cgi?id=710447
import threading
threading.Thread(target=lambda: None).start()
def _gtk_icons_init(theme_search_path, default_icon_name=None):
"""Register a local fallback icon theme containing our own icons.
`default_icon_name` is the icon name used for all windows if nothing
else is specified.
"""
from gi.repository import Gtk
theme = Gtk.IconTheme.get_default()
assert os.path.exists(theme_search_path)
theme.append_search_path(theme_search_path)
if default_icon_name is not None:
assert theme.has_icon(default_icon_name)
Gtk.Window.set_default_icon_name(default_icon_name)
def _dbus_init():
"""Setup dbus mainloop integration. Call before using dbus"""
try:
from dbus.mainloop.glib import DBusGMainLoop, threads_init
except ImportError:
try:
import dbus.glib
dbus.glib
except ImportError:
return
else:
threads_init()
DBusGMainLoop(set_as_default=True)
def _gettext_init():
"""Call before using gettext helpers"""
# set by tests
if "QUODLIBET_NO_TRANS" in environ:
return
set_i18n_envvars()
fixup_i18n_envvars()
print_d("LANGUAGE: %r" % environ.get("LANGUAGE"))
print_d("LANG: %r" % environ.get("LANG"))
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
pass
# Use the locale dir in ../build/share/locale if there is one
base_dir = get_base_dir()
localedir = os.path.dirname(base_dir)
localedir = os.path.join(localedir, "build", "share", "locale")
if not os.path.isdir(localedir) and os.name == "nt":
# py2exe case
localedir = os.path.join(
base_dir, "..", "..", "share", "locale")
if os.path.isdir(localedir):
print_d("Using local localedir: %r" % unexpand(localedir))
else:
localedir = gettext.bindtextdomain("quodlibet")
try:
t = gettext.translation("quodlibet", localedir,
class_=GlibTranslations)
except IOError:
print_d("No translation found in %r" % unexpand(localedir))
t = GlibTranslations()
else:
print_d("Translations loaded: %r" % unexpand(t.path))
debug_text = environ.get("QUODLIBET_TEST_TRANS")
t.install(unicode=True, debug_text=debug_text)
def _python_init():
import sys
if sys.version_info < MinVersions.PYTHON:
actual = Version(sys.version_info[:3])
raise ImportError("Python %s required. %s found." %
(MinVersions.PYTHON, actual))
# some code depends on utf-8 default encoding (pygtk used to set it)
reload(sys)
sys.setdefaultencoding("utf-8")
__builtin__.__dict__["print_"] = print_
__builtin__.__dict__["print_d"] = print_d
__builtin__.__dict__["print_e"] = print_e
__builtin__.__dict__["print_w"] = print_w
_python_init()
_gettext_init()
def init(icon=None, proc_title=None, name=None):
global quodlibet
print_d("Entering quodlibet.init")
_gtk_init()
_gtk_icons_init(get_image_dir(), icon)
_gst_init()
_dbus_init()
_init_debug()
from gi.repository import GLib
if proc_title:
GLib.set_prgname(proc_title)
set_process_title(proc_title)
# Issue 736 - set after main loop has started (gtk seems to reset it)
GLib.idle_add(set_process_title, proc_title)
if name:
GLib.set_application_name(name)
mkdir(get_user_dir(), 0750)
print_d("Finished initialization.")
def init_plugins(no_plugins=False):
print_d("Starting plugin manager")
from quodlibet import plugins
folders = [os.path.join(get_base_dir(), "ext", kind)
for kind in PLUGIN_DIRS]
folders.append(os.path.join(get_user_dir(), "plugins"))
print_d("Scanning folders: %s" % folders)
pm = plugins.init(folders, no_plugins)
pm.rescan()
from quodlibet.qltk.edittags import EditTags
from quodlibet.qltk.renamefiles import RenameFiles
from quodlibet.qltk.tagsfrompath import TagsFromPath
EditTags.init_plugins()
RenameFiles.init_plugins()
TagsFromPath.init_plugins()
return pm
def enable_periodic_save(save_library):
import quodlibet.library
from quodlibet.util import copool
from quodlibet import config
timeout = 5 * 60 * 1000 # 5 minutes
def periodic_config_save():
while 1:
config.save()
yield
copool.add(periodic_config_save, timeout=timeout)
if not save_library:
return
def periodic_library_save():
while 1:
# max every 15 minutes
quodlibet.library.save(save_period=15 * 60)
yield
copool.add(periodic_library_save, timeout=timeout)
def is_first_session(app_name):
"""Returns True if the current session is the first one to e.g.
show a wizard/setup dialog etc.
Will return True after each upgrade as well.
app_name: e.g. 'quodlibet'
"""
from quodlibet import config
from quodlibet import const
value = config.get("memory", "%s_last_active_version" % app_name, "")
if value != const.VERSION:
return True
return False
def finish_first_session(app_name):
"""Call on shutdown so that is_first_session() works"""
from quodlibet import config
from quodlibet import const
config.set("memory", "%s_last_active_version" % app_name, const.VERSION)
def _init_debug():
from gi.repository import GLib
from quodlibet.qltk.debugwindow import ExceptionDialog
print_d("Initializing debugging extensions")
def _override_exceptions():
print_d("Enabling custom exception handler.")
sys.excepthook = ExceptionDialog.excepthook
GLib.idle_add(_override_exceptions)
# faulthandler gives a python stacktrace on segfaults..
try:
import faulthandler
except ImportError:
pass
else:
faulthandler.enable()
def _init_osx(window):
from AppKit import NSObject, NSApplication
import objc
try:
from gi.repository import GtkosxApplication
osx_app = GtkosxApplication.Application()
except ImportError:
print_d("importing GtkosxApplication failed, no native menus")
else:
window.set_as_osx_window(osx_app)
osx_app.ready()
# Instead of quitting when the main window gets closed just hide it.
# If the dock icon gets clicked we get
# applicationShouldHandleReopen_hasVisibleWindows_ and show everything.
class Delegate(NSObject):
@objc.signature('B@:#B')
def applicationShouldHandleReopen_hasVisibleWindows_(
self, ns_app, flag):
print_d("osx: handle reopen")
app.present()
return True
def applicationShouldTerminate_(self, sender):
print_d("osx: block termination")
# FIXME: figure out why idle_add is needed here
from gi.repository import GLib
GLib.idle_add(app.quit)
return False
shared_app = NSApplication.sharedApplication()
delegate = Delegate.alloc().init()
delegate.retain()
shared_app.setDelegate_(delegate)
# QL shouldn't exit on window close, EF should
if window.get_osx_is_persistent():
window.connect(
"delete-event", lambda window, event: window.hide() or True)
def main(window, before_quit=None):
print_d("Entering quodlibet.main")
from gi.repository import Gtk, Gdk
def quit_gtk(window):
if before_quit is not None:
before_quit()
# disable plugins
import quodlibet.plugins
quodlibet.plugins.quit()
# for debug: this will list active copools
from quodlibet.util import copool
copool.pause_all()
# See which browser windows are open and save their names
# so we can restore them on start
from quodlibet.qltk.browser import LibraryBrowser
LibraryBrowser.save()
# destroy all open windows so they hide immediately on close:
# destroying all top level windows doesn't work (weird errors),
# so we hide them all and only destroy our tracked instances
# (browser windows, tag editors, pref window etc.)
from quodlibet.qltk import Window
for toplevel in Gtk.Window.list_toplevels():
toplevel.hide()
for window in Window.windows:
window.destroy()
Gtk.main_quit()
print_d("Quit GTK: done.")
window.connect('destroy', quit_gtk)
if sys.platform == "darwin":
_init_osx(window)
if not window.show_maybe():
# if we don't show a window, startup isn't completed, so call manually
Gdk.notify_startup_complete()
Gtk.main()
print_d("Gtk.main() done.")