2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4
import QtCore
, QtGui
24 from htmlentitydefs
import name2codepoint
as n2cp
26 socket
.setdefaulttimeout(8)
28 appIcon
= ':icons/nephilim_small.png'
32 # custom mimetypes used for drag&drop
33 MIMETYPES
= {'songs' : 'application/x-mpd-songlist', 'plistsongs' : 'application/x-mpd-playlistsonglist'}
36 """Converts seconds to min:sec."""
39 if sec
<10:sec
='0'+str(sec
)
40 return str(min)+':'+str(sec
)
42 class Button(QtGui
.QPushButton
):
44 """A simple Button class which calls $onClick when clicked."""
45 def __init__(self
, caption
, onClick
=None, iconPath
=None, iconOnly
=False, parent
=None):
46 QtGui
.QPushButton
.__init
__(self
, parent
)
49 self
.clicked
.connect(onClick
)
51 self
.changeIcon(iconPath
)
53 if not(iconPath
and iconOnly
):
54 QtGui
.QPushButton
.setText(self
, caption
)
56 self
.setToolTip(caption
)
58 def setText(self
, caption
):
59 self
.setToolTip(caption
)
63 def changeIcon(self
, iconPath
):
65 icon
.addFile(iconPath
, QtCore
.QSize(self
.iconSize
, self
.iconSize
))
68 def expand_tags(string
, expanders
):
69 for expander
in expanders
:
70 string
= expander
.expand_tags(string
)
72 #remove unexpanded tags
73 return re
.sub('\$\{.*\}', '', string
)
75 def generate_metadata_path(song
, dir_tag
, file_tag
):
76 """Generate dirname and (db files only) full file path for reading/writing metadata files
77 (cover, lyrics) from $tags in dir/filename."""
78 if QtCore
.QDir
.isAbsolutePath(song
['file']):
79 dirname
= os
.path
.dirname(song
['file'])
81 elif '://' in song
['file']: # we are streaming
85 dirname
= expand_tags(dir_tag
, (QtGui
.QApplication
.instance(), song
))
86 filepath
= '%s/%s'%(dirname
, expand_tags(file_tag
, (QtGui
.QApplication
.instance(), song
)).replace('/', '_'))
88 return dirname
, filepath
90 def substitute_entity(match
):
92 if match
.group(1) == "#":
93 if match
.group(2) == '':
94 return unichr(int(ent
))
95 elif match
.group(2) == 'x':
96 return unichr(int('0x'+ent
, 16))
104 def decode_htmlentities(string
):
105 entity_re
= re
.compile(r
'&(#?)(x?)(\w+);')
106 return entity_re
.subn(substitute_entity
, string
)[0]
108 class SongsMimeData(QtCore
.QMimeData
):
113 def set_songs(self
, songs
):
119 def set_plistsongs(self
, songs
):
120 self
.__plistsongs
= songs
122 def plistsongs(self
):
123 return self
.__plistsongs
126 types
= QtCore
.QMimeData
.formats(self
)
128 types
+= MIMETYPES
['songs']
129 if self
.__plistsongs
:
130 types
+= MIMETYPES
['plistsongs']
133 def hasFormat(self
, format
):
134 if format
== MIMETYPES
['songs'] and self
.__songs
:
136 elif format
== MIMETYPES
['plistsongs'] and self
.__plistsongs
:
138 return QtCore
.QMimeData
.hasFormat(self
, format
)