| 1 |
################################################################################ |
|---|
| 2 |
# Copyright (C) 2006 Travis Shirk <travis@pobox.com> |
|---|
| 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 as published by |
|---|
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 |
# (at your option) any later version. |
|---|
| 8 |
# |
|---|
| 9 |
# This program is distributed in the hope that it will be useful, |
|---|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
# GNU General Public License for more details. |
|---|
| 13 |
# |
|---|
| 14 |
# You should have received a copy of the GNU General Public License |
|---|
| 15 |
# along with this program; if not, write to the Free Software |
|---|
| 16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 |
# |
|---|
| 18 |
# $Id$ |
|---|
| 19 |
################################################################################ |
|---|
| 20 |
import os, string |
|---|
| 21 |
|
|---|
| 22 |
import mesk |
|---|
| 23 |
import mesk.uri |
|---|
| 24 |
from mesk.i18n import _ |
|---|
| 25 |
|
|---|
| 26 |
def get_all_playlist_names(): |
|---|
| 27 |
playlists = [] |
|---|
| 28 |
for section in mesk.config.sections(): |
|---|
| 29 |
if section.startswith(mesk.CONFIG_PLAYLIST + '.'): |
|---|
| 30 |
name = section.split('.', 1)[1] |
|---|
| 31 |
playlists.append(name) |
|---|
| 32 |
return playlists |
|---|
| 33 |
|
|---|
| 34 |
class PlaylistConfig: |
|---|
| 35 |
name = '' |
|---|
| 36 |
uri = None |
|---|
| 37 |
|
|---|
| 38 |
def __init__(self, name): |
|---|
| 39 |
self.set_name(name) |
|---|
| 40 |
|
|---|
| 41 |
if (mesk.config.has_section(self._section) and |
|---|
| 42 |
mesk.config.has_option(self._section, 'uri') and |
|---|
| 43 |
mesk.config.get(self._section, 'uri')): |
|---|
| 44 |
# Initialize from config |
|---|
| 45 |
self.uri = mesk.uri.make_uri(mesk.config.get(self._section, 'uri')) |
|---|
| 46 |
else: |
|---|
| 47 |
if not mesk.config.has_section(self._section): |
|---|
| 48 |
mesk.config.add_section(self._section) |
|---|
| 49 |
pl_dir = mesk.config.get(mesk.CONFIG_MAIN, 'playlist_dir') |
|---|
| 50 |
import tempfile |
|---|
| 51 |
(fd, path) = tempfile.mkstemp('.xspf', 'playlist-', pl_dir) |
|---|
| 52 |
os.close(fd) |
|---|
| 53 |
self.uri = mesk.uri.make_uri('file://%s' % |
|---|
| 54 |
mesk.uri.escape_path(path)) |
|---|
| 55 |
|
|---|
| 56 |
def update(self): |
|---|
| 57 |
mesk.config.set(self._section, 'uri', str(self.uri)) |
|---|
| 58 |
|
|---|
| 59 |
def set_name(self, name): |
|---|
| 60 |
self._section = None |
|---|
| 61 |
if name == self.name or not name: |
|---|
| 62 |
return |
|---|
| 63 |
|
|---|
| 64 |
old_name = self.name |
|---|
| 65 |
self.name = name |
|---|
| 66 |
old_section = mesk.CONFIG_PLAYLIST + '.' + old_name |
|---|
| 67 |
self._section = mesk.CONFIG_PLAYLIST + '.' + self.name |
|---|
| 68 |
|
|---|
| 69 |
# Convert config if necessary |
|---|
| 70 |
if (not mesk.config.has_section(self._section) and |
|---|
| 71 |
mesk.config.has_section(old_section)): |
|---|
| 72 |
mesk.config.add_section(self._section) |
|---|
| 73 |
for nv in mesk.config.items(old_section): |
|---|
| 74 |
mesk.config.set(self._section, nv[0], nv[1]) |
|---|
| 75 |
mesk.config.remove_section(old_section) |
|---|
| 76 |
|
|---|
| 77 |
def delete(self): |
|---|
| 78 |
mesk.config.remove_section(self._section) |
|---|