searchui/stateproviders/searchstateprovider/src/settingswidget.cpp
author hgs
Mon, 06 Sep 2010 09:21:19 +0530
changeset 21 708468d5143e
parent 16 e918432ddd92
child 26 367228f82b66
permissions -rw-r--r--
201035

/*
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:  Implementation of the settings widget
 *
 */
#include "settingswidget.h"
#include "onlinehandler.h"
#include <hbdialog.h>
#include <hbcheckbox.h>
#include <qgraphicslinearlayout.h>
#include <hbaction.h>
#include <hblabel.h>
#include <qsignalmapper.h>
#include <hbtooltip.h>
#include <qsettings.h>
#include <hbinputdialog.h>
#include <hbscrollarea.h>
#include <qgraphicswidget.h>
#include <hbglobal.h>
#include <hbdataform.h>
#include <hbdataformmodel.h>
#include <hbdocumentloader.h>
#include <hbcombobox.h>
#include <hbdataformviewitem.h>
#include <hbradiobuttonlist.h>
#include <qdir.h>
#include <qsql.h>
#include <qsqldatabase.h>
#include <qsqlquery.h>
#include <cpixcontentinfocommon.h>

const char *DELIMETER_DOCML = ":/xml/delimeterscreen.docml";
const char *DIALOG = "dialog";
const char *DATAFORM = "dataForm";

