示例#1
0
void LuaAVConsole :: addScript(const char *name, const char *filename, luaav_state *S) {
	int idx = mScriptModel->getList().size();
	mScriptModel->insertRow(idx, QModelIndex());

	QModelIndex index = mScriptModel->index(idx, 0, QModelIndex());
	mScriptModel->setData(index, QString(name), Qt::EditRole);

	resizeTable(mSplitter->sizes().at(0), 0);

	// table buttons for script actions
	char bname[16];
	QPushButton *closeButton = new QPushButton();
	closeButton->setIcon(QIcon(QPixmap("/usr/share/luaav/x.png")));
	closeButton->setFlat(true);
	closeButton->setAutoFillBackground(true);
	sprintf(bname, "close:%d", mScriptsLoaded);
	closeButton->setObjectName(QString(bname));
	connect(closeButton, SIGNAL(clicked()), this, SLOT(fileAction()));
	mTableView->setIndexWidget(mScriptModel->index(idx, 1), closeButton);

	QPushButton *reloadButton = new QPushButton();
	reloadButton->setIcon(QIcon(QPixmap("/usr/share/luaav/reload.png")));
	reloadButton->setFlat(true);
	reloadButton->setAutoFillBackground(true);
	sprintf(bname, "reload:%d", mScriptsLoaded);	
	reloadButton->setObjectName(QString(bname));
	connect(reloadButton, SIGNAL(clicked()), this, SLOT(fileAction()));
	mTableView->setIndexWidget(mScriptModel->index(idx, 2), reloadButton);

	QPushButton *editButton = new QPushButton();
	editButton->setIcon(QIcon(QPixmap("/usr/share/luaav/eye.png")));
	editButton->setFlat(true);
	editButton->setAutoFillBackground(true);
	sprintf(bname, "edit:%d", mScriptsLoaded);
	editButton->setObjectName(QString(bname));
	connect(editButton, SIGNAL(clicked()), this, SLOT(fileAction()));
	mTableView->setIndexWidget(mScriptModel->index(idx, 3), editButton);

	mScripts.push_back(new ScriptData(mScriptsLoaded, name, filename, S));
	mScriptsLoaded++;
}
/*
 * Walk down all the directories under the specified 
 * location, and do something (something specified
 * by the fileAction and dirAction function pointers).
 *
 * Unfortunately, while nftw(3) could replace this and reduce 
 * code size a bit, nftw() wasn't supported before GNU libc 2.1, 
 * and so isn't sufficiently portable to take over since glibc2.1
 * is so stinking huge.
 */
int recursive_action(const char *fileName,
					int recurse, int followLinks, int depthFirst,
					int (*fileAction) (const char *fileName,
									   struct stat * statbuf,
									   void* userData),
					int (*dirAction) (const char *fileName,
									  struct stat * statbuf,
									  void* userData),
					void* userData)
{
	int status;
	struct stat statbuf;
	struct dirent *next;

	if (followLinks)
		status = stat(fileName, &statbuf);
	else
		status = lstat(fileName, &statbuf);

	if (status < 0) {
#ifdef DEBUG_RECURS_ACTION
		fprintf(stderr,
				"status=%d followLinks=%d TRUE=%d\n",
				status, followLinks, TRUE);
#endif
		perror_msg("%s", fileName);
		return FALSE;
	}

	if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
		if (fileAction == NULL)
			return TRUE;
		else
			return fileAction(fileName, &statbuf, userData);
	}

	if (! recurse) {
		if (S_ISDIR(statbuf.st_mode)) {
			if (dirAction != NULL)
				return (dirAction(fileName, &statbuf, userData));
			else
				return TRUE;
		}
	}

	if (S_ISDIR(statbuf.st_mode)) {
		DIR *dir;

		if (dirAction != NULL && ! depthFirst) {
			status = dirAction(fileName, &statbuf, userData);
			if (! status) {
				perror_msg("%s", fileName);
				return FALSE;
			} else if (status == SKIP)
				return TRUE;
		}
		dir = opendir(fileName);
		if (!dir) {
			perror_msg("%s", fileName);
			return FALSE;
		}
		status = TRUE;
		while ((next = readdir(dir)) != NULL) {
			char *nextFile;

			if ((strcmp(next->d_name, "..") == 0)
					|| (strcmp(next->d_name, ".") == 0)) {
				continue;
			}
			nextFile = concat_path_file(fileName, next->d_name);
			if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
						fileAction, dirAction, userData)) {
				status = FALSE;
			}
			free(nextFile);
		}
		closedir(dir);
		if (dirAction != NULL && depthFirst) {
			if (! dirAction(fileName, &statbuf, userData)) {
				perror_msg("%s", fileName);
				return FALSE;
			}
		}
		if (! status)
			return FALSE;
	} else {
		if (fileAction == NULL)
			return TRUE;
		else
			return fileAction(fileName, &statbuf, userData);
	}
	return TRUE;
}