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

📄 mediabutton.cpp

📁 LINUX 下, 以 QT/KDE 写的档案管理员
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			else typeName="floppy";		}	else if (-1!=mp->mountPoint().find("floppy",0,FALSE)) typeName="floppy";		else if (-1!=mp->mountPoint().find("zip",0,FALSE)) typeName="zip";	else if (-1!=mp->mountType().find("nfs",0,FALSE)) typeName="nfs";	else if (-1!=mp->mountType().find("smb",0,FALSE)) typeName="smb";	else if (-1!=mp->mountedFrom().find("//",0,FALSE)) typeName="smb";	else typeName="hdd";		return typeName;}void MediaButton::slotPopupActivated( int elem ) {	if( !quasiMounted[ elem ] && mimes[ elem ].endsWith( "_unmounted" ) ) {		mount( elem );		waitingForMount = elem;		maxMountWait = 20;		newTabAfterMount = false;		mountCheckerTimer.start( 1000, true );		return;	}	emit openUrl( urls[ elem ] );}void MediaButton::gettingSpaceData(const QString &mountPoint, unsigned long kBSize, unsigned long, unsigned long ) {	KURL mediaURL = KURL::fromPathOrURL( mountPoint );		KIO::filesize_t size = kBSize;	size *= 1024;	QString sizeText = KIO::convertSize( size );		for( unsigned i=0; i != urls.size(); i++ ) {		if( mediaURL.equals( urls[ i ], true ) ) {			if( kBSize == 0 ) { // if df gives 0, it means the device is quasy umounted				QString mime = mimes[ i ];				if( mimes[ i ].endsWith( "_mounted" ) ) {					quasiMounted[ i ] = true;					mimes[ i ] = mimes[ i ].replace( "_mounted", "_unmounted" );				}								QPixmap pixmap = FL_LOADICON( KMimeType::mimeType( mimes[ i ] ) ->icon( QString::null, true ) );				popupMenu->changeItem( i, pixmap, popupMenu->text( i ) );			}			else if( mimes[ i ].contains( "hdd_" ) )				popupMenu->changeItem( i, sizeText + " " + popupMenu->text( i ).stripWhiteSpace() );			return;		}	}}void MediaButton::openPopup() {	QPopupMenu * pP = popup();	if ( pP ) {		popup() ->exec( mapToGlobal( QPoint( 0, height() ) ) );	}}void MediaButton::addMountPoint( KMountPoint * mp, bool isMounted ) {	QString mountString = isMounted ? "_mounted" : "_unmounted";	if( mp->mountPoint() == "/dev/swap" || 		mp->mountPoint() == "/dev/pts"  ||		mp->mountPoint().startsWith( "/sys/kernel" ) ||		mp->mountPoint().find( "/proc" ) == 0 )		return;	if( mp->mountType() == "swap" ||		mp->mountType() == "sysfs" ||		mp->mountType() == "tmpfs" ||		mp->mountType() == "kernfs" ||		mp->mountType() == "usbfs" ||		mp->mountType() == "unknown" ||		mp->mountType() == "none" ||		mp->mountType() == "sunrpc" )		return;	if( mp->mountedFrom() == "none" ||		mp->mountedFrom() == "tmpfs" ||		mp->mountedFrom().find( "shm" )  != -1 )		return;		int overwrite = -1;	KURL mountURL = KURL::fromPathOrURL( mp->mountPoint() );		for( unsigned i=0; i != urls.size(); i++ ) 		if( urls[ i ].equals( mountURL, true ) ) {			overwrite = i;			break;		}		QString name;	QString type = detectType( mp );		/* WORKAROUND CODE START */	/* add spaces to avoid widget resize in gettingSpaceData,	   which is buggy in QT when transparency is set */	QString extSpc = ( isMounted && type == "hdd" ) ? extraSpaces : "";	/* WORKAROUND CODE END */		#if KDE_IS_VERSION(3,4,0)	QString mimeBase = "media/";#else	QString mimeBase = "kdedevice/";#endif		QString mime = mimeBase + type + mountString;		if( type == "hdd" )		name =  i18n( "Hard Disk" ) ;	else if( type == "cdrom" )		name = i18n( "CD-ROM" );	else if( type == "cdwriter" )		name = i18n( "CD Recorder" );	else if( type == "dvdwriter" ) {		mime = mimeBase + "cdwriter" + mountString;		name = i18n( "DVD Recorder" );	}	else if( type == "dvd" )		name = i18n( "DVD" );	else if( type == "smb" )		name = i18n( "Remote Share" );	else if( type == "nfs" )		name = i18n( "Remote Share" );	else if( type == "floppy" )		name = i18n( "Floppy" );	else if( type == "floppy5" )		name = i18n( "Floppy" );	else if( type == "zip" )		name = i18n( "Zip Disk" );	else {		mime = mimeBase + "hdd" + mountString;		name = i18n( "Unknown" );	}		if( isMounted ) {		KDiskFreeSp *sp = KDiskFreeSp::findUsageInfo( mp->mountPoint() );		connect( sp, SIGNAL( foundMountPoint( const QString &, unsigned long, unsigned long, unsigned long ) ),		         this, SLOT( gettingSpaceData( const QString&, unsigned long, unsigned long, unsigned long ) ) );	}		QPixmap pixmap = FL_LOADICON( KMimeType::mimeType( mime ) ->icon( QString::null, true ) );		if( overwrite == -1 ) {		int index = popupMenu->count();		urls.append( KURL::fromPathOrURL( mp->mountPoint() ) );		mimes.append( mime );		mediaUrls.append( KURL() );		quasiMounted.append( false );		popupMenu->insertItem( pixmap, name + "  [" + mp->mountPoint() + "]" + extSpc, index, index );	}	else {		mimes[ overwrite ] = mime;		popupMenu->changeItem( overwrite, pixmap, name + "  [" + mp->mountPoint() + "]" + extSpc );	}}bool MediaButton::eventFilter( QObject *o, QEvent *e ) {	if( o == popupMenu ) {		if( e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease ) {			QMouseEvent *m = (QMouseEvent *)e;			if( m->button() == RightButton ) {				if( e->type() == QEvent::MouseButtonPress ) {					int id = popupMenu->idAt( m->pos() );					if( id != -1 )						rightClickMenu( id );				} 				m->accept();				return true;			}		}	}	return false;}void MediaButton::rightClickMenu( int index ) {	if( rightMenu )		rightMenu->close();		QString mime = mimes[ index ];	bool ejectable = mime.contains( "dvd_" ) || mime.contains( "dvdwriter_" ) || mime.contains( "cdrom_" ) || mime.contains( "cdwriter_" );	bool mounted = mime.contains( "_mounted" );		QPopupMenu * myMenu = rightMenu = new QPopupMenu( popupMenu );	myMenu->insertItem( i18n( "Open" ), 1 );	myMenu->insertItem( i18n( "Open in a new tab" ), 2 );	myMenu->insertSeparator();	if( !mounted )		myMenu->insertItem( i18n( "Mount" ), 3 );	else		myMenu->insertItem( i18n( "Unmount" ), 4 );	if( ejectable )		myMenu->insertItem( i18n( "Eject" ), 5 );	int result = myMenu->exec( QCursor::pos() );	delete myMenu;	if( rightMenu == myMenu )		rightMenu = 0;	else		return;		switch ( result ) {	case 1:	case 2:		popupMenu->close();		if( mounted || quasiMounted[ index ] ) {			if( result == 1 )				emit openUrl( urls[ index ] );			else				SLOTS->newTab( urls[ index ] );		} else {			mount( index ); // mount first, when mounted open the tab			waitingForMount = index;			maxMountWait = 20;			newTabAfterMount = ( result == 2 );			mountCheckerTimer.start( 1000, true );		}		break;	case 3:		mount( index );		break;	case 4:		umount( index );		break;	case 5:		eject( index );		break;	default:		break;	}}bool MediaButton::mount( int index ) {	if ( (unsigned)index < mimes.count() ) {		if( !mediaUrls[ index ].isEmpty() ) {			KProcess proc;			proc << KrServices::fullPathName( "kio_media_mounthelper" ) << "-m" << mediaUrls[ index ].url();			proc.start( KProcess::DontCare );		} else {			krMtMan.mount( urls[ index ].path(), false );		}	}	return false;}bool MediaButton::umount( int index ) {	if ( (unsigned)index < mimes.count() ) {		if( !mediaUrls[ index ].isEmpty() ) {			KProcess proc;			proc << KrServices::fullPathName( "kio_media_mounthelper" ) << "-u" << mediaUrls[ index ].url();			proc.start( KProcess::DontCare );		} else {			krMtMan.unmount( urls[ index ].path(), false );		}	}	return false;}bool MediaButton::eject( int index ) {	if ( (unsigned)index < mimes.count() ) {		if( !mediaUrls[ index ].isEmpty() ) {			KProcess proc;			proc << KrServices::fullPathName( "kio_media_mounthelper" ) << "-e" << mediaUrls[ index ].url();			proc.start( KProcess::DontCare );		} else {			krMtMan.eject( urls[ index ].path() );		}	}	return false;}void MediaButton::slotTimeout() {	if( isHidden() && ( waitingForMount < 0 ) )		return;		KMountPoint::List mountList = KMountPoint::currentMountPoints();		for( unsigned index = 0; index < urls.count(); index++ ) {		bool mounted = false;				QString text = popupMenu->text( index );				if( mediaUrls[ index ].isEmpty() ) {			for (KMountPoint::List::iterator it = mountList.begin(); it != mountList.end(); ++it)				if( (*it)->mountPoint() == urls[ index ].path() ) {					mounted = true;;					break;				}		} else {			KURL uri = getLocalPath( mediaUrls[ index ], &mountList );			if(  uri.isLocalFile() ) {				urls[ index ] = uri;				mounted = true;								if( !text.contains( uri.path() ) ) 				{					if( text.endsWith( "]" ) )					{						int ndx = text.findRev( "  [" );						if( ndx >0 )							text.truncate( ndx );					}										text += "  [" + uri.path() + "]";				}			}			else			{				if( text.endsWith( "]" ) )				{					int ndx = text.findRev( "  [" );					if( ndx >0 )						text.truncate( ndx );				}			}		}				if( quasiMounted[ index ] ) // mounted but not listed with DF			mounted = false;				if( mimes[ index ].contains( "_mounted" ) && !mounted )			mimes[ index ] = mimes[ index ].replace( "_mounted", "_unmounted" );		if( mimes[ index ].contains( "_unmounted" ) && mounted )			mimes[ index ] = mimes[ index ].replace( "_unmounted", "_mounted" );				QPixmap pixmap = FL_LOADICON( KMimeType::mimeType( mimes[ index ] ) ->icon( QString::null, true ) );		popupMenu->changeItem( index, pixmap, text );				if( ((int)index == waitingForMount) && mounted ) {			waitingForMount = -1;			if( newTabAfterMount )				SLOTS->newTab( urls[ index ] );			else				emit openUrl( urls[ index ] );		}	}		if( waitingForMount >= 0 ) {         // maximum wait for mounting expired ?		if( --maxMountWait < 0 ) {			waitingForMount = -1;			return;		}	}		mountCheckerTimer.start( 1000, true );}#include "mediabutton.moc"

⌨️ 快捷键说明

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