libyui  3.3.1
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 
35 
37 {
38  YDownloadProgressPrivate( const std::string & label,
39  const std::string & filename,
40  YFileSize_t expectedSize )
41  : label( label )
42  , filename( filename )
43  , expectedSize( expectedSize )
44  {}
45 
46  std::string label;
47  std::string filename;
48  YFileSize_t expectedSize;
49 };
50 
51 
53  const std::string & label,
54  const std::string & filename,
55  YFileSize_t expectedSize )
56  : YWidget( parent )
57  , priv( new YDownloadProgressPrivate( label, filename, expectedSize ) )
58 {
59  YUI_CHECK_NEW( priv );
60 
61  setDefaultStretchable( YD_HORIZ, true );
62  setStretchable( YD_VERT, false );
63 }
64 
65 
67 {
68  // NOP
69 }
70 
71 
72 std::string
74 {
75  return priv->label;
76 }
77 
78 
79 void
80 YDownloadProgress::setLabel( const std::string & label )
81 {
82  priv->label = label;
83 }
84 
85 
86 std::string
88 {
89  return priv->filename;
90 }
91 
92 
93 void
95 {
96  priv->filename = filename;
97 }
98 
99 
100 YFileSize_t
102 {
103  return priv->expectedSize;
104 }
105 
106 
107 void
109 {
110  priv->expectedSize = newSize;
111 }
112 
113 
114 int
116 {
117  if ( priv->expectedSize == 0 ) // Avoid division by zero
118  return 0;
119 
120  YFileSize_t currentSize = currentFileSize();
121 
122  if ( currentSize >= priv->expectedSize )
123  return 100;
124  else
125  return (int) ( (100 * currentSize ) / priv->expectedSize );
126 }
127 
128 
129 YFileSize_t
131 {
132  struct stat stat_info;
133 
134  if ( stat( priv->filename.c_str(), & stat_info ) == 0 )
135  return (YFileSize_t) stat_info.st_size;
136  else
137  return 0;
138 }
139 
140 
141 const YPropertySet &
143 {
144  static YPropertySet propSet;
145 
146  if ( propSet.isEmpty() )
147  {
148  /*
149  * @property std::string Label text above the progress bar
150  * @property std::string Filename name of the file that is monitored
151  * @property integer ExpectedSize expected size of the file in bytes
152  * @property integer CurrentSize current size of the file in bytes (read-only!)
153  * @property integer Value current percent of the download (read-only!)
154  */
155  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
156  propSet.add( YProperty( YUIProperty_Filename, YStringProperty ) );
157  propSet.add( YProperty( YUIProperty_ExpectedSize, YIntegerProperty ) );
158  propSet.add( YProperty( YUIProperty_CurrentSize, YIntegerProperty, true ) ); // read-only
159  propSet.add( YProperty( YUIProperty_Value, YIntegerProperty, true ) ); // read-only
160  propSet.add( YWidget::propertySet() );
161  }
162 
163  return propSet;
164 }
165 
166 
167 bool
168 YDownloadProgress::setProperty( const std::string & propertyName, const YPropertyValue & val )
169 {
170  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
171 
172  if ( propertyName == YUIProperty_Label ) setLabel ( val.stringVal() );
173  if ( propertyName == YUIProperty_Filename ) setFilename ( val.stringVal() );
174  if ( propertyName == YUIProperty_ExpectedSize ) setExpectedSize( val.integerVal() );
175  else
176  {
177  YWidget::setProperty( propertyName, val );
178  }
179 
180  return true; // success -- no special handling necessary
181 }
182 
183 
185 YDownloadProgress::getProperty( const std::string & propertyName )
186 {
187  propertySet().check( propertyName ); // throws exceptions if not found
188 
189  if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
190  if ( propertyName == YUIProperty_Filename ) return YPropertyValue( filename() );
191  if ( propertyName == YUIProperty_ExpectedSize ) return YPropertyValue( expectedSize() );
192  if ( propertyName == YUIProperty_CurrentSize ) return YPropertyValue( currentFileSize());
193  if ( propertyName == YUIProperty_Value ) return YPropertyValue( currentPercent() );
194  else
195  {
196  return YWidget::getProperty( propertyName );
197  }
198 }
std::string label() const
Get the label (the text above the progress bar).
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual YFileSize_t currentFileSize() const
Return the current size of the file that is being downloaded or 0 if this file doesn&#39;t exist (yet)...
Transport class for the value of simple properties.
Definition: YProperty.h:104
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:428
A set of properties to check names and types against.
Definition: YProperty.h:197
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
virtual void setExpectedSize(YFileSize_t newSize)
Set the expected file size.
std::string filename() const
Return the name of the file that is being monitored.
virtual void setLabel(const std::string &label)
Set the label (the text above the progress bar).
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual void setFilename(const std::string &filename)
Set the name of a new file to monitor.
virtual ~YDownloadProgress()
Destructor.
YDownloadProgress(YWidget *parent, const std::string &label, const std::string &filename, YFileSize_t expectedSize)
Constructor.
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:561
Class for widget properties.
Definition: YProperty.h:51
YFileSize_t expectedSize() const
Return the expected file size.
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
void setStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch" regardless of any hstretch or vstretch options.
Definition: YWidget.cc:555
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
Abstract base class of all UI widgets.
Definition: YWidget.h:54
int currentPercent() const
Return the percentage (0..100) of the file being downloaded so far.
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169