Tizen 2.1 base
[apps/osp/MyFiles.git] / src / MfFileManageWorkerThread.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (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: MfFileManageWorkerThread.cpp
19  * @brief: This file contains the implementation of FileManageWorkerThread class, which manages file operations like
20  * Copy, Move, Delete, Create and other utilities.
21  */
22
23 #include "MfFileManageWorkerThread.h"
24
25 using namespace Tizen::App;
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28 using namespace Tizen::Base::Runtime;
29 using namespace Tizen::Base::Utility;
30 using namespace Tizen::Content;
31 using namespace Tizen::Io;
32
33 int FileManageWorkerThread::__fileManagedCounter = 0;
34 unsigned long FileManageWorkerThread::E_THREAD_CANCEL = 1000;
35
36 FileManageWorkerThread::FileManageWorkerThread(void)
37         : __pFolderEntryPM(null)
38         , __pListOfFiles(null)
39 {
40         __operationId = OPERATIONID_MANAGE_MIN;
41         __threadRunningState = THREAD_RUNNING_STATE_ALIVE;
42         __fileExistingOperationId = FILE_EXISTING_OPERATIONID_NONE;
43         __fileManagingingResult = FILE_MANAGING_RESULT_NONE;
44 }
45
46 FileManageWorkerThread::~FileManageWorkerThread(void)
47 {
48         if (__pFolderEntryPM != null)
49         {
50                 delete __pFolderEntryPM;
51         }
52 }
53
54 FileManageWorkerThread&
55 FileManageWorkerThread::operator =(const FileManageWorkerThread& fileManageWorkerThread)
56 {
57         return *this;
58 }
59
60 FileManageWorkerThread::FileManageWorkerThread(const FileManageWorkerThread& fileManageWorkerThread)
61 {
62         //Do Nothing
63 }
64
65 result
66 FileManageWorkerThread::Construct(void)
67 {
68         result r = E_SUCCESS;
69
70         r = Thread::Construct(THREAD_TYPE_WORKER);
71         //r = EventDrivenThread::Construct(THREAD_TYPE_WORKER);
72
73         __pFolderEntryPM = new (std::nothrow) FolderEntryPresentationModel();
74         r = __pFolderEntryPM->Construct();
75         AppAssert(__pFolderEntryPM != null)
76
77         AppLogDebug("Exit %s", GetErrorMessage(r));
78         return r;
79 }
80
81 result
82 FileManageWorkerThread::CreateFolder(const String& destPath, const String& folderName)
83 {
84         AppLogDebug("Entry");
85         result r = E_SUCCESS;
86         String filePath;
87
88         filePath.Append(destPath);
89         filePath.Append(folderName);
90
91         r = Directory::Create(filePath, false);
92         AppLogDebug("Exit %s", GetErrorMessage(r));
93         return r;
94 }
95
96 result
97 FileManageWorkerThread::DeleteFile(const String& filePath)
98 {
99         AppLogDebug("Entry");
100         result r = E_SUCCESS;
101         FileAttributes fileAttrib;
102         File::GetAttributes(filePath, fileAttrib);
103
104         if (fileAttrib.IsDirectory())
105         {
106                 r = Directory::Remove(filePath, true);
107         }
108         else
109         {
110                 r = File::Remove(filePath);
111         }
112
113         TryCatch(r == E_SUCCESS, , "file deletion failed");
114
115         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_SUCCESS;
116         AppLogDebug("Exit %s", GetErrorMessage(r));
117         return r;
118
119 CATCH:
120         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_FAILED;
121         AppLogDebug("Exit %s", GetErrorMessage(r));
122         return r;
123 }
124
125 result
126 FileManageWorkerThread::CopyFile(String sourcePath, String destPath)
127 {
128         AppLogDebug("Entry");
129         result r = E_SUCCESS;
130         ArrayList* pBackUpContentList = null;
131         DirectoryEntry* pDirStr = null;
132         DirectoryEntry* pDirStrTemp = null;
133         ArrayList* pTempContentList = null;
134         String tempDestPath;
135
136         FileAttributes fileAttrib;
137         File::GetAttributes(sourcePath, fileAttrib);
138
139         if (fileAttrib.IsDirectory())
140         {
141                 sourcePath.Append(L"/");
142                 __pFolderEntryPM->SetFolderEntryPath(sourcePath);
143                 pTempContentList = __pFolderEntryPM->GetFolderEntryList();
144                 TryCatch(pTempContentList != null, , "Failed to allocate memory to original content list");
145
146                 pBackUpContentList = new (std::nothrow) ArrayList();
147                 pBackUpContentList->Construct();
148
149                 for (int count = 0; count < pTempContentList->GetCount(); count++)
150                 {
151                         pDirStrTemp = static_cast< DirectoryEntry* >(pTempContentList->GetAt(count));
152                         TryCatch(pDirStrTemp != null, , "Failed to allocate memory to pDirStrTemp");
153
154                         pDirStr = new (std::nothrow) DirectoryEntry(*pDirStrTemp);
155                         TryCatch(pDirStr != null, , "Failed to allocate memory to pdirstr");
156                         pBackUpContentList->Add(pDirStr);
157                 }
158
159                 r = Directory::Create(destPath, false);
160                 TryCatch(r == E_SUCCESS, , "dir already exist");
161
162                 destPath.Append(L"/");
163                 for (int count = 0; count < pBackUpContentList->GetCount(); count++)
164                 {
165                         tempDestPath.Clear();
166                         tempDestPath.Append(destPath);
167
168                         DirectoryEntry* pDirStr = null;
169                         pDirStr = static_cast< DirectoryEntry* >(pBackUpContentList->GetAt(count));
170                         tempDestPath.Append(pDirStr->GetFileName());
171                         if (!pDirStr->IsDirectory())
172                         {
173                                 tempDestPath.Append(L".");
174                                 tempDestPath.Append(pDirStr->GetFileExtension());
175                         }
176
177                         File::GetAttributes(pDirStr->GetFullFilePath(), fileAttrib);
178                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
179                         {
180                                 r = CopyFile(pDirStr->GetFullFilePath(), tempDestPath);
181                         }
182                         else
183                         {
184                                 pBackUpContentList->RemoveAll(true);
185                                 delete pBackUpContentList;
186                                 pBackUpContentList = null;
187                                 return E_SUCCESS;
188                         }
189
190                 }
191
192
193                 pBackUpContentList->RemoveAll(true);
194                 delete pBackUpContentList;
195                 pBackUpContentList = null;
196
197         }
198         else
199         {
200                 if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
201                 {
202                         //r = File::Copy(sourcePath, destPath, true);
203                         r = Copy(sourcePath, destPath);
204                         TryCatch(r == E_SUCCESS, , " file Copy  Failed");
205
206                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
207                         {
208                                 r = UpdateContentDB(destPath);
209                         }
210                         else
211                         {
212                                 return E_SUCCESS;
213                         }
214                 }
215                 else
216                 {
217                         return E_SUCCESS;
218                 }
219         }
220
221         AppLogDebug("Exit %s", GetErrorMessage(r));
222         return E_SUCCESS;
223 CATCH:
224         //r = E_FAILURE;
225         if (pBackUpContentList != null)
226         {
227                 pBackUpContentList->RemoveAll(true);
228                 delete pBackUpContentList;
229                 pBackUpContentList = null;
230         }
231
232         AppLogDebug("Exit %s", GetErrorMessage(r));
233         return r;
234 }
235
236 result
237 FileManageWorkerThread::MoveFile(String sourcePath, String destPath)
238 {
239         AppLogDebug("Entry");
240         result r = E_SUCCESS;
241
242         FileAttributes fileAttrib;
243         File::GetAttributes(sourcePath, fileAttrib);
244
245         ArrayList* pBackUpContentList = null;
246         ArrayList* pTempContentList = null;
247         DirectoryEntry* pDirStr = null;
248         DirectoryEntry* pDirStrTemp = null;
249         String tempDestPath;
250
251         if (fileAttrib.IsDirectory())
252         {
253                 sourcePath.Append(L"/");
254                 __pFolderEntryPM->SetFolderEntryPath(sourcePath);
255                 pTempContentList = __pFolderEntryPM->GetFolderEntryList();
256                 TryCatch(pTempContentList != null, , "Failed to allocate memory to original content list");
257
258                 pBackUpContentList = new (std::nothrow) ArrayList();
259                 pBackUpContentList->Construct();
260
261                 for (int count = 0; count < pTempContentList->GetCount(); count++)
262                 {
263                         pDirStrTemp = static_cast< DirectoryEntry* >(pTempContentList->GetAt(count));
264                         pDirStr = new (std::nothrow) DirectoryEntry(*pDirStrTemp);
265                         TryCatch(pDirStr != null, , "Failed to allocate memory to pdirstr");
266                         pBackUpContentList->Add(pDirStr);
267                 }
268
269                 r = Directory::Create(destPath, false);
270                 TryCatch(r == E_SUCCESS, , "dir Creation Failed");
271
272                 destPath.Append(L"/");
273                 for (int count = 0; count < pBackUpContentList->GetCount(); count++)
274                 {
275                         tempDestPath.Clear();
276                         tempDestPath.Append(destPath);
277                         DirectoryEntry* pDirStr = null;
278                         pDirStr = static_cast< DirectoryEntry* >(pBackUpContentList->GetAt(count));
279                         tempDestPath.Append(pDirStr->GetFileName());
280                         if (!pDirStr->IsDirectory())
281                         {
282                                 tempDestPath.Append(L".");
283                                 tempDestPath.Append(pDirStr->GetFileExtension());
284                         }
285
286                         File::GetAttributes(pDirStr->GetFullFilePath(), fileAttrib);
287                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
288                         {
289                                 r = MoveFile(pDirStr->GetFullFilePath(), tempDestPath);
290                         }
291                         else
292                         {
293                                 pBackUpContentList->RemoveAll(true);
294                                 delete pBackUpContentList;
295                                 pBackUpContentList = null;
296                                 return E_SUCCESS;
297                         }
298
299                 }
300
301                 r = DeleteFile(sourcePath);
302                 TryCatch(r == E_SUCCESS, , "Directory Deletion Failed");
303
304                 pBackUpContentList->RemoveAll(true);
305                 delete pBackUpContentList;
306                 pBackUpContentList = null;
307
308         }
309         else
310         {
311                 if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
312                 {
313                         //r = File::Copy(sourcePath, destPath, true);
314                         r = Copy(sourcePath, destPath);
315                         TryCatch(r == E_SUCCESS, , " file already exist");
316                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
317                         {
318                                 r = UpdateContentDB(destPath);
319                                 r = DeleteFileContentDB(sourcePath);
320                                 TryCatch(r == E_SUCCESS || r == E_THREAD_CANCEL,,"MOve file Failed");
321                         }
322                         else
323                         {
324                                 return E_SUCCESS;
325                         }
326                 }
327                 else
328                 {
329                         return E_SUCCESS;
330                 }
331         }
332
333
334         AppLogDebug("Exit %s", GetErrorMessage(r));
335         return E_SUCCESS;
336
337 CATCH:
338         if (pBackUpContentList != null)
339         {
340                 pBackUpContentList->RemoveAll(true);
341                 delete pBackUpContentList;
342                 pBackUpContentList = null;
343         }
344
345         AppLogDebug("Exit %s", GetErrorMessage(r));
346         return r;
347 }
348
349 bool
350 FileManageWorkerThread::CheckFileDuplicity(void)
351 {
352         //return File::IsFileExist(filePath);
353         bool res = false;
354         DirectoryEntry* pDir = null;
355         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
356         while (pMapEnum->MoveNext() == E_SUCCESS)
357         {
358                 pDir = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
359
360                 String filePath;
361                 filePath.Append(__currentRootPath);
362                 if (pDir->IsDirectory())
363                 {
364                         filePath.Append(pDir->GetFileName());
365                 }
366                 else
367                 {
368                         filePath.Append(pDir->GetFileName());
369                         filePath.Append(L".");
370                         filePath.Append(pDir->GetFileExtension());
371                 }
372                 res = File::IsFileExist(filePath);
373                 if (res == true)
374                 {
375                         delete pMapEnum;
376                         return res;
377                 }
378         }
379         delete pMapEnum;
380         return res;
381 }
382
383 result
384 FileManageWorkerThread::DeleteFolderEntry(void)
385 {
386         AppLogDebug("Entry");
387         result r = E_SUCCESS;
388         DirectoryEntry* pDir = null;
389         ArrayList* pArg = null;
390         Integer* pFileManagedCounter = null;
391         Integer* pTotalFileCount = null;
392         Integer* pFileManagingResult = null;
393         Integer* pFolderEntryDeleteIndex = null;
394         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
395
396         ArrayList* pDeletedFolderEntry = null;
397         pDeletedFolderEntry = new (std::nothrow) ArrayList();
398         pDeletedFolderEntry->Construct();
399         int count = 0;
400
401         if (pMapEnum != null)
402         {
403                 while (pMapEnum->MoveNext() == E_SUCCESS)
404                 {
405                         pDir = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
406                         TryCatch(pDir != null, , "directory retrieval falied");
407
408                         r = DeleteFileContentDB(pDir->GetFullFilePath());
409                         TryCatch(r == E_SUCCESS || r == E_THREAD_CANCEL, , " delete file failed");
410
411                         if(r == E_THREAD_CANCEL)
412                         {
413                                 break;
414                         }
415
416                         pFolderEntryDeleteIndex = static_cast< Integer* >(pMapEnum->GetKey());
417                         pDeletedFolderEntry->Add((new Integer(pFolderEntryDeleteIndex->ToInt())));
418
419
420                 count++;
421
422                 __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
423
424                 pArg = new (std::nothrow) ArrayList();
425                 pArg->Construct();
426
427                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
428                 //pFolderEntryDeleteIndex = new (std::nothrow) Integer(0);
429                 pFileManagedCounter = new (std::nothrow) Integer(count);
430                 pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
431
432                 pArg->Add(pFileManagingResult);
433                 //pArg->Add(pFolderEntryDeleteIndex);
434                 pArg->Add(pFileManagedCounter);
435                 pArg->Add(pTotalFileCount);
436
437                 Application::GetInstance()->SendUserEvent(ID_DELETE_COMPLETE, pArg);
438                 //Application::GetInstance()->SendUserEvent(ID_DELETE_COMPLETE, pArg);
439                 }
440
441                 delete pMapEnum;
442         }
443
444         SortArrayList(pDeletedFolderEntry);
445
446         count = 0;
447         for (int i = pDeletedFolderEntry->GetCount() - 1; i >= 0; i--)
448         {
449                 Integer* PIndex = (Integer*) pDeletedFolderEntry->GetAt(i);
450
451                 __fileManagingingResult = FILE_MANAGING_REFRESH_LIST;
452
453                 pArg = new (std::nothrow) ArrayList();
454                 pArg->Construct();
455
456                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
457                 pFolderEntryDeleteIndex = new (std::nothrow) Integer(PIndex->ToInt());
458                 //pFileManagedCounter = new (std::nothrow) Integer(count++);
459                 //pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
460
461                 pArg->Add(pFileManagingResult);
462                 pArg->Add(pFolderEntryDeleteIndex);
463                 //pArg->Add(pFileManagedCounter);
464                 //pArg->Add(pTotalFileCount);
465
466                 Application::GetInstance()->SendUserEvent(ID_DELETE_COMPLETE, pArg);
467         }
468
469         if (pDeletedFolderEntry != null)
470         {
471                 pDeletedFolderEntry->RemoveAll(true);
472                 delete pDeletedFolderEntry;
473                 pDeletedFolderEntry = null;
474         }
475
476         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_SUCCESS;
477         AppLogDebug("Exit %s", GetErrorMessage(r));
478         return r;
479
480 CATCH:
481
482         SortArrayList(pDeletedFolderEntry);
483
484         for (int i = pDeletedFolderEntry->GetCount() - 1; i >= 0; i--)
485         {
486                 Integer* PIndex = (Integer*) pDeletedFolderEntry->GetAt(i);
487
488                 __fileManagingingResult = FILE_MANAGING_REFRESH_LIST;
489
490                 pArg = new (std::nothrow) ArrayList();
491                 pArg->Construct();
492
493                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
494                 pFolderEntryDeleteIndex = new (std::nothrow) Integer(PIndex->ToInt());
495                 //pFileManagedCounter = new (std::nothrow) Integer(count++);
496                 //pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
497
498                 pArg->Add(pFileManagingResult);
499                 pArg->Add(pFolderEntryDeleteIndex);
500                 //pArg->Add(pFileManagedCounter);
501                 //pArg->Add(pTotalFileCount);
502
503                 Application::GetInstance()->SendUserEvent(ID_DELETE_COMPLETE, pArg);
504         }
505         if (pDeletedFolderEntry != null)
506         {
507                 pDeletedFolderEntry->RemoveAll(true);
508                 delete pDeletedFolderEntry;
509                 pDeletedFolderEntry = null;
510         }
511
512         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_FAILED;
513         AppLogDebug("Exit %s", GetErrorMessage(r));
514         return r;
515 }
516
517 bool
518 FileManageWorkerThread::OnStart(void)
519 {
520         return true;
521 }
522
523 void
524 FileManageWorkerThread::OnStop(void)
525 {
526         //empty implementation
527 }
528
529 Object*
530 FileManageWorkerThread::Run(void)
531 {
532         AppLogDebug("Entry");
533         ArrayList* pArg = null;
534         result r = E_SUCCESS;
535         Integer* pFileProcessingResult = null;
536         __threadRunningState = THREAD_RUNNING_STATE_ALIVE;
537
538         switch (__operationId)
539         {
540         case OPERATIONID_MANAGE_DELETING:
541         {
542                 if (__pListOfFiles == null)
543                 {
544                         //DeleteFile(__filePath);
545                         r = DeleteFileContentDB(__filePath);
546                         if(r == E_THREAD_CANCEL)
547                         {
548                                 __fileManagingingResult = FILE_MANAGING_RESULT_THREAD_STOP;
549                         }
550                 }
551                 else
552                 {
553                         DeleteFolderEntry();
554                 }
555
556                 pArg = new (std::nothrow) ArrayList();
557                 pArg->Construct();
558                 pFileProcessingResult = new (std::nothrow) Integer(__fileManagingingResult);
559                 pArg->Add(pFileProcessingResult);
560                 Application::GetInstance()->SendUserEvent(ID_FILE_RENAME_COMPLETE, pArg);
561         }
562         break;
563
564         case OPERATIONID_MANAGE_COPYING:
565         {
566                 if (__pListOfFiles != null)
567                 {
568                         CopyFolderEntry(__fileExistingOperationId);
569                 }
570                 else
571                 {
572                         __fileManagingingResult = FILE_MANAGING_RESULT_COPYING_FAILED;
573                 }
574
575                 pArg = new (std::nothrow) ArrayList;
576                 pArg->Construct();
577                 pFileProcessingResult = new (std::nothrow) Integer(__fileManagingingResult);
578                 pArg->Add(pFileProcessingResult);
579                 pArg->Add(&__duplicateFileName);
580                 Application::GetInstance()->SendUserEvent(ID_COPY_COMPLETE, pArg);
581
582         }
583         break;
584
585         case OPERATIONID_MANAGE_MOVING:
586         {
587                 if (__pListOfFiles != null)
588                 {
589                         MoveFolderEntry(__fileExistingOperationId);
590                 }
591                 else
592                 {
593                         __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_FAILED;
594                 }
595
596                 pArg = new (std::nothrow) ArrayList();
597                 pArg->Construct();
598                 pFileProcessingResult = new (std::nothrow) Integer(__fileManagingingResult);
599                 pArg->Add(pFileProcessingResult);
600                 pArg->Add(&__duplicateFileName);
601                 Application::GetInstance()->SendUserEvent(ID_MOVING_COMPLETE, pArg);
602         }
603         break;
604
605         case OPERATIONID_MANAGE_CREATE_FOLDER:
606         {
607                 //empty implementation
608         }
609         break;
610
611         case OPERATIONID_MANAGE_RENAME:
612         {
613                 RenameFolderEntry();
614                 pArg = new (std::nothrow) ArrayList();
615                 pArg->Construct();
616                 pFileProcessingResult = new (std::nothrow) Integer(__fileManagingingResult);
617                 pArg->Add(pFileProcessingResult);
618                 //pArg->Add(&__duplicateFileName);
619                 Application::GetInstance()->SendUserEvent(ID_MOVING_COMPLETE, pArg);
620         }
621         break;
622         default:
623         {
624                 //empty implementation
625         }
626         break;
627         }
628
629         AppLogDebug("Exit");
630         return null;
631 }
632
633 void
634 FileManageWorkerThread::SetNameConflictOperationId(FileManaging& fileManagingStruct)
635 {
636         AppLogDebug("Entry");
637         __pListOfFiles = fileManagingStruct.GetListOfFiles();
638         __operationId = fileManagingStruct.GetOperationId();
639         __currentRootPath = fileManagingStruct.GetCurrentRootPath();
640         __filePath = fileManagingStruct.GetFilePath();
641         __renameFilePath = fileManagingStruct.GetNewFilePath();
642         __fileExistingOperationId = fileManagingStruct.GetFileExistingOperationId();
643         AppLogDebug("Exit");
644 }
645
646 void
647 FileManageWorkerThread::CopyFolderEntry(FileExistingOperationId fileExistingOperationId)
648 {
649         AppLogDebug("Entry");
650         result r = E_SUCCESS;
651         DirectoryEntry* pDirStr = null;
652         String destPath;
653         ArrayList* pArg = null;
654         Integer* pFileManagedCounter = null;
655         Integer* pTotalFileCount = null;
656         Integer* pFileManagingResult = null;
657         String* pRenameFilePath = null;
658         ArrayList* renameFileList = null;
659         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
660
661         __fileManagedCounter = 0;
662
663         if (__pListOfFiles->GetCount() > 0)
664         {
665                 switch (fileExistingOperationId)
666                 {
667                 case FILE_EXISTING_OPERATIONID_NONE:
668                 {
669                         if (!CheckForLegalDestination())
670                         {
671                                 __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
672                                 delete pMapEnum;
673                                 return;
674                         }
675                         if (CheckFileDuplicity() == true)
676                         {
677                                 __fileManagingingResult = FILE_MANAGING_RESULT_DUPLICITY;
678                                 delete pMapEnum;
679                                 return;
680                         }
681
682                 }
683                 break;
684
685                 case FILE_EXISTING_OPERATIONID_RENAME:
686                 {
687                         AppLogDebug("FILE_EXISTING_OPERATIONID_RENAME");
688                         renameFileList = GetListOfRenameFilesN();
689                         TryCatch(renameFileList != null, , "Failed to allocate memory to rename file list");
690                         int counter = 0;
691                         while (pMapEnum->MoveNext() == E_SUCCESS)
692                         {
693                                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
694                                 if (pDirStr != null)
695                                 {
696                                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
697                                         {
698                                                 __fileManagedCounter++;
699                                                 __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
700
701                                                 pArg = new (std::nothrow) ArrayList();
702                                                 pArg->Construct();
703                                                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
704                                                 pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
705                                                 pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
706                                                 pArg->Add(pFileManagingResult);
707                                                 pArg->Add(pFileManagedCounter);
708                                                 pArg->Add(pTotalFileCount);
709                                                 Application::GetInstance()->SendUserEvent(ID_COPY_COMPLETE, pArg);
710
711                                                 pRenameFilePath = static_cast< String* >(renameFileList->GetAt(counter));
712                                                 r = CopyFile(pDirStr->GetFullFilePath(), *pRenameFilePath);
713                                                 TryCatch(r == E_SUCCESS, , "Failed to Copy File");
714                                                 //r = UpdateContentDB(*pRenameFilePath);
715                                                 //TryCatch(r == E_SUCCESS, , "Content db update failed");
716                                         }
717                                         else
718                                         {
719                                                 __fileManagingingResult = FILE_MANAGING_RESULT_COPYING_SUCCESS;
720                                                 __fileManagedCounter = 0;
721                                                 delete pMapEnum;
722                                                 return;
723                                         }
724                                 }
725                                 counter++;
726                         }
727                         if (renameFileList != null)
728                         {
729                                 renameFileList->RemoveAll(true);
730                                 delete renameFileList;
731                                 renameFileList = null;
732                         }
733                 }
734                 break;
735
736                 case FILE_EXISTING_OPERATIONID_REPLACE:
737                 {
738                         while (pMapEnum->MoveNext() == E_SUCCESS)
739                         {
740                                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
741                                 if (pDirStr != null)
742                                 {
743                                         destPath.Clear();
744                                         destPath.Append(__currentRootPath);
745                                         destPath.Append(pDirStr->GetFileName());
746                                         if (!pDirStr->IsDirectory())
747                                         {
748                                                 destPath.Append(L".");
749                                                 destPath.Append(pDirStr->GetFileExtension());
750                                         }
751
752                                         __fileManagedCounter++;
753                                         __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
754
755                                         pArg = new (std::nothrow) ArrayList();
756                                         pArg->Construct();
757                                         pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
758                                         pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
759                                         pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
760                                         pArg->Add(pFileManagingResult);
761                                         pArg->Add(pFileManagedCounter);
762                                         pArg->Add(pTotalFileCount);
763                                         Application::GetInstance()->SendUserEvent(ID_COPY_COMPLETE, pArg);
764
765                                         AppLogDebug("full file path is %ls", pDirStr->GetFullFilePath().GetPointer());
766                                         AppLogDebug("dest path is %ls", destPath.GetPointer());
767
768                                         String tempDestStr;
769                                         tempDestStr.Append(destPath);
770                                         //tempDestStr.Append(L"/");
771                                         if(pDirStr->IsDirectory())
772                                         {
773                                                 tempDestStr.Append(L"/");
774                                         }
775                                         AppLogDebug("temp dest str is %ls",tempDestStr.GetPointer());
776
777                                         if(pDirStr->GetFullFilePath() == tempDestStr)
778                                         {
779                                                 //AppLogDebug("hello");
780
781                                                 //break;
782                                                 continue;
783                                         }
784                                         if (pDirStr->GetFullFilePath().StartsWith(tempDestStr, 0))
785                                         {
786                                                 AppLogDebug("case exist");
787                                                 String modifiedDestPath;
788                                                 modifiedDestPath.Append(destPath);
789                                                 DateTime t;
790                                                 Tizen::System::SystemTime::GetCurrentTime(t);
791                                                 modifiedDestPath.Append(t.GetDay());
792                                                 modifiedDestPath.Append(t.GetMonth());
793                                                 modifiedDestPath.Append(t.GetYear());
794                                                 modifiedDestPath.Append(t.GetHour());
795                                                 modifiedDestPath.Append(t.GetMinute());
796                                                 modifiedDestPath.Append(t.GetSecond());
797
798                                                 String modifiedSourcePath;
799                                                 modifiedSourcePath.Append(modifiedDestPath);
800                                                 modifiedSourcePath.Append(L"/");
801                                                 String tempStr;
802                                                 pDirStr->GetFullFilePath().SubString(destPath.GetLength() + 1, pDirStr->GetFullFilePath().GetLength() - destPath.GetLength() - 1, tempStr);
803                                                 modifiedSourcePath.Append(tempStr);
804
805                                                 AppLogDebug("modified source path is %ls", modifiedSourcePath.GetPointer());
806
807                                                 r = Directory::Rename(destPath, modifiedDestPath);
808                                                 TryCatch(r == E_SUCCESS, , "Rename Failed");
809                                                 AppLogDebug("r %s", GetErrorMessage(r));
810                                                 r = CopyFile(modifiedSourcePath, destPath);
811                                                 TryCatch(r == E_SUCCESS, , "Failed To Copy File");
812                                                 //r = UpdateContentDB(destPath);
813                                                 //TryCatch(r == E_SUCCESS, , "update db content failed");
814                                                 r = DeleteFile(modifiedDestPath);
815                                                 TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND, , "Delete Failed");
816                                         }
817                                         else
818                                         {
819                                                 if (!pDirStr->GetFullFilePath().StartsWith(destPath, 0))
820                                                 {
821                                                         /*r = DeleteFile(destPath);
822                                                         TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND,,"Delete Failed");
823                                                         r = CopyFile(pDirStr->GetFullFilePath(), destPath);
824                                                         TryCatch(r == E_SUCCESS,,"Failed To Copy File");*/
825
826                                                         r = DeleteFileContentDB(destPath);
827                                                         TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND, , "Delete Failed");
828                                                         r = CopyFile(pDirStr->GetFullFilePath(), destPath);
829                                                         TryCatch(r == E_SUCCESS, , "copy failed");
830                                                         //r = UpdateContentDB(destPath);
831                                                         //TryCatch(r == E_SUCCESS, , "update db content failed");
832                                                 }
833                                         }
834
835                                 }
836                         }
837                 }
838                 break;
839
840                 case FILE_EXISTING_OPERATIONID_CANCEL:
841                 {
842                         //goto STOPTHREAD;
843                 }
844                 break;
845
846                 default:
847                 {
848                         // Deafult Case
849                 }
850                 break;
851                 }
852
853                 while (pMapEnum->MoveNext() == E_SUCCESS)
854                 {
855                         pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
856                         destPath.Clear();
857                         destPath.Append(__currentRootPath);
858                         destPath.Append(pDirStr->GetFileName());
859                         if (pDirStr->IsDirectory() == false)
860                         {
861                                 destPath.Append(L".");
862                                 destPath.Append(pDirStr->GetFileExtension());
863                         }
864                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
865                         {
866                                 __fileManagedCounter++;
867
868                                 __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
869                                 pArg = new (std::nothrow) ArrayList();
870                                 pArg->Construct();
871                                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
872                                 pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
873                                 pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
874                                 pArg->Add(pFileManagingResult);
875                                 pArg->Add(pFileManagedCounter);
876                                 pArg->Add(pTotalFileCount);
877                                 Application::GetInstance()->SendUserEvent(ID_COPY_COMPLETE, pArg);
878
879
880                                 r = CopyFile(pDirStr->GetFullFilePath(), destPath);
881                                 TryCatch(r == E_SUCCESS, , "Copy File Failed");
882                                 //r = UpdateContentDB(destPath);
883                                 //TryCatch(r == E_SUCCESS, , "update db Copy File Failed");
884
885                                 //r = Copy(pDirStr->GetFullFilePath(), destPath);
886
887                         }
888                         else
889                         {
890                                 __fileManagingingResult = FILE_MANAGING_RESULT_COPYING_SUCCESS;
891                                 __fileManagedCounter = 0;
892                                 delete pMapEnum;
893                                 return;
894                         }
895
896                 }
897
898                 __fileManagingingResult = FILE_MANAGING_RESULT_COPYING_SUCCESS;
899                 __fileManagedCounter = 0;
900                 AppLogDebug("Exit %s", GetErrorMessage(r));
901                 delete pMapEnum;
902                 return;
903
904 CATCH:
905
906         //AppLogDebug("")
907                 if (r == E_STORAGE_FULL)
908                 {
909                         __fileManagingingResult = FILE_MANAGING_RESULT_STORAGE_FULL;
910                 }
911                 else
912                 {
913                         __fileManagingingResult = FILE_MANAGING_RESULT_COPYING_FAILED;
914                 }
915                 //r = DeleteFile(destPath);
916                 if (renameFileList != null)
917                 {
918                         renameFileList->RemoveAll(true);
919                         delete renameFileList;
920                         renameFileList = null;
921                 }
922
923                 /*pArg = new (std::nothrow) ArrayList();
924                 pArg->Construct();
925                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
926                 pArg->Add(pFileManagingResult);
927                 Application::GetInstance()->SendUserEvent(ID_FILE_MANAGING_FAILED, pArg);*/
928                 AppLogDebug("Exit %s", GetErrorMessage(r));
929                 delete pMapEnum;
930                 return;
931         }
932 }
933 void
934 FileManageWorkerThread::MoveFolderEntry(FileExistingOperationId fileExistingOperationId)
935 {
936         AppLogDebug("Entry");
937         result r = E_SUCCESS;
938         DirectoryEntry* pDirStr = null;
939         String destPath;
940         ArrayList* pArg = null;
941         Integer* pFileManagedCounter = null;
942         Integer* pTotalFileCount = null;
943         Integer* pFileManagingResult = null;
944         String* pRenameFilePath;
945         ArrayList* renameFileList = null;
946         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
947
948     __fileManagedCounter = 0;
949
950         if (__pListOfFiles->GetCount() > 0)
951         {
952                 switch (fileExistingOperationId)
953                 {
954                 case FILE_EXISTING_OPERATIONID_NONE:
955                 {
956                         if (IsSourceSameAsDestination())
957                         {
958                                 __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
959                                 delete pMapEnum;
960                                 return;
961                         }
962                         if (CheckFileDuplicity() == true)
963                         {
964                                 __fileManagingingResult = FILE_MANAGING_RESULT_DUPLICITY;
965                                 delete pMapEnum;
966                                 return;
967                         }
968
969                 }
970                 break;
971
972                 case FILE_EXISTING_OPERATIONID_RENAME:
973                 {
974                         renameFileList = GetListOfRenameFilesN();
975                         TryCatch(renameFileList != null, , "Failed to allocate memory to rename file list");
976                         int counter = 0;
977                         while (pMapEnum->MoveNext() == E_SUCCESS)
978                         {
979                                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
980                                 if (pDirStr != null)
981                                 {
982                                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
983                                         {
984                                                 __fileManagedCounter++;
985                                                 __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
986
987                                                 pArg = new (std::nothrow) ArrayList();
988                                                 pArg->Construct();
989                                                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
990                                                 pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
991                                                 pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
992                                                 pArg->Add(pFileManagingResult);
993                                                 pArg->Add(pFileManagedCounter);
994                                                 pArg->Add(pTotalFileCount);
995                                                 Application::GetInstance()->SendUserEvent(ID_MOVING_COMPLETE, pArg);
996
997                                                 pRenameFilePath = static_cast< String* >(renameFileList->GetAt(counter));
998
999                                                 r = MoveFile(pDirStr->GetFullFilePath(), *pRenameFilePath);
1000                                                 TryCatch(r == E_SUCCESS || r == E_THREAD_CANCEL,,"Move File Failed");
1001
1002                                                 if(r == E_THREAD_CANCEL)
1003                                                 {
1004                                                         __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_SUCCESS;
1005                                                         __fileManagedCounter = 0;
1006                                                         delete pMapEnum;
1007                                                         return;
1008                                                 }
1009                                                 /*r = CopyFile(pDirStr->GetFullFilePath(), *pRenameFilePath);
1010                                                 r = UpdateContentDB(*pRenameFilePath);
1011                                                 //TryCatch(r == E_SUCCESS, , "move File Failed");
1012                                                 r = DeleteFileContentDB(pDirStr->GetFullFilePath());
1013                                                 TryCatch(r == E_SUCCESS, , "Delete source failed");*/
1014                                         }
1015                                         else
1016                                         {
1017                                                 __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_SUCCESS;
1018                                                 __fileManagedCounter = 0;
1019                                                 delete pMapEnum;
1020                                                 return;
1021                                         }
1022                                 }
1023                                 counter++;
1024                         }
1025                         if (renameFileList != null)
1026                         {
1027                                 renameFileList->RemoveAll(true);
1028                                 delete renameFileList;
1029                                 renameFileList = null;
1030                         }
1031                 }
1032                 break;
1033
1034                 case FILE_EXISTING_OPERATIONID_REPLACE:
1035                 {
1036                         while (pMapEnum->MoveNext() == E_SUCCESS)
1037                         {
1038                                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1039                                 if (pDirStr != null)
1040                                 {
1041                                         destPath.Clear();
1042                                         destPath.Append(__currentRootPath);
1043                                         destPath.Append(pDirStr->GetFileName());
1044                                         if (!pDirStr->IsDirectory())
1045                                         {
1046                                                 destPath.Append(L".");
1047                                                 destPath.Append(pDirStr->GetFileExtension());
1048                                         }
1049
1050                                         __fileManagedCounter++;
1051                                         __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
1052
1053                                         pArg = new (std::nothrow) ArrayList();
1054                                         pArg->Construct();
1055                                         pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
1056                                         pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
1057                                         pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
1058                                         pArg->Add(pFileManagingResult);
1059                                         pArg->Add(pFileManagedCounter);
1060                                         pArg->Add(pTotalFileCount);
1061                                         Application::GetInstance()->SendUserEvent(ID_MOVING_COMPLETE, pArg);
1062
1063                                         String tempDestStr;
1064                                         tempDestStr.Append(destPath);
1065                                         tempDestStr.Append(L"/");
1066                                         if (pDirStr->GetFullFilePath().StartsWith(tempDestStr, 0))
1067                                         {
1068                                                 AppLogDebug("case exist");
1069                                                 String modifiedDestPath;
1070                                                 modifiedDestPath.Append(destPath);
1071                                                 DateTime t;
1072                                                 Tizen::System::SystemTime::GetCurrentTime(t);
1073                                                 modifiedDestPath.Append(t.GetDay());
1074                                                 modifiedDestPath.Append(t.GetMonth());
1075                                                 modifiedDestPath.Append(t.GetYear());
1076                                                 modifiedDestPath.Append(t.GetHour());
1077                                                 modifiedDestPath.Append(t.GetMinute());
1078                                                 modifiedDestPath.Append(t.GetSecond());
1079
1080                                                 String modifiedSourcePath;
1081                                                 modifiedSourcePath.Append(modifiedDestPath);
1082                                                 modifiedSourcePath.Append(L"/");
1083                                                 String tempStr;
1084                                                 pDirStr->GetFullFilePath().SubString(destPath.GetLength() + 1, pDirStr->GetFullFilePath().GetLength() - destPath.GetLength() - 1, tempStr);
1085                                                 modifiedSourcePath.Append(tempStr);
1086
1087                                                 AppLogDebug("modified source path is %ls", modifiedSourcePath.GetPointer());
1088
1089                                                 r = Directory::Rename(destPath, modifiedDestPath);
1090                                                 TryCatch(r == E_SUCCESS, , "Rename Failed");
1091                                                 AppLogDebug("r %s", GetErrorMessage(r));
1092                                                 r = MoveFile(modifiedSourcePath, destPath);
1093                                                 TryCatch(r == E_SUCCESS, , "Move File Failed");
1094                                                 r = DeleteFile(modifiedDestPath);
1095                                                 TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND, , "Delete Failed");
1096                                         }
1097                                         else
1098                                         {
1099
1100                                                 /*r = DeleteFile(destPath);
1101                                                 TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND,,"Delete Failed");
1102                                                 r = MoveFile(pDirStr->GetFullFilePath(), destPath);
1103                                                 TryCatch(r == E_SUCCESS,,"Move File Failed");*/
1104
1105                                                 r = DeleteFileContentDB(destPath);
1106                                                 TryCatch(r == E_SUCCESS || r == E_FILE_NOT_FOUND, , "Delete same file failed");
1107                                                 r = CopyFile(pDirStr->GetFullFilePath(), destPath);
1108                                                 r = UpdateContentDB(destPath);
1109                                                 //TryCatch(r == E_SUCCESS, , "move File Failed");
1110                                                 r = DeleteFileContentDB(pDirStr->GetFullFilePath());
1111                                                 TryCatch(r == E_SUCCESS, , "Delete source failed");
1112                                         }
1113
1114                                 }
1115                         }
1116                 }
1117                 break;
1118
1119                 case FILE_EXISTING_OPERATIONID_CANCEL:
1120                 {
1121                         //goto STOPTHREAD;
1122                 }
1123                 break;
1124
1125                 default:
1126                 {
1127                         // Deafult Case
1128                 }
1129                 break;
1130                 }
1131
1132                 while (pMapEnum->MoveNext() == E_SUCCESS)
1133                 {
1134                         pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1135                         destPath.Clear();
1136                         destPath.Append(__currentRootPath);
1137                         destPath.Append(pDirStr->GetFileName());
1138                         if (pDirStr->IsDirectory() == false)
1139                         {
1140                                 destPath.Append(L".");
1141                                 destPath.Append(pDirStr->GetFileExtension());
1142                         }
1143                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
1144                         {
1145
1146                                 __fileManagedCounter++;
1147
1148                                 __fileManagingingResult = FILE_MANAGING_RESULT_CURRENT_STATUS;
1149                                 pArg = new (std::nothrow) ArrayList();
1150                                 pArg->Construct();
1151                                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
1152                                 pFileManagedCounter = new (std::nothrow) Integer(__fileManagedCounter);
1153                                 pTotalFileCount = new (std::nothrow) Integer(__pListOfFiles->GetCount());
1154                                 pArg->Add(pFileManagingResult);
1155                                 pArg->Add(pFileManagedCounter);
1156                                 pArg->Add(pTotalFileCount);
1157                                 Application::GetInstance()->SendUserEvent(ID_MOVING_COMPLETE, pArg);
1158
1159                                 r = MoveFile(pDirStr->GetFullFilePath(), destPath);
1160                                 TryCatch(r == E_SUCCESS || r == E_THREAD_CANCEL, , "move File Failed");
1161
1162                                 if(r == E_THREAD_CANCEL)
1163                                 {
1164                                         __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_SUCCESS;
1165                                         __fileManagedCounter = 0;
1166                                         delete pMapEnum;
1167                                         return;
1168                                 }
1169                                 /*r = CopyFile(pDirStr->GetFullFilePath(), destPath);
1170                                 TryCatch(r == E_SUCCESS, , "Copy file failed");
1171                                 r = UpdateContentDB(destPath);
1172                                 //TryCatch(r == E_SUCCESS, , "move File Failed");
1173                                 r = DeleteFileContentDB(pDirStr->GetFullFilePath());
1174                                 TryCatch(r == E_SUCCESS, , "Delete source failed");*/
1175
1176                         }
1177                         else
1178                         {
1179                                 __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_SUCCESS;
1180                                 __fileManagedCounter = 0;
1181                                 delete pMapEnum;
1182                                 return;
1183                         }
1184
1185                 }
1186
1187                 __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_SUCCESS;
1188                 __fileManagedCounter = 0;
1189                 AppLogDebug("Exit %s", GetErrorMessage(r));
1190                 delete pMapEnum;
1191                 return;
1192
1193 CATCH:
1194                 if (r == E_STORAGE_FULL)
1195                 {
1196                         __fileManagingingResult = FILE_MANAGING_RESULT_STORAGE_FULL;
1197                 }
1198                 else
1199                 {
1200                         __fileManagingingResult = FILE_MANAGING_RESULT_MOVING_FAILED;
1201                 }
1202
1203                 if (renameFileList != null)
1204                 {
1205                         renameFileList->RemoveAll(true);
1206                         delete renameFileList;
1207                         renameFileList = null;
1208                 }
1209
1210                 //__fileManagingingResult = FILE_MANAGING_RESULT_MOVING_FAILED;
1211                 pArg = new (std::nothrow) ArrayList();
1212                 pArg->Construct();
1213                 pFileManagingResult = new (std::nothrow) Integer(__fileManagingingResult);
1214                 pArg->Add(pFileManagingResult);
1215                 Application::GetInstance()->SendUserEvent(ID_FILE_MANAGING_FAILED, pArg);
1216                 AppLogDebug("Exit %s", GetErrorMessage(r));
1217                 delete pMapEnum;
1218                 return;
1219         }
1220 }
1221
1222 bool
1223 FileManageWorkerThread::IsSourceSameAsDestination(void)
1224 {
1225         AppLogDebug("Entry");
1226         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
1227         bool r = false;
1228         DirectoryEntry* pDirStr = null;
1229         String destPath;
1230         if (__pListOfFiles->GetCount() > 0)
1231         {
1232                 while (pMapEnum->MoveNext() == E_SUCCESS)
1233                 {
1234                         pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1235                         if (pDirStr != null)
1236                         {
1237                                 destPath.Clear();
1238                                 if (pDirStr->IsDirectory())
1239                                 {
1240                                         destPath.Append(__currentRootPath);
1241                                         destPath.Append(pDirStr->GetFileName());
1242                                         destPath.Append(L"/");
1243
1244                                         /*if (__currentRootPath.StartsWith(pDirStr->GetFullFilePath(), 0))
1245                                         {
1246                                             AppLogDebug("hello");
1247                                             __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
1248                                             r = true;
1249                                             break;
1250                                         }
1251                                         else
1252                                         {
1253                                             r = false;
1254                                         }
1255                                          */
1256                                 }
1257                                 else
1258                                 {
1259                                         destPath.Append(__currentRootPath);
1260                                         destPath.Append(pDirStr->GetFileName());
1261                                         destPath.Append(L".");
1262                                         destPath.Append(pDirStr->GetFileExtension());
1263                                 }
1264
1265                                 if (destPath == pDirStr->GetFullFilePath() || __currentRootPath.StartsWith(pDirStr->GetFullFilePath(), 0))
1266                                 {
1267                                         AppLogDebug("hello");
1268                                         __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
1269                                         r = true;
1270                                         break;
1271                                 }
1272                                 else
1273                                 {
1274                                         r = false;
1275                                 }
1276
1277                         }
1278                 }
1279
1280
1281         }
1282         AppLogDebug("Exit");
1283         delete pMapEnum;
1284         return r;
1285 }
1286
1287 String
1288 FileManageWorkerThread::GenerateNewName(DirectoryEntry* pSourceDirStr)
1289 {
1290         AppLogDebug("Entry");
1291         IMapEnumerator* pMapEnum = null;
1292         pMapEnum = __pListOfFiles->GetMapEnumeratorN();
1293         String rename;
1294         int count = 1;
1295         DirectoryEntry* pDirStr = null;
1296         String filePath;
1297
1298         rename.Append(pSourceDirStr->GetFileName());
1299         rename.Append(L"_");
1300         rename.Append(count);
1301
1302         while (pMapEnum->MoveNext() == E_SUCCESS)
1303         {
1304                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1305                 filePath.Clear();
1306                 filePath.Append(__currentRootPath);
1307                 filePath.Append(rename);
1308                 if (pDirStr != null)
1309                 {
1310                         if (pDirStr->IsDirectory() == false)
1311                         {
1312                                 filePath.Append(L".");
1313                                 filePath.Append(pDirStr->GetFileExtension());
1314                         }
1315
1316                         if (rename == pDirStr->GetFileName() || CheckFileDuplicity())
1317                         {
1318                                 count++;
1319                                 rename.Clear();
1320                                 rename.Append(pSourceDirStr->GetFileName());
1321                                 rename.Append(L"_");
1322                                 rename.Append(count);
1323                                 pMapEnum->Reset();
1324                         }
1325
1326                 }
1327
1328         }
1329
1330         delete pMapEnum;
1331         pMapEnum = null;
1332         AppLogDebug("Exit");
1333         return rename;
1334 }
1335
1336 void
1337 FileManageWorkerThread::StopThread(void)
1338 {
1339         __threadRunningState = THREAD_RUNNING_STATE_STOP;
1340 }
1341
1342 ArrayList*
1343 FileManageWorkerThread::GetListOfRenameFilesN(void)
1344 {
1345
1346         AppLogDebug("Enter");
1347         ArrayList* pRenameFileList;
1348         pRenameFileList = null;
1349         IMapEnumerator* pMapEnum = null;
1350         String* pRenameFile;
1351         pRenameFile = null;
1352         pRenameFileList = new (std::nothrow) ArrayList();
1353         TryCatch(pRenameFileList != null, , "failed to allocate memory to renameFileList");
1354
1355         pMapEnum = __pListOfFiles->GetMapEnumeratorN();
1356         while (pMapEnum->MoveNext() == E_SUCCESS)
1357         {
1358
1359                 DirectoryEntry* pDirStr = null;
1360                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1361                 if (pDirStr != null)
1362                 {
1363                         String renameFileName(L"");
1364                         FileAttributes fileAttrib;
1365                         File::GetAttributes(pDirStr->GetFullFilePath(), fileAttrib);
1366                         String fileName = pDirStr->GetFileName();
1367                         if (!pDirStr->IsDirectory())
1368                         {
1369                                 fileName.Append(L".");
1370                                 fileName.Append(pDirStr->GetFileExtension());
1371                         }
1372
1373                         renameFileName.Append(__currentRootPath);
1374                         renameFileName.Append(fileName);
1375                         int count = 1;
1376                         while (File::IsFileExist(renameFileName) == true || IsFileNameExist(renameFileName, pRenameFileList) == true)
1377                         {
1378                                 renameFileName.Clear();
1379                                 if (fileAttrib.IsDirectory())
1380                                 {
1381                                         renameFileName.Append(__currentRootPath);
1382                                         renameFileName.Append(pDirStr->GetFileName());
1383                                         renameFileName.Append(L"_");
1384                                         renameFileName.Append(count);
1385                                 }
1386                                 else
1387                                 {
1388
1389                                         renameFileName.Append(__currentRootPath);
1390                                         renameFileName.Append(pDirStr->GetFileName());
1391                                         renameFileName.Append(L"_");
1392                                         renameFileName.Append(count);
1393                                         renameFileName.Append(L".");
1394                                         renameFileName.Append(pDirStr->GetFileExtension());
1395                                 }
1396                                 count++;
1397                         }
1398
1399                         pRenameFile = new (std::nothrow) String();
1400                         TryCatch(pRenameFile != null, , "failed to allocate memoy to renamefile");
1401
1402                         pRenameFile->Append(renameFileName);
1403                         pRenameFileList->Add(pRenameFile);
1404
1405
1406                 }
1407
1408         }
1409         for (int i = 0; i < pRenameFileList->GetCount(); i++)
1410         {
1411                 String* pName = (String*) pRenameFileList->GetAt(i);
1412                 AppLogDebug("rename is %ls", pName->GetPointer());
1413         }
1414         delete pMapEnum;
1415         return pRenameFileList;
1416
1417 CATCH:
1418         if (pRenameFileList != null)
1419         {
1420                 pRenameFileList->RemoveAll(true);
1421                 delete pRenameFileList;
1422                 pRenameFileList = null;
1423         }
1424         return null;
1425
1426 }
1427
1428 bool
1429 FileManageWorkerThread::CheckForLegalDestination(void)
1430 {
1431         AppLogDebug("Entry");
1432         IMapEnumerator* pMapEnum = __pListOfFiles->GetMapEnumeratorN();
1433         bool r = true;
1434         DirectoryEntry* pDirStr = null;
1435
1436         while (pMapEnum->MoveNext() == E_SUCCESS)
1437         {
1438                 pDirStr = static_cast< DirectoryEntry* >(pMapEnum->GetValue());
1439                 if (pDirStr != null)
1440                 {
1441                         if (pDirStr->IsDirectory())
1442                         {
1443
1444                                 if (__currentRootPath.StartsWith(pDirStr->GetFullFilePath(), 0))
1445                                 {
1446                                         __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
1447                                         r = false;
1448                                         break;
1449                                 }
1450                                 else
1451                                 {
1452                                         r = true;
1453                                 }
1454
1455                         }
1456                         /*else
1457                         {
1458                             destPath.Append(__currentRootPath);
1459                             destPath.Append(pDirStr->GetFileName());
1460                             destPath.Append(L".");
1461                             destPath.Append(pDirStr->GetFileExtension());
1462                         }*/
1463
1464                         /*if (destPath == pDirStr->GetFullFilePath() || __currentRootPath.StartsWith(pDirStr->GetFullFilePath(), 0))
1465                         {
1466                             AppLogDebug("hello");
1467                             __fileManagingingResult = FILE_MANAGING_RESULT_SOURCE_SAME_AS_DESTINATION;
1468                             r = true;
1469                             break;
1470                         }
1471                         else
1472                         {
1473                             r = false;
1474                         }
1475                          */
1476                 }
1477         }
1478         AppLogDebug("Exit");
1479         delete pMapEnum;
1480         return r;
1481 }
1482
1483 bool
1484 FileManageWorkerThread::IsFileNameExist(Tizen::Base::String fileName, Tizen::Base::Collection::ArrayList* pRenameList)
1485 {
1486         for (int i = 0; i < pRenameList->GetCount(); i++)
1487         {
1488                 String* pRenamedFileName = static_cast< String* >(pRenameList->GetAt(i));
1489                 if (*pRenamedFileName == fileName)
1490                 {
1491                         return true;
1492                 }
1493         }
1494         return false;
1495 }
1496
1497 void
1498 FileManageWorkerThread::SortArrayList(Tizen::Base::Collection::ArrayList* pArrList)
1499 {
1500         for (int i = 0; i < pArrList->GetCount(); i++)
1501         {
1502                 Integer* pValue1 = (Integer*) pArrList->GetAt(i);
1503                 for (int j = i + 1; j < pArrList->GetCount(); j++)
1504                 {
1505                         Integer* pValue2 = (Integer*) pArrList->GetAt(j);
1506                         if (pValue1->ToInt() > pValue2->ToInt())
1507                         {
1508                                 int temp;
1509                                 temp = pValue1->ToInt();
1510                                 *pValue1 = *pValue2;
1511                                 *pValue2 = temp;
1512                                 //value2(temp);
1513                         }
1514                 }
1515         }
1516 }
1517
1518 ContentType
1519 FileManageWorkerThread::GetContentTypeInfo(const String& FilePath)
1520 {
1521
1522         ContentType contentType = CONTENT_TYPE_UNKNOWN;
1523         contentType = ContentManagerUtil::CheckContentType(FilePath);
1524         AppLogDebug("Content type result is %s",GetErrorMessage(GetLastResult()));
1525         return contentType;
1526 }
1527
1528 result
1529 FileManageWorkerThread::DeleteFileContentDB(String filePath)
1530 {
1531         AppLogDebug("Entry");
1532         result r = E_SUCCESS;
1533         ContentId contentId;
1534         ContentManager contentManager;
1535         ArrayList* pContentList = null;
1536         ArrayList* pBackUpContentList = null;
1537         DirectoryEntry* pDirStr = null;
1538         DirectoryEntry* pDirStrTemp = null;
1539         String sourcePath;
1540         FileAttributes fileAttrib;
1541         File::GetAttributes(filePath, fileAttrib);
1542
1543         AppLogDebug("file path is %ls", filePath.GetPointer());
1544         if (fileAttrib.IsDirectory())
1545         {
1546                 pBackUpContentList = new (std::nothrow) ArrayList();
1547                 pBackUpContentList->Construct();
1548
1549                 AppLogDebug("source path is %ls", sourcePath.GetPointer());
1550                 if(!filePath.EndsWith(L"/"))
1551                 {
1552                         filePath.Append(L"/");
1553                 }
1554                 __pFolderEntryPM->SetFolderEntryPath(filePath);
1555                 pContentList = __pFolderEntryPM->GetFolderEntryList();
1556                 TryCatch(pContentList != null, , "Failed to get content list");
1557                 AppLogDebug("pcontent list count is %d", pContentList->GetCount());
1558
1559                 for (int count = 0; count < pContentList->GetCount(); count++)
1560                 {
1561                         pDirStrTemp = static_cast< DirectoryEntry* >(pContentList->GetAt(count));
1562                         TryCatch(pDirStrTemp != null, , "Failed to allocate memory to pDirStrTemp");
1563                         pDirStr = new (std::nothrow) DirectoryEntry(*pDirStrTemp);
1564                         TryCatch(pDirStr != null, , "Failed to allocate memory to pdirstr");
1565                         pBackUpContentList->Add(pDirStr);
1566                 }
1567
1568                 for (int counter = 0; counter < pBackUpContentList->GetCount(); counter++)
1569                 {
1570                         pDirStr = static_cast< DirectoryEntry* >(pBackUpContentList->GetAt(counter));
1571                         AppLogDebug("dir name is %ls", pDirStr->GetFileName().GetPointer());
1572                         sourcePath.Clear();
1573                         sourcePath.Append(pDirStr->GetFullFilePath());
1574
1575                         if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
1576                         {
1577                            r = DeleteFileContentDB(sourcePath);
1578                         }
1579                         else
1580                         {
1581                                 pBackUpContentList->RemoveAll(true);
1582                                 delete pBackUpContentList;
1583                                 pBackUpContentList = null;
1584                                 return E_THREAD_CANCEL;
1585                         }
1586                         //TryCatch(r == E_SUCCESS, , "content Delete failed");
1587                 }
1588
1589                 r = Directory::Remove(filePath, true);
1590                 TryCatch(r == E_SUCCESS, , "content Delete failed");
1591
1592                 pBackUpContentList->RemoveAll(true);
1593                 delete pBackUpContentList;
1594                 pBackUpContentList = null;
1595
1596         }
1597         else
1598         {
1599                 if (__threadRunningState == THREAD_RUNNING_STATE_ALIVE)
1600                 {
1601                         contentManager.Construct();
1602                         r = GetContentID(filePath, contentId);
1603                         if(r == E_SUCCESS)
1604                         {
1605                                 contentManager.DeleteContent(contentId);
1606                         }
1607                         else
1608                         {
1609                                 r = DeleteFile(filePath);
1610                         }
1611                 }
1612                 else
1613                 {
1614                         return E_THREAD_CANCEL;
1615                 }
1616
1617         }
1618         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_SUCCESS;
1619         AppLogDebug("Exit");
1620         return r;
1621 CATCH:
1622
1623         __fileManagingingResult = FILE_MANAGING_RESULT_DELETING_FAILED;
1624         AppLogDebug("Catch result %s", GetErrorMessage(r));
1625         AppLogDebug("Exit");
1626         r = E_FAILURE;
1627         return r;
1628 }
1629
1630 result
1631 FileManageWorkerThread::UpdateContentDB(Tizen::Base::String filePath)
1632 {
1633         AppLogDebug("Entry");
1634         result r = E_SUCCESS;
1635         ContentId contentId;
1636         ArrayList* pContentList = null;
1637         DirectoryEntry* pDirStr = null;
1638         String tempFilePath;
1639         FileAttributes fileAttrib;
1640         File::GetAttributes(filePath, fileAttrib);
1641         AppLogDebug("file path is %ls", filePath.GetPointer());
1642         //AppLogDebug("dest path is %ls",destPath.GetPointer());
1643
1644         if (fileAttrib.IsDirectory())
1645         {
1646                 filePath.Append(L"/");
1647                 __pFolderEntryPM->SetFolderEntryPath(filePath);
1648                 pContentList = __pFolderEntryPM->GetFolderEntryList();
1649                 TryCatch(pContentList != null, , "Failed to get content list");
1650                 AppLogDebug("pcontent list count is %d", pContentList->GetCount());
1651                 for (int counter = 0; counter < pContentList->GetCount(); counter++)
1652                 {
1653                         pDirStr = static_cast< DirectoryEntry* >(pContentList->GetAt(counter));
1654                         AppLogDebug("dir name is %ls", pDirStr->GetFileName().GetPointer());
1655                         tempFilePath.Clear();
1656                         tempFilePath.Append(pDirStr->GetFullFilePath());
1657                         r = UpdateContentDB(tempFilePath);
1658                         //TryCatch(r == E_SUCCESS, , "content copy updated");
1659                 }
1660         }
1661         else
1662         {
1663                 r = GetContentID(filePath, contentId);
1664
1665         }
1666         AppLogDebug("Exit");
1667         return r;
1668 CATCH:
1669         r = GetLastResult();
1670         AppLogDebug("result is %s", GetErrorMessage(r));
1671         AppLogDebug("Exit");
1672         return r;
1673 }
1674
1675 result
1676 FileManageWorkerThread::GetContentID(const String& filePath, ContentId& contentId)
1677 {
1678
1679         ContentManager contentManager;
1680         //ContentId contentId;
1681         result r = E_SUCCESS;
1682         ContentType contentType;
1683         ContentInfo* contentInfo = null;
1684         contentType = CONTENT_TYPE_UNKNOWN;
1685
1686         contentManager.Construct();
1687
1688         contentType = GetContentTypeInfo(filePath);
1689
1690
1691         switch (contentType)
1692         {
1693         case CONTENT_TYPE_IMAGE:
1694         {
1695                 AppLogDebug("CONTENT_TYPE_IMAGE");
1696                 ImageContentInfo* imagecontentInfo = null;
1697                 imagecontentInfo = new (std::nothrow) ImageContentInfo();
1698                 String* pPath = new (std::nothrow) String(filePath);
1699                 imagecontentInfo->Construct(pPath);
1700                 delete pPath;
1701                 contentInfo = imagecontentInfo;
1702                 contentId = contentManager.CreateContent(*imagecontentInfo);
1703                 TryCatch(GetLastResult() == E_SUCCESS, , "create content failed");
1704                 delete imagecontentInfo;
1705         }
1706         break;
1707
1708         case CONTENT_TYPE_AUDIO:
1709         {
1710                 AppLogDebug("CONTENT_TYPE_AUDIO");
1711                 AudioContentInfo* audiocontentInfo = null;
1712                 audiocontentInfo = new (std::nothrow) AudioContentInfo();
1713                 TryCatch(audiocontentInfo != null, , "memory for audio failed");
1714                 String* pPath = new (std::nothrow) String(filePath);
1715                 r = audiocontentInfo->Construct(pPath);
1716                 delete pPath;
1717                 contentInfo = audiocontentInfo;
1718                 contentId = contentManager.CreateContent(*audiocontentInfo);
1719                 TryCatch(GetLastResult() == E_SUCCESS, , "create content failed");
1720                 delete audiocontentInfo;
1721         }
1722         break;
1723
1724         case CONTENT_TYPE_VIDEO:
1725         {
1726                 AppLogDebug("CONTENT_TYPE_VIDEO");
1727                 VideoContentInfo* videocontentInfo = null;
1728                 videocontentInfo = new (std::nothrow) VideoContentInfo();
1729                 String* pPath = new (std::nothrow) String(filePath);
1730                 videocontentInfo->Construct(pPath);
1731                 delete pPath;
1732                 contentInfo = videocontentInfo;
1733                 contentId = contentManager.CreateContent(*videocontentInfo);
1734                 TryCatch(GetLastResult() == E_SUCCESS, , "create content failed");
1735                 delete videocontentInfo;
1736         }
1737         break;
1738
1739         case CONTENT_TYPE_OTHER:
1740         {
1741                 AppLogDebug("CONTENT_TYPE_OTHER");
1742                 OtherContentInfo* othercontentInfo = null;
1743                 othercontentInfo = new (std::nothrow) OtherContentInfo();
1744                 String* pPath = new (std::nothrow) String(filePath);
1745                 othercontentInfo->Construct(pPath);
1746                 delete pPath;
1747                 contentInfo = othercontentInfo;
1748                 contentId = contentManager.CreateContent(*othercontentInfo);
1749                 TryCatch(GetLastResult() == E_SUCCESS, , "create content failed");
1750                 delete othercontentInfo;
1751         }
1752         break;
1753
1754         case CONTENT_TYPE_UNKNOWN:
1755         {
1756                 AppLogDebug("CONTENT_TYPE_UNKNOWN");
1757                 return E_FAILURE;
1758         }
1759         break;
1760
1761         default:
1762                 break;
1763         }
1764
1765         AppLogDebug("Exit");
1766         return E_SUCCESS;
1767
1768 CATCH:
1769         r = GetLastResult();
1770         AppLogDebug("catch result %s", GetErrorMessage(r));
1771         if (contentInfo != null)
1772         {
1773                 delete contentInfo;
1774                 contentInfo = null;
1775         }
1776
1777
1778         AppLogDebug("catch result %s", GetErrorMessage(r));
1779         AppLogDebug("Exit");
1780         return r;
1781 }
1782
1783 result
1784 FileManageWorkerThread::RenameFolderEntry(void)
1785 {
1786         result r = E_SUCCESS;
1787         FileAttributes fileAttrib;
1788
1789         r = CopyFile(__filePath,__renameFilePath);
1790         TryCatch(r == E_SUCCESS,,"copy failed");
1791
1792         r = DeleteFileContentDB(__filePath);
1793
1794         File::GetAttributes(__renameFilePath, fileAttrib);
1795         if(fileAttrib.IsDirectory())
1796         {
1797                 __renameFilePath.Append(L"/");
1798         }
1799
1800         __fileManagingingResult = FILE_MANAGING_RENAME_SUCCESS;
1801
1802         return r;
1803 CATCH:
1804         r = DeleteFile(__renameFilePath);
1805         __fileManagingingResult = FILE_MANAGING_RENAME_FAILED;
1806         AppLogDebug("catch result is %s",GetErrorMessage(r));
1807         return r;
1808 }
1809
1810 result
1811 FileManageWorkerThread::Copy(Tizen::Base::String& sourcePath, Tizen::Base::String& destPath)
1812 {
1813         AppLogDebug("FileManageWorkerThread::Copy");
1814         result r;
1815         const int BUFFER_SIZE_MAX = 4096;
1816         File sourceFIle;
1817         File destFile;
1818         int count= 0;
1819         void* byteToWrite = null;
1820         byteToWrite = (void*) malloc(sizeof(char)*BUFFER_SIZE_MAX);
1821
1822         r = sourceFIle.Construct(sourcePath, "r");
1823         TryCatch(r == E_SUCCESS, , "Failed to open source file in read mode");
1824
1825         AppLogDebug("FileManageWorkerThread::Copy:%s", GetErrorMessage(r));
1826
1827         r = destFile.Construct(destPath, "w+");
1828         TryCatch(r == E_SUCCESS, , "Failed to open destination file in write mode");
1829
1830         count = sourceFIle.Read(byteToWrite, BUFFER_SIZE_MAX);
1831         AppLogDebug("FileManageWorkerThread::Copy:%d", count);
1832         while (count >0)
1833         {
1834                 if (__threadRunningState != THREAD_RUNNING_STATE_ALIVE)
1835                 {
1836                         File::Remove(destPath);
1837                         return E_SUCCESS;
1838                 }
1839                 if (!IsFailed(GetLastResult()))
1840                 {
1841                         destFile.Write(byteToWrite, BUFFER_SIZE_MAX);
1842                         count = sourceFIle.Read(byteToWrite, BUFFER_SIZE_MAX);
1843                 }
1844         }
1845         return E_SUCCESS;
1846 CATCH:
1847         return E_FAILURE;
1848 }