1b2fefc7b9ef31b1a25d99f32cf8f45dbad252c9
[apps/native/sample/ImageFeatureManager.git] / project / src / FileManagerForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "FileManagerForm.h"
19 #include <FSysEnvironment.h>
20
21 using namespace Tizen::App;
22 using namespace Tizen::Ui;
23 using namespace Tizen::Ui::Controls;
24 using namespace Tizen::Ui::Scenes;
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27 using namespace Tizen::Graphics;
28
29 FileManagerForm::FileManagerForm(void)
30     : __pList(0)
31     , __pEditField(0)
32     , __canChooseNew(false)
33     , __canChooseOne(true)
34 {
35 }
36
37 FileManagerForm::~FileManagerForm(void)
38 {
39 }
40
41 bool
42 FileManagerForm::Initialize(const Tizen::Base::Collection::ArrayListT<Tizen::Base::String>& fileExt, bool isEditField)
43 {
44     Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
45
46     __fileExts.Construct(fileExt);
47     __canChooseNew = isEditField;
48
49     AppResource* pAppResource = Application::GetInstance()->GetAppResource();
50     __pFileIcon   = pAppResource->GetBitmapN(L"home_type3.png");
51     __pFolderIcon = pAppResource->GetBitmapN(L"message_type3.png");
52
53     return true;
54 }
55
56 result
57 FileManagerForm::OnInitializing(void)
58 {
59     if (!CreateMainList())
60     {
61         return E_FAILURE;
62     }
63
64     Footer* pFooter = GetFooter();
65
66     pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
67     Form::SetFormBackEventListener(this);
68
69     pFooter->AddActionEventListener(*this);
70
71     return E_SUCCESS;
72 }
73
74 result
75 FileManagerForm::OnTerminating(void)
76 {
77     result r = E_SUCCESS;
78     return r;
79 }
80
81 bool
82 FileManagerForm::CreateMainList(void)
83 {
84     __pList = new ListView();
85
86     if (__canChooseNew)
87     {
88         __pEditField = new EditField();
89         __pEditField->Construct(Rectangle(30, 0, GetClientAreaBounds().width - 60, 100));
90         __pEditField->SetKeypadEnabled(false);
91         this->AddControl(*__pEditField);
92
93         __pList->Construct(Rectangle(0, 130, GetClientAreaBounds().width, GetClientAreaBounds().height - 130), true, false);
94     }
95     else
96     {
97         __pList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height), true, false);
98     }
99     __pList->SetItemProvider(*this);
100     __pList->AddListViewItemEventListener(*this);
101     AddControl(*__pList);
102
103     return true;
104 }
105
106 void
107 FileManagerForm::OnActionPerformed (const Tizen::Ui::Control &source, int actionId)
108 {
109     if (actionId == ID_BUTTON_EXIT)
110     {
111         SceneManager* pSceneManager = SceneManager::GetInstance();
112         AppAssert(pSceneManager);
113         ArrayList* pList = new (std::nothrow) ArrayList;
114         AppAssert(pList);
115         pList->Construct();
116         String* pFolder = new String("");
117         if (__canChooseNew)
118         {
119             String fileName = __pEditField->GetText();
120             if (fileName.IsEmpty())
121             {
122                 fileName = "default.xdb";
123             }
124             if (!fileName.EndsWith(".xdb"))
125             {
126                 fileName.Append(".xdb");
127             }
128             *pFolder = __currentPath + "/" + fileName;
129         }
130         else
131         {
132             *pFolder = __currentPath;
133         }
134         pList->Add(*pFolder);
135
136         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), pList);
137     }
138 }
139
140 void
141 FileManagerForm::OnFormBackRequested(Tizen::Ui::Controls::Form&  source)
142 {
143     SceneManager* pSceneManager = SceneManager::GetInstance();
144     AppAssert(pSceneManager);
145     pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), null /*pList*/);
146 }
147
148
149 int
150 FileManagerForm::GetItemCount(void)
151 {
152     return __files.GetCount();
153 }
154
155 bool
156 FileManagerForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
157 {
158     delete pItem;
159     pItem = null;
160     return true;
161 }
162
163 Tizen::Ui::Controls::ListItemBase*
164 FileManagerForm::CreateItem(int index, int itemWidth)
165 {
166     ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
167     CustomItem* pItem = new CustomItem();
168     pItem->Construct(Tizen::Graphics::Dimension(itemWidth, 100), style);
169
170     Tizen::Base::String fileName;
171     __files.GetAt(index, fileName);
172
173     Tizen::Io::FileAttributes fileAtt;
174
175     if (Tizen::Io::File::GetAttributes(__currentPath + "/" + fileName, fileAtt) == E_SUCCESS)
176     {
177         if (fileAtt.IsDirectory())
178         {
179             pItem->AddElement(Rectangle(10, 20, 60, 60), ID_FORMAT_BITMAP, *__pFolderIcon, null, null);
180         }
181         else
182         {
183             pItem->AddElement(Rectangle(10, 20, 60, 60), ID_FORMAT_BITMAP, *__pFileIcon, null, null);
184         }
185     }
186
187     pItem->AddElement(Rectangle(80, 25, 720, 50), ID_FORMAT_STRING, fileName, true);
188
189     return pItem;
190 }
191
192 void
193 FileManagerForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView &view, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
194 {
195     Tizen::Base::String fileName;
196     __files.GetAt(index, fileName);
197
198     if (status == LIST_ITEM_STATUS_SELECTED)
199     {
200         // Load database on click
201         if (fileName == "..")
202         {
203             GoDownDir();
204             __pList->ScrollToItem(0);
205             GetDirs(__currentPath);
206         }
207         else if (CheckExt(fileName))
208         {
209             if (__canChooseNew)
210             {
211                 __pEditField->SetText(fileName);
212                 __pEditField->RequestRedraw(true);
213             }
214             else if (__canChooseOne)
215             {
216                 SceneManager* pSceneManager = SceneManager::GetInstance();
217                 AppAssert(pSceneManager);
218                 ArrayList* pList = new (std::nothrow) ArrayList;
219                 AppAssert(pList);
220                 pList->Construct();
221                 String* pFolder = new String(__currentPath + "/" + fileName);
222                 pList->Add(*pFolder);
223
224                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), pList);
225             }
226         }
227         else
228         {
229             GoUpDir(fileName);
230             __pList->ScrollToItem(0);
231             GetDirs(__currentPath);
232         }
233
234         __pList->UpdateList();
235         __pList->Draw(true);
236         __pList->Show();
237     }
238 }
239
240 void
241 FileManagerForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView &listView, int index, Tizen::Ui::Controls::SweepDirection direction)
242 {
243 }
244
245 void
246 FileManagerForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView &listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
247 {
248 }
249
250 void
251 FileManagerForm::OnItemReordered(Tizen::Ui::Controls::ListView &listView, int oldIndex, int newIndex)
252 {
253 }
254
255 bool
256 FileManagerForm::CheckExt(Tizen::Base::String fileName)
257 {
258     int period = 0;
259     const Tizen::Base::String period_symbol = ".";
260     if (E_SUCCESS != fileName.LastIndexOf(period_symbol, fileName.GetLength() - 1, period))
261     {
262         return false;
263     }
264     Tizen::Base::String extension;
265     if (E_SUCCESS != fileName.SubString(period + 1, extension))
266     {
267         return false;
268     }
269
270     return __fileExts.Contains(extension);
271
272 }
273
274 void
275 FileManagerForm::GetDirs(Tizen::Base::String dirPath)
276 {
277     Tizen::Io::Directory dir;
278     Tizen::Io::DirEnumerator *pDirEnum(null);
279     Tizen::Io::File file;
280     Tizen::Io::FileAttributes fileAtt;
281
282     if (dir.Construct(dirPath) == E_SUCCESS)
283     {
284         pDirEnum = dir.ReadN();
285         __files.RemoveAll();
286
287         while(pDirEnum->MoveNext() == E_SUCCESS)
288         {
289             Tizen::Io::DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
290             if (CheckExt(dirEntry.GetName()) || dirEntry.IsDirectory())
291             {
292                 __files.Add(dirEntry.GetName());
293             }
294         }
295
296         StrComparer* pStrComparer = new StrComparer();
297         __files.Sort(*pStrComparer);
298         delete pStrComparer;
299
300         __files.Remove(".");
301
302         if (dirPath.CompareTo("/") == 0)
303         {
304                 __files.Remove("..");
305         }
306
307     }
308     else if (Tizen::Io::File::GetAttributes(dirPath, fileAtt) == E_SUCCESS)
309     {
310         GoDownDir();
311     }
312
313     delete pDirEnum;
314 }
315
316 void
317 FileManagerForm::GoDownDir(void)
318 {
319     int separator_pos = 0;
320     const Tizen::Base::String separator_symbol = "/";
321     if (E_SUCCESS != __currentPath.LastIndexOf(separator_symbol, __currentPath.GetLength() - 1, separator_pos))
322     {
323         return;
324     }
325     if (E_SUCCESS != __currentPath.SubString(0, separator_pos, __currentPath))
326     {
327         return;
328     }
329     if (__currentPath.IsEmpty())
330     {
331         __currentPath = "/";
332     }
333
334     if (__pEditField)
335     {
336         __pEditField->RequestRedraw(true);
337     }
338 }
339
340 void
341 FileManagerForm::GoUpDir(Tizen::Base::String dir)
342 {
343     if (!__currentPath.Equals(L"/", false))
344     {
345         __currentPath += "/";
346     }
347     __currentPath += dir;
348
349     if (__pEditField)
350     {
351         __pEditField->RequestRedraw(true);
352     }
353 }
354
355 bool
356 FileManagerForm::IsFile(Tizen::Base::String filePath)
357 {
358     Tizen::Io::File file;
359     Tizen::Io::FileAttributes fileAtt;
360     return Tizen::Io::File::GetAttributes(filePath, fileAtt) == E_SUCCESS;
361 }
362
363 void
364 FileManagerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
365         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
366 {
367
368     Footer* pFooter = GetFooter();
369     pFooter->RemoveAllButtons();
370     pFooter->SetBackButton();
371
372     if (currentSceneId == "ChooseImageScene")
373     {
374         FooterItem footerItemChooseFolder;
375         footerItemChooseFolder.Construct(ID_BUTTON_EXIT);
376         footerItemChooseFolder.SetText("Choose");
377         pFooter->AddItem(footerItemChooseFolder);
378     }
379     else if (currentSceneId == "ChooseNewFeatureSetScene")
380     {
381         __pEditField->SetKeypadEnabled(true);
382         FooterItem footerItemChooseFolder;
383         footerItemChooseFolder.Construct(ID_BUTTON_EXIT);
384         footerItemChooseFolder.SetText("Create");
385         pFooter->AddItem(footerItemChooseFolder);
386     }
387
388     __currentPath = Tizen::System::Environment::GetMediaPath();
389     if (__currentPath.EndsWith("/"))
390     {
391         __currentPath.Remove(__currentPath.GetLength() - 1, 1);
392     }
393     __pList->ScrollToItem(0);
394     GetDirs(__currentPath);
395     __pList->UpdateList();
396 }
397
398 void
399 FileManagerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
400         const Tizen::Ui::Scenes::SceneId& nextSceneId)
401 {
402 }