libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YDownloadProgress.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YDownloadProgress.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <sys/stat.h>
00027 
00028 #define YUILogComponent "ui"
00029 #include "YUILog.h"
00030 
00031 #include "YUISymbols.h"
00032 #include "YDownloadProgress.h"
00033 
00034 
00035 
00036 struct YDownloadProgressPrivate
00037 {
00038     YDownloadProgressPrivate( const std::string &       label,
00039                               const std::string &       filename,
00040                               YFileSize_t               expectedSize )
00041         : label( label )
00042         , filename( filename )
00043         , expectedSize( expectedSize )
00044         {}
00045 
00046     std::string label;
00047     std::string filename;
00048     YFileSize_t expectedSize;
00049 };
00050 
00051 
00052 YDownloadProgress::YDownloadProgress( YWidget *                 parent,
00053                                       const std::string &       label,
00054                                       const std::string &       filename,
00055                                       YFileSize_t               expectedSize )
00056     : YWidget( parent )
00057     , priv( new YDownloadProgressPrivate( label, filename, expectedSize ) )
00058 {
00059     YUI_CHECK_NEW( priv );
00060 
00061     setDefaultStretchable( YD_HORIZ, true );
00062     setStretchable( YD_VERT, false );
00063 }
00064 
00065 
00066 YDownloadProgress::~YDownloadProgress()
00067 {
00068     // NOP
00069 }
00070 
00071 
00072 std::string
00073 YDownloadProgress::label() const
00074 {
00075     return priv->label;
00076 }
00077 
00078 
00079 void
00080 YDownloadProgress::setLabel( const std::string & label )
00081 {
00082     priv->label = label;
00083 }
00084 
00085 
00086 std::string
00087 YDownloadProgress::filename() const
00088 {
00089     return priv->filename;
00090 }
00091 
00092 
00093 void
00094 YDownloadProgress::setFilename( const std::string & filename )
00095 {
00096     priv->filename = filename;
00097 }
00098 
00099 
00100 YFileSize_t
00101 YDownloadProgress::expectedSize() const
00102 {
00103     return priv->expectedSize;
00104 }
00105 
00106 
00107 void
00108 YDownloadProgress::setExpectedSize( YFileSize_t newSize )
00109 {
00110     priv->expectedSize = newSize;
00111 }
00112 
00113 
00114 int
00115 YDownloadProgress::currentPercent() const
00116 {
00117     if ( priv->expectedSize == 0 )      // Avoid division by zero
00118         return 0;
00119 
00120     YFileSize_t currentSize = currentFileSize();
00121 
00122     if ( currentSize >= priv->expectedSize )
00123         return 100;
00124     else
00125         return (int) ( (100 * currentSize ) / priv->expectedSize );
00126 }
00127 
00128 
00129 YFileSize_t
00130 YDownloadProgress::currentFileSize() const
00131 {
00132     struct stat stat_info;
00133 
00134     if ( stat( priv->filename.c_str(), & stat_info ) == 0 )
00135         return (YFileSize_t) stat_info.st_size;
00136     else
00137         return 0;
00138 }
00139 
00140 
00141 const YPropertySet &
00142 YDownloadProgress::propertySet()
00143 {
00144     static YPropertySet propSet;
00145 
00146     if ( propSet.isEmpty() )
00147     {
00148         /*
00149          * @property std::string        Label           text above the progress bar
00150          * @property std::string        Filename        name of the file that is monitored
00151          * @property integer            ExpectedSize    expected size of the file in bytes
00152          * @property integer            CurrentSize     current  size of the file in bytes (read-only!)
00153          * @property integer            Value           current  percent of the download   (read-only!)
00154          */
00155         propSet.add( YProperty( YUIProperty_Label,              YStringProperty  ) );
00156         propSet.add( YProperty( YUIProperty_Filename,           YStringProperty  ) );
00157         propSet.add( YProperty( YUIProperty_ExpectedSize,       YIntegerProperty ) );
00158         propSet.add( YProperty( YUIProperty_CurrentSize,        YIntegerProperty, true ) ); // read-only
00159         propSet.add( YProperty( YUIProperty_Value,              YIntegerProperty, true ) ); // read-only
00160         propSet.add( YWidget::propertySet() );
00161     }
00162 
00163     return propSet;
00164 }
00165 
00166 
00167 bool
00168 YDownloadProgress::setProperty( const std::string & propertyName, const YPropertyValue & val )
00169 {
00170     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00171 
00172     if ( propertyName == YUIProperty_Label              )       setLabel       ( val.stringVal()  );
00173     if ( propertyName == YUIProperty_Filename           )       setFilename    ( val.stringVal()  );
00174     if ( propertyName == YUIProperty_ExpectedSize       )       setExpectedSize( val.integerVal() );
00175     else
00176     {
00177         YWidget::setProperty( propertyName, val );
00178     }
00179 
00180     return true; // success -- no special handling necessary
00181 }
00182 
00183 
00184 YPropertyValue
00185 YDownloadProgress::getProperty( const std::string & propertyName )
00186 {
00187     propertySet().check( propertyName ); // throws exceptions if not found
00188 
00189     if ( propertyName == YUIProperty_Label              )       return YPropertyValue( label()          );
00190     if ( propertyName == YUIProperty_Filename           )       return YPropertyValue( filename()       );
00191     if ( propertyName == YUIProperty_ExpectedSize       )       return YPropertyValue( expectedSize()   );
00192     if ( propertyName == YUIProperty_CurrentSize        )       return YPropertyValue( currentFileSize());
00193     if ( propertyName == YUIProperty_Value              )       return YPropertyValue( currentPercent() );
00194     else
00195     {
00196         return YWidget::getProperty( propertyName );
00197     }
00198 }
 All Classes Functions Variables Enumerations Friends