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