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