filesystem.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef FILESYSTEM_H
  2. #define FILESYSTEM_H
  3. #include <QObject>
  4. #include <QFile>
  5. #include <QDir>
  6. #include <QFileInfo>
  7. #include <QCryptographicHash>
  8. #include <QDebug>
  9. #include <QSettings>
  10. namespace FileSystem {
  11. static bool fileExists(QString path) {
  12. QFileInfo check_file(path);
  13. return check_file.exists() && check_file.isFile();
  14. }
  15. static bool folderExists(QString path) {
  16. return QDir(path).exists();
  17. }
  18. static QString fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm = QCryptographicHash::Md5) {
  19. QFile file(fileName);
  20. if (file.open(QIODevice::ReadOnly)) {
  21. QCryptographicHash hash(hashAlgorithm);
  22. hash.addData(&file);
  23. QByteArray hashData = hash.result();
  24. return hashData.toHex();
  25. }
  26. return QByteArray();
  27. }
  28. static void clearFolder(QDir &dir) {
  29. //Получаем список файлов
  30. QStringList lstFiles = dir.entryList(QDir::Files);
  31. //Удаляем файлы
  32. foreach (QString entry, lstFiles){
  33. QString entryAbsPath = dir.absolutePath() + "/" + entry;
  34. //QFile::setPermissions(entryAbsPath, QFile::ReadOwner | QFile::WriteOwner);
  35. qDebug() << dir.absolutePath();
  36. QFile::remove(entryAbsPath);
  37. }
  38. }
  39. static QStringList recognizeRegistryLotroPath() {
  40. QStringList paths;
  41. #ifdef _WIN32
  42. // Windows 8, 10
  43. QSettings n("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\12bbe590-c890-11d9-9669-0800200c9a66_is1", QSettings::NativeFormat);
  44. foreach (QString key, n.allKeys()) {
  45. qDebug() << key;
  46. if(key.contains("InstallLocation") || key.contains("installlocation")){
  47. QString folder = n.value(key).toString()
  48. .replace("\\", "/")
  49. .replace("/TurbineLauncher.exe", "")
  50. .replace("/LotroLauncher.exe", "")
  51. .replace("\"", "");
  52. if(fileExists(folder + "/LotroLauncher.exe"))
  53. paths.append(folder);
  54. }
  55. }
  56. // Windows 7
  57. QSettings m("HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache", QSettings::NativeFormat);
  58. foreach (QString key, m.allKeys()) {
  59. if((key.contains("TurbineLauncher.exe") || key.contains("LotroLauncher.exe")) && fileExists(key)){
  60. QString folder = n.value(key).toString()
  61. .replace("\\", "/")
  62. .replace("/TurbineLauncher.exe", "")
  63. .replace("/LotroLauncher.exe", "")
  64. .replace("\"", "");
  65. if(fileExists(folder + "/LotroLauncher.exe"))
  66. paths.append(folder);
  67. }
  68. }
  69. #else
  70. // Реализация под Linux
  71. #endif
  72. return paths;
  73. }
  74. }
  75. #endif // FILESYSTEM_H