//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::SettingsWidget()
//
//----------------------------------------------------------------------------------------------------------------------------
SettingsWidget::SettingsWidget() :
    signalMapper(NULL), popup(NULL), mDocumentLoader(NULL), dataform(NULL),
            mModel(NULL), mSelectedScope(0), mSelectedProvider(0), comboBox(
                    NULL), mInternetHandler(NULL)
    {
    for (int i = 0; i < 8; i++)
        {
        mDeviceMapping.append(false);
        }
    mDeviceCategoryRefList = (QStringList() << "first" << "second" << "third"
            << "fourth" << "fifth" << "sixth" << "seventh" << "eigth");

    mchangestate = true;
    mInstialize = true;
    isInternetSelected = false;
    storeDefaultSettings();
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::intialize()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::initialize()
    {
    bool ok = false;
    if (!mDocumentLoader)
        {
        mDocumentLoader = new HbDocumentLoader();
        mDocumentLoader->load(DELIMETER_DOCML, &ok);
        }
    QGraphicsWidget *widget = NULL;
    if (!popup)
        {
        widget = mDocumentLoader->findWidget(DIALOG);
        Q_ASSERT_X(ok && (widget != 0), "DIALOG", "invalid view");
        popup = qobject_cast<HbDialog*> (widget);
        }
    if (!dataform)
        {
        widget = mDocumentLoader->findWidget(DATAFORM);
        Q_ASSERT_X(ok && (widget != 0), "DATAFORM", "invalid view");
        dataform = qobject_cast<HbDataForm*> (widget);
        }
    if (!mModel)
        {
        mModel = new HbDataFormModel();
        }
    if (!signalMapper)
        {
        signalMapper = new QSignalMapper(this);
        }
    createGui();
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::createGui()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::createGui()
    {
    popup->setDismissPolicy(HbDialog::NoDismiss);
    popup->setTimeout(HbDialog::NoTimeout);

    HbAction * action = new HbAction(hbTrId("txt_common_button_ok"), popup);
    connect(action, SIGNAL(triggered()), this, SLOT(checkBoxOkEvent()));
    mActions.append(action);

    action = new HbAction(hbTrId("txt_common_button_cancel"), popup);
    connect(action, SIGNAL(triggered()), this, SLOT(checkBoxCancelEvent()));
    mActions.append(action);

    popup->addActions(mActions);

    dataform->setModel(mModel);

    HbLabel *label = new HbLabel(hbTrId("txt_search_title_search_scope"));
    popup->setHeadingWidget(label);

    preparecategories();

    loadBaseSettings();

    HbDataFormModelItem* themeComboGeneral = mModel->appendDataFormItem(
            HbDataFormModelItem::ComboBoxItem, hbTrId(
                    "txt_search_info_select_search_scope"));

    themeComboGeneral->setContentWidgetData("items", mCategoryList);

    themeComboGeneral->setContentWidgetData("currentIndex", mSelectedScope);

    QModelIndex index = mModel->indexFromItem(themeComboGeneral);

    HbDataFormViewItem *formItem =
            static_cast<HbDataFormViewItem *> (dataform->itemByIndex(index));

    comboBox = static_cast<HbComboBox*> (formItem->dataItemContentWidget());

    q_currentIndexChanged(mSelectedScope);

    connect(comboBox, SIGNAL(currentIndexChanged(int)), this,
            SLOT(q_currentIndexChanged(int)));

    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::q_currentIndexChanged()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::q_currentIndexChanged(int avalue)
    {
    for (int i = 0; i < mModelItemList.count(); i++)
        {
        mModel->removeItem(mModelItemList.at(i));
        }
    mModelItemList.clear();
    // int ret = mModel->rowCount();
    if (!avalue) //device category creation
        {
        isInternetSelected = false;
        HbCheckBox *checkboxitem = NULL;
        mDeviceCheckBoxList.clear();
        for (int i = 0; i < mDeviceListDisplay.count(); i++)
            {

            HbDataFormModelItem* mModelItem = mModel->appendDataFormItem(
                    HbDataFormModelItem::CheckBoxItem);
            mModelItemList.append(mModelItem);
            QModelIndex index = mModel->indexFromItem(mModelItem);

            HbDataFormViewItem *formItem =
                    static_cast<HbDataFormViewItem *> (dataform->itemByIndex(
                            index));

            checkboxitem
                    = static_cast<HbCheckBox*> (formItem->dataItemContentWidget());

            checkboxitem->setText(mDeviceListDisplay.at(i));

            mDeviceCheckBoxList.append(checkboxitem);

            signalMapper->setMapping(mDeviceCheckBoxList.at(i), i);

            connect(mDeviceCheckBoxList.at(i), SIGNAL(stateChanged(int)),
                    signalMapper, SLOT(map()));

            connect(signalMapper, SIGNAL(mapped(int)), this,
                    SLOT(itemChecked(int)));
            }
        loadDeviceSettings();
        }
    else
        {
        isInternetSelected = true;
        mDeviceCheckBoxList.clear();
        HbDataFormModelItem* mModelItem = mModel->appendDataFormItem(
                HbDataFormModelItem::RadioButtonListItem);
        mModelItemList.append(mModelItem);
        QStringList internetCategoryList;
        QMapIterator<int, QString> i(mServiceProviders);
        int selectedindex = 0;
        int Iterator = 0;
        while (i.hasNext())
            {
            i.next();
            if (i.key() == mSelectedProvider)
                selectedindex = Iterator;
            Iterator++;
            internetCategoryList.append(i.value());            
            }
        mModelItem->setContentWidgetData("items", internetCategoryList);

        mModelItem->setContentWidgetData("previewMode",
                HbRadioButtonList::NoPreview);

        mModelItem->setContentWidgetData("selected", selectedindex);

        QModelIndex index = mModel->indexFromItem(mModelItem);

        HbDataFormViewItem *formItem =
                static_cast<HbDataFormViewItem *> (dataform->itemByIndex(
                        index));
        mradiolist
                = static_cast<HbRadioButtonList*> (formItem->dataItemContentWidget());

        connect(mradiolist, SIGNAL(itemSelected(int)), this,
                SLOT(slotitemSelected(int)));
        }
    setActionVisibility();
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::preparecategories()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::preparecategories()
    {
    // read form database

    mDeviceListDisplay.clear();
    mCategoryList.clear();
    mDeviceMapping.clear();
    for (int i = 0; i < 8; i++)
        {
        mDeviceMapping.append(false);
        }
    mCategoryDbMapping.clear();

    QString mConnectionName(QString::fromUtf16(KContentInfoFileName().Ptr(),
            KContentInfoFileName().Length()));
    QString mDatabaseName(QString::fromUtf16(KCIDBPath().Ptr(),
            KCIDBPath().Length()) + "\\" + mConnectionName);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", mConnectionName);
    db.setDatabaseName(mDatabaseName);

    QSqlQuery query(QSqlDatabase::database(mConnectionName));
    QString statement = QString("SELECT * FROM ") + QString::fromUtf16(
            KContentInfoTableName().Ptr(), KContentInfoTableName().Length());
    query.prepare(statement);
    query.exec();
    while (query.next())
        {
        QString category_name = query.value(0).toString();
        int value = query.value(2).toInt();
        if (value)
            {
            mCategoryDbMapping.insert(category_name, false);
            }
        else
            {
            mCategoryDbMapping.insert(category_name, true);
            }
        }
    db.close();

    /*  mCategoryDbMapping.insert("Contacts", true); 
     mCategoryDbMapping.insert("Audios", true); 
     mCategoryDbMapping.insert("Images", true); 
     mCategoryDbMapping.insert("Videos", true); 
     mCategoryDbMapping.insert("Messages", true); 
     mCategoryDbMapping.insert("email", true); 
     mCategoryDbMapping.insert("Calendar", true); 
     mCategoryDbMapping.insert("Notes", true); 
     mCategoryDbMapping.insert("Applications", true); 
     mCategoryDbMapping.insert("Bookmarks", true); 
     mCategoryDbMapping.insert("Files", true); */

    mDeviceListDisplay.append(hbTrId("txt_search_list_select_all"));
    mDeviceMapping.insert(0, true);

    if (mCategoryDbMapping.value("Contacts"))
        {
        mDeviceMapping.insert(1, true);
        mDeviceListDisplay.append(hbTrId("txt_search_list_contatcs"));
        }
    if (mCategoryDbMapping.value("Audios") || mCategoryDbMapping.value(
            "Images") || mCategoryDbMapping.value("Videos"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_media"));
        mDeviceMapping.insert(2, true);
        }
    if (mCategoryDbMapping.value("Messages") || mCategoryDbMapping.value(
            "email"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_messagemail"));
        mDeviceMapping.insert(3, true);
        }
    if (mCategoryDbMapping.value("Calendar") || mCategoryDbMapping.value(
            "Notes"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_calendarnotes"));
        mDeviceMapping.insert(4, true);
        }
    if (mCategoryDbMapping.value("Applications"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_applications"));
        mDeviceMapping.insert(5, true);
        }
    if (mCategoryDbMapping.value("Bookmarks"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_bookmarks"));
        mDeviceMapping.insert(6, true);
        }
    if (mCategoryDbMapping.value("Files"))
        {
        mDeviceListDisplay.append(hbTrId("txt_search_list_all_other_files"));
        mDeviceMapping.insert(7, true);
        }

    if (mServiceProviders.count())
        {
        mCategoryList = (QStringList() << hbTrId("txt_search_list_device")
                << hbTrId("txt_search_list_internet"));
        }
    else
        {
        mCategoryList = (QStringList() << hbTrId("txt_search_list_device"));
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::~SettingsWidget()
//
//----------------------------------------------------------------------------------------------------------------------------
SettingsWidget::~SettingsWidget()
    {
    delete signalMapper;
    delete mModel;
    mDeviceCheckBoxList.clear();
    mInternetCheckBoxList.clear();
    mModelItemList.clear();
    mActions.clear();
    delete mDocumentLoader;
    delete mInternetHandler;
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::launchSettingWidget()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::launchSettingWidget()
    {
    if (mInstialize)
        {
        initialize();
        mInstialize = false;
        }
    if (mDeviceListDisplay.count() != 7)
        {
        preparecategories();
        q_currentIndexChanged(mSelectedScope);
        QDir dir;
        dir.remove(SETTINGS_INI_PATH);
        storeDefaultSettings();
        }
    // refresh the screen to top     
    QModelIndex index = mModel->index(0, 0);
    dataform->scrollTo(index);
    comboBox->setCurrentIndex(mSelectedScope);
    loadDeviceSettings();
    setActionVisibility();
    popup->show();
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::setActionVisibility()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::setActionVisibility()
    {
    bool noItemSelected = true;
    for (int i = 0; i < mDeviceCheckBoxList.count(); i++)
        {
        if (mDeviceCheckBoxList.at(i)->checkState() == Qt::Checked)
            {
            noItemSelected = false;
            break;
            }
        }
    if (!isInternetSelected)
        {
        mActions.at(0)->setVisible(true);
        if (noItemSelected)
            {
            mActions.at(0)->setVisible(false);
            }
        }
    else
        {
        mActions.at(0)->setVisible(true);
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::itemChecked( int selectedIndex)
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::itemChecked(int selectedIndex)
    {
    if (mDeviceCheckBoxList.count() != 0)
        {
        if (selectedIndex == 0 && mchangestate) // validating the select all
            {
            if ((mDeviceCheckBoxList.at(selectedIndex)->checkState()
                    == Qt::Checked))
                {
                checkSubCategories();
                }
            else
                {
                unCheckSubCategories();
                }
            }
        else
            {
            if ((mDeviceCheckBoxList.at(selectedIndex)->checkState()
                    == Qt::Unchecked))
                {
                mchangestate = false;
                mDeviceCheckBoxList.at(0)->setCheckState(Qt::Unchecked);
                setActionVisibility();
                return;
                }
            }
        }
    mchangestate = true;
    setActionVisibility();
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::checkSubCategories
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::checkSubCategories()
    {
    for (int i = 0; i < mDeviceCheckBoxList.count(); i++)
        {
        mDeviceCheckBoxList.at(i)->setCheckState(Qt::Checked);
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::unCheckSubCategories
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::unCheckSubCategories()
    {
    for (int i = 0; i < mDeviceCheckBoxList.count(); i++)
        {
        mDeviceCheckBoxList.at(i)->setCheckState(Qt::Unchecked);
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::checkBoxCancelEvent()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::checkBoxCancelEvent()
    {
    //   loadDeviceSettings();//bug fix for cancel event selection should not reflect "go" button
    emit settingsEvent(false);
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::checkBoxOkEvent()
//
//----------------------------------------------------------------------------------------------------------------------------

void SettingsWidget::checkBoxOkEvent()
    {
    storeSettingsToiniFile();
    emit settingsEvent(true);
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::storeSettingsToiniFile()
//
//----------------------------------------------------------------------------------------------------------------------------

void SettingsWidget::storeSettingsToiniFile()
    {
    QSettings appSettings(SETTINGS_INI_PATH, QSettings::IniFormat);
    isInternetSelected ? (mSelectedScope = 1) : (mSelectedScope = 0);
    appSettings.setValue("selectedcategory", mSelectedScope);
    appSettings.setValue("devicecount", mDeviceCategoryRefList.count());
    if (!isInternetSelected)
        {
        int j = 0;
        for (int i = 0; i < mDeviceCategoryRefList.count(); i++)
            {
            if (mDeviceMapping.at(i))
                {
                if (mDeviceCheckBoxList.at(j)->checkState() == Qt::Checked)
                    {
                    appSettings.setValue(mDeviceCategoryRefList.at(i), 1);
                    emit selectedItemCategory(i, true);
                    }
                else
                    {
                    appSettings.setValue(mDeviceCategoryRefList.at(i), 0);
                    emit selectedItemCategory(i, false);
                    }
                j++;
                }
            else
                {
                appSettings.setValue(mDeviceCategoryRefList.at(i), 0);
                emit selectedItemCategory(i, false);
                }
            }
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::loadBaseSettings()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::loadBaseSettings()
    {
    QSettings appSettings(SETTINGS_INI_PATH, QSettings::IniFormat);
    mSelectedScope = appSettings.value("selectedcategory", 0).toInt();
    mSelectedScope ? (isInternetSelected = true) : (isInternetSelected
            = false);
    }

//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::loadDeviceSettings()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::loadDeviceSettings()
    {
    QSettings appSettings(SETTINGS_INI_PATH, QSettings::IniFormat);
    int value;
    if (!isInternetSelected)
        {
        int j = 0;
        for (int i = 0; i < mDeviceCategoryRefList.count(); i++)
            {
            value
                    = appSettings.value(mDeviceCategoryRefList.at(i), 1).toInt();
            if (mDeviceMapping.count() && mDeviceCheckBoxList.count()
                    && mDeviceMapping.at(i))
                {
                if (value)
                    {
                    mDeviceCheckBoxList.at(j)->setCheckState(Qt::Checked);
                    }
                else
                    {
                    mDeviceCheckBoxList.at(j)->setCheckState(Qt::Unchecked);
                    }
                j++;
                }
            value
                  ? (emit selectedItemCategory(i, true))
                     : (emit selectedItemCategory(i, false));
            }
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//SettingsWidget::isInternetSearchOptionSelected()
//
//----------------------------------------------------------------------------------------------------------------------------
bool SettingsWidget::isInternetSearchOptionSelected()
    {
    loadBaseSettings();
    return isInternetSelected;
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::storeDefaultSettings()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::storeDefaultSettings()
    {
    QSettings appSettings(SETTINGS_INI_PATH, QSettings::IniFormat);
    if (!appSettings.contains("selectedcategory")) // change the settings for the first time only
        {
        mSelectedScope = 0;
        appSettings.setValue("selectedcategory", mSelectedScope);
        appSettings.setValue("devicecount", mDeviceCategoryRefList.count());
        for (int i = 0; i < mDeviceCategoryRefList.count(); i++)
            appSettings.setValue(mDeviceCategoryRefList.at(i), 1);
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::slotproviderDetails()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::slotproviderDetails(QString name, HbIcon icon, int id)
    {
    mServiceProviders.insert(id, name);
    emit ISProvidersIcon(icon, id);
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::slotdefaultProvider()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::slotdefaultProvider(const int value)
    {
    mSelectedProvider = value;
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::slotitemSelected()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::slotitemSelected(int value)
    {
    QMapIterator<int, QString> i(mServiceProviders);
    int selectedindex = 0;
    while (i.hasNext())
        {
        i.next();
        if (selectedindex == value)
            {
            mInternetHandler->setDefaultProvider(i.key());
            break;
            }
        selectedindex++;
        }
    }
//----------------------------------------------------------------------------------------------------------------------------
//void SettingsWidget::loadIS()
//
//----------------------------------------------------------------------------------------------------------------------------
void SettingsWidget::loadIS()
    {
    if (!mInternetHandler)
        {
        mInternetHandler = new OnlineHandler();
        connect(mInternetHandler,
                SIGNAL(providerDetails(QString, HbIcon, int)), this,
                SLOT(slotproviderDetails(QString, HbIcon, int)));

        connect(mInternetHandler, SIGNAL(defaultProvider(const int)), this,
                SLOT(slotdefaultProvider(const int)));
        mInternetHandler->readSettings();
        }
    }