phonebookengines/cntimageutility/src/cntimageutility.cpp
changeset 31 2a11b5b00470
parent 27 de1630741fbe
equal deleted inserted replaced
27:de1630741fbe 31:2a11b5b00470
    23 #include <QDir>
    23 #include <QDir>
    24 #include <QImage>
    24 #include <QImage>
    25 #include <QPixmap>
    25 #include <QPixmap>
    26 #include <QDateTime>
    26 #include <QDateTime>
    27 
    27 
    28 const char* PBK_FOLDER   = "Private\\20022EF9";
    28 const char* CNT_IMAGES_FOLDER   = "Data\\20022EF9\\";
    29 const char* IMAGEFILE_EXT   = ".jpg";
    29 const char* IMAGEFILE_EXT   = ".jpg";
    30 const char* IMAGE_TYPE   = "JPEG";
    30 const char* IMAGE_TYPE   = "JPEG";
    31 const char* FOLDER_SEPARATOR   = "\\";
       
    32 
    31 
    33 const TInt64 KMinRequiredSpaceImage = 2000000;
    32 const TInt64 KMinRequiredSpaceImage = 2000000;
       
    33 
       
    34 _LIT( KImagesFolder, "Data\\20022EF9\\");
       
    35 
       
    36 // Local function
       
    37 inline TPtrC16 convertToTPtrC16(const QString& string)
       
    38 {
       
    39     return TPtrC16(reinterpret_cast<const TUint16*>(string.utf16()));
       
    40 }
       
    41 
    34 
    42 
    35 CntImageUtility::CntImageUtility(QObject *parent)
    43 CntImageUtility::CntImageUtility(QObject *parent)
    36     : QObject(parent)
    44     : QObject(parent)
    37 {
    45 {
       
    46     m_Fs.Connect();
    38 }
    47 }
    39 
    48 
    40 CntImageUtility::~CntImageUtility()
    49 CntImageUtility::~CntImageUtility()
    41 {
    50 {
    42 }
    51     m_Fs.Close();
    43 
    52 }
    44 bool CntImageUtility::createImage(const QString& filePath, QString& imagePath, int& error)
    53 
    45 {
    54 /*
    46     Q_UNUSED(error);
    55  * Create thumbnail image.
    47     
    56  *
    48     int drive;
    57  * \param filePath The file path of source image.
    49     bool success(false);
    58  * \param imagePath The file path of new/created image.
       
    59  * \return Success status.
       
    60  */
       
    61 bool CntImageUtility::createImage(const QString& filePath, QString& imagePath)
       
    62 {
       
    63     int drive;
       
    64     bool success(false);
       
    65     m_error = CntImageUtility::NoError;
       
    66     int err= selectDrive(drive);
       
    67     if (KErrNone==err
       
    68         && initPath(imagePath, drive)) {
       
    69         // Filename format
       
    70         // image_<yyhh_mm_ss_zzz>.jpg
       
    71         QDateTime dateTime = QDateTime::currentDateTime();
       
    72         QString dateTimeString = dateTime.toString("yyhh_mm_ss_zzz");
       
    73         QFileInfo fi(filePath);
       
    74         QString filename = "image_" + dateTimeString + "." + fi.suffix();
       
    75         imagePath += filename;
       
    76         
       
    77         // Copy image
       
    78         QFile destFile;
       
    79         success= destFile.copy(filePath,imagePath);
       
    80     }
       
    81     else {
       
    82         m_error = CntImageUtility::UnspecifiedError;
       
    83     }
       
    84     return success;
       
    85 }
       
    86 
       
    87 /*
       
    88  * Create thumbnail image.
       
    89  *
       
    90  * \param pixmap The pixmap of source image.
       
    91  * \param imagePath The file path of new/created image.
       
    92  * \return Success status.
       
    93  */
       
    94 bool CntImageUtility::createImage(const QPixmap& pixmap, QString& imagePath)
       
    95 {
       
    96     int drive;
       
    97     bool success(false);
       
    98     m_error = CntImageUtility::NoError;
    50     QString fileExt(IMAGEFILE_EXT);
    99     QString fileExt(IMAGEFILE_EXT);
    51     int err= selectDrive(drive);
   100     int err= selectDrive(drive);
    52     if (KErrNone==err)
   101     if (KErrNone==err
    53         {
   102         && initPath(imagePath, drive)) {
    54         initPath(imagePath, drive);
       
    55         // Filename format
   103         // Filename format
    56         // image_<yyhh_mm_ss_zzz>.jpg
   104         // image_<yyhh_mm_ss_zzz>.jpg
    57         QDateTime dateTime = QDateTime::currentDateTime();
   105         QDateTime dateTime = QDateTime::currentDateTime();
    58         QString dateTimeString = dateTime.toString("yyhh_mm_ss_zzz");
   106         QString dateTimeString = dateTime.toString("yyhh_mm_ss_zzz");
    59         QString filename = "image_" + dateTimeString + fileExt;
   107         QString filename = "image_" + dateTimeString + fileExt;
    60         imagePath += FOLDER_SEPARATOR + filename;
   108         imagePath += filename;
    61         
   109         
    62         // Copy source image to contacts images
   110         if(!pixmap.isNull()) {
    63         QFile destFile;
       
    64         success= destFile.copy(filePath,imagePath);
       
    65         }
       
    66     return success;
       
    67 }
       
    68 
       
    69 bool CntImageUtility::createImage(const QPixmap& pixmap, QString& imagePath, int& error)
       
    70 {
       
    71     Q_UNUSED(error);
       
    72 	
       
    73     int drive;
       
    74     bool success(false);
       
    75     QString fileExt(IMAGEFILE_EXT);
       
    76     int err= selectDrive(drive);
       
    77     if (KErrNone==err)
       
    78         {
       
    79         initPath(imagePath, drive);
       
    80         // Filename format
       
    81         // image_<yyhh_mm_ss_zzz>.jpg
       
    82         QDateTime dateTime = QDateTime::currentDateTime();
       
    83         QString dateTimeString = dateTime.toString("yyhh_mm_ss_zzz");
       
    84         QString filename = "image_" + dateTimeString + fileExt;
       
    85         imagePath += FOLDER_SEPARATOR + filename;
       
    86         
       
    87         if(!pixmap.isNull())
       
    88             {
       
    89             QPixmap pix(pixmap);
   111             QPixmap pix(pixmap);
    90             QImage image(pix.toImage());
   112             QImage image(pix.toImage());
    91             success= image.save(imagePath, IMAGE_TYPE);
   113             success= image.save(imagePath, IMAGE_TYPE);
    92             }
       
    93         }
   114         }
    94     return success;
   115     }
    95 }
   116     else {
    96 
   117         m_error = CntImageUtility::UnspecifiedError;
    97 bool CntImageUtility::removeImage(const QString& imagePath, int& error)
   118     }
    98 {
   119     return success;
    99     Q_UNUSED(error);
   120 }
   100     
   121 
       
   122 /*
       
   123  * Remove thumbnail image.
       
   124  *
       
   125  * \param imagePath The file path of thumbnail image.
       
   126  * \return Success status.
       
   127  */
       
   128 bool CntImageUtility::removeImage(const QString& imagePath)
       
   129 {
   101     QDir dir;
   130     QDir dir;
   102     bool success(false);
   131     bool success(false);
       
   132     m_error = CntImageUtility::NoError;
   103     
   133     
   104     QFile file( imagePath );
   134     QFile file( imagePath );
   105     if( !file.exists()
   135     if( !file.exists()
   106       || dir.remove(imagePath))
   136       || dir.remove(imagePath)) {
   107         success= true;
   137         success= true;
   108     
   138     }
   109     return success;
   139     return success;
   110 }
   140 }
   111 
   141 
   112 int CntImageUtility::selectDrive(int &driveIndex)
   142 int CntImageUtility::selectDrive(int &driveIndex)
   113 {
   143 {
   114     int err = KErrNone;
   144     int err = KErrNone;
   115     TInt64 minSpaceInBytes = KMinRequiredSpaceImage;
   145     TInt64 minSpaceInBytes = KMinRequiredSpaceImage;
   116     RFs fsSession;
       
   117     
       
   118     // Connect to file session
       
   119     err= fsSession.Connect();
       
   120     if(err!=KErrNone)
       
   121         return err;
       
   122     
   146     
   123     // Get the drive/volume details
   147     // Get the drive/volume details
   124     TVolumeInfo vInfo;
   148     TVolumeInfo vInfo;
   125     err = DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, driveIndex);
   149     err = DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, driveIndex);
   126     if (KErrNone==err)
   150     if (KErrNone==err) {
   127         {
   151         err = m_Fs.Volume(vInfo, driveIndex);
   128         err = fsSession.Volume(vInfo, driveIndex);
   152     }
   129         }
       
   130 
   153 
   131     // Check the availability and disk space
   154     // Check the availability and disk space
   132     if ( !err && vInfo.iFree < minSpaceInBytes)
   155     if ( !err && vInfo.iFree < minSpaceInBytes) {
   133         {
       
   134         // All drives are full or inaccessible
   156         // All drives are full or inaccessible
   135         err = KErrDiskFull;
   157         err = KErrDiskFull;
       
   158     }
       
   159     return err;
       
   160 }
       
   161 
       
   162 bool CntImageUtility::initPath(QString &path, int drive)
       
   163 {
       
   164     int err = KErrNone;
       
   165     bool success(false);
       
   166         
       
   167     // Get contacts images path
       
   168     TFileName tPath;
       
   169     PathInfo::GetRootPath(tPath, drive);
       
   170     tPath.Append(KImagesFolder());
       
   171     path = QString::fromUtf16(tPath.Ptr(), tPath.Length());
       
   172     
       
   173     /* Contact images should go to the
       
   174      * hidden folder \data\20022EF9
       
   175      */
       
   176     QDir dir(path);
       
   177     if (!dir.cd(CNT_IMAGES_FOLDER)) {
       
   178         TPtrC16 pathDesc(convertToTPtrC16(path));
       
   179         err=m_Fs.MkDirAll(tPath);
       
   180         err=m_Fs.SetAtt(tPath, KEntryAttHidden, 0);
       
   181      }
       
   182     
       
   183     success = (err == KErrNone);
       
   184     return success;
       
   185 }
       
   186 
       
   187 /*
       
   188  * \return whether mass storage is supported in phone.
       
   189  */
       
   190 bool CntImageUtility::isMassStorageAvailable()
       
   191 {
       
   192     int err = KErrNone;
       
   193     bool success(false);
       
   194     m_error = CntImageUtility::NoError;
       
   195         
       
   196     int drive;
       
   197     err = DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, drive);
       
   198     success = (err == KErrNone);
       
   199     return success;
       
   200 }
       
   201 
       
   202 /*
       
   203  * \return whether thumbnail image is removable.
       
   204  */
       
   205 bool CntImageUtility::isImageRemovable(const QString& filePath)
       
   206 {
       
   207     int drive;
       
   208     bool success(false);
       
   209     m_error = CntImageUtility::NoError;
       
   210     
       
   211     // Image removable if saved in contacts images folder
       
   212     // Folder <Drive>:/Data/20022EF9
       
   213     int err= selectDrive(drive);
       
   214     if(KErrNone==err) {
       
   215         // Get the root path
       
   216         TFileName tPath;
       
   217         PathInfo::GetRootPath(tPath, drive);
       
   218         QString basePath = QString::fromUtf16(tPath.Ptr(), tPath.Length());
       
   219         
       
   220         QDir dir(basePath);
       
   221         if (dir.cd(CNT_IMAGES_FOLDER)) {
       
   222             basePath=QDir::toNativeSeparators(dir.path());
       
   223             if(filePath.startsWith(basePath))
       
   224                 success=true;
   136         }
   225         }
   137     return err;
   226     }
   138 }
   227     return success;
   139 
   228 }
   140 void CntImageUtility::initPath(QString &path, int drive)
   229 
   141 {
   230 /*
   142      // Get the root path for the given drive.
   231  * \return Error status.
   143      TFileName tPath;
   232  */
   144      PathInfo::GetRootPath(tPath, drive);
   233 CntImageUtility::Error CntImageUtility::error()
   145      path = QString::fromUtf16(tPath.Ptr(), tPath.Length());
   234 {
   146      
   235     return m_error;
   147      // Image files saved in Phonebook folder
   236 }
   148      // Create folder if not exists
       
   149      QDir dir(path);
       
   150      if (!dir.cd(PBK_FOLDER)) 
       
   151          {
       
   152          dir.mkpath(PBK_FOLDER);
       
   153          }
       
   154      path = dir.path();
       
   155 }