src/hbcore/i18n/hbfindfile.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    29 #include <eikenv.h> 
    29 #include <eikenv.h> 
    30 #endif
    30 #endif
    31 #include <QString>
    31 #include <QString>
    32 #include <QCoreApplication>
    32 #include <QCoreApplication>
    33 #include <QFileInfo>
    33 #include <QFileInfo>
    34 
       
    35 #include <hbfindfile.h>
    34 #include <hbfindfile.h>
       
    35 #include <qbasicatomic.h>
       
    36 #include <QDir>
       
    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 
       
    46 #ifdef Q_OS_SYMBIAN
       
    47 /*!
       
    48     Convert path to Symbian version
       
    49 */
       
    50 static void toSymbianPath(QString &path) {    
       
    51     int len=path.length();
       
    52     for (int i=0; i<len; i++) {
       
    53         QCharRef ref=path[i];
       
    54         if (ref == '/') {
       
    55            ref= '\\';
       
    56         }
       
    57     }
       
    58 }
       
    59 #endif
    36 
    60 
    37 /*!
    61 /*!
    38     @beta
    62     @beta
    39     @hbcore
    63     @hbcore
    40     \class HbFindFile
    64     \class HbFindFile
    41     \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 /*!
    42     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.
       
    75 
       
    76     \attention Cross-Platform API
    43 
    77 
    44     \param str is file and path beginning with "/"
    78     \param str is file and path beginning with "/"
    45     \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.
    46     \return true if file is found. Otherwise return false.
    80     
       
    81     \return True if file is found. Otherwise return false.
    47 */
    82 */
    48 bool HbFindFile::hbFindFile(QString &str, const QChar &defaultDrive)
    83 bool HbFindFile::hbFindFile(QString &str, const QChar &defaultDrive)
    49 {
    84 {
       
    85 #if defined(Q_OS_SYMBIAN)
       
    86     RFs& fs = CCoeEnv::Static()->FsSession();
       
    87     TFindFile ff(fs);
       
    88     QString str2 = str;
       
    89     toSymbianPath(str2);
       
    90     TPtrC fName((ushort*)(str2.constData()),str2.length());
       
    91     QString dNameString;
       
    92     
       
    93     if (!defaultDrive.isNull()) {
       
    94         dNameString.append(defaultDrive);
       
    95         dNameString += QString(":");
       
    96     }    
       
    97     dNameString += QString("\\");    
       
    98     TPtrC dName((ushort*)(dNameString.constData()),dNameString.length());
       
    99     TInt err=ff.FindByDir(fName, dName);
       
   100     if (err==KErrNone) {
       
   101         TParse p;
       
   102         p.Set(ff.File(), 0,0);
       
   103         TPtrC ptrC = p.Drive();
       
   104         QString str3 = QString::fromRawData((QChar*)(ushort*)ptrC.Ptr(),ptrC.Length()); 
       
   105         str.prepend(str3);
       
   106         return true;
       
   107     }    
       
   108     else {
       
   109         return false;
       
   110     }
       
   111 #elif defined(Q_OS_WIN32)
    50     QString file = str;
   112     QString file = str;
    51 #if defined(Q_OS_WIN32)
       
    52     file = "C:" + str;
       
    53 #endif
       
    54 #if !defined(Q_OS_SYMBIAN)
       
    55     QFileInfo info(file);
       
    56     if (info.exists()) {
       
    57         str = file;
       
    58         return true;
       
    59     }
       
    60     return false;
       
    61 #endif
       
    62 
       
    63     if (!defaultDrive.isNull()) {
   113     if (!defaultDrive.isNull()) {
    64         file = defaultDrive + QString(":") + str;
   114         file = defaultDrive + QString(":") + str;
    65         QFileInfo info(file);
   115         QFileInfo info(file);
    66         if (info.exists()) {
   116         if (info.exists()) {
    67             str = file;
   117             str = file;
    80             str = file;
   130             str = file;
    81             return true;
   131             return true;
    82         }
   132         }
    83     }
   133     }
    84     return false;
   134     return false;
    85 }
   135 #else
    86 
   136     Q_UNUSED(defaultDrive);
    87 /*!
   137     QFileInfo info(str);
    88     Returns available drives in device (Symbian). Empty for other platforms.
   138     return info.exists();
    89 */
   139 #endif   
    90 QString HbFindFile::availableDrives()
   140 }
       
   141 
       
   142 #ifdef PLATFORM_WITH_DRIVES
       
   143 /*!
       
   144     Helper class
       
   145 */
       
   146 class AvailableDrives : public QString
    91 {
   147 {
    92     QString drives = "";
   148 public:
    93 #if defined(Q_OS_SYMBIAN)
   149     AvailableDrives();    
       
   150 };
       
   151 
       
   152 /*!
       
   153     Search available drives
       
   154 */
       
   155 AvailableDrives::AvailableDrives() {
       
   156 #ifdef Q_OS_SYMBIAN    
    94     RFs& fs = CCoeEnv::Static()->FsSession();
   157     RFs& fs = CCoeEnv::Static()->FsSession();
    95     TDriveList driveList;
   158     TDriveList driveList;
    96     fs.DriveList(driveList);
   159     fs.DriveList(driveList);
       
   160     if ( driveList.Size() == 0 ) {
       
   161         return;
       
   162     }
       
   163     
    97     TChar driveLetter;
   164     TChar driveLetter;
    98     for (TInt driveNumber = EDriveA; driveNumber <= EDriveZ; driveNumber++) {
   165     
       
   166     // add first C and then Y..A and then Z.
       
   167     TInt driveNumber;
       
   168     if (driveList[EDriveC]) {        
       
   169         driveNumber = EDriveC;
    99         fs.DriveToChar(driveNumber, driveLetter);
   170         fs.DriveToChar(driveNumber, driveLetter);
   100         QChar c = static_cast<QChar>(driveLetter);
   171         QChar cC = static_cast<QChar>(driveLetter);    
   101         drives.append(c);
   172         this->append(cC);
   102     }
   173     }
   103 #endif
   174     for (driveNumber = EDriveY; driveNumber >= EDriveA; --driveNumber) {
   104     return drives;
   175         if (driveNumber == EDriveC) {
   105 }
   176             continue;
       
   177         } else {
       
   178             if (driveList[driveNumber]) {
       
   179                 fs.DriveToChar(driveNumber, driveLetter);
       
   180                 QChar c = static_cast<QChar>(driveLetter);
       
   181                 this->append(c);
       
   182             }    
       
   183         }    
       
   184     }
       
   185     if (driveList[EDriveZ]) {    
       
   186         driveNumber = EDriveZ;
       
   187         fs.DriveToChar(driveNumber, driveLetter);
       
   188         QChar cZ = static_cast<QChar>(driveLetter);
       
   189         this->append(cZ);
       
   190     }  
       
   191 #else // Q_OS_SYMBIAN           
       
   192      QFileInfoList fil = QDir::drives();
       
   193      for (int j=0; j< fil.length(); j++) {
       
   194          QString fp = fil.at(j).filePath();
       
   195          if ( fp.isEmpty() ) {
       
   196             return;
       
   197         }
       
   198          
       
   199          if ( (fp[0] != '/') && (fp[0] != '\\') ) {                
       
   200          this->append(fp[0]);
       
   201          }        
       
   202      }
       
   203 #endif // Q_OS_SYMBIAN    
       
   204 }
       
   205 
       
   206 Q_GLOBAL_STATIC(AvailableDrives, gs_AvailableDrives)
       
   207 #endif  // PLATFORM_WITH_DRIVES 
       
   208 
       
   209 /*!
       
   210     \attention Cross-Platform API
       
   211     
       
   212     \returns Available drive(s) if platform supports (Eg. Symbian, Windows...). 
       
   213     If platform doesn't support drive(s) (Eg. Linux) then empty QString is returned. 
       
   214 */
       
   215 QString HbFindFile::availableDrives()
       
   216 {
       
   217 #ifdef PLATFORM_WITH_DRIVES
       
   218      QString *str = gs_AvailableDrives();
       
   219      if (str) {
       
   220          return *str;         
       
   221      } else {
       
   222          return QString(); 
       
   223      }
       
   224 #else // PLATFORM_WITH_DRIVES           
       
   225      return QString(); 
       
   226 #endif  // PLATFORM_WITH_DRIVES 
       
   227 }