~ubuntu-branches/ubuntu/karmic/quodlibet/karmic

« back to all changes in this revision

Viewing changes to browsers/filesystem.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-01-30 23:55:34 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20090130235534-l4e72ulw0vqfo17w
Tags: 2.0-1ubuntu1
* Merge from Debian experimental (LP: #276856), remaining Ubuntu changes:
  + debian/patches/40-use-music-profile.patch:
    - Use the "Music and Movies" pipeline per default.
* Refresh the above patch for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# Copyright 2004-2005 Joe Wreschnig, Michael Urman, Iñigo Serna
3
 
#
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
7
 
#
8
 
# $Id: filesystem.py 4047 2007-04-30 03:49:58Z piman $
9
 
 
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.
13
 
 
14
 
import os
15
 
 
16
 
import gtk
17
 
 
18
 
import config
19
 
import const
20
 
import formats
21
 
import qltk
22
 
 
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
28
 
 
29
 
class FileSystem(Browser, gtk.ScrolledWindow):
30
 
    __gsignals__ = Browser.__gsignals__
31
 
    expand = qltk.RHPaned
32
 
    __library = None
33
 
 
34
 
    name = _("File System")
35
 
    accelerated_name = _("_File System")
36
 
    priority = 10
37
 
 
38
 
    def __added(klass, library, songs):
39
 
        klass.__library.remove(songs)
40
 
    __added = classmethod(__added)
41
 
 
42
 
    @classmethod
43
 
    def init(klass, library):
44
 
        klass.__glibrary = library
45
 
        klass.__library = SongFileLibrary("filesystem")
46
 
        library.connect('added', klass.__remove_because_added)
47
 
 
48
 
    @classmethod
49
 
    def __remove_because_added(klass, library, songs):
50
 
        songs = filter(klass.__library.__contains__, songs)
51
 
        klass.__library.remove(songs)
52
 
 
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)
57
 
 
58
 
        folders = filter(None, config.get("settings", "scan").split(":"))
59
 
        if folders:
60
 
            folders.append(None)
61
 
        if const.HOME not in folders:
62
 
            folders.append(const.HOME)
63
 
        if "/" not in folders:
64
 
            folders.append("/")
65
 
 
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)
71
 
 
72
 
        sel = dt.get_selection()
73
 
        sel.unselect_all()
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
77
 
        self.add(dt)
78
 
        self.show_all()
79
 
 
80
 
    def __drag_data_get(self, view, ctx, sel, tid, etime):
81
 
        for songs in self.__find_songs(view.get_selection()):
82
 
            pass
83
 
        if tid == 1:
84
 
            cant_add = filter(lambda s: not s.can_add, songs)
85
 
            if cant_add:
86
 
                qltk.ErrorMessage(
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()
90
 
                ctx.drag_abort(etime)
91
 
                return
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))
96
 
        else:
97
 
            uris = [song("~uri") for song in songs]
98
 
            sel.set_uris(uris)
99
 
 
100
 
    def __play(self, view, indices, column, player):
101
 
        player.reset()
102
 
 
103
 
    def can_filter(self, key):
104
 
        return (key == "~dirname")
105
 
 
106
 
    def filter(self, key, values):
107
 
        self.child.get_selection().unselect_all()
108
 
        for v in values: self.child.go_to(v)
109
 
 
110
 
    def restore(self):
111
 
        try: paths = config.get("browsers", "filesystem").split("\n")
112
 
        except: pass
113
 
        else:
114
 
            self.child.get_selection().unselect_all()
115
 
            for path in paths: self.child.go_to(path)
116
 
 
117
 
    def save(self):
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)
121
 
 
122
 
    def activate(self):
123
 
        copool.add(self.__songs_selected, self.child.get_selection())
124
 
 
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)
131
 
        for song in songs:
132
 
            if song not in self.__glibrary:
133
 
                i.set_sensitive(True)
134
 
                break
135
 
        menu.preseparate()
136
 
        menu.prepend(i)
137
 
        return menu
138
 
 
139
 
    def __add_songs(self, item, songs):
140
 
        songs = filter(self.__library.__contains__, songs)
141
 
        self.__library.librarian.move(songs, self.__library, self.__glibrary)
142
 
 
143
 
    def __remove_songs(self, songs):
144
 
        songs = filter(self.__glibrary.__contains__, songs)
145
 
        self.__library.librarian.move(songs, self.__glibrary, self.__library)
146
 
 
147
 
    def __find_songs(self, selection):
148
 
        model, rows = selection.get_selected_rows()
149
 
        dirs = [model[row][0] for row in rows]
150
 
        songs = []
151
 
        to_add = []
152
 
        for dir in dirs:
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)
159
 
                    if song:
160
 
                        to_add.append(song)
161
 
                        songs.append(song)
162
 
                if fn in self.__library:
163
 
                    song = self.__library[fn]
164
 
                    if not song.valid():
165
 
                        self.__library.reload(song)
166
 
                    if song in self.__library:
167
 
                        songs.append(song)
168
 
            if not len(to_add) & 0x7:
169
 
                yield songs
170
 
        self.__library.add(to_add)
171
 
        yield songs
172
 
 
173
 
    def __songs_selected(self, selection):
174
 
        if self.window:
175
 
            self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
176
 
        for songs in self.__find_songs(selection):
177
 
            yield True
178
 
        if self.window:
179
 
            self.window.set_cursor(None)
180
 
        self.emit('songs-selected', songs, None)
181
 
        self.save()
182
 
 
183
 
browsers = [FileSystem]