6093b72ac3f1b409779d38739d7a30f97231bda6
[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                 String path = cntInfo->GetContentPath();
168                 String dirName = GetDirecotyNameFromFullPath(path);
169
170                 FileListPresentationModel* pFileListPM = FileListPresentationModel::GetInstance();
171                 pFileListPM->AddDirectoryIfNew(dirName);
172                 AlbumListPresentationModel::GetInstance()->OnContentCreated();
173                 pFileListPM->OnContentCreated();
174         }
175
176         AppLogDebug("EXIT");
177 }
178
179 void
180 ContentUpdateEventListener::OnContentFileUpdated(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
181 {
182         AppLogDebug("ENTER");
183         
184         if ( contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_VIDEO)
185         {
186                 AlbumListPresentationModel::GetInstance()->OnContentUpdated();
187                 FileListPresentationModel::GetInstance()->OnContentUpdated();
188         }
189         AppLogDebug("EXIT");
190 }
191
192 void
193 ContentUpdateEventListener::OnContentFileDeleted(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
194 {
195         AppLogDebug("ENTER");
196
197         if ( contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_VIDEO)
198         {
199                 Tizen::App::UiApp* appPtr = Tizen::App::UiApp::GetInstance();
200                 if (appPtr->GetAppUiState() == Tizen::App::APP_UI_STATE_FOREGROUND)
201                 {
202                         AlbumListPresentationModel::GetInstance()->OnContentDeleted();
203                         FileListPresentationModel::GetInstance()->OnContentDeleted();
204                 }
205                 else
206                 {
207                         __changeNotificationReceived = true;
208                 }
209
210         }
211         AppLogDebug("EXIT");
212 }
213
214 void
215 ContentUpdateEventListener::OnContentDirectoryScanCompleted(const Tizen::Base::String& directoryPath, result r)
216 {
217         AppLogDebug("ENTER");
218
219         Tizen::App::UiApp* appPtr = Tizen::App::UiApp::GetInstance();
220         if (appPtr->GetAppUiState() == Tizen::App::APP_UI_STATE_FOREGROUND)
221         {
222                 AlbumListPresentationModel::GetInstance()->OnContentDeleted();
223                 FileListPresentationModel::GetInstance()->OnContentDeleted();
224                 AlbumListPresentationModel::GetInstance()->OnContentScanCompleted( directoryPath );
225         }
226         else
227         {
228                 __changeNotificationReceived = true;
229         }\r
230
231         AppLogDebug("EXIT");
232 }