- just typos in AudioManagerCore
[profile/ivi/audiomanager.git] / AudioManagerDaemon / DataBaseHandler.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * AudioManangerDeamon
5  *
6  * \file DataBaseHandler.cpp
7  *
8  * \date 20.05.2011
9  * \author Christian Müller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG – Christian Müller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  *
24  */
25
26 #include "DataBaseHandler.h"
27
28 DataBaseHandler::DataBaseHandler() {
29
30         //knock down database
31         QString path = (QDir::home().path());
32
33         path.append(QDir::separator()).append(AUDIO_DATABASE);
34         path = QDir::toNativeSeparators(path);
35
36         QFile db_file(path);
37         if (db_file.exists()) {
38                 db_file.remove();
39         }
40         if (!this->open_database()) {
41                 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Problems with opening the database"));
42         }
43 }
44
45 DataBaseHandler::~DataBaseHandler() {
46         this->close_database();
47 }
48
49 bool DataBaseHandler::open_database() {
50         m_database = QSqlDatabase::addDatabase("QSQLITE");
51         QString path = (QDir::home().path());
52
53         path.append(QDir::separator()).append(AUDIO_DATABASE);
54         path = QDir::toNativeSeparators(path);
55         m_database.setDatabaseName(path);
56         return m_database.open();
57 }
58
59 void DataBaseHandler::close_database() {
60         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Closed Database"));
61         m_database.close();
62 }
63
64 bool DataBaseHandler::delete_data(QString table) {
65         QSqlQuery query;
66         QString comand = "DELETE FROM " + table;
67         return query.exec(comand);
68 }
69
70 bool DataBaseHandler::create_tables() {
71
72         QSqlQuery query;
73         QString
74                         command =
75                                         "CREATE TABLE " + QString(DOMAIN_TABLE)
76                                                         + " (ID INTEGER NOT NULL, DomainName VARCHAR(50), BusName VARCHAR(50), NodeName VARCHAR(50), EarlyMode BOOL, PRIMARY KEY(ID));";
77         if (query.exec(command) != true) {
78                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(DOMAIN_TABLE));
79                 return false;
80         }
81
82         command
83                         = "CREATE TABLE " + QString(SOURCE_CLASS_TABLE)
84                                         + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), VolumeOffset INTEGER, IsInterrupt BOOL, IsMixed BOOL, PRIMARY KEY(ID));";
85         if (query.exec(command) != true) {
86                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_CLASS_TABLE));
87                 return false;
88         }
89
90         command = "CREATE TABLE " + QString(SINK_CLASS_TABLE)
91                         + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), PRIMARY KEY(ID));";
92         if (query.exec(command) != true) {
93                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SINK_CLASS_TABLE));
94                 return false;
95         }
96
97         command
98                         = "CREATE TABLE " + QString(SOURCE_TABLE)
99                                         + " (ID INTEGER NOT NULL, Name VARCHAR(50), Class_ID INTEGER, Domain_ID INTEGER, IsGateway BOOL, PRIMARY KEY(ID), FOREIGN KEY (Domain_ID) REFERENCES "
100                                         + DOMAIN_TABLE + "(ID), FOREIGN KEY (Class_ID) REFERENCES "
101                                         + SOURCE_CLASS_TABLE + "(ID));";
102         if (query.exec(command) != true) {
103                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_TABLE));
104                 return false;
105         }
106
107         command
108                         = "CREATE TABLE " + QString(SINK_TABLE)
109                                         + " (ID INTEGER NOT NULL, Name VARCHAR(50), Class_ID INTEGER, Domain_ID INTEGER, IsGateway BOOL, PRIMARY KEY(ID), FOREIGN KEY (DOMAIN_ID) REFERENCES "
110                                         + DOMAIN_TABLE + "(ID), FOREIGN KEY (Class_ID) REFERENCES "
111                                         + SOURCE_CLASS_TABLE + "(ID));";
112         if (query.exec(command) != true) {
113                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_CLASS_TABLE));
114                 return false;
115         }
116
117         command
118                         = "CREATE TABLE " + QString(GATEWAY_TABLE)
119                                         + " (ID INTEGER NOT NULL, Name VARCHAR(50), Sink_ID INTEGER, Source_ID INTEGER, DomainSource_ID INTEGER, DomainSink_ID INTEGER, ControlDomain_ID Integer, IsBlocked BOOL, PRIMARY KEY(ID), FOREIGN KEY (Sink_ID) REFERENCES "
120                                         + SINK_TABLE + "(ID), FOREIGN KEY (Source_ID) REFERENCES "
121                                         + SOURCE_TABLE
122                                         + "(ID),FOREIGN KEY (DomainSource_ID) REFERENCES "
123                                         + DOMAIN_TABLE
124                                         + "(ID),FOREIGN KEY (DomainSink_ID) REFERENCES "
125                                         + DOMAIN_TABLE + "(ID));";
126         if (query.exec(command) != true) {
127                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(GATEWAY_TABLE));
128                 return false;
129         }
130
131         command
132                         = "CREATE TABLE " + QString(CONNECTION_TABLE)
133                                         + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, PRIMARY KEY(ID));";
134         if (query.exec(command) != true) {
135                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(CONNECTION_TABLE));
136                 return false;
137         }
138
139         command
140                         = "CREATE TABLE " + QString(INTERRUPT_TABLE)
141                                         + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, Connection_ID INTEGER, mixed BOOL, listInterruptedSources INTEGER, PRIMARY KEY(ID));";
142         if (query.exec(command) != true) {
143                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(INTERRUPT_TABLE));
144                 return false;
145         }
146
147         command
148                         = "CREATE TABLE " + QString(MAIN_TABLE)
149                                         + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, route INTEGER, PRIMARY KEY(ID));";
150         if (query.exec(command) != true) {
151                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(MAIN_TABLE));
152                 return false;
153         }
154
155         return true;
156
157 }
158
159 domain_t DataBaseHandler::insert_into_Domains_table(QString DomainName,
160                 QString BusName, QString NodeName, bool EarlyMode) {
161         QSqlQuery query;
162         QString _EarlyMode = "false";
163         if (EarlyMode) {
164                 _EarlyMode = "true";
165         }
166
167         QString command = "SELECT BusName,ID FROM " + QString(DOMAIN_TABLE)
168                         + " WHERE DomainName=\"" + DomainName + "\";";
169
170         if (query.exec(command) == true) {
171                 if (query.next()) {
172                         if (!query.value(0).toString().isEmpty()) {
173                                 return query.value(1).toInt();
174                         } else {
175                                 command = "UPDATE " + QString(DOMAIN_TABLE) + "SET Busname="
176                                                 + BusName + " Nodename=" + NodeName + " EarlyMode="
177                                                 + _EarlyMode + " WHERE DomainName=" + DomainName;
178                         }
179                 } else {
180                         command = "INSERT INTO " + QString(DOMAIN_TABLE)
181                                         + " (DomainName, BusName, NodeName, EarlyMode) VALUES (\""
182                                         + DomainName + "\",\"" + BusName + "\",\"" + NodeName
183                                         + "\",\"" + _EarlyMode + "\")";
184                 }
185         }
186
187         if (query.exec(command) != true) {
188                 return -1;
189         } else {
190                 return get_Domain_ID_from_Name(DomainName);
191         }
192 }
193 sourceClass_t DataBaseHandler::insert_into_Source_Class_table(
194                 QString ClassName, volume_t VolumeOffset, bool IsInterrupt,
195                 bool IsMixed) {
196         QSqlQuery query;
197         QString _IsInterrupt = "false";
198         QString _IsMixed = "false";
199
200         if (IsInterrupt) {
201                 _IsInterrupt = "true";
202         }
203         if (IsMixed) {
204                 _IsMixed = "true";
205         }
206
207         QString command = "SELECT ID FROM " + QString(SOURCE_CLASS_TABLE)
208                         + " WHERE ClassName=\"" + ClassName + "\";";
209
210         if (query.exec(command) == true) {
211                 if (query.next()) {
212                         return query.value(0).toInt();
213                 }
214         }
215
216         command = "INSERT INTO " + QString(SOURCE_CLASS_TABLE)
217                         + " (ClassName, VolumeOffset, IsInterrupt, IsMixed) VALUES (\""
218                         + ClassName + "\"," + QString::number(VolumeOffset) + ",\""
219                         + _IsInterrupt + "\",\"" + _IsMixed + "\")";
220
221         if (query.exec(command) != true) {
222                 return -1;
223         } else {
224                 return query.lastInsertId().toInt();
225         }
226 }
227
228 sink_t DataBaseHandler::insert_into_Sink_Class_table(QString ClassName) {
229         QSqlQuery query;
230
231         QString command = "SELECT ID FROM " + QString(SINK_CLASS_TABLE)
232                         + " WHERE ClassName=\"" + ClassName + "\";";
233
234         if (query.exec(command) == true) {
235                 if (query.next()) {
236                         return query.value(0).toInt();
237                 }
238         }
239
240         command = "INSERT INTO " + QString(SINK_CLASS_TABLE)
241                         + " (ClassName) VALUES (\"" + ClassName + "\")";
242
243         if (query.exec(command) != true) {
244                 return -1;
245         } else {
246                 return query.lastInsertId().toInt();
247         }
248 }
249
250 source_t DataBaseHandler::insert_into_Source_table(QString Name,
251                 sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
252         QSqlQuery query;
253         QString _IsGateway = "false";
254
255         if (IsGateway) {
256                 _IsGateway = "true";
257         }
258
259         QString command = "SELECT ID FROM " + QString(SOURCE_TABLE)
260                         + " WHERE Name=\"" + Name + "\";";
261
262         if (query.exec(command) == true) {
263                 if (query.next()) {
264                         return query.value(0).toInt();
265                 }
266         }
267
268         command = "INSERT INTO " + QString(SOURCE_TABLE)
269                         + " (Name, Class_ID, Domain_ID, IsGateway) VALUES (\"" + Name
270                         + "\"," + QString::number(Class_ID) + ",\"" + QString::number(
271                         Domain_ID) + "\",\"" + _IsGateway + "\")";
272
273         if (query.exec(command) != true) {
274                 return -1;
275         } else {
276                 emit signal_numberOfSourcesChanged();
277                 return query.lastInsertId().toInt();
278         }
279 }
280
281 sink_t DataBaseHandler::insert_into_Sink_table(QString Name,
282                 sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
283         QSqlQuery query;
284         QString _IsGateway = "false";
285
286         if (IsGateway) {
287                 _IsGateway = "true";
288         }
289
290         QString command = "SELECT ID FROM " + QString(SINK_TABLE)
291                         + " WHERE Name=\"" + Name + "\";";
292
293         if (query.exec(command) == true) {
294                 if (query.next()) {
295                         return query.value(0).toInt();
296                 }
297         }
298
299         command = "INSERT INTO " + QString(SINK_TABLE)
300                         + " (Name, Class_ID, Domain_ID, IsGateway) VALUES (\"" + Name
301                         + "\"," + QString::number(Class_ID) + ",\"" + QString::number(
302                         Domain_ID) + "\",\"" + _IsGateway + "\")";
303
304         if (query.exec(command) != true) {
305                 return -1;
306         } else {
307                 emit signal_numberOfSinksChanged();
308                 return query.lastInsertId().toInt();
309         }
310 }
311
312 gateway_t DataBaseHandler::insert_into_Gatway_table(QString Name,
313                 sink_t Sink_ID, source_t Source_ID, domain_t DomainSource_ID,
314                 domain_t DomainSink_ID, domain_t ControlDomain_ID) {
315         QSqlQuery query;
316         QString command = "SELECT ID FROM " + QString(GATEWAY_TABLE)
317                         + " WHERE Name=\"" + Name + "\";";
318
319         if (query.exec(command) == true) {
320                 if (query.next()) {
321                         return query.value(0).toInt();
322                 }
323         }
324
325         command
326                         = "INSERT INTO " + QString(GATEWAY_TABLE)
327                                         + " (Name, Sink_ID, Source_ID, DomainSource_ID, DomainSink_ID, ControlDomain_ID, IsBlocked) VALUES (\""
328                                         + Name + "\"," + QString::number(Sink_ID) + ","
329                                         + QString::number(Source_ID) + "," + QString::number(
330                                         DomainSource_ID) + "," + QString::number(DomainSink_ID)
331                                         + "," + QString::number(ControlDomain_ID) + ",\"false\")";
332         if (query.exec(command) != true) {
333                 return -1;
334         } else {
335                 return query.lastInsertId().toInt();
336         }
337 }
338
339 genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID) {
340         QSqlQuery query;
341         query.prepare(
342                         "INSERT INTO " + QString(INTERRUPT_TABLE) + "(Source_ID, Sink_ID)"
343                                 " VALUES(:Source_ID, :Sink_ID)");
344         query.bindValue(":Source_ID", Source_ID);
345         query.bindValue(":Sink_ID", Sink_ID);
346         if (query.exec() != true) {
347                 return -1;
348         } else {
349                 return query.lastInsertId().toInt();
350         }
351 }
352
353 genError_t DataBaseHandler::updateInterrupt(const genInt_t intID,
354                 connection_t connID, bool mixed,
355                 QList<source_t> listInterruptedSources) {
356         QSqlQuery query;
357         QString _mixed = "false";
358
359         if (mixed) {
360                 _mixed = "true";
361         }
362
363         //This information is not handy to be stored directly in the database. So we put it on the heap and store the pointer to it.
364         QList<source_t>* pointer = new QList<source_t> (listInterruptedSources);
365
366         query.prepare(
367                         "UPDATE " + QString(INTERRUPT_TABLE)
368                                         + " SET Connection_ID=:Connection_ID, mixed=:mixed ,listInterruptedSources=:listInterruptedSources WHERE ID=:id");
369         query.bindValue(":Connection_ID", connID);
370         query.bindValue(":mixed", _mixed);
371         query.bindValue(":listInterruptedSources", int(pointer));
372         query.bindValue(":id", intID);
373         if (query.exec() != true) {
374                 return GEN_DATABASE_ERROR;
375         } else {
376                 return GEN_OK;
377         }
378 }
379
380 genError_t DataBaseHandler::getInterruptDatafromID(const genInt_t intID,
381                 connection_t* return_connID, sink_t* return_Sink_ID,
382                 source_t* return_Source_ID, bool* return_mixed,
383                 QList<source_t>** return_listInterruptedSources) {
384         QSqlQuery query;
385         QString
386                         command =
387                                         "SELECT Connection_ID, Sink_ID, Source_ID, mixed, listInterruptedSources FROM "
388                                                         + QString(INTERRUPT_TABLE) + " WHERE ID="
389                                                         + QString::number(intID) + ";";
390
391         if (query.exec(command) != true) {
392                 return GEN_DATABASE_ERROR;
393         } else {
394                 if (query.next()) {
395                         *return_connID = query.value(0).toInt();
396                         *return_Sink_ID = query.value(1).toInt();
397                         *return_Source_ID = query.value(2).toInt();
398                         *return_mixed = query.value(3).toBool();
399                         *return_listInterruptedSources
400                                         = reinterpret_cast<QList<source_t>*> (query.value(4).toInt());
401                         return GEN_OK;
402                 } else {
403                         return GEN_UNKNOWN;
404                 }
405         }
406 }
407
408 genError_t DataBaseHandler::removeInterrupt(const genInt_t intID) {
409         QSqlQuery query;
410         QString command = "SELECT listInterruptedSources FROM " + QString(
411                         INTERRUPT_TABLE) + " WHERE ID=" + QString::number(intID) + ";";
412         if (query.exec(command) != true) {
413                 return GEN_DATABASE_ERROR;
414         } else {
415                 if (query.next()) {
416                         delete reinterpret_cast<QList<source_t>*> (query.value(0).toInt());
417                         command = "DELETE FROM " + QString(INTERRUPT_TABLE)
418                                         + " WHERE ID=\"" + QString::number(intID) + "\";";
419                         if (query.exec(command) != true) {
420                                 return GEN_DATABASE_ERROR;
421                         } else {
422                                 return GEN_OK;
423                         }
424                 }
425         }
426         return GEN_UNKNOWN;
427 }
428
429 domain_t DataBaseHandler::peek_Domain_ID(QString DomainName) {
430         QSqlQuery query;
431
432         QString command = "SELECT ID FROM " + QString(DOMAIN_TABLE)
433                         + " WHERE DomainName=\"" + DomainName + "\";";
434
435         if (query.next()) {
436                 return query.value(0).toInt();
437         } else {
438                 command = "INSERT INTO " + QString(DOMAIN_TABLE)
439                                 + " (DomainName) VALUES (\"" + DomainName + "\")";
440         }
441
442         if (query.exec(command) != true) {
443                 return -1;
444         } else {
445                 return query.lastInsertId().toInt();
446         }
447 }
448
449 domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID) {
450         QSqlQuery query;
451         QString command = "SELECT Domain_ID FROM " + QString(SOURCE_TABLE)
452                         + " WHERE ID=" + QString::number(Source_ID) + ";";
453
454         if (query.exec(command) != true) {
455                 return -1;
456         } else {
457                 query.next();
458                 return query.value(0).toInt();
459         }
460 }
461
462 domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID) {
463         QSqlQuery query;
464         QString command = "SELECT Domain_ID FROM " + QString(SINK_TABLE)
465                         + " WHERE ID=" + QString::number(Sink_ID) + ";";
466
467         if (query.exec(command) != true) {
468                 return -1;
469         } else {
470                 query.next();
471                 return query.value(0).toInt();
472         }
473 }
474
475 source_t DataBaseHandler::get_Source_ID_from_Name(QString name) {
476         QSqlQuery query;
477         QString command = "SELECT ID FROM " + QString(SOURCE_TABLE)
478                         + " WHERE Name=\"" + name + "\";";
479
480         if (query.exec(command) != true) {
481                 return -1;
482         } else {
483                 if (query.next()) {
484                         return query.value(0).toInt();
485                 } else {
486                         return 0;
487                 }
488         }
489 }
490
491 sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(QString name) {
492         QSqlQuery query;
493         QString command = "SELECT ID FROM " + QString(SOURCE_CLASS_TABLE)
494                         + " WHERE ClassName=\"" + name + "\";";
495         if (query.exec(command) != true) {
496                 return -1;
497         } else {
498                 if (query.next()) {
499                         return query.value(0).toInt();
500                 } else {
501                         return 0;
502                 }
503         }
504 }
505
506 domain_t DataBaseHandler::get_Domain_ID_from_Name(QString name) {
507         QSqlQuery query;
508         QString command = "SELECT ID FROM " + QString(DOMAIN_TABLE)
509                         + " WHERE DomainName=\"" + name + "\";";
510
511         if (query.exec(command) != true) {
512                 return -1;
513         } else {
514                 if (query.next()) {
515                         return query.value(0).toInt();
516                 } else {
517                         return 0;
518                 }
519         }
520 }
521
522 gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(
523                 domain_t startDomain_ID, domain_t targetDomain_ID) {
524         QSqlQuery query;
525         QString command = "SELECT ID FROM " + QString(GATEWAY_TABLE)
526                         + " WHERE DomainSource_ID=" + QString::number(startDomain_ID)
527                         + " AND DomainSink_ID=" + QString::number(targetDomain_ID) + ";";
528
529         if (query.exec(command) != true) {
530                 return -1;
531         } else {
532                 if (query.next()) {
533                         return query.value(0).toInt();
534                 } else {
535                         return 0;
536                 }
537         }
538 }
539
540 genError_t DataBaseHandler::get_Gateway_Source_Sink_Domain_ID_from_ID(
541                 gateway_t Gateway_ID, source_t* return_Source_ID,
542                 sink_t* return_Sink_ID, domain_t* return_ControlDomain_ID) {
543         QSqlQuery query;
544         QString command = "SELECT Source_ID, Sink_ID, ControlDomain_ID FROM "
545                         + QString(GATEWAY_TABLE) + " WHERE ID=" + QString::number(
546                         Gateway_ID) + ";";
547
548         if (query.exec(command) != true) {
549                 return GEN_DATABASE_ERROR;
550         } else {
551                 if (query.next()) {
552                         *return_Source_ID = query.value(0).toInt();
553                         *return_Sink_ID = query.value(1).toInt();
554                         *return_ControlDomain_ID = query.value(2).toInt();
555                         return GEN_OK;
556                 } else {
557                         return GEN_UNKNOWN;
558                 }
559         }
560 }
561
562 void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree,
563                 QList<RoutingTreeItem*>* allItems) {
564         QSqlQuery query;
565         int RootID = Tree->returnRootDomainID();
566         RoutingTreeItem *parent = Tree->returnRootItem();
567         QString _onlyfree = "false";
568         int i = 0;
569
570         if (onlyfree) {
571                 _onlyfree = "true";
572         }
573
574         query.prepare(
575                         "SELECT ID,DomainSource_ID FROM " + QString(GATEWAY_TABLE)
576                                         + " WHERE DomainSink_ID=:id AND IsBlocked=:flag;");
577
578         do {
579                 query.bindValue(":id", RootID);
580                 query.bindValue(":flag", _onlyfree);
581                 query.exec();
582                 while (query.next()) {
583                         allItems->append(
584                                         Tree->insertItem(query.value(1).toInt(),
585                                                         query.value(0).toInt(), parent));
586                 }
587                 parent = allItems->value(i);
588                 RootID = parent->returnDomainID();
589                 i++;
590         } while (allItems->length() > i);
591 }
592
593 QString DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID) {
594         QSqlQuery query;
595         QString command = "SELECT BusName FROM " + QString(DOMAIN_TABLE)
596                         + " WHERE ID=" + QString::number(Domain_ID) + ";";
597
598         if (query.exec(command) != true) {
599                 return NULL;
600         } else {
601                 query.next();
602                 return query.value(0).toString();
603         }
604 }
605
606 domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID) {
607         QSqlQuery query;
608         QString command = "SELECT Source_ID FROM " + QString(CONNECTION_TABLE)
609                         + " WHERE ID=" + QString::number(ID) + ";";
610
611         if (query.exec(command) != true) {
612                 return -1;
613         }
614         query.next();
615         int SourceID = query.value(0).toInt();
616         command = "SELECT Domain_ID FROM " + QString(SOURCE_TABLE) + " WHERE ID="
617                         + QString::number(SourceID) + ";";
618         if (query.exec(command) != true) {
619                 return -1;
620         } else {
621                 query.next();
622                 return query.value(0).toInt();
623         }
624 }
625
626 void DataBaseHandler::getListofSources(QList<SourceType>* SourceList) {
627         QSqlQuery query;
628         SourceType sType;
629         QString command = "SELECT ID,NAME FROM " + QString(SOURCE_TABLE)
630                         + " WHERE isGateway=\"false\";";
631         if (query.exec(command) != true) {
632
633         } else {
634                 while (query.next()) {
635                         sType.ID = query.value(0).toInt();
636                         sType.name = query.value(1).toString();
637                         SourceList->append(sType);
638                 }
639         }
640 }
641
642 void DataBaseHandler::getListofSinks(QList<SinkType>* SinkList) {
643         QSqlQuery query;
644         SinkType sType;
645         QString command = "SELECT ID,NAME FROM " + QString(SINK_TABLE) + ";";
646         if (query.exec(command) != true) {
647
648         } else {
649                 while (query.next()) {
650                         sType.ID = query.value(0).toInt();
651                         sType.name = query.value(1).toString();
652                         SinkList->append(sType);
653                 }
654         }
655 }
656
657 void DataBaseHandler::getListofConnections(
658                 QList<ConnectionType>* ConnectionList) {
659         QSqlQuery query;
660         ConnectionType sType;
661         QString command = "SELECT Source_ID,Sink_ID FROM " + QString(
662                         CONNECTION_TABLE) + ";";
663         if (query.exec(command) != true) {
664
665         } else {
666                 while (query.next()) {
667                         sType.Source_ID = query.value(0).toInt();
668                         sType.Sink_ID = query.value(1).toInt();
669                         ConnectionList->append(sType);
670                 }
671         }
672 }
673
674 bool DataBaseHandler::is_source_Mixed(source_t source) {
675         QSqlQuery query;
676         int classID = 0;
677
678         QString command = "SELECT Class_ID FROM " + QString(SOURCE_TABLE)
679                         + " WHERE ID=\"" + QString::number(source) + "\";";
680         if (query.exec(command) == true) {
681                 if (query.next()) {
682                         classID = query.value(0).toInt();
683                 }
684         }
685         command = "SELECT isMixed FROM " + QString(SOURCE_CLASS_TABLE)
686                         + " WHERE ID=\"" + QString::number(classID) + "\";";
687
688         if (query.exec(command) == true) {
689                 if (query.next()) {
690                         if (query.value(0).toString().compare("true") == 0) {
691                                 return true;
692                         }
693                 }
694         }
695         return false;
696 }
697
698 sink_t DataBaseHandler::get_Sink_ID_from_Name(QString name) {
699         QSqlQuery query;
700         QString command = "SELECT ID FROM " + QString(SINK_TABLE)
701                         + " WHERE Name=\"" + name + "\";";
702
703         if (query.exec(command) != true) {
704                 return -1;
705         } else {
706                 if (query.next()) {
707                         return query.value(0).toInt();
708                 } else {
709                         return 0;
710                 }
711         }
712 }
713
714 connection_t DataBaseHandler::getConnectionID(source_t SourceID, sink_t SinkID) {
715         QSqlQuery query;
716         query.prepare(
717                         "SELECT ID FROM " + QString(MAIN_TABLE)
718                                         + " WHERE Source_ID=:sourceID AND Sink_ID=:sinkID");
719         query.bindValue(":sourceID", SourceID);
720         query.bindValue(":sinkID", SinkID);
721         if (query.exec() != true) {
722                 return -1;
723         } else {
724                 if (query.next()) {
725                         return query.value(0).toInt();
726                 } else {
727                         return -1;
728                 }
729         }
730 }
731
732 connection_t DataBaseHandler::insertConnection(source_t SourceID, sink_t SinkID) {
733         QSqlQuery query;
734         QString command = "INSERT INTO " + QString(CONNECTION_TABLE)
735                         + " (Source_ID, Sink_ID) VALUES (" + QString::number(SourceID)
736                         + "," + QString::number(SinkID) + ");";
737         if (query.exec(command) != true) {
738                 return -1;
739         } else {
740                 return query.lastInsertId().toInt();
741         }
742 }
743
744 genError_t DataBaseHandler::removeConnection(connection_t ConnectionID) {
745         QSqlQuery query;
746         QString command = "DELETE FROM " + QString(CONNECTION_TABLE)
747                         + " WHERE ID=\"" + QString::number(ConnectionID) + "\";";
748         if (query.exec(command) != true) {
749                 return GEN_DATABASE_ERROR;
750         } else {
751                 return GEN_OK;
752         }
753 }
754
755 connection_t DataBaseHandler::reserveMainConnection(source_t source,
756                 sink_t sink) {
757         QSqlQuery query;
758         query.prepare("INSERT INTO " + QString(MAIN_TABLE) + "(Source_ID, Sink_ID)"
759                 " VALUES(:Source_ID, :Sink_ID)");
760         query.bindValue(":Source_ID", source);
761         query.bindValue(":Sink_ID", sink);
762         if (query.exec() != true) {
763                 return -1;
764         } else {
765                 return query.lastInsertId().toInt();
766         }
767 }
768
769 genError_t DataBaseHandler::updateMainConnection(connection_t connID,
770                 genRoute_t route) {
771         QSqlQuery query;
772
773         //This information is not handy to be stored directly in the database. So we put it on the heap and store the pointer to it.
774         genRoute_t* routeheap = new genRoute_t(route);
775
776         query.prepare(
777                         "UPDATE " + QString(MAIN_TABLE)
778                                         + " SET route=:route WHERE ID=:connID");
779         query.bindValue(":connID", connID);
780         query.bindValue(":route", int(routeheap));
781         if (query.exec() != true) {
782                 return GEN_DATABASE_ERROR;
783         } else {
784                 return GEN_OK;
785         }
786 }
787
788 genError_t DataBaseHandler::getMainConnectionDatafromID(
789                 const connection_t connID, sink_t* return_sinkID,
790                 source_t* return_sourceID, genRoute_t** return_route) {
791         QSqlQuery query;
792         QString command = "SELECT Sink_ID, Source_ID, route FROM " + QString(
793                         MAIN_TABLE) + " WHERE ID=" + QString::number(connID) + ";";
794
795         if (query.exec(command) != true) {
796                 return GEN_DATABASE_ERROR;
797         } else {
798                 if (query.next()) {
799                         *return_sinkID = query.value(0).toInt();
800                         *return_sourceID = query.value(1).toInt();
801                         *return_route
802                                         = reinterpret_cast<genRoute_t*> (query.value(2).toInt());
803                         return GEN_OK;
804                 } else {
805                         return GEN_UNKNOWN;
806                 }
807         }
808 }
809
810 connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID(
811                 sink_t sink, source_t source) {
812         QSqlQuery query;
813         query.prepare(
814                         "SELECT ID FROM " + QString(MAIN_TABLE)
815                                         + " WHERE Sink_ID=:sinkID AND Source_ID=:SourceID");
816         query.bindValue(":SinkID", sink);
817         query.bindValue(":SourceID", source);
818
819         if (query.exec() != true) {
820                 return -1;
821         } else {
822                 if (query.next()) {
823                         return query.value(0).toInt();
824                 }
825         }
826         return -1;
827 }
828
829 QList<source_t> DataBaseHandler::getSourceIDsForSinkID(sink_t sink) {
830         QList<source_t> list;
831         QSqlQuery query;
832         query.prepare(
833                         "SELECT Source_ID FROM " + QString(MAIN_TABLE)
834                                         + " WHERE Sink_ID=:sinkID");
835         query.bindValue(":sinkID", sink);
836         if (query.exec() == true) {
837                 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("query good"));
838                 while (query.next()) {
839                         int p = query.value(0).toInt();
840                         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("SourceID"), DLT_INT(p));
841                         list.append(query.value(0).toInt());
842                 }
843         }
844         return list;
845 }
846
847 QList<ConnectionType> DataBaseHandler::getListAllMainConnections() {
848         QList<ConnectionType> connectionList;
849         QSqlQuery query;
850         QString command = "SELECT Sink_ID, Source_ID FROM " + QString(
851                         MAIN_TABLE) + ";";
852
853         if (query.exec(command) != true) {
854
855         } else {
856                 while (query.next()) {
857                         ConnectionType temp;
858                         temp.Sink_ID = query.value(0).toInt();
859                         temp.Source_ID = query.value(1).toInt();
860                         connectionList.append(temp);
861                         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Added Connection"), DLT_INT(temp.Sink_ID),DLT_INT(temp.Source_ID));
862                 }
863         }
864         return connectionList;
865 }
866
867 genError_t DataBaseHandler::removeMainConnection(connection_t connID) {
868         QSqlQuery query;
869         QString command = "SELECT route FROM " + QString(MAIN_TABLE) + " WHERE ID="
870                         + QString::number(connID) + ";";
871         if (query.exec(command) != true) {
872                 return GEN_DATABASE_ERROR;
873         } else {
874                 if (query.next()) {
875                         delete reinterpret_cast<genRoute_t*> (query.value(0).toInt());
876                         command = "DELETE FROM " + QString(MAIN_TABLE) + " WHERE ID=\""
877                                         + QString::number(connID) + "\";";
878                         if (query.exec(command) != true) {
879                                 return GEN_DATABASE_ERROR;
880                         } else {
881                                 return GEN_OK;
882                         }
883                 }
884         }
885         return GEN_UNKNOWN;
886 }