config.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "app.h"
  2. #include "filesystem.h"
  3. AConfig::AConfig() {
  4. conffile = QApplication::applicationDirPath() + "/settings.ini";
  5. //conffile = "../settings.ini";
  6. }
  7. AConfig::AConfig(const AConfig&) {
  8. throw QException(); // Выкидываем QException, в случае если
  9. // вообще попытались скопировать объект
  10. }
  11. AConfig& AConfig::operator=(AConfig&) {
  12. throw QException(); // Выкидываем QException, в случае если
  13. // вообще попытались скопировать объект
  14. }
  15. void AConfig::saveConfig(){
  16. QSettings settings( conffile, QSettings::IniFormat );
  17. //Global
  18. settings.setValue("Global/current_theme", "lotro");
  19. settings.setValue("Global/default_theme", "lotro");
  20. //Editor
  21. settings.setValue("Editor/texts", true);
  22. settings.setValue("Editor/fonts", true);
  23. settings.setValue("Editor/images", true);
  24. settings.setValue("Editor/sounds", true);
  25. settings.setValue("Editor/videos", false);
  26. settings.setValue("Editor/screens", true);
  27. settings.setValue("Editor/textures", true);
  28. //Network
  29. settings.setValue("Network/server", "http://translate.lotros.ru");
  30. settings.setValue("Network/coupon", "http://translate.lotros.ru/coupon");
  31. settings.setValue("Network/servers", "http://translate.lotros.ru/servers");
  32. settings.setValue("Network/foot", "http://translate.lotros.ru/profmessage");
  33. settings.setValue("Network/news", "http://translate.lotros.ru/groupware/launcher_news");
  34. settings.setValue("Network/launcher", "http://translate.lotros.ru/upload/launcher");
  35. settings.setValue("Network/releases", "http://translate.lotros.ru/groupware/check_updates");
  36. settings.setValue("Network/info", "http://translate.lotros.ru/paths");
  37. settings.setValue("Network/update", "http://translate.lotros.ru/groupware/update");
  38. // Values
  39. settings.setValue("Values/coupon", "");
  40. settings.setValue("Values/servers", "");
  41. // Local
  42. settings.setValue("Local/file", "client_local_English.dat");
  43. //Datetime
  44. settings.setValue("Datetime/texts", true);
  45. settings.setValue("Datetime/fonts", true);
  46. settings.setValue("Datetime/images", true);
  47. settings.setValue("Datetime/sounds", true);
  48. settings.setValue("Datetime/videos", false);
  49. settings.setValue("Datetime/loadscreens", true);
  50. settings.setValue("Datetime/textures", true);
  51. //Updates
  52. settings.setValue("Updates/update", false);
  53. settings.setValue("Updates/micro", false);
  54. settings.sync();
  55. }
  56. void AConfig::loadConfig(){
  57. App *app = &App::getInstance();
  58. app->logSectionStart("Загрузка конфигурации");
  59. qInfo("%s:%i: %s", __FILE__, __LINE__, "Находим каталог с игрой");
  60. app->window->ui->lotropathLabel->setText(app->config->getValue("Local", "folder") != "-1" ? app->config->getValue("Local", "folder") : "Выберите папку с игрой");
  61. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Шрифты\"");
  62. app->window->ui->checkFonts->setChecked(app->config->getValue("Editor", "fonts") == "true" ? true : false);
  63. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Тексты\"");
  64. app->window->ui->checkTexts->setChecked(app->config->getValue("Editor", "texts")== "true" ? true : false);
  65. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Карты\"");
  66. app->window->ui->checkMaps->setChecked(app->config->getValue("Editor", "images") == "true" ? true : false);
  67. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Звуки\"");
  68. app->window->ui->checkSounds->setChecked(app->config->getValue("Editor", "sounds")== "true" ? true : false);
  69. app->window->ui->checkVideos->setChecked(app->config->getValue("Editor", "videos") == "true" ? true : false);
  70. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Заставки\"");
  71. app->window->ui->checkScreens->setChecked(app->config->getValue("Editor", "screens")== "true" ? true : false);
  72. qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность микрообновлений");
  73. app->window->ui->checkMicro->setChecked(app->config->getValue("Updates", "micro")== "true" ? true : false);
  74. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  75. app->logSectionEnd();
  76. }
  77. QString AConfig::getSkinFile(){
  78. App *app = &App::getInstance();
  79. QString current_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "current_theme") + "/skin.ini";
  80. QString default_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "default_theme") + "/skin.ini";
  81. QString skinfile = FileSystem::fileExists(current_theme) ? current_theme : default_theme;
  82. return skinfile;
  83. }
  84. QString AConfig::getSkinName(){
  85. App *app = &App::getInstance();
  86. QString current_theme = app->config->getValue("Global", "current_theme");
  87. QString default_theme = app->config->getValue("Global", "default_theme");
  88. QString path = QApplication::applicationDirPath() + "/styles/" + current_theme + "/skin.ini";
  89. QString skinname = FileSystem::fileExists(path) ? current_theme : default_theme;
  90. return skinname;
  91. }
  92. void AConfig::loadSkin(){
  93. App *app = &App::getInstance();
  94. app->logSectionStart("Загружаем скин лаунчера");
  95. qInfo("%s:%i: %s", __FILE__, __LINE__, "Определяем активную тему оформления");
  96. QSettings settings( getSkinFile(), QSettings::IniFormat );
  97. QStringList items = settings.childGroups();
  98. QVariant k;
  99. QPushButton butt;
  100. QLabel lab;
  101. qInfo("%s:%i: %s", __FILE__, __LINE__, "Начинаем настройку элементов");
  102. foreach (QString item, items){
  103. qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Устанавливаем настройки для ", item.toLocal8Bit().data());
  104. QWidget* obj = app->window->findChild<QWidget*>(item);
  105. if (obj != nullptr){
  106. obj->installEventFilter(app->window);
  107. settings.beginGroup(item);
  108. if(settings.value("left").toString() != "-1"){
  109. obj->setGeometry(QRect(
  110. settings.value("left").toInt(),
  111. settings.value("top").toInt(),
  112. settings.value("width").toInt(),
  113. settings.value("height").toInt()
  114. ));
  115. }
  116. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": настройка геометрии завершена");
  117. if(settings.value("shadow") != k){
  118. QString color = settings.value("shcolor") != k ? settings.value("shcolor").toString() : "#222";
  119. int blur = settings.value("shblur") != k ? settings.value("shblur").toInt() : 2;
  120. int offset_x = settings.value("shx") != k ? settings.value("shx").toInt() : 1;
  121. int offset_y = settings.value("shy") != k ? settings.value("shy").toInt() : 1;
  122. app->helper->applyShadow(obj, color, blur, offset_x, offset_y);
  123. }
  124. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": настройка теней завершена");
  125. if(settings.value("text") != k){
  126. QString text = settings.value("text").toString();
  127. if(text == "del") text = "";
  128. if(obj->metaObject()->className() == butt.metaObject()->className()){
  129. QPushButton * b = dynamic_cast<QPushButton* >(obj);
  130. b->setText(text);
  131. }
  132. if(obj->metaObject()->className() == lab.metaObject()->className()){
  133. QLabel * l = dynamic_cast<QLabel* >(obj);
  134. l->setText(settings.value("text").toString());
  135. }
  136. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": текст установлен");
  137. }
  138. if(settings.value("align") != k){
  139. QLabel * lab = dynamic_cast<QLabel* >(obj);
  140. if(settings.value("align") == "left") lab->setAlignment(Qt::AlignLeft);
  141. if(settings.value("align") == "center") lab->setAlignment(Qt::AlignCenter);
  142. if(settings.value("align") == "right") lab->setAlignment(Qt::AlignRight);
  143. }
  144. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": настройки позиционирования применены");
  145. settings.endGroup();
  146. } else {
  147. if(item == "mainWindow") {
  148. settings.beginGroup(item);
  149. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": обработка параметров главного окна");
  150. int w = settings.value("width").toInt();
  151. int h = settings.value("height").toInt();
  152. QDesktopWidget desktop;
  153. QRect rect = desktop.availableGeometry(desktop.primaryScreen()); // прямоугольник с размерами экрана
  154. QPoint center = rect.center(); //координаты центра экрана
  155. int x = center.x() - (w/2); // учитываем половину ширины окна
  156. int y = center.y() - (h/2); // .. половину высоты
  157. center.setX(x);
  158. center.setY(y);
  159. app->window->move(center);
  160. settings.endGroup();
  161. }
  162. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": закончен просчет геометрии");
  163. if(item == "htmlColors") {
  164. settings.beginGroup(item);
  165. foreach(QString key, settings.allKeys()){
  166. app->helper->htmlColors.insert(key, settings.value(key).toString());
  167. }
  168. settings.endGroup();
  169. }
  170. qInfo("%s:%i: %s%s", __FILE__, __LINE__, item.toLocal8Bit().data(), ": загружены HTML-цвета");
  171. }
  172. }
  173. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  174. app->logSectionEnd();
  175. }
  176. QString AConfig::getValue(QString section, QString key){
  177. QSettings settings( conffile, QSettings::IniFormat );
  178. settings.beginGroup(section);
  179. key = settings.value(key, -1).toString();
  180. settings.endGroup();
  181. return key;
  182. }
  183. void AConfig::setValue(QString section, QString key, QString value){
  184. QSettings settings( conffile, QSettings::IniFormat );
  185. settings.beginGroup(section);
  186. settings.setValue(key, value);
  187. settings.sync();
  188. }
  189. void AConfig::deleteKey(QString section, QString key){
  190. QSettings settings( conffile, QSettings::IniFormat );
  191. QStringList keys = settings.childKeys();
  192. settings.beginGroup(section);
  193. if (keys.contains(key)){ settings.remove(key); }
  194. settings.endGroup();
  195. }
  196. void AConfig::deleteSection(QString section){
  197. QSettings settings( conffile, QSettings::IniFormat );
  198. settings.beginGroup(section);
  199. settings.remove("");
  200. settings.endGroup();
  201. }
  202. QStringList AConfig::getLotroPath(){
  203. QStringList paths;
  204. //#if defined(Q_WS_WIN)
  205. // Windows 7
  206. QSettings m("HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache", QSettings::NativeFormat);
  207. foreach (QString key, m.allKeys()) {
  208. if(key.contains("TurbineLauncher.exe") && FileSystem::fileExists(key)){
  209. paths.append(key.replace("/TurbineLauncher.exe", ""));
  210. }
  211. }
  212. // Windows 8, 10
  213. QSettings n("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\12bbe590-c890-11d9-9669-0800200c9a66_is1", QSettings::NativeFormat);
  214. foreach (QString key, n.allKeys()) {
  215. qDebug() << key;
  216. if(key.contains("InstallLocation") || key.contains("installlocation")){
  217. QString folder = n.value(key).toString().replace("\\", "/").replace("/TurbineLauncher.exe", "").replace("\"", "");
  218. if(FileSystem::fileExists(folder + "/TurbineLauncher.exe")) paths.append(folder);
  219. }
  220. }
  221. //#else
  222. // Реализация под Linux
  223. //#endif
  224. return paths;
  225. }
  226. const char * AConfig::getDatPath(int id){
  227. App *app = &App::getInstance();
  228. QStringList dats;
  229. dats << app->config->getValue("Local", "file") << "client_general.dat" << "client_sound.dat" << "client_surface.dat" << "client_highres.dat";
  230. //std::string file = (app->config->getValue("Local", "folder") + "/" + dats.at(id)).toStdString();
  231. return (app->config->getValue("Local", "folder") + "/" + dats.at(id)).toLocal8Bit().data();
  232. }