summaryrefslogtreecommitdiffstats
path: root/src/makedlg.cpp
blob: cfd96f091cc9114c83704183f0b5b5da35bfde75 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/***************************************************************************
 *
 * Copyright (C) 2006 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 <tqtextcodec.h>
#include <ktextbrowser.h>
#include <kcombobox.h>
#include <kurlrequester.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include "makedlg.h"
#include "makefrontend.h"
#include "queryview.h"

/** Window flags for call-tree widgets. */
#define MAKE_DLG_W_FLAGS \
	WStyle_Customize | \
	WStyle_NormalBorder | \
	WStyle_Title

/**
 * Class constructor.
 * @param	pParent	The parent widget
 * @param	szName	The widget's name
 */
MakeDlg::MakeDlg(TQWidget* pParent, const char* szName) :
	MakeLayout(pParent, szName, MAKE_DLG_W_FLAGS)
{
	// Don't show the "Function" column
	m_pErrorView->setColumnWidthMode(0, TQListView::Manual);
	m_pErrorView->setColumnWidth(0, 0);
	
	// Create a new make front-end
	m_pMake = new MakeFrontend();
	connect(m_pMake, TQ_SIGNAL(dataReady(FrontendToken*)), this,
		TQ_SLOT(slotShowOutput(FrontendToken*)));
	connect(m_pMake, TQ_SIGNAL(finished(uint)), this, TQ_SLOT(slotFinished(uint)));
	connect(m_pMake,
		TQ_SIGNAL(error(const TQString&, const TQString&, const TQString&)),
		this,
		TQ_SLOT(slotAddError(const TQString&, const TQString&, const TQString&)));
	
	// The Root URL control should browse directories
	m_pRootURL->setMode(KFile::Directory);
	
	// Handle URL links in the browser
	m_pOutputBrowser->setNotifyClick(true);
	connect(m_pOutputBrowser, TQ_SIGNAL(urlClick(const TQString&)), this,
		TQ_SLOT(slotBrowserClicked(const TQString&)));
		
	// Handle selections in the error view
	connect(m_pErrorView, TQ_SIGNAL(lineRequested(const TQString& , uint)), this,
		TQ_SIGNAL(fileRequested(const TQString&, uint)));
		
	// Do not allow duplicates in the command history
	m_pCommandHistory->setDuplicatesEnabled(false);
}

/**
 * Class destructor.
 */
MakeDlg::~ MakeDlg()
{
	delete m_pMake;
}

/**
 * @return	The currently set make command
 */
TQString MakeDlg::getCommand() const
{
	return m_pCommandHistory->currentText();
}

/**
 * @param	sCmd	The new make command to use
 */
void MakeDlg::setCommand(const TQString& sCmd)
{
	m_pCommandHistory->setCurrentText(sCmd);
	m_pCommandHistory->addToHistory(sCmd);
}

/**
 * @return	The directory in which to run the make command
 */
TQString MakeDlg::getDir() const
{
	return m_pRootURL->url();
}

/**
 * @param	sURL	The new root directory to use
 */
void MakeDlg::setDir(const TQString& sURL)
{
	m_pRootURL->setURL(sURL);
}

/**
 * Overrides the default close behaviour.
 * Makes sure that a window is not closed while a make process is running,
 * unless the user explicitly requests it. In this case, the make process
 * is killed.
 * @param	pEvent	The close event descriptor
 */
void MakeDlg::closeEvent(TQCloseEvent* pEvent)
{
	// Check if a process is currently running
	if (m_pMake->isRunning()) {
		// Prompt the user
		switch (KMessageBox::questionYesNoCancel(this, 
			i18n("A make process is running. Would you like to stop it first?"),
			i18n("Close Make Window"))) {
		case KMessageBox::Yes:
			// Stop the process first
			m_pMake->kill();
			break;
				
		case KMessageBox::No:
			// Do nothing
			break;
				
		case KMessageBox::Cancel:
			// Abort closing
			pEvent->ignore();
			return;
		}
	}
	
	TQWidget::closeEvent(pEvent);
}

/**
 * Starts a make process using the user-supplied command.
 * This slot is connected to the clicked() signal of the "Make" button.
 */
void MakeDlg::slotMake()
{
	TQString sCommand;
	
	// Clear the current contents
	m_pOutputBrowser->clear();
	m_pErrorView->clear();
	
	// Run the make command
	sCommand = m_pCommandHistory->currentText();
	if (!m_pMake->run("make", TQStringList::split(" ", sCommand),
		m_pRootURL->url())) {
		KMessageBox::error(this, m_pMake->getRunError());
		return;
	}
		
	// Add the command to the command history
	m_pCommandHistory->addToHistory(sCommand);
		
	// Disbale the make button
	m_pMakeButton->setEnabled(false);
	m_pStopButton->setEnabled(true);
}

/**
 * Terminates the current make process.
 * This slot is connected to the clicked() signal of the stop button.
 */
void MakeDlg::slotStop()
{
	m_pMake->kill();
}

/**
 * Displays the parsed output, as generated by the MakeFrontend object.
 * This slot is connected to the dataReady() signal of the make front-end.
 * @param	pToken	Holds the parsed data
 */
void MakeDlg::slotShowOutput(FrontendToken* pToken)
{
	m_pOutputBrowser->append(pToken->getData());
}

/**
 * Displays the results of the make command.
 * This slot is connected to the finished() signal of the make front-end.
 */
void MakeDlg::slotFinished(uint)
{
	// Add "Success" or "Error" at the end of the output
	if (m_pMake->exitStatus() == 0) {
		m_pOutputBrowser->append("<font color=\"#008000\"><b>Success</b>"
			"</font>");
	}
	else {
		m_pOutputBrowser->append("<font color=\"#ff0000\"><b>Error</b></font>");
	}
	
	// Re-enable the "Make" button
	m_pMakeButton->setEnabled(true);
	m_pStopButton->setEnabled(false);
}

/**
 * Emits the fileRequested() signal when a browser link is clicked.
 * This slot is connected to the urlClick() signal of the browser.
 * @param	sURL	The requested URL
 */
void MakeDlg::slotBrowserClicked(const TQString& sURL)
{
	TQString sFile;
	TQString sLine;
	
	// Exract the file name and the line number from the URL
	sFile = sURL.section('&', 0, 0);
	sLine = sURL.section('&', 1, 1);
	
	// Add root path for relative paths
	if (!sFile.startsWith("/"))
		sFile = m_pRootURL->url() + "/" + sFile;
	
	// Emit the signal
	emit fileRequested(sFile, sLine.toUInt());
}

/**
 * Show an error/warning on a deidicated list.
 * This slot is connected to the error() signal of the make front-end.
 * @param	sFile	The file name containing the error/warning
 * @param	sLine	The line number
 * @param	sText	An explanation of the error
 */
void MakeDlg::slotAddError(const TQString& sFile, const TQString& sLine,
	const TQString& sText)
{
	m_pErrorView->addRecord("", sFile, sLine, sText);
}

#include "makedlg.moc"