filesystem.h 3.0 KB

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