📄 locate.cpp
字号:
/*************************************************************************** locate.cpp - description ------------------- copyright : (C) 2004 by Csaba Karai e-mail : krusader@users.sourceforge.net web site : http://krusader.sourceforge.net --------------------------------------------------------------------------- Description *************************************************************************** A db dD d8888b. db db .d8888. .d8b. d8888b. d88888b d8888b. 88 ,8P' 88 `8D 88 88 88' YP d8' `8b 88 `8D 88' 88 `8D 88,8P 88oobY' 88 88 `8bo. 88ooo88 88 88 88ooooo 88oobY' 88`8b 88`8b 88 88 `Y8b. 88~~~88 88 88 88~~~~~ 88`8b 88 `88. 88 `88. 88b d88 db 8D 88 88 88 .8D 88. 88 `88. YP YD 88 YD ~Y8888P' `8888Y' YP YP Y8888D' Y88888P 88 YD S o u r c e F i l e *************************************************************************** * * * 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. * * * ***************************************************************************/#include "locate.h"#include "../krusader.h"#include "../krusaderview.h"#include "../Panel/listpanel.h"#include "../Panel/panelfunc.h"#include "../defaults.h"#include "../krservices.h"#include "../VFS/vfs.h"#include "../VFS/virt_vfs.h"#include "../KViewer/krviewer.h"#include "../panelmanager.h"#include <klocale.h>#include <qhbox.h>#include <qlabel.h>#include <qlayout.h>#include <qfontmetrics.h>#include <kmessagebox.h>#include <kpopupmenu.h>#include <qcursor.h>#include <qeventloop.h>#include <kfinddialog.h>#include <kinputdialog.h>#include <qregexp.h>#include <qdir.h>#include <qclipboard.h>#include <kurldrag.h>#include <../kicons.h>// these are the values that will exist in the menu#define VIEW_ID 90#define EDIT_ID 91#define FIND_ID 92#define FIND_NEXT_ID 93#define FIND_PREV_ID 94#define COPY_SELECTED_TO_CLIPBOARD 95//////////////////////////////////////////////////////////class LocateListView : public KListView{public: LocateListView( QWidget * parent, const char * name = 0 ) : KListView( parent, name ) { } void startDrag() { KURL::List urls; QListViewItem * item = firstChild(); while( item ) { if( item->isSelected() ) urls.push_back( vfs::fromPathOrURL( item->text( 0 ) ) ); item = item->nextSibling(); } if( urls.count() == 0 ) return; KURLDrag *d = new KURLDrag(urls, this); d->setPixmap( FL_LOADICON( "file" ), QPoint( -7, 0 ) ); d->dragCopy(); }};KProcess * LocateDlg::updateProcess = 0;LocateDlg * LocateDlg::LocateDialog = 0;LocateDlg::LocateDlg() : KDialogBase(0,0,false,"Locate", KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 | KDialogBase::Close, KDialogBase::User3, false, i18n("Stop"), i18n("Update DB"), i18n("Locate") ), isFeedToListBox( false ){ QWidget *widget=new QWidget(this, "locateMainWidget"); QGridLayout *grid = new QGridLayout( widget ); grid->setSpacing( 6 ); grid->setMargin( 11 ); setPlainCaption( i18n( "Krusader::Locate" ) ); QHBox *hbox = new QHBox( widget, "locateHBox" ); QLabel *label = new QLabel( i18n( "Search for:" ), hbox, "locateLabel" ); locateSearchFor = new KHistoryCombo( false, hbox, "locateSearchFor" ); label->setBuddy( locateSearchFor ); krConfig->setGroup("Locate"); QStringList list = krConfig->readListEntry("Search For"); locateSearchFor->setMaxCount(25); // remember 25 items locateSearchFor->setHistoryItems(list); locateSearchFor->setEditable( true ); locateSearchFor->setDuplicatesEnabled( false ); locateSearchFor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); locateSearchFor->lineEdit()->setFocus(); grid->addWidget( hbox, 0, 0 ); QHBox *hbox2 = new QHBox( widget, "locateHBox" ); QSpacerItem* spacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); hbox2->layout()->addItem( spacer ); dontSearchInPath = new QCheckBox( i18n( "Don't search in path" ), hbox2, "dontSearchInPath" ); dontSearchInPath->setChecked( krConfig->readBoolEntry("Dont Search In Path") ); existingFiles = new QCheckBox( i18n( "Show only the existing files" ), hbox2, "existingFiles" ); existingFiles->setChecked( krConfig->readBoolEntry("Existing Files") ); caseSensitive = new QCheckBox( i18n( "Case Sensitive" ), hbox2, "caseSensitive" ); caseSensitive->setChecked( krConfig->readBoolEntry("Case Sensitive") ); grid->addWidget( hbox2, 1, 0 ); QFrame *line1 = new QFrame( widget, "locateLine1" ); line1->setFrameStyle( QFrame::HLine | QFrame::Sunken ); grid->addWidget( line1, 2, 0 ); resultList=new LocateListView( widget ); // create the main container krConfig->setGroup("Look&Feel"); resultList->setFont(krConfig->readFontEntry("Filelist Font",_FilelistFont)); resultList->setAllColumnsShowFocus(true); resultList->setVScrollBarMode(QScrollView::Auto); resultList->setHScrollBarMode(QScrollView::Auto); resultList->setShowSortIndicator(false); resultList->setSorting(-1); resultList->setSelectionMode( QListView::Extended ); resultList->addColumn( i18n("Results"), QFontMetrics(resultList->font()).width("W") * 60 ); resultList->setColumnWidthMode(0,QListView::Maximum); connect( resultList,SIGNAL(rightButtonPressed(QListViewItem *, const QPoint &, int)), this, SLOT(slotRightClick(QListViewItem *))); connect( resultList,SIGNAL(doubleClicked(QListViewItem *)), this, SLOT(slotDoubleClick(QListViewItem *))); connect( resultList,SIGNAL(returnPressed(QListViewItem *)), this, SLOT(slotDoubleClick(QListViewItem *))); grid->addWidget( resultList, 3, 0 ); QFrame *line2 = new QFrame( widget, "locateLine2" ); line2->setFrameStyle( QFrame::HLine | QFrame::Sunken ); grid->addWidget( line2, 4, 0 ); enableButton( KDialogBase::User1, false ); /* disable the stop button */ if( updateProcess ) { if( updateProcess->isRunning() ) { connect( updateProcess, SIGNAL(processExited(KProcess *)), this, SLOT(updateFinished())); enableButton( KDialogBase::User2, false ); } else updateFinished(); } setMainWidget(widget); show(); LocateDialog = this;}void LocateDlg::slotUser1() /* The stop / feed to listbox button */{ if( isFeedToListBox ) feedToListBox(); else stopping = true;}void LocateDlg::slotUser2() /* The Update DB button */{ if( !updateProcess ) { krConfig->setGroup("Locate"); updateProcess = new KProcess(); *updateProcess << KrServices::fullPathName( "updatedb" ); *updateProcess << KrServices::separateArgs( krConfig->readEntry( "UpdateDB Arguments", "" ) ); connect( updateProcess, SIGNAL(processExited(KProcess *)), this, SLOT(updateFinished())); updateProcess->start(KProcess::NotifyOnExit); enableButton( KDialogBase::User2, false ); }}void LocateDlg::updateFinished(){ delete updateProcess; updateProcess = 0; enableButton( KDialogBase::User2, true );}void LocateDlg::slotUser3() /* The locate button */{ locateSearchFor->addToHistory(locateSearchFor->currentText()); QStringList list = locateSearchFor->historyItems(); krConfig->setGroup("Locate"); krConfig->writeEntry("Search For", list); krConfig->writeEntry("Dont Search In Path", dontSearchPath = dontSearchInPath->isChecked() ); krConfig->writeEntry("Existing Files", onlyExist = existingFiles->isChecked() ); krConfig->writeEntry("Case Sensitive", isCs = caseSensitive->isChecked() ); if( !KrServices::cmdExist( "locate" ) ) { KMessageBox::error(0, i18n("Can't start 'locate'! Check the 'Dependencies' page in konfigurator.")); return; } resultList->clear(); lastItem = 0; remaining = ""; enableButton( KDialogBase::User3, false ); /* disable the locate button */ enableButton( KDialogBase::User1, true ); /* enable the stop button */ setButtonText( KDialogBase::User1, i18n( "Stop" ) ); /* the button behaves as stop */ isFeedToListBox = false; resultList->setFocus(); qApp->processEvents(); stopping = false; KProcess locateProc; connect( &locateProc, SIGNAL( receivedStdout(KProcess *, char *, int) ), this, SLOT( processStdout(KProcess *, char *, int) ) ); connect( &locateProc, SIGNAL( receivedStderr(KProcess *, char *, int) ), this, SLOT( processStderr(KProcess *, char *, int) ) ); locateProc << KrServices::fullPathName( "locate" ); if( !isCs ) locateProc << "-i"; locateProc << (pattern = locateSearchFor->currentText()); if( !pattern.startsWith( "*" ) ) pattern = "*" + pattern; if( !pattern.endsWith( "*" ) ) pattern = pattern + "*"; collectedErr = ""; bool result = !locateProc.start( KProcess::Block, KProcess::AllOutput ); if( !collectedErr.isEmpty() && ( !locateProc.normalExit() || locateProc.exitStatus() ) ) { KMessageBox::error( krApp, i18n( "Locate produced the following error message:\n\n" ) + collectedErr ); }else if ( result ) { KMessageBox::error( krApp, i18n( "Error during the start of 'locate' process!" ) ); } enableButton( KDialogBase::User3, true ); /* enable the locate button */ if( resultList->childCount() == 0 ) { locateSearchFor->setFocus(); enableButton( KDialogBase::User1, false ); /* disable the stop button */ isFeedToListBox = false; }else{ setButtonText( KDialogBase::User1, i18n("Feed to listbox") ); /* feed to listbox */ isFeedToListBox = true; }}void LocateDlg::processStdout(KProcess *proc, char *buffer, int length){ char *buf = new char[ length+1 ]; memcpy( buf, buffer, length ); buf[ length ] = 0; remaining += QString::fromLocal8Bit( buf ); delete []buf; QStringList list = QStringList::split("\n", remaining ); int items = list.size(); for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { if( --items == 0 && buffer[length-1] != '\n' ) remaining = *it; else { if( dontSearchPath ) { QRegExp regExp( pattern, isCs, true );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -