filesystem.cpp 3.2 KB

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