1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*****************************************************************************
* var_text.cpp
*****************************************************************************
* Copyright (C) 2003 VideoLAN
* $Id$
*
* Authors: Cyril Deguet <asmax@via.ecp.fr>
* Olivier Teulire <ipkiss@via.ecp.fr>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include "var_text.hpp"
#include "../src/vlcproc.hpp"
#include "../src/var_manager.hpp"
#include "../vars/time.hpp"
#include "../vars/volume.hpp"
#include "../vars/stream.hpp"
const string VarText::m_type = "text";
VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
m_text( pIntf, "" ), m_lastText( pIntf, "" )
{
}
VarText::~VarText()
{
// Remove the observers
VlcProc *pVlcProc = VlcProc::instance( getIntf() );
pVlcProc->getTimeVar().delObserver( this );
pVlcProc->getVolumeVar().delObserver( this );
pVlcProc->getStreamVar().delObserver( this );
VarManager *pVarManager = VarManager::instance( getIntf() );
pVarManager->getHelpText().delObserver( this );
}
const UString VarText::get() const
{
uint32_t pos;
VlcProc *pVlcProc = VlcProc::instance( getIntf() );
// Fill a temporary UString object, and replace the escape characters
// ($H for help, $T for current time, $L for time left, $D for duration,
// $V for volume)
UString temp( m_text );
// $H is processed first, in case the help string contains other variables
// to replace. And it is replaced only once, in case one of these other
// variables is $H...
if( (pos = temp.find( "$H" )) != UString::npos )
{
VarManager *pVarManager = VarManager::instance( getIntf() );
// We use .getRaw() to avoid replacing the $H recursively!
temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
}
while( (pos = temp.find( "$T" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
}
while( (pos = temp.find( "$L" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
}
while( (pos = temp.find( "$D" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getTimeVar().getAsStringDuration().c_str() );
}
while( (pos = temp.find( "$V" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
}
while( (pos = temp.find( "$N" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getStreamVar().getAsStringName().c_str() );
}
while( (pos = temp.find( "$F" )) != UString::npos )
{
temp.replace( pos, 2,
pVlcProc->getStreamVar().getAsStringFullName().c_str() );
}
return temp;
}
void VarText::set( const UString &rText )
{
// Avoid an infinite loop
if( rText == m_text )
{
return;
}
// Stop observing other variables
VlcProc *pVlcProc = VlcProc::instance( getIntf() );
pVlcProc->getTimeVar().delObserver( this );
pVlcProc->getVolumeVar().delObserver( this );
pVlcProc->getStreamVar().delObserver( this );
VarManager *pVarManager = VarManager::instance( getIntf() );
pVarManager->getHelpText().delObserver( this );
m_text = rText;
// Observe needed variables
if( m_text.find( "$H" ) != UString::npos )
{
pVarManager->getHelpText().addObserver( this );
}
if( m_text.find( "$T" ) != UString::npos )
{
pVlcProc->getTimeVar().addObserver( this );
}
if( m_text.find( "$L" ) != UString::npos )
{
pVlcProc->getTimeVar().addObserver( this );
}
if( m_text.find( "$D" ) != UString::npos )
{
pVlcProc->getTimeVar().addObserver( this );
}
if( m_text.find( "$V" ) != UString::npos )
{
pVlcProc->getVolumeVar().addObserver( this );
}
if( m_text.find( "$N" ) != UString::npos )
{
pVlcProc->getStreamVar().addObserver( this );
}
if( m_text.find( "$F" ) != UString::npos )
{
pVlcProc->getStreamVar().addObserver( this );
}
notify();
}
void VarText::onUpdate( Subject<VarPercent> &rVariable )
{
UString newText = get();
// If the text has changed, notify the observers
if( newText != m_lastText )
{
m_lastText = newText;
notify();
}
}
void VarText::onUpdate( Subject<VarText> &rVariable )
{
UString newText = get();
// If the text has changed, notify the observers
if( newText != m_lastText )
{
m_lastText = newText;
notify();
}
}