Fixed the permissions for all sources files
[kfluid:loureiro-clone.git] / src / xmlfavorite.cpp
1 /*
2  *  Copyright (c) 2009 Zagaia Project - INdT/Fucapi
3  *  Website: http://www.zagaia.org
4  *
5  *  This file is part of kfluid.
6  *
7  *  kfluid is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  kfluid is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with kfluid. If not, see <http://www.gnu.org/licenses/>
19  *
20  */
21
22
23 #include "xmlfavorite.h"
24
25 /*
26  * XMLFavorite constructor
27  */
28 XMLFavorite::XMLFavorite()
29 {
30         setFileName("bookmarks");
31 }
32
33 /*
34  * XMLFavorite destructor
35  */
36 XMLFavorite::~XMLFavorite()
37 {}
38
39 /*
40  * creates a bookmark.xml file through a list of objects
41  * paramters objects
42  */
43 void XMLFavorite::writeObjects(QList<FluidDeviceModel *> objects)
44 {
45
46         if(!objects.isEmpty())
47         {
48                 startWriteToFile();
49
50                 for(int i = 0; i < objects.size(); i++)
51                 {
52                         if (objects.at(i)->isFavorite())
53                         {
54                                 startOfTag("Bluetooth");
55                                 addElement("Name", objects.at(i)->getName());
56                                 addElement("Address", objects.at(i)->getAddress());
57                                 addElement("Icon", objects.at(i)->getIcon());
58                                 endOfTag();
59                         }
60                 }
61
62                 endOfTag();
63                 _xml.close();
64         }
65         else
66         {
67             if (_xml.exists())
68                     _xml.remove();
69         }
70 }
71
72 /*
73  * extracts objects from a bookmark.xml
74  */
75 QList<FluidDeviceModel *> XMLFavorite::extractObjects()
76 {
77     QList<FluidDeviceModel *> _favlist;
78
79     if(_xml.exists())
80     {
81         _xml.open(QIODevice::ReadOnly);
82
83         _xmlReader.setDevice(&_xml);
84         _xmlReader.readNext();
85
86         while(!_xmlReader.atEnd())
87         {
88             if(_xmlReader.isStartElement() && _xmlReader.name().toString().contains("Name", Qt::CaseSensitive))
89             {
90                 FluidDeviceModel *device = new FluidDeviceModel();
91
92                 device->setName(_xmlReader.readElementText());
93                 _xmlReader.readNext();
94                 _xmlReader.readNext();
95                 device->setAddress(_xmlReader.readElementText());
96                 _xmlReader.readNext();
97                 _xmlReader.readNext();
98                 device->setIcon(_xmlReader.readElementText());
99                 device->markAsFavorite();
100
101                 _favlist.append(device);
102             }
103             _xmlReader.readNext();
104         }
105
106         _xml.close();
107
108         return _favlist;
109     }
110     else
111         return _favlist;
112 }