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 #include <boost/multiprecision/cpp_int.hpp> 69 #define MIN_FREE_MB_WARN 400 70 #define MIN_FREE_MB_PROXIMITY 700 72 #define MIN_PERCENT_WARN 90 73 #define MIN_PERCENT_PROXIMITY 80 75 #define OVERFLOW_MB_WARN 0 76 #define OVERFLOW_MB_PROXIMITY 300 93 ZyppDuSet diskUsage = zypp::getZYpp()->diskUsage();
95 if ( diskUsage.empty() )
97 zypp::getZYpp()->setPartitions( zypp::DiskUsageCounter::detectMountPoints() );
98 diskUsage = zypp::getZYpp()->diskUsage();
111 int usedPercentInt(
const FSize &used,
const FSize &total)
116 percent = int(( 100 * used ) / total);
129 for (
const ZyppPartitionDu &du: get_du())
131 if (
int(du.dir.length()) > width )
132 width = du.dir.length();
134 yuiDebug() <<
"The longest mount point path: " << width <<
" characters" << endl;
141 if (width > NCurses::cols() - 6)
142 width = NCurses::cols() - 6;
144 yuiDebug() <<
"Dialog width: " << width << endl;
155 return (NCurses::cols() - dialogWidth()) / 2;
167 NCPkgDiskspace::NCPkgDiskspace(
bool testMode )
168 : testmode( testMode )
174 yuiMilestone() <<
"TESTMODE Diskspace" << endl;
175 zypp::getZYpp()->setPartitions(zypp::DiskUsageCounter::detectMountPoints());
176 testDiskUsage = zypp::getZYpp()->diskUsage();
188 NCPkgDiskspace::~NCPkgDiskspace()
200 void NCPkgDiskspace::fillPartitionTable()
202 NCTable * partitions = popupWin->Partitions();
203 partitions->deleteAllItems();
205 ZyppDuSet du = get_du();
206 for (
const ZyppPartitionDu &item: du)
211 FSize pkg_used (item.pkg_size, FSize::Unit::K);
212 FSize pkg_available ((item.total_size - item.pkg_size), FSize::Unit::K);
213 FSize total (item.total_size, FSize::Unit::K);
215 YTableItem *newItem =
new YTableItem( item.dir,
217 pkg_available.form(8),
219 usedPercent( pkg_used, total ) );
221 partitions->addItem( newItem );
234 std::string NCPkgDiskspace::checkDiskSpace()
236 ZyppDuSet diskUsage = get_du();
238 std::string text =
"";
239 for (
const ZyppPartitionDu &du: diskUsage)
244 FSize pkg_available(du.total_size - du.pkg_size, FSize::Unit::K);
245 if ( pkg_available < 0 )
253 text += NCPkgStrings::MoreText();
255 text += pkg_available.asString();
257 text += NCPkgStrings::MoreSpaceText();
273 void NCPkgDiskspace::checkRemainingDiskSpace(
const ZyppPartitionDu & partition )
275 if ( partition.readonly )
278 FSize usedSize ( partition.pkg_size, FSize::Unit::K );
279 FSize totalSize ( partition.total_size, FSize::Unit::K );
281 int percent = usedPercentInt(usedSize, totalSize);
284 boost::multiprecision::cpp_int free = ( totalSize - usedSize ).in_unit(FSize::Unit::M);
286 yuiMilestone() <<
"Partition: " << partition.dir <<
" Used percent: " 287 << percent <<
" Free: " << free << endl;
289 if ( percent > MIN_PERCENT_WARN )
293 if ( free < MIN_FREE_MB_PROXIMITY )
295 yuiWarning() <<
"free < MIN_FREE_MB_PROXIMITY (" << MIN_FREE_MB_PROXIMITY <<
")" << endl;
298 if ( free < MIN_FREE_MB_WARN )
300 yuiWarning() <<
"free < MIN_FREE_MB_WARN (" << MIN_FREE_MB_WARN <<
")" << endl;
305 if ( free < MIN_FREE_MB_PROXIMITY )
307 if ( percent > MIN_PERCENT_PROXIMITY )
311 if ( free < OVERFLOW_MB_WARN )
314 if ( free < OVERFLOW_MB_PROXIMITY )
332 void NCPkgDiskspace::setDiskSpace( wint_t ch )
335 for (
const ZyppPartitionDu &partitionDu: testDiskUsage )
337 FSize usedSize ( partitionDu.pkg_size, FSize::Unit::K );
338 FSize totalSize ( partitionDu.total_size, FSize::Unit::K );
339 int percent = usedPercentInt(usedSize, totalSize);
343 else if ( ch ==
'-' )
349 partitionDu.pkg_size = partitionDu.total_size / 100 * percent;
351 FSize newSize ( partitionDu.pkg_size, FSize::Unit::K );
353 yuiMilestone() <<
"Used size (MiB): " << newSize.in_unit(FSize::Unit::M) << endl;
354 yuiMilestone() <<
"Total size (MiB): " << totalSize.in_unit(FSize::Unit::M) << endl;
366 void NCPkgDiskspace::checkDiskSpaceRange()
369 runningOutWarning.
clear();
370 overflowWarning.
clear();
374 diskUsage = testDiskUsage;
376 diskUsage = zypp::getZYpp()->diskUsage();
378 for (
const ZyppPartitionDu &du: diskUsage)
383 checkRemainingDiskSpace( du );
389 showInfoPopup( _(
"Error: Out of disk space!" ) );
397 showInfoPopup( _(
"Warning: Disk space is running out!" ) );
410 yuiMilestone() <<
"Running out Warning:" << endl;
411 runningOutWarning.logSettings();
413 yuiMilestone() <<
"Overflow Warning:" << endl;
414 overflowWarning.logSettings();
418 std::string NCPkgDiskspace::usedPercent(
const FSize &used,
const FSize &total )
420 int percent = usedPercentInt(used, total);
421 return zypp::str::form(
"%2d%%", percent );
433 void NCPkgDiskspace::showInfoPopup( std::string headline )
436 popupWin =
new NCPkgPopupDiskspace (wpos( (NCurses::lines() - 15)/2, dialogXpos() ), headline );
438 fillPartitionTable();
440 YDialog::deleteTopmostDialog();
443 FSize NCPkgDiskspace::calculateDiff()
445 ZyppDuSet diskUsage = get_du();
448 for (
const ZyppPartitionDu &du: diskUsage)
450 diff += FSize(du.pkg_size - du.used_size, FSize::Unit::K);
462 NCPkgPopupDiskspace::NCPkgPopupDiskspace(
const wpos at, std::string headline )
463 : NCPopup( at, false )
468 createLayout( headline );
477 NCPkgPopupDiskspace::~NCPkgPopupDiskspace()
489 void NCPkgPopupDiskspace::createLayout( std::string headline )
492 NCLayoutBox * split =
new NCLayoutBox(
this, YD_VERT );
494 head =
new NCLabel( split,
"",
true,
false );
495 head->setLabel( headline );
497 YTableHeader * tableHeader =
new YTableHeader();
498 tableHeader->addColumn( NCPkgStrings::Partition(), YAlignBegin );
499 tableHeader->addColumn( NCPkgStrings::UsedSpace(), YAlignBegin );
500 tableHeader->addColumn( NCPkgStrings::FreeSpace(), YAlignBegin );
501 tableHeader->addColumn( NCPkgStrings::TotalSpace(), YAlignBegin );
502 tableHeader->addColumn(
"% ", YAlignBegin );
505 partitions =
new NCTable( split, tableHeader );
509 okButton->setFunctionKey( 10 );
510 okButton->setKeyboardFocus();
520 int NCPkgPopupDiskspace::preferredWidth()
522 return dialogWidth();
531 int NCPkgPopupDiskspace::preferredHeight()
533 if ( NCurses::lines() > 15 )
536 return NCurses::lines()-4;
539 void NCPkgPopupDiskspace::doit()
541 postevent = NCursesEvent();
546 }
while ( postAgain() );
559 NCursesEvent NCPkgPopupDiskspace::wHandleInput( wint_t ch )
562 return NCursesEvent::cancel;
564 if ( ch == KEY_RETURN )
565 return NCursesEvent::button;
567 return NCDialog::wHandleInput( ch );
578 bool NCPkgPopupDiskspace::postAgain()
580 if ( ! postevent.widget )
583 if ( postevent == NCursesEvent::button || postevent == NCursesEvent::cancel )
604 _hasBeenClose = _isClose;
613 _hasBeenClose =
false;
614 _warningPosted =
false;
630 _hasBeenClose =
true;
637 _warningPosted =
true;
651 return ! _isClose && ! _hasBeenClose;
658 return _inRange && ! _warningPosted;
662 NCPkgWarningRangeNotifier::logSettings()
const 664 yuiMilestone() <<
"in range: " << (_inRange?
"true":
"false") << endl;
665 yuiMilestone() <<
"is close: " << (_isClose?
"true":
"false") << endl;
666 yuiMilestone() <<
"has been close: " << (_hasBeenClose?
"true":
"false") << endl;
667 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.