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