SettingsComponent.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. #include "SettingsComponent.h"
  2. #include "SettingsSection.h"
  3. #include "Paths.h"
  4. #include "utils/Utils.h"
  5. #include "QsLog.h"
  6. #include "AudioSettingsController.h"
  7. #include "Names.h"
  8. #include <QSaveFile>
  9. #include <QJsonDocument>
  10. #include <QJsonObject>
  11. #include <QJsonArray>
  12. #include <QList>
  13. #include <QSettings>
  14. #include "input/InputComponent.h"
  15. #include "system/SystemComponent.h"
  16. #include "Version.h"
  17. #define OLDEST_PREVIOUS_VERSION_KEY "oldestPreviousVersion"
  18. ///////////////////////////////////////////////////////////////////////////////////////////////////
  19. SettingsComponent::SettingsComponent(QObject *parent) : ComponentBase(parent), m_settingsVersion(-1)
  20. {
  21. }
  22. /////////////////////////////////////////////////////////////////////////////////////////
  23. void SettingsComponent::componentPostInitialize()
  24. {
  25. InputComponent::Get().registerHostCommand("cycle_setting", this, "cycleSettingCommand");
  26. InputComponent::Get().registerHostCommand("set_setting", this, "setSettingCommand");
  27. }
  28. /////////////////////////////////////////////////////////////////////////////////////////
  29. void SettingsComponent::cycleSettingCommand(const QString& args)
  30. {
  31. QString settingName = args;
  32. QStringList sub = settingName.split(".");
  33. if (sub.size() != 2)
  34. {
  35. QLOG_ERROR() << "Setting must be in the form section.name but got:" << settingName;
  36. return;
  37. }
  38. QString sectionID = sub[0];
  39. QString valueName = sub[1];
  40. SettingsSection* section = getSection(sectionID);
  41. if (!section)
  42. {
  43. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  44. return;
  45. }
  46. QVariantList values = section->possibleValues(valueName);
  47. // If no possible values are defined, check the type of the default value.
  48. // In the case it's a boolean simply negate the current value to cycle through.
  49. // Otherwise log an error message, that it's not possible to cycle through the value.
  50. if (values.size() == 0)
  51. {
  52. if (section->defaultValue(valueName).type() == QVariant::Bool)
  53. {
  54. QVariant currentValue = section->value(valueName);
  55. auto nextValue = currentValue.toBool() ? false : true;
  56. setValue(sectionID, valueName, nextValue);
  57. QLOG_DEBUG() << "Setting" << settingName << "to " << (nextValue ? "Enabled" : "Disabled");
  58. emit SystemComponent::Get().settingsMessage(valueName, nextValue ? "Enabled" : "Disabled");
  59. return;
  60. }
  61. else
  62. {
  63. QLOG_ERROR() << "Setting" << settingName << "is unknown or is not cycleable.";
  64. return;
  65. }
  66. }
  67. QVariant currentValue = section->value(valueName);
  68. int nextValueIndex = 0;
  69. for (int n = 0; n < values.size(); n++)
  70. {
  71. if (currentValue == values[n].toMap()["value"])
  72. {
  73. nextValueIndex = n + 1;
  74. break;
  75. }
  76. }
  77. if (nextValueIndex >= values.size())
  78. nextValueIndex = 0;
  79. auto nextSetting = values[nextValueIndex].toMap();
  80. auto nextValue = nextSetting["value"];
  81. QLOG_DEBUG() << "Setting" << settingName << "to" << nextValue;
  82. setValue(sectionID, valueName, nextValue);
  83. emit SystemComponent::Get().settingsMessage(valueName, nextSetting["title"].toString());
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////
  86. void SettingsComponent::setSettingCommand(const QString& args)
  87. {
  88. int spaceIndex = args.indexOf(" ");
  89. if (spaceIndex < 0)
  90. {
  91. QLOG_ERROR() << "No value provided to settings set command.";
  92. return;
  93. }
  94. QString settingName = args.mid(0, spaceIndex);
  95. QString settingValue = args.mid(spaceIndex + 1);
  96. int subIndex = settingName.indexOf(".");
  97. if (subIndex < 0 || subIndex == args.size() - 1)
  98. {
  99. QLOG_ERROR() << "Setting must be in the form section.name but got:" << settingName;
  100. return;
  101. }
  102. QString sectionID = settingName.mid(0, subIndex);
  103. QString valueName = settingName.mid(subIndex + 1);
  104. SettingsSection* section = getSection(sectionID);
  105. if (!section)
  106. {
  107. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  108. return;
  109. }
  110. QString jsonString = "{\"value\": " + settingValue + "}";
  111. QJsonParseError err;
  112. QVariant value = QJsonDocument::fromJson(jsonString.toUtf8(), &err).object()["value"].toVariant();
  113. printf("val: '%s'\n", settingValue.toUtf8().data());
  114. if (!value.isValid())
  115. {
  116. QLOG_ERROR() << "Invalid settings value:" << settingValue << "(if it's a string, make sure to quote it)";
  117. return;
  118. }
  119. QLOG_DEBUG() << "Setting" << settingName << "to" << value;
  120. setValue(sectionID, valueName, value);
  121. emit SystemComponent::Get().settingsMessage(valueName, value.toString());
  122. }
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////
  124. void SettingsComponent::updatePossibleValues(const QString &sectionID, const QString &key, const QVariantList &possibleValues)
  125. {
  126. SettingsSection* section = getSection(sectionID);
  127. if (!section)
  128. {
  129. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  130. return;
  131. }
  132. section->updatePossibleValues(key, possibleValues);
  133. QLOG_DEBUG() << "Updated possible values for:" << key << "to" << possibleValues;
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////
  136. QVariant SettingsComponent::allValues(const QString& section)
  137. {
  138. if (section.isEmpty())
  139. {
  140. QVariantMap all;
  141. for(const QString& sname : m_sections.keys())
  142. all[sname] = m_sections[sname]->allValues();
  143. return all;
  144. }
  145. else if (m_sections.contains(section))
  146. {
  147. return m_sections[section]->allValues();
  148. }
  149. else
  150. {
  151. // look for the value in the webclient section
  152. return m_sections[SETTINGS_SECTION_WEBCLIENT]->value(section);
  153. }
  154. }
  155. ///////////////////////////////////////////////////////////////////////////////////////////////////
  156. static void writeFile(const QString& filename, const QByteArray& data)
  157. {
  158. QSaveFile file(filename);
  159. file.open(QIODevice::WriteOnly | QIODevice::Text);
  160. file.write(data);
  161. if (!file.commit())
  162. {
  163. QLOG_ERROR() << "Could not write" << filename;
  164. }
  165. }
  166. ///////////////////////////////////////////////////////////////////////////////////////////////////
  167. static QJsonObject loadJson(const QString& filename)
  168. {
  169. // Checking existence before opening is technically a race condition, but
  170. // it looks like Qt doesn't let us distinguish errors on opening.
  171. if (!QFile(filename).exists())
  172. return QJsonObject();
  173. QJsonParseError err;
  174. QJsonDocument json = Utils::OpenJsonDocument(filename, &err);
  175. if (json.isNull())
  176. {
  177. QLOG_ERROR() << "Could not open" << filename << "due to" << err.errorString();
  178. }
  179. return json.object();
  180. }
  181. ///////////////////////////////////////////////////////////////////////////////////////////////////
  182. static void writeJson(const QString& filename, const QJsonObject& data, bool pretty = true)
  183. {
  184. QJsonDocument json(data);
  185. writeFile(filename, json.toJson(pretty ? QJsonDocument::Indented : QJsonDocument::Compact));
  186. }
  187. /////////////////////////////////////////////////////////////////////////////////////////
  188. QVariant SettingsComponent::readPreinitValue(const QString& sectionID, const QString& key)
  189. {
  190. QJsonObject json = loadJson(Paths::dataDir("jellyfinmediaplayer.conf"));
  191. return json["sections"].toObject()[sectionID].toObject()[key].toVariant();
  192. }
  193. /////////////////////////////////////////////////////////////////////////////////////////
  194. void SettingsComponent::load()
  195. {
  196. loadConf(Paths::dataDir("jellyfinmediaplayer.conf"), false);
  197. loadConf(Paths::dataDir("storage.json"), true);
  198. }
  199. ///////////////////////////////////////////////////////////////////////////////////////////////////
  200. void SettingsComponent::loadConf(const QString& path, bool storage)
  201. {
  202. bool migrateJmpSettings = false;
  203. QJsonObject json = loadJson(path);
  204. int version = json["version"].toInt(0);
  205. if (version == 4 && m_settingsVersion == 5)
  206. {
  207. migrateJmpSettings = true;
  208. }
  209. else if (version != m_settingsVersion)
  210. {
  211. QString backup = path + ".broken";
  212. QFile::remove(backup);
  213. QFile::rename(path, backup);
  214. if (version == 0)
  215. QLOG_ERROR() << "Could not read config file.";
  216. else
  217. QLOG_ERROR() << "Config version is" << version << "but" << m_settingsVersion << "expected. Moving old config to" << backup;
  218. // Overwrite/create it with the defaults.
  219. if (storage)
  220. saveStorage();
  221. else
  222. saveSettings();
  223. return;
  224. }
  225. QJsonObject jsonSections = json["sections"].toObject();
  226. for(const QString& section : jsonSections.keys())
  227. {
  228. QJsonObject jsonSection = jsonSections[section].toObject();
  229. SettingsSection* sec = getSection(section);
  230. if (!sec && storage)
  231. {
  232. sec = new SettingsSection(section, PLATFORM_ANY, -1, this);
  233. sec->setHidden(true);
  234. sec->setStorage(true);
  235. m_sections.insert(section, sec);
  236. }
  237. else if (!sec)
  238. {
  239. QLOG_ERROR() << "Trying to load section:" << section << "from config file, but we don't want that.";
  240. continue;
  241. }
  242. for(const QString& setting : jsonSection.keys())
  243. sec->setValue(setting, jsonSection.value(setting).toVariant());
  244. }
  245. if (migrateJmpSettings) {
  246. getSection(SETTINGS_SECTION_MAIN)->setValue("webMode", "desktop");
  247. getSection(SETTINGS_SECTION_MAIN)->setValue("layout", "desktop");
  248. if (getSection(SETTINGS_SECTION_VIDEO)->value("hardwareDecoding") == "disabled") {
  249. getSection(SETTINGS_SECTION_VIDEO)->setValue("hardwareDecoding", "enabled");
  250. }
  251. }
  252. }
  253. ///////////////////////////////////////////////////////////////////////////////////////////////////
  254. void SettingsComponent::saveSettings()
  255. {
  256. if (m_oldestPreviousVersion.isEmpty())
  257. {
  258. QLOG_ERROR() << "Not writing settings: uninitialized.\n";
  259. return;
  260. }
  261. QVariantMap sections;
  262. for(SettingsSection* section : m_sections.values())
  263. {
  264. if (!section->isStorage())
  265. sections.insert(section->sectionName(), section->allValues());
  266. }
  267. QJsonObject json;
  268. json.insert("sections", QJsonValue::fromVariant(sections));
  269. json.insert("version", m_settingsVersion);
  270. writeJson(Paths::dataDir("jellyfinmediaplayer.conf"), json);
  271. }
  272. ///////////////////////////////////////////////////////////////////////////////////////////////////
  273. void SettingsComponent::saveStorage()
  274. {
  275. QVariantMap storage;
  276. for(SettingsSection* section : m_sections.values())
  277. {
  278. if (section->isStorage())
  279. storage.insert(section->sectionName(), section->allValues());
  280. }
  281. QJsonObject storagejson;
  282. storagejson.insert("sections", QJsonValue::fromVariant(storage));
  283. storagejson.insert("version", m_settingsVersion);
  284. writeJson(Paths::dataDir("storage.json"), storagejson, false);
  285. }
  286. ///////////////////////////////////////////////////////////////////////////////////////////////////
  287. void SettingsComponent::saveSection(SettingsSection* section)
  288. {
  289. if (section && section->isStorage())
  290. saveStorage();
  291. else
  292. saveSettings();
  293. }
  294. ///////////////////////////////////////////////////////////////////////////////////////////////////
  295. QVariant SettingsComponent::value(const QString& sectionID, const QString &key)
  296. {
  297. SettingsSection* section = getSection(sectionID);
  298. if (!section)
  299. {
  300. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  301. return QVariant();
  302. }
  303. return section->value(key);
  304. }
  305. ///////////////////////////////////////////////////////////////////////////////////////////////////
  306. void SettingsComponent::setValue(const QString& sectionID, const QString &key, const QVariant &value)
  307. {
  308. SettingsSection* section = getSection(sectionID);
  309. if (!section)
  310. {
  311. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  312. return;
  313. }
  314. section->setValue(key, value);
  315. saveSection(section);
  316. }
  317. ///////////////////////////////////////////////////////////////////////////////////////////////////
  318. void SettingsComponent::setValues(const QVariantMap& options)
  319. {
  320. Q_ASSERT(options.contains("key"));
  321. Q_ASSERT(options.contains("value"));
  322. QString key = options["key"].toString();
  323. QVariant values = options["value"];
  324. if (values.type() == QVariant::Map || values.isNull())
  325. {
  326. SettingsSection* section = getSection(key);
  327. if (!section)
  328. {
  329. // let's create this section since it's most likely created by the webclient
  330. section = new SettingsSection(key, PLATFORM_ANY, -1, this);
  331. section->setHidden(true);
  332. section->setStorage(true);
  333. m_sections.insert(key, section);
  334. }
  335. if (values.isNull())
  336. section->resetValues();
  337. else
  338. section->setValues(values);
  339. saveSection(section);
  340. }
  341. else if (values.type() == QVariant::String)
  342. {
  343. setValue(SETTINGS_SECTION_WEBCLIENT, key, values);
  344. }
  345. else
  346. {
  347. QLOG_WARN() << "the values sent was not a map, string or empty value. it will be ignored";
  348. // return so we don't call save()
  349. return;
  350. }
  351. }
  352. ///////////////////////////////////////////////////////////////////////////////////////////////////
  353. void SettingsComponent::removeValue(const QString &sectionOrKey)
  354. {
  355. SettingsSection* section = getSection(sectionOrKey);
  356. if (section)
  357. {
  358. // we want to remove a full section
  359. // dont remove the section, but remove all keys
  360. section->resetValues();
  361. saveSection(section);
  362. }
  363. else
  364. {
  365. // we want to remove a root key from webclient
  366. // which is stored in webclient section
  367. section = m_sections[SETTINGS_SECTION_WEBCLIENT];
  368. section->resetValue(sectionOrKey);
  369. saveSection(section);
  370. }
  371. }
  372. ///////////////////////////////////////////////////////////////////////////////////////////////////
  373. void SettingsComponent::resetToDefault(const QString &sectionID)
  374. {
  375. SettingsSection* section = getSection(sectionID);
  376. if (section)
  377. {
  378. section->resetValues();
  379. saveSection(section);
  380. }
  381. }
  382. ///////////////////////////////////////////////////////////////////////////////////////////////////
  383. void SettingsComponent::resetToDefaultAll()
  384. {
  385. for(SettingsSection *section : m_sections)
  386. {
  387. section->resetValues();
  388. saveSection(section);
  389. }
  390. }
  391. /////////////////////////////////////////////////////////////////////////////////////////
  392. struct SectionOrderIndex
  393. {
  394. inline bool operator ()(SettingsSection* a, SettingsSection* b)
  395. {
  396. return a->orderIndex() < b->orderIndex();
  397. }
  398. };
  399. ///////////////////////////////////////////////////////////////////////////////////////////////////
  400. QVariantList SettingsComponent::settingDescriptions()
  401. {
  402. QJsonArray desc;
  403. QList<SettingsSection*> sectionList = m_sections.values();
  404. std::sort(sectionList.begin(), sectionList.end(), SectionOrderIndex());
  405. for(SettingsSection* section : sectionList)
  406. {
  407. if (!section->isHidden())
  408. desc.push_back(QJsonValue::fromVariant(section->descriptions()));
  409. }
  410. return desc.toVariantList();
  411. }
  412. /////////////////////////////////////////////////////////////////////////////////////////
  413. bool SettingsComponent::loadDescription()
  414. {
  415. QJsonParseError err;
  416. auto doc = Utils::OpenJsonDocument(":/settings/settings_description.json", &err);
  417. if (doc.isNull())
  418. {
  419. QLOG_ERROR() << "Failed to read settings description:" << err.errorString();
  420. throw FatalException("Failed to read settings description!");
  421. }
  422. if (!doc.isArray())
  423. {
  424. QLOG_ERROR() << "The object needs to be an array";
  425. return false;
  426. }
  427. m_sectionIndex = 0;
  428. for(auto val : doc.array())
  429. {
  430. if (!val.isObject())
  431. {
  432. QLOG_ERROR() << "Hoped to find sections in the root array, but they where not JSON objects";
  433. return false;
  434. }
  435. QJsonObject section = val.toObject();
  436. if (!section.contains("section"))
  437. {
  438. QLOG_ERROR() << "A section needs to contain the section keyword.";
  439. return false;
  440. }
  441. if (section["section"] == "__meta__")
  442. m_settingsVersion = section.value("version").toInt();
  443. else
  444. parseSection(section);
  445. }
  446. return true;
  447. }
  448. /////////////////////////////////////////////////////////////////////////////////////////
  449. void SettingsComponent::parseSection(const QJsonObject& sectionObject)
  450. {
  451. QString sectionName = sectionObject.value("section").toString();
  452. if (!sectionObject.contains("values") || !sectionObject.value("values").isArray())
  453. {
  454. QLOG_ERROR() << "section object:" << sectionName << "did not contain a values array";
  455. return;
  456. }
  457. int platformMask = platformMaskFromObject(sectionObject);
  458. auto section = new SettingsSection(sectionName, (quint8)platformMask, m_sectionIndex ++, this);
  459. section->setHidden(sectionObject.value("hidden").toBool(false));
  460. section->setStorage(sectionObject.value("storage").toBool(false));
  461. auto values = sectionObject.value("values").toArray();
  462. int order = 0;
  463. for(auto val : values)
  464. {
  465. if (!val.isObject())
  466. continue;
  467. QJsonObject valobj = val.toObject();
  468. if (!valobj.contains("value") || !valobj.contains("default") || valobj.value("value").isNull())
  469. continue;
  470. QJsonValue defaults = valobj.value("default");
  471. QVariant defaultval = defaults.toVariant();
  472. if (defaults.isArray())
  473. {
  474. defaultval = QVariant();
  475. // Whichever default matches the current platform first is used.
  476. for(auto v : defaults.toArray())
  477. {
  478. auto vobj = v.toObject();
  479. int defPlatformMask = platformMaskFromObject(vobj);
  480. if ((defPlatformMask & Utils::CurrentPlatform()) == Utils::CurrentPlatform())
  481. {
  482. defaultval = vobj.value("value").toVariant();
  483. break;
  484. }
  485. }
  486. }
  487. int vPlatformMask = platformMaskFromObject(valobj);
  488. SettingsValue* setting = new SettingsValue(valobj.value("value").toString(), defaultval, (quint8)vPlatformMask, this);
  489. setting->setHasDescription(true);
  490. setting->setHidden(valobj.value("hidden").toBool(false));
  491. setting->setIndexOrder(order ++);
  492. if (valobj.contains("input_type"))
  493. setting->setInputType(valobj.value("input_type").toString());
  494. if (valobj.contains("possible_values") && valobj.value("possible_values").isArray())
  495. {
  496. auto list = valobj.value("possible_values").toArray();
  497. for(auto v : list)
  498. {
  499. int platform = PLATFORM_ANY;
  500. auto vl = v.toArray();
  501. if (vl.size() < 2)
  502. continue;
  503. if (vl.size() == 3 && vl.at(2).isObject())
  504. {
  505. // platform options
  506. QJsonObject options = vl.at(2).toObject();
  507. platform = platformMaskFromObject(options);
  508. }
  509. if ((platform & Utils::CurrentPlatform()) == Utils::CurrentPlatform())
  510. setting->addPossibleValue(vl.at(1).toString(), vl.at(0).toVariant());
  511. }
  512. }
  513. section->registerSetting(setting);
  514. }
  515. m_sections.insert(sectionName, section);
  516. }
  517. /////////////////////////////////////////////////////////////////////////////////////////
  518. int SettingsComponent::platformMaskFromObject(const QJsonObject& object)
  519. {
  520. int platformMask = PLATFORM_ANY;
  521. // only include the listed platforms
  522. if (object.contains("platforms"))
  523. {
  524. // start by resetting it to no platforms
  525. platformMask = PLATFORM_UNKNOWN;
  526. QJsonValue platforms = object.value("platforms");
  527. // platforms can be both array or a single string
  528. if (platforms.isArray())
  529. {
  530. for(auto pl : platforms.toArray())
  531. {
  532. if (!pl.isString())
  533. continue;
  534. platformMask |= platformFromString(pl.toString());
  535. }
  536. }
  537. else
  538. {
  539. platformMask = platformFromString(platforms.toString());
  540. }
  541. }
  542. else if (object.contains("platforms_excluded"))
  543. {
  544. QJsonValue val = object.value("platforms_excluded");
  545. if (val.isArray())
  546. {
  547. for(auto pl : val.toArray())
  548. {
  549. if (!pl.isString())
  550. continue;
  551. platformMask &= ~platformFromString(pl.toString());
  552. }
  553. }
  554. else
  555. {
  556. platformMask &= ~platformFromString(val.toString());
  557. }
  558. }
  559. return platformMask;
  560. }
  561. /////////////////////////////////////////////////////////////////////////////////////////
  562. Platform SettingsComponent::platformFromString(const QString& platformString)
  563. {
  564. if (platformString == "osx")
  565. return PLATFORM_OSX;
  566. else if (platformString == "windows")
  567. return PLATFORM_WINDOWS;
  568. else if (platformString == "linux")
  569. return PLATFORM_LINUX;
  570. else if (platformString == "oe")
  571. return PLATFORM_OE;
  572. else if (platformString == "oe_rpi")
  573. return PLATFORM_OE_RPI;
  574. else if (platformString == "oe_x86")
  575. return PLATFORM_OE_X86;
  576. else if (platformString == "any")
  577. return PLATFORM_ANY;
  578. return PLATFORM_UNKNOWN;
  579. }
  580. /////////////////////////////////////////////////////////////////////////////////////////
  581. bool SettingsComponent::componentInitialize()
  582. {
  583. if (!loadDescription())
  584. return false;
  585. // Must be called before we possibly write the config file.
  586. setupVersion();
  587. load();
  588. // add our AudioSettingsController that will inspect audio settings and react.
  589. // then run the signal the first time to make sure that we set the proper visibility
  590. // on the items from the start.
  591. //
  592. auto ctrl = new AudioSettingsController(this);
  593. QVariantMap val;
  594. val.insert("devicetype", value(SETTINGS_SECTION_AUDIO, "devicetype"));
  595. ctrl->valuesUpdated(val);
  596. connect(ctrl, &AudioSettingsController::settingsUpdated, this, &SettingsComponent::groupUpdate);
  597. return true;
  598. }
  599. /////////////////////////////////////////////////////////////////////////////////////////
  600. void SettingsComponent::setupVersion()
  601. {
  602. QSettings settings;
  603. m_oldestPreviousVersion = settings.value(OLDEST_PREVIOUS_VERSION_KEY).toString();
  604. if (m_oldestPreviousVersion.isEmpty())
  605. {
  606. // Version key was not present. It could still be a pre-1.1 PMP install,
  607. // so here we try to find out whether this is the very first install, or
  608. // if an older one exists.
  609. QFile configFile(Paths::dataDir("jellyfinmediaplayer.conf"));
  610. if (configFile.exists())
  611. m_oldestPreviousVersion = "legacy";
  612. else
  613. m_oldestPreviousVersion = Version::GetVersionString();
  614. settings.setValue(OLDEST_PREVIOUS_VERSION_KEY, m_oldestPreviousVersion);
  615. }
  616. }
  617. /////////////////////////////////////////////////////////////////////////////////////////
  618. bool SettingsComponent::resetAndSaveOldConfiguration()
  619. {
  620. QFile settingsFile(Paths::dataDir("jellyfinmediaplayer.conf"));
  621. return settingsFile.rename(Paths::dataDir("jellyfinmediaplayer.conf.old"));
  622. }
  623. /////////////////////////////////////////////////////////////////////////////////////////
  624. QString SettingsComponent::getWebClientUrl(bool desktop)
  625. {
  626. QString url;
  627. url = SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl_desktop").toString();
  628. if (url == "bundled")
  629. {
  630. auto path = Paths::webClientPath("desktop");
  631. url = "file:///" + path;
  632. }
  633. QLOG_DEBUG() << "Using web-client URL: " << url;
  634. return url;
  635. }
  636. /////////////////////////////////////////////////////////////////////////////////////////
  637. QString SettingsComponent::getExtensionPath()
  638. {
  639. QString url;
  640. url = SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl_extension").toString();
  641. if (url == "bundled")
  642. {
  643. auto path = Paths::webExtensionPath("extension");
  644. url = path;
  645. }
  646. return url;
  647. }
  648. /////////////////////////////////////////////////////////////////////////////////////////
  649. QString SettingsComponent::getClientName()
  650. {
  651. QString name;
  652. name = SettingsComponent::Get().value(SETTINGS_SECTION_SYSTEM, "systemname").toString();
  653. if (name.compare("JellyfinMediaPlayer") == 0) {
  654. name = Utils::ComputerName();
  655. }
  656. return name;
  657. }
  658. /////////////////////////////////////////////////////////////////////////////////////////
  659. void SettingsComponent::setCommandLineValues(const QStringList& values)
  660. {
  661. QLOG_DEBUG() << values;
  662. for (const QString& value : values)
  663. {
  664. if (value == "fullscreen")
  665. setValue(SETTINGS_SECTION_MAIN, "fullscreen", true);
  666. else if (value == "windowed")
  667. setValue(SETTINGS_SECTION_MAIN, "fullscreen", false);
  668. else if (value == "desktop")
  669. setValue(SETTINGS_SECTION_MAIN, "layout", "desktop");
  670. else if (value == "tv")
  671. setValue(SETTINGS_SECTION_MAIN, "layout", "tv");
  672. }
  673. }