1 /* 2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> 3 * (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com> 4 * 5 * This file is part of lsp-plugins 6 * Created on: 21 июн. 2017 г. 7 * 8 * lsp-plugins is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * any later version. 12 * 13 * lsp-plugins is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License 19 * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22 #include <ui/ui.h> 23 #include <ui/serialize.h> 24 #include <ui/XMLNode.h> 25 #include <ui/XMLHandler.h> 26 #include <core/files/xml/PushParser.h> 27 28 namespace lsp 29 { 30 using namespace tk; 31 32 class lsp_theme_handler: public XMLNode 33 { 34 protected: 35 LSPTheme *pTheme; 36 37 public: lsp_theme_handler(LSPTheme * theme)38 explicit lsp_theme_handler(LSPTheme *theme) 39 { 40 pTheme = theme; 41 } 42 completed(XMLNode * child)43 virtual status_t completed(XMLNode *child) 44 { 45 if (child != NULL) 46 delete child; 47 return STATUS_OK; 48 } 49 }; 50 51 class lsp_theme_color_handler: public lsp_theme_handler 52 { 53 public: lsp_theme_color_handler(LSPTheme * theme)54 explicit lsp_theme_color_handler(LSPTheme *theme) : lsp_theme_handler(theme) {} 55 56 public: start_element(XMLNode ** child,const LSPString * name,const LSPString * const * atts)57 virtual status_t start_element(XMLNode **child, const LSPString *name, const LSPString * const *atts) 58 { 59 const LSPString *value = find_attribute(atts, "value"); 60 if (value != NULL) 61 { 62 if (!pTheme->add_color(name->get_utf8(), value->get_utf8())) 63 return STATUS_NO_MEM; 64 return STATUS_OK; 65 } 66 67 lsp_error("\"value\" attribute expected for element <%s>", name->get_utf8()); 68 return STATUS_CORRUPTED; 69 } 70 }; 71 72 class lsp_theme_body_handler: public lsp_theme_handler 73 { 74 public: lsp_theme_body_handler(LSPTheme * theme)75 explicit lsp_theme_body_handler(LSPTheme *theme) : lsp_theme_handler(theme) {} 76 77 public: start_element(XMLNode ** child,const LSPString * name,const LSPString * const * atts)78 virtual status_t start_element(XMLNode **child, const LSPString *name, const LSPString * const *atts) 79 { 80 if (name->equals_ascii("colors")) 81 { 82 if ((*child = new lsp_theme_color_handler(pTheme)) == NULL) 83 return STATUS_NO_MEM; 84 return STATUS_OK; 85 } 86 87 lsp_error("unexpected element <%s>", name->get_utf8()); 88 return STATUS_CORRUPTED; 89 } 90 }; 91 92 class lsp_theme_root_handler: public lsp_theme_handler 93 { 94 public: lsp_theme_root_handler(LSPTheme * theme)95 explicit lsp_theme_root_handler(LSPTheme *theme) : lsp_theme_handler(theme) {} 96 97 public: start_element(XMLNode ** child,const LSPString * name,const LSPString * const * atts)98 virtual status_t start_element(XMLNode **child, const LSPString *name, const LSPString * const *atts) 99 { 100 if (name->equals_ascii("theme")) 101 { 102 if ((*child = new lsp_theme_body_handler(pTheme)) == NULL) 103 return STATUS_NO_MEM; 104 return STATUS_OK; 105 } 106 107 lsp_error("expected root element <theme>, received: <%s>", name->get_utf8()); 108 return STATUS_CORRUPTED; 109 } 110 completed(XMLNode * child)111 virtual status_t completed(XMLNode *child) 112 { 113 if (child != NULL) 114 delete child; 115 return STATUS_OK; 116 } 117 }; 118 load_theme(tk::LSPTheme * theme,const LSPString * uri)119 status_t load_theme(tk::LSPTheme *theme, const LSPString *uri) 120 { 121 lsp_theme_root_handler root(theme); 122 XMLHandler handler; 123 124 status_t res = handler.parse(uri, &root); 125 if (res == STATUS_OK) 126 res = theme->after_load(); 127 return res; 128 } 129 load_theme(tk::LSPTheme * theme,const char * uri)130 status_t load_theme(tk::LSPTheme *theme, const char *uri) 131 { 132 lsp_theme_root_handler root(theme); 133 XMLHandler handler; 134 status_t res = handler.parse(uri, &root); 135 if (res == STATUS_OK) 136 res = theme->after_load(); 137 return res; 138 } 139 } 140 141 142