Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_Clipboard.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://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  * @file                FUi_Clipboard.cpp
19  * @brief               This is the implementation file for the _Clipboard class.
20  */
21 #include <FIoFile.h>
22 #include <FTextUtf8Encoding.h>
23 #include <FBaseColLinkedList.h>
24 #include <FBaseErrorDefine.h>
25 #include <FUiIClipboardPopupEventListener.h>
26 #include <FUiClipboard.h>
27 #include <FMediaImageTypes.h>
28 #include <FGrpBitmap.h>
29 #include <FBaseSysLog.h>
30 #include <FMedia_ImageEncoder.h>
31 #include <FMedia_ImageDecoder.h>
32 #include <FMedia_ImageUtil.h>
33 #include <FGrp_BitmapImpl.h>
34 #include "FUi_Clipboard.h"
35 #include "FUi_ClipboardItem.h"
36 #include "FUi_ControlManager.h"
37 #include "FUi_EcoreEvasMgr.h"
38 #include "FUiCtrl_PublicClipboardPopupEvent.h"
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Io;
45 using namespace Tizen::Media;
46 using namespace Tizen::Text;
47 using namespace Tizen::Ui::Controls;
48
49 namespace
50 {
51 const int _CLIPBOARD_ITEM_MAX = 12;
52 const int _CBHM_ITEM_MAX = 12;
53 const String _CLIPBOARD_DEFAULT_PATH = L"/tmp/clipboard_";
54 const String _CLIPBOARD_DEFAULT_EXTENSION = L".png";
55 } // Anonymous
56
57 namespace Tizen { namespace Ui
58 {
59 _Clipboard* _Clipboard::__pInstance = null;
60
61 _Clipboard*
62 _Clipboard::GetInstance(void)
63 {
64         if (__pInstance)
65         {
66                 return __pInstance;
67         }
68
69         if (_ControlManager::GetInstance())
70         {
71                 __pInstance = new (std::nothrow) _Clipboard;
72                 SysAssert(__pInstance);
73
74                 return __pInstance;
75         }
76
77         return null;
78 }
79
80 void
81 _Clipboard::ReleaseInstance(void)
82 {
83         if (__pInstance)
84         {
85                 delete __pInstance;
86                 __pInstance = null;
87         }
88 }
89
90 _Clipboard::~_Clipboard(void)
91 {
92         HidePopup();
93
94         if (__pPublicEvent)
95         {
96                 delete __pPublicEvent;
97                 __pPublicEvent = null;
98         }
99 }
100
101 result
102 _Clipboard::CopyItem(const _ClipboardItem& item)
103 {
104         SysTryReturn(NID_UI, IsValidDataType(item.GetDataType()), E_INVALID_ARG, E_INVALID_ARG,
105                                 "[E_INVALID_ARG] The data type is invalid.");
106
107         _ClipFormat format = ConvertToClipFormat(item.GetDataType());
108         SysTryReturn(NID_UI, format != _CLIP_FORMAT_NONE, E_INVALID_ARG, E_INVALID_ARG,
109                                 "[E_INVALID_ARG] The data type is invalid.");
110
111         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
112         SysTryReturn(NID_UI, pEcoreEvas, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
113
114         char* pChar = null;
115         if (format == _CLIP_FORMAT_IMAGE)
116         {
117                 const Bitmap* pBitmap = dynamic_cast <const Bitmap*>(item.GetData());
118                 SysTryReturn(NID_UI, pBitmap, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The data is invalid.");
119
120                 pChar = CreateImageFileN(*pBitmap);
121         }
122         else
123         {
124                 const String* pString = dynamic_cast <const String*>(item.GetData());
125                 SysTryReturn(NID_UI, pString, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The data is invalid.");
126
127                 pChar = EncodeToCharN(*pString);
128         }
129
130         result r = GetLastResult();
131         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
132
133         r = pEcoreEvas->CopyClip(format, pChar);
134         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
135
136         SysLog(NID_UI, "[Clipboard] Clip : format = [%d], char = [%s] is copied.", format, pChar);
137
138         // fall through
139
140 CATCH:
141         if (pChar)
142         {
143                 free(pChar);
144         }
145
146         return r;
147 }
148
149 IList*
150 _Clipboard::RetrieveItemsN(unsigned long dataTypes)
151 {
152         SysTryReturn(NID_UI, IsValidDataType(dataTypes), null, E_INVALID_ARG,
153                                 "[E_INVALID_ARG] The data type is invalid.");
154
155         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
156         SysTryReturn(NID_UI, pEcoreEvas, null, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
157
158         int count = pEcoreEvas->GetClipCount();
159         SysTryReturn(NID_UI, count > 0, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
160
161         LinkedList* pList = new (std::nothrow) LinkedList;
162         SysTryReturn(NID_UI, pList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
163
164         // Add items.
165         int format = _CLIP_FORMAT_NONE;
166         char* pData = null;
167         ClipboardDataType type = CLIPBOARD_DATA_TYPE_NONE;
168         for (int i = 0; i < count; i++)
169         {
170                 // Get the clip with index.
171                 bool ret = pEcoreEvas->RetrieveClipN(i, &format, &pData);
172
173                 if (ret == false)
174                 {
175                         SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
176                         continue;
177                 }
178
179                 //if (!pData) // prevent -> SLP CBHM
180                 //{
181                 //      SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
182                 //      continue;
183                 //}
184
185                 type = ConvertToDataType(format, pData);
186
187                 if (!IsValidDataType(type))
188                 {
189                         SysLog(NID_UI, "The type of a clip to be get form _EcoreEvas is invalid.");
190                         free(pData);
191                         continue;
192                 }
193
194                 // If the clip to be get from _EcoreEvas is in agreement with dataTypes.
195                 if (dataTypes & type)
196                 {
197                         result r = E_SUCCESS;
198                         _ClipboardItem* pItem = null;
199
200                         if (type == CLIPBOARD_DATA_TYPE_IMAGE)
201                         {
202                                 const Bitmap* pBitmap = LoadBitmapN(String(pData));
203                                 r = GetLastResult();
204
205                                 if (!pBitmap || (r != E_SUCCESS))
206                                 {
207                                         SysLog(NID_UI, "It's failed to load a bitmap.");
208                                         free(pData);
209                                         continue;
210                                 }
211
212                                 pItem = _ClipboardItem::CreateInstanceN(type, *pBitmap);
213                                 if (!pItem)
214                                 {
215                                         SysLog(NID_UI, "It's failed to create a image item.");
216                                 }
217
218                                 delete pBitmap;
219                                 free(pData);
220                         }
221                         else
222                         {
223                                 pItem = _ClipboardItem::CreateInstanceN(type, String(pData));
224                                 if (!pItem)
225                                 {
226                                         SysLog(NID_UI, "It's failed to create a clipboard item.");
227                                 }
228
229                                 free(pData);
230                         }
231
232                         if (pItem)
233                         {
234                                 r = pList->Add(*pItem);
235                                 if (r != E_SUCCESS)
236                                 {
237                                         delete pItem;
238                                         SysLog(NID_UI, "It's failed to add a item into a list.");
239                                 }
240
241                         }
242                 }
243                 // [ToDo]
244                 /*
245                 else
246                 {
247                         free(pData);
248                 }
249                 */
250         }
251
252         SysTryCatch(NID_UI, pList->GetCount() > 0, , E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
253
254         SetLastResult(E_SUCCESS);
255
256         return pList;
257
258 CATCH:
259         delete pList;
260         return null;
261 }
262
263 const _ClipboardItem*
264 _Clipboard::RetrieveLatestItemN(unsigned long dataTypes)
265 {
266         SysTryReturn(NID_UI, IsValidDataType(dataTypes), null, E_INVALID_ARG,
267                                 "[E_INVALID_ARG] The data type is invalid.");
268
269         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
270         SysTryReturn(NID_UI, pEcoreEvas, null, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
271
272         int count = pEcoreEvas->GetClipCount();
273         SysTryReturn(NID_UI, count > 0, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
274
275         // Find the item with dataTypes.
276         int format = _CLIP_FORMAT_NONE;
277         char* pData = null;
278         ClipboardDataType type = CLIPBOARD_DATA_TYPE_NONE;
279         bool found = false;
280         for (int i = 0; i < count; i++)
281         {
282                 // Get the clip with index.
283                 bool ret = pEcoreEvas->RetrieveClipN(i, &format, &pData);
284
285                 if (ret == false)
286                 {
287                         SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
288                         continue;
289                 }
290
291                 //if (!pData) // prevent -> SLP CBHM
292                 //{
293                 //      SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
294                 //      continue;
295                 //}
296
297                 type = ConvertToDataType(format, pData);
298
299                 if (!IsValidDataType(type))
300                 {
301                         SysLog(NID_UI, "The type of a clip to be get form _EcoreEvas is invalid.");
302                         free(pData);
303                         continue;
304                 }
305
306                 // If the clip to be get from _EcoreEvas is in agreement with dataTypes.
307                 if (dataTypes & type)
308                 {
309                         found = true;
310                         break;
311                 }
312                 else
313                 {
314                         free(pData);
315                 }
316         }
317
318         SysTryReturn(NID_UI, found == true, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
319
320         SysLog(NID_UI, "[Clipboard] Clip : format = [%d], char = [%s] is retrieved.", format, pData);
321
322         result r = E_SUCCESS;
323         _ClipboardItem* pItem = null;
324         if (type == CLIPBOARD_DATA_TYPE_IMAGE)
325         {
326                 const Bitmap* pBitmap = LoadBitmapN(String(pData));
327                 r = GetLastResult();
328                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
329
330                 pItem = _ClipboardItem::CreateInstanceN(type, *pBitmap);
331                 r = GetLastResult();
332
333                 delete pBitmap;
334                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
335         }
336         else
337         {
338                 pItem = _ClipboardItem::CreateInstanceN(type, String(pData));
339                 r = GetLastResult();
340                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
341         }
342
343         free(pData);
344
345         SetLastResult(E_SUCCESS);
346
347         return pItem;
348
349 CATCH:
350         if (pItem)
351         {
352                 delete pItem;
353         }
354
355         free(pData);
356         return null;
357 }
358
359 result
360 _Clipboard::ShowPopup(unsigned long dataTypes, const IClipboardPopupEventListener &listener)
361 {
362         SysTryReturn(NID_UI, IsPopupVisible() == false, E_INVALID_OPERATION, E_INVALID_OPERATION,
363                                 "[E_INVALID_OPERATION] The popup is already shown.");
364
365         result r = E_SUCCESS;
366
367         if (__pPublicEvent == null)
368         {
369                 __pPublicEvent = _PublicClipboardPopupEvent::CreateInstanceN();
370                 r = GetLastResult();
371                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
372         }
373
374         r = __pPublicEvent->AddListener(listener);
375         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
376
377         unsigned long clipFormats = _CLIP_FORMAT_NONE;
378         if (CLIPBOARD_DATA_TYPE_IMAGE & dataTypes)
379         {
380                 clipFormats = _CLIP_FORMAT_IMAGE;
381         }
382
383         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
384
385         pEcoreEvas->OpenClipboard(clipFormats);
386
387         return r;
388 }
389
390 result
391 _Clipboard::HidePopup(void)
392 {
393         SysTryReturn(NID_UI, IsPopupVisible() == true, E_INVALID_OPERATION, E_INVALID_OPERATION,
394                                 "[E_INVALID_OPERATION] The popup is not shown.");
395
396         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
397         pEcoreEvas->CloseClipboard();
398
399         if (__pPublicEvent)
400         {
401                 delete __pPublicEvent;
402                 __pPublicEvent = null;
403         }
404
405         return E_SUCCESS;
406 }
407
408 bool
409 _Clipboard::IsPopupVisible(void)
410 {
411         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
412
413         bool opened = pEcoreEvas->IsClipboardOpened();
414
415         SetLastResult(E_SUCCESS);
416         return opened;
417 }
418
419 void
420 _Clipboard::FireEvent(int format, char* pData)
421 {
422         if (!__pPublicEvent)
423         {
424                 SysLog(NID_UI, "__pPublicEvent is null.");
425
426                 HidePopup();
427                 return;
428         }
429
430         result r = E_SUCCESS;
431         _ClipboardItem* pItemCore = null;
432         ClipboardItem* pItem = null;
433
434         if (pData)
435         {
436                 ClipboardDataType type = ConvertToDataType(format, pData);
437                 if (!IsValidDataType(type))
438                 {
439                         SysLog(NID_UI, "The type of a clip to be get form _EcoreEvas is invalid.");
440                         free(pData);
441
442                         HidePopup();
443                         return;
444                 }
445
446                 if (type == CLIPBOARD_DATA_TYPE_IMAGE)
447                 {
448                         const Bitmap* pBitmap = LoadBitmapN(String(pData));
449                         r = GetLastResult();
450
451                         if (!pBitmap || (r != E_SUCCESS))
452                         {
453                                 SysLog(NID_UI, "It's failed to load a bitmap.");
454
455                                 if (pBitmap)
456                                 {
457                                         delete pBitmap;
458                                 }
459                                 free(pData);
460
461                                 HidePopup();
462                                 return;
463                         }
464
465                         pItemCore = _ClipboardItem::CreateInstanceN(type, *pBitmap);
466                         if (!pItemCore)
467                         {
468                                 SysLog(NID_UI, "It's failed to create a image item.");
469                         }
470
471                         delete pBitmap;
472                         free(pData);
473                 }
474                 else
475                 {
476                         pItemCore = _ClipboardItem::CreateInstanceN(type, String(pData));
477                         if (!pItemCore)
478                         {
479                                 SysLog(NID_UI, "It's failed to create a clipboard item.");
480                         }
481
482                         free(pData);
483                 }
484
485                 if (!pItemCore)
486                 {
487                         SysLog(NID_UI, "pItemCore is null.");
488
489                         HidePopup();
490                         return;
491                 }
492
493                 pItem = new (std::nothrow) ClipboardItem;
494                 pItem->Construct(pItemCore->GetDataType(), *(pItemCore->GetData()));
495
496                 delete pItemCore;
497         }
498
499         IEventArg* pArg = _PublicClipboardPopupEvent::CreateClipboardPopupEventArgN(pItem);
500         r = GetLastResult();
501         SysTryReturnVoidResult(NID_UI, pArg, r, "[%s] Propagating.", GetErrorMessage(r));
502
503         __pPublicEvent->Fire(*pArg);
504         HidePopup();
505
506         SetLastResult(E_SUCCESS);
507 }
508
509 _ClipFormat
510 _Clipboard::ConvertToClipFormat(ClipboardDataType type)
511 {
512         _ClipFormat format = _CLIP_FORMAT_NONE;
513         switch (type)
514         {
515         case CLIPBOARD_DATA_TYPE_TEXT:
516                 // fall through
517         case CLIPBOARD_DATA_TYPE_VIDEO:
518                 // fall through
519         case CLIPBOARD_DATA_TYPE_AUDIO:
520                 format = _CLIP_FORMAT_TEXT;
521                 break;
522         case CLIPBOARD_DATA_TYPE_HTML:
523                 format = _CLIP_FORMAT_HTML;
524                 break;
525         case CLIPBOARD_DATA_TYPE_IMAGE:
526                 format = _CLIP_FORMAT_IMAGE;
527                 break;
528         default:
529                 break;
530         }
531
532         return format;
533 }
534
535 ClipboardDataType
536 _Clipboard::ConvertToDataType(int format, const char* pData)
537 {
538         ClipboardDataType type = CLIPBOARD_DATA_TYPE_NONE;
539         switch (format)
540         {
541         case _CLIP_FORMAT_TEXT:
542                 if (IsVideoDataType(pData))
543                 {
544                         type = CLIPBOARD_DATA_TYPE_VIDEO;
545                 }
546                 else if (IsAudioDataType(pData))
547                 {
548                         type = CLIPBOARD_DATA_TYPE_AUDIO;
549                 }
550                 else
551                 {
552                         type = CLIPBOARD_DATA_TYPE_TEXT;
553                 }
554                 break;
555         case _CLIP_FORMAT_HTML:
556                 type = CLIPBOARD_DATA_TYPE_HTML;
557                 break;
558         case _CLIP_FORMAT_IMAGE:
559                 type = CLIPBOARD_DATA_TYPE_IMAGE;
560                 break;
561         default:
562                 break;
563         }
564
565         return type;
566 }
567
568 char*
569 _Clipboard::EncodeToCharN(const String& string)
570 {
571         Utf8Encoding encoder;
572         ByteBuffer* pByteBuffer = encoder.GetBytesN(string);
573
574         result r = GetLastResult();
575         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
576
577         char* pChar = strdup((char*)pByteBuffer->GetPointer());
578         SysTryCatch(NID_UI, pChar, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
579
580         SetLastResult(E_SUCCESS);
581
582         // fall through
583
584 CATCH:
585         delete pByteBuffer;
586         return pChar;
587 }
588
589 char*
590 _Clipboard::CreateImageFileN(const Bitmap& bitmap)
591 {
592         // Obtain the buffer information from the bitmap.
593         Bitmap& tmpBitmap = const_cast<Bitmap&>(bitmap);
594         BufferInfo bufferInfo;
595
596         result r = tmpBitmap.Lock(bufferInfo);
597         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
598
599         // Create a source buffer.
600         ByteBuffer* pSrcBuf = new (std::nothrow) ByteBuffer;
601         SysTryReturn(NID_UI, pSrcBuf, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
602
603         int outLength = bufferInfo.width * bufferInfo.height * bufferInfo.bitsPerPixel / 8;
604
605         BitmapPixelFormat bmpPixelFormat = bitmap.GetPixelColorFormat();
606         MediaPixelFormat mediaPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
607         ImageFormat imageFormat = IMG_FORMAT_PNG;
608
609         ByteBuffer* pImageBuffer = null;
610         File file;
611         char* pChar = null;
612         String filePath;
613         bool existFile = false;
614
615         r = pSrcBuf->Construct(outLength);
616         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
617
618         r = pSrcBuf->SetArray((const byte*)bufferInfo.pPixels, 0, outLength);
619         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
620
621         pSrcBuf->Flip();
622
623         // Decide formats.
624         switch (bmpPixelFormat)
625         {
626         case BITMAP_PIXEL_FORMAT_RGB565:
627                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGB565LE;
628                 break;
629         case BITMAP_PIXEL_FORMAT_ARGB8888:
630                 // fall through
631         case BITMAP_PIXEL_FORMAT_R8G8B8A8:
632                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_BGRA8888;
633                 break;
634         default:
635                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] The invalid argument is given.");
636                 goto CATCH;
637         }
638
639         // Encode for getting a image buffer.
640         pImageBuffer = _ImageEncoder::EncodeToBufferN(*pSrcBuf, bufferInfo.width, bufferInfo.height, mediaPixelFormat, imageFormat);
641         r = GetLastResult();
642         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
643
644         delete pSrcBuf;
645         pSrcBuf = null;
646
647         // Create a image file.
648         filePath = CreateImageFilePath();
649         r = GetLastResult();
650         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
651
652         r = file.Construct(filePath, L"w+");
653         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
654
655         existFile = true;
656
657         r = file.Write(*pImageBuffer);
658         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
659
660         delete pImageBuffer;
661         pImageBuffer = null;
662
663         pChar = EncodeToCharN(filePath);
664         r = GetLastResult();
665         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
666
667         SetLastResult(E_SUCCESS);
668
669         return pChar;
670
671 CATCH:
672         if (pSrcBuf)
673         {
674                 delete pSrcBuf;
675         }
676
677         if (pImageBuffer)
678         {
679                 delete pImageBuffer;
680         }
681
682         if (existFile)
683         {
684                 File::Remove(filePath);
685         }
686
687         if (pChar)
688         {
689                 free(pChar);
690         }
691
692         return null;
693 }
694
695 String
696 _Clipboard::CreateImageFilePath(void)
697 {
698         result r = RemoveUnnecessaryImageFiles();
699         SysTryReturn(NID_UI, r == E_SUCCESS, String(""), r, "[%s] Propagating.", GetErrorMessage(r));
700
701         String filePath;
702         String extension(_CLIPBOARD_DEFAULT_EXTENSION);
703
704         bool find = false;
705         for (int i = 0; i < _CLIPBOARD_ITEM_MAX; i++)
706         {
707                 filePath = _CLIPBOARD_DEFAULT_PATH;
708
709                 r = filePath.Append(i);
710                 SysTryReturn(NID_UI, r == E_SUCCESS, String(""), r, "[%s] Propagating.", GetErrorMessage(r));
711
712                 r = filePath.Append(extension);
713                 SysTryReturn(NID_UI, r == E_SUCCESS, String(""), r, "[%s] Propagating.", GetErrorMessage(r));
714
715                 if (File::IsFileExist(filePath) == false)
716                 {
717                         find = true;
718                         break;
719                 }
720         }
721
722         SysTryReturn(NID_UI, find == true, String(""), E_SYSTEM, "[E_SYSTEM] A system error occurred.");
723
724         SetLastResult(E_SUCCESS);
725
726         return filePath;
727 }
728
729 result
730 _Clipboard::RemoveUnnecessaryImageFiles(void)
731 {
732         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
733         SysTryReturn(NID_UI, pEcoreEvas, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
734
735         result r = E_SUCCESS;
736
737         int clipCount = pEcoreEvas->GetClipCount();
738         if (clipCount < 1)
739         {
740                 r = RemoveAllImageFiles();
741                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
742
743                 return r;
744         }
745
746         ArrayList imageClipList;
747
748         r = imageClipList.Construct();
749         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
750
751         int format = _CLIP_FORMAT_NONE;
752         char* pData = null;
753         for (int i = 0; i < clipCount; i++)
754         {
755                 bool ret = pEcoreEvas->RetrieveClipN(i, &format, &pData);
756
757                 if (ret == false)
758                 {
759                         SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
760                         continue;
761                 }
762
763                 //if (!pData) // prevent -> SLP CBHM
764                 //{
765                 //      SysLog(NID_UI, "It's failed to get the clip from _EcoreEvas.");
766                 //      continue;
767                 //}
768
769                 if (format == _CLIP_FORMAT_IMAGE)
770                 {
771                         String temp(pData);
772                         int length = temp.GetLength();
773                         int defaultLength = _CLIPBOARD_DEFAULT_PATH.GetLength();
774
775                         if (length < defaultLength)
776                         {
777                                 free(pData);
778                                 continue;
779                         }
780
781                         temp.Remove(defaultLength, length - defaultLength);
782
783                         int compare = temp.CompareTo(_CLIPBOARD_DEFAULT_PATH);
784
785                         if (compare == 0)
786                         {
787                                 String* pString = new (std::nothrow) String(pData);
788                                 imageClipList.Add(*pString);
789                         }
790                 }
791
792                 free(pData);
793         }
794
795         String filePath;
796         IEnumerator* pEnumerator = null;
797
798         int imageClipCount = imageClipList.GetCount();
799
800         if (imageClipCount < 1)
801         {
802                 r = RemoveAllImageFiles();
803                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
804
805                 return r;
806         }
807         else if (imageClipCount == _CBHM_ITEM_MAX)
808         {
809                 String* plastImageClip = dynamic_cast<String*>(imageClipList.GetAt(_CBHM_ITEM_MAX - 1));
810                 SysTryCatch(NID_UI, plastImageClip, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
811
812                 if (File::IsFileExist(*plastImageClip))
813                 {
814                         r = File::Remove(*plastImageClip);
815                         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
816                 }
817         }
818
819         pEnumerator = imageClipList.GetEnumeratorN();
820         SysTryCatch(NID_UI, pEnumerator, , r, "[%s] Propagating.", GetErrorMessage(r));
821
822         for (int i = 0; i < _CLIPBOARD_ITEM_MAX; i++)
823         {
824                 filePath = _CLIPBOARD_DEFAULT_PATH;
825
826                 r = filePath.Append(i);
827                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
828
829                 r = filePath.Append(_CLIPBOARD_DEFAULT_EXTENSION);
830                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
831
832                 bool equal = false;
833                 while (pEnumerator->MoveNext() == E_SUCCESS)
834                 {
835                         String* pImageClip = dynamic_cast<String*>(pEnumerator->GetCurrent());
836                         SysTryCatch(NID_UI, pImageClip, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
837
838                         equal = filePath.Equals(*pImageClip);
839                         if (equal == true)
840                         {
841                                 break;
842                         }
843                 }
844
845                 r = pEnumerator->Reset();
846                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
847
848                 if (equal == false)
849                 {
850                         if (File::IsFileExist(filePath))
851                         {
852                                 r = File::Remove(filePath);
853                                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
854                         }
855                 }
856         }
857
858         // fall through
859
860 CATCH:
861         imageClipList.RemoveAll(true);
862
863         if (pEnumerator)
864         {
865                 delete pEnumerator;
866         }
867
868         return r;
869 }
870
871 result
872 _Clipboard::RemoveAllImageFiles(void)
873 {
874         String filePath;
875         result r = E_SUCCESS;
876
877         for (int i = 0; i < _CLIPBOARD_ITEM_MAX; i++)
878         {
879                 filePath = _CLIPBOARD_DEFAULT_PATH;
880
881                 r = filePath.Append(i);
882                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
883
884                 r = filePath.Append(_CLIPBOARD_DEFAULT_EXTENSION);
885                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
886
887                 if (File::IsFileExist(filePath))
888                 {
889                         r = File::Remove(filePath);
890                         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
891                 }
892         }
893
894         return r;
895 }
896
897 Bitmap*
898 _Clipboard::LoadBitmapN(const String& path)
899 {
900         result r = E_SUCCESS;
901
902         String* pStr = const_cast<String*>(&path);
903
904         int index = 0;
905         r = pStr->IndexOf(L"file://", 0, index);
906
907         if (r != E_OBJ_NOT_FOUND)
908         {
909                 pStr->Remove(0, 7);
910         }
911
912         bool hasAlpha = _ImageUtil::HasAlphaChannel(*pStr);
913
914         r = GetLastResult();
915         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
916
917         BitmapPixelFormat bmpPixelFormat = BITMAP_PIXEL_FORMAT_RGB565;
918         MediaPixelFormat mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGB565LE;
919
920         if (hasAlpha == true)
921         {
922                 bmpPixelFormat = BITMAP_PIXEL_FORMAT_ARGB8888;
923                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_BGRA8888;
924         }
925
926         int bmpWidth = 0;
927         int bmpHeight = 0;
928
929         // Decode.
930         ByteBuffer* pBuffer = _ImageDecoder::DecodeToBufferN(*pStr, mediaPixelFormat, bmpWidth, bmpHeight);
931         r = GetLastResult();
932         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
933
934         // Create a bitmap.
935         Bitmap* pBitmap = _BitmapImpl::GetNonScaledBitmapN(*pBuffer, Dimension(bmpWidth, bmpHeight), bmpPixelFormat);
936         r = GetLastResult();
937         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
938
939         delete pBuffer;
940
941         SetLastResult(E_SUCCESS);
942
943         return pBitmap;
944
945 CATCH:
946         delete pBuffer;
947
948         if (pBitmap)
949         {
950                 delete pBitmap;
951         }
952
953         return null;
954 }
955
956 bool
957 _Clipboard::IsValidDataType(unsigned long dataTypes)
958 {
959         return ((CLIPBOARD_DATA_TYPE_TEXT & dataTypes) ||
960                 (CLIPBOARD_DATA_TYPE_HTML & dataTypes) ||
961                 (CLIPBOARD_DATA_TYPE_IMAGE & dataTypes) ||
962                 (CLIPBOARD_DATA_TYPE_VIDEO & dataTypes) ||
963                 (CLIPBOARD_DATA_TYPE_AUDIO & dataTypes));
964 }
965
966 bool
967 _Clipboard::IsVideoDataType(const char* pChar)
968 {
969         // [ToDo]
970         return (strstr(pChar, ".mp4") != null);
971 }
972
973 bool
974 _Clipboard::IsAudioDataType(const char* pChar)
975 {
976         // [ToDo]
977         return (strstr(pChar, ".mp3") != null);
978 }
979
980 _Clipboard::_Clipboard(void)
981         : __pPublicEvent(null)
982 {
983 }
984
985 }} // Tizen::Ui