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