~ubuntu-branches/ubuntu/dapper/superkaramba/dapper

« back to all changes in this revision

Viewing changes to src/noatunsensor.cpp

  • Committer: Package Import Robot
  • Author(s): Jean-Michel Kelbert
  • Date: 2004-04-10 11:26:19 UTC
  • Revision ID: package-import@ubuntu.com-20040410112619-9sw20pgtkqkckdkk
Tags: upstream-0.33
ImportĀ upstreamĀ versionĀ 0.33

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2003 by Hans Karlsson                                   *
 
3
 *   karlsson.h@home.se                                                    *
 
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
#include "noatunsensor.h"
 
11
 
 
12
NoatunSensor::NoatunSensor( int interval,  DCOPClient *c)
 
13
        : Sensor( interval )
 
14
{
 
15
    client = c;
 
16
    noatunID = "noatun";
 
17
}
 
18
 
 
19
 
 
20
NoatunSensor::~NoatunSensor()
 
21
{}
 
22
 
 
23
 
 
24
void NoatunSensor::update()
 
25
{
 
26
    QString format;
 
27
    SensorParams *sp;
 
28
    Meter *meter;
 
29
    QObjectListIt it( *objList );
 
30
 
 
31
    QString title;
 
32
    int songLength = 0;
 
33
    int currentTime = 0;
 
34
 
 
35
    bool running = isRunning();
 
36
 
 
37
    if( running )
 
38
    {
 
39
        title = getTitle();
 
40
 
 
41
       if( title == "" )
 
42
            title = "Noatun";
 
43
        currentTime = getTime();
 
44
        if( currentTime == -1 )
 
45
            currentTime = 0;
 
46
 
 
47
        songLength = getLength();
 
48
        if( songLength == -1 )
 
49
            songLength = 0;
 
50
    }
 
51
 
 
52
 
 
53
    while (it != 0)
 
54
    {
 
55
        sp = (SensorParams*)(*it);
 
56
        meter = sp->getMeter();
 
57
 
 
58
        if( running )
 
59
        {
 
60
 
 
61
            format = sp->getParam("FORMAT");
 
62
            if (format.length() == 0 )
 
63
            {
 
64
                format = "%title %time / %length";
 
65
            }
 
66
 
 
67
            if( format.lower() == "%ms" )
 
68
            {
 
69
                meter->setMax( songLength );
 
70
                meter->setValue( currentTime );
 
71
            }
 
72
            else
 
73
                if ( format.lower() == "%full" )
 
74
                {
 
75
                    meter->setValue( 1 );
 
76
                }
 
77
                else
 
78
 
 
79
                {
 
80
                   format.replace( QRegExp("%title", false), title );
 
81
                   format.replace( QRegExp("%id", false), noatunID );
 
82
 
 
83
                    format.replace( QRegExp("%length", false), QTime( 0,0,0 ).
 
84
                                    addMSecs( songLength )
 
85
                                    .toString( "h:mm:ss" ) );
 
86
 
 
87
                    format.replace( QRegExp("%time", false), QTime( 0,0,0 ).
 
88
                                    addMSecs( currentTime )
 
89
                                    .toString( "h:mm:ss" ) );
 
90
                    format.replace( QRegExp("%remain", false), QTime( 0,0,0 ).
 
91
                                    addMSecs( songLength )
 
92
                                    .addMSecs(-currentTime )
 
93
                                    .toString( "h:mm:ss" ) );
 
94
 
 
95
                    meter->setValue(format);
 
96
                }
 
97
        }
 
98
        else
 
99
 
 
100
        {
 
101
            meter->setValue("");
 
102
        }
 
103
        ++it;
 
104
 
 
105
 
 
106
    }
 
107
 
 
108
}
 
109
 
 
110
bool NoatunSensor::isRunning()
 
