SilentEye 0.4.1
|
00001 // This file is part of SilentEye. 00002 // 00003 // SilentEye is free software: you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation, either version 3 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // SilentEye is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with SilentEye. If not, see <http://www.gnu.org/licenses/>. 00015 00016 #include "config.h" 00017 00018 namespace SilentEyeFramework { 00019 00020 Config::Config(QObject* parent) 00021 : QObject(parent) 00022 { 00023 setObjectName("Config"); 00024 m_logger = new Logger(this); 00025 m_isLoaded = false; 00026 m_filename = "se-noname.conf"; 00027 m_filepath = "/tmp/"; 00028 } 00029 00030 Config::Config(QString filePath, QString filename, 00031 bool hasExt, QObject* parent) 00032 : QObject(parent) 00033 { 00034 setObjectName("Config"); 00035 m_logger = new Logger(this); 00036 if (hasExt) { 00037 m_filename = filename; 00038 } else { 00039 m_filename = filename+".conf"; 00040 } 00041 00042 m_filepath = filePath; 00043 00044 m_isLoaded = load(); 00045 } 00046 00047 Config::Config(const QString& content, QObject* parent) 00048 : QObject(parent) 00049 { 00050 setObjectName("Config"); 00051 m_logger = new Logger(this); 00052 m_filename = "se-noname.conf"; 00053 m_filepath = "/tmp/"; 00054 00055 m_content = content; 00056 m_isLoaded = load(); 00057 } 00058 00059 Config::Config(const Config& config) 00060 { 00061 setObjectName("Config"); 00062 m_logger = new Logger(this); 00063 m_filename = config.filename(); 00064 m_valueMap = config.values(); 00065 } 00066 00067 Config::~Config(){ 00068 if (!m_logger.isNull()) 00069 { 00070 delete m_logger; 00071 } 00072 } 00073 00074 QString Config::filename() const 00075 { 00076 return m_filename; 00077 } 00078 00079 QString Config::filepath() const 00080 { 00081 return m_filepath; 00082 } 00083 00084 QMap<QString, QString> Config::values() const 00085 { 00086 return m_valueMap; 00087 } 00088 00089 bool Config::isLoaded() const 00090 { 00091 return m_isLoaded; 00092 } 00093 00094 bool Config::contains(QString name) const 00095 { 00096 return m_valueMap.contains(name); 00097 } 00098 00099 void Config::set(QString name, QString value) 00100 { 00101 m_valueMap[name] = value; 00102 } 00103 00104 QString Config::get(QString name) const 00105 { 00106 if (m_valueMap.contains(name)) 00107 return m_valueMap.value(name); 00108 else 00109 return QString(); 00110 } 00111 00112 void Config::save() 00113 { 00114 QDomDocument doc( "SilentEye" ); 00115 QDomElement root = doc.createElement("configuration"); 00116 doc.appendChild(root); 00117 00118 QMap<QString, QString>::const_iterator i = m_valueMap.constBegin(); 00119 while (i != m_valueMap.constEnd()) { 00120 QDomElement conf = doc.createElement(i.key()); 00121 conf.appendChild(doc.createTextNode(i.value())); 00122 root.appendChild(conf); 00123 ++i; 00124 } 00125 00126 /* open the output file */ 00127 QFile file(m_filepath+m_filename); 00128 if( !file.open( QIODevice::WriteOnly ) ) 00129 { 00130 m_logger->warning("Can't save configuration file to " 00131 + m_filepath + m_filename); 00132 return; 00133 } 00134 00135 /* write data to the output file */ 00136 QTextStream ts( &file ); 00137 /* Profile serialize datas */ 00138 ts << doc.toString(); 00139 file.close(); 00140 } 00141 00142 bool Config::load() 00143 { 00144 QDomDocument doc( "SilentEye" ); 00145 00146 if (!m_content.isEmpty()) { 00147 if(!doc.setContent( m_content ) ) 00148 { 00149 m_logger->warning("Can't load buffer content to xml document: " 00150 + m_content); 00151 return false; 00152 } 00153 } else { 00154 QFile file(m_filepath+m_filename); 00155 if(file.open( QIODevice::ReadOnly ) ) 00156 { 00157 /* load file content to xml document */ 00158 if(!doc.setContent( &file ) ) 00159 { 00160 m_logger->warning("Can't load file content to xml document: " 00161 + m_filepath + m_filename); 00162 file.close(); 00163 return false; 00164 } 00165 file.close(); 00166 } 00167 else 00168 { 00169 m_logger->warning("Can't open configuration file: " 00170 + m_filepath + m_filename); 00171 return false; 00172 } 00173 } 00174 00175 QDomElement root = doc.documentElement(); 00176 00177 QDomNode n = root.firstChild(); 00178 00179 while(!n.isNull()) 00180 { 00181 QDomElement e = n.toElement(); 00182 if( !e.isNull() ) 00183 { 00184 m_valueMap[e.tagName()] = e.text(); 00185 } 00186 n = n.nextSibling(); 00187 } 00188 return true; 00189 } 00190 00191 Config& Config::operator=(const Config& config) 00192 { 00193 m_filename = config.filename(); 00194 m_filepath = config.filepath(); 00195 m_valueMap = config.values(); 00196 return *this; 00197 } 00198 00199 }