~mithunit/pogo/open-containing-folder

« back to all changes in this revision

Viewing changes to src/remote.py

  • Committer: François Ingelrest
  • Date: 2010-05-16 08:43:26 UTC
  • Revision ID: francois.ingelrest@gmail.com-20100516084326-sc82svgr51u1zyd0
* Initial import of Decibel 1.04

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Author: Ingelrest François (Francois.Ingelrest@gmail.com)
 
4
#
 
5
# This program 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 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program 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 Library General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
18
 
 
19
import dbus, os.path, sys, tools.consts
 
20
 
 
21
(CMD_ARGS, CMD_HELP, CMD_MSG) = range(3)
 
22
 
 
23
commands = {
 
24
                'play':    ( '',                 'Start playing the current track',             tools.consts.MSG_CMD_TOGGLE_PAUSE      ),
 
25
                'pause':   ( '',                 'Pause or continue playing the current track', tools.consts.MSG_CMD_TOGGLE_PAUSE      ),
 
26
                'next':    ( '',                 'Jump to the next track',                      tools.consts.MSG_CMD_NEXT              ),
 
27
                'prev':    ( '',                 'Jump to the previous track',                  tools.consts.MSG_CMD_PREVIOUS          ),
 
28
                'stop':    ( '',                 'Stop playback',                               tools.consts.MSG_CMD_STOP              ),
 
29
                'pl-set':  ( 'file1 file2...',   'Set the playlist to the given files',         tools.consts.MSG_CMD_TRACKLIST_SET     ),
 
30
                'pl-add':  ( 'file1 file2...',   'Append the given files to the playlist',      tools.consts.MSG_CMD_TRACKLIST_ADD     ),
 
31
                'pl-clr':  ( '',                 'Clear the playlist',                          tools.consts.MSG_CMD_TRACKLIST_CLR     ),
 
32
                'shuffle': ( '',                 'Shuffle the playlist',                        tools.consts.MSG_CMD_TRACKLIST_SHUFFLE ),
 
33
                'volume':  ( 'value (0 -- 100)', 'Set the volume',                              tools.consts.MSG_CMD_SET_VOLUME        ),
 
34
           }
 
35
 
 
36
# Check the command line
 
37
cmdLineOk = False
 
38
 
 
39
if len(sys.argv) > 1:
 
40
    cmdName = sys.argv[1]
 
41
 
 
42
    if cmdName not in commands:                                    print '%s is not a valid command\n'     % cmdName
 
43
    elif len(sys.argv) == 2 and commands[cmdName][CMD_ARGS] != '': print '%s needs some arguments\n'       % cmdName
 
44
    elif len(sys.argv) > 2 and commands[cmdName][CMD_ARGS] == '':  print '%s does not take any argument\n' % cmdName
 
45
    else:                                                          cmdLineOk = True
 
46
 
 
47
if not cmdLineOk:
 
48
    print 'Usage: %s command [arg1 arg2...]\n' % os.path.basename(sys.argv[0])
 
49
    print 'Command  | Arguments      | Description'
 
50
    print '-----------------------------------------------------------------------'
 
51
    for cmd, data in sorted(commands.iteritems()):
 
52
        print '%s| %s| %s' % (cmd.ljust(9), data[CMD_ARGS].ljust(17), data[CMD_HELP])
 
53
    sys.exit(1)
 
54
 
 
55
# Get a valid D-Bus interface
 
56
session        = dbus.SessionBus()
 
57
activeServices = session.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus').ListNames()
 
58
 
 
59
if not tools.consts.dbusService in activeServices:
 
60
    print '%s is not running, or D-Bus support is not available' % tools.consts.appName
 
61
    sys.exit(2)
 
62
 
 
63
remoteObject    = session.get_object(tools.consts.dbusService, tools.consts.dbusObject)
 
64
remoteInterface = dbus.Interface(remoteObject, tools.consts.dbusInterface)
 
65
 
 
66
# Send the command
 
67
if not remoteInterface.sendMsg(commands[cmdName][CMD_MSG], sys.argv[2:]):
 
68
    print '%s returned an error' % tools.consts.appName
 
69
sys.exit(0)