summaryrefslogtreecommitdiff
path: root/nephilim/misc.py
blob: 7ebecb14302900c8a25ce21966d3b4e3f178f2e8 (plain)
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
#
#    Copyright (C) 2008 jerous <jerous@gmail.com>
#    Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
#    Nephilim is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Nephilim is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Nephilim.  If not, see <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtCore, QtGui
import socket
import logging
import os

socket.setdefaulttimeout(8)

appIcon = 'gfx/nephilim_small.png'
APPNAME = 'nephilim'
ORGNAME = 'nephilim'

def sec2min(secs):
    """Converts seconds to min:sec."""
    min=int(secs/60)
    sec=secs%60
    if sec<10:sec='0'+str(sec)
    return str(min)+':'+str(sec)

class Button(QtGui.QPushButton):
    iconSize=32
    """A simple Button class which calls $onClick when clicked."""
    def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None):
        QtGui.QPushButton.__init__(self, parent)

        if onClick:
            self.connect(self, QtCore.SIGNAL('clicked(bool)'), onClick)
        if iconPath:
            self.changeIcon(iconPath)

        if not(iconPath and iconOnly):
            QtGui.QPushButton.setText(self, caption)

        self.setToolTip(caption)

    def setText(self, caption):
        self.setToolTip(caption)
        if self.icon()==None:
            self.setText(caption)

    def changeIcon(self, iconPath):
        icon=QtGui.QIcon()
        icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
        self.setIcon(icon)

def expand_tags(str, expanders):
    #ensure that str is QString
    str = QtCore.QString(str)
    for expander in expanders:
        str = expander.expand_tags(str)

    #remove unexpanded tags
    return str.replace(QtCore.QRegExp('\\$\\w+'), '')

def generate_metadata_path(main_win, song, dir_tag, file_tag):
    """Generate dirname and (db files only) full file path for reading/writing metadata files
       (cover, lyrics) from $tags in dir/filename.
        HACK main_win argument should be removed ASAP"""
    if QtCore.QDir.isAbsolutePath(song.filepath()):
        dirname  = os.path.dirname(song.filepath())
        filepath = ''
    elif '://' in song.filepath():   # we are streaming
        dirname  = ''
        filepath = ''
    else:
        dirname  = expand_tags(dir_tag, (main_win, song))
        filepath = '%s/%s'%(dirname, expand_tags(file_tag, (main_win, song)).replace('/', '_'))

    return dirname, filepath