README
[n9profile] / profilesmanager.cpp
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>
8 #include "profil.h"
9 #include <QtGui/QStandardItemModel>
10 ProfilesManager::ProfilesManager(QObject *parent) :
11     QObject(parent)
12 {
13 }
14
15 /** Init
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()
21 */
22 bool ProfilesManager::Init()
23 {
24     //connect
25     if(profile_deamon->ConnectToDeamon() == false)
26     {
27         qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot connect";
28         return false;
29     }
30     qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "bude init file ================= ";
31     //open the file
32     if(InitFile() == false) {
33         return false;
34     }
35
36     if(file.exists()) {
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());
40             return true;
41         } else {
42             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "neco spatne ve sceni souborů================= ";
43             return false;
44         }
45     } else {
46         if(CreateFile() == true){
47             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "file created";
48             //(general a silent)
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());
55             return true;
56         } else {
57             return false;
58         }
59     }
60     return false;
61 }
62 //=============================================================
63 /** InitFile
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)
66 */
67 bool ProfilesManager::InitFile()
68 {
69     QDir Dir = QDir::homePath();
70
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";
75             return false;
76         }
77     }
78     //switches to a folder
79     if(!Dir.cd("NProfile")){
80         qWarning() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "cannot cd in NProfile";
81         return false;
82     }
83
84     file.setFileName(Dir.filePath("profiles.xml")); //set
85     return true;
86 }
87
88 /** ReadFile
89 Opens a file and reads the XML document using domDocument.setContent and check it with checkDomDoc ()
90 */
91 bool ProfilesManager::ReadFile()
92 {
93     if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
94         //////qDebug() <<  __FILE__  << __LINE__  << __FUNCTION__ << "Cannot open file";
95         return false;
96     }
97     QString errorStr;
98     int errorLine;
99     int errorColumn;
100     if (!domDocument.setContent((QIODevice * )&file, false, &errorStr, &errorLine,
101                                 &errorColumn)) {
102         //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot set content " << errorStr << "line " << errorLine << "Column " << errorColumn;
103         file.close();
104         return false;
105     }
106
107     //control
108     if(!CheckDomDoc()) {
109         //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Error in document, this is not calendar file ";
110         file.close();
111         return false;
112     }
113     //  print();
114     file.close();
115     return true;
116 }
117
118 /** CreateFile
119 Open the file and create XML using createXmlText () and write to a file.
120 Sets the document and check with domDocument.setContent checkDomDoc ()
121 and closes the file.
122 */
123 bool ProfilesManager::CreateFile()
124 { //otevřu
125     if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
126         //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot open file";
127         return false;
128     }
129     QString errorStr;
130     int errorLine;
131     int errorColumn;
132     QTextStream out(&file);
133     QString text = CreateXmlText(); //create text xml
134     out.setCodec("UTF-8");//set utf 8
135     out.setAutoDetectUnicode(true);
136     out << text;//write
137     out.flush(); //
138
139     if (!domDocument.setContent(text, false, &errorStr, &errorLine,
140                                 &errorColumn)) {
141         //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Cannot set content " << errorStr << "line " << errorLine << "Column " << errorColumn;
142         file.close();
143         return false;
144     }
145     //check
146     if(!CheckDomDoc()) {
147         //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" <<  "Error in document, this is not calendar file  ";
148         file.close();
149         return false;
150     }
151     file.close();//close
152     return true;
153 }
154
155 /** createXmlText
156 Creates the xml text
157 */
158 QString ProfilesManager::CreateXmlText()
159 {
160     QString text("");
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
165     return text;
166 }
167
168 /** checkDomDoc
169 Checks if the document is in order
170 */
171 bool ProfilesManager::CheckDomDoc()
172 {
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";
176         return false;
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";
180         return false;
181     }
182     return true;
183 }
184
185
186 //=============================================================================================================
187 //http://www.decompile.com/cpp/faq/file_and_line_error_string.htm
188 // pro //////qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ <<
189
190 /** SaveProfilesToXml
191 Saves the document to a file
192 */
193 void ProfilesManager::SaveProfilesToXml()
194 {
195
196     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
197         //////qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "nejde otevrit soubor";
198     }
199     QTextStream out(&file);
200     out.setCodec("UTF-8");
201     out.setAutoDetectUnicode(true);
202     out << domDocument.toString();
203     out.flush();
204     file.close();//close file
205 }
206
207 /** GetProfilesNames
208 Returns a list of names of profiles
209 */
210 QStringList ProfilesManager::GetProfilesNames()
211 {
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();
218     }
219     return profiles_names;
220 }
221 //=============================================================================================================
222 /** GetRootElemetn
223   Get root element
224   */
225 QDomElement ProfilesManager::GetRootElement() const
226 {
227     return domDocument.documentElement();
228 }
229
230 /** GetProfile
231 Returns an object filled with data
232  \param profil Pointer to object to the data of the profile
233 */
234 bool ProfilesManager::GetProfile(Profil * profil)
235 {
236     QDomElement root = domDocument.documentElement();
237     QDomElement profil_xml = root.firstChildElement(profil->GetName()); // find profile
238
239     if(profil_xml.isNull()) return false;// if not found return
240
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";
243
244     while(!n.isNull())
245     {
246         ////qDebug() << "Element name and text: " << n.tagName() << n.text();
247         profil->SetValue(n.tagName(), n.text());
248         n = n.nextSiblingElement();
249     }
250     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " return";
251     return true;
252 }
253
254 /** CreateProfile
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
257 */
258 bool ProfilesManager::CreateProfile(Profil *Data)
259 {
260     QDomElement root = domDocument.documentElement();
261     QDomElement ui = CreateNewProfile(Data);
262     root.appendChild(ui);
263     emit  s_profiles_name_change(GetProfilesNames());
264     return true;
265 }
266
267 /** UpdateProfile
268 Changes in the profile data
269  \param Data Pointer to an object with data on the profile
270 */
271 void ProfilesManager::UpdateProfile(Profil * Data )
272 {
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());
278 }
279
280 /** DeleteProfile
281 Delete profile
282  \param profile_name name of profile
283 */
284 void ProfilesManager::DeleteProfile(QString profile_name)
285 {
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());
291 }
292
293
294 /** createNewProfile
295 Creates records in the XLM document
296  \param Data Pointer to an object with data on the profile
297 */
298 QDomElement ProfilesManager::CreateNewProfile( Profil *Data)
299 {
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)
303     {
304         CreateNode(&ui, p_profile_values->at(i)->key, p_profile_values->at(i)->type, p_profile_values->at(i)->value.toString());
305     }
306     return ui;
307 }
308
309 /** createNode
310 Creates an item in the XML file and adds it to the record
311  \param profil Pointer to the element in xml
312  \param name Name
313  \param attribute attribute of item
314  \param text text in item
315 */
316 void ProfilesManager::CreateNode(QDomElement * profil, QString name, QString attribute, QString text)
317 {
318     QDomElement ProfilData = domDocument.createElement(name);
319     if(!attribute.isEmpty()){
320         ProfilData.setAttribute("type", attribute );
321     }
322     QDomText newTitleText = domDocument.createTextNode(text);
323     ProfilData.appendChild(newTitleText);
324     profil->appendChild(ProfilData);
325 }
326
327 /** TestPrint
328   Prints all profiles, !!! only for testing purposes
329 */
330 void ProfilesManager::TestPrint()
331 {
332     //////qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << " test print in DB";
333     if(!domDocument.isNull()){
334         QDomElement root = domDocument.documentElement();
335         QDomElement first =  root.firstChildElement();
336         QDomElement chield;
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();
344             }
345             first = first.nextSiblingElement();
346         }
347     }
348 }
349
350 /** SetProfile(QString str_profile)
351   Sets profile
352 */
353 void ProfilesManager::SetProfile(QString str_profile)
354 {
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))
358     {
359         return;
360     }
361     profile_deamon->SetProfile(profil);
362     delete profil;
363 }
364
365
366