~ubuntu-branches/ubuntu/jaunty/imms/jaunty

« back to all changes in this revision

Viewing changes to clients/clientstub.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Veber
  • Date: 2005-04-13 23:43:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050413234311-kzr68z9l7z5mv551
Tags: 2.0.3-2
Build depend on xmms-dev (>= 1.2.10+cvs20050209)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __CLIENTSTUB_H_
 
2
#define __CLIENTSTUB_H_
 
3
 
 
4
#include "giosocket.h"
 
5
#include "utils.h"
 
6
#include "clientstubbase.h"
 
7
 
 
8
#include <stdlib.h>
 
9
#include <errno.h>
 
10
 
 
11
#include <sstream>
 
12
#include <iostream>
 
13
 
 
14
using std::stringstream;
 
15
using std::ostringstream;
 
16
using std::cerr;
 
17
using std::endl;
 
18
 
 
19
template <typename Ops>
 
20
class IMMSClient : public IMMSClientStub, protected GIOSocket 
 
21
{
 
22
public:
 
23
    IMMSClient() : connected(false) { }
 
24
    bool connect()
 
25
    {
 
26
        int fd = socket_connect(get_imms_root("socket"));
 
27
        if (fd > 0)
 
28
        {
 
29
            init(fd);
 
30
            return connected = true;
 
31
        }
 
32
        cerr << "Connection failed: " << strerror(errno) << endl;
 
33
        return false;
 
34
    }
 
35
    virtual void write_command(const string &line)
 
36
        { if (isok()) GIOSocket::write(line + "\n"); }
 
37
    virtual void process_line(const string &line)
 
38
    {
 
39
        stringstream sstr;
 
40
        sstr << line;
 
41
 
 
42
#if defined(DEBUG) && 1
 
43
        std::cout << "< " << line << endl;
 
44
#endif
 
45
 
 
46
        string command = "";
 
47
        sstr >> command;
 
48
 
 
49
        if (command == "ResetSelection")
 
50
        {
 
51
            Ops::reset_selection();
 
52
            return;
 
53
        }
 
54
        if (command == "TryAgain")
 
55
        {
 
56
            write_command("SelectNext");
 
57
            return;
 
58
        }
 
59
        if (command == "EnqueueNext")
 
60
        {
 
61
            int next;
 
62
            sstr >> next;
 
63
            Ops::set_next(next);
 
64
            return;
 
65
        }
 
66
        if (command == "PlaylistChanged")
 
67
        {
 
68
            IMMSClientStub::playlist_changed(Ops::get_length());
 
69
            return;
 
70
        }
 
71
        if (command == "GetPlaylistItem")
 
72
        {
 
73
            int i;
 
74
            sstr >> i;
 
75
            send_item("PlaylistItem", i);
 
76
            return;
 
77
        }
 
78
        if (command == "GetEntirePlaylist")
 
79
        {
 
80
            for (int i = 0; i < Ops::get_length(); ++i)
 
81
                send_item("Playlist", i);
 
82
            write_command("PlaylistEnd");
 
83
            return;
 
84
        }
 
85
 
 
86
        cerr << "IMMS: Unknown command: " << command << endl;
 
87
    }
 
88
    virtual void connection_lost() { connected = false; }
 
89
 
 
90
    bool check_connection()
 
91
    {
 
92
        if (isok())
 
93
            return false;
 
94
 
 
95
        system("immsd &");
 
96
 
 
97
        return connect();
 
98
    }
 
99
    
 
100
    bool isok() { return connected; }
 
101
private:
 
102
    bool connected;
 
103
 
 
104
    void send_item(const char *command, int i)
 
105
    {
 
106
        ostringstream osstr;
 
107
        osstr << command << " " << i << " " << Ops::get_item(i);
 
108
        write_command(osstr.str());
 
109
    }
 
110
};
 
111
 
 
112
#endif