1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QDebug> |
|
19 #include <HbListView.h> |
|
20 #include <HbMenu.h> |
|
21 #include <HbAction.h> |
|
22 #include <HbLabel.h> |
|
23 #include <HbMessageBox.h> |
|
24 #include <QGraphicsLinearLayout> |
|
25 #include <logsmodel.h> |
|
26 #include <logsevent.h> |
|
27 #include <logscustomfilter.h> |
|
28 #include <centralrepository.h> |
|
29 #include <logsdomaincrkeys.h> |
|
30 #include <logsfilter.h> |
|
31 #include "logsengineapitester.h" |
|
32 |
|
33 Q_DECLARE_METATYPE(LogsEvent *) |
|
34 |
|
35 #define LOGSAPITEST_PRINT_ALLOC_SIZE { \ |
|
36 User::CompressAllHeaps(); \ |
|
37 TInt allocsize; \ |
|
38 User::AllocSize(allocsize); \ |
|
39 qDebug() << "LogsApiTest alloc size:" << allocsize; } \ |
|
40 |
|
41 TestView::TestView() : HbView(), mList(0), mModel(0), mLogsModel(0), mFilter(0), mShowAll(true) |
|
42 { |
|
43 qDebug() << "LogsApiTest::LogsApiTest ->"; |
|
44 |
|
45 LOGSAPITEST_PRINT_ALLOC_SIZE |
|
46 |
|
47 mRepository = CRepository::NewL( KCRUidLogs ); |
|
48 |
|
49 mLabel = new HbLabel(this); |
|
50 mList = new HbListView(this); |
|
51 mList->setItemRecycling(true); |
|
52 mList->setUniformItemSizes(true); |
|
53 mLogsModel = new LogsModel(LogsModel::LogsFullModel); |
|
54 mModel = new TestModel(*mLogsModel); |
|
55 mList->setModel(mModel); |
|
56 QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical); |
|
57 layout->addItem(mLabel); |
|
58 layout->addItem(mList); |
|
59 setLayout(layout); |
|
60 |
|
61 mMissedCallsFilter = new LogsFilter(LogsFilter::Missed); |
|
62 mMissedCallsFilter->setMaxSize(5); |
|
63 mMissedCallsFilter->setSourceModel(mLogsModel); |
|
64 connect(mMissedCallsFilter, SIGNAL(rowsInserted(const QModelIndex &, int, int)), |
|
65 this, SLOT(updateMissedCallsLabel())); |
|
66 |
|
67 HbAction* filterAction = new HbAction; |
|
68 filterAction->setText("Change filter"); |
|
69 connect(filterAction, SIGNAL(triggered()), this, SLOT(changeFilter()) ); |
|
70 menu()->addAction(filterAction); |
|
71 |
|
72 HbAction* addMissedCallAction = new HbAction; |
|
73 addMissedCallAction->setText("Add missed call"); |
|
74 connect(addMissedCallAction, SIGNAL(triggered()), this, SLOT(addMissedCall()) ); |
|
75 menu()->addAction(addMissedCallAction); |
|
76 |
|
77 HbAction* clearMissedCallsAction = new HbAction; |
|
78 clearMissedCallsAction->setText("Clear missed calls"); |
|
79 connect(clearMissedCallsAction, SIGNAL(triggered()), this, SLOT(clearMissedCalls()) ); |
|
80 menu()->addAction(clearMissedCallsAction); |
|
81 |
|
82 HbAction* deleteAction = new HbAction; |
|
83 deleteAction->setText("Delete events"); |
|
84 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteEvents()) ); |
|
85 menu()->addAction(deleteAction); |
|
86 |
|
87 updateMissedCallsLabel(); |
|
88 |
|
89 qDebug() << "LogsApiTest::LogsApiTest <-"; |
|
90 |
|
91 } |
|
92 |
|
93 |
|
94 TestView::~TestView() |
|
95 { |
|
96 qDebug() << "LogsApiTest::~LogsApiTest ->"; |
|
97 |
|
98 delete mMissedCallsFilter; |
|
99 mList->setModel(0); |
|
100 delete mModel; |
|
101 delete mLogsModel; |
|
102 |
|
103 delete mRepository; |
|
104 |
|
105 qDebug() << "LogsApiTest::~LogsApiTest <-"; |
|
106 } |
|
107 |
|
108 void TestView::changeFilter() |
|
109 { |
|
110 quint32 contactId = 2; |
|
111 if ( mShowAll ){ |
|
112 if ( mFilter ){ |
|
113 delete mFilter; |
|
114 mFilter = 0; |
|
115 } |
|
116 mFilter = new LogsCustomFilter; |
|
117 mFilter->setContactId(contactId); |
|
118 mFilter->setSourceModel(mLogsModel); |
|
119 connect( mFilter, SIGNAL(markingCompleted(int)), this, SLOT(markingCompleted(int)) ); |
|
120 connect( mFilter, SIGNAL(clearingCompleted(int)), this, SLOT(clearingCompleted(int)) ); |
|
121 mList->setModel(0); |
|
122 delete mModel; |
|
123 mModel = 0; |
|
124 mModel = new TestModel(*mFilter); |
|
125 mList->setModel(mModel); |
|
126 mShowAll = false; |
|
127 } |
|
128 else { |
|
129 mList->setModel(0); |
|
130 delete mFilter; |
|
131 mFilter = 0; |
|
132 delete mModel; |
|
133 mModel = 0; |
|
134 mModel = new TestModel(*mLogsModel); |
|
135 mList->setModel(mModel); |
|
136 mShowAll = true; |
|
137 } |
|
138 } |
|
139 |
|
140 void TestView::addMissedCall() |
|
141 { |
|
142 TInt errorCode( KErrNone ); |
|
143 TInt value; |
|
144 |
|
145 errorCode = mRepository->Get( KLogsNewMissedCalls, value ); |
|
146 |
|
147 if ( errorCode == KErrNone ){ |
|
148 ++value; |
|
149 errorCode = mRepository->Set( KLogsNewMissedCalls, value ); |
|
150 } |
|
151 |
|
152 // Do here what some API user might do when it gets |
|
153 // notification about missed call counter increase |
|
154 updateMissedCallsLabel(); |
|
155 } |
|
156 |
|
157 void TestView::clearMissedCalls() |
|
158 { |
|
159 mRepository->Set( KLogsNewMissedCalls, 0 ); |
|
160 if ( mFilter ){ |
|
161 mFilter->markEventsSeen(); |
|
162 } |
|
163 updateMissedCallsLabel(); |
|
164 } |
|
165 |
|
166 void TestView::deleteEvents() |
|
167 { |
|
168 if ( mFilter ){ |
|
169 mFilter->clearEvents(); |
|
170 } else { |
|
171 mLogsModel->clearList(LogsModel::TypeLogsClearAll); |
|
172 } |
|
173 } |
|
174 |
|
175 void TestView::updateMissedCallsLabel() |
|
176 { |
|
177 qDebug() << "LogsApiTest::updateMissedCallsLabel ->"; |
|
178 |
|
179 LOGSAPITEST_PRINT_ALLOC_SIZE |
|
180 |
|
181 QString lastMissedCallFrom; |
|
182 int repeatedMissedCalls = 0; |
|
183 TInt value(0); |
|
184 mRepository->Get( KLogsNewMissedCalls, value ); |
|
185 if ( mMissedCallsFilter->rowCount() > 0 ){ |
|
186 lastMissedCallFrom = mMissedCallsFilter->data(mMissedCallsFilter->index(0,0), Qt::DisplayRole).toStringList().at(0); |
|
187 repeatedMissedCalls = 1; |
|
188 } |
|
189 for( int i = 1; i < value && i < mMissedCallsFilter->rowCount(); i++ ){ |
|
190 QStringList displayData = mMissedCallsFilter->data(mMissedCallsFilter->index(i,0), Qt::DisplayRole).toStringList(); |
|
191 if ( lastMissedCallFrom == displayData.at(0) ){ |
|
192 repeatedMissedCalls++; |
|
193 } else { |
|
194 i = value; // Break |
|
195 } |
|
196 } |
|
197 |
|
198 if ( value == 0 ){ |
|
199 mLabel->setPlainText( "No missed calls" ); |
|
200 } else if ( repeatedMissedCalls >= value ){ |
|
201 mLabel->setPlainText( |
|
202 QString("%1 missed call(s) from %2").arg(value).arg(lastMissedCallFrom) ); |
|
203 } else { |
|
204 mLabel->setPlainText( QString("%1 missed call(s)").arg(value) ); |
|
205 } |
|
206 qDebug() << "LogsApiTest::updateMissedCallsLabel <-"; |
|
207 } |
|
208 |
|
209 void TestView::markingCompleted(int err) |
|
210 { |
|
211 HbMessageBox box(HbMessageBox::MessageTypeInformation); |
|
212 box.setText( QString("Marking completed, err:%1").arg(err) ); |
|
213 box.exec(); |
|
214 } |
|
215 |
|
216 void TestView::clearingCompleted(int err) |
|
217 { |
|
218 HbMessageBox box(HbMessageBox::MessageTypeInformation); |
|
219 box.setText( QString("Clearing completed, err:%1").arg(err) ); |
|
220 box.exec(); |
|
221 } |
|
222 |
|
223 TestModel::TestModel(QAbstractItemModel& logsModel) : mLogsModel(logsModel) |
|
224 { |
|
225 readEvents(); |
|
226 connect( &mLogsModel, SIGNAL( dataChanged(const QModelIndex&,const QModelIndex&)), |
|
227 this, SLOT(handleModelUpdated())); |
|
228 connect( &mLogsModel, SIGNAL( rowsInserted(const QModelIndex&,int,int)), |
|
229 this, SLOT(handleModelUpdated())); |
|
230 connect( &mLogsModel, SIGNAL( rowsRemoved(const QModelIndex&,int,int)), |
|
231 this, SLOT(handleModelUpdated())); |
|
232 connect( &mLogsModel, SIGNAL(modelReset()), |
|
233 this, SLOT(handleModelUpdated())); |
|
234 } |
|
235 TestModel::~TestModel() |
|
236 { |
|
237 } |
|
238 int TestModel::rowCount(const QModelIndex &parent) const |
|
239 { |
|
240 return mEvents.count(); |
|
241 } |
|
242 QVariant TestModel::data(const QModelIndex &index, int role) const |
|
243 { |
|
244 if ( role == Qt::DisplayRole && index.row() >= 0 && index.row() < mEvents.count() ){ |
|
245 return mEvents.at(index.row()); |
|
246 } |
|
247 return QVariant(); |
|
248 } |
|
249 void TestModel::handleModelUpdated() |
|
250 { |
|
251 LOGSAPITEST_PRINT_ALLOC_SIZE |
|
252 |
|
253 // Do it in simple but unefficient way |
|
254 readEvents(); |
|
255 reset(); |
|
256 } |
|
257 |
|
258 void TestModel::readEvents() |
|
259 { |
|
260 mEvents.clear(); |
|
261 for ( int i = 0; i < mLogsModel.rowCount(); ++i ){ |
|
262 LogsEvent* event = qVariantValue<LogsEvent*>( |
|
263 mLogsModel.data( mLogsModel.index(i, 0), LogsModel::RoleFullEvent ) ); |
|
264 if ( event ){ |
|
265 |
|
266 mEvents.append( directionText(*event) ); |
|
267 // Can check occurence time etc. |
|
268 } |
|
269 } |
|
270 } |
|
271 |
|
272 QString TestModel::directionText(const LogsEvent& event) |
|
273 { |
|
274 QString direction; |
|
275 if ( event.direction() == LogsEvent::DirIn ) { |
|
276 direction = QString("Received call from "); |
|
277 } else if ( event.direction() == LogsEvent::DirOut ) { |
|
278 direction = QString("Called to "); |
|
279 } else if ( event.direction() == LogsEvent::DirMissed ) { |
|
280 direction = QString("Missed call from "); |
|
281 } |
|
282 if ( event.remoteParty().length() > 0 ){ |
|
283 direction.append( event.remoteParty() ); |
|
284 } |
|
285 else { |
|
286 direction.append( event.number() ); |
|
287 } |
|
288 |
|
289 if ( event.direction() == LogsEvent::DirMissed ){ |
|
290 if ( event.isRead() ){ |
|
291 direction.append( " Seen"); |
|
292 } else { |
|
293 direction.append( " Unseen"); |
|
294 } |
|
295 } |
|
296 |
|
297 return direction; |
|
298 } |
|
299 |
|
300 |
|
301 |
|