You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WBConfig.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "WBConfig.h"
  2. #include <QDir>
  3. stWBBottle::stWBBottle(QString bottle, QString wine, QString launcher)
  4. : bottle(bottle), wine(wine), launcher(launcher){
  5. auto modPath = [](QString &path){
  6. if (path.isEmpty())
  7. path = "/";
  8. else if (path.back() != '/')
  9. path.push_back('/');
  10. };
  11. modPath(this->bottle);
  12. modPath(this->wine);
  13. }
  14. stWBProgram::stWBProgram(QString bottleName, QStringList args, QStringList envs)
  15. : bottleName(bottleName), args(args)
  16. {
  17. if (!envs.isEmpty())
  18. this->envs = new Environments(envs);
  19. else this->envs = nullptr;
  20. }
  21. stWBProgram::~stWBProgram()
  22. {
  23. if (envs)
  24. delete envs;
  25. }
  26. WBConfig::WBConfig(QObject *parent)
  27. : QObject (parent)
  28. {
  29. _settings = new QSettings("Prime-Hack", "WineBottle", this);
  30. _bottleCount = _settings->value("General/BottleCount", 0).toUInt();
  31. _lastBottleId = _settings->value("General/LastBottle", 0).toUInt();
  32. if (!_bottleCount){
  33. QDir defaulthBottle(QDir::homePath() + ".wine");
  34. if (defaulthBottle.exists())
  35. _bottles["Default"] = stWBBottle(defaulthBottle.path());
  36. } else {
  37. for (uint id = 1; id <= _bottleCount; ++id){
  38. QString section = "Bottle_" + QString::number(id) + "/";
  39. stWBBottle btl = stWBBottle(
  40. _settings->value(section + "bottle").toString(),
  41. _settings->value(section + "wine").toString(),
  42. _settings->value(section + "launcher").toString()
  43. );
  44. _bottles[_settings->value(section + "name").toString()] = btl;
  45. if (_lastBottleId == id)
  46. _lastBottle = _settings->value(section + "name").toString();
  47. }
  48. }
  49. if (_lastBottle.isEmpty() && !_bottles.isEmpty())
  50. _lastBottle = _bottles.firstKey();
  51. }
  52. WBConfig::~WBConfig()
  53. {
  54. delete _settings;
  55. }
  56. stWBBottle &WBConfig::bottle(const QString &name)
  57. {
  58. return _bottles[name];
  59. }
  60. QStringList WBConfig::names()
  61. {
  62. return _bottles.keys();
  63. }
  64. stWBProgram WBConfig::loadProgram(const QString &name)
  65. {
  66. auto bottleName = _settings->value(name + "/Bottle", "").toString();
  67. if (bottleName.isEmpty())
  68. return stWBProgram(_lastBottle);
  69. auto program = stWBProgram(bottleName,
  70. _settings->value(name + "/Args", "").toString().split(" "),
  71. _settings->value(name + "/AddEnv", "").toString().split("\n")
  72. );
  73. auto window = _settings->value(name + "/Window", "0").toString();
  74. // `window == "true"` is for compatible with WB2
  75. if (window == "true" || window == "1")
  76. program.deskMode = eWineDesktop::WD_wine;
  77. else if (window == "2")
  78. program.deskMode = eWineDesktop::WD_xephyr;
  79. program.wndSize.rx() = _settings->value(name + "/WinX", 1024).toInt();
  80. program.wndSize.ry() = _settings->value(name + "/WinY", 768).toInt();
  81. program.dxMode = _settings->value(name + "/DirectX", -1).toInt();
  82. auto log = _settings->value(name + "/Logging", "").toString();
  83. // `log == "true"` is for compatible with WB2
  84. if (log == "true" || log == "2")
  85. program.logMode = eWineLog::WL_file;
  86. else if (log == "1")
  87. program.logMode = eWineLog::WL_default;
  88. return program;
  89. }
  90. void WBConfig::saveProgram(const QString &name, const stWBProgram &program)
  91. {
  92. _settings->setValue(name + "/Bottle", program.bottleName);
  93. _settings->setValue(name + "/Args", program.args);
  94. if (program.envs)
  95. _settings->setValue(name + "/AddEnv", program.envs->environments());
  96. _settings->setValue(name + "/Window", program.deskMode);
  97. _settings->setValue(name + "/WinX", program.wndSize.x());
  98. _settings->setValue(name + "/WinY", program.wndSize.y());
  99. _settings->setValue(name + "/DirectX", program.dxMode);
  100. _settings->setValue(name + "/Logging", program.logMode);
  101. }