111
{
 
112
    QRegExp rx("(noatun)|(noatun-\\d+)");
 
113
    QCStringList list = client->registeredApplications();
 
114
    QValueList<QCString>::iterator it;
 
115
    it = list.begin();
 
116
    bool foundNoatun = false;
 
117
    noatunID = "noatun";
 
118
    while( foundNoatun == false && it != list.end() )
 
119
    {
 
120
        if( rx.search(  *it ) != -1 )
 
121
        {
 
122
            foundNoatun = true;
 
123
            noatunID = *it;
 
124
        }
 
125
        ++it;
 
126
    }
 
127
    return ( client->isApplicationRegistered ( noatunID ) );
 
128
}
 
129
 
 
130
 
 
131
QString NoatunSensor::getTitle()
 
132
{
 
133
    QByteArray data, replyData;
 
134
    QCString replyType;
 
135
    QString result;
 
136
    QDataStream arg(data, IO_WriteOnly);
 
137
    arg << 5;
 
138
    if (!client->call( noatunID, "Noatun", "title()",
 
139
                       data, replyType, replyData))
 
140
    {
 
141
        result = "";
 
142
        qDebug("there was some error using DCOP.");
 
143
    }
 
144
    else
 
145
    {
 
146
        QDataStream reply(replyData, IO_ReadOnly);
 
147
        if (replyType == "QString")
 
148
        {
 
149
            reply >> result;
 
150
            result.replace( QRegExp("_")," " );
 
151
            result.replace( QRegExp(".mp3$"),"" );
 
152
 
 
153
        }
 
154
        else
 
155
        {
 
156
            result = "";
 
157
            qDebug("title returned an unexpected type of reply!");
 
158
        }
 
159
    }
 
160
    return result;
 
161
}
 
162
 
 
163
 
 
164
int NoatunSensor::getTime()
 
165
{
 
166
    QByteArray data, replyData;
 
167
    QCString replyType;
 
168
    int result;
 
169
    QDataStream arg(data, IO_WriteOnly);
 
170
    arg << 5;
 
171
    if (!client->call( noatunID, "Noatun", "position()",
 
172
                       data, replyType, replyData))
 
173
    {
 
174
        result = 0;
 
175
        qDebug("there was some error using DCOP.");
 
176
    }
 
177
    else
 
178
    {
 
179
        QDataStream reply(replyData, IO_ReadOnly);
 
180
        if (replyType == "int")
 
181
        {
 
182
            reply >> result;
 
183
        }
 
184
        else
 
185
        {
 
186
            result = 0;
 
187
            qDebug("title returned an unexpected type of reply!");
 
188
        }
 
189
    }
 
190
    return result;
 
191
}
 
192
 
 
193
 
 
194
int NoatunSensor::getLength()
 
195
{
 
196
    QByteArray data, replyData;
 
197
    QCString replyType;
 
198
    int result;
 
199
    QDataStream arg(data, IO_WriteOnly);
 
200
    arg << 5;
 
201
    if (!client->call( noatunID, "Noatun", "length()",
 
202
                       data, replyType, replyData))
 
203
    {
 
204
        result = 0;
 
205
        qDebug("there was some error using DCOP.");
 
206
    }
 
207
    else
 
208
    {
 
209
        QDataStream reply(replyData, IO_ReadOnly);
 
210
        if (replyType == "int")
 
211
        {
 
212
            reply >> result;
 
213
        }
 
214
        else
 
215
        {
 
216
            result = 0;
 
217
            qDebug("title returned an unexpected type of reply!");
 
218
        }
 
219
    }
 
220
    return result;
 
221
}
 
222
 
 
223
 
 
224
void NoatunSensor::setMaxValue( SensorParams *sp)
 
225
{
 
226
    Meter *meter;
 
227
    meter = sp->getMeter();
 
228
    QString f;
 
229
    f = sp->getParam("FORMAT");
 
230
 
 
231
    if ( f == "%full" )
 
232
        meter->setMax( 1 );
 
233
 
 
234
}