summaryrefslogtreecommitdiffstats
path: root/src/locater.cpp
blob: 120ec99bcba12d4012e5ae0603d056ebc7d3d442 (plain)
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
/***************************************************************************
 *   kio-locate: KDE I/O Slave for the locate command                      *
 *                                                                         *
 *   Copyright (C) 2005 by Tobi Vollebregt                                 *
 *   tobivollebregt@gmail.com                                              *
 *                                                                         *
 *   Thanks to Google's Summer Of Code Program!                            *
 *                                                                         *
 *   Copyright (C) 2004 by Armin Straub                                    *
 *   linux@arminstraub.de                                                  *
 *                                                                         *
 *   This program was initially written by Michael Schuerig.               *
 *   Although I have completely rewritten it, most ideas are adopted       *
 *   from his original work.                                               *
 *                                                                         *
 *   Copyright (C) 2002 by Michael Schuerig                                *
 *   michael@schuerig.de                                                   *
 *                                                                         *
 *                                                                         *
 *   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 <tqtextcodec.h>

#include <kdebug.h>
#include <kstandarddirs.h>

#include "locater.h"


Locater::Locater(TQObject *parent, const char *name)
	: TQObject(parent, name), m_process(TQTextCodec::codecForLocale())
{
	DEBUGSTR << "Locater::Locater" << endl;

	connect(&m_process, TQT_SIGNAL(processExited(KProcess*)),
			 this, TQT_SLOT(finished(KProcess*)));
	connect(&m_process, TQT_SIGNAL(readReady(KProcIO*)),
			 this, TQT_SLOT(gotOutput(KProcIO*)));

	setupLocate();
}


Locater::~Locater()
{
	DEBUGSTR << "Locater::~Locater" << endl;
}


void Locater::setupLocate(const TQString& binary, const TQString& additionalArguments)
{
	DEBUGSTR << "Locater::setupLocate(" << binary << ", " << additionalArguments << ")" << endl;

    // Automatically choose the correct binary if not specified.
    if (binary.isEmpty()) {
        if (KStandardDirs::findExe("slocate")) {
            m_binary = "slocate";
        } else if (KStandardDirs::findExe("rlocate")) {
            m_binary = "rlocate";
        } else {
            m_binary = "locate";
        }
        DEBUGSTR << "Using binary:" << m_binary << endl;
    } else {
        m_binary = binary;
    }
	m_additionalArguments = additionalArguments;
    m_binaryExists = KStandardDirs::findExe(m_binary) != TQString();
}


bool Locater::locate(const TQString& pattern, bool ignoreCase, bool regExp)
{
	DEBUGSTR << "Locater::locate(" << pattern << "," << ignoreCase << "," << regExp << ")" << endl;

	m_process.resetAll();
	m_process << m_binary;
	if (!m_additionalArguments.isEmpty()) {
		m_process << m_additionalArguments;
	}
	if (ignoreCase) {
		// m_process << "--ignore-case";
		m_process << "-i";
	}
	if (regExp) {
		m_process << "-r";
	}
	m_process << pattern;

	return m_process.start(KProcess::Block, false);
}

void Locater::stop()
{
	DEBUGSTR << "Locater::stop()" << endl;

	m_process.kill();
	emit finished();
}


void Locater::gotOutput(KProcIO* /*proc*/)
{
	//DEBUGSTR << "Locater::gotOutput" << endl;

	TQStringList items;
	TQString line;

	while (m_process.readln(line) != -1) {
		//DEBUGSTR << "OUTPUT>> '" << line << "'" << endl;

		items << line;
	}

	emit found(items);
}


void Locater::finished(KProcess* /*proc*/)
{
	DEBUGSTR << "Locater::finished" << endl;

	emit finished();
}


#include "locater.moc"