1 #include "profilesmanager.h"
2 #include "profiledeamon.h"
3 #include <QtCore/QDebug> //Debug pro informace
4 #include <QtXml/QDomElement>
5 #include <QtCore/QStringList>
6 #include <QtCore/QTextStream>
7 #include <QtCore/QString>
9 #include <QtGui/QStandardItemModel>
10 ProfilesManager::ProfilesManager(QObject *parent) :
16 Creates an object ProfileDeamon and connects via ProfileDeamon: ConnectToDeamon
17 Create a model that will list all audio files
18 Then initializes the XML file using InitFile()
19 If the file exists so it attempts to read with readfile()
20 and if not it creates and initializes using CreateFile()
22 bool ProfilesManager::Init()
25 if(profile_deamon->ConnectToDeamon() == false)
27 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot connect";
30 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "bude init file ================= ";
32 if(InitFile() == false) {
37 if(ReadFile() == true){
38 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "File read";
39 emit s_profiles_name_change(GetProfilesNames());
42 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "neco spatne ve sceni souborů================= ";
46 if(CreateFile() == true){
47 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "file created";
49 Profil general = profile_deamon->GetProfile("general");
50 Profil silent = profile_deamon->GetProfile("silent");
51 CreateProfile(&general);
52 CreateProfile(&silent);
53 qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "added two profiles general silent ";
54 emit s_profiles_name_change(GetProfilesNames());
62 //=============================================================
64 Opens home folder and check if there is an dir NProfile and if not that it creates it
65 and set the file to Dir.filePath(profiles.xml)
67 bool ProfilesManager::InitFile()
69 QDir Dir = QDir::homePath();
71 //vytvori slozku pokud neni
72 if(!Dir.exists("NProfile")){
73 if(!Dir.mkdir("NProfile")){
74 qWarning() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "Cannot create folder NProfile in home";
78 //switches to a folder
79 if(!Dir.cd("NProfile")){
80 qWarning() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "cannot cd in NProfile";
84 file.setFileName(Dir.filePath("profiles.xml")); //set
89 Opens a file and reads the XML document using domDocument.setContent and check it with checkDomDoc ()
91 bool ProfilesManager::ReadFile()
93 if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
94 //////qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "Cannot open file";
100 if (!domDocument.setContent((QIODevice * )&file, false, &errorStr, &errorLine,
102 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot set content " << errorStr << "line " << errorLine << "Column " << errorColumn;
109 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Error in document, this is not calendar file ";
119 Open the file and create XML using createXmlText () and write to a file.
120 Sets the document and check with domDocument.setContent checkDomDoc ()
123 bool ProfilesManager::CreateFile()
125 if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
126 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot open file";
132 QTextStream out(&file);
133 QString text = CreateXmlText(); //create text xml
134 out.setCodec("UTF-8");//set utf 8
135 out.setAutoDetectUnicode(true);
139 if (!domDocument.setContent(text, false, &errorStr, &errorLine,
141 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot set content " << errorStr << "line " << errorLine << "Column " << errorColumn;
147 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Error in document, this is not calendar file ";
158 QString ProfilesManager::CreateXmlText()
161 text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
162 text += "<!DOCTYPE NProf>\n";
163 text +="<NProf version=\"1.0\">\n";//začátek
164 text += "</NProf>\n";//konec souboru
169 Checks if the document is in order
171 bool ProfilesManager::CheckDomDoc()
173 QDomElement root = domDocument.documentElement();
174 if (root.tagName() != "NProf") {
175 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Error in root";
177 } else if (root.hasAttribute("version")
178 && root.attribute("version") != "1.0") {
179 //////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Error in version";
186 //=============================================================================================================
187 //http://www.decompile.com/cpp/faq/file_and_line_error_string.htm
188 // pro //////qDebug() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ <<
190 /** SaveProfilesToXml
191 Saves the document to a file
193 void ProfilesManager::SaveProfilesToXml()
196 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
197 //////qDebug() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "nejde otevrit soubor";
199 QTextStream out(&file);
200 out.setCodec("UTF-8");
201 out.setAutoDetectUnicode(true);
202 out << domDocument.toString();
204 file.close();//close file
208 Returns a list of names of profiles
210 QStringList ProfilesManager::GetProfilesNames()
212 QStringList profiles_names = QStringList();
213 QDomElement root = domDocument.documentElement();
214 QDomElement first = root.firstChildElement();
215 while(!first.isNull()) {
216 profiles_names.append( first.tagName());
217 first = first.nextSiblingElement();
219 return profiles_names;
221 //=============================================================================================================
225 QDomElement ProfilesManager::GetRootElement() const
227 return domDocument.documentElement();
231 Returns an object filled with data
232 \param profil Pointer to object to the data of the profile
234 bool ProfilesManager::GetProfile(Profil * profil)
236 QDomElement root = domDocument.documentElement();
237 QDomElement profil_xml = root.firstChildElement(profil->GetName()); // find profile
239 if(profil_xml.isNull()) return false;// if not found return
241 QDomElement n = profil_xml.firstChildElement(); //ziskám první prvek
242 ////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Get profile from xml";
246 ////qDebug() << "Element name and text: " << n.tagName() << n.text();
247 profil->SetValue(n.tagName(), n.text());
248 n = n.nextSiblingElement();
250 ////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " return";
255 Saves profile data to XML document and emit a signal, added or delete profile
256 \param Data Pointer to an object with data of the profile
258 bool ProfilesManager::CreateProfile(Profil *Data)
260 QDomElement root = domDocument.documentElement();
261 QDomElement ui = CreateNewProfile(Data);
262 root.appendChild(ui);
263 emit s_profiles_name_change(GetProfilesNames());
268 Changes in the profile data
269 \param Data Pointer to an object with data on the profile
271 void ProfilesManager::UpdateProfile(Profil * Data )
273 QDomElement root = domDocument.documentElement();
274 QDomElement profil_old = root.firstChildElement(Data->GetName());
275 QDomElement profil_new = this->CreateNewProfile(Data);
276 root.replaceChild(profil_new, profil_old);
277 emit s_profile_update(Data->GetName());
282 \param profile_name name of profile
284 void ProfilesManager::DeleteProfile(QString profile_name)
286 QDomElement root = domDocument.documentElement();
287 root.removeChild(root.firstChildElement(profile_name)); // find element end delete
288 ////qDebug() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "smazan a emit";
289 emit s_delete_Profiles_Name_From_Models(profile_name);
290 emit s_profiles_name_change(GetProfilesNames());
295 Creates records in the XLM document
296 \param Data Pointer to an object with data on the profile
298 QDomElement ProfilesManager::CreateNewProfile( Profil *Data)
300 QDomElement ui = domDocument.createElement(Data->GetName()); //create element
301 QList<ProfileValue *>* p_profile_values = Data->ListOfValues();
302 for (int i = 0; i < p_profile_values->size(); ++i)
304 CreateNode(&ui, p_profile_values->at(i)->key, p_profile_values->at(i)->type, p_profile_values->at(i)->value.toString());
310 Creates an item in the XML file and adds it to the record
311 \param profil Pointer to the element in xml
313 \param attribute attribute of item
314 \param text text in item
316 void ProfilesManager::CreateNode(QDomElement * profil, QString name, QString attribute, QString text)
318 QDomElement ProfilData = domDocument.createElement(name);
319 if(!attribute.isEmpty()){
320 ProfilData.setAttribute("type", attribute );
322 QDomText newTitleText = domDocument.createTextNode(text);
323 ProfilData.appendChild(newTitleText);
324 profil->appendChild(ProfilData);
328 Prints all profiles, !!! only for testing purposes
330 void ProfilesManager::TestPrint()
332 //////qDebug() << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << " test print in DB";
333 if(!domDocument.isNull()){
334 QDomElement root = domDocument.documentElement();
335 QDomElement first = root.firstChildElement();
337 //list of all profile
338 while(!first.isNull()) {
339 //////qDebug() << "|Jmeno profilu" << first.tagName();
340 chield = first.firstChildElement();
341 while(!chield.isNull()){
342 //////qDebug() << "->>> atribut:"<< chield.tagName() << " hodnota: " << chield.text();
343 chield = chield.nextSiblingElement();
345 first = first.nextSiblingElement();
350 /** SetProfile(QString str_profile)
353 void ProfilesManager::SetProfile(QString str_profile)
355 ////qDebug() << "In file:" << __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Set profile into mobile-------------------" << str_profile;
356 Profil * profil = new Profil(str_profile);
357 if(!GetProfile(profil))
361 profile_deamon->SetProfile(profil);