]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/vars/time.cpp
Skins2: Cosmetics and drop const from string return as there really is no point:...
[vlc] / modules / gui / skins2 / vars / time.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 436465e..3adec76
@@ -1,11 +1,11 @@
 /*****************************************************************************
  * time.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
- * $Id: time.cpp,v 1.1 2004/01/03 23:31:34 asmax Exp $
+ * Copyright (C) 2003 the VideoLAN team
+ * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
- *          Olivier Teulière <ipkiss@via.ecp.fr>
+ *          Olivier Teulière <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
  * 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.
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <stdio.h>  // snprintf
-
+#include <vlc_input.h>
 #include "time.hpp"
-#include <vlc/input.h>
 
 
-void Time::set( double percentage, bool updateVLC )
-{
-    if( getIntf()->p_sys->p_input == NULL )
-    {
-        return;
-    }
+inline bool StreamTime::havePosition() const {
+    input_thread_t *p_input = getIntf()->p_sys->p_input;
+    return p_input && ( var_GetFloat( p_input, "position" ) != 0.0 );
+}
+
 
+void StreamTime::set( float percentage, bool updateVLC )
+{
     VarPercent::set( percentage );
 
     // Avoid looping forever...
-    if( updateVLC )
-    {
-        vlc_value_t pos;
-        pos.f_float = percentage;
-
-        var_Set( getIntf()->p_sys->p_input, "position", pos );
-    }
+    if( updateVLC && getIntf()->p_sys->p_input )
+        var_SetFloat( getIntf()->p_sys->p_input, "position", percentage );
 }
 
 
-string Time::getAsStringPercent() const
+string StreamTime::getAsStringPercent() const
 {
     int value = (int)(100. * get());
     // 0 <= value <= 100, so we need 4 chars
-    char *str = new char[4];
+    char str[4];
     snprintf( str, 4, "%d", value );
-    string ret = str;
-    delete[] str;
-
-    return ret;
+    return string(str);
 }
 
 
-string Time::getAsStringTime() const
+string StreamTime::formatTime( int seconds, bool bShortFormat ) const
 {
-    if( getIntf()->p_sys->p_input == NULL ||
-        !getIntf()->p_sys->p_input->stream.b_seekable )
+    char psz_time[MSTRTIME_MAX_SIZE];
+    if( bShortFormat && (seconds < 60 * 60) )
     {
-        return "-:--:--";
+        snprintf( psz_time, MSTRTIME_MAX_SIZE, "%02d:%02d",
+                  (int) (seconds / 60 % 60),
+                  (int) (seconds % 60) );
     }
+    else
+    {
+        snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
+                  (int) (seconds / (60 * 60)),
+                  (int) (seconds / 60 % 60),
+                  (int) (seconds % 60) );
+    }
+    return string(psz_time);
+}
 
-    vlc_value_t time;
-    char *psz_time = new char[MSTRTIME_MAX_SIZE];
-    var_Get( getIntf()->p_sys->p_input, "time", &time );
 
-    int i_seconds = time.i_time / 1000000;
-    snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
-              (int) (i_seconds / (60 * 60)),
-              (int) (i_seconds / 60 % 60),
-              (int) (i_seconds % 60) );
+string StreamTime::getAsStringCurrTime( bool bShortFormat ) const
+{
+    if( !havePosition() )
+        return "-:--:--";
+
+    mtime_t time = var_GetTime( getIntf()->p_sys->p_input, "time" );
+    return formatTime( time / 1000000, bShortFormat );
+}
+
+
+string StreamTime::getAsStringTimeLeft( bool bShortFormat ) const
+{
+    if( !havePosition() )
+        return "-:--:--";
+
+    mtime_t time = var_GetTime( getIntf()->p_sys->p_input, "time" ),
+        duration = var_GetTime( getIntf()->p_sys->p_input, "length" );
+
+    return formatTime( (duration - time) / 1000000, bShortFormat );
+}
 
-    string ret = psz_time;
-    delete[] psz_time;
 
-    return ret;
+string StreamTime::getAsStringDuration( bool bShortFormat ) const
+{
+    if( !havePosition() )
+        return "-:--:--";
+
+    mtime_t time = var_GetTime( getIntf()->p_sys->p_input, "length" );
+    return formatTime( time / 1000000, bShortFormat );
 }