SettingsComponent.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 migrateJmpSettings4 = false;
  203. bool migrateJmpSettings5 = false;
  204. QJsonObject json = loadJson(path);
  205. int version = json["version"].toInt(0);
  206. if (version == 4 && m_settingsVersion == 6)
  207. {
  208. migrateJmpSettings4 = true;
  209. }
  210. else if (version == 5 && m_settingsVersion == 6)
  211. {
  212. migrateJmpSettings5 = true;
  213. }
  214. else if (version != m_settingsVersion)
  215. {
  216. QString backup = path + ".broken";
  217. QFile::remove(backup);
  218. QFile::rename(path, backup);
  219. if (version == 0)
  220. QLOG_ERROR() << "Could not read config file.";
  221. else
  222. QLOG_ERROR() << "Config version is" << version << "but" << m_settingsVersion << "expected. Moving old config to" << backup;
  223. // Overwrite/create it with the defaults.
  224. if (storage)
  225. saveStorage();
  226. else
  227. saveSettings();
  228. return;
  229. }
  230. QJsonObject jsonSections = json["sections"].toObject();
  231. for(const QString& section : jsonSections.keys())
  232. {
  233. QJsonObject jsonSection = jsonSections[section].toObject();
  234. SettingsSection* sec = getSection(section);
  235. if (!sec && storage)
  236. {
  237. sec = new SettingsSection(section, PLATFORM_ANY, -1, this);
  238. sec->setHidden(true);
  239. sec->setStorage(true);
  240. m_sections.insert(section, sec);
  241. }
  242. else if (!sec)
  243. {
  244. QLOG_ERROR() << "Trying to load section:" << section << "from config file, but we don't want that.";
  245. continue;
  246. }
  247. for(const QString& setting : jsonSection.keys())
  248. sec->setValue(setting, jsonSection.value(setting).toVariant());
  249. }
  250. if (migrateJmpSettings4) {
  251. getSection(SETTINGS_SECTION_MAIN)->setValue("webMode", "desktop");
  252. getSection(SETTINGS_SECTION_MAIN)->setValue("layout", "desktop");
  253. if (getSection(SETTINGS_SECTION_VIDEO)->value("hardwareDecoding") == "disabled") {
  254. getSection(SETTINGS_SECTION_VIDEO)->setValue("hardwareDecoding", "copy");
  255. }
  256. } else if (migrateJmpSettings5) {
  257. if (getSection(SETTINGS_SECTION_VIDEO)->value("hardwareDecoding") == "enabled") {
  258. getSection(SETTINGS_SECTION_VIDEO)->setValue("hardwareDecoding", "copy");
  259. }
  260. }
  261. }
  262. ///////////////////////////////////////////////////////////////////////////////////////////////////
  263. void SettingsComponent::saveSettings()
  264. {
  265. if (m_oldestPreviousVersion.isEmpty())
  266. {
  267. QLOG_ERROR() << "Not writing settings: uninitialized.\n";
  268. return;
  269. }
  270. QVariantMap sections;
  271. for(SettingsSection* section : m_sections.values())
  272. {
  273. if (!section->isStorage())
  274. sections.insert(section->sectionName(), section->allValues());
  275. }
  276. QJsonObject json;
  277. json.insert("sections", QJsonValue::fromVariant(sections));
  278. json.insert("version", m_settingsVersion);
  279. writeJson(Paths::dataDir("jellyfinmediaplayer.conf"), json);
  280. }
  281. ///////////////////////////////////////////////////////////////////////////////////////////////////
  282. void SettingsComponent::saveStorage()
  283. {
  284. QVariantMap storage;
  285. for(SettingsSection* section : m_sections.values())
  286. {
  287. if (section->isStorage())
  288. storage.insert(section->sectionName(), section->allValues());
  289. }
  290. QJsonObject storagejson;
  291. storagejson.insert("sections", QJsonValue::fromVariant(storage));
  292. storagejson.insert("version", m_settingsVersion);
  293. writeJson(Paths::dataDir("storage.json"), storagejson, false);
  294. }
  295. ///////////////////////////////////////////////////////////////////////////////////////////////////
  296. void SettingsComponent::saveSection(SettingsSection* section)
  297. {
  298. if (section && section->isStorage())
  299. saveStorage();
  300. else
  301. saveSettings();
  302. }
  303. ///////////////////////////////////////////////////////////////////////////////////////////////////
  304. QVariant SettingsComponent::value(const QString& sectionID, const QString &key)
  305. {
  306. SettingsSection* section = getSection(sectionID);
  307. if (!section)
  308. {
  309. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  310. return QVariant();
  311. }
  312. return section->value(key);
  313. }
  314. ///////////////////////////////////////////////////////////////////////////////////////////////////
  315. void SettingsComponent::setValue(const QString& sectionID, const QString &key, const QVariant &value)
  316. {
  317. SettingsSection* section = getSection(sectionID);
  318. if (!section)
  319. {
  320. QLOG_ERROR() << "Section" << sectionID << "is unknown";
  321. return;
  322. }
  323. section->setValue(key, value);
  324. saveSection(section);
  325. }
  326. ///////////////////////////////////////////////////////////////////////////////////////////////////
  327. void SettingsComponent::setValues(const QVariantMap& options)
  328. {
  329. Q_ASSERT(options.contains("key"));
  330. Q_ASSERT(options.contains("value"));
  331. QString key = options["key"].toString();
  332. QVariant values = options["value"];
  333. if (values.type() == QVariant::Map || values.isNull())
  334. {
  335. SettingsSection* section = getSection(key);
  336. if (!section)
  337. {
  338. // let's create this section since it's most likely created by the webclient
  339. section = new SettingsSection(key, PLATFORM_ANY, -1, this);
  340. section->setHidden(true);
  341. section->setStorage(true);
  342. m_sections.insert(key, section);
  343. }
  344. if (values.isNull())
  345. section->resetValues();
  346. else
  347. section->setValues(values);
  348. saveSection(section);
  349. }
  350. else if (values.type() == QVariant::String)
  351. {
  352. setValue(SETTINGS_SECTION_WEBCLIENT, key, values);
  353. }
  354. else
  355. {
  356. QLOG_WARN() << "the values sent was not a map, string or empty value. it will be ignored";
  357. // return so we don't call save()
  358. return;
  359. }
  360. }
  361. ///////////////////////////////////////////////////////////////////////////////////////////////////
  362. void SettingsComponent::removeValue(const QString &sectionOrKey)
  363. {
  364. SettingsSection* section = getSection(sectionOrKey);
  365. if (section)
  366. {
  367. // we want to remove a full section
  368. // dont remove the section, but remove all keys
  369. section->resetValues();
  370. saveSection(section);
  371. }
  372. else
  373. {
  374. // we want to remove a root key from webclient
  375. // which is stored in webclient section
  376. section = m_sections[SETTINGS_SECTION_WEBCLIENT];
  377. section->resetValue(sectionOrKey);
  378. saveSection(section);
  379. }
  380. }
  381. ///////////////////////////////////////////////////////////////////////////////////////////////////
  382. void SettingsComponent::resetToDefault(const QString &sectionID)
  383. {
  384. SettingsSection* section = getSection(sectionID);
  385. if (section)
  386. {
  387. section->resetValues();
  388. saveSection(section);
  389. }
  390. }
  391. ///////////////////////////////////////////////////////////////////////////////////////////////////
  392. void SettingsComponent::resetToDefaultAll()
  393. {
  394. for(SettingsSection *section : m_sections)
  395. {
  396. section->resetValues();
  397. saveSection(section);
  398. }
  399. }
  400. /////////////////////////////////////////////////////////////////////////////////////////
  401. struct SectionOrderIndex
  402. {
  403. inline bool operator ()(SettingsSection* a, SettingsSection* b)
  404. {
  405. return a->orderIndex() < b->orderIndex();
  406. }
  407. };
  408. ///////////////////////////////////////////////////////////////////////////////////////////////////
  409. QVariantList SettingsComponent::settingDescriptions()
  410. {
  411. QJsonArray desc;
  412. QList<SettingsSection*> sectionList = m_sections.values();
  413. std::sort(sectionList.begin(), sectionList.end(), SectionOrderIndex());
  414. for(SettingsSection* section : sectionList)
  415. {
  416. if (!section->isHidden())
  417. desc.push_back(QJsonValue::fromVariant(section->descriptions()));
  418. }
  419. return desc.toVariantList();
  420. }
  421. /////////////////////////////////////////////////////////////////////////////////////////
  422. bool SettingsComponent::loadDescription()
  423. {
  424. QJsonParseError err;
  425. auto doc = Utils::OpenJsonDocument(":/settings/settings_description.json", &err);
  426. if (doc.isNull())
  427. {
  428. QLOG_ERROR() << "Failed to read settings description:" << err.errorString();
  429. throw FatalException("Failed to read settings description!");
  430. }
  431. if (!doc.isArray())
  432. {
  433. QLOG_ERROR() << "The object needs to be an array";
  434. return false;
  435. }
  436. m_sectionIndex = 0;
  437. for(auto val : doc.array())
  438. {
  439. if (!val.isObject())
  440. {
  441. QLOG_ERROR() << "Hoped to find sections in the root array, but they where not JSON objects";
  442. return false;
  443. }
  444. QJsonObject section = val.toObject();
  445. if (!section.contains("section"))
  446. {
  447. QLOG_ERROR() << "A section needs to contain the section keyword.";
  448. return false;
  449. }
  450. if (section["section"] == "__meta__")
  451. m_settingsVersion = section.value("version").toInt();
  452. else
  453. parseSection(section);
  454. }
  455. return true;
  456. }
  457. /////////////////////////////////////////////////////////////////////////////////////////
  458. void SettingsComponent::parseSection(const QJsonObject& sectionObject)
  459. {
  460. QString sectionName = sectionObject.value("section").toString();
  461. if (!sectionObject.contains("values") || !sectionObject.value("values").isArray())
  462. {
  463. QLOG_ERROR() << "section object:" << sectionName << "did not contain a values array";
  464. return;
  465. }
  466. int platformMask = platformMaskFromObject(sectionObject);
  467. auto section = new SettingsSection(sectionName, (quint8)platformMask, m_sectionIndex ++, this);
  468. section->setHidden(sectionObject.value("hidden").toBool(false));
  469. section->setStorage(sectionObject.value("storage").toBool(false));
  470. auto values = sectionObject.value("values").toArray();
  471. int order = 0;
  472. for(auto val : values)
  473. {
  474. if (!val.isObject())
  475. continue;
  476. QJsonObject valobj = val.toObject();
  477. if (!valobj.contains("value") || !valobj.contains("default") || valobj.value("value").isNull())
  478. continue;
  479. QJsonValue defaults = valobj.value("default");
  480. QVariant defaultval = defaults.toVariant();
  481. if (defaults.isArray())
  482. {
  483. defaultval = QVariant();
  484. // Whichever default matches the current platform first is used.
  485. for(auto v : defaults.toArray())
  486. {
  487. auto vobj = v.toObject();
  488. int defPlatformMask = platformMaskFromObject(vobj);
  489. if ((defPlatformMask & Utils::CurrentPlatform()) == Utils::CurrentPlatform())
  490. {
  491. defaultval = vobj.value("value").toVariant();
  492. break;
  493. }
  494. }
  495. }
  496. int vPlatformMask = platformMaskFromObject(valobj);
  497. SettingsValue* setting = new SettingsValue(valobj.value("value").toString(), defaultval, (quint8)vPlatformMask, this);
  498. setting->setHasDescription(true);
  499. setting->setHidden(valobj.value("hidden").toBool(false));
  500. setting->setIndexOrder(order ++);
  501. if (valobj.contains("input_type"))
  502. setting->setInputType(valobj.value("input_type").toString());
  503. if (valobj.contains("possible_values") && valobj.value("possible_values").isArray())
  504. {
  505. auto list = valobj.value("possible_values").toArray();
  506. for(auto v : list)
  507. {
  508. int platform = PLATFORM_ANY;
  509. auto vl = v.toArray();
  510. if (vl.size() < 2)
  511. continue;
  512. if (vl.size() == 3 && vl.at(2).isObject())
  513. {
  514. // platform options
  515. QJsonObject options = vl.at(2).toObject();
  516. platform = platformMaskFromObject(options);
  517. }
  518. if ((platform & Utils::CurrentPlatform()) == Utils::CurrentPlatform())
  519. setting->addPossibleValue(vl.at(1).toString(), vl.at(0).toVariant());
  520. }
  521. }
  522. section->registerSetting(setting);
  523. }
  524. m_sections.insert(sectionName, section);
  525. }
  526. /////////////////////////////////////////////////////////////////////////////////////////
  527. int SettingsComponent::platformMaskFromObject(const QJsonObject& object)
  528. {
  529. int platformMask = PLATFORM_ANY;
  530. // only include the listed platforms
  531. if (object.contains("platforms"))
  532. {
  533. // start by resetting it to no platforms
  534. platformMask = PLATFORM_UNKNOWN;
  535. QJsonValue platforms = object.value("platforms");
  536. // platforms can be both array or a single string
  537. if (platforms.isArray())
  538. {
  539. for(auto pl : platforms.toArray())
  540. {
  541. if (!pl.isString())
  542. continue;
  543. platformMask |= platformFromString(pl.toString());
  544. }
  545. }
  546. else
  547. {
  548. platformMask = platformFromString(platforms.toString());
  549. }
  550. }
  551. else if (object.contains("platforms_excluded"))
  552. {
  553. QJsonValue val = object.value("platforms_excluded");
  554. if (val.isArray())
  555. {
  556. for(auto pl : val.toArray())
  557. {
  558. if (!pl.isString())
  559. continue;
  560. platformMask &= ~platformFromString(pl.toString());
  561. }
  562. }
  563. else
  564. {
  565. platformMask &= ~platformFromString(val.toString());
  566. }
  567. }
  568. return platformMask;
  569. }
  570. /////////////////////////////////////////////////////////////////////////////////////////
  571. Platform SettingsComponent::platformFromString(const QString& platformString)
  572. {
  573. if (platformString == "osx")
  574. return PLATFORM_OSX;
  575. else if (platformString == "windows")
  576. return PLATFORM_WINDOWS;
  577. else if (platformString == "linux")
  578. return PLATFORM_LINUX;
  579. else if (platformString == "oe")
  580. return PLATFORM_OE;
  581. else if (platformString == "oe_rpi")
  582. return PLATFORM_OE_RPI;
  583. else if (platformString == "oe_x86")
  584. return PLATFORM_OE_X86;
  585. else if (platformString == "any")
  586. return PLATFORM_ANY;
  587. return PLATFORM_UNKNOWN;
  588. }
  589. /////////////////////////////////////////////////////////////////////////////////////////
  590. bool SettingsComponent::componentInitialize()
  591. {
  592. if (!loadDescription())
  593. return false;
  594. // Must be called before we possibly write the config file.
  595. setupVersion();
  596. load();
  597. // add our AudioSettingsController that will inspect audio settings and react.
  598. // then run the signal the first time to make sure that we set the proper visibility
  599. // on the items from the start.
  600. //
  601. auto ctrl = new AudioSettingsController(this);
  602. QVariantMap val;
  603. val.insert("devicetype", value(SETTINGS_SECTION_AUDIO, "devicetype"));
  604. ctrl->valuesUpdated(val);
  605. connect(ctrl, &AudioSettingsController::settingsUpdated, this, &SettingsComponent::groupUpdate);
  606. return true;
  607. }
  608. /////////////////////////////////////////////////////////////////////////////////////////
  609. void SettingsComponent::setupVersion()
  610. {
  611. QSettings settings;
  612. m_oldestPreviousVersion = settings.value(OLDEST_PREVIOUS_VERSION_KEY).toString();
  613. if (m_oldestPreviousVersion.isEmpty())
  614. {
  615. // Version key was not present. It could still be a pre-1.1 PMP install,
  616. // so here we try to find out whether this is the very first install, or
  617. // if an older one exists.
  618. QFile configFile(Paths::dataDir("jellyfinmediaplayer.conf"));
  619. if (configFile.exists())
  620. m_oldestPreviousVersion = "legacy";
  621. else
  622. m_oldestPreviousVersion = Version::GetVersionString();
  623. settings.setValue(OLDEST_PREVIOUS_VERSION_KEY, m_oldestPreviousVersion);
  624. }
  625. }
  626. /////////////////////////////////////////////////////////////////////////////////////////
  627. bool SettingsComponent::resetAndSaveOldConfiguration()
  628. {
  629. QFile settingsFile(Paths::dataDir("jellyfinmediaplayer.conf"));
  630. return settingsFile.rename(Paths::dataDir("jellyfinmediaplayer.conf.old"));
  631. }
  632. /////////////////////////////////////////////////////////////////////////////////////////
  633. QString SettingsComponent::getWebClientUrl(bool desktop)
  634. {
  635. QString url;
  636. url = SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl_desktop").toString();
  637. if (url == "bundled")
  638. {
  639. auto path = Paths::webClientPath("desktop");
  640. url = "file:///" + path;
  641. }
  642. QLOG_DEBUG() << "Using web-client URL: " << url;
  643. return url;
  644. }
  645. /////////////////////////////////////////////////////////////////////////////////////////
  646. QString SettingsComponent::getExtensionPath()
  647. {
  648. QString url;
  649. url = SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl_extension").toString();
  650. if (url == "bundled")
  651. {
  652. auto path = Paths::webExtensionPath("extension");
  653. url = path;
  654. }
  655. return url;
  656. }
  657. /////////////////////////////////////////////////////////////////////////////////////////
  658. QString SettingsComponent::getClientName()
  659. {
  660. QString name;
  661. name = SettingsComponent::Get().value(SETTINGS_SECTION_SYSTEM, "systemname").toString();
  662. if (name.compare("JellyfinMediaPlayer") == 0) {
  663. name = Utils::ComputerName();
  664. }
  665. return name;
  666. }
  667. /////////////////////////////////////////////////////////////////////////////////////////
  668. bool SettingsComponent::ignoreSSLErrors()
  669. {
  670. return SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "ignoreSSLErrors").toBool();
  671. }
  672. /////////////////////////////////////////////////////////////////////////////////////////
  673. void SettingsComponent::setCommandLineValues(const QStringList& values)
  674. {
  675. QLOG_DEBUG() << values;
  676. for (const QString& value : values)
  677. {
  678. if (value == "fullscreen")
  679. setValue(SETTINGS_SECTION_MAIN, "fullscreen", true);
  680. else if (value == "windowed")
  681. setValue(SETTINGS_SECTION_MAIN, "fullscreen", false);
  682. else if (value == "desktop")
  683. setValue(SETTINGS_SECTION_MAIN, "layout", "desktop");
  684. else if (value == "tv")
  685. setValue(SETTINGS_SECTION_MAIN, "layout", "tv");
  686. }
  687. }