42 #define YUILogComponent "ncurses-pkg" 45 #include "YMenuButton.h" 49 #include "NCLayoutBox.h" 50 #include "NCSpacing.h" 51 #include "NCPkgStrings.h" 53 #include "NCPushButton.h" 58 #include "NCPkgPopupDiskspace.h" 63 #include <zypp/base/String.h> 66 #define MIN_FREE_MB_WARN 400 67 #define MIN_FREE_MB_PROXIMITY 700 69 #define MIN_PERCENT_WARN 90 70 #define MIN_PERCENT_PROXIMITY 80 72 #define OVERFLOW_MB_WARN 0 73 #define OVERFLOW_MB_PROXIMITY 300 89 NCPkgDiskspace::NCPkgDiskspace(
bool testMode )
90 : testmode( testMode )
96 yuiMilestone() <<
"TESTMODE Diskspace" << endl;
97 zypp::getZYpp()->setPartitions(zypp::DiskUsageCounter::detectMountPoints ());
98 testDiskUsage = zypp::getZYpp()->diskUsage();
110 NCPkgDiskspace::~NCPkgDiskspace()
115 std::string formatSize(
double size,
int width = 0)
118 FSize::Unit unit = (size >= FSize::TB) ? FSize::T : FSize(size).bestUnit();
119 int prec = unit == FSize::B ? 0 : 2;
121 return zypp::str::form(
"%*.*f %s", width, prec, size / FSize::factor(unit), FSize::unit(unit));
131 int usedPercentInt(
long long used,
long long total)
137 percent = ( 100.0 * used ) / total;
143 std::string usedPercentStr(
long long used,
long long total)
145 int percent = usedPercentInt(used, total);
146 return zypp::str::form(
"%2d%%", percent );
158 void NCPkgDiskspace::fillPartitionTable()
160 NCTable * partitions = popupWin->Partitions();
161 partitions->deleteAllItems();
163 zypp::ZYpp::Ptr z = zypp::getZYpp();
164 ZyppDuSet du = z->diskUsage ();
169 z->setPartitions(zypp::DiskUsageCounter::detectMountPoints ());
173 for (
const ZyppPartitionDu &item: du)
178 double pkg_used = double(item.pkg_size) * FSize::KB;
179 double pkg_available = double(item.total_size - item.pkg_size) * FSize::KB;
180 double total = double(item.total_size) * FSize::KB;
182 YTableItem * newItem =
new YTableItem( item.dir,
183 formatSize(pkg_used, 8),
184 formatSize(pkg_available, 8),
185 formatSize(total, 8),
186 usedPercentStr( item.pkg_size, item.total_size ) );
188 partitions->addItem( newItem );
201 std::string NCPkgDiskspace::checkDiskSpace()
205 zypp::ZYpp::Ptr z = zypp::getZYpp();
206 ZyppDuSet du = z->diskUsage ();
211 z->setPartitions(zypp::DiskUsageCounter::detectMountPoints ());
215 for (
const ZyppPartitionDu &item: du)
221 long long pkg_available = item.total_size - item.pkg_size;
222 if ( pkg_available < 0 )
228 text += NCPkgStrings::MoreText();
231 text += formatSize(-1.0 *
double(pkg_available) * FSize::KB);
233 text += NCPkgStrings::MoreSpaceText();
249 void NCPkgDiskspace::checkRemainingDiskSpace(
const ZyppPartitionDu & partition )
251 if ( partition.readonly )
254 int percent = usedPercentInt(partition.pkg_size, partition.total_size);
257 long long free = (partition.total_size - partition.pkg_size) / 1024;
259 yuiMilestone() <<
"Partition: " << partition.dir <<
" Total (MiB): " 260 << partition.total_size / 1024 <<
" Used (MiB): " << partition.pkg_size / 1024
261 <<
" Used percent: " << percent <<
"% Free (MiB): " << free << endl;
263 if ( percent > MIN_PERCENT_WARN )
267 if ( free < MIN_FREE_MB_PROXIMITY )
269 yuiWarning() <<
"free < MIN_FREE_MB_PROXIMITY (" << MIN_FREE_MB_PROXIMITY <<
")" << endl;
272 if ( free < MIN_FREE_MB_WARN )
274 yuiWarning() <<
"free < MIN_FREE_MB_WARN (" << MIN_FREE_MB_WARN <<
")" << endl;
279 if ( free < MIN_FREE_MB_PROXIMITY )
281 if ( percent > MIN_PERCENT_PROXIMITY )
285 if ( free < OVERFLOW_MB_WARN )
288 if ( free < OVERFLOW_MB_PROXIMITY )
306 void NCPkgDiskspace::setDiskSpace( wint_t ch )
309 for (
const ZyppPartitionDu &partitionDu: testDiskUsage )
311 int percent = usedPercentInt(partitionDu.pkg_size, partitionDu.total_size);
315 else if ( ch ==
'-' )
321 partitionDu.pkg_size = partitionDu.total_size / 100 * percent;
324 yuiMilestone() <<
"Used size (MiB): " << partitionDu.pkg_size / 1024 << endl;
325 yuiMilestone() <<
"Total size (MiB): " << partitionDu.total_size / 1024 << endl;
337 void NCPkgDiskspace::checkDiskSpaceRange( )
340 runningOutWarning.
clear();
341 overflowWarning.
clear();
345 diskUsage = testDiskUsage;
347 diskUsage = zypp::getZYpp()->diskUsage();
349 for ( ZyppDuSetIterator it = diskUsage.begin();
350 it != diskUsage.end();
356 checkRemainingDiskSpace( *it );
362 showInfoPopup( _(
"Error: Out of disk space!" ) );
370 showInfoPopup( _(
"Warning: Disk space is running out!" ) );
383 yuiMilestone() <<
"Running out Warning:" << endl;
384 runningOutWarning.logSettings();
386 yuiMilestone() <<
"Overflow Warning:" << endl;
387 overflowWarning.logSettings();
392 std::string NCPkgDiskspace::usedPercent( FSize used, FSize total )
398 percent = ( 100 * used ) / total;
400 sprintf( percentStr,
"%d%%", percent );
413 void NCPkgDiskspace::showInfoPopup( std::string headline )
416 popupWin =
new NCPkgPopupDiskspace (wpos( (NCurses::lines() - 15)/2, NCurses::cols()/6 ), headline );
418 fillPartitionTable();
420 YDialog::deleteTopmostDialog();
423 zypp::ByteCount NCPkgDiskspace::calculateDiff()
425 zypp::ZYpp::Ptr z = zypp::getZYpp();
426 ZyppDuSet du = z->diskUsage ();
431 z->setPartitions(zypp::DiskUsageCounter::detectMountPoints ());
435 zypp::ByteCount diff = 0;
436 for (
const ZyppPartitionDu &item: du)
439 diff += (item.pkg_size - item.used_size) * 1024;
451 NCPkgPopupDiskspace::NCPkgPopupDiskspace(
const wpos at, std::string headline )
452 : NCPopup( at, false )
457 createLayout( headline );
466 NCPkgPopupDiskspace::~NCPkgPopupDiskspace()
478 void NCPkgPopupDiskspace::createLayout( std::string headline )
481 NCLayoutBox * split =
new NCLayoutBox(
this, YD_VERT );
483 head =
new NCLabel( split,
"",
true,
false );
484 head->setLabel( headline );
486 YTableHeader * tableHeader =
new YTableHeader();
487 tableHeader->addColumn( NCPkgStrings::Partition(), YAlignBegin );
488 tableHeader->addColumn( NCPkgStrings::UsedSpace(), YAlignBegin );
489 tableHeader->addColumn( NCPkgStrings::FreeSpace(), YAlignBegin );
490 tableHeader->addColumn( NCPkgStrings::TotalSpace(), YAlignBegin );
491 tableHeader->addColumn(
"% ", YAlignBegin );
494 partitions =
new NCTable( split, tableHeader );
498 okButton->setFunctionKey( 10 );
499 okButton->setKeyboardFocus();
509 int NCPkgPopupDiskspace::preferredWidth()
511 return NCurses::cols()*2/3;
520 int NCPkgPopupDiskspace::preferredHeight()
522 if ( NCurses::lines() > 15 )
525 return NCurses::lines()-4;
528 void NCPkgPopupDiskspace::doit()
530 postevent = NCursesEvent();
534 }
while ( postAgain() );
547 NCursesEvent NCPkgPopupDiskspace::wHandleInput( wint_t ch )
550 return NCursesEvent::cancel;
552 if ( ch == KEY_RETURN )
553 return NCursesEvent::button;
555 return NCDialog::wHandleInput( ch );
566 bool NCPkgPopupDiskspace::postAgain()
568 if ( ! postevent.widget )
571 if ( postevent == NCursesEvent::button || postevent == NCursesEvent::cancel )
592 _hasBeenClose = _isClose;
601 _hasBeenClose =
false;
602 _warningPosted =
false;
618 _hasBeenClose =
true;
625 _warningPosted =
true;
639 return ! _isClose && ! _hasBeenClose;
646 return _inRange && ! _warningPosted;
650 NCPkgWarningRangeNotifier::logSettings()
const 652 yuiMilestone() <<
"in range: " << (_inRange?
"true":
"false") << endl;
653 yuiMilestone() <<
"is close: " << (_isClose?
"true":
"false") << endl;
654 yuiMilestone() <<
"has been close: " << (_hasBeenClose?
"true":
"false") << endl;
655 yuiMilestone() <<
"warning posted: " << (_warningPosted?
"true":
"false") << endl;
bool needWarning() const
Check if a warning should be posted, i.e.
void warningPostedNotify()
Notification that a warning has been posted.
void clearHistory()
Clear everything, including all history values such as if a warning has been posted.
bool leavingProximity() const
Check if the value is leaving the proximity range.
void clear()
Clear the current values, i.e.
NCPkgWarningRangeNotifier()
Constructor.
bool inRange() const
Check if the value is in range, i.e.
void enterProximity()
Notification that the proximity range is entered, i.e.
void enterRange()
Notification that the inner range is entered.
static const std::string OKLabel()
The label of the OK button.