common: split MetadataFetcher into its own file
[nephilim.git] / nephilim / common.py
blobddbb96b98201718e1b70bc3f819eea50deaf0529
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
20 import socket
21 import logging
22 import os
23 import re
24 from htmlentitydefs import name2codepoint as n2cp
26 socket.setdefaulttimeout(8)
28 appIcon = ':icons/nephilim_small.png'
29 APPNAME = 'nephilim'
30 ORGNAME = 'nephilim'
32 # custom mimetypes used for drag&drop
33 MIMETYPES = {'songs' : 'application/x-mpd-songlist', 'plistsongs' : 'application/x-mpd-playlistsonglist'}
35 def sec2min(secs):
36 """Converts seconds to min:sec."""
37 min=int(secs/60)
38 sec=secs%60
39 if sec<10:sec='0'+str(sec)
40 return str(min)+':'+str(sec)
42 class Button(QtGui.QPushButton):
43 iconSize=32
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)
48 if onClick:
49 self.clicked.connect(onClick)
50 if iconPath:
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)
60 if self.icon()==None:
61 self.setText(caption)
63 def changeIcon(self, iconPath):
64 icon=QtGui.QIcon()
65 icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
66 self.setIcon(icon)
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'])
80 filepath = ''
81 elif '://' in song['file']: # we are streaming
82 dirname = ''
83 filepath = ''
84 else:
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):
91 ent = match.group(3)
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))
97 else:
98 cp = n2cp.get(ent)
99 if cp:
100 return unichr(cp)
101 else:
102 return match.group()
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):
109 # private
110 __songs = None
111 __plistsongs = None
113 def set_songs(self, songs):
114 self.__songs = songs
116 def songs(self):
117 return self.__songs
119 def set_plistsongs(self, songs):
120 self.__plistsongs = songs
122 def plistsongs(self):
123 return self.__plistsongs
125 def formats(self):
126 types = QtCore.QMimeData.formats(self)
127 if self.__songs:
128 types += MIMETYPES['songs']
129 if self.__plistsongs:
130 types += MIMETYPES['plistsongs']
131 return types
133 def hasFormat(self, format):
134 if format == MIMETYPES['songs'] and self.__songs:
135 return True
136 elif format == MIMETYPES['plistsongs'] and self.__plistsongs:
137 return True
138 return QtCore.QMimeData.hasFormat(self, format)