1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
/*
* kdfwidget.cpp
*
* $Id: kdfwidget.cpp,v 1.41 2004/07/13 20:33:13 bmeyer Exp $
*
* Copyright (c) 1998-2001 Michael Kropfberger <michael.kropfberger@gmx.net>
*
* Requires the Qt widget libraries, available at no cost at
* http://www.troll.no/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//
// 1999-11-29 Espen Sand
// Converted to QLayout and QListView + cleanups
// 1999-12-05 Espen Sand
// Usage bars should work again.
//
#include <stdlib.h>
#include <qheader.h>
#include <qtimer.h>
#include <qlayout.h>
#include <qpainter.h>
#include <kapplication.h>
#include <kcmenumngr.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kprocess.h>
#include "listview.h"
#include "kdfwidget.h"
#include "optiondialog.h"
#define BAR_COLUMN 7
#define FULL_PERCENT 95.0
#ifndef GUI_DEFINED
static bool GUI;
#define GUI_DEFINED
#endif
/**************************************************************/
CListViewItem::CListViewItem( CListView * parent, QListViewItem * after )
:QListViewItem( parent, after )
{}
// 2001-03-10 Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
// This method returns for all numeric columns the converted number.
// The conversion is done with
// 1. fixed position of the colon
// 2. a sufficient number of leading blanks
// This ensures that the lexical string comparison gives numerical order.
QString CListViewItem::key ( int column, bool ) const
{
QString tmp;
switch (column) {
case KDFWidget::sizeCol:
tmp.sprintf("%10d",size);
break;
case KDFWidget::freeCol:
tmp.sprintf("%10d",avail);
break;
case KDFWidget::fullCol:
case KDFWidget::usageCol:
tmp.sprintf("%7.2f",full);
break;
default:
tmp = text(column);
break;
}
return tmp;
}
void CListViewItem::setKeys (int kb_size, int kb_avail, float percent_full)
{
size = kb_size;
avail = kb_avail;
full = (percent_full >= 0.) ? percent_full : 0.;
return;
}
/**************************************************************/
KDFWidget::KDFWidget( QWidget *parent, const char *name, bool init )
: QWidget(parent, name), mOptionDialog(0), mPopup(0), mTimer(0)
{
connect(&mDiskList , SIGNAL(readDFDone() ),
this, SLOT (updateDFDone()) );
connect(&mDiskList , SIGNAL(criticallyFull(DiskEntry*)),
this, SLOT (criticallyFull(DiskEntry*)) );
mTabProp.resize(8);
mTabProp[0] = new CTabEntry( "Icon", i18n("Icon"), true, 32);
mTabProp[1] = new CTabEntry( "Device", i18n("Device"), true, 80);
mTabProp[2] = new CTabEntry( "Type", i18n("Type"), true, 50);
mTabProp[3] = new CTabEntry( "Size", i18n("Size"), true, 72);
mTabProp[4] = new CTabEntry( "MountPoint", i18n("Mount Point"), true, 90 );
mTabProp[5] = new CTabEntry( "Free", i18n("Free"), true, 55);
// xgettext:no-c-format
mTabProp[6] = new CTabEntry( "Full%", i18n("Full %"), true, 70);
mTabProp[7] = new CTabEntry( "UsageBar", i18n("Usage"), true, 100);
GUI = !init;
if( GUI )
{
QVBoxLayout *topLayout = new QVBoxLayout( this, 0, 0 );
mList = new CListView( this, "list" );
topLayout->addWidget( mList );
mList->setAllColumnsShowFocus( true );
mList->setFrameStyle( QFrame::WinPanel + QFrame::Sunken );
mList->setShowSortIndicator(true);
connect( mList,
SIGNAL( pressed( QListViewItem *, const QPoint &, int )),
this, SLOT( rightButtonPressed( QListViewItem *, const QPoint &, int )));
connect( mList,
SIGNAL( clicked( QListViewItem *, const QPoint &, int )),
this, SLOT( rightButtonClicked( QListViewItem *, const QPoint &, int )));
connect( mList->header(), SIGNAL(sizeChange(int, int, int)),
this, SLOT(columnSizeChanged(int, int, int)) );
makeColumns();
mIsTopLevel = QString(parent->className()) == "KDFTopLevel" ? true : false;
}
loadSettings();
if( init == true )
{
applySettings();
}
}
KDFWidget::~KDFWidget()
{
}
void KDFWidget::makeColumns( void )
{
uint i;
//
// 1999-11-29 Espen Sand
// This smells like a bad hack but I need to remove the headers
// first. If I don't, the list look like shit afterwards. The iterator
// is just used to prevent an endless loop. With my Qt (1999-11-10 ?)
// I only need as many iterations as there are header items but who knows
// what a new Qt can do!
//
for( i=1000; mList->header()->count() > 0 && i>0; i-- )
{
mList->header()->removeLabel( 0 );
mList->header()->update();
}
for( i=0; i<mTabProp.size(); mList->removeColumn(i++) );
mList->clear();
for( i=0; i<mTabProp.size(); i++ )
{
CTabEntry &e = *mTabProp[i];
if( e.mVisible == true )
mList->addColumn( e.mName, e.mWidth );
else
mList->addColumn( e.mName, 0 ); // zero width makes it invisible
}
}
/******************************************************************/
void KDFWidget::closeEvent(QCloseEvent *)
{
applySettings();
kapp->quit();
}
void KDFWidget::settingsChanged( void )
{
applySettings();
loadSettings();
}
/***************************************************************************
* writes the KConfig
**/
void KDFWidget::applySettings( void )
{
KConfig &config = *kapp->config();
config.setGroup("KDiskFree");
if( GUI )
{
for( uint i=0; i<mTabProp.size(); i++ )
{
CTabEntry &e = *mTabProp[i];
if( e.mVisible == true )
{
e.mWidth = mList->columnWidth(i);
}
config.writeEntry( e.mRes, e.mWidth );
}
}
config.sync();
updateDF();
}
/***************************************************************************
* reads the KConfig
**/
void KDFWidget::loadSettings( void )
{
mStd.updateConfiguration();
if(GUI)
{
KConfig &config = *kapp->config();
config.setGroup("KDiskFree");
for( uint i=0; i<mTabProp.size(); i++ )
{
CTabEntry &e = *mTabProp[i];
e.mWidth = config.readNumEntry( e.mRes, e.mWidth );
}
if (mTabProp[usageCol]->mWidth > 16)
mTabProp[usageCol]->mWidth -= 16;
config.setGroup("KDFConfig");
for( uint i=0; i<mTabProp.size(); i++ )
{
CTabEntry &e = *mTabProp[i];
e.mVisible = config.readBoolEntry( e.mRes, e.mVisible );
}
makeColumns();
setUpdateFrequency( mStd.updateFrequency() );
updateDF();
}
}
/***************************************************************************
* pops up the SettingsBox if the settingsBtn is clicked
**/
void KDFWidget::settingsBtnClicked( void )
{
if( mIsTopLevel == true )
{
if( mOptionDialog == 0 )
{
mOptionDialog = new COptionDialog( this, "options", false );
if( mOptionDialog == 0 ) { return; }
connect( mOptionDialog, SIGNAL(valueChanged()),
this, SLOT(settingsChanged()) );
}
mOptionDialog->show();
}
}
/***************************************************************************
* resets the timer for automatic df-refreshes
**/
void KDFWidget::setUpdateFrequency( int frequency )
{
//
// Kill current timer and restart it if the frequency is
// larger than zero.
//
killTimers();
if( frequency > 0 )
{
startTimer( frequency * 1000 );
}
}
/***************************************************************************
* Update (reread) all disk-dependencies
**/
void KDFWidget::timerEvent(QTimerEvent *)
{
updateDF();
}
/***************************************************************************
* checks fstab & df
**/
void KDFWidget::updateDF( void )
{
//
// We can only do this if the popupmenu is not present
//
if( mPopup == 0 )
{
readingDF = TRUE;
mDiskList.readFSTAB();
mDiskList.readDF();
}
}
/***************************************************************************
* gets the signal when the diskList is complete and up to date
**/
void KDFWidget::updateDFDone( void ){
if (mPopup) //The popup menu is ont he screen... Don't touch the list view...
return;
mList->clear();
int i=0;
CListViewItem *item = 0;
for( DiskEntry *disk=mDiskList.first(); disk!=0; disk=mDiskList.next() )
{
i++;
QString size, percent;
if( disk->kBSize() > 0 )
{
percent = KGlobal::locale()->formatNumber(disk->percentFull(), 1) + '%';
size = disk->prettyKBSize();
}
else
{
percent = i18n("N/A");
size = i18n("N/A");
}
int k=0;
item = new CListViewItem( mList, item );
bool root = disk->mountOptions().find("user",0,false)==-1 ? true : false;
item->setPixmap( k++, mList->icon( disk->iconName(), root ) );
item->setText( k++, disk->deviceName() );
item->setText( k++, disk->fsType() );
item->setText( k++, size );
item->setText( k++, disk->mountPoint() );
item->setText( k++, disk->prettyKBAvail() );
item->setText( k++, percent );
item->setKeys( disk->kBSize(), disk->kBAvail(), disk->percentFull());
}
readingDF = false;
updateDiskBarPixmaps();
mList->sort();
}
/***************************************************************************
* Update display
**/
void KDFWidget::resizeEvent( QResizeEvent * )
{
updateDiskBarPixmaps();
}
/**************************************************************************
* connected with diskList
**/
void KDFWidget::criticallyFull( DiskEntry *disk )
{
if( mStd.popupIfFull() == true )
{
QString msg = i18n("Device [%1] on [%2] is getting critically full!").
arg(disk->deviceName()).arg(disk->mountPoint());
KMessageBox::sorry( this, msg, i18n("Warning"));
}
}
/**************************************************************************
* find correct disk related to list item
**/
DiskEntry *KDFWidget::selectedDisk( QListViewItem *item )
{
if( item == 0 )
{
item = mList->selectedItem();
}
if( item == 0 )
{
return(0);
}
DiskEntry disk(item->text(deviceCol));
disk.setMountPoint(item->text(mntCol));
// I can't get find() to work. The Disks::compareItems(..) is
// never called.
//
//int pos=mDiskList->find(disk);
int pos = -1;
for( u_int i=0; i<mDiskList.count(); i++ )
{
DiskEntry *item = mDiskList.at(i);
int res = disk.deviceName().compare( item->deviceName() );
if( res == 0 )
{
res = disk.mountPoint().compare( item->mountPoint() );
}
if( res == 0 )
{
pos = i;
break;
}
}
return mDiskList.at(pos);
// return(0);
}
void KDFWidget::rightButtonPressed( QListViewItem *item, const QPoint &p, int )
{
if( KContextMenuManager::showOnButtonPress() == true )
{
popupMenu( item, p );
}
}
void KDFWidget::rightButtonClicked( QListViewItem *item, const QPoint &p, int )
{
if( KContextMenuManager::showOnButtonPress() == false )
{
popupMenu( item, p );
}
}
/**************************************************************************
* pops up and asks for mount/umount right-clicked device
**/
void KDFWidget::popupMenu( QListViewItem *item, const QPoint &p )
{
if (mPopup) //The user may even be able to popup another menu while this open is active...
return;
//
// The list update will be disabled as long as this menu is
// visible. Reason: The 'disk' may no longer be valid.
//
mDiskList.setUpdatesDisabled(true);
DiskEntry *disk = selectedDisk( item );
if( disk == 0 )
{
return;
}
mPopup = new KPopupMenu( 0 );
mPopup->insertTitle( disk->mountPoint() );
mPopup->insertItem( i18n("Mount Device"), 0 );
mPopup->insertItem( i18n("Unmount Device"), 1 );
mPopup->insertSeparator();
mPopup->insertItem( i18n("Open in File Manager"), 2 );
mPopup->setItemEnabled( 0, disk->mounted() ? false : true );
mPopup->setItemEnabled( 1, disk->mounted() );
mPopup->setItemEnabled( 2, disk->mounted() );
int position = mPopup->exec( p );
bool openFileManager = false;
if( position == -1 )
{
mDiskList.setUpdatesDisabled(false);
delete mPopup; mPopup = 0;
return;
}
else if( position == 0 || position == 1 )
{
item->setText( sizeCol, i18n("MOUNTING") );
item->setText( freeCol, i18n("MOUNTING") );
item->setPixmap( 0, mList->icon( "mini-clock", false ) );
int val = disk->toggleMount();
if( val != 0 /*== false*/ )
{
KMessageBox::error( this, disk->lastSysError() );
}
else if ( ( mStd.openFileManager() == true)
&& (position == 0) ) //only on mount
{
openFileManager = true;
}
delete item;
mDiskList.deleteAllMountedAt(disk->mountPoint());
}
else if( position == 2 )
{
openFileManager = true;
}
if( openFileManager == true )
{
kdDebug() << "opening filemanager" << endl;
if( mStd.fileManager().isEmpty() == false )
{
QString cmd = mStd.fileManager();
int pos = cmd.find("%m");
if( pos > 0 )
{
cmd = cmd.replace( pos, 2, KProcess::quote(disk->mountPoint()) ) + " &";
}
else
{
cmd += " " + KProcess::quote(disk->mountPoint()) +" &";
}
system( QFile::encodeName(cmd) );
}
}
//Update only here as showing of error message triggers event loop.
mDiskList.setUpdatesDisabled(false);
delete mPopup; mPopup = 0;
if( position != 2 ) // No need to update when just opening the fm.
{
updateDF();
}
}
/**************************************************************************
* recalculates and repaints the pixBars
**/
void KDFWidget::updateDiskBarPixmaps( void )
{
if (mTabProp[usageCol]->mVisible != true)
return;
int size=0, w=0;
for(uint i=0; i<mTabProp.size()-1; i++ )
size += mList->columnWidth(i);
w=mList->width() - size - 4;
if (w<0)
w=0;
mList->setColumnWidth(usageCol, w );
int h = mList->fontMetrics().lineSpacing()-2;
if( h <= 0 )
{
return;
}
int i=0;
for(QListViewItem *it=mList->firstChild(); it!=0;it=it->nextSibling(),i++ )
{
// I can't get find() to work. The Disks::compareItems(..) is
// never called.
//
//int pos=mDiskList->find(disk);
DiskEntry dummy(it->text(deviceCol));
dummy.setMountPoint(it->text(mntCol));
int pos = -1;
for( u_int i=0; i<mDiskList.count(); i++ )
{
DiskEntry *item = mDiskList.at(i);
int res = dummy.deviceName().compare( item->deviceName() );
if( res == 0 )
{
res = dummy.mountPoint().compare( item->mountPoint() );
}
if( res == 0 )
{
pos = i;
break;
}
}
DiskEntry *disk = mDiskList.at(pos);
if( disk == 0 ) { continue; }
if( disk->mounted() == true && disk->percentFull() != -1 )
{
int w = mList->columnWidth(usageCol)-2;
if( w <= 0 ) { continue; }
QPixmap *pix = new QPixmap( w, h );
if( pix == 0 ) { continue; }
pix->fill(white);
QPainter p(pix);
p.setPen(black);
p.drawRect(0,0,w,h);
QColor c;
if ( (disk->iconName().find("cdrom") != -1)
|| (disk->iconName().find("writer") != -1) )
c = gray;
else
c = disk->percentFull() > FULL_PERCENT ? red : darkGreen;
p.setBrush(c );
p.setPen(white);
p.drawRect(1,1,(int)(((float)pix->width()-2)*(disk->percentFull()/100)),
pix->height()-2);
it->setPixmap ( usageCol, *pix );
p.end();
delete pix;
}
}
}
void KDFWidget::columnSizeChanged( int, int, int )
{
if( mTimer == 0 )
{
mTimer = new QTimer( this );
connect( mTimer, SIGNAL(timeout()), this, SLOT(updateDiskBarPixmaps()) );
}
else if( mTimer->isActive() == true )
{
mTimer->stop();
}
mTimer->start( 10, true );
}
void KDFWidget::invokeHelp()
{
kapp->invokeHelp("", "kcontrol/kdf");
}
#include "kdfwidget.moc"
|