utilities/downloadmanager/src/storageutility.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 "storageutility.h"
       
    18 #include <QFile>
       
    19 #include <QDir>
       
    20 #include <qregexp.h>
       
    21 
       
    22 
       
    23 int StorageUtility::generateUniqueFileName(const QString &fileName, const QString &tempPath,
       
    24         const QString& permPath, QString &outFileName)
       
    25 {
       
    26     int uniqueExt = 0;
       
    27     QString orgFilename = fileName;    
       
    28     QFileInfo fileinfo(tempPath, fileName);
       
    29     QFileInfo fileinfoPerm(permPath, fileName);
       
    30 
       
    31     while(true)
       
    32     {
       
    33         uniqueExt++;
       
    34         // check if file exists
       
    35         if (fileinfo.exists() || fileinfoPerm.exists()) {
       
    36             // a file with this name already exists in either temporary
       
    37             // or permanent download location
       
    38             QString tempFileName = orgFilename;
       
    39             QString ext = fileinfo.suffix();
       
    40             QString fileNoExt = tempFileName;
       
    41             if (!ext.isEmpty())
       
    42                 fileNoExt = tempFileName.mid(0, tempFileName.length()-ext.length()-1);
       
    43 
       
    44             // add index to filename
       
    45             QString strExt;
       
    46             strExt.setNum(uniqueExt);
       
    47             tempFileName =  fileNoExt+"("+strExt+")";
       
    48             if (!ext.isEmpty())
       
    49                 tempFileName = tempFileName+"."+ext;
       
    50 
       
    51             // set fileinfo to check again if any file with 
       
    52             // this new filename already exists
       
    53             fileinfo.setFile(tempPath, tempFileName);
       
    54             fileinfoPerm.setFile(permPath, tempFileName);
       
    55         }
       
    56         else {
       
    57             // this is a unique filename
       
    58             outFileName = fileinfo.fileName();
       
    59             return 0;
       
    60         }
       
    61     }   
       
    62     return -1;
       
    63 }
       
    64 
       
    65 void StorageUtility::validateFileName(const QString &fileName, QString &outFileName)
       
    66 {
       
    67     QString orgFileName = fileName;
       
    68     // Remove invalid filename characters
       
    69     orgFileName.replace( QRegExp("[?:*\"\\/<>|]"), "" );
       
    70 
       
    71     // Check if filename is empty
       
    72     outFileName = orgFileName.trimmed();
       
    73     if (outFileName.isEmpty())
       
    74         outFileName = FILENAME_UNKNOWN;
       
    75 }
       
    76 
       
    77 QString StorageUtility::createTemporaryPath(const QString& clientName)
       
    78 {
       
    79     /* create "downloads" folder : ./<clientname>/downloads */ 
       
    80     QString orgClientName = clientName;
       
    81     orgClientName.replace( QRegExp("[\\\\/:\\*\\?\"<>\\|]+"), "_" );
       
    82     QDir currentDir(QDir::current());
       
    83     QString tempPath = currentDir.absolutePath() + "/" + orgClientName + "/" + QObject::tr(TEMPORARY_FOLDER);
       
    84     currentDir.mkpath(tempPath);
       
    85     return tempPath;
       
    86 }