00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include <qdir.h>
00013
#include <qlayout.h>
00014
#include <qregexp.h>
00015
#include <qpainter.h>
00016
#include <kdialogbase.h>
00017
#include <klocale.h>
00018
#include <kprocess.h>
00019
#include <kparts/part.h>
00020
#include <ktexteditor/selectioninterface.h>
00021
using namespace KTextEditor;
00022
00023
#include "kdevcore.h"
00024
#include "kdevproject.h"
00025
#include "kdevmainwindow.h"
00026
#include "kdevpartcontroller.h"
00027
00028
#include "grepdlg.h"
00029
#include "grepviewpart.h"
00030
#include "grepviewwidget.h"
00031
00032
00033 class GrepListBoxItem :
public ProcessListBoxItem
00034 {
00035
public:
00036
GrepListBoxItem(
const QString &
fileName,
const QString &
lineNumber,
const QString &
text,
bool showFilename);
00037 QString filename()
00038 {
return fileName; }
00039 int linenumber()
00040 {
return lineNumber.toInt(); }
00041
virtual bool isCustomItem();
00042
00043
private:
00044
virtual void paint(
QPainter *p);
00045 QString fileName, lineNumber,
text;
00046 bool show;
00047 };
00048
00049
00050 GrepListBoxItem::GrepListBoxItem(
const QString &fileName,
const QString &lineNumber,
const QString &text,
bool showFilename)
00051 :
ProcessListBoxItem(
QString::null, Normal),
00052 fileName(fileName), lineNumber(lineNumber),
text(
text.stripWhiteSpace()),
00053 show(showFilename)
00054 {}
00055
00056
00057 bool GrepListBoxItem::isCustomItem()
00058 {
00059
return true;
00060 }
00061
00062
00063 void GrepListBoxItem::paint(
QPainter *p)
00064 {
00065
QFontMetrics fm = p->fontMetrics();
00066
QString stx =
lineNumber +
": ";
00067
int y = fm.ascent()+fm.leading()/2;
00068
int x = 3;
00069
if (
show)
00070 {
00071 p->setPen(Qt::darkGreen);
00072 p->drawText(x, y,
fileName);
00073 x += fm.width(
fileName);
00074 }
00075
else {
00076 p->setPen(Qt::black);
00077
QFont font1(p->font());
00078
QFont font2(font1);
00079 font2.setBold(
true);
00080 p->setFont(font2);
00081 p->drawText(x, y, stx);
00082 p->setFont(font1);
00083 x += fm.width(stx);
00084
00085 p->setPen(Qt::blue);
00086 p->drawText(x, y,
text);
00087 }
00088 }
00089
00090
00091 GrepViewWidget::GrepViewWidget(
GrepViewPart *part)
00092 :
ProcessWidget(0, "grep widget")
00093 , m_matchCount(0)
00094 {
00095
grepdlg =
new GrepDialog(
this,
"grep widget");
00096 connect(
grepdlg, SIGNAL(searchClicked()),
00097
this, SLOT(
searchActivated()) );
00098 connect(
this, SIGNAL(clicked(
QListBoxItem*)),
00099
this, SLOT(
slotExecuted(
QListBoxItem*)) );
00100 connect(
this, SIGNAL(returnPressed(
QListBoxItem*)),
00101
this, SLOT(
slotExecuted(
QListBoxItem*)) );
00102
00103
m_part = part;
00104 }
00105
00106
00107 GrepViewWidget::~GrepViewWidget()
00108 {}
00109
00110
00111 void GrepViewWidget::showDialog()
00112 {
00113
00114
KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(
m_part->
partController()->
activePart());
00115
if (ro_part)
00116 {
00117
SelectionInterface *selectIface = dynamic_cast<SelectionInterface*>(ro_part);
00118
if(selectIface && selectIface->
hasSelection())
00119 {
00120
QString selText = selectIface->
selection();
00121
if(!selText.contains(
'\n'))
00122 {
00123
grepdlg->
setPattern(selText);
00124 }
00125 }
00126 }
00127
grepdlg->
show();
00128 }
00129
00130
00131 static QString escape(
const QString &str)
00132 {
00133
QString escaped(
"[]{}()\\^$?.+-*");
00134
QString res;
00135
00136
for (uint i=0; i < str.length(); ++i) {
00137
if (escaped.find(str[i]) != -1)
00138 res +=
"\\";
00139 res += str[i];
00140 }
00141
00142
return res;
00143 }
00144
00145
00146 void GrepViewWidget::showDialogWithPattern(
QString pattern)
00147 {
00148
00149
00150
int len = pattern.length();
00151
if (
len > 0 && pattern[0] ==
'\n')
00152 {
00153 pattern.remove(0, 1);
00154
len--;
00155 }
00156
if (
len > 0 && pattern[
len-1] ==
'\n')
00157 pattern.truncate(
len-1);
00158
grepdlg->
setPattern( pattern );
00159
grepdlg->
show();
00160 }
00161
00162
00163 void GrepViewWidget::searchActivated()
00164 {
00165
m_matchCount = 0;
00166
_lastfilename =
"";
00167
00168
QString files;
00169
00170
QStringList filelist = QStringList::split(
",",
grepdlg->
filesString());
00171
if (!filelist.isEmpty())
00172 {
00173 QStringList::Iterator it(filelist.begin());
00174 files = KShellProcess::quote(*it);
00175 ++it;
00176
for (; it != filelist.end(); ++it)
00177 files +=
" -o -name " + KShellProcess::quote(*it);
00178 }
00179
00180
QString pattern =
grepdlg->
templateString();
00181
00182 pattern.replace(
QRegExp(
"%s"),
escape(
grepdlg->
patternString() ) );
00183 pattern.replace(
QRegExp(
"'"),
"'\\''");
00184
00185
QString filepattern =
"find ";
00186 filepattern += KShellProcess::quote(
grepdlg->
directoryString());
00187
if (!
grepdlg->
recursiveFlag())
00188 filepattern +=
" -maxdepth 1";
00189 filepattern +=
" \\( -name ";
00190 filepattern += files;
00191 filepattern +=
" \\) -print -follow";
00192
00193
QString command = filepattern +
" " ;
00194
if (
grepdlg->
ignoreSCMDirsFlag()) {
00195 command +=
"| grep -v \"SCCS/\" ";
00196 command +=
"| grep -v \"CVS/\" ";
00197 }
00198
00199
00200 command +=
"| sed \"s/ /\\\\\\ /g\" ";
00201
00202 command +=
"| xargs " ;
00203
00204
#ifndef USE_SOLARIS
00205
command +=
"egrep -H -n -e ";
00206
#else
00207
00208
00209 command +=
"egrep -n -e ";
00210
#endif
00211
command += KShellProcess::quote(pattern);
00212 startJob(
"", command);
00213
00214
m_part->
mainWindow()->
raiseView(
this);
00215
m_part->
core()->
running(
m_part,
true);
00216 }
00217
00218
00219 void GrepViewWidget::childFinished(
bool normal,
int status)
00220 {
00221
00222
00223
00224
00225
00226
00227
if (status == 123 && numRows() > 1)
00228 status = 0;
00229
00230 insertItem(
new ProcessListBoxItem(i18n(
"*** %n match found. ***",
"*** %n matches found. ***",
m_matchCount), ProcessListBoxItem::Diagnostic));
00231
maybeScrollToBottom();
00232
00233 ProcessWidget::childFinished(normal, status);
00234
m_part->
core()->
running(
m_part,
false);
00235 }
00236
00237
00238 void GrepViewWidget::slotExecuted(
QListBoxItem* item)
00239 {
00240
ProcessListBoxItem *i = static_cast<ProcessListBoxItem*>(item);
00241
if (!i || !i->
isCustomItem())
00242
return;
00243
00244
GrepListBoxItem *gi = static_cast<GrepListBoxItem*>(i);
00245
m_part->
partController()->
editDocument(
KURL( gi->
filename() ), gi->
linenumber()-1 );
00246
00247 }
00248
00249
00250 void GrepViewWidget::insertStdoutLine(
const QString &line)
00251 {
00252
int pos;
00253
QString filename, linenumber, rest;
00254
00255
QString str = line;
00256
if ( (pos = str.find(
':')) != -1)
00257 {
00258 filename = str.left(pos);
00259 str.remove( 0, pos+1 );
00260
if ( ( pos = str.find(
':') ) != -1)
00261 {
00262 linenumber = str.left(pos);
00263 str.remove( 0, pos+1 );
00264
00265
00266
00267
if (
_lastfilename != filename )
00268 {
00269
_lastfilename = filename;
00270 insertItem(
new GrepListBoxItem(filename,
"0", str,
true));
00271 insertItem(
new GrepListBoxItem(filename, linenumber, str,
false));
00272 }
00273
else
00274 {
00275 insertItem(
new GrepListBoxItem(filename, linenumber, str,
false));
00276 }
00277
maybeScrollToBottom();
00278 }
00279
m_matchCount++;
00280 }
00281 }
00282
00283
00284 void GrepViewWidget::projectChanged(
KDevProject *project)
00285 {
00286
QString dir = project? project->
projectDirectory() : QDir::homeDirPath();
00287
grepdlg->
setDirectory(dir);
00288 }
00289
00290
#include "grepviewwidget.moc"