Fixed prevent issue
[apps/osp/Gallery.git] / src / GlContentUpdateEventListener.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                GlContentUpdateEventListener.cpp
19  * @brief               This is the implementation file for ContentUpdateEventListener class.
20  */
21
22 #include <FApp.h>
23 #include <FUi.h>
24 #include "GlContentUpdateEventListener.h"
25 #include "GlFileListPresentationModel.h"
26 #include "GlAlbumListPresentationModel.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Utility;
31 using namespace Tizen::Content;
32
33 ContentUpdateEventListener* ContentUpdateEventListener::__pContentUpdateEventListener = null;
34
35 ContentUpdateEventListener::ContentUpdateEventListener(void)
36         : __pContentManager(null)
37         , __isTimerActive(false)
38         , __changeNotificationReceived(false)
39 {
40         AppLogDebug("ENTER");
41         AppLogDebug("EXIT");
42 }
43
44 ContentUpdateEventListener::~ContentUpdateEventListener(void)
45 {
46         AppLogDebug("ENTER");
47         __pContentManager->RemoveContentUpdateEventListener(*this);
48         delete __pContentManager;
49         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
50 }
51
52 ContentUpdateEventListener*
53 ContentUpdateEventListener::GetInstance(void)
54 {
55         AppLogDebug("ENTER");
56         if (__pContentUpdateEventListener == null)
57         {
58                 CreateInstance();
59         }
60         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
61
62         return __pContentUpdateEventListener;
63 }
64
65 void ContentUpdateEventListener::AddContentListener(void)
66 {
67         __pContentManager->AddContentUpdateEventListener(*this);
68 }
69
70 void ContentUpdateEventListener::RemoveContentListener(void)
71 {
72         __pContentManager->RemoveContentUpdateEventListener(*this);
73 }
74
75 result
76 ContentUpdateEventListener::Construct(void)
77 {
78         AppLogDebug("ENTER");
79
80         __pContentManager = new (std::nothrow) ContentManager();
81         __pContentManager->Construct();
82         __pContentManager->AddContentUpdateEventListener(*this);
83
84         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
85         return E_SUCCESS;
86 }
87
88 void
89 ContentUpdateEventListener::CreateInstance(void)
90 {
91         AppLogDebug("ENTER");
92         __pContentUpdateEventListener = new (std::nothrow) ContentUpdateEventListener();
93         result r = __pContentUpdateEventListener->Construct();
94         if (IsFailed(r) == true)
95         {
96                 delete __pContentUpdateEventListener;
97                 __pContentUpdateEventListener = null;
98                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
99
100                 return;
101         }
102
103         std::atexit(DestroyInstance);
104         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
105 }
106
107 void
108 ContentUpdateEventListener::DestroyInstance(void)
109 {
110         AppLogDebug("ENTER");
111
112         delete __pContentUpdateEventListener;
113         __pContentUpdateEventListener = null;
114
115         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
116 }
117
118 bool
119 ContentUpdateEventListener::GetChangeNotificationStatus()
120 {
121         AppLogDebug("ENTER");
122         AppLogDebug("EXIT");
123         return __changeNotificationReceived;
124 }
125
126 void
127 ContentUpdateEventListener::ResumeOperation()
128 {
129         AppLogDebug("ENTER");
130         AlbumListPresentationModel::GetInstance()->OnContentDeleted();
131         FileListPresentationModel::GetInstance()->OnContentDeleted();
132         __changeNotificationReceived = false;
133         AppLogDebug("EXIT");
134 }
135
136 String
137 ContentUpdateEventListener::GetDirecotyNameFromFullPath(const String& fullPath)const
138 {
139         AppLogDebug("ENTER");
140         if (fullPath == null)
141         {
142                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
143
144                 return null;
145         }
146         String delim(DIRECTORY_SEPARATOR);
147         StringTokenizer st(fullPath,delim);
148         String token;
149         String tokenPrev;
150         while (st.HasMoreTokens())
151         {
152                 tokenPrev = token;
153                 st.GetNextToken(token);
154         }
155
156         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
157
158         return tokenPrev;
159 }
160
161 void
162 ContentUpdateEventListener::OnContentFileCreated(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
163 {
164         AppLogDebug("ENTER");
165         if ( !__isTimerActive && ( contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_VIDEO) )
166         {
167                 Tizen::Content::ContentInfo* cntInfo = __pContentManager->GetContentInfoN(contentId);
168
169                 if ( cntInfo != NULL)
170                 {
171                         String path = cntInfo->GetContentPath();
172                         String dirName = GetDirecotyNameFromFullPath(path);
173
174                         FileListPresentationModel* pFileListPM = FileListPresentationModel::GetInstance();
175                         pFileListPM->AddDirectoryIfNew(dirName);
176                         AlbumListPresentationModel::GetInstance()->OnContentCreated();
177                         pFileListPM->OnContentCreated();
178                 }
179         }
180
181         AppLogDebug("EXIT");
182 }
183
184 void
185 ContentUpdateEventListener::OnContentFileUpdated(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
186 {
187         AppLogDebug("ENTER");
188         
189         if ( !__isTimerActive && ( contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_VIDEO) )
190         {
191                 AlbumListPresentationModel::GetInstance()->OnContentUpdated();
192                 FileListPresentationModel::GetInstance()->OnContentUpdated();
193         }
194         else
195         {
196                 FileListPresentationModel::GetInstance()->OnContentUpdated(contentId);
197         }
198         AppLogDebug("EXIT");
199 }
200
201 void
202 ContentUpdateEventListener::OnContentFileDeleted(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
203 {
204         AppLogDebug("ENTER");
205
206         if ( !__isTimerActive && ( contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_VIDEO) )
207         {
208                 Tizen::App::UiApp* appPtr = Tizen::App::UiApp::GetInstance();
209                 if (appPtr->GetAppUiState() == Tizen::App::APP_UI_STATE_FOREGROUND)
210                 {
211                         AlbumListPresentationModel::GetInstance()->OnContentDeleted();
212                         FileListPresentationModel::GetInstance()->OnContentDeleted();
213                 }
214                 else
215                 {
216                         __changeNotificationReceived = true;
217                 }
218
219         }
220         AppLogDebug("EXIT");
221 }
222
223 void
224 ContentUpdateEventListener::OnContentDirectoryScanCompleted(const Tizen::Base::String& directoryPath, result r)
225 {
226         AppLogDebug("ENTER");
227
228         if ( __isTimerActive )
229         {
230                 AlbumListPresentationModel::GetInstance()->OnContentScanCompleted( directoryPath );
231                 return;
232         }
233         Tizen::App::UiApp* appPtr = Tizen::App::UiApp::GetInstance();
234         if (appPtr->GetAppUiState() == Tizen::App::APP_UI_STATE_FOREGROUND)
235         {
236                 AlbumListPresentationModel::GetInstance()->OnContentDeleted();
237                 FileListPresentationModel::GetInstance()->OnContentDeleted();
238                 AlbumListPresentationModel::GetInstance()->OnContentScanCompleted(directoryPath);
239         }
240         else
241         {
242                 bool renameHandled = AlbumListPresentationModel::GetInstance()->OnContentScanCompleted(directoryPath);
243                 if (!renameHandled)
244                 {
245                         __changeNotificationReceived = true;
246                 }
247         }\r
248
249         AppLogDebug("EXIT");
250 }
251 void
252 ContentUpdateEventListener::SetTimerActive(void)
253 {
254         __isTimerActive = true;
255 }
256
257 void
258 ContentUpdateEventListener::ReSetTimerActive(void)
259 {
260         __isTimerActive = false;
261 }