summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntmodel/src/cntimagerescaleutility.cpp
blob: cbd2c45906cee900215d7a811eb8ae913a35980e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

#include "cntimagerescaler.h"
#include "cntimagerescaleutility.h"
#include "cntdbconsts_internal.h"

#include "cntitem.h"
#include "cntfldst.h"

#include <pathinfo.h>
#include <driveinfo.h>
#include <bautils.h>
#include <utf.h>

_LIT(KImagesBackupFolderName, "backup_registration_images.xml");
_LIT8(KXmlFilePart1,
            "<?xml version=\"1.0\" standalone=\"yes\"?>\r\n"
            "<backup_registration>\r\n"
            "    <public_backup delete_before_restore=\"yes\">\r\n"
            "        <include_directory name=\"");
_LIT8(KXmlFilePart2,
            "\"/>\r\n"
            "    </public_backup>\r\n"
            "</backup_registration>\r\n");
    
EXPORT_C TPath TCntImageRescaleUtility::ImageDirectoryL()
{
    TInt drive = KErrNotFound;
    RFs fs;
    CleanupClosePushL( fs );
    User::LeaveIfError( fs.Connect() );
        
    #ifdef __WINS__
        User::LeaveIfError(DriveInfo::GetDefaultDrive(DriveInfo::EDefaultPhoneMemory, drive));
    #else
        TInt errorCode = DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, drive);        
        if ( KErrNone == errorCode )
            {
            errorCode = TCntImageRescaleUtility::IsDriveReady( fs, drive );               
            }        
        User::LeaveIfError( errorCode );
    #endif
        
    // Get the root path in this drive to create the images directory
    TPath imagesDirPath;
    imagesDirPath.Zero();
    
    User::LeaveIfError(PathInfo::GetRootPath(imagesDirPath, drive));
    imagesDirPath.Append(KImagesFolder);
    
    if ( !BaflUtils::PathExists( fs, imagesDirPath ) )
        {
        BaflUtils::EnsurePathExistsL( fs, imagesDirPath );
        TCntImageRescaleUtility::CreateBackupAndRestoreFileL( fs, imagesDirPath );
        User::LeaveIfError(fs.SetAtt(imagesDirPath, KEntryAttHidden, KEntryAttNormal));
        }    
    
    CleanupStack::PopAndDestroy(); // RFs
    return imagesDirPath;
}

EXPORT_C TPath TCntImageRescaleUtility::CreateImageDirectoryL()
{
    TPath imagePath;
    TRAP_IGNORE( imagePath = TCntImageRescaleUtility::ImageDirectoryL() );    
    return imagePath;
}

EXPORT_C void TCntImageRescaleUtility::DeleteImageDirectoryL()
{
    TPath dir;
    TRAPD( err, dir = TCntImageRescaleUtility::ImageDirectoryL() );
    if ( err == KErrNone )
        {
        RFs fs;
        CleanupClosePushL( fs );
        User::LeaveIfError( fs.Connect() );
       
        CFileMan* fileMan = CFileMan::NewL( fs );
        fileMan->RmDir(dir); // err not used
        delete fileMan;
       
        CleanupStack::PopAndDestroy(); // close RFs
        }
}

EXPORT_C void TCntImageRescaleUtility::DeleteImageL( const CContactItem& aItem )
{
    CContactItemFieldSet& fieldSet = aItem.CardFields();
    RFs fs;
    CleanupClosePushL( fs );
    User::LeaveIfError( fs.Connect() );
                    
    // Find the image field
    TInt index = fieldSet.Find(KUidContactFieldCodImage, KUidContactFieldVCardMapUnknown);
    if (index != KErrNotFound)
        {
        // Image path field from list of contact fields
        CContactItemField& field = fieldSet[index];
        TPtrC imagePath = field.TextStorage()->Text();
                    
        // Remove image file if it is stored in private folder
        BaflUtils::DeleteFile(fs, imagePath); // Error value not necessary
        }
    
    CleanupStack::PopAndDestroy(); // RFs
}

EXPORT_C void TCntImageRescaleUtility::StoreImageFieldL( const CContactItem& aItem, const TPath aPath )
{
    CContactItemFieldSet& fieldSet = aItem.CardFields();
    if ( aPath.Length() > 0 )
    {
        CContactItemField* field = NULL;
                    
        TInt privateImageIndex = fieldSet.Find( KUidContactFieldCodImage, KUidContactFieldVCardMapUnknown );
        if ( privateImageIndex != KErrNotFound )
        {
            field = &fieldSet[privateImageIndex];
            field->TextStorage()->SetTextL( aPath );
        }
    }
}

EXPORT_C void TCntImageRescaleUtility::UpdateImageNameL( const CContactItem& aItem )
{
    CContactItemFieldSet& fieldSet = aItem.CardFields();
            
    // Find the image field
    TInt index = fieldSet.Find( KUidContactFieldCodImage, KUidContactFieldVCardMapUnknown );
    if (index != KErrNotFound)
    {
        // Image path field from list of contact fields
        CContactItemField& field = fieldSet[index];
        TPtrC privateImagePath = field.TextStorage()->Text();
        // unfortunately the Guid method is not const (?) and this ugly cast is needed
        TPtrC guid = const_cast<CContactItem&>(aItem).Guid();
                
        // Append the guid in the filename if it doesn't already exists
        if (privateImagePath.Length() && privateImagePath.Find(guid) == KErrNotFound)
        {
            // Image file type
            TParse p;
            p.Set(privateImagePath, NULL, NULL);
            
            // Generate the image path
            // Format <path>guid_timestamp_filename.ext
            TPath newImagePath;
            newImagePath.Append(TCntImageRescaleUtility::ImageDirectoryL());
            newImagePath.Append(guid);
            newImagePath.Append(p.NameAndExt());
            
            RFs fs;
            CleanupClosePushL( fs );
            User::LeaveIfError( fs.Connect() );
            User::LeaveIfError(BaflUtils::RenameFile(fs, privateImagePath, newImagePath)); // Rename the file
            CleanupStack::PopAndDestroy(); // RFs
            
            field.TextStorage()->SetTextL(newImagePath);
        }
    }
}
    
void TCntImageRescaleUtility::CreateBackupAndRestoreFileL( RFs& aFs, const TPath& aDir )
{
    TPath privatePath;
    User::LeaveIfError( aFs.PrivatePath(privatePath));
    User::LeaveIfError( aFs.SetSessionPath(privatePath));

    // If the path does not exist create it.
    TInt err = aFs.MkDirAll(privatePath);
    if (err != KErrAlreadyExists )
    {
        User::LeaveIfError(err);
    }

    // Remove previous backup registration file. The drive path may have
    // changed so this file needs to be regenerated all the time
    BaflUtils::DeleteFile( aFs, KImagesBackupFolderName()); // Error value not necessary

    const TInt newFileNameLength = KImagesBackupFolderName().Length();
    HBufC* newFileName = HBufC::NewLC(newFileNameLength);
    TPtr newFileNamePtr(newFileName->Des());
    newFileNamePtr.Append(KImagesBackupFolderName);

    // Create registration file.
    RFile file;
    CleanupClosePushL(file);

    // File should not exist since it was deleted
    User::LeaveIfError( file.Create(aFs, *newFileName, EFileWrite) );
            
    HBufC* nameAndExt = NULL;
    nameAndExt = HBufC::NewLC(aDir.Length());
    nameAndExt->Des().Append(aDir);

    // Convert foldername and extension to UTF8 before writing to file.
    HBufC8* folderName = HBufC8::NewLC(aDir.Length());
    TPtr8 pFolderName(folderName->Des());
        
    User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(pFolderName,*nameAndExt));

    // Write data into file.
    User::LeaveIfError(file.Write(KXmlFilePart1()));
    User::LeaveIfError(file.Write(pFolderName));
    User::LeaveIfError(file.Write(KXmlFilePart2()));
    User::LeaveIfError(file.Flush());

    User::LeaveIfError( aFs.SetSessionPath(aDir) );
    CleanupStack::PopAndDestroy(4, newFileName); // folderName, nameAndExt, file, newFileName
}

// ------------------------------------------------------------------------------------------------
// TInt IsDriveReady( RFs& aRfs, TInt aDrive )
// ------------------------------------------------------------------------------------------------
TInt TCntImageRescaleUtility::IsDriveReady( RFs& aRfs, TInt aDrive )
{  
    TUint driveStatus( 0 );    
    TInt ret( KErrNone );    
    TInt driveErr = DriveInfo::GetDriveStatus( aRfs, aDrive, driveStatus );
    
    if( KErrNone != driveErr ||
    !( driveStatus & DriveInfo::EDrivePresent ) ||
    ( driveStatus & DriveInfo::EDriveInUse ) )
    {
        ret = KErrBadHandle;           
    }

return ret;
}


// End of File