Yet another fix for the script path detection
[gpodder.git] / bin / gpodder
blobafce4c8b0b86a6c09d8bd08f80d972d81946f084
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
5 # gPodder - A media aggregator and podcast client
6 # Copyright (c) 2005-2009 Thomas Perl and the gPodder Team
8 # gPodder is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # gPodder is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 """
23 gPodder enables you to subscribe to RSS feeds and download
24 podcast episodes from these feeds. gPodder can operate in
25 GUI mode and in CLI mode. Downloaded podcasts can either
26 be synchronized to portable MP3 players (including iPods)
27 or played back on the user's desktop.
28 """
30 import sys
31 import os
32 import os.path
33 import platform
35 import gettext
37 import dbus
38 import dbus.glib
40 from optparse import OptionParser
42 if __name__ == '__main__':
43 # Paths to important files
44 gpodder_script = sys.argv[0]
45 if os.path.islink(gpodder_script):
46 gpodder_script = os.readlink(gpodder_script)
47 gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
48 prefix = os.path.abspath(os.path.normpath(gpodder_dir))
50 src_dir = os.path.join(prefix, 'src')
51 data_dir = os.path.join(prefix, 'data')
52 locale_dir = os.path.join(prefix, 'share', 'locale')
53 glade_file = os.path.join(prefix, 'share', 'gpodder', 'gpodder.glade')
54 icon_file = os.path.join(prefix, 'share', 'icons', 'hicolor', 'scalable', 'apps', 'gpodder.svg')
56 if os.path.exists(src_dir) and os.path.exists(data_dir) and \
57 not prefix.startswith('/usr'):
58 # Run gPodder from local source folder (not installed)
59 print >>sys.stderr, 'Using modules from', src_dir
60 sys.path.insert(0, src_dir)
61 print >>sys.stderr, 'Using resources from', data_dir
62 locale_dir = os.path.join(data_dir, 'locale')
63 glade_file = os.path.join(data_dir, 'gpodder.glade')
64 icon_file = os.path.join(data_dir, 'gpodder.svg')
66 # Set up the path to translation files
67 gettext.bindtextdomain('gpodder', locale_dir)
69 import gpodder
71 # Enable i18n for gPodder translations
72 _ = gpodder.gettext
74 # Set up paths to gpodder.glade and gpodder.svg
75 gpodder.glade_file = glade_file
76 gpodder.icon_file = icon_file
78 s_usage = 'usage: %%prog [options]\n\n%s' % ( __doc__.strip() )
79 s_version = '%%prog %s' % ( gpodder.__version__ )
81 parser = OptionParser( usage = s_usage, version = s_version)
83 parser.add_option("-v", "--verbose",
84 action="store_true", dest="verbose", default=False,
85 help=_("Print debugging output to stdout"))
87 parser.add_option("-t", "--local",
88 action="store_true", dest="local", default=False,
89 help='Deprecated.')
91 parser.add_option("-m", "--maemo",
92 action="store_true", dest="maemo", default=False,
93 help=_("Start the Maemo user interface of gPodder"))
95 parser.add_option("-l", "--list",
96 action="store_true", dest="list", default=False,
97 help=_("List all channel subscriptions"))
99 parser.add_option("-r", "--run",
100 action="store_true", dest="run", default=False,
101 help=_("Update channel list, download new podcasts"))
103 parser.add_option("-u", "--update",
104 action="store_true", dest="update", default=False,
105 help=_("Update channel list and exit"))
107 parser.add_option("-s", "--sync",
108 action="store_true", dest="sync", default=False,
109 help=_("Synchronize channels to configured device"))
111 parser.add_option("-a", "--add", dest="add",
112 help=_("Subscribe to channel from URL"), metavar="URL")
114 parser.add_option("-d", "--delete", dest="delete",
115 help=_("Delete channel specified by URL"), metavar="URL")
117 parser.add_option("-S", "--stats",
118 action="store_true", dest="stats", default=False,
119 help=_("Get sync statistics"))
121 (options, args) = parser.parse_args(sys.argv)
123 if options.maemo:
124 gpodder.interface = gpodder.MAEMO
125 elif options.list or options.run or options.update or \
126 options.sync or options.add or options.delete:
127 gpodder.interface = gpodder.CLI
128 else:
129 gpodder.interface = gpodder.GUI
131 if options.local:
132 print >>sys.stderr, 'Ignoring deprecated option --local.'
134 if options.verbose:
135 from gpodder.liblogger import enable_verbose
136 enable_verbose()
138 # Try to find an already-running instance of gPodder
139 try:
140 session_bus = dbus.SessionBus()
141 remote_object = session_bus.get_object(gpodder.dbus_bus_name, gpodder.dbus_gui_object_path)
142 from gpodder.liblogger import log
143 log('Found gPodder GUI instance already running')
144 except dbus.exceptions.DBusException:
145 remote_object = None
147 from gpodder import console
148 if options.list:
149 console.list_channels()
150 elif options.run:
151 console.run()
152 elif options.update:
153 console.update()
154 elif options.sync:
155 console.sync_device()
156 elif options.add:
157 console.add_channel( options.add)
158 elif options.delete:
159 console.del_channel( options.delete)
160 elif options.stats:
161 console.sync_stats()
162 elif remote_object is not None:
163 # An instance of GUI is already running
164 remote_object.show_gui_window(dbus_interface=gpodder.dbus_interface)
165 else:
166 # gPodder is not yet running - create new instance
167 from gpodder import gui
168 from gpodder import SimpleGladeApp
170 # check if we have an X11 connection (but not on Windows)
171 if platform.system() != 'Windows' and \
172 not os.environ.get('DISPLAY', None):
173 print >>sys.stderr, 'Cannot start gPodder: $DISPLAY is not set.'
174 sys.exit(1)
176 SimpleGladeApp.bindtextdomain(gpodder.textdomain, locale_dir)
177 gui.main()