⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskusage.cpp

📁 LINUX 下, 以 QT/KDE 写的档案管理员
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  createStatus();}int DiskUsage::del( File *file, bool calcPercents, int depth ){  int deleteNr = 0;  if( file == root )    return 0;  krConfig->setGroup( "General" );  bool trash = krConfig->readBoolEntry( "Move To Trash", _MoveToTrash );  KURL url = vfs::fromPathOrURL( file->fullPath() );  if( calcPercents )  {    // now ask the user if he want to delete:    krConfig->setGroup( "Advanced" );    if ( krConfig->readBoolEntry( "Confirm Delete", _ConfirmDelete ) ) {      QString s, b;      if ( trash && url.isLocalFile() ) {        s = i18n( "Do you really want to move this item to the trash?" );        b = i18n( "&Trash" );      } else {        s = i18n( "Do you really want to delete this item?" );        b = i18n( "&Delete" );      }      QStringList name;      name.append( file->fullPath() );      // show message      // note: i'm using continue and not yes/no because the yes/no has cancel as default button      if ( KMessageBox::warningContinueCancelList( krApp, s, name, i18n( "Warning" ), b ) != KMessageBox::Continue )        return 0;    }    emit status( i18n( "Deleting %1..." ).arg( file->name() ) );  }  if( file == currentDirectory )    dirUp();  if( file->isDir() )  {    Directory *dir = dynamic_cast<Directory *>( file );    Iterator<File> it;    while( ( it = dir->iterator() ) != dir->end() )      deleteNr += del( *it, false, depth + 1 );    QString path;    for( const Directory *d = (Directory*)file; d != root && d && d->parent() != 0; d = d->parent() )    {      if( !path.isEmpty() )        path = "/" + path;      path = d->name() + path;    }    contentMap.remove( path );  }  emit deleted( file );  deleteNr++;  QGuardedPtr<KIO::Job> job;  if( trash )  {#if KDE_IS_VERSION(3,4,0)    job = KIO::trash( url, true );#else    job = new KIO::CopyJob( url,KGlobalSettings::trashPath(),KIO::CopyJob::Move,false,true );#endif    connect(job,SIGNAL(result(KIO::Job*)),krApp,SLOT(changeTrashIcon()));  }  else  {    job = new KIO::DeleteJob( vfs::fromPathOrURL( file->fullPath() ), false, false);  }  deleting = true;    // during qApp->processEvent strange things can occur  grabMouse();        // that's why we disable the mouse and keyboard events  grabKeyboard();  while( !job.isNull() )    qApp->processEvents();  releaseMouse();  releaseKeyboard();   deleting = false;  ((Directory *)(file->parent()))->remove( file );  delete file;  if( depth == 0 )    createStatus();  if( calcPercents )  {    calculateSizes( root, true );    calculatePercents( true );    createStatus();    emit enteringDirectory( currentDirectory );  }  if( depth == 0 && deleteNr != 0 )    emit deleteFinished();  return deleteNr;}void * DiskUsage::getProperty( File *item, QString key ){  Properties * props = propertyMap.find( item );  if( props == 0 )    return 0;  return props->find( key );}void DiskUsage::addProperty( File *item, QString key, void * prop ){  Properties * props = propertyMap.find( item );  if( props == 0 )  {    props = new Properties();    propertyMap.insert( item, props );  }  props->insert( key, prop );}void DiskUsage::removeProperty( File *item, QString key ){  Properties * props = propertyMap.find( item );  if( props == 0 )    return;  props->remove( key );  if( props->count() == 0 )    propertyMap.remove( item );}void DiskUsage::createStatus(){  Directory *dirEntry = currentDirectory;  if( dirEntry == 0 )    return;  KURL url = baseURL;  if( dirEntry != root )      url.addPath( dirEntry->directory() );  emit status( i18n( "Current directory:%1,  Total size:%2,  Own size:%3" )               .arg( vfs::pathOrURL( url, -1 ) )               .arg( " "+KRpermHandler::parseSize( dirEntry->size() ) )               .arg( " "+KRpermHandler::parseSize( dirEntry->ownSize() ) ) );}void DiskUsage::changeDirectory( Directory *dir ){  currentDirectory = dir;  currentSize = dir->size();  calculatePercents( true, dir );  createStatus();  emit enteringDirectory( dir );}Directory* DiskUsage::getCurrentDir(){  return currentDirectory;}void DiskUsage::rightClickMenu( File *fileItem, KPopupMenu *addPopup, QString addPopupName ){  KPopupMenu popup( this );  popup.insertTitle( i18n("Disk Usage"));  if( fileItem != 0 )  {    popup.insertItem(  i18n("Delete"),          DELETE_ID);    popup.setAccel( Key_Delete, DELETE_ID );    popup.insertItem(  i18n("Exclude"),         EXCLUDE_ID);    popup.setAccel( CTRL + Key_E, EXCLUDE_ID );    popup.insertSeparator();  }  popup.insertItem(  i18n("Up one directory"),  PARENT_DIR_ID);  popup.setAccel( SHIFT + Key_Up, PARENT_DIR_ID );  popup.insertItem(  i18n("New search"),        NEW_SEARCH_ID);  popup.setAccel( CTRL + Key_N, NEW_SEARCH_ID );  popup.insertItem(  i18n("Refresh"),           REFRESH_ID);  popup.setAccel( CTRL + Key_R, REFRESH_ID );  popup.insertItem(  i18n("Include all"),       INCLUDE_ALL_ID);  popup.setAccel( CTRL + Key_I, INCLUDE_ALL_ID );  popup.insertItem(  i18n("Step into"),         STEP_INTO_ID);  popup.setAccel( SHIFT + Key_Down, STEP_INTO_ID );  popup.insertSeparator();  if( addPopup != 0 )  {    popup.insertItem( QPixmap(), addPopup, ADDITIONAL_POPUP_ID );    popup.changeItem( ADDITIONAL_POPUP_ID, addPopupName );  }  KPopupMenu viewPopup;  viewPopup.insertItem(i18n("Lines"),      LINES_VIEW_ID);  viewPopup.setAccel( CTRL + Key_L, LINES_VIEW_ID );  viewPopup.insertItem(i18n("Detailed"),   DETAILED_VIEW_ID);  viewPopup.setAccel( CTRL + Key_D, DETAILED_VIEW_ID );  viewPopup.insertItem(i18n("Filelight"),  FILELIGHT_VIEW_ID);  viewPopup.setAccel( CTRL + Key_F, FILELIGHT_VIEW_ID );  viewPopup.insertSeparator();  viewPopup.insertItem(i18n("Next"),       NEXT_VIEW_ID);  viewPopup.setAccel( SHIFT + Key_Right, NEXT_VIEW_ID );  viewPopup.insertItem(i18n("Previous"),   PREVIOUS_VIEW_ID);  viewPopup.setAccel( SHIFT + Key_Left, PREVIOUS_VIEW_ID );  popup.insertItem( QPixmap(), &viewPopup, VIEW_POPUP_ID );  popup.changeItem( VIEW_POPUP_ID, i18n( "View" ) );  int result=popup.exec(QCursor::pos());  executeAction( result, fileItem );}void DiskUsage::executeAction( int action, File * fileItem ){  // check out the user's option  switch ( action )  {  case DELETE_ID:    if( fileItem )      del( fileItem );    break;  case EXCLUDE_ID:    if( fileItem )      exclude( fileItem );    break;  case PARENT_DIR_ID:    dirUp();    break;  case NEW_SEARCH_ID:    emit newSearch();    break;  case REFRESH_ID:    load( baseURL );    break;  case INCLUDE_ALL_ID:    includeAll();    break;  case STEP_INTO_ID:    {      QString uri;      if( fileItem && fileItem->isDir() )        uri = fileItem->fullPath();      else        uri = currentDirectory->fullPath();      ACTIVE_FUNC->openUrl(vfs::fromPathOrURL( uri ));    }    break;  case LINES_VIEW_ID:    setView( VIEW_LINES );    break;  case DETAILED_VIEW_ID:    setView( VIEW_DETAILED );    break;  case FILELIGHT_VIEW_ID:    setView( VIEW_FILELIGHT );    break;  case NEXT_VIEW_ID:    setView( ( activeView + 1 ) % 3 );    break;  case PREVIOUS_VIEW_ID:    setView( ( activeView + 2 ) % 3 );    break;  }  visibleWidget()->setFocus();}void DiskUsage::keyPressEvent( QKeyEvent *e ){  if( activeView != VIEW_LOADER )  {    switch ( e->key() )    {    case Key_E:      if( e->state() == ControlButton )      {        executeAction( EXCLUDE_ID, getCurrentFile() );        return;      }    case Key_D:      if( e->state() == ControlButton )      {        executeAction( DETAILED_VIEW_ID );        return;      }    case Key_F:      if( e->state() == ControlButton )      {        executeAction( FILELIGHT_VIEW_ID );        return;      }    case Key_I:      if( e->state() == ControlButton )      {        executeAction( INCLUDE_ALL_ID );        return;      }      break;    case Key_L:      if( e->state() == ControlButton )      {        executeAction( LINES_VIEW_ID );        return;      }    case Key_N:      if( e->state() == ControlButton )      {        executeAction( NEW_SEARCH_ID );        return;      }      break;    case Key_R:      if( e->state() == ControlButton )      {        executeAction( REFRESH_ID );        return;      }      break;    case Key_Up:      if( e->state() == ShiftButton )      {        executeAction( PARENT_DIR_ID );        return;      }      break;    case Key_Down:      if( e->state() == ShiftButton )      {        executeAction( STEP_INTO_ID );        return;      }      break;    case Key_Left:      if( e->state() == ShiftButton )      {        executeAction( PREVIOUS_VIEW_ID );        return;      }      break;    case Key_Right:      if( e->state() == ShiftButton )      {        executeAction( NEXT_VIEW_ID );        return;      }      break;    case Key_Delete:      if( !e->state() )      {        executeAction( DELETE_ID, getCurrentFile() );        return;      }    case Key_Plus:      if( activeView == VIEW_FILELIGHT )      {        filelightView->zoomIn();        return;      }      break;    case Key_Minus:      if( activeView == VIEW_FILELIGHT )      {        filelightView->zoomOut();        return;      }      break;    }  }  QWidgetStack::keyPressEvent( e );}QPixmap DiskUsage::getIcon( QString mime ){  QPixmap icon;  if ( !QPixmapCache::find( mime, icon ) )  {    // get the icon.    if ( mime == "Broken Link !" )      icon = FL_LOADICON( "file_broken" );    else      icon = FL_LOADICON( KMimeType::mimeType( mime ) ->icon( QString::null, true ) );    // insert it into the cache    QPixmapCache::insert( mime, icon );  }  return icon;}int DiskUsage::calculatePercents( bool emitSig, Directory *dirEntry, int depth ){  int changeNr = 0;  if( dirEntry == 0 )    dirEntry = root;  for( Iterator<File> it = dirEntry->iterator(); it != dirEntry->end(); ++it )  {    File *item = *it;    if( !item->isExcluded() )    {      int newPerc;      if( dirEntry->size() == 0 && item->size() == 0 )        newPerc = 0;      else if( dirEntry->size() == 0 )        newPerc = -1;      else        newPerc = (int)((double)item->size() / (double)currentSize * 10000. + 0.5);      int oldPerc = item->intPercent();      item->setPercent( newPerc );      if( emitSig && newPerc != oldPerc ) {        emit changed( item );        changeNr++;      }      if( item->isDir() )        changeNr += calculatePercents( emitSig, dynamic_cast<Directory *>( item ), depth + 1 );    }  }  if( depth == 0 && changeNr != 0 )    emit changeFinished();  return changeNr;}QString DiskUsage::getToolTip( File *item ){  KMimeType::Ptr mimePtr = KMimeType::mimeType( item->mime() );  QString mime = mimePtr->comment();  time_t tma = item->time();  struct tm* t=localtime((time_t *)&tma);  QDateTime tmp(QDate(t->tm_year+1900, t->tm_mon+1, t->tm_mday), QTime(t->tm_hour, t->tm_min));  QString date = KGlobal::locale()->formatDateTime(tmp);  QString str = "<qt><h5><table><tr><td>" + i18n( "Name:" ) +  "</td><td>" + item->name() + "</td></tr>"+                "<tr><td>" + i18n( "Type:" ) +  "</td><td>" + mime + "</td></tr>"+                "<tr><td>" + i18n( "Size:" ) +  "</td><td>" + KRpermHandler::parseSize( item->size() ) + "</td></tr>";  if( item->isDir() )    str +=      "<tr><td>" + i18n( "Own size:" ) +  "</td><td>" + KRpermHandler::parseSize( item->ownSize() ) + "</td></tr>";  str +=        "<tr><td>" + i18n( "Last modified:" ) +  "</td><td>" + date + "</td></tr>"+                "<tr><td>" + i18n( "Permissions:" ) +  "</td><td>" + item->perm() + "</td></tr>"+                "<tr><td>" + i18n( "Owner:" ) +  "</td><td>" + item->owner() + " - " + item->group() + "</td></tr>"+                "</table></h5></qt>";  str.replace( " ", "&nbsp;" );  return str;}void DiskUsage::setView( int view ){  switch( view )  {  case VIEW_LINES:    raiseWidget( lineView );    break;  case VIEW_DETAILED:    raiseWidget( listView );    break;  case VIEW_FILELIGHT:    raiseWidget( filelightView );    break;  case VIEW_LOADER:    raiseWidget( loaderView );    break;  }  visibleWidget()->setFocus();  emit viewChanged( activeView = view );}File * DiskUsage::getCurrentFile(){  File * file = 0;  switch( activeView )  {  case VIEW_LINES:    file = lineView->getCurrentFile();    break;  case VIEW_DETAILED:    file = listView->getCurrentFile();    break;  case VIEW_FILELIGHT:    file = filelightView->getCurrentFile();    break;  }  return file;}bool DiskUsage::event( QEvent * e ){  if( deleting ) {                       // if we are deleting, disable the mouse and    switch( e->type() ) {                // keyboard events    case QEvent::MouseButtonPress:    case QEvent::MouseButtonRelease:    case QEvent::MouseButtonDblClick:    case QEvent::MouseMove:    case QEvent::KeyPress:    case QEvent::KeyRelease:      return true;    default:      break;    }  }  if ( e->type() == QEvent::AccelOverride )  {    QKeyEvent* ke = (QKeyEvent*) e;    if ( ke->state() == NoButton || ke->state() == Keypad )    {      switch ( ke->key() )      {        case Key_Delete:        case Key_Plus:        case Key_Minus:          ke->accept();          break;      }    }else if( ke->state() == ShiftButton )    {      switch ( ke->key() )      {        case Key_Left:        case Key_Right:        case Key_Up:        case Key_Down:          ke->accept();          break;      }    }else if ( ke->state() & ControlButton )    {      switch ( ke->key() )      {        case Key_D:        case Key_E:        case Key_F:        case Key_I:        case Key_L:        case Key_N:        case Key_R:          ke->accept();          break;      }    }  }  return QWidgetStack::event( e );}#include "diskusage.moc"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -