libyui  3.10.0
YDownloadProgress.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YDownloadProgress.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <sys/stat.h>
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUISymbols.h"
32 #include "YDownloadProgress.h"
33 
34 using std::string;
35 
36 
38 {
39  YDownloadProgressPrivate( const string & label,
40  const string & filename,
41  YFileSize_t expectedSize )
42  : label( label )
43  , filename( filename )
44  , expectedSize( expectedSize )
45  {}
46 
47  string label;
48  string filename;
49  YFileSize_t expectedSize;
50 };
51 
52 
54  const string & label,
55  const string & filename,
56  YFileSize_t expectedSize )
57  : YWidget( parent )
58  , priv( new YDownloadProgressPrivate( label, filename, expectedSize ) )
59 {
60  YUI_CHECK_NEW( priv );
61 
62  setDefaultStretchable( YD_HORIZ, true );
63  setStretchable( YD_VERT, false );
64 }
65 
66 
68 {
69  // NOP
70 }
71 
72 
73 string
75 {
76  return priv->label;
77 }
78 
79 
80 void
81 YDownloadProgress::setLabel( const string & label )
82 {
83  priv->label = label;
84 }
85 
86 
87 string
89 {
90  return priv->filename;
91 }
92 
93 
94 void
95 YDownloadProgress::setFilename( const string & filename )
96 {
97  priv->filename = filename;
98 }
99 
100 
101 YFileSize_t
103 {
104  return priv->expectedSize;
105 }
106 
107 
108 void
110 {
111  priv->expectedSize = newSize;
112 }
113 
114 
115 int
117 {
118  if ( priv->expectedSize == 0 ) // Avoid division by zero
119  return 0;
120 
121  YFileSize_t currentSize = currentFileSize();
122 
123  if ( currentSize >= priv->expectedSize )
124  return 100;
125  else
126  return (int) ( (100 * currentSize ) / priv->expectedSize );
127 }
128 
129 
130 YFileSize_t
132 {
133  struct stat stat_info;
134 
135  if ( stat( priv->filename.c_str(), & stat_info ) == 0 )
136  return (YFileSize_t) stat_info.st_size;
137  else
138  return 0;
139 }
140 
141 
142 const YPropertySet &
144 {
145  static YPropertySet propSet;
146 
147  if ( propSet.isEmpty() )
148  {
149  /*
150  * @property string Label text above the progress bar
151  * @property string Filename name of the file that is monitored
152  * @property integer ExpectedSize expected size of the file in bytes
153  * @property integer CurrentSize current size of the file in bytes (read-only!)
154  * @property integer Value current percent of the download (read-only!)
155  */
156  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
157  propSet.add( YProperty( YUIProperty_Filename, YStringProperty ) );
158  propSet.add( YProperty( YUIProperty_ExpectedSize, YIntegerProperty ) );
159  propSet.add( YProperty( YUIProperty_CurrentSize, YIntegerProperty, true ) ); // read-only
160  propSet.add( YProperty( YUIProperty_Value, YIntegerProperty, true ) ); // read-only
161  propSet.add( YWidget::propertySet() );
162  }
163 
164  return propSet;
165 }
166 
167 
168 bool
169 YDownloadProgress::setProperty( const string & propertyName, const YPropertyValue & val )
170 {
171  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
172 
173  if ( propertyName == YUIProperty_Label ) setLabel ( val.stringVal() );
174  if ( propertyName == YUIProperty_Filename ) setFilename ( val.stringVal() );
175  if ( propertyName == YUIProperty_ExpectedSize ) setExpectedSize( val.integerVal() );
176  else
177  {
178  YWidget::setProperty( propertyName, val );
179  }
180 
181  return true; // success -- no special handling necessary
182 }
183 
184 
186 YDownloadProgress::getProperty( const string & propertyName )
187 {
188  propertySet().check( propertyName ); // throws exceptions if not found
189 
190  if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
191  if ( propertyName == YUIProperty_Filename ) return YPropertyValue( filename() );
192  if ( propertyName == YUIProperty_ExpectedSize ) return YPropertyValue( expectedSize() );
193  if ( propertyName == YUIProperty_CurrentSize ) return YPropertyValue( currentFileSize());
194  if ( propertyName == YUIProperty_Value ) return YPropertyValue( currentPercent() );
195  else
196  {
197  return YWidget::getProperty( propertyName );
198  }
199 }
YPropertySet::add
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YDownloadProgress::currentPercent
int currentPercent() const
Return the percentage (0..100) of the file being downloaded so far.
Definition: YDownloadProgress.cc:116
YDownloadProgress::filename
std::string filename() const
Return the name of the file that is being monitored.
Definition: YDownloadProgress.cc:88
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:197
YDownloadProgress::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YDownloadProgress.cc:186
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YDownloadProgress::setExpectedSize
virtual void setExpectedSize(YFileSize_t newSize)
Set the expected file size.
Definition: YDownloadProgress.cc:109
YPropertyValue::stringVal
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YDownloadProgress::currentFileSize
virtual YFileSize_t currentFileSize() const
Return the current size of the file that is being downloaded or 0 if this file doesn't exist (yet).
Definition: YDownloadProgress.cc:131
YDownloadProgress::setLabel
virtual void setLabel(const std::string &label)
Set the label (the text above the progress bar).
Definition: YDownloadProgress.cc:81
YDownloadProgressPrivate
Definition: YDownloadProgress.cc:37
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YProperty
Class for widget properties.
Definition: YProperty.h:51
YWidget::setStretchable
void setStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch" regardless of any hstretch or vstretch options.
Definition: YWidget.cc:560
YDownloadProgress::label
std::string label() const
Get the label (the text above the progress bar).
Definition: YDownloadProgress.cc:74
YDownloadProgress::setFilename
virtual void setFilename(const std::string &filename)
Set the name of a new file to monitor.
Definition: YDownloadProgress.cc:95
YDownloadProgress::~YDownloadProgress
virtual ~YDownloadProgress()
Destructor.
Definition: YDownloadProgress.cc:67
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YDownloadProgress::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YDownloadProgress.cc:143
YDownloadProgress::YDownloadProgress
YDownloadProgress(YWidget *parent, const std::string &label, const std::string &filename, YFileSize_t expectedSize)
Constructor.
Definition: YDownloadProgress.cc:53
YDownloadProgress::expectedSize
YFileSize_t expectedSize() const
Return the expected file size.
Definition: YDownloadProgress.cc:102
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:104
YWidget::setDefaultStretchable
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
YDownloadProgress::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YDownloadProgress.cc:169
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395