src/hbcore/i18n/hbfindfile.cpp
changeset 7 923ff622b8b9
parent 6 c3690ec91ef8
child 23 e6ad4ef83b23
equal deleted inserted replaced
6:c3690ec91ef8 7:923ff622b8b9
    33 #include <QFileInfo>
    33 #include <QFileInfo>
    34 #include <hbfindfile.h>
    34 #include <hbfindfile.h>
    35 #include <qbasicatomic.h>
    35 #include <qbasicatomic.h>
    36 #include <QDir>
    36 #include <QDir>
    37 
    37 
       
    38 #if defined(Q_OS_SYMBIAN)
       
    39     #define PLATFORM_WITH_DRIVES
       
    40 #elif defined(Q_OS_WIN32)
       
    41     #define PLATFORM_WITH_DRIVES
       
    42 #else
       
    43     #undef PLATFORM_WITH_DRIVES
       
    44 #endif
       
    45 
    38 #ifdef Q_OS_SYMBIAN
    46 #ifdef Q_OS_SYMBIAN
    39 /*!
    47 /*!
    40     Convert path to Symbian version
    48     Convert path to Symbian version
    41 */
    49 */
    42 static void toSymbianPath(QString &path) {    
    50 static void toSymbianPath(QString &path) {    
    53 /*!
    61 /*!
    54     @beta
    62     @beta
    55     @hbcore
    63     @hbcore
    56     \class HbFindFile
    64     \class HbFindFile
    57     \brief Checks from which drive a certain file is found.
    65     \brief Checks from which drive a certain file is found.
       
    66      
       
    67     Example:
       
    68 
       
    69     \snippet{unittest_hbfindfile/unittest_hbfindfile.cpp,1} 
       
    70     
       
    71 */
       
    72 
       
    73 /*!
    58     Scans drives through and adds drive information to \a str if file is found.
    74     Scans drives through and adds drive information to \a str if file is found.
    59     
    75 
    60     \attention Cross-Platform API
    76     \attention Cross-Platform API
    61 
    77 
    62     \param str is file and path beginning with "/"
    78     \param str is file and path beginning with "/"
    63     \param defaultDrive is drive letter which should be checked first. Default value is null.
    79     \param defaultDrive is drive letter which should be checked first. Default value is null.
    64     
    80     
    65     \return True if file is found. Otherwise return false.
    81     \return True if file is found. Otherwise return false.
    66 */
    82 */
    67 bool HbFindFile::hbFindFile(QString &str, const QChar &defaultDrive)
    83 bool HbFindFile::hbFindFile(QString &str, const QChar &defaultDrive)
    68 {
    84 {
    69 #ifdef Q_OS_SYMBIAN
    85 #if defined(Q_OS_SYMBIAN)
    70     RFs& fs = CCoeEnv::Static()->FsSession();
    86     RFs& fs = CCoeEnv::Static()->FsSession();
    71     TFindFile ff(fs);
    87     TFindFile ff(fs);
    72     QString str2 = str;
    88     QString str2 = str;
    73     toSymbianPath(str2);
    89     toSymbianPath(str2);
    74     TPtrC fName((ushort*)(str2.constData()),str2.length());
    90     TPtrC fName((ushort*)(str2.constData()),str2.length());
    90         return true;
   106         return true;
    91     }    
   107     }    
    92     else {
   108     else {
    93         return false;
   109         return false;
    94     }
   110     }
    95 #else
   111 #elif defined(Q_OS_WIN32)
    96     QString file = str;
   112     QString file = str;
    97     if (!defaultDrive.isNull()) {
   113     if (!defaultDrive.isNull()) {
    98         file = defaultDrive + QString(":") + str;
   114         file = defaultDrive + QString(":") + str;
    99         QFileInfo info(file);
   115         QFileInfo info(file);
   100         if (info.exists()) {
   116         if (info.exists()) {
   114             str = file;
   130             str = file;
   115             return true;
   131             return true;
   116         }
   132         }
   117     }
   133     }
   118     return false;
   134     return false;
   119 #endif
   135 #else
   120     
   136     Q_UNUSED(defaultDrive);
   121 }
   137     QFileInfo info(str);
   122 
   138     return info.exists();
       
   139 #endif   
       
   140 }
       
   141 
       
   142 #ifdef PLATFORM_WITH_DRIVES
   123 /*!
   143 /*!
   124     Helper class
   144     Helper class
   125 */
   145 */
   126 class AvailableDrives : public QString
   146 class AvailableDrives : public QString
   127 {
   147 {
   135 AvailableDrives::AvailableDrives() {
   155 AvailableDrives::AvailableDrives() {
   136 #ifdef Q_OS_SYMBIAN    
   156 #ifdef Q_OS_SYMBIAN    
   137     RFs& fs = CCoeEnv::Static()->FsSession();
   157     RFs& fs = CCoeEnv::Static()->FsSession();
   138     TDriveList driveList;
   158     TDriveList driveList;
   139     fs.DriveList(driveList);
   159     fs.DriveList(driveList);
       
   160     if ( driveList.Size() == 0 ) {
       
   161         return;
       
   162     }
       
   163     
   140     TChar driveLetter;
   164     TChar driveLetter;
   141     
   165     
   142     // add first C and then Y..A and then Z.
   166     // add first C and then Y..A and then Z.
   143     TInt driveNumber;
   167     TInt driveNumber;
   144     if (driveList[EDriveC]) {        
   168     if (driveList[EDriveC]) {        
   148         this->append(cC);
   172         this->append(cC);
   149     }
   173     }
   150     for (driveNumber = EDriveY; driveNumber >= EDriveA; --driveNumber) {
   174     for (driveNumber = EDriveY; driveNumber >= EDriveA; --driveNumber) {
   151         if (driveNumber == EDriveC) {
   175         if (driveNumber == EDriveC) {
   152             continue;
   176             continue;
   153         }
   177         } else {
   154         else {
       
   155             if (driveList[driveNumber]) {
   178             if (driveList[driveNumber]) {
   156                 fs.DriveToChar(driveNumber, driveLetter);
   179                 fs.DriveToChar(driveNumber, driveLetter);
   157                 QChar c = static_cast<QChar>(driveLetter);
   180                 QChar c = static_cast<QChar>(driveLetter);
   158                 this->append(c);
   181                 this->append(c);
   159             }    
   182             }    
   162     if (driveList[EDriveZ]) {    
   185     if (driveList[EDriveZ]) {    
   163         driveNumber = EDriveZ;
   186         driveNumber = EDriveZ;
   164         fs.DriveToChar(driveNumber, driveLetter);
   187         fs.DriveToChar(driveNumber, driveLetter);
   165         QChar cZ = static_cast<QChar>(driveLetter);
   188         QChar cZ = static_cast<QChar>(driveLetter);
   166         this->append(cZ);
   189         this->append(cZ);
   167     }
   190     }  
   168 
   191 #else // Q_OS_SYMBIAN           
   169     
       
   170 #else            
       
   171      QFileInfoList fil = QDir::drives();
   192      QFileInfoList fil = QDir::drives();
   172      for (int j=0; j< fil.length(); j++) {
   193      for (int j=0; j< fil.length(); j++) {
   173          QString fp = fil.at(j).filePath();
   194          QString fp = fil.at(j).filePath();
       
   195          if ( fp.isEmpty() ) {
       
   196             return;
       
   197         }
   174          
   198          
   175          if ((!fp.isEmpty()) && (fp[0] != '/') && (fp[0] != '\\')) {                
   199          if ( (fp[0] != '/') && (fp[0] != '\\') ) {                
   176          this->append(fp[0]);
   200          this->append(fp[0]);
   177          }        
   201          }        
   178      }
   202      }
   179 #endif     
   203 #endif // Q_OS_SYMBIAN    
   180 }
   204 }
   181 
   205 
   182 Q_GLOBAL_STATIC(AvailableDrives, gs_AvailableDrives)
   206 Q_GLOBAL_STATIC(AvailableDrives, gs_AvailableDrives)
       
   207 #endif  // PLATFORM_WITH_DRIVES 
   183 
   208 
   184 /*!
   209 /*!
   185     \attention Cross-Platform API
   210     \attention Cross-Platform API
   186     
   211     
   187     \returns Available drive(s) if platform supports (Eg. Symbian, Windows...). 
   212     \returns Available drive(s) if platform supports (Eg. Symbian, Windows...). 
   188     If platform doesn't support drive(s) (Eg. Linux) then empty QString is returned. 
   213     If platform doesn't support drive(s) (Eg. Linux) then empty QString is returned. 
   189 */
   214 */
   190 QString HbFindFile::availableDrives()
   215 QString HbFindFile::availableDrives()
   191 {
   216 {
       
   217 #ifdef PLATFORM_WITH_DRIVES
   192      QString *str = gs_AvailableDrives();
   218      QString *str = gs_AvailableDrives();
   193      if (str) {
   219      if (str) {
   194          return *str;         
   220          return *str;         
   195      }
   221      } else {
   196      else {
       
   197          return QString(); 
   222          return QString(); 
   198      }
   223      }
   199 }
   224 #else // PLATFORM_WITH_DRIVES           
       
   225      return QString(); 
       
   226 #endif  // PLATFORM_WITH_DRIVES 
       
   227 }