2007-08-29 20:30:26 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-03-30 00:07:27 +02:00
|
|
|
#
|
2007-08-29 20:30:26 +02:00
|
|
|
# gPodder - A media aggregator and podcast client
|
2006-12-29 16:52:52 +01:00
|
|
|
# Copyright (C) 2005-2007 Thomas Perl <thp at perli.net>
|
2006-03-30 00:07:27 +02:00
|
|
|
#
|
2007-08-29 20:30:26 +02:00
|
|
|
# gPodder 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.
|
2006-04-07 22:22:30 +02:00
|
|
|
#
|
2007-08-29 20:30:26 +02:00
|
|
|
# gPodder is distributed in the hope that it will be useful,
|
2006-04-07 22:22:30 +02:00
|
|
|
# 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
|
2007-08-29 20:30:26 +02:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-04-07 22:22:30 +02:00
|
|
|
#
|
|
|
|
|
2006-03-30 00:07:27 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# libplayers.py -- get list of potential playback apps
|
|
|
|
# thomas perl <thp@perli.net> 20060329
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2006-12-11 01:26:10 +01:00
|
|
|
from glob import glob
|
2006-03-30 00:07:27 +02:00
|
|
|
from os.path import basename
|
|
|
|
from os.path import splitext
|
|
|
|
from os.path import exists
|
|
|
|
|
|
|
|
from ConfigParser import RawConfigParser
|
|
|
|
|
|
|
|
import gobject
|
|
|
|
|
2007-03-17 13:11:13 +01:00
|
|
|
import gtk
|
|
|
|
|
2006-03-30 00:07:27 +02:00
|
|
|
from gtk import IconTheme
|
|
|
|
from gtk import ListStore
|
|
|
|
|
|
|
|
from gtk.gdk import pixbuf_new_from_file_at_size
|
|
|
|
from gtk.gdk import Pixbuf
|
|
|
|
|
2006-11-17 15:26:10 +01:00
|
|
|
from liblogger import log
|
2006-03-30 00:07:27 +02:00
|
|
|
|
|
|
|
# where are the .desktop files located?
|
2007-04-02 13:46:48 +02:00
|
|
|
userappsdirs = [ '/usr/share/applications/', '/usr/local/share/applications/', '/usr/share/applications/kde/' ]
|
2006-03-30 00:07:27 +02:00
|
|
|
|
|
|
|
# the name of the section in the .desktop files
|
|
|
|
sect = 'Desktop Entry'
|
|
|
|
|
|
|
|
class UserApplication(object):
|
|
|
|
def __init__( self, name, cmd, mime, icon):
|
|
|
|
self.name = name
|
|
|
|
self.cmd = cmd
|
|
|
|
self.icon = icon
|
|
|
|
self.theme = IconTheme()
|
|
|
|
|
|
|
|
def get_icon( self):
|
|
|
|
if self.icon != None:
|
|
|
|
if exists( self.icon):
|
|
|
|
return pixbuf_new_from_file_at_size( self.icon, 24, 24)
|
|
|
|
icon_name = splitext( basename( self.icon))[0]
|
|
|
|
if self.theme.has_icon( icon_name):
|
|
|
|
return self.theme.load_icon( icon_name, 24, 0)
|
|
|
|
|
|
|
|
def get_name( self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_action( self):
|
|
|
|
return self.cmd
|
|
|
|
|
|
|
|
|
|
|
|
class UserAppsReader(object):
|
|
|
|
def __init__( self):
|
|
|
|
self.apps = []
|
|
|
|
|
|
|
|
def read( self):
|
2006-04-06 16:11:03 +02:00
|
|
|
for dir in userappsdirs:
|
|
|
|
if exists( dir):
|
2006-12-11 01:26:10 +01:00
|
|
|
for file in glob( dir+'/*.desktop'):
|
|
|
|
self.parse_and_append( file)
|
2007-03-17 13:11:13 +01:00
|
|
|
self.apps.append( UserApplication( 'Shell command', '', 'audio/*', gtk.STOCK_EXECUTE))
|
2006-03-30 00:07:27 +02:00
|
|
|
|
2006-12-11 01:26:10 +01:00
|
|
|
def parse_and_append( self, filename):
|
2006-03-30 00:07:27 +02:00
|
|
|
try:
|
2006-12-11 01:26:10 +01:00
|
|
|
parser = RawConfigParser()
|
|
|
|
parser.read( [ filename ])
|
|
|
|
if not parser.has_section( sect):
|
|
|
|
return
|
|
|
|
|
2006-03-30 00:07:27 +02:00
|
|
|
app_name = parser.get( sect, 'Name')
|
|
|
|
app_cmd = parser.get( sect, 'Exec')
|
|
|
|
app_mime = parser.get( sect, 'MimeType')
|
|
|
|
app_icon = parser.get( sect, 'Icon')
|
|
|
|
if app_mime.find( 'audio/') != -1:
|
2006-12-11 01:26:10 +01:00
|
|
|
log( 'Player found: %s (%s)', app_name, filename)
|
2006-03-30 00:07:27 +02:00
|
|
|
self.apps.append( UserApplication( app_name, app_cmd, app_mime, app_icon))
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_applications_as_model( self):
|
|
|
|
result = ListStore( gobject.TYPE_STRING, gobject.TYPE_STRING, Pixbuf)
|
|
|
|
for app in self.apps:
|
|
|
|
iter = result.append()
|
|
|
|
result.set_value( iter, 0, app.get_name())
|
|
|
|
result.set_value( iter, 1, app.get_action())
|
|
|
|
result.set_value( iter, 2, app.get_icon())
|
|
|
|
return result
|
|
|
|
# end of UserAppsReader
|
|
|
|
|
2006-06-13 20:52:25 +02:00
|
|
|
|
|
|
|
def dotdesktop_command( command, filename):
|
|
|
|
# the following flags are specified in the FDO standards for ".desktop" files:
|
|
|
|
# http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-0.9.4.html
|
|
|
|
if command.find( '%U') != -1:
|
|
|
|
# A list of URLs. (we only need one, anyway..)
|
2006-06-13 20:55:28 +02:00
|
|
|
return command.replace( '%U', ( '"file://%s"' % (filename) ) )
|
|
|
|
if command.find( '%u') != -1:
|
2006-06-13 20:52:25 +02:00
|
|
|
# A single URL.
|
2006-06-13 20:55:28 +02:00
|
|
|
return command.replace( '%u', ( '"file://%s"' % (filename) ) )
|
|
|
|
if command.find( '%F') != -1:
|
|
|
|
# A list of files. (we only need one...)
|
|
|
|
return command.replace( '%F', filename )
|
|
|
|
if command.find( '%f') != -1:
|
|
|
|
# A single file name, even if multiple files are selected.
|
|
|
|
return command.replace( '%f', filename )
|
|
|
|
|
|
|
|
# default known-good variant: 1st parameter = filename
|
|
|
|
return '%s "%s"' % ( command, filename )
|
2006-06-13 20:52:25 +02:00
|
|
|
# end dotdesktop_command
|
|
|
|
|
|
|
|
|