utilities/downloadmanager/src/downloadinfo.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /**
       
     2    This file is part of CWRT package **
       
     3 
       
     4    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). **
       
     5 
       
     6    This program is free software: you can redistribute it and/or modify
       
     7    it under the terms of the GNU (Lesser) General Public License as 
       
     8    published by the Free Software Foundation, version 2.1 of the License. 
       
     9    This program is distributed in the hope that it will be useful, but
       
    10    WITHOUT ANY WARRANTY; without even the implied warranty of 
       
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
       
    12    (Lesser) General Public License for more details. You should have 
       
    13    received a copy of the GNU (Lesser) General Public License along 
       
    14    with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 */
       
    16 
       
    17 #include "downloadinfo.h"
       
    18 #include "dmcommon.h"
       
    19 #include <QStringList>
       
    20 #include <QSettings>
       
    21 #define ORGANIZATION "Nokia"
       
    22 
       
    23 class DownloadInfoPrivate
       
    24 {
       
    25     DM_DECLARE_PUBLIC(DownloadInfo);
       
    26 public:
       
    27     DownloadInfoPrivate();
       
    28     ~DownloadInfoPrivate();
       
    29 
       
    30     QSettings* m_dlInfo;
       
    31     QString m_clientName;
       
    32 
       
    33 };
       
    34 
       
    35 DownloadInfoPrivate::DownloadInfoPrivate()
       
    36 {
       
    37     m_dlInfo = 0;
       
    38     m_clientName = "";
       
    39 }
       
    40 
       
    41 DownloadInfoPrivate::~DownloadInfoPrivate()
       
    42 {
       
    43     if(m_dlInfo)
       
    44     {
       
    45          m_dlInfo->sync();
       
    46          delete m_dlInfo;
       
    47          m_dlInfo = 0;
       
    48     }
       
    49 }
       
    50 
       
    51 /*
       
    52 Constructor
       
    53 */
       
    54 DownloadInfo::DownloadInfo(const QString& clientName)
       
    55 {
       
    56     DM_INITIALIZE(DownloadInfo);
       
    57     priv->m_clientName = clientName;
       
    58     priv->m_dlInfo = new QSettings(ORGANIZATION, clientName);
       
    59 }
       
    60 
       
    61 /*
       
    62 Destructor
       
    63 */
       
    64 DownloadInfo::~DownloadInfo()
       
    65 {
       
    66     DM_UNINITIALIZE(DownloadInfo);
       
    67 }
       
    68 
       
    69 /*
       
    70 Sets a string value. Gives option to updte the info file immediately or later
       
    71 Returns : 0 on success, non zero on error
       
    72 */
       
    73 int DownloadInfo::setValue(int aDlId, Key aKey, const QString& aStrValue, int aParentId /*= INVALID_DL_ID*/)
       
    74 {
       
    75     DM_PRIVATE(DownloadInfo);
       
    76     if(aParentId > INVALID_DL_ID)
       
    77         priv->m_dlInfo->setValue(genStrKey(aParentId, aDlId, aKey), aStrValue);
       
    78     else
       
    79         priv->m_dlInfo->setValue(genStrKey(aDlId, aKey), aStrValue);
       
    80     return 0;
       
    81 }
       
    82 
       
    83 /*
       
    84 Sets a string value. Gives option to updte the info file immediately or later
       
    85 Returns : 0 on success, non zero on error
       
    86 */
       
    87 int DownloadInfo::setValueForChild(int aDlId, Key aKey, const QString& aStrValue, int aChildId /*= INVALID_DL_ID*/)
       
    88 {
       
    89     DM_PRIVATE(DownloadInfo);
       
    90     if(aChildId > INVALID_DL_ID)
       
    91         priv->m_dlInfo->setValue(genStrKey(aDlId, aChildId, aKey), aStrValue);
       
    92     else
       
    93         priv->m_dlInfo->setValue(genStrKey(aDlId, aKey), aStrValue);
       
    94     return 0;
       
    95 }
       
    96 
       
    97 /*
       
    98 Sets an int value. Gives option to updte the info file immediately or later
       
    99 Returns : 0 on success, non zero on error
       
   100 */
       
   101 int DownloadInfo::setValue(int aDlId, Key aKeyInt, long aLongValue, int aParentId /*= INVALID_DL_ID*/)
       
   102 {
       
   103     DM_PRIVATE(DownloadInfo);
       
   104     QString strKey;
       
   105     if(aParentId > INVALID_DL_ID)
       
   106         strKey = genStrKey(aParentId, aDlId, aKeyInt);
       
   107     else
       
   108         strKey = genStrKey(aDlId, aKeyInt);
       
   109     QVariant v((int)aLongValue);
       
   110     priv->m_dlInfo->setValue(strKey, v);
       
   111     return 0;
       
   112 }
       
   113 
       
   114 /*
       
   115 Sets an int value. Gives option to updte the info file immediately or later
       
   116 Returns : 0 on success, non zero on error
       
   117 */
       
   118 int DownloadInfo::setValueForChild(int aDlId, Key aKeyInt, long aLongValue, int aChildId /*= INVALID_DL_ID*/)
       
   119 {
       
   120     DM_PRIVATE(DownloadInfo);
       
   121     QString strKey;
       
   122     if(aChildId > INVALID_DL_ID)
       
   123         strKey = genStrKey(aDlId, aChildId, aKeyInt);
       
   124     else
       
   125         strKey = genStrKey(aDlId, aKeyInt);
       
   126     QVariant v((int)aLongValue);
       
   127     priv->m_dlInfo->setValue(strKey, v);
       
   128     return 0;
       
   129 }
       
   130 
       
   131 /*
       
   132 Sets media object list belonging to the parent. Gives option to updte the info file immediately or later
       
   133 Returns : 0 on success, non zero on error
       
   134 */
       
   135 int DownloadInfo::setValue(int aDlId, Key aKey, const QList<QVariant>& aChildIds)
       
   136 {
       
   137     DM_PRIVATE(DownloadInfo);
       
   138     QString strKey = genStrKey(aDlId, aKey);
       
   139     QVariant v(aChildIds);
       
   140     priv->m_dlInfo->setValue(strKey, v);
       
   141     return 0;
       
   142 }
       
   143 
       
   144 /*
       
   145 Updates(serializes) the info with all set values at once.
       
   146 Returns : 0 on success, non zero on error
       
   147 */
       
   148 int DownloadInfo::update()
       
   149 {
       
   150     DM_PRIVATE(DownloadInfo);
       
   151     priv->m_dlInfo->sync();
       
   152     return 0;
       
   153 }
       
   154 
       
   155 /*
       
   156 Deletes the download info of a particular download represented by aDlId.
       
   157 Returns : 0 on success, non zero on error
       
   158 */
       
   159 int DownloadInfo::remove(int aDlId, int aParentId /*= INVALID_DL_ID*/)
       
   160 {
       
   161     DM_PRIVATE(DownloadInfo);
       
   162     QString strDlId;
       
   163     strDlId = priv->m_clientName;
       
   164     strDlId.append("/");
       
   165 
       
   166     QString str;
       
   167     if(aParentId > INVALID_DL_ID)
       
   168     {
       
   169         str.setNum(aParentId);
       
   170         strDlId.append(str);
       
   171         strDlId.append("/");
       
   172     }
       
   173 
       
   174     str.setNum(aDlId);
       
   175     strDlId.append(str);
       
   176     priv->m_dlInfo->remove(strDlId);
       
   177     return 0;
       
   178 }
       
   179 
       
   180 /*
       
   181 Retrieves the string value
       
   182 Returns : 0 on success, non zero on error
       
   183 */
       
   184 int DownloadInfo::getValue(int aDlId, Key aKeyStr, QString& aStrValue, int aParentId)
       
   185 {
       
   186     DM_PRIVATE(DownloadInfo);
       
   187     aStrValue = "";
       
   188     QString strDlId;
       
   189     if (aParentId > INVALID_DL_ID)
       
   190         strDlId = genStrKey(aParentId, aDlId, aKeyStr);
       
   191     else
       
   192         strDlId = genStrKey(aDlId, aKeyStr);
       
   193 
       
   194     if(priv->m_dlInfo->contains(strDlId))
       
   195     {
       
   196         aStrValue = priv->m_dlInfo->value(strDlId).toString();
       
   197         return 0;
       
   198     }
       
   199     return -1;
       
   200 }
       
   201 
       
   202 /*
       
   203 Retrieves the string value
       
   204 Returns : 0 on success, non zero on error
       
   205 */
       
   206 int DownloadInfo::getValueForChild(int aDlId, Key aKeyStr, QString& aStrValue, int aChildId /*= INVALID_DL_ID*/)
       
   207 {
       
   208     DM_PRIVATE(DownloadInfo);
       
   209     aStrValue = "";
       
   210     QString strDlId;
       
   211     if (aChildId > INVALID_DL_ID)
       
   212         strDlId = genStrKey(aDlId, aChildId, aKeyStr);
       
   213     else
       
   214         strDlId = genStrKey(aDlId, aKeyStr);
       
   215 
       
   216     if(priv->m_dlInfo->contains(strDlId))
       
   217     {
       
   218         aStrValue = priv->m_dlInfo->value(strDlId).toString();
       
   219         return 0;
       
   220     }
       
   221     return -1;
       
   222 }
       
   223 
       
   224 /*
       
   225 Retrieves the int value
       
   226 Returns : 0 on success, non zero on error
       
   227 */
       
   228 int DownloadInfo::getValue(int aDlId, Key aKeyInt, long& aLongValue, int aParentId)
       
   229 {
       
   230     DM_PRIVATE(DownloadInfo);
       
   231     QString strDlId;
       
   232     if(aParentId > INVALID_DL_ID)
       
   233         strDlId = genStrKey(aParentId, aDlId, aKeyInt);
       
   234     else
       
   235         strDlId = genStrKey(aDlId, aKeyInt);
       
   236 
       
   237     if(priv->m_dlInfo->contains(strDlId))
       
   238     {
       
   239         aLongValue = priv->m_dlInfo->value(strDlId).toInt();
       
   240         return 0;
       
   241     }
       
   242     return -1;
       
   243 }
       
   244 
       
   245 /*
       
   246 Retrieves the int value
       
   247 Returns : 0 on success, non zero on error
       
   248 */
       
   249 int DownloadInfo::getValueForChild(int aDlId, Key aKeyInt, long& aLongValue, int aChildId /*= INVALID_DL_ID*/)
       
   250 {
       
   251     DM_PRIVATE(DownloadInfo);
       
   252     QString strDlId;
       
   253     if(aChildId > INVALID_DL_ID)
       
   254         strDlId = genStrKey(aDlId, aChildId, aKeyInt);
       
   255     else
       
   256         strDlId = genStrKey(aDlId, aKeyInt);
       
   257 
       
   258     if(priv->m_dlInfo->contains(strDlId)) 
       
   259     {
       
   260         aLongValue = priv->m_dlInfo->value(strDlId).toInt();
       
   261         return 0;
       
   262     }
       
   263     return -1;
       
   264 }
       
   265 
       
   266 /*
       
   267 Retrieves the mediaObject list
       
   268 Returns : 0 on success, non zero on error
       
   269 */
       
   270 int DownloadInfo::getValue(int aDlId, Key aKey, QList<QVariant>& aChildIds)
       
   271 {
       
   272     DM_PRIVATE(DownloadInfo);
       
   273     QString strDlId = genStrKey(aDlId, aKey);
       
   274     if(priv->m_dlInfo->contains(strDlId)) 
       
   275     {
       
   276         QStringList strList(priv->m_dlInfo->value(strDlId).toStringList());
       
   277         for (int i = 0; i < strList.count(); i++) 
       
   278             aChildIds.append(strList[i]);
       
   279         return 0;
       
   280     }
       
   281     return -1;
       
   282 }
       
   283 
       
   284 /*
       
   285 Returns all download ids in a vector
       
   286 */
       
   287 QVector<int> DownloadInfo::getAllDownloads(const QString& aClientName)
       
   288 {
       
   289     QVector<int> ids;
       
   290     QSettings *dlInfo = new QSettings(ORGANIZATION, aClientName);
       
   291     dlInfo->beginGroup(aClientName);
       
   292     QStringList idList = dlInfo->childGroups();
       
   293     dlInfo->endGroup();
       
   294     for(int i=0; i<idList.size(); i++)
       
   295     {
       
   296         ids.append(idList[i].toInt());
       
   297     }
       
   298     delete dlInfo;
       
   299     return ids;
       
   300 }
       
   301 
       
   302 /*
       
   303 Helper function to generate a string key combining dlId and the InfoKey
       
   304 */
       
   305 QString DownloadInfo::genStrKey(int aDlId, Key aKey)
       
   306 {   
       
   307     DM_PRIVATE(DownloadInfo);
       
   308     QString strDlId = "";
       
   309     strDlId = priv->m_clientName;
       
   310     strDlId.append("/");
       
   311 
       
   312     QString str;
       
   313     str.setNum(aDlId);
       
   314     strDlId.append(str);
       
   315 
       
   316     strDlId.append("/");
       
   317     str.setNum((int)aKey);
       
   318     strDlId.append(str);
       
   319 
       
   320     return strDlId;
       
   321 }
       
   322 
       
   323 /*
       
   324 Helper function to generate a string key combining dlId, mediaObjdlId and the InfoKey
       
   325 */
       
   326 QString DownloadInfo::genStrKey(int aParentId, int aChildId, Key aKey)
       
   327 {   
       
   328     DM_PRIVATE(DownloadInfo);
       
   329     QString strDlId = "";
       
   330     strDlId = priv->m_clientName;
       
   331     strDlId.append("/");
       
   332 
       
   333     QString str;
       
   334     str.setNum(aParentId);
       
   335     strDlId.append(str);
       
   336     strDlId.append("/");
       
   337 
       
   338     str.setNum(aChildId);
       
   339     strDlId.append(str);
       
   340 
       
   341     strDlId.append("/");
       
   342     str.setNum((int)aKey);
       
   343     strDlId.append(str);
       
   344 
       
   345     return strDlId;
       
   346 }