first commits to develop
[vncallhistory] / elv1db.cc
1 /*
2 Copyright (C) 2011  by Cuong Le <metacuong@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>
16 */
17
18 #include "elv1db.h"
19
20 #include <QtSql/QSqlQuery>
21 #include <QtSql/QSqlRecord>
22
23 #include <QString>
24 #include <QVariant>
25
26 #include <QDebug>
27
28 elv1db::elv1db(QObject *parent):
29     QThread(parent),
30     m_type_query(0),
31     m_db(QSqlDatabase::addDatabase("QSQLITE"))
32 {
33     m_db.setDatabaseName("/home/user/.rtcom-eventlogger/el-v1.db");
34     m_db.open();
35 }
36
37 elv1db::~elv1db(){
38     if (m_db.isOpen())
39         m_db.close();
40 }
41
42 PhoneType elv1db::phonetype(QString metastr){
43     if (metastr.contains("ring/tel/ring"))
44         return GSM_NETWORK;
45     else if (metastr.contains("spirit/skype"))
46         return SKYPE;
47     else if (metastr.contains("gabble/jabber") && metastr.contains("40gmail_2ecom0"))
48         return GOOGLE_TALK;
49     else if (metastr.contains("gabble/jabber"))
50         return JABBER;
51     return UNKNOW;
52 }
53
54 QString elv1db::calltype(){
55     return this->m_call_type==ALL_CALL?"and (event_type_id = 1 or event_type_id = 3)":
56                                                       this->m_call_type==INCOMING?"and event_type_id = 1  and outgoing = 0":
57                                                       this->m_call_type==OUTGOING?"and event_type_id = 1  and outgoing = 1":"and event_type_id = 3";
58 }
59
60 void elv1db::run(){
61     if (this->m_type_query == 0)
62         emit start_indicator();
63     else
64         emit detail_start_indicator();
65
66     sleep(1);
67
68     if (this->m_type_query == 0){
69         QString m_group_by_call(H_TOTAL_GROUP_BY);
70         setQuery(m_group_by_call
71                  .arg(this->m_all_call?"":"and E.local_uid='ring/tel/ring'")
72                  .arg(calltype()));
73     }
74     else{
75         QString m_filter(" and remote_name = '%1' %2 ");
76         m_filter = m_filter.arg(m_contact_name).arg(calltype());
77         QString m_default_search = QString(H_DEFAULT_SEARCH).arg(m_filter).arg(100);
78         setQuery(m_default_search);
79     }
80
81     QSqlQuery *query = new QSqlQuery(this->m_query);
82     QSqlRecord record = query->record();
83     if(record.count() > 0){
84         if (this->m_type_query == 0){
85             this->m_records.clear();
86               while (query->next()) {
87                   elv1rec *ef=new elv1rec();
88                   ef->set_contact_name(query->value(0).value<QString>());
89                   ef->set_total_call(query->value(1).value<QString>());
90                   this->m_records << ef;
91               }
92         }else{
93
94             this->m_detail_records.clear();
95
96             while (query->next()) {
97                 elv1Detailrec *ef=new elv1Detailrec();
98
99                 int calltype =  query->value(0).value< int >();
100
101                 if (calltype == 1 || calltype == 3){
102                     ef->set_phonenumber(query->value(5).value<QString>());
103                     ef->set_starttime(query->value(1).value<uint>());
104                     ef->set_endtime(query->value(2).value<uint>());
105                     if (query->value(2).value<uint>())
106                         ef->set_callduration(query->value(2).value<uint>() - query->value(1).value<uint>());
107                     else
108                         ef->set_callduration(0);
109                     if (calltype == 1) //call (outgoing/incoming)
110                         ef->set_calltype(query->value(7).value<bool>()?OUTGOING:INCOMING);
111                     else // call but missed
112                         ef->set_calltype(MISSED);
113
114                     ef->set_phonetype(
115                                 this->phonetype(query->value(4).value<QString>())
116                                 );
117
118                     this->m_detail_records << ef;
119                 }
120
121             }
122         }
123     }
124
125     delete query;
126
127     if (this->m_type_query == 0)
128         emit group_by_finished();
129     else
130         emit detail_finished();
131 }