Merge commit '0.0.2'
[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_search_val(new QString("")),
32     m_db(QSqlDatabase::addDatabase("QSQLITE"))
33 {
34     m_db.setDatabaseName("/home/user/.rtcom-eventlogger/el-v1.db");
35     m_db.open();
36 }
37
38 elv1db::~elv1db(){
39     if (m_db.isOpen())
40         m_db.close();
41     delete this->m_search_val;
42 }
43
44 PhoneType elv1db::phonetype(QString metastr){
45     if (metastr.contains("ring/tel/ring"))
46         return GSM_NETWORK;
47     else if (metastr.contains("spirit/skype"))
48         return SKYPE;
49     else if (metastr.contains("gabble/jabber") && metastr.contains("40gmail_2ecom0"))
50         return GOOGLE_TALK;
51     else if (metastr.contains("gabble/jabber"))
52         return JABBER;
53     return UNKNOW;
54 }
55
56 QString elv1db::calltype(){
57     return this->m_call_type==ALL_CALL?"and (event_type_id = 1 or event_type_id = 2)":
58                                                       this->m_call_type==INCOMING?"and event_type_id = 1  and outgoing = 0":
59                                                       this->m_call_type==OUTGOING?"and event_type_id = 1  and outgoing = 1":"and event_type_id = 2";
60 }
61
62 void elv1db::run(){
63     if (this->m_type_query == 0 || this->m_type_query == 2)
64         emit start_indicator();
65     else
66         emit detail_start_indicator();
67
68     sleep(1);
69
70     if (this->m_type_query == 0){ //all
71         QString m_group_by_call(H_TOTAL_GROUP_BY);
72         setQuery(m_group_by_call
73                  .arg(this->m_all_call?"":"and E.local_uid='ring/tel/ring'")
74                  .arg(calltype()));
75     }
76     else if (this->m_type_query == 2 ){ //doSearch
77         QString m_group_by_and_search_call(H_TOTAL_GROUP_BY_AND_SEARCH);
78         QString m_search_val_convert("and remote_name like '%%1%'");
79         m_search_val_convert = m_search_val_convert.arg(*this->m_search_val);
80
81         setQuery(m_group_by_and_search_call
82                  .arg(this->m_all_call?"":"and E.local_uid='ring/tel/ring'")
83                  .arg(calltype())
84                  .arg(m_search_val_convert));
85     }
86     else { // for contact details
87         QString m_filter(" and remote_name = '%1' %2 ");
88         m_filter = m_filter.arg(m_contact_name).arg(calltype());
89         QString m_default_search = QString(H_DEFAULT_SEARCH).arg(m_filter).arg(100);
90         setQuery(m_default_search);
91     }
92
93     QSqlQuery *query = new QSqlQuery(this->m_query);
94     QSqlRecord record = query->record();
95     if(record.count() > 0){
96         if (this->m_type_query == 0 || this->m_type_query == 2){
97             this->m_records.clear();
98               while (query->next()) {
99                   elv1rec *ef=new elv1rec();
100                   ef->set_contact_name(query->value(0).value<QString>());
101                   ef->set_total_call(query->value(1).value<QString>());
102                   this->m_records << ef;
103               }
104         }
105         else{
106
107             this->m_detail_records.clear();
108
109             while (query->next()) {
110                 elv1Detailrec *ef=new elv1Detailrec();
111
112                 int calltype =  query->value(0).value< int >();
113
114                 if (calltype == 1 || calltype == 2){
115                     ef->set_phonenumber(query->value(5).value<QString>());
116                     ef->set_starttime(query->value(1).value<uint>());
117                     ef->set_endtime(query->value(2).value<uint>());
118                     if (query->value(2).value<uint>())
119                         ef->set_callduration(query->value(2).value<uint>() - query->value(1).value<uint>());
120                     else
121                         ef->set_callduration(0);
122                     if (calltype == 1) //call (outgoing/incoming)
123                         ef->set_calltype(query->value(7).value<bool>()?OUTGOING:INCOMING);
124                     else // call but missed
125                         ef->set_calltype(MISSED);
126
127                     ef->set_phonetype(
128                                 this->phonetype(query->value(4).value<QString>())
129                                 );
130
131                     this->m_detail_records << ef;
132                 }
133
134             }
135         }
136     }
137
138     delete query;
139
140     if (this->m_type_query == 0 || this->m_type_query == 2)
141         emit group_by_finished();
142     else
143         emit detail_finished();
144 }