Merge commit '0.0.2'
[vncallhistory] / mainwindow.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 "mainwindow.h"
19
20 #include <QtGui>
21 #include <QtCore/QCoreApplication>
22 #include <QDebug>
23
24 #include <QInputDialog>
25
26 #include "listitemdetaildelegate.h"
27
28 MainWindow::MainWindow(QWidget *parent):
29     QMainWindow(parent),
30     m_elv1db(new elv1db(parent))
31 {
32
33     detail_dlg = new QMainWindow(this);
34     detail_dlg->installEventFilter(this);
35
36     m_contact_detail = new QListWidget(detail_dlg);
37     m_contact_detail->setSelectionMode(QAbstractItemView::NoSelection);
38     m_contact_detail->setObjectName("m_contact_detail");
39     detail_dlg->setCentralWidget(m_contact_detail);
40  #ifdef Q_WS_MAEMO_5
41     detail_dlg->setAttribute(Qt::WA_Maemo5StackedWindow);
42  #endif
43
44
45     this->setWindowTitle(QString::fromUtf8("VN Call History"));
46
47         m_contacts_widget = new QTableWidget(0, 3, this);
48         m_contacts_widget->setProperty( "FingerScrollable", true );
49         m_contacts_widget->setSelectionBehavior(QAbstractItemView::SelectRows);
50         m_contacts_widget->setSelectionMode(QAbstractItemView::SingleSelection);
51         m_contacts_widget->setIconSize( QSize( 48, 48 ) );
52
53         m_contacts_widget->horizontalHeader()->setDefaultSectionSize( 30 );
54         m_contacts_widget->horizontalHeader()->setResizeMode( 0, QHeaderView::Fixed );
55         m_contacts_widget->horizontalHeader()->setResizeMode( 1, QHeaderView::Stretch );
56         m_contacts_widget->horizontalHeader()->setResizeMode( 2, QHeaderView::Fixed );
57
58         m_contacts_widget->verticalHeader()->hide();
59         m_contacts_widget->horizontalHeader()->hide();
60
61         connect(m_contacts_widget, SIGNAL(cellClicked(int,int)), this, SLOT(cellClicked(int,int)));
62
63         m_elv1db->m_type_query = 0;
64
65         m_in_search = false;
66         m_type_query_temp = 0;
67
68         connect(m_elv1db, SIGNAL(group_by_finished()), this, SLOT(elv1db_finised()));
69         connect(m_elv1db, SIGNAL(detail_finished()), this, SLOT(elv1db_detail_finised()));
70         connect(m_elv1db, SIGNAL(start_indicator()), this, SLOT(start_indicator()));
71         connect(m_elv1db, SIGNAL(detail_start_indicator()), this, SLOT(detail_start_indicator()));
72
73         this->setup_menu();
74         this->setup_menu_detail();
75
76         this->setCentralWidget(m_contacts_widget);
77
78         m_elv1db->m_call_type = ALL_CALL;
79         m_elv1db->m_all_call = false;
80         m_elv1db->start();
81
82 #ifdef Q_WS_MAEMO_5
83     this->setAttribute(Qt::WA_Maemo5StackedWindow);
84 #endif
85
86 }
87
88 MainWindow::~MainWindow()
89 {
90     delete this->m_contacts_widget;
91     delete this->m_contact_detail;
92     delete this->m_elv1db;
93     delete this->m_no_search;
94 }
95
96 void MainWindow::cellClicked(int row, int col){
97     qDebug() << "cellClicked : " << row << ":" << col;
98
99     m_contact_detail->clear();
100
101     const elv1rec *ef= m_elv1db->m_records.at(row);
102
103     m_elv1db->m_contact_name = ef->contact_name();
104
105     detail_dlg->setWindowTitle(ef->contact_name());
106     detail_dlg->show();
107
108     m_elv1db->m_type_query = 1;
109     m_elv1db->start();
110 }
111
112 void MainWindow::elv1db_detail_finised(){
113     QDate t_cache;
114
115     m_contact_detail->setItemDelegate(new listitemdetailDelegate(m_contact_detail));
116
117     foreach(elv1Detailrec *ef, m_elv1db->m_detail_records){
118         QString detail_format("%1 | %2 | Duration: %3");
119         QTime final_duration;
120
121         uint secs = ef->get_duration() % 60;
122         uint mins = ef->get_duration() / 60;
123         uint hours = ef->get_duration() / 3600;
124
125         final_duration.setHMS(hours,mins,secs);
126
127         if ((t_cache.day()!=ef->get_startdate().day()) ||
128                 (t_cache.month()!=ef->get_startdate().month()) ||
129                   (t_cache.year()!=ef->get_startdate().year()) ){
130
131             QListWidgetItem *header = new QListWidgetItem();
132             header->setData(Qt::DisplayRole, ef->get_startdate().toString().toUtf8());
133             m_contact_detail->addItem(header);
134
135             t_cache=ef->get_startdate();
136         }
137
138         QListWidgetItem *item = new QListWidgetItem();
139         item->setData(Qt::UserRole + 1, ef->get_phonenumber());
140         item->setData(Qt::UserRole + 2, detail_format.arg(ef->get_starttime().toString())
141                       .arg(ef->get_endtime().toString())
142                       .arg(final_duration.toString()));
143         item->setData(Qt::UserRole + 4, ef->get_icon());
144         item->setData(Qt::UserRole + 5, ef->get_type_call_icon());
145         m_contact_detail->addItem(item);
146     }
147
148     this->me_indicator(this->detail_dlg, false);
149     m_elv1db->m_type_query = 0;
150 }
151
152 void MainWindow::elv1db_finised(){
153     qDebug() << " Process the elv1 database is done";
154
155     m_contacts_widget->clear();
156     m_contacts_widget->setRowCount(0);
157
158     int i=0;
159
160     foreach(elv1rec *ef,m_elv1db->m_records){
161         QTableWidgetItem *m_thumbnail = new QTableWidgetItem;
162         m_thumbnail->setIcon(ef->avatar());
163
164         m_contacts_widget->insertRow(i);
165         m_contacts_widget->setItem(i,0, m_thumbnail);
166
167         QString _m_total_call(" (%1) ");
168         _m_total_call = _m_total_call.arg(ef->total_call());
169
170         QString _m_contact_name(" %1");
171         _m_contact_name = _m_contact_name.arg(ef->contact_name());
172
173         QLabel *m_total_call = new QLabel(_m_total_call);
174         m_total_call->setAlignment(Qt::AlignCenter);
175
176         m_contacts_widget->setCellWidget(i,1, new QLabel(_m_contact_name));
177         m_contacts_widget->setCellWidget(i,2, m_total_call);
178         i++;
179     }
180
181     m_contacts_widget->resizeRowsToContents();
182     m_contacts_widget->resizeColumnsToContents();
183
184     this->stop_indicator();
185
186     m_contacts_widget->setEnabled(true);
187 }
188
189 void MainWindow::start_indicator(){
190     this->me_indicator(this, true);
191 }
192
193 void MainWindow::stop_indicator(){
194     this->me_indicator(this, false);
195 }
196
197 void MainWindow::detail_start_indicator(){
198     this->me_indicator(this->detail_dlg, true);
199 }
200
201 void MainWindow::me_indicator(QMainWindow *qmw, bool m_show){
202     qmw->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, m_show);
203 }
204
205 void MainWindow::setup_menu(){
206     QActionGroup* m_type_group = new QActionGroup(this);
207     m_type_group->setExclusive(true);
208     m_type_group->setObjectName("m_type_group");
209
210     QAction* m_all_calls = new QAction(QString::fromUtf8("All call"), m_type_group );
211     m_all_calls->setCheckable(true);
212     m_all_calls->setObjectName( "m_all_calls" );
213     connect(m_all_calls, SIGNAL(triggered()), this, SLOT(select_all_call()));
214
215     QAction* m_gsm_calls = new QAction(QString::fromUtf8("GSM cellular"), m_type_group );
216     m_gsm_calls->setCheckable(true);
217     m_gsm_calls->setObjectName("m_gsm_calls");
218     connect(m_gsm_calls, SIGNAL(triggered()), this, SLOT(select_gsm_calls()));
219
220     m_gsm_calls->setChecked(true);
221
222
223     QActionGroup* m_type_group_af = new QActionGroup(this);
224     m_type_group_af->setExclusive(true);
225     m_type_group_af->setObjectName("m_type_group_detail");
226
227     QAction* m_all = new QAction(QString::fromUtf8("All"), m_type_group_af );
228     m_all->setCheckable(true);
229     m_all->setObjectName( "m_all" );
230     connect(m_all, SIGNAL(triggered()), this, SLOT(select_all_typecall()));
231
232     QAction* m_incoming = new QAction(QString::fromUtf8("Incoming"), m_type_group_af );
233     m_incoming->setCheckable(true);
234     m_incoming->setObjectName( "m_incoming" );
235     connect(m_incoming, SIGNAL(triggered()), this, SLOT(select_incoming_calls()));
236
237     QAction* m_outgoing = new QAction(QString::fromUtf8("Outgoing"), m_type_group_af );
238     m_outgoing->setCheckable(true);
239     m_outgoing->setObjectName( "m_outgoing" );
240     connect(m_outgoing, SIGNAL(triggered()), this, SLOT(select_outgoing_calls()));
241
242     QAction* m_missed = new QAction(QString::fromUtf8("Missed"), m_type_group_af );
243     m_missed->setCheckable(true);
244     m_missed->setObjectName( "m_missed" );
245     connect(m_missed, SIGNAL(triggered()), this, SLOT(select_missed_calls()));
246
247     m_all->setChecked(true);
248
249     this->menuBar()->addActions(m_type_group->actions());
250     this->menuBar()->addActions(m_type_group_af->actions());
251
252 /*    QAction *m_clear_log = this->menuBar()->addAction("Clear Log");
253     QAction *m_options = this->menuBar()->addAction(QString::fromUtf8("Options"));
254
255     connect(m_clear_log,SIGNAL(triggered()), this, SLOT(clearLog()));
256     connect(m_options, SIGNAL(triggered()), this, SLOT(dlgOptions()));*/
257
258     QAction *m_search = this->menuBar()->addAction("Search");
259     connect(m_search,SIGNAL(triggered()), this, SLOT(dlgSearch()));
260
261     m_no_search = this->menuBar()->addAction("Search is Off");
262     connect(m_no_search,SIGNAL(triggered()), this, SLOT(noSearch()));
263
264     QAction *m_about = this->menuBar()->addAction("About");
265     QAction *m_about_qt = this->menuBar()->addAction("About Qt");
266
267     connect(m_about,SIGNAL(triggered()), this, SLOT(dlgAbout()));
268     connect(m_about_qt,SIGNAL(triggered()), qApp, SLOT(aboutQt()));
269
270 }
271
272 void MainWindow::noSearch(){
273     if (m_in_search){
274         m_contacts_widget->setEnabled(false);
275         m_no_search->setText("Search is Off");
276         m_elv1db->m_type_query = m_type_query_temp;
277         m_in_search = false;
278         m_elv1db->start();
279     }
280 }
281
282 void MainWindow::dlgSearch(){
283     bool ok;
284     QString text = QInputDialog::getText(this, tr("Search"),
285                                          tr("Give me a contact name to do a search :"), QLineEdit::Normal,
286                                          *m_elv1db->m_search_val, &ok);
287     if (ok && !text.isEmpty() && !text.trimmed().isEmpty()){
288         m_no_search->setText("Search is On");
289         m_in_search = true;
290         this->doSearch(text);
291     }
292 }
293
294 void MainWindow::doSearch(QString val){
295     m_contacts_widget->setEnabled(false);
296     *m_elv1db->m_search_val = val;
297     m_elv1db->m_type_query = 2;
298     m_elv1db->start();
299 }
300
301 void MainWindow::select_all_call(){ //ring/tel/ring, spirit/skype, gabble/jabber
302     m_contacts_widget->setEnabled(false);
303     m_elv1db->m_all_call = true;
304     m_type_query_temp = 0;
305     m_elv1db->start();
306 }
307
308 void MainWindow::select_gsm_calls(){ //ring/tel/ring
309     m_contacts_widget->setEnabled(false);
310     m_elv1db->m_all_call = false;
311     m_type_query_temp = 0;
312     m_elv1db->start();
313 }
314
315 void MainWindow::select_all_typecall(){ //incoming, outgoing and missed calls
316     m_contacts_widget->setEnabled(false);
317     m_elv1db->m_call_type = ALL_CALL;
318     m_elv1db->start();
319 }
320
321 void MainWindow::select_incoming_calls(){
322     m_contacts_widget->setEnabled(false);
323     m_elv1db->m_call_type = INCOMING;
324     m_elv1db->start();
325 }
326
327 void MainWindow::select_outgoing_calls(){
328     m_contacts_widget->setEnabled(false);
329     m_elv1db->m_call_type = OUTGOING;
330     m_elv1db->start();
331 }
332
333 void MainWindow::select_missed_calls(){
334     m_contacts_widget->setEnabled(false);
335     m_elv1db->m_call_type = MISSED;
336     m_elv1db->start();
337 }
338
339 void MainWindow::clearLog(){
340 }
341
342 void MainWindow::setup_menu_detail(){
343 }
344
345 void MainWindow::dlgAbout(){
346     QMessageBox m_about;
347     m_about.setWindowTitle("About");
348     m_about.setText(
349                 "<font size=4>VN Call History</font><br>"
350                 "<font size=3>Version : 0.0.2 (09/29/2011)</font><br>"
351                 "Author : Cuong Le <a href=\"mailto:metacuong@gmail.com\">metacuong@gmail.com</a><br>"
352                 "<font size=2>"
353                 "Homepage : <a href=\"http://vncallhistory.garage.maemo.org/\">http://vncallhistory.garage.maemo.org/</a><br>"
354                 "Bugs report : <a href=\"https://garage.maemo.org/tracker/?group_id=2259\">https://garage.maemo.org/tracker/?group_id=2259</a><br>"
355                 "License : GNU/GPL version 2"
356                 "</font>"
357                 );
358     m_about.exec();
359 }
360
361 void MainWindow::dlgOptions(){
362     dlg_options = new QMainWindow(this);
363     dlg_options->setWindowTitle(QString::fromUtf8("Options"));
364     dlg_options->installEventFilter(this);
365 #ifdef Q_WS_MAEMO_5
366     dlg_options->setAttribute(Qt::WA_Maemo5StackedWindow);
367 #endif
368     dlg_options->show();
369 }
370
371 void MainWindow::setOrientation(ScreenOrientation orientation)
372 {
373 #if defined(Q_OS_SYMBIAN)
374     // If the version of Qt on the device is < 4.7.2, that attribute won't work
375     if (orientation != ScreenOrientationAuto) {
376         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
377         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
378             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
379             return;
380         }
381     }
382 #endif // Q_OS_SYMBIAN
383
384     Qt::WidgetAttribute attribute;
385     switch (orientation) {
386 #if QT_VERSION < 0x040702
387     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
388     case ScreenOrientationLockPortrait:
389         attribute = static_cast<Qt::WidgetAttribute>(128);
390         break;
391     case ScreenOrientationLockLandscape:
392         attribute = static_cast<Qt::WidgetAttribute>(129);
393         break;
394     default:
395     case ScreenOrientationAuto:
396         attribute = static_cast<Qt::WidgetAttribute>(130);
397         break;
398 #else // QT_VERSION < 0x040702
399     case ScreenOrientationLockPortrait:
400         attribute = Qt::WA_LockPortraitOrientation;
401         break;
402     case ScreenOrientationLockLandscape:
403         attribute = Qt::WA_LockLandscapeOrientation;
404         break;
405     default:
406     case ScreenOrientationAuto:
407         attribute = Qt::WA_AutoOrientation;
408         break;
409 #endif // QT_VERSION < 0x040702
410     };
411     setAttribute(attribute, true);
412 }
413
414 void MainWindow::showExpanded()
415 {
416 #ifdef Q_OS_SYMBIAN
417     showFullScreen();
418 #elif defined(Q_WS_MAEMO_5)
419     showMaximized();
420 #else
421     show();
422 #endif
423 }