/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \title Enginio C++ Examples - Cloud Address Book
    \example cloudaddressbook
    \brief The Cloud Address Book example shows sorting, filtering and the full text search functionality
    \ingroup enginio-examples
    \inmodule enginio-qt

    This example explains how to use the full text search feature of Enginio and how to sort and filter data
    showed from the EnginioModel. To present that. a simple address book like application, will be created.
    This example doesn't cover security or multi-user management, for such topics please refer
    to Social Todo example.

    \image cloudaddressbook-example.png

    \section1 Preconditions
    To start the example, a backend id and a backend secret have to be provided to an existing and configured
    Enginio backend. The backend can be created using dashboard, the Cloud Address Book pre-configured backend
    can be chosen.

    \section1 Backend description
    We recommend to use pre-configured backend, because it contain already all data and structures that are
    needed to run this examples, but it is not difficult to create the backend from scratch too.
    The backend should contain one custom object type "objects.addressbook" having properties:
    \list
        \li firstName
        \li lastName
        \li email
        \li phone
        \li address
    \endlist
    All properties are of string type and have to be indexed, because only indexed properties will be searched
    by the full text search.

    \section1 Application design
    The application's ui mainly consist of a table showing all addresses and a text filed where a query
    can be typed. A user should be able to sort addresses or highlight an address containing a specified phrase.

    \section1 Implementation
    From a developer point of view the application from a few simple components:
    \list
        \li EnginioClient which encapsulates all information that are needed to keep connection to the backend
        \li EnginioModel which queries all addresses
        \li QSortFilterProxy which sorts the data
        \li QTableView which shows the data
    \endlist

    The first thing to be done is to construct connection to Enginio service. We need to specify backend id as well
    as backend secret.
    \snippet cloudaddressbook/mainwindow.cpp client

    The second step is to create EnginioModel which queries all data from the backend. The query is quite simple,
    it specifies an object type of which all objects needs to be returned.
    \snippet cloudaddressbook/mainwindow.cpp model

    EnginioModel can sort or filter data only initially, which means that for example a newly added
    item will not be placed correctly. To solve the problem QSortFilterProxy has to be used.
    \snippet cloudaddressbook/mainwindow.cpp assignProxyModel

    Now it is a time to look deeper into EngnioModel. EnginioModel should define data roles.
    \snippet cloudaddressbook/addressbookmodel.h Roles
    \snippet cloudaddressbook/addressbookmodel.cpp Roles

    and as always \l{EnginioModel::data()}{data()} \l{EnginioModel::setData()}{setData()} functions have to be overridden
    to provide Qt::DisplayRole in the way it is needed to nicely cooperate with QTableView.
    \snippet cloudaddressbook/addressbookmodel.cpp data

    Usage of QTableView requires to provide columns headers, so they can be shown in the user interface
    \snippet cloudaddressbook/addressbookmodel.cpp tableViewIntegration

    Integration of the full text search is the last step in this tutorial. The goal is to highlight items that
    contains a given phrase. The highlighting is done by \l{EnginioModel::data()}{data()}, which returns a bold font
    for Qt::FontRole, if an item is matching the search query. This example for simplicity assumes that matching
    items count is not big and can be kept in a QSet, which would be recreated on each search.

    To do a full text search, a JSON query needs to be constructed.
    \snippet cloudaddressbook/addressbookmodel.cpp query
    The query contains "objectTypes" property (mark 's' at the end) which is an array of all object types which
    have to be searched. In this case, it is only one type "objects.addressbook". Next "search" property has to
    be filed. It is an object that is required to have "phrase" property. The property is exactly the search
    phrase that has to be found. Please mark '*' characters, without them the full text search would find
    only full words. To avoid founding substrings, for example in on object id, which is not visible for a user
    the search has to limit scanned property list, by "properties" array.

    When the search query is constructed it is enough to call \l{EnginioClient::fullTextSearch()}{fullTextSearch()}:
    \snippet cloudaddressbook/addressbookmodel.cpp callSearch
    The result will be delivered to the searchResultsArrived slot. In which from result array all objects ids
    are gathered in \a markedItems set.
    \snippet cloudaddressbook/addressbookmodel.cpp results
*/
