README
[n9profile] / soundfilesmanager.cpp
1 #include "soundfilesmanager.h"
2 #include <QtGui/QStandardItemModel>
3 #include <QtCore/QStringList>
4 #include <QtCore/QDir>
5 #include <QtXml/QDomElement>
6 #include <QtCore/QDebug> //Debug pro informace
7 SoundFilesManager::SoundFilesManager(QObject *parent) :
8         QObject(parent)
9 {
10 }
11
12 bool SoundFilesManager::Init(QDomElement root)
13 {
14     if(InitModel() == false)
15         return false;
16
17     filesFromXmlDB(root);
18     return true;
19 }
20
21
22 /** InitModel().
23 Used to initialize the model for store names and paths to music files.
24 */
25 bool SoundFilesManager::InitModel()
26 {
27     model_of_files = new QStandardItemModel(this);
28     //QDir dir("/usr/share/sounds");
29     QDir dir("/home/user/MyDocs/.sounds/Ringtones");
30     QFileInfoList list_of_in_home =   dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
31     QFileInfoList list_of_maemo_sounds = QFileInfoList();
32     //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " budou se pridavat soubory" ;
33     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/NokiaTune.aac"));
34     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Beeb.aac"));
35     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Emailalert1.aac"));
36     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Emailalert2.aac"));
37     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Message1.aac"));
38     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Message2.aac"));
39     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Message3.aac"));
40     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Message4.aac"));
41     appendFile(&list_of_maemo_sounds,QFileInfo("/usr/share/sounds/Message5.aac"));
42
43     list_of_maemo_sounds += list_of_in_home;
44     appendToModel(&list_of_maemo_sounds);
45     return true;
46 }
47
48 /** appendToModel().
49 append item into model of files
50 */
51 void SoundFilesManager::appendToModel(QFileInfoList * list_of_files)
52 {
53     QList<QStandardItem *>  items;
54     foreach(QFileInfo file, *list_of_files)
55     {
56         if(checkFileInModelAndExist(file)){
57             //////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " bude se pridavat do modelu toto" << file.absoluteFilePath();
58             items.append(new QStandardItem(file.fileName()));
59             items.append(new QStandardItem(file.absoluteFilePath()));
60             model_of_files->appendRow(items);
61             items.clear();
62         }
63     }
64 }
65
66 /** appendFile().
67 append item into list of files
68 */
69 void SoundFilesManager::appendFile(QFileInfoList * list_of_files, QFileInfo inf)
70 {
71     if(inf.exists()) list_of_files->append(inf);
72 }
73
74 /** checkFileInModelAndExist().
75 Check if file exist and if is its in model
76 */
77 bool SoundFilesManager::checkFileInModelAndExist(QFileInfo inf)
78 {
79     if(!inf.exists()) return false;
80     if(!model_of_files->findItems(inf.absoluteFilePath(),Qt::MatchExactly, 1).empty()) return false;
81     return true;
82 }
83
84 /** filesFromXmlDB().
85 Load files from xml
86 */
87 void SoundFilesManager::filesFromXmlDB(QDomElement root)
88 {
89     qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "ted z xml";
90     QFileInfoList list_of_files;
91     QDomElement first =  root.firstChildElement();
92         qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "sml neni nulll";
93         //list of all profile
94         while(!first.isNull()) {
95             qDebug() << "|Jmeno profilu" << first.tagName();
96             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "bude se pridavat z xml" << first.firstChildElement("email.alert.tone").tagName();
97             appendFile(&list_of_files, QFileInfo(first.firstChildElement("email.alert.tone").text()));
98             appendFile(&list_of_files, QFileInfo(first.firstChildElement("im.alert.tone").text()));
99             appendFile(&list_of_files, QFileInfo(first.firstChildElement("ringing.alert.tone").text()));
100             appendFile(&list_of_files, QFileInfo(first.firstChildElement("sms.alert.tone").text()));
101             first = first.nextSiblingElement();
102         }
103     appendToModel(&list_of_files);
104 }
105
106 /** GetModel().
107 Returns a reference to the model of music files.
108 */
109 QStandardItemModel * SoundFilesManager::GetModel()
110 {
111     return model_of_files;
112 }