projectmanager.cpp

来自「This a source insight software in Linux.」· C++ 代码 · 共 745 行 · 第 1/2 页

CPP
745
字号
/*************************************************************************** * * 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 <unistd.h>#include <kmessagebox.h>#include <klocale.h>#include "projectmanager.h"#include "kscopeconfig.h"#define CONFIG_VER 2#define DEF_IS_KERNEL		false#define DEF_INV_INDEX		true#define DEF_AC_MIN_CHARS	3#define DEF_AC_DELAY		500#define DEF_AC_MAX_ENTRIES	100/** * Class constructor. */ProjectManager::ProjectManager() :	m_pFiles(NULL),	m_pConf(NULL),	m_bOpen(false),	m_bTempProj(false),	m_nAutoRebuildTime(-1){}/** * Class destructor. */ProjectManager::~ProjectManager(){	close();}/** * Creates a project's directory, and associates this directory with the * current object. This directory is created under the given path, and using * the project's name (which, thus, has to be a legal file name).  * Note: this function attempts to create a new directory, so the given path * and name must not lead to an existing one. * @param	sName		The project's name * @param	sPath		The parent directory under which to create the  *						project's directory * @param	opt			A structure containing project options * @return	true if successful, false otherwise */bool ProjectManager::create(const QString& sName, const QString& sPath, 	const Options& opt){	QFile* pFilesFile;	KConfig* pConf;		// If the project is open, close it first	if (m_bOpen)		close();	// Make sure the directory doesn't exist	QDir dir(sPath);	if (dir.exists(sName)) {		KMessageBox::error(0, i18n("Cannot create a project inside an "			"existing directory"));		return false;	}	// Try to create the projcet's directory	if (!dir.mkdir(sName, false) || !dir.cd(sName, false)) {		KMessageBox::error(0, i18n("Failed to create the project's "			"directory"));		return false;	}		// Prepare the project's files	initConfig(dir.absPath(), &pFilesFile, &pConf);	// Write the configuration file version	pConf->setGroup("");	pConf->writeEntry("Version", CONFIG_VER);		// Write project properties in the configuration file	pConf->setGroup("Project");	pConf->writeEntry("Name", sName);	writeOptions(pConf, opt);		// Flush the config file data, so the project is created even if KScope	// crashes...	pConf->sync();	delete pConf;	// Create the "files" file	if (!pFilesFile->open(IO_WriteOnly)) {		delete pFilesFile;		KMessageBox::error(0, i18n("Failed to open the \"files\" file"));		return false;	}	pFilesFile->close();	delete pFilesFile;		return true;}/** * Opens a project and makes it the current one. * @param	sPath	The directory containing the project's files * @return	true if successful, false otherwise */bool ProjectManager::open(const QString& sPath){	QString sConfFile;	Options opt;		// Associate the object with the project directory	m_dir.setPath(sPath);	if (!m_dir.exists()) {		KMessageBox::error(0, i18n("Project directory does not exist"));		return false;	}	// Open the configuration files	initConfig(sPath, &m_pFiles, &m_pConf);	// Verify the configuration file's version is compatible	m_pConf->setGroup("");	if (m_pConf->readUnsignedNumEntry("Version", 0) != CONFIG_VER) {		KMessageBox::error(0, i18n("Your project is not compatible with this "		"version of KScope.\nPlease re-create the project."));		return false;	}		// Get the project name	m_pConf->setGroup("Project");	m_sName = m_pConf->readEntry("Name");	if (m_sName == QString::null) {		KMessageBox::error(0, i18n("Cannot read project name"));		return false;	}			// Indicate that a project has been opened	m_bOpen = true;	m_bTempProj = false;	// Create the argument list for invoking Cscope	getOptions(opt);	if (opt.bKernel)		m_slArgs << "-k";	if (opt.bInvIndex)		m_slArgs << "-q";		// Get the auto-rebuild	time value	m_nAutoRebuildTime = opt.nAutoRebuildTime;		// Add to the list of recently opened projects	Config().addRecentProject(sPath);		return true;}/** * Opens a Cscope.out file as a temporary project. * @param	sFilePath	The full path of the Cscope.out file * @return	true if successful, false otherwise */bool ProjectManager::openCscopeOut(const QString& sFilePath){	QFileInfo fi(sFilePath);		// Make sure the file exists, and that is is a cross-reference file	if (!fi.exists() || !isCscopeOut(fi.absFilePath()))		return false;			// Set the project's directory	m_dir = fi.dirPath(true);		// Set the name of the project to be the full path of the file	m_sName = fi.absFilePath();	// Indicate that a project has been opened	m_bOpen = true;	m_bTempProj = true;		return true;}/** * Performs clean-up on the project's variables, and detaches the associated * directory. */void ProjectManager::close(){	if (!m_bTempProj) {		m_dir = QDir();		delete m_pFiles;		m_pFiles = NULL;		delete m_pConf;		m_pConf = NULL;	}		m_slArgs = QStringList();	m_slSymHistory.clear();	m_nAutoRebuildTime = -1;	m_bOpen = false;}/** * Adds a set of files to the current project. * @param	slFiles	A list of file names to add * @return	The total number of files added to the project */int ProjectManager::addFiles(const QStringList& slFiles){	QStringList::const_iterator itr;	int nFiles = 0;	// Cannot add files if no project is open, or the project is temporary	if (!m_bOpen || m_bTempProj)		return 0;	// Open the source files file, to which the file paths will be added	if (!m_pFiles->open(IO_WriteOnly | IO_Append))		return 0;	// Add each file to the project	QTextStream str(m_pFiles);	for (itr = slFiles.begin(); itr != slFiles.end(); ++itr) {		str << *itr << "\n";		nFiles++;	}	m_pFiles->close();	emit filesAdded(slFiles);		return nFiles;}/** * Fills a list object with all files in the project. * List items are created by reading and parsing all file name entries from * the project's 'cscope.files' file. * Note that the file may contain option lines, beginning with a dash. These * should be ignored. * @param	pList	Pointer to the object to fill */void ProjectManager::fillList(FileListTarget* pList){	QString sFilePath;		// Must have an open persistent project	if (!m_bOpen || m_bTempProj)		return;		// Open the 'cscope.files' file	if (!m_pFiles->open(IO_ReadOnly))		return;	// Read all file names from the file	QTextStream str(m_pFiles);	while ((sFilePath = str.readLine()) != QString::null) {		// Skip option lines		if (sFilePath.at(0) == '-')			continue;		// Set the new list item		pList->addItem(sFilePath);	}	m_pFiles->close();}/** * Writes all file entries in a list view widget to the project's * 'cscope.files' file (replacing current file contents.) * @param	pList	Pointer to the object from which to take the new entries */void ProjectManager::writeList(FileListSource* pList){	QString sFilePath;		// Must have an open persistent project	if (!m_bOpen || m_bTempProj)		return;		// Open the 'cscope.files' file	if (!m_pFiles->open(IO_WriteOnly))		return;	QTextStream str(m_pFiles);	// Write all file names	if (pList->firstItem(sFilePath)) {		do {			str << sFilePath << "\n";		} while (pList->nextItem(sFilePath));	}	m_pFiles->close();	emit fileListChanged();}/** * Determines whether the project includes any files. * Reads the 'cscope.files' file and looks for any file names in it. If none * is present, then the project is considered empty. * Note that the file may contain option lines, beginning with a dash. These * should be ignored. * @return	true if no files are included in the project, false otherwise */bool ProjectManager::isEmpty(){	QString sPath, sFileName;	bool bResult = true;		// Must have an active project	if (!m_bOpen)		return true;	// Assume there are some files if using only a cscope.out file	if (m_bTempProj)		return false;		// Open the 'cscope.files' file	if (!m_pFiles->open(IO_ReadOnly))		return true;	// Find at least one file name entry in the file	QTextStream str(m_pFiles);	while ((sPath = str.readLine()) != QString::null) {		if (sPath.at(0) != '-') {			bResult = false;			break;		}	}	m_pFiles->close();	return bResult;}/** * Returns a semi-colon separated list of the file types included in the * current project. */QString ProjectManager::getFileTypes() const{	QString sTypes;		// Must have an open persistent project	if (!m_bOpen || m_bTempProj)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?