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