summaryrefslogtreecommitdiffstats
path: root/src/queryviewdriver.cpp
blob: 59ad92a07d994577bd46702dd70b1f98f6e528c9 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
 *
 * Copyright (C) 2005 Elad Lahav (elad_lahav@users.sourceforge.net)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include <tdelocale.h>
#include "queryviewdriver.h"
#include "queryview.h"

/**
 * Class constructor.
 * Creates a driver that adds new records as root items in the given view.
 * @param	pView	The view to which this driver should add records
 * @param	pParent	The parent object of the driver
 * @param	szName	The name of the object
 */
QueryViewDriver::QueryViewDriver(QueryView* pView, TQObject* pParent, 
	const char* szName) : TQObject(pParent, szName),
	m_pView(pView),
	m_pItem(NULL),
	m_progress(pView),
	m_bRunning(false)
{
	m_pCscope = new CscopeFrontend();	
		
	// Add records to the page when Cscope outputs them
	connect(m_pCscope, TQ_SIGNAL(dataReady(FrontendToken*)), this,
		TQ_SLOT(slotDataReady(FrontendToken*)));
		
	// Report progress information
	connect(m_pCscope, TQ_SIGNAL(progress(int, int)), this,
		TQ_SLOT(slotProgress(int, int)));

	// Perform tasks when the query process terminates
	connect(m_pCscope, TQ_SIGNAL(finished(uint)), this,
		TQ_SLOT(slotFinished(uint)));
	
	connect(m_pView, TQ_SIGNAL(destroyed()), this, TQ_SLOT(slotViewClosed()));
}

/**
 * Class destructor.
 */
QueryViewDriver::~QueryViewDriver()
{
	delete m_pCscope;
}

/**
 * Executes a query.
 * @param	nType	The type of the query (@see CscopeFrontend)
 * @param	sText	The query's text
 * @param	bCase		true for case-sensitive queries, false otherwise
 * @param	pItem	If non-null, represents an view item passed back to
 *					the view in a call to addRecord()
 */
void QueryViewDriver::query(uint nType, const TQString& sText, bool bCase,
	TQListViewItem* pItem)
{
	m_pItem = pItem;
	
	// Make sure sorting is disabled while entries are added
	m_pView->setSorting(100);
		
	// Execute the query
	m_pCscope->query(nType, sText, bCase);
	m_bRunning = true;
	m_pView->setEnabled(false);
	m_pView->setUpdatesEnabled(false);
}

/**
 * Adds a query entry to the view.
 * Called by a CscopeFrontend object, when a new entry was received in its
 * whole from the Cscope back-end process.
 * @param	pToken	The first token in the entry
 */
void QueryViewDriver::slotDataReady(FrontendToken* pToken)
{
	TQString sFile, sFunc, sLine, sText;
	
	// Get the file name
	sFile = pToken->getData();
	pToken = pToken->getNext();

	// Get the function name
	sFunc = pToken->getData();
	pToken = pToken->getNext();

	// Get the line number
	sLine = pToken->getData();
	if (!sLine.toInt()) {
		// Line number could not be 0!
		// means that function name was empty
		sLine = sFunc;
		sFunc = "<global>";
	}
	else {
		pToken = pToken->getNext();
	}
	
	// Get the line's text
	sText = pToken->getData();
	pToken = pToken->getNext();

	// Add a new item at the end of the list
	m_pView->addRecord(sFunc, sFile, sLine, sText, m_pItem);
}

/**
 * Handles a finished query event, reported by the Cscope frontend object.
 * If no resutls are available, a proper message is displayed. If only one 
 * record was generated by Cscope, it is automatically selected for viewing.
 * @param	nRecords	The number of records the query has generated
 */
void QueryViewDriver::slotFinished(uint nRecords)
{
	// The query is no longer running
	m_bRunning = false;
	m_pView->setEnabled(true);
	m_pView->setUpdatesEnabled(true);
	m_pView->triggerUpdate();
	
	// Destroy the progress bar
	m_progress.finished();

	// Let owner widget decide what to do based on the number of records
	m_pView->queryFinished(nRecords, m_pItem);
}

/**
 * Displays search progress information.
 * This slot is connected to the progress() signal emitted by a
 * CscopeFrontend object.
 * @param	nFiles	The number of files scanned
 * @param	nTotal	The total number of files in the project
 */
void QueryViewDriver::slotProgress(int nFiles, int nTotal)
{
	// A progress report is available, instruct the owner object to show the
	// view
	if (nTotal > 1)
		m_pView->queryProgress();
		
	// Set the progress bar
	m_progress.setProgress(nFiles, nTotal);
}

/**
 * Called when the owner view is destroyed.
 */
void QueryViewDriver::slotViewClosed()
{
	m_pView = NULL;
	m_pCscope->kill();
}

#include "queryviewdriver.moc"