notes/notesui/notesmodel/src/notesmodel.cpp
changeset 18 c198609911f9
child 23 fd30d51f876b
equal deleted inserted replaced
0:f979ecb2b13e 18:c198609911f9
       
     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 * Definition file for class NotesModel.
       
    16 *
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <QDebug>
       
    21 #include <QAbstractItemModel>
       
    22 #include <QStandardItemModel>
       
    23 #include <QTimer>
       
    24 #include <QDateTime>
       
    25 #include <HbIcon>
       
    26 
       
    27 // User includes
       
    28 #include "notesmodel.h"
       
    29 #include "agendautil.h"
       
    30 #include "agendaentry.h"
       
    31 #include "notescommon.h"
       
    32 
       
    33 /*!
       
    34 	\class NotesModel
       
    35 
       
    36 	This is the source model class for the notes application views. This owns
       
    37 	a QStandardItemModel which is a model for the data containing notes, todos
       
    38 	and reminders.
       
    39  */
       
    40 
       
    41 /*!
       
    42 	Constructor.
       
    43 
       
    44 	\param agendaUtil Pointer to the object of AgendaUtil.
       
    45 	\param parent Parent of type QObject.
       
    46  */
       
    47 NotesModel::NotesModel(AgendaUtil *agendaUtil, QObject *parent)
       
    48 :QObject(parent),
       
    49  mAgendaUtil(agendaUtil)
       
    50 {
       
    51 	qDebug() << "notes: NotesModel::NotesModel -->";
       
    52 
       
    53 	// Construct the source model.
       
    54 	mSourceModel = new QStandardItemModel(0, 1, this);
       
    55 
       
    56 	connect(
       
    57 			mAgendaUtil, SIGNAL(entriesChanged(QList<ulong>)),
       
    58 			this, SLOT(populateSourceModel(QList<ulong>)));
       
    59 
       
    60 	// Connect to entryAdded signal of agendautil.
       
    61 	connect(
       
    62 			mAgendaUtil, SIGNAL(entryAdded(ulong)),
       
    63 			this, SLOT(addEntryToModel(ulong)));
       
    64 	// Connect to entryDeleted signal of agendautil.
       
    65 	connect(
       
    66 			mAgendaUtil, SIGNAL(entryDeleted(ulong)),
       
    67 			this, SLOT(removeEntryFromModel(ulong)));
       
    68 	// Connect to entryUpdated signal of agendautil.
       
    69 	connect(
       
    70 			mAgendaUtil, SIGNAL(entryUpdated(ulong)),
       
    71 			this, SLOT(updateSourceModel(ulong)));
       
    72 
       
    73 	// Populate the model in a different thread.
       
    74 	QTimer::singleShot(1, this, SLOT(populateSourceModel()));
       
    75 
       
    76 	qDebug() << "notes: NotesModel::NotesModel <--";
       
    77 }
       
    78 
       
    79 /*!
       
    80 	Destructor.
       
    81  */
       
    82 NotesModel::~NotesModel()
       
    83 {
       
    84 	qDebug() << "notes: NotesModel::~NotesModel -->";
       
    85 
       
    86 	// Nothing yet.
       
    87 
       
    88 	qDebug() << "notes: NotesModel::~NotesModel <--";
       
    89 }
       
    90 
       
    91 /*!
       
    92 	Returns the source model to be used with a view.
       
    93 
       
    94 	\return QAbstractItemModel
       
    95 	\sa QAbstractItemModel, HbListView.
       
    96  */
       
    97 QAbstractItemModel *NotesModel::sourceModel()
       
    98 {
       
    99 	qDebug() << "notes: NotesModel::sourceModel -->";
       
   100 
       
   101 	Q_ASSERT(mSourceModel);
       
   102 
       
   103 	qDebug() << "notes: NotesModel::sourceModel <--";
       
   104 
       
   105 	return mSourceModel;
       
   106 }
       
   107 
       
   108 /*!
       
   109 	Populates the source model.
       
   110  */
       
   111 void NotesModel::populateSourceModel()
       
   112 {
       
   113 	qDebug() << "notes: NotesModel::populateSourceModel -->";
       
   114 
       
   115 	// Clear the model if it has any data already
       
   116 	mSourceModel->clear();
       
   117 	mSourceModel->setColumnCount(1);
       
   118 	mNotesCount = mInCompTodoCount = mCompTodoCount = 0;
       
   119 
       
   120 	// The order of appending the items to the model is:
       
   121 	// 1. Notes, 2. Incompleted to-dos, 3. Completed to-dos.
       
   122 	// First get only the notes and populate the model.
       
   123 	QList<ulong> entryIds = mAgendaUtil->entryIds(
       
   124 			(AgendaUtil::FilterFlags) (AgendaUtil::IncludeNotes));
       
   125 
       
   126 	// Add the notes to the model.
       
   127 	appendNotesToModel(entryIds);
       
   128 
       
   129 	// Get the incompleted to-dos.
       
   130 	entryIds.clear();
       
   131 	entryIds = mAgendaUtil->entryIds(
       
   132 			(AgendaUtil::FilterFlags) (AgendaUtil::IncludeIncompletedTodos));
       
   133 
       
   134 	// Add the incompleted to-dos to the model.
       
   135 	appendInCompTodosToModel(entryIds);
       
   136 
       
   137 	// Get the completed to-dos.
       
   138 	entryIds.clear();
       
   139 	entryIds = mAgendaUtil->entryIds(
       
   140 			(AgendaUtil::FilterFlags) (AgendaUtil::IncludeCompletedTodos));
       
   141 
       
   142 	// Add the completed to-dos to the model.
       
   143 	appendCompTodosToModel(entryIds);
       
   144 
       
   145 	qDebug() << "notes: NotesModel::populateSourceModel <--";
       
   146 }
       
   147 
       
   148 /*!
       
   149 	Updates the source model with the given entry `id'.
       
   150 	The entry could be either have undergone a status change or have just been
       
   151 	modified.
       
   152 
       
   153 	\param id Of type ulong, identifies the entry which was changed.
       
   154  */
       
   155 void NotesModel::updateSourceModel(ulong id)
       
   156 {
       
   157 	qDebug() << "notes: NotesModel::updateSourceModel -->";
       
   158 
       
   159 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   160 	if (entry.isNull()) {
       
   161 		return;
       
   162 	}
       
   163 
       
   164 	// Entry can be updated in many ways.
       
   165 	// 1. Notes can just change their content or can be marked/unmarked as
       
   166 	// favourites. So for any change with a note, we move the model index
       
   167 	// representing the note to the top of the list.
       
   168 	if (AgendaEntry::TypeNote == entry.type()) {
       
   169 		QModelIndex foundIndex;
       
   170 		for (int iter = 0; iter < mNotesCount; iter++) {
       
   171 			foundIndex = mSourceModel->index(iter, 0);
       
   172 			if (!foundIndex.isValid()) {
       
   173 				continue;
       
   174 			}
       
   175 
       
   176 			ulong noteId = foundIndex.data(
       
   177 					NotesNamespace::IdRole).value<qulonglong>();
       
   178 			if (id == noteId) {
       
   179 				break;
       
   180 			}
       
   181 		}
       
   182 		QStandardItem *item = mSourceModel->takeItem(foundIndex.row());
       
   183 		mSourceModel->removeRow(foundIndex.row());
       
   184 		mNotesCount--;
       
   185 
       
   186 		// Insert the note to the 0th postion.
       
   187 		insertNoteToModel(foundIndex, id);
       
   188 
       
   189 	} else if (AgendaEntry::TypeTodo == entry.type()) {
       
   190 		// 2. To-dos content can be modified.
       
   191 		// So we check the status of the to-do. If the status has changed, we
       
   192 		// check the previous status and remove the model index from the
       
   193 		// corresponding section. Else we just update the to-do contents.
       
   194 		QModelIndex foundIndex;
       
   195 		for (int iter = mNotesCount;
       
   196 				iter < mNotesCount + mInCompTodoCount + mCompTodoCount + 1;
       
   197 				iter++) {
       
   198 			foundIndex = mSourceModel->index(iter, 0);
       
   199 			if (!foundIndex.isValid()) {
       
   200 				continue;
       
   201 			}
       
   202 
       
   203 			ulong noteId = foundIndex.data(
       
   204 					NotesNamespace::IdRole).value<qulonglong>();
       
   205 			if (id == noteId) {
       
   206 				break;
       
   207 			}
       
   208 		}
       
   209 
       
   210 		// Get the status of the to-do.
       
   211 		AgendaEntry::Status entryStatus = (AgendaEntry::Status) foundIndex.data(
       
   212 				NotesNamespace::StatusRole).value<int>();
       
   213 
       
   214 		if (entry.status() == entryStatus) {
       
   215 			if (AgendaEntry::TodoNeedsAction == entryStatus) {
       
   216 				// Make sure that the old model index is removed.
       
   217 				mSourceModel->removeRow(foundIndex.row());
       
   218 				mInCompTodoCount--;
       
   219 
       
   220 				// Now insert the to-do.
       
   221 				insertInCompTodoToModel(foundIndex, id);
       
   222 
       
   223 			} else if (AgendaEntry::TodoCompleted == entryStatus) {
       
   224 				// Make sure that the old model index is removed.
       
   225 				mSourceModel->removeRow(foundIndex.row());
       
   226 				mCompTodoCount--;
       
   227 
       
   228 				// Now insert the completed to-do.
       
   229 				insertCompTodoToModel(foundIndex, id);
       
   230 
       
   231 			}
       
   232 		} else {
       
   233 			if (AgendaEntry::TodoNeedsAction == entryStatus) {
       
   234 				// Here the to-do was marked completed. Hence we need to remove
       
   235 				// the incompleted to-do and insert a completed to-do.
       
   236 				mSourceModel->removeRow(foundIndex.row());
       
   237 				mInCompTodoCount--;
       
   238 
       
   239 				// Now insert the completed to-do.
       
   240 				insertCompTodoToModel(foundIndex, id);
       
   241 
       
   242 			} else if (AgendaEntry::TodoCompleted == entryStatus) {
       
   243 				// Here the to-do was marked incompleted. Hence we need to
       
   244 				// remove the completed to-do and insert an incompleted to-do.
       
   245 				mSourceModel->removeRow(foundIndex.row());
       
   246 				mCompTodoCount--;
       
   247 
       
   248 				// Now insert the incompleted to-do.
       
   249 				insertInCompTodoToModel(foundIndex, id);
       
   250 
       
   251 			}
       
   252 		}
       
   253 	}
       
   254 	qDebug() << "notes: NotesModel::updateSourceModel <--";
       
   255 }
       
   256 
       
   257 /*!
       
   258 	Populates the source model. This slot is called when we get a db change
       
   259 	notification from agenda server caused by another session.
       
   260 
       
   261 	\param ids List of ids containing the entries that got changed.
       
   262  */
       
   263 void NotesModel::populateSourceModel(QList<ulong> ids)
       
   264 {
       
   265 	qDebug() << "notes: NotesModel::populateSourceModel(ids) -->";
       
   266 
       
   267 	Q_UNUSED(ids)
       
   268 
       
   269 	QTimer::singleShot(1, this, SLOT(populateSourceModel()));
       
   270 
       
   271 	qDebug() << "notes: NotesModel::populateSourceModel(ids) <--";
       
   272 }
       
   273 
       
   274 /*!
       
   275 	Adds an entry to the model i.e., creates a QModelIndex and inserts it to the
       
   276 	model.
       
   277 
       
   278 	\param id The id of the entry to be added to the model.
       
   279  */
       
   280 void NotesModel::addEntryToModel(ulong id)
       
   281 {
       
   282 	qDebug() << "notes: NotesModel::addEntryToModel -->";
       
   283 
       
   284 	// We have different logic for adding a note or an incompleted to-do or a
       
   285 	// completed to-do.
       
   286 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   287 	if (entry.isNull()) {
       
   288 		return;
       
   289 	}
       
   290 	bool notify = false;
       
   291 	QModelIndex indexToNotify;
       
   292 
       
   293 	if (AgendaEntry::TypeNote == entry.type()) {
       
   294 		// All new notes are inserted at the top of the list by default.
       
   295 		// Create a new model index at the 0th position.
       
   296 		notify = insertNoteToModel(indexToNotify, id);
       
   297 
       
   298 	} else if (AgendaEntry::TypeTodo == entry.type()) {
       
   299 		if (AgendaEntry::TodoNeedsAction == entry.status()) {
       
   300 			// Try to insert the to-do at its appropriate position.
       
   301 			notify = insertInCompTodoToModel(indexToNotify, id);
       
   302 
       
   303 		} else if (AgendaEntry::TodoCompleted == entry.status()) {
       
   304 			// Try to insert the to-do at its appropriate position.
       
   305 			insertCompTodoToModel(indexToNotify, id);
       
   306 		}
       
   307 	}
       
   308 
       
   309 	if (notify) {
       
   310 		emit rowAdded(indexToNotify);
       
   311 	}
       
   312 
       
   313 	qDebug() << "notes: NotesModel::addEntryToModel <--";
       
   314 }
       
   315 
       
   316 /*!
       
   317 	Deletes an entry from model.
       
   318 
       
   319 	\param Deletes the entry from model and hence updating the view.
       
   320  */
       
   321 void NotesModel::removeEntryFromModel(ulong id)
       
   322 {
       
   323 	for (int iter = 0; iter < mSourceModel->rowCount(); iter++) {
       
   324 		QModelIndex mdlIndex = mSourceModel->index(iter, 0);
       
   325 
       
   326 		if (!mdlIndex.isValid()) {
       
   327 			return;
       
   328 		}
       
   329 
       
   330 		ulong noteId = mdlIndex.data(
       
   331 				NotesNamespace::IdRole).value<qulonglong>();
       
   332 		AgendaEntry::Type entryType = (AgendaEntry::Type) mdlIndex.data(
       
   333 				NotesNamespace::TypeRole).value<int>();
       
   334 		AgendaEntry::Status entryStatus = (AgendaEntry::Status) mdlIndex.data(
       
   335 				NotesNamespace::StatusRole).value<int>();
       
   336 
       
   337 		if (id == noteId) {
       
   338 			mSourceModel->removeRow(mdlIndex.row());
       
   339 
       
   340 			// Update the note, to-do counts depending on the type.
       
   341 			if (entryType == AgendaEntry::TypeNote) {
       
   342 				mNotesCount--;
       
   343 			} else if (entryType == AgendaEntry::TypeTodo) {
       
   344 				if (entryStatus == AgendaEntry::TodoCompleted) {
       
   345 					mCompTodoCount--;
       
   346 				} else if (entryStatus == AgendaEntry::TodoNeedsAction) {
       
   347 					mInCompTodoCount--;
       
   348 				}
       
   349 			}
       
   350 			break;
       
   351 		}
       
   352 	}
       
   353 }
       
   354 
       
   355 /*!
       
   356 	Modifies the content of a given `row' with the data of the entry given by
       
   357 	`id'.
       
   358 
       
   359 	\param id The id of the entry.
       
   360 	\param row The row corresponding to the given id.
       
   361  */
       
   362 void NotesModel::modifyEntryInModel(ulong id, int row)
       
   363 {
       
   364 	qDebug() << "notes: NotesModel::modifyEntryInModel -->";
       
   365 
       
   366 	// Get the model index.
       
   367 	QModelIndex modelIndex = mSourceModel->index(row, 0);
       
   368 	Q_ASSERT(modelIndex.isValid());
       
   369 
       
   370 	// Fetch the entry
       
   371 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   372 	Q_ASSERT(!entry.isNull());
       
   373 
       
   374 	// Set the note id.
       
   375 	mSourceModel->setData(
       
   376 			modelIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   377 	// Set the type of the note.
       
   378 	mSourceModel->setData(
       
   379 			modelIndex, entry.type(), NotesNamespace::TypeRole);
       
   380 
       
   381 	// Set the display data to the index.
       
   382 	if (AgendaEntry::TypeNote == entry.type()) {
       
   383 		// Read modification time from agenda entry
       
   384 		QString displayText;
       
   385 		QString timeText(qtTrId("txt_notes_dblist_val_modified_on_1_2"));
       
   386 		QString modifiedDateText =
       
   387 				entry.startTime().date().toString("dd/MM/yyyy");
       
   388 		QString modifiedTimeText =
       
   389 						entry.startTime().time().toString("hh:mm ap");
       
   390 		displayText = timeText.arg(modifiedDateText,modifiedTimeText);
       
   391 
       
   392 		QStringList stringList;
       
   393 		stringList << entry.description().left(15) << displayText;
       
   394 		// Set the data.
       
   395 		mSourceModel->setData(
       
   396 				modelIndex, stringList, Qt::DisplayRole);
       
   397 		// Set the favourite icon.
       
   398 		if (entry.favourite()) {
       
   399 			QList<QVariant> iconList;
       
   400 			iconList.append(QVariant(QVariant::Invalid));
       
   401 			iconList.append(HbIcon("qtg_mono_favourites"));
       
   402 			mSourceModel->setData(modelIndex, iconList, Qt::DecorationRole);
       
   403 		} else {
       
   404 			QList<QVariant> iconList;
       
   405 			iconList.append(QVariant(QVariant::Invalid));
       
   406 			iconList.append(QVariant(QVariant::Invalid));
       
   407 			mSourceModel->setData(modelIndex, iconList, Qt::DecorationRole);
       
   408 		}
       
   409 	} else if (AgendaEntry::TypeTodo == entry.type()) {
       
   410 		QStringList stringList;
       
   411 		stringList << entry.summary();
       
   412 		if (AgendaEntry::TodoNeedsAction == entry.status()) {
       
   413 			// Read due date from agenda entry
       
   414 			QString displayText;
       
   415 			QString timeText(hbTrId("txt_notes_dblist_val_due_on_1"));
       
   416 			QString dueDateText =
       
   417 					entry.startTime().toString("dd/MM/yyyy");
       
   418 			displayText = timeText.arg(dueDateText);
       
   419 			stringList << displayText;
       
   420 		}
       
   421 		// Set the data.
       
   422 		mSourceModel->setData(
       
   423 				modelIndex, stringList, Qt::DisplayRole);
       
   424 
       
   425 		// Set the to-do done/undone icon based on the status.
       
   426 		QList<QVariant> iconList;
       
   427 		if (AgendaEntry::TodoCompleted == entry.status()) {
       
   428 			iconList.append(HbIcon("qtg_small_todo_done"));
       
   429 		} else {
       
   430 			iconList.append(HbIcon("qtg_small_todo"));
       
   431 		}
       
   432 
       
   433 		// Set the alarm icon if reminder is set.
       
   434 		if (!entry.alarm().isNull()) {
       
   435 			iconList.append(HbIcon("qtg_mono_alarm"));
       
   436 		} else {
       
   437 			iconList.append(QVariant(QVariant::Invalid));
       
   438 		}
       
   439 		// Set the icons.
       
   440 		mSourceModel->setData(modelIndex, iconList, Qt::DecorationRole);
       
   441 	}
       
   442 
       
   443 	qDebug() << "notes: NotesModel::modifyEntryInModel <--";
       
   444 }
       
   445 
       
   446 /*!
       
   447 	Appends notes to the model.
       
   448 
       
   449 	\param ids QList of uids containing the notes.
       
   450  */
       
   451 void NotesModel::appendNotesToModel(QList<ulong> &ids)
       
   452 {
       
   453 	qDebug() << "notes: NotesModel::appendNotesToModel -->";
       
   454 
       
   455 	// Iterate and add notes to the model.
       
   456 	mSourceModel->insertRows(mSourceModel->rowCount(), ids.count());
       
   457 	int rowCount = mSourceModel->rowCount();
       
   458 	for (int idIter = 0, modelIter = rowCount - ids.count();
       
   459 			idIter < ids.count(); idIter++, modelIter++) {
       
   460 		// Fetch the note details.
       
   461 		ulong id = ids[rowCount - 1 - idIter];
       
   462 		AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   463 
       
   464 		if (AgendaEntry::TypeNote != entry.type()) {
       
   465 			continue;
       
   466 		}
       
   467 
       
   468 		// Create a model index.
       
   469 		QModelIndex mdlIndex = mSourceModel->index(qAbs(modelIter), 0);
       
   470 
       
   471 		// Set the note id.
       
   472 		mSourceModel->setData(
       
   473 				mdlIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   474 		// Set the type of the note.
       
   475 		mSourceModel->setData(
       
   476 				mdlIndex, entry.type(), NotesNamespace::TypeRole);
       
   477 		// Set the favourite property.
       
   478 		mSourceModel->setData(
       
   479 				mdlIndex, entry.favourite(), NotesNamespace::FavouriteRole);
       
   480 
       
   481 		// Set the display data now.
       
   482 		// Read modification time from agenda entry
       
   483 		QString displayText;
       
   484 		QString timeText(hbTrId("txt_notes_dblist_val_modified_on_1_2"));
       
   485 		QString modifiedDateText =
       
   486 				entry.lastModifiedDateTime().date().toString("dd/MM/yyyy");
       
   487 		QString modifiedTimeText =
       
   488 						entry.lastModifiedDateTime().time().toString("hh:mm ap");
       
   489 		displayText = timeText.arg(modifiedDateText,modifiedTimeText);
       
   490 
       
   491 		QStringList stringList;
       
   492 		stringList << entry.description().left(100) << displayText;
       
   493 		// Set the data.
       
   494 		mSourceModel->setData(
       
   495 				mdlIndex, stringList, Qt::DisplayRole);
       
   496 
       
   497 		if (entry.favourite()) {
       
   498 			// Set the favourite icon.
       
   499 			QList<QVariant> iconList;
       
   500 			iconList.append(QVariant(QVariant::Invalid));
       
   501 			iconList.append(HbIcon("qtg_mono_favourites"));
       
   502 			mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   503 		} else {
       
   504 			QList<QVariant> iconList;
       
   505 			iconList.append(QVariant(QVariant::Invalid));
       
   506 			iconList.append(QVariant(QVariant::Invalid));
       
   507 			mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   508 		}
       
   509 
       
   510 		// Update the notes count.
       
   511 		mNotesCount++;
       
   512 	}
       
   513 
       
   514 	qDebug() << "notes: NotesModel::appendNotesToModel <--";
       
   515 }
       
   516 
       
   517 /*!
       
   518 	Appends incompleted to-dos to the model.
       
   519 
       
   520 	\param ids QList of uids containing the incompleted to-dos.
       
   521  */
       
   522 void NotesModel::appendInCompTodosToModel(QList<ulong> &ids)
       
   523 {
       
   524 	qDebug() << "notes: NotesModel::appendInCompTodosToModel -->";
       
   525 
       
   526 	// Iterate and add incomplete to-do to the model.
       
   527 	mSourceModel->insertRows(mSourceModel->rowCount(), ids.count());
       
   528 	int rowCount = mSourceModel->rowCount();
       
   529 	for (int idIter = 0, modelIter = rowCount - ids.count();
       
   530 			idIter < ids.count(); idIter++, modelIter++) {
       
   531 		// Fetch the to-do details.
       
   532 		ulong id = ids[idIter];
       
   533 		AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   534 
       
   535 		if (AgendaEntry::TypeTodo != entry.type()) {
       
   536 			continue;
       
   537 		}
       
   538 
       
   539 		if (AgendaEntry::TodoNeedsAction != entry.status()) {
       
   540 			continue;
       
   541 		}
       
   542 
       
   543 		// Create a model index.
       
   544 		QModelIndex mdlIndex = mSourceModel->index(modelIter, 0);
       
   545 
       
   546 		// Set the to-do id.
       
   547 		mSourceModel->setData(
       
   548 				mdlIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   549 		// Set the type of the to-do.
       
   550 		mSourceModel->setData(
       
   551 				mdlIndex, entry.type(), NotesNamespace::TypeRole);
       
   552 		// Set the status of the to-do.
       
   553 		mSourceModel->setData(
       
   554 				mdlIndex, (int) entry.status(), NotesNamespace::StatusRole);
       
   555 
       
   556 		// Set the display data now.
       
   557 		QStringList stringList;
       
   558 		stringList << entry.summary();
       
   559 		if (AgendaEntry::TodoNeedsAction == entry.status()) {
       
   560 			// Read due date from agenda entry
       
   561 			QString displayText;
       
   562 			QString timeText(hbTrId("txt_notes_dblist_val_due_on_1"));
       
   563 			QString dueDateText =
       
   564 					entry.startTime().toString("dd/MM/yyyy");
       
   565 			displayText = timeText.arg(dueDateText);
       
   566 			stringList << displayText;
       
   567 		}
       
   568 		// Set the data.
       
   569 		mSourceModel->setData(mdlIndex, stringList, Qt::DisplayRole);
       
   570 		// Set the to-do done/undone icon based on the status.
       
   571 		QList<QVariant> iconList;
       
   572 		iconList.append(HbIcon("qtg_small_todo"));
       
   573 
       
   574 		// Set the alarm icon if reminder is set.
       
   575 		if (!entry.alarm().isNull()) {
       
   576 			iconList.append(HbIcon("qtg_mono_alarm"));
       
   577 		} else {
       
   578 			iconList.append(QVariant(QVariant::Invalid));
       
   579 		}
       
   580 
       
   581 		// Set the icons.
       
   582 		mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   583 
       
   584 		// Update the incompleted to-do count.
       
   585 		mInCompTodoCount++;
       
   586 	}
       
   587 
       
   588 	qDebug() << "notes: NotesModel::appendInCompTodosToModel <--";
       
   589 }
       
   590 
       
   591 /*!
       
   592 	Appends incompleted to-dos to the model.
       
   593 
       
   594 	\param ids QList of uids containing the completed to-dos.
       
   595  */
       
   596 void NotesModel::appendCompTodosToModel(QList<ulong> &ids)
       
   597 {
       
   598 	qDebug() << "notes: NotesModel::appendCompTodosToModel -->";
       
   599 
       
   600 	// Iterate and add complete to-do to the model.
       
   601 	mSourceModel->insertRows(mSourceModel->rowCount(), ids.count());
       
   602 	int rowCount = mSourceModel->rowCount();
       
   603 	for (int idIter = 0, modelIter = rowCount - ids.count();
       
   604 			idIter < ids.count(); idIter++, modelIter++) {
       
   605 		// Fetch the to-do details.
       
   606 		ulong id = ids[idIter];
       
   607 		AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   608 
       
   609 		if (AgendaEntry::TypeTodo != entry.type()) {
       
   610 			continue;
       
   611 		}
       
   612 
       
   613 		if (AgendaEntry::TodoCompleted != entry.status()) {
       
   614 			continue;
       
   615 		}
       
   616 
       
   617 		// Create a model index.
       
   618 		QModelIndex mdlIndex = mSourceModel->index(modelIter, 0);
       
   619 
       
   620 		// Set the to-do id.
       
   621 		mSourceModel->setData(
       
   622 				mdlIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   623 		// Set the type of the to-do.
       
   624 		mSourceModel->setData(
       
   625 				mdlIndex, entry.type(), NotesNamespace::TypeRole);
       
   626 		// Set the status of the to-do.
       
   627 		mSourceModel->setData(
       
   628 				mdlIndex, (int) entry.status(), NotesNamespace::StatusRole);
       
   629 
       
   630 		// Set the display data now.
       
   631 		QStringList stringList;
       
   632 		stringList << entry.summary();
       
   633 		if (AgendaEntry::TodoCompleted == entry.status()) {
       
   634 			// Read completed date from agenda entry
       
   635 			QString displayText;
       
   636 			QString timeText(hbTrId("txt_notes_dblist_val_completed_on_1"));
       
   637 			QString completedTimeText =
       
   638 					entry.completedDateTime().toString("dd/MM/yyyy");
       
   639 			displayText = timeText.arg(completedTimeText);
       
   640 			stringList << displayText;
       
   641 		}
       
   642 		// Set the data.
       
   643 		mSourceModel->setData(mdlIndex, stringList, Qt::DisplayRole);
       
   644 
       
   645 		// Set the to-do done icon.
       
   646 		QList<QVariant> iconList;
       
   647 		iconList.append(HbIcon("qtg_small_todo_done"));
       
   648 		// To-do is already completed. No need to set alarm.
       
   649 		iconList.append(QVariant(QVariant::Invalid));
       
   650 
       
   651 		// Set the icons.
       
   652 		mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   653 
       
   654 		// Update the completed to-do count.
       
   655 		mCompTodoCount++;
       
   656 	}
       
   657 
       
   658 	qDebug() << "notes: NotesModel::appendCompTodosToModel <--";
       
   659 }
       
   660 
       
   661 /*!
       
   662 	Inserts a note at the 0th position.
       
   663 
       
   664 	\param index Will contain the model index of the newly created row.
       
   665 	\param id The id of the note to be inserted.
       
   666 	\return bool true if the insertion was successful, false otherwise.
       
   667  */
       
   668 bool NotesModel::insertNoteToModel(QModelIndex &index, ulong id)
       
   669 {
       
   670 	qDebug() << "notes: NotesModel::insertNoteToModel -->";
       
   671 
       
   672 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   673 	if (entry.isNull()) {
       
   674 		return false;
       
   675 	}
       
   676 
       
   677 	mSourceModel->insertRows(0, 1);
       
   678 	QModelIndex mdlIndex = mSourceModel->index(0, 0);
       
   679 
       
   680 	// Now set the details of this note.
       
   681 	// Set the note id.
       
   682 	mSourceModel->setData(
       
   683 			mdlIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   684 	// Set the type of the note.
       
   685 	mSourceModel->setData(
       
   686 			mdlIndex, entry.type(), NotesNamespace::TypeRole);
       
   687 	// Set the favourite property.
       
   688 	mSourceModel->setData(
       
   689 			mdlIndex, entry.favourite(), NotesNamespace::FavouriteRole);
       
   690 
       
   691 	// Set the display data now.
       
   692 	// Read modification time from agenda entry
       
   693 	QString displayText;
       
   694 	QString timeText(hbTrId("txt_notes_dblist_val_modified_on_1_2"));
       
   695 	QString modifiedDateText =
       
   696 			entry.lastModifiedDateTime().date().toString("dd/MM/yyyy");
       
   697 	QString modifiedTimeText =
       
   698 					entry.lastModifiedDateTime().time().toString("hh:mm ap");
       
   699 	displayText = timeText.arg(modifiedDateText,modifiedTimeText);
       
   700 
       
   701 	QStringList stringList;
       
   702 	stringList << entry.description().left(100) << displayText;
       
   703 	// Set the data.
       
   704 	mSourceModel->setData(
       
   705 			mdlIndex, stringList, Qt::DisplayRole);
       
   706 
       
   707 	// Set the favourite icon.
       
   708 	if (entry.favourite()) {
       
   709 		QList<QVariant> iconList;
       
   710 		iconList.append(QVariant(QVariant::Invalid));
       
   711 		iconList.append(HbIcon("qtg_mono_favourites"));
       
   712 		mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   713 	} else {
       
   714 		QList<QVariant> iconList;
       
   715 		iconList.append(QVariant(QVariant::Invalid));
       
   716 		iconList.append(QVariant(QVariant::Invalid));
       
   717 		mSourceModel->setData(mdlIndex, iconList, Qt::DecorationRole);
       
   718 	}
       
   719 	// Update the notes count.
       
   720 	mNotesCount++;
       
   721 
       
   722 	index = mdlIndex;
       
   723 
       
   724 	qDebug() << "notes: NotesModel::insertNoteToModel <--";
       
   725 
       
   726 	return true;
       
   727 }
       
   728 
       
   729 /*!
       
   730 	Inserts an incomplete to-do at a position based on the id of the
       
   731 	to-do given.
       
   732 
       
   733 	\param index This will hold the model index of the newly inserted to-do.
       
   734 	\param id The id of the to-do to be inserted.
       
   735 	\return bool true if the Insertion was successful, false otherwise.
       
   736  */
       
   737 bool NotesModel::insertInCompTodoToModel(QModelIndex &index, ulong id)
       
   738 {
       
   739 	qDebug() << "notes: NotesModel::insertInCompTodoToModel -->";
       
   740 
       
   741 	bool success = false;
       
   742 
       
   743 	// Fetch the entry first.
       
   744 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   745 	if (entry.isNull()) {
       
   746 		return success;
       
   747 	}
       
   748 
       
   749 
       
   750 	// First fetch the list of incompleted to-dos.
       
   751 	QList<ulong> entryIds = mAgendaUtil->entryIds(
       
   752 			(AgendaUtil::FilterFlags) (AgendaUtil::IncludeIncompletedTodos));
       
   753 
       
   754 	// Get the index of the added to-do in the above list.
       
   755 	int newTodoIndex = -1;
       
   756 	for (int iter = 0; iter < entryIds.count(); iter++) {
       
   757 		if (id == entryIds[iter]) {
       
   758 			newTodoIndex = iter;
       
   759 			break;
       
   760 		}
       
   761 	}
       
   762 
       
   763 	// We display the to-dos in the same order that we get from the
       
   764 	// agenda server. This is a new to-do that has been added. Hence we
       
   765 	// have to insert a model index at the same position as we found the
       
   766 	// id in the list of to-dos above.
       
   767 	// Calculate the position.
       
   768 	if (-1 != newTodoIndex) {
       
   769 		mSourceModel->insertRows(mNotesCount + newTodoIndex, 1);
       
   770 		QModelIndex newModelIndex =
       
   771 				mSourceModel->index(mNotesCount + newTodoIndex, 0);
       
   772 
       
   773 		// We continue only if the index is valid.
       
   774 		if (newModelIndex.isValid()) {
       
   775 			// Things look ok, start adding data to the model index.
       
   776 			// Set the to-do id.
       
   777 			mSourceModel->setData(
       
   778 					newModelIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   779 			// Set the type of the to-do.
       
   780 			mSourceModel->setData(
       
   781 					newModelIndex, entry.type(), NotesNamespace::TypeRole);
       
   782 			// Set the status of the to-do.
       
   783 			mSourceModel->setData(
       
   784 					newModelIndex, (int) entry.status(),
       
   785 					NotesNamespace::StatusRole);
       
   786 
       
   787 			// Set the display data now.
       
   788 			QStringList stringList;
       
   789 			stringList << entry.summary();
       
   790 			if (AgendaEntry::TodoNeedsAction == entry.status()) {
       
   791 				// Read due date from agenda entry
       
   792 				QString displayText;
       
   793 				QString timeText(hbTrId("txt_notes_dblist_val_due_on_1"));
       
   794 				QString dueDateText =
       
   795 						entry.startTime().toString("dd/MM/yyyy");
       
   796 				displayText = timeText.arg(dueDateText);
       
   797 				stringList << displayText;
       
   798 			}
       
   799 			// Set the data.
       
   800 			mSourceModel->setData(
       
   801 					newModelIndex, stringList, Qt::DisplayRole);
       
   802 			// Set the to-do done/undone icon based on the status.
       
   803 			QList<QVariant> iconList;
       
   804 			iconList.append(HbIcon("qtg_small_todo"));
       
   805 
       
   806 			// Set the alarm icon if reminder is set.
       
   807 			if (!entry.alarm().isNull()) {
       
   808 				iconList.append(HbIcon("qtg_mono_alarm"));
       
   809 			} else {
       
   810 				iconList.append(QVariant(QVariant::Invalid));
       
   811 			}
       
   812 			// Set the icons.
       
   813 			mSourceModel->setData(
       
   814 					newModelIndex, iconList, Qt::DecorationRole);
       
   815 
       
   816 			// Update the incompleted to-do count.
       
   817 			mInCompTodoCount++;
       
   818 
       
   819 			success = true;
       
   820 			index = newModelIndex;
       
   821 		}
       
   822 	}
       
   823 
       
   824 	qDebug() << "notes: NotesModel::insertInCompTodoToModel <--";
       
   825 
       
   826 	return success;
       
   827 }
       
   828 
       
   829 /*!
       
   830 	Inserts a complete to-do at a position based on the id of the
       
   831 	to-do given.
       
   832 
       
   833 	\param index This will hold the model index of the newly inserted to-do.
       
   834 	\param id The id of the to-do to be inserted.
       
   835 	\return bool true if the Insertion was successful, false otherwise.
       
   836  */
       
   837 bool NotesModel::insertCompTodoToModel(QModelIndex &index, ulong id)
       
   838 {
       
   839 	qDebug() << "notes: NotesModel::insertCompTodoToModel -->";
       
   840 
       
   841 	bool success = false;
       
   842 
       
   843 	// Fetch the entry first.
       
   844 	AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   845 	if (entry.isNull()) {
       
   846 		return success;
       
   847 	}
       
   848 
       
   849 	// Now fetch all the incomplete to-dos again and add them.
       
   850 	QList<ulong> entryIds = mAgendaUtil->entryIds(
       
   851 			(AgendaUtil::FilterFlags) (AgendaUtil::IncludeCompletedTodos));
       
   852 
       
   853 	// Get the index of the added to-do in the above list.
       
   854 	int newTodoIndex = -1;
       
   855 	for (int iter = 0; iter < entryIds.count(); iter++) {
       
   856 		if (id == entryIds[iter]) {
       
   857 			newTodoIndex = iter;
       
   858 			break;
       
   859 		}
       
   860 	}
       
   861 
       
   862 	// We display the to-dos in the same order that we get from the
       
   863 	// agenda server. This is a new to-do that has been added. Hence we
       
   864 	// have to insert a model index at the same position as we found the
       
   865 	// id in the list of to-dos above.
       
   866 	// Calculate the position.
       
   867 	if (-1 != newTodoIndex) {
       
   868 		mSourceModel->insertRows(
       
   869 				mNotesCount + mInCompTodoCount + newTodoIndex, 1);
       
   870 		QModelIndex newModelIndex = mSourceModel->index(
       
   871 						mNotesCount + mInCompTodoCount + newTodoIndex, 0);
       
   872 
       
   873 		// We continue only if the index is valid.
       
   874 		if (newModelIndex.isValid()) {
       
   875 			// Things look ok, start adding data to the model index.
       
   876 			AgendaEntry entry = mAgendaUtil->fetchById(id);
       
   877 
       
   878 			// Set the to-do id.
       
   879 			mSourceModel->setData(
       
   880 					newModelIndex, (qulonglong) id, NotesNamespace::IdRole);
       
   881 			// Set the type of the to-do.
       
   882 			mSourceModel->setData(
       
   883 					newModelIndex, entry.type(), NotesNamespace::TypeRole);
       
   884 			// Set the status of the to-do.
       
   885 			mSourceModel->setData(
       
   886 					newModelIndex, (int) entry.status(),
       
   887 					NotesNamespace::StatusRole);
       
   888 
       
   889 			// Set the display data now.
       
   890 			QStringList stringList;
       
   891 			stringList << entry.summary();
       
   892 			if (AgendaEntry::TodoCompleted == entry.status()) {
       
   893 				// Read completed date from agenda entry
       
   894 				QString displayText;
       
   895 				QString timeText(hbTrId("txt_notes_dblist_val_completed_on_1"));
       
   896 				QString completedTimeText =
       
   897 						entry.completedDateTime().toString("dd/MM/yyyy");
       
   898 				displayText = timeText.arg(completedTimeText);
       
   899 				stringList << displayText;
       
   900 			}
       
   901 			// Set the data.
       
   902 			mSourceModel->setData(
       
   903 					newModelIndex, stringList, Qt::DisplayRole);
       
   904 
       
   905 			// Set the to-do done icon.
       
   906 			QList<QVariant> iconList;
       
   907 			iconList.append(HbIcon("qtg_small_todo_done"));
       
   908 			// To-do is already completed. No need to set alarm.
       
   909 			iconList.append(QVariant(QVariant::Invalid));
       
   910 
       
   911 			// Set the icons.
       
   912 			mSourceModel->setData(
       
   913 					newModelIndex, iconList, Qt::DecorationRole);
       
   914 
       
   915 			// Update the completed to-do count.
       
   916 			mCompTodoCount++;
       
   917 
       
   918 			success = true;
       
   919 			index = newModelIndex;
       
   920 		}
       
   921 	}
       
   922 
       
   923 	qDebug() << "notes: NotesModel::insertCompTodoToModel <--";
       
   924 
       
   925 	return success;
       
   926 }
       
   927 
       
   928 // End of file	--Don't remove this.