1
# -*- coding: utf-8 -*-
2
# Copyright 2004-2005 Joe Wreschnig, Michael Urman, Iñigo Serna
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License version 2 as
6
# published by the Free Software Foundation
8
# $Id: filesystem.py 4047 2007-04-30 03:49:58Z piman $
10
# Some sort of crazy directory-based browser. QL is full of minor hacks
11
# to support this by automatically adding songs to the library when it
12
# needs them to be there.
23
from browsers._base import Browser
24
from library import SongFileLibrary
25
from qltk.filesel import DirectoryTree
26
from qltk.songsmenu import SongsMenu
27
from util import copool
29
class FileSystem(Browser, gtk.ScrolledWindow):
30
__gsignals__ = Browser.__gsignals__
34
name = _("File System")
35
accelerated_name = _("_File System")
38
def __added(klass, library, songs):
39
klass.__library.remove(songs)
40
__added = classmethod(__added)
43
def init(klass, library):
44
klass.__glibrary = library
45
klass.__library = SongFileLibrary("filesystem")
46
library.connect('added', klass.__remove_because_added)
49
def __remove_because_added(klass, library, songs):
50
songs = filter(klass.__library.__contains__, songs)
51
klass.__library.remove(songs)
53
def __init__(self, library, player):
54
super(FileSystem, self).__init__()
55
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
56
self.set_shadow_type(gtk.SHADOW_IN)
58
folders = filter(None, config.get("settings", "scan").split(":"))
61
if const.HOME not in folders:
62
folders.append(const.HOME)
63
if "/" not in folders:
66
dt = DirectoryTree(initial=const.HOME, folders=folders)
67
targets = [("text/x-quodlibet-songs", gtk.TARGET_SAME_APP, 1),
68
("text/uri-list", 0, 2)]
69
dt.drag_source_set(gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_COPY)
70
dt.connect('drag-data-get', self.__drag_data_get)
72
sel = dt.get_selection()
74
sel.connect_object('changed', copool.add, self.__songs_selected, sel)
75
if player: dt.connect('row-activated', self.__play, player)
76
else: self.save = lambda: None
80
def __drag_data_get(self, view, ctx, sel, tid, etime):
81
for songs in self.__find_songs(view.get_selection()):
84
cant_add = filter(lambda s: not s.can_add, songs)
87
qltk.get_top_parent(self), _("Unable to copy songs"),
88
_("The files selected cannot be copied to other "
89
"song lists or the queue.")).run()
92
to_add = filter(self.__library.__contains__, songs)
93
self.__add_songs(view, to_add)
94
filenames = [song("~filename") for song in songs]
95
sel.set("text/x-quodlibet-songs", 8, "\x00".join(filenames))
97
uris = [song("~uri") for song in songs]
100
def __play(self, view, indices, column, player):
103
def can_filter(self, key):
104
return (key == "~dirname")
106
def filter(self, key, values):
107
self.child.get_selection().unselect_all()
108
for v in values: self.child.go_to(v)
111
try: paths = config.get("browsers", "filesystem").split("\n")
114
self.child.get_selection().unselect_all()
115
for path in paths: self.child.go_to(path)
118
model, rows = self.child.get_selection().get_selected_rows()
119
paths = "\n".join([model[row][0] for row in rows])
120
config.set("browsers", "filesystem", paths)
123
copool.add(self.__songs_selected, self.child.get_selection())
125
def Menu(self, songs, songlist, library):
126
menu = SongsMenu(library, songs, remove=self.__remove_songs,
127
delete=True, accels=songlist.accelerators)
128
i = qltk.MenuItem(_("_Add to Library"), gtk.STOCK_ADD)
129
i.set_sensitive(False)
130
i.connect('activate', self.__add_songs, songs)
132
if song not in self.__glibrary:
133
i.set_sensitive(True)
139
def __add_songs(self, item, songs):
140
songs = filter(self.__library.__contains__, songs)
141
self.__library.librarian.move(songs, self.__library, self.__glibrary)
143
def __remove_songs(self, songs):
144
songs = filter(self.__glibrary.__contains__, songs)
145
self.__library.librarian.move(songs, self.__glibrary, self.__library)
147
def __find_songs(self, selection):
148
model, rows = selection.get_selected_rows()
149
dirs = [model[row][0] for row in rows]
153
for file in filter(formats.filter, sorted(os.listdir(dir))):
154
fn = os.path.realpath(os.path.join(dir, file))
155
if fn in self.__glibrary:
156
songs.append(self.__glibrary[fn])
157
elif fn not in self.__library:
158
song = formats.MusicFile(fn)
162
if fn in self.__library:
163
song = self.__library[fn]
165
self.__library.reload(song)
166
if song in self.__library:
168
if not len(to_add) & 0x7:
170
self.__library.add(to_add)
173
def __songs_selected(self, selection):
175
self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
176
for songs in self.__find_songs(selection):
179
self.window.set_cursor(None)
180
self.emit('songs-selected', songs, None)
183
browsers = [FileSystem]