limiting the file name size to 32 charcaters
[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.1 (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://floralicense.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 #include <FMedia.h>
21 #include <FText.h>
22
23 using namespace Tizen::App;
24 using namespace Tizen::Ui;
25 using namespace Tizen::Ui::Controls;
26 using namespace Tizen::Ui::Scenes;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Media;
31 using namespace Tizen::Text;
32
33 FileManagerForm::FileManagerForm(void)
34     : __pList(0)
35     , __pEditField(0)
36     , __canChooseNew(false)
37     , __canChooseOne(true)
38 {
39 }
40
41 FileManagerForm::~FileManagerForm(void)
42 {
43 }
44
45 bool
46 FileManagerForm::Initialize(const Tizen::Base::Collection::ArrayListT<Tizen::Base::String>& fileExt, bool isEditField)
47 {
48     Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
49
50     __fileExts.Construct(fileExt);
51     __canChooseNew = isEditField;
52
53     AppResource* pAppResource = Application::GetInstance()->GetAppResource();
54     __pFolderIcon = pAppResource->GetBitmapN(L"U01_icon_folder.png");
55
56     return true;
57 }
58
59 result
60 FileManagerForm::OnInitializing(void)
61 {
62     if (!CreateMainList())
63     {
64         return E_FAILURE;
65     }
66
67     Footer* pFooter = GetFooter();
68
69     pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
70     Form::SetFormBackEventListener(this);
71
72     pFooter->AddActionEventListener(*this);
73
74     return E_SUCCESS;
75 }
76
77 result
78 FileManagerForm::OnTerminating(void)
79 {
80     result r = E_SUCCESS;
81     return r;
82 }
83
84 bool
85 FileManagerForm::CreateMainList(void)
86 {
87     __pList = new ListView();
88
89     if (__canChooseNew)
90     {
91         __pEditField = new EditField();
92         __pEditField->Construct(Rectangle(30, 20, GetClientAreaBounds().width - 60, 100));
93         __pEditField->SetKeypadEnabled(false);
94         this->AddControl(__pEditField);
95
96         __pList->Construct(Rectangle(0, 150, GetClientAreaBounds().width, GetClientAreaBounds().height-150), true, false);
97     }
98     else
99     {
100         __pList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height), true, false);
101     }
102     __pList->SetItemProvider(*this);
103     __pList->AddListViewItemEventListener(*this);
104     AddControl(__pList);
105
106     return true;
107 }
108
109 void
110 FileManagerForm::OnActionPerformed (const Tizen::Ui::Control &source, int actionId)
111 {
112         if (actionId == ID_BUTTON_EXIT)
113         {
114                 SceneManager* pSceneManager = SceneManager::GetInstance();
115                 AppAssert(pSceneManager);
116                 ArrayList* pList = new (std::nothrow) ArrayList;
117                 AppAssert(pList);
118                 pList->Construct();
119                 String* pFolder = new String("");
120
121                 if (__canChooseNew)
122                 {
123                         String fileName = __pEditField->GetText();
124
125                         if (fileName.IsEmpty() || fileName == ".xdb")
126                         {
127                                 fileName = "default.xdb";
128                         }
129                         if (!fileName.EndsWith(".xdb"))
130                         {
131                                 fileName.Append(".xdb");
132                         }
133
134                         if(fileName.GetLength() > 32)
135                         {
136                                 MessageBox msgBox;
137                                 int modalResult;
138
139                                 msgBox.Construct(L"Warning", L"File name too long.\nPlease rename (max 32 characters supported).", MSGBOX_STYLE_OK);
140                                 msgBox.ShowAndWait(modalResult);
141                                 return;
142                         }
143
144                         *pFolder = __currentPath + "/" + fileName;
145
146                         Tizen::Text::Utf8Encoding utf8;
147                         int countOfBytes = 0;
148                         result res = utf8.GetByteCount(fileName,countOfBytes);
149
150                         if(countOfBytes > 240)
151                         {
152                                 MessageBox msgBox;
153                                 int modalResult;
154
155                                 msgBox.Construct(L"Warning", L"File name too long.\nPlease rename.", MSGBOX_STYLE_OK);
156                                 msgBox.ShowAndWait(modalResult);
157
158                                 if (modalResult != MSGBOX_RESULT_OK)
159                                 {
160                                         return;
161                                 }
162                         }
163                         else
164                         {
165                                 pList->Add(*pFolder);
166
167                                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), pList);
168                         }
169                 }
170                 else
171                 {
172                         *pFolder = __currentPath;
173
174                         pList->Add(*pFolder);
175
176                         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), pList);
177                 }
178         }
179 }
180
181 void
182 FileManagerForm::OnFormBackRequested(Tizen::Ui::Controls::Form&  source)
183 {
184         if ( (__currentPath + "/" ) == Tizen::System::Environment::GetMediaPath())
185         {
186                 SceneManager* pSceneManager = SceneManager::GetInstance();
187                 AppAssert(pSceneManager);
188                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), null /*pList*/);
189         }
190         else
191         {
192                 GoDownDir();
193             __pList->ScrollToItem(0);
194             GetDirs(__currentPath);
195
196             __pList->UpdateList();
197             __pList->Draw(true);
198             __pList->Show();
199         }
200 }
201
202 int
203 FileManagerForm::GetItemCount(void)
204 {
205     return __files.GetCount();
206 }
207
208 bool
209 FileManagerForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
210 {
211     delete pItem;
212     pItem = null;
213     return true;
214 }
215
216 Tizen::Ui::Controls::ListItemBase*
217 FileManagerForm::CreateItem(int index, int itemWidth)
218 {
219     ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
220     CustomItem* pItem = new CustomItem();
221     pItem->Construct(Tizen::Graphics::Dimension(itemWidth, 100), style);
222
223     Tizen::Base::String fileName;
224     __files.GetAt(index, fileName);
225
226     Tizen::Io::FileAttributes fileAtt;
227
228     if (Tizen::Io::File::GetAttributes(__currentPath + "/" + fileName, fileAtt) == E_SUCCESS)
229     {
230         if (fileAtt.IsDirectory())
231         {
232             pItem->AddElement(Rectangle(10, 10, 80, 80), ID_FORMAT_BITMAP, *__pFolderIcon, null, null);
233         }
234         else
235         {
236                 result r = E_SUCCESS;
237                 Tizen::Media::Image* pImage = new (std::nothrow) Image();
238                 TryReturn(pImage != null, null, "Failed to create Image");
239                 r = pImage->Construct();
240                 TryReturn(r == E_SUCCESS, null, "Failed to construct Image");
241
242                 Tizen::Graphics::Bitmap* pFileIcon;
243
244                 pFileIcon = pImage->DecodeN(__currentPath + "/" + fileName, BITMAP_PIXEL_FORMAT_RGB565, 60, 60);
245             pItem->AddElement(Rectangle(10, 10, 80, 80), ID_FORMAT_BITMAP, *pFileIcon, null, null);
246
247             delete pFileIcon;
248             delete pImage;
249         }
250     }
251
252     pItem->AddElement(Rectangle(110, 10, 610, 80), ID_FORMAT_STRING, fileName, true);
253
254     return pItem;
255 }
256
257 void
258 FileManagerForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView &view, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
259 {
260     Tizen::Base::String fileName;
261     __files.GetAt(index, fileName);
262
263     if (status == LIST_ITEM_STATUS_SELECTED)
264     {
265         // Load database on click
266         if (fileName == "..")
267         {
268             GoDownDir();
269             __pList->ScrollToItem(0);
270             GetDirs(__currentPath);
271         }
272         else if (CheckExt(fileName))
273         {
274             if (__canChooseNew)
275             {
276                 __pEditField->SetText(fileName);
277                 __pEditField->RequestRedraw(true);
278             }
279             else if (__canChooseOne)
280             {
281                 SceneManager* pSceneManager = SceneManager::GetInstance();
282                 AppAssert(pSceneManager);
283                 ArrayList* pList = new (std::nothrow) ArrayList;
284                 AppAssert(pList);
285                 pList->Construct();
286                 String* pFolder = new String(__currentPath + "/" + fileName);
287                 pList->Add(*pFolder);
288
289                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT), pList);
290                 return;
291             }
292         }
293         else
294         {
295             GoUpDir(fileName);
296             __pList->ScrollToItem(0);
297             GetDirs(__currentPath);
298         }
299
300         __pList->UpdateList();
301         __pList->Draw(true);
302         __pList->Show();
303     }
304 }
305
306 void
307 FileManagerForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView &listView, int index, Tizen::Ui::Controls::SweepDirection direction)
308 {
309 }
310
311 void
312 FileManagerForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView &listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
313 {
314 }
315
316 void
317 FileManagerForm::OnItemReordered(Tizen::Ui::Controls::ListView &listView, int oldIndex, int newIndex)
318 {
319 }
320
321 bool
322 FileManagerForm::CheckExt(Tizen::Base::String fileName)
323 {
324     int period = 0;
325     const Tizen::Base::String period_symbol = ".";
326     if (E_SUCCESS != fileName.LastIndexOf(period_symbol, fileName.GetLength() - 1, period))
327     {
328         return false;
329     }
330     Tizen::Base::String extension;
331     if (E_SUCCESS != fileName.SubString(period + 1, extension))
332     {
333         return false;
334     }
335
336     return __fileExts.Contains(extension);
337
338 }
339
340 void
341 FileManagerForm::GetDirs(Tizen::Base::String dirPath)
342 {
343     Tizen::Io::Directory dir;
344     Tizen::Io::DirEnumerator *pDirEnum(null);
345     Tizen::Io::File file;
346     Tizen::Io::FileAttributes fileAtt;
347
348     if (dir.Construct(dirPath) == E_SUCCESS)
349     {
350         pDirEnum = dir.ReadN();
351         __files.RemoveAll();
352
353         while(pDirEnum->MoveNext() == E_SUCCESS)
354         {
355             Tizen::Io::DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
356             if (CheckExt(dirEntry.GetName()) || dirEntry.IsDirectory())
357             {
358                 if(dirEntry.GetName().CompareTo("..") == 0 || (Tizen::Io::File::GetAttributes(dirPath + "/" + dirEntry.GetName(), fileAtt) == E_SUCCESS && !fileAtt.IsHidden()))
359                 {
360                         __files.Add(dirEntry.GetName());
361                 }
362             }
363         }
364
365         StrComparer* pStrComparer = new StrComparer();
366         __files.Sort(*pStrComparer);
367         delete pStrComparer;
368
369         __files.Remove(".");
370
371         String mediaPath = Tizen::System::Environment::GetMediaPath();
372         if (mediaPath.EndsWith("/"))
373         {
374                 mediaPath.Remove(mediaPath.GetLength() - 1, 1);
375         }
376         if (dirPath.CompareTo("/") == 0 || dirPath.CompareTo(mediaPath) == 0)
377         {
378                 __files.Remove("..");
379         }
380
381     }
382     else if (Tizen::Io::File::GetAttributes(dirPath, fileAtt) == E_SUCCESS)
383     {
384         GoDownDir();
385     }
386
387     delete pDirEnum;
388 }
389
390 void
391 FileManagerForm::GoDownDir(void)
392 {
393     int separator_pos = 0;
394     const Tizen::Base::String separator_symbol = "/";
395     if (E_SUCCESS != __currentPath.LastIndexOf(separator_symbol, __currentPath.GetLength() - 1, separator_pos))
396     {
397         return;
398     }
399     if (E_SUCCESS != __currentPath.SubString(0, separator_pos, __currentPath))
400     {
401         return;
402     }
403     if (__currentPath.IsEmpty())
404     {
405         __currentPath = "/";
406     }
407
408     if (__pEditField)
409     {
410         __pEditField->RequestRedraw(true);
411     }
412 }
413
414 void
415 FileManagerForm::GoUpDir(Tizen::Base::String dir)
416 {
417     if (!__currentPath.Equals(L"/", false))
418     {
419         __currentPath += "/";
420     }
421     __currentPath += dir;
422
423     if (__pEditField)
424     {
425         __pEditField->RequestRedraw(true);
426     }
427 }
428
429 bool
430 FileManagerForm::IsFile(Tizen::Base::String filePath)
431 {
432     Tizen::Io::File file;
433     Tizen::Io::FileAttributes fileAtt;
434     return Tizen::Io::File::GetAttributes(filePath, fileAtt) == E_SUCCESS;
435 }
436
437 void
438 FileManagerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
439         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
440 {
441
442     Footer* pFooter = GetFooter();
443     pFooter->RemoveAllButtons();
444     pFooter->SetBackButton();
445
446     if (currentSceneId == "ChooseImageScene")
447     {
448         FooterItem footerItemChooseFolder;
449         footerItemChooseFolder.Construct(ID_BUTTON_EXIT);
450         footerItemChooseFolder.SetText("Choose");
451         pFooter->AddItem(footerItemChooseFolder);
452     }
453     else if (currentSceneId == "ChooseNewFeatureSetScene")
454     {
455         __pEditField->SetKeypadEnabled(true);
456         FooterItem footerItemChooseFolder;
457         footerItemChooseFolder.Construct(ID_BUTTON_EXIT);
458         footerItemChooseFolder.SetText("Create");
459         pFooter->AddItem(footerItemChooseFolder);
460     }
461     else if (currentSceneId == "ChooseExistingFeatureSetScene")
462     {
463         pFooter-> SetShowState(true);
464     }
465         
466     __currentPath = Tizen::System::Environment::GetMediaPath();
467     if (__currentPath.EndsWith("/"))
468     {
469         __currentPath.Remove(__currentPath.GetLength() - 1, 1);
470     }
471     __pList->ScrollToItem(0);
472     GetDirs(__currentPath);
473     __pList->UpdateList();
474 }
475
476 void
477 FileManagerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
478         const Tizen::Ui::Scenes::SceneId& nextSceneId)
479 {
480 }