Ejemplo n.º 1
0
/*
 * Safely remove a file
 */
static bool mixp_remove_file(const QString &filename)
{
	if(!QFileInfo(filename).exists() || !QFileInfo(filename).isFile())
	{
		return true;
	}
	else
	{
		if(!QFile::remove(filename))
		{
			static const DWORD attrMask = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
			const DWORD attributes = GetFileAttributesW(QWCHAR(filename));
			if(attributes & attrMask)
			{
				SetFileAttributesW(QWCHAR(filename), FILE_ATTRIBUTE_NORMAL);
			}
			if(!QFile::remove(filename))
			{
				qWarning("Could not delete \"%s\"", filename.toLatin1().constData());
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return true;
		}
	}
}
Ejemplo n.º 2
0
/*
 * Write registry value
 */
bool mixp_reg_value_write(int rootKey, const QString &keyName, const QString &valueName, const QString &value)
{
	bool success = false; HKEY hKey = NULL;
	if(RegCreateKeyEx(mixp_reg_root(rootKey), QWCHAR(keyName), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
	{
		if(RegSetValueEx(hKey, valueName.isEmpty() ? NULL : QWCHAR(valueName), 0, REG_SZ, reinterpret_cast<const BYTE*>(value.utf16()), (value.length() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS)
		{
			success = true;
		}
		CloseHandle(hKey);
	}
	return success;
}
Ejemplo n.º 3
0
/*
 * Read registry value
 */
bool mixp_reg_value_read(int rootKey, const QString &keyName, const QString &valueName, quint32 &value)
{
	bool success = false; HKEY hKey = NULL;
	if(RegOpenKeyEx(mixp_reg_root(rootKey), QWCHAR(keyName), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
	{
		DWORD size = sizeof(quint32), type = -1;
		if(RegQueryValueEx(hKey, valueName.isEmpty() ? NULL : QWCHAR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS)
		{
			success = (type == REG_DWORD);
		}
		CloseHandle(hKey);
	}
	return success;
}
Ejemplo n.º 4
0
LockedFile::LockedFile(const QString &filePath)
{
	m_fileHandle = NULL;
	QFileInfo existingFile(filePath);
	existingFile.setCaching(false);
	
	//Make sure the file exists, before we try to lock it
	if(!existingFile.exists())
	{
		char error_msg[256];
		strcpy_s(error_msg, 256, QString("File '%1' does not exist!").arg(existingFile.fileName()).toLatin1().constData());
		throw error_msg;
	}
	
	//Remember file path
	m_filePath = existingFile.canonicalFilePath();

	//Now lock the file
	for(int i = 0; i < 64; i++)
	{
		m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
		if((m_fileHandle != NULL) && (m_fileHandle != INVALID_HANDLE_VALUE)) break;
		if(!i) qWarning("Failed to lock file on first attemp, retrying...");
		Sleep(100);
	}

	//Locked successfully?
	if((m_fileHandle == NULL) || (m_fileHandle == INVALID_HANDLE_VALUE))
	{
		THROW(QString("File '%1' could not be locked!").arg(existingFile.fileName()).toLatin1().constData());
	}
}
Ejemplo n.º 5
0
QIcon QFileIconProviderEx::icon(const QFileInfo &info) const
{
	if(info.isFile())
	{
		return m_emptyIcon;
	}
	else if(info.isRoot())
	{
		switch(GetDriveType(QWCHAR(QDir::toNativeSeparators(info.absoluteFilePath()))))
		{
		case DRIVE_CDROM:
			return m_cdromIcon;
			break;
		case DRIVE_REMOVABLE:
			return m_floppyIcon;
			break;
		case DRIVE_REMOTE:
			return m_networkIcon;
			break;
		default:
			return m_driveIcon;
			break;
		}
	}
	else
	{
		const QString filePath = info.filePath();
		if(m_homeDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_homeIcon;
		}
		else if(m_desktopDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_desktopIcon;
		}
		else if(m_musicDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_musicIcon;
		}
		else if(m_moviesDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_moviesIcon;
		}
		else if(m_picturesDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_picturesIcon;
		}
		else if(m_installDir.compare(filePath, Qt::CaseInsensitive) == 0)
		{
			return m_heartIcon;
		}
		else
		{
			return  m_folderIcon;
		}
	}
}
Ejemplo n.º 6
0
bool QFileSystemModelEx::hasSubfolders(const QString &path)
{
	if(s_findFirstFileExInfoLevel == INT_MAX)
	{
		const lamexp_os_version_t &osVersionNo = lamexp_get_os_version();
		s_findFirstFileExInfoLevel = (osVersionNo >= lamexp_winver_win70) ? FindExInfoBasic : FindExInfoStandard;
	}

	WIN32_FIND_DATAW findData;
	bool bChildren = false;

	HANDLE h = FindFirstFileEx(QWCHAR(QDir::toNativeSeparators(path + "/*")), ((FINDEX_INFO_LEVELS)s_findFirstFileExInfoLevel), &findData, FindExSearchLimitToDirectories, NULL, 0);

	if(h != INVALID_HANDLE_VALUE)
	{
		if(NO_DOT_OR_DOTDOT(findData.cFileName))
		{
			bChildren = IS_DIR(findData.dwFileAttributes);
		}
		while((!bChildren) && FindNextFile(h, &findData))
		{
			if(NO_DOT_OR_DOTDOT(findData.cFileName))
			{
				bChildren = IS_DIR(findData.dwFileAttributes);
			}
		}
		FindClose(h);
	}
	else
	{
		DWORD err = GetLastError();
		if((err == ERROR_NOT_SUPPORTED) || (err == ERROR_INVALID_PARAMETER))
		{
			qWarning("FindFirstFileEx failed with error code #%u", err);
		}
	}

	return bChildren;
}
Ejemplo n.º 7
0
/*
 * Natural Order String Comparison - the 'lessThan' helper function *with* case folding
 */
static bool lamexp_natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
{
	return (strnatcasecmp(QWCHAR(str1), QWCHAR(str2)) < 0);
}
Ejemplo n.º 8
0
/*
 * Helper functions
 */
bool AbstractEncoder::isUnicode(const QString &original)
{
    QString asLatin1 = QString::fromLatin1(original.toLatin1().constData());
    return (wcscmp(QWCHAR(original), QWCHAR(asLatin1)) != 0);
}
Ejemplo n.º 9
0
/*
 * Delete registry key
 */
bool mixp_reg_key_delete(int rootKey, const QString &keyName)
{
	return (RegDeleteTree(mixp_reg_root(rootKey), QWCHAR(keyName)) == ERROR_SUCCESS);
}
Ejemplo n.º 10
0
LockedFile::LockedFile(const QString &resourcePath, const QString &outPath, const QByteArray &expectedHash)
{
	m_fileHandle = NULL;
	QResource resource(resourcePath);
	
	//Make sure the resource is valid
	if(!resource.isValid())
	{
		THROW(QString("Resource '%1' is invalid!").arg(QFileInfo(resourcePath).absoluteFilePath().replace(QRegExp("^:/"), QString())).toUtf8().constData());
	}

	QFile outFile(outPath);
	m_filePath = QFileInfo(outFile).absoluteFilePath();
	
	//Open output file
	for(int i = 0; i < 64; i++)
	{
		if(outFile.open(QIODevice::WriteOnly)) break;
		if(!i) qWarning("Failed to open file on first attemp, retrying...");
		Sleep(100);
	}
	
	//Write data to file
	if(outFile.isOpen() && outFile.isWritable())
	{
		if(outFile.write(reinterpret_cast<const char*>(resource.data()), resource.size()) != resource.size())
		{
			QFile::remove(QFileInfo(outFile).canonicalFilePath());
			THROW(QString("File '%1' could not be written!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
		}
		outFile.close();
	}
	else
	{
		THROW(QString("File '%1' could not be created!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
	}

	//Now lock the file!
	for(int i = 0; i < 64; i++)
	{
		m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(m_filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
		if((m_fileHandle != NULL) && (m_fileHandle != INVALID_HANDLE_VALUE)) break;
		if(!i) qWarning("Failed to lock file on first attemp, retrying...");
		Sleep(100);
	}
	
	//Locked successfully?
	if((m_fileHandle == NULL) || (m_fileHandle == INVALID_HANDLE_VALUE))
	{
		QFile::remove(QFileInfo(outFile).canonicalFilePath());
		char error_msg[512];
		strcpy_s(error_msg, 512, QString("File '%1' could not be locked!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
		throw error_msg;
	}

	//Open file for reading
	if(g_useFileDescr)
	{
		int fd = _open_osfhandle(reinterpret_cast<intptr_t>(m_fileHandle), _O_RDONLY | _O_BINARY);
		if(fd >= 0) outFile.open(fd, QIODevice::ReadOnly);
	}
	else
	{
		for(int i = 0; i < 64; i++)
		{
			if(outFile.open(QIODevice::ReadOnly)) break;
			if(!i) qWarning("Failed to re-open file on first attemp, retrying...");
			Sleep(100);
		}
	}

	//Verify file contents
	QByteArray hash;
	if(outFile.isOpen())
	{
		hash = fileHash(outFile).toHex();
		outFile.close();
	}
	else
	{
		QFile::remove(m_filePath);
		THROW(QString("File '%1' could not be read!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
	}

	//Compare hashes
	if(hash.isNull() || _stricmp(hash.constData(), expectedHash.constData()))
	{
		qWarning("\nFile checksum error:\n A = %s\n B = %s\n", expectedHash.constData(), hash.constData());
		LAMEXP_CLOSE(m_fileHandle);
		QFile::remove(m_filePath);
		THROW(QString("File '%1' is corruputed, take care!").arg(QFileInfo(resourcePath).absoluteFilePath().replace(QRegExp("^:/"), QString())).toLatin1().constData());
	}
}