| 37 |      1 | /*
 | 
|  |      2 |  * Copyright (c) 2010 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 |  *     The source file for messaging's audio fetcher model
 | 
|  |     16 |  */
 | 
|  |     17 | 
 | 
|  |     18 | // SYSTEM INCLUDES
 | 
|  |     19 | #include <xqutils.h>
 | 
|  |     20 | #include <xqappmgr.h>
 | 
|  |     21 | 
 | 
|  |     22 | // USER INCLUDES
 | 
|  |     23 | #include "msgaudiofetchermodel.h"
 | 
|  |     24 | #include "msgaudioselectionengine.h"
 | 
|  |     25 | 
 | 
|  |     26 | MsgAudioFetcherModel::MsgAudioFetcherModel(QObject *parent) :
 | 
|  |     27 |     QStandardItemModel(parent), mSelectionEngine(0)
 | 
|  |     28 | {
 | 
|  |     29 |     init();   
 | 
|  |     30 | }
 | 
|  |     31 | 
 | 
|  |     32 | MsgAudioFetcherModel::~MsgAudioFetcherModel()
 | 
|  |     33 | {
 | 
|  |     34 |     delete mSelectionEngine;
 | 
|  |     35 | }
 | 
|  |     36 | 
 | 
|  |     37 | void MsgAudioFetcherModel::init()
 | 
|  |     38 | {
 | 
|  |     39 |     mSelectionEngine = new MsgAudioSelectionEngine();
 | 
|  |     40 | 
 | 
|  |     41 |     connect(mSelectionEngine, SIGNAL(mdeSessionOpened()), this, SLOT(mdeSessionOpened()));
 | 
|  |     42 |     connect(mSelectionEngine, SIGNAL(mdeSessionError(int)), this, SLOT(mdeSessionError(int)));
 | 
|  |     43 |     connect(mSelectionEngine, SIGNAL(queryComplete(QStringList, QStringList)), this,
 | 
|  |     44 |         SLOT(queryComplete(QStringList, QStringList)));
 | 
|  |     45 |     connect(mSelectionEngine, SIGNAL(queryError(int)), this, SLOT(queryError(int)));
 | 
|  |     46 | 
 | 
|  |     47 |     connect(mSelectionEngine, SIGNAL(notifyObjectChanged()), this, SLOT(onObjectChanged()));
 | 
|  |     48 |     mSelectionEngine->CreateMetaDataSession();
 | 
|  |     49 | }
 | 
|  |     50 | 
 | 
|  |     51 | void MsgAudioFetcherModel::getRomToneFiles()
 | 
|  |     52 | {
 | 
|  |     53 |     // z:\data\sounds\digital
 | 
|  |     54 |     QDir digitalsoundPath(XQUtils::romRootPath() + XQUtils::digitalSoundsPath());
 | 
|  |     55 |     addToneFiles(digitalsoundPath);
 | 
|  |     56 | 
 | 
|  |     57 |     //z:\data\sounds\simple
 | 
|  |     58 |     QDir simpleSoundPath(XQUtils::romRootPath() + XQUtils::simpleSoundsPath());
 | 
|  |     59 |     addToneFiles(simpleSoundPath);
 | 
|  |     60 | 
 | 
|  |     61 |     //z:\data\sounds
 | 
|  |     62 |     QDir soundPath(XQUtils::romRootPath() + XQUtils::soundsPath());
 | 
|  |     63 |     addToneFiles(soundPath);
 | 
|  |     64 | }
 | 
|  |     65 | 
 | 
|  |     66 | void MsgAudioFetcherModel::addToneFiles(QDir& toneDir)
 | 
|  |     67 | {
 | 
|  |     68 |     // List only xx.amr files
 | 
|  |     69 |     QStringList filters;
 | 
|  |     70 |     filters << "*.amr";
 | 
|  |     71 |     toneDir.setNameFilters(filters);
 | 
|  |     72 | 
 | 
|  |     73 |     QStringList soundList = toneDir.entryList();
 | 
|  |     74 |     for (int i = 0; i < soundList.size(); ++i) {
 | 
|  |     75 |         QString fullPath = QDir::toNativeSeparators(toneDir.absolutePath());
 | 
|  |     76 |         fullPath.append(QDir::separator());
 | 
|  |     77 |         fullPath.append(soundList.at(i));
 | 
|  |     78 |         addRow(fullPath);
 | 
|  |     79 |     }
 | 
|  |     80 | }
 | 
|  |     81 | 
 | 
|  |     82 | QVariant MsgAudioFetcherModel::data(const QModelIndex &index, int role) const
 | 
|  |     83 | {
 | 
|  |     84 |     return QStandardItemModel::data(index, role);
 | 
|  |     85 | }
 | 
|  |     86 | 
 | 
|  |     87 | void MsgAudioFetcherModel::addRow(QString filepath)
 | 
|  |     88 | {
 | 
|  |     89 |     if(isDRM(filepath))
 | 
|  |     90 |     {
 | 
|  |     91 |         // don't insert DRM protected files, as sending itself
 | 
|  |     92 |         // is not allowed in 'Restricted Mode'
 | 
|  |     93 |         return;
 | 
|  |     94 |     }
 | 
|  |     95 | 
 | 
|  |     96 |     QFileInfo fInfo(filepath);
 | 
|  |     97 |     QStandardItem* item = new QStandardItem();
 | 
|  |     98 |     QString filename = fInfo.baseName();
 | 
|  |     99 |     item->setData(filename, Qt::DisplayRole);
 | 
|  |    100 |     item->setData(filepath, Qt::UserRole);
 | 
|  |    101 | 
 | 
|  |    102 |     // get insert index
 | 
|  |    103 |     int index = this->insertIndex(0, rowCount(), filename);
 | 
|  |    104 |     insertRow(index, item);
 | 
|  |    105 | }
 | 
|  |    106 | 
 | 
|  |    107 | int MsgAudioFetcherModel::insertIndex(int low, int high, QString value)
 | 
|  |    108 | {
 | 
|  |    109 |     if (low == high) {
 | 
|  |    110 |         return low;
 | 
|  |    111 |     }
 | 
|  |    112 |     int middle = (low + high - 1) / 2;
 | 
|  |    113 |     QModelIndex lowItemIndex = ((QStandardItemModel *) this)->index(low, 0);
 | 
|  |    114 |     QModelIndex highItemIndex = ((QStandardItemModel *) this)->index(high - 1, 0);
 | 
|  |    115 |     QModelIndex middleItemIndex = ((QStandardItemModel *) this)->index(middle, 0);
 | 
|  |    116 |     QString lowString = data(lowItemIndex).toString();
 | 
|  |    117 |     QString highString = data(highItemIndex).toString();
 | 
|  |    118 |     QString middleString = data(middleItemIndex).toString();
 | 
|  |    119 | 
 | 
|  |    120 |     if (value >= highString) {
 | 
|  |    121 |         return high;
 | 
|  |    122 |     }
 | 
|  |    123 |     if (value < lowString) {
 | 
|  |    124 |         return low;
 | 
|  |    125 |     }
 | 
|  |    126 |     high = high - 1;
 | 
|  |    127 |     while (low < high) {
 | 
|  |    128 |         middle = (low + high) / 2;
 | 
|  |    129 |         middleItemIndex = ((QStandardItemModel *) this)->index(middle, 0);
 | 
|  |    130 |         middleString = data(middleItemIndex).toString();
 | 
|  |    131 |         if (value >= middleString) {
 | 
|  |    132 |             low = middle + 1;
 | 
|  |    133 |         }
 | 
|  |    134 |         else {
 | 
|  |    135 |             high = middle;
 | 
|  |    136 |         }
 | 
|  |    137 |     }
 | 
|  |    138 |     return low;
 | 
|  |    139 | }
 | 
|  |    140 | 
 | 
|  |    141 | void MsgAudioFetcherModel::clearAll()
 | 
|  |    142 | {    
 | 
|  |    143 |     QStandardItemModel::clear();
 | 
|  |    144 | }
 | 
|  |    145 | 
 | 
|  |    146 | void MsgAudioFetcherModel::mdeSessionOpened()
 | 
|  |    147 | {
 | 
|  |    148 |     mSelectionEngine->QueryTones();
 | 
|  |    149 |     getRomToneFiles();
 | 
|  |    150 | }
 | 
|  |    151 | 
 | 
|  |    152 | void MsgAudioFetcherModel::queryComplete(const QStringList &nameList, const QStringList &uriList)
 | 
|  |    153 | {
 | 
|  |    154 |     for (int i = 0; i < nameList.size(); ++i) {
 | 
|  |    155 |         addRow(uriList.at(i));
 | 
|  |    156 |     }
 | 
|  |    157 | }
 | 
|  |    158 | 
 | 
|  |    159 | void MsgAudioFetcherModel::queryError(int error)
 | 
|  |    160 | {
 | 
|  |    161 |     Q_UNUSED(error);        
 | 
|  |    162 | }
 | 
|  |    163 | 
 | 
|  |    164 | void MsgAudioFetcherModel::mdeSessionError(int error)
 | 
|  |    165 | {
 | 
|  |    166 |     Q_UNUSED(error);
 | 
|  |    167 | }
 | 
|  |    168 | 
 | 
|  |    169 | void MsgAudioFetcherModel::onObjectChanged()
 | 
|  |    170 | {
 | 
|  |    171 |     clearAll();
 | 
|  |    172 |     getRomToneFiles();
 | 
|  |    173 |     mSelectionEngine->QueryTones();
 | 
|  |    174 | }
 | 
|  |    175 | 
 | 
|  |    176 | bool MsgAudioFetcherModel::isDRM(QString filepath)
 | 
|  |    177 | {
 | 
|  |    178 |     QList<int> attrNames;
 | 
|  |    179 |     attrNames.append(XQApplicationManager::IsProtected);
 | 
|  |    180 |     
 | 
|  |    181 |     QFile file(filepath);
 | 
|  |    182 |     QVariantList attrValues;
 | 
|  |    183 |     XQApplicationManager appmgr;
 | 
|  |    184 |     bool ok = appmgr.getDrmAttributes(file, attrNames, attrValues);
 | 
|  |    185 |     if(!ok)
 | 
|  |    186 |     {
 | 
|  |    187 |         return true;
 | 
|  |    188 |     }
 | 
|  |    189 |     return (0 < attrValues.at(0).toInt());
 | 
|  |    190 | }
 | 
|  |    191 | 
 | 
|  |    192 | //End of File
 |