Initialize Tizen 2.3
[apps/osp/Gallery.git] / src / GlFileUpdateTimer.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                GlFileUpdateTimer.cpp
19  * @brief               This is the source file for FileUpdateTimer class.
20  */
21
22 #include <FIo.h>
23
24 #include "GlFileUpdateTimer.h"
25 #include "GlFileListPresentationModel.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Utility;
30 using namespace Tizen::Content;
31 using namespace Tizen::Io;
32 using namespace Tizen::System;
33 using namespace Tizen::Ui;
34 using namespace Tizen::Ui::Controls;
35 using namespace Tizen::Ui::Scenes;
36
37 FileUpdateTimer::FileUpdateTimer(Tizen::Base::Collection::IList* pDelList
38         , IContentFileUpdateManager* pFDM
39         , IFileOpInvalidateListener* invalidate
40         , FileActionMode actionId
41         , RotateMode rotateMode)
42         : GlTimerBase(invalidate, actionId)
43         , __pUpdateIndexList(pDelList)
44         , __pFileUpdateManager(pFDM)
45         , __rotateMode(rotateMode)
46         , __scanDirMap(SingleObjectDeleter)
47         , __pContentManager(null)
48 {
49         __scanDirMap.Construct();
50         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
51 }
52
53 FileUpdateTimer::~FileUpdateTimer(void)
54 {
55         AppLogDebug("ENTER");
56         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
57         ScanDirectories();
58         delete __pUpdateIndexList;
59         delete __pContentManager;
60         AppLogDebug("EXIT");
61 }
62
63 IList *
64 FileUpdateTimer::TimerStart(void)
65 {
66         AppLogDebug("ENTER");
67         __pContentManager = new (std::nothrow) ContentManager();
68         __pContentManager->Construct();
69         IList* pCntIdList = __pFileUpdateManager->GetContentIdListAtIndexN(*__pUpdateIndexList);
70         AppLogDebug("EXIT");
71         return pCntIdList;
72 }
73
74 result
75 FileUpdateTimer::TimerExpired(const Object* contentId)
76 {
77         AppLogDebug("ENTER");
78         if (_actionId == FILE_ACTION_DELETE)
79         {
80                 const ContentId* cntId = static_cast<const ContentId*>(contentId);
81                 if(cntId == null)
82                 {
83                         return E_FAILURE;
84                 }
85                 ContentInfo* cntInfo = __pContentManager->GetContentInfoN(*cntId);
86                 if (cntInfo)
87                 {
88                         String filePath = cntInfo->GetContentPath();
89                         delete cntInfo;
90                         result r = __pFileUpdateManager->DeleteContentFile(filePath);
91                         if (!IsFailed(r))
92                         {
93                                 String dirPath;
94                                 int index =0;
95                                 result r = filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, index);
96                                 TryReturn(r == E_SUCCESS, false, "[%s] Unable to get Dirpath", GetErrorMessage(r));
97                                 filePath.SubString(0, index, dirPath);
98                                 if(!__scanDirMap.ContainsKey(dirPath))
99                                 {
100                                         __scanDirMap.Add(new String(dirPath), new Integer(0));
101                                 }
102                         }
103                         return r;
104                 }
105                 else
106                 {
107                         return E_FAILURE;
108                 }
109         }
110         else
111         {
112                 result r = E_SUCCESS;
113                 const ContentId* cntId = static_cast<const ContentId*>(contentId);
114                 if (contentId != null)
115                 {
116                     r = __pFileUpdateManager->RotateImage(*cntId, __rotateMode);
117                     if ( r == E_SUCCESS )
118                     {
119                         __pFileUpdateManager->RequestThumbnail(*cntId); // do not request thumbnail for video files
120                     }
121                     else
122                     {
123                         return r;
124                     }
125                 }
126                 return r;
127         }
128         AppLogDebug("EXIT");
129 }
130
131 result
132 FileUpdateTimer::ScanDirectories(void)
133 {
134         AppLogDebug("ENTER");
135         RequestId reqId;
136         result r = E_SUCCESS;
137         if (_actionId == FILE_ACTION_DELETE)
138         {
139                 int scanCount = __scanDirMap.GetCount();
140                 if( scanCount == 0)
141                 {
142                         return r;
143                 }
144                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
145                 String* pKey = null;
146                 while (pMapEnum->MoveNext() == E_SUCCESS)
147                 {
148                         pKey = static_cast< String* > (pMapEnum->GetKey());
149                         r = ContentManager::ScanDirectory(*(pKey), true, null, reqId);
150                 }
151                 __scanDirMap.RemoveAll();
152                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
153                 return r;
154         }
155         else
156         {
157                 AppLogDebug("EXIT, nothing to be done");
158                 return E_SUCCESS;
159         }
160 }
161
162 int
163 FileUpdateTimer::GetDirScanCount()
164 {
165         IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
166         String* pKey = null;
167         while (pMapEnum->MoveNext() == E_SUCCESS)
168         {
169                 pKey = static_cast< String* > (pMapEnum->GetKey());
170                 if (File::IsFileExist(*pKey) == false)
171                 {
172                         __scanDirMap.Remove(*pKey);
173                 }
174         }
175         delete pMapEnum;
176         return __scanDirMap.GetCount();
177 }
178
179 void
180 FileUpdateTimer::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
181 {
182         AppLogDebug("ENTER");
183         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED &&
184                         _actionId == FILE_ACTION_DELETE)
185         {
186                 String sdcPath = Environment::GetExternalStoragePath();
187                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
188                 String* pKey = null;
189                 while (pMapEnum->MoveNext() == E_SUCCESS)
190                 {
191                         pKey = static_cast< String* > (pMapEnum->GetKey());
192                         if (pKey->StartsWith(sdcPath, 0))
193                         {
194                                 __scanDirMap.Remove(*pKey);
195                         }
196                 }
197                 delete pMapEnum;
198         }
199         AppLogDebug("EXIT");
200 }
201
202 void
203 FileUpdateTimer::TimerCancel(int, enum FileActionCancelRes res)
204 {
205         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
206 }
207 void
208 FileUpdateTimer::TimerComplete(int, enum FileActionCompleteRes res)
209 {
210         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
211 }