Apply fallback for icon
[platform/framework/native/appfw.git] / src / app / package / FAppPkg_PackageParser.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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  * @file        FAppPkg_PackageParser.cpp
18  * @brief       This is the implementation for the _PackageParser class.
19  */
20
21 #include <libxml/parserInternals.h>
22 #include <system_info.h>
23
24 #include <FAppPkgPackageAppInfo.h>
25 #include <FBaseSysLog.h>
26 #include <FIoFile.h>
27 #include <FIoDirectory.h>
28 #include <FAppPkg_PackageAppInfoImpl.h>
29 #include <FAppPkg_PackageInfoImpl.h>
30 #include <FAppPkg_PackageParser.h>
31 #include <FBase_StringConverter.h>
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Utility;
36 using namespace Tizen::App;
37 using namespace Tizen::Io;
38 using namespace Tizen::System;
39
40 namespace Tizen { namespace App { namespace Package
41 {
42
43 _PackageXmlAttribute::_PackageXmlAttribute(void)
44 :__pName(null)
45 ,__pValue(null)
46 ,__pNext(null)
47 {
48 }
49
50 _PackageXmlAttribute::~_PackageXmlAttribute(void)
51 {
52         delete[] __pName;
53         delete[] __pValue;
54         delete __pNext;
55 }
56
57 bool
58 _PackageXmlAttribute::Construct(const char* pName, const char* pValue)
59 {
60         if (pName == 0 || pValue == 0)
61         {
62                 return true;
63         }
64
65         __pName = new (std::nothrow) char[strlen(pName)+1];
66         SysTryReturn(NID_APP, __pName, false, E_OUT_OF_MEMORY, "__pName is null");
67         strcpy(__pName, pName);
68
69         __pValue = new (std::nothrow) char[strlen(pValue)+1];
70         SysTryReturn(NID_APP, __pValue, false, E_OUT_OF_MEMORY, "__pValue is null");
71         strcpy(__pValue, pValue);
72
73         return true;
74 }
75
76 char*
77 _PackageXmlAttribute::Find(const char* pName)
78 {
79         if (pName == 0)
80         {
81                 return null;
82         }
83
84         if (__pName == 0 || __pValue == 0)
85         {
86                 return null;
87         }
88
89         if (strcasecmp(pName, __pName) == 0)
90         {
91                 return __pValue;
92         }
93
94         if (__pNext)
95         {
96                 return __pNext->Find(pName);
97         }
98
99         return null;
100 }
101
102 bool
103 _PackageXmlAttribute::Add(const char* pName, const char* pValue)
104 {
105         if (pName == 0 || pValue == 0)
106         {
107                 return true;
108         }
109
110         if (__pNext)
111         {
112                 _PackageXmlAttribute* pNext = __pNext;
113                 while (pNext->__pNext)
114                 {
115                         pNext = pNext->__pNext;
116                 }
117
118                 return pNext->Add(pName, pValue);
119         }
120         else
121         {
122                 __pNext = new (std::nothrow) _PackageXmlAttribute();
123                 SysTryReturn(NID_APP, __pNext, false, E_OUT_OF_MEMORY, "__pNext is null");
124
125                 __pNext->Construct(pName, pValue);
126         }
127
128         return true;
129 }
130
131
132 _PackageXmlHandler::_PackageXmlHandler(void)
133 :__pAttr(null)
134 ,__pElementName(null)
135 ,__pCharacters(null)
136 ,__error(false)
137 {
138 }
139
140 _PackageXmlHandler::~_PackageXmlHandler(void)
141 {
142         delete __pAttr;
143         __pAttr = null;
144
145         delete[] __pElementName;
146         __pElementName = null;
147 }
148
149 bool
150 _PackageXmlHandler::OnStartDocument(void)
151 {
152         return true;
153 }
154
155 bool
156 _PackageXmlHandler::OnEndDocument(void)
157 {
158         return true;
159 }
160
161 bool
162 _PackageXmlHandler::OnStartElement(const char* pName)
163 {
164         return true;
165 }
166
167 bool
168 _PackageXmlHandler::OnEndElement(const char* pName)
169 {
170         return true;
171 }
172
173 bool
174 _PackageXmlHandler::OnCharacters(const char* pCharacters)
175 {
176         return true;
177 }
178
179 void
180 _PackageXmlHandler::StartElement(void* pCtx, const xmlChar* pName, const xmlChar** ppAtts)
181 {
182         if (pName == 0)
183         {
184                 return;
185         }
186
187         _PackageXmlHandler* pHandler = null;
188         pHandler = (_PackageXmlHandler*) pCtx;
189         bool xmlResult = false;
190
191         pHandler->SetElementName((const char *) pName);
192
193         if (ppAtts)
194         {
195                 _PackageXmlAttribute* pAttr = 0;
196
197                 if (ppAtts[0] != null && ppAtts[1] != null)
198                 {
199                         pAttr = new (std::nothrow) _PackageXmlAttribute();
200                         SysTryReturnVoidResult(NID_APP, pAttr, E_OUT_OF_MEMORY, "pAttr is null.");
201
202                         pAttr->Construct((const char *)ppAtts[0], (const char *)ppAtts[1]);
203                         ppAtts = &ppAtts[2];
204                 }
205
206                 while (ppAtts != null && ppAtts[0] != null && ppAtts[1] != null)
207                 {
208                         pAttr->Add((const char *)ppAtts[0], (const char *)ppAtts[1]);
209                         ppAtts = &ppAtts[2];
210                 }
211
212                 pHandler->SetAttribute(pAttr);
213         }
214
215         xmlResult = pHandler->OnStartElement((const char *)pName);
216         if (xmlResult == false)
217         {
218                 pHandler->SetError();
219         }
220 }
221
222 void
223 _PackageXmlHandler::EndElement(void* pCtx, const xmlChar* pName)
224 {
225         _PackageXmlHandler* pHandler = null;
226         pHandler = (_PackageXmlHandler*) pCtx;
227         bool xmlResult = false;
228         char* pCharacters = pHandler->GetCharacters();
229
230         if (pCharacters && (strlen(pCharacters) > 0))
231         {
232                 xmlResult = pHandler->OnCharacters(pCharacters);
233                 if (xmlResult == false)
234                 {
235                         pHandler->SetError();
236                 }
237         }
238
239         xmlResult = pHandler->OnEndElement((const char *)pName);
240         if (xmlResult == false)
241         {
242                 pHandler->SetError();
243         }
244
245         pHandler->DeleteElement();
246         pHandler->DeleteAttribute();
247         pHandler->DeleteCharacters();
248 }
249
250 void
251 _PackageXmlHandler::Characters(void* pCtx, const xmlChar* pCh, int len)
252 {
253         SysTryReturnVoidResult(NID_APP, pCh, E_SYSTEM, "pCh is null.");
254
255         _PackageXmlHandler* pHandler = null;
256         pHandler = (_PackageXmlHandler*) pCtx;
257         char* pCharacters = null;
258
259         if (pCh[0] == 0x20 || pCh[0] == 0x09 || pCh[0] == 0x0D || pCh[0] == 0x0A)
260         {
261                 return;
262         }
263
264         pCharacters = new (std::nothrow) char[len+1];
265         SysTryReturnVoidResult(NID_APP, pCharacters, E_OUT_OF_MEMORY, "pCharacters is null.");
266
267         strncpy(pCharacters, (const char *)pCh, len);
268         pCharacters[len] = 0;
269
270         pHandler->SetCharacters(pCharacters);
271
272         delete[] pCharacters;
273 }
274
275 bool
276 _PackageXmlHandler::ParseNormalizedDocument(const char* pFilepath)
277 {
278         xmlSAXHandler* pSAXHandler = null;
279         xmlParserCtxtPtr ctxt = 0;
280         bool ret = true;
281         File file;
282         FileAttributes attr;
283         result r = E_SUCCESS;
284         char* pBuf = null;
285         char* pNormalizedBuf = null;
286         int size = 0;
287         int normalizedSize = 0;
288         int readSize = 0;
289
290         r = file.Construct(pFilepath, L"r");
291         TryCatch(r == E_SUCCESS, ret = false, "file.Construct is failed. [%s]", pFilepath);
292
293         r = file.GetAttributes(pFilepath, attr);
294         TryCatch(IsFailed(r) == false, ret = false, "file.GetAttributes is failed. [%s]", pFilepath);
295
296         size = (int)attr.GetFileSize();
297         TryCatch(size > 0, ret = false, "size is invalid. [%s]", pFilepath);
298
299         pBuf = new (std::nothrow) char[size+1];
300         TryCatch(pBuf, ret = false, "pBuf is null");
301
302         pNormalizedBuf = new (std::nothrow) char[size+1];
303         TryCatch(pNormalizedBuf, ret = false, "pNormalizedBuf is null");
304
305         memset(pBuf, 0, size+1);
306         memset(pNormalizedBuf, 0, size+1);
307
308         readSize = file.Read(pBuf, size);
309         TryCatch(readSize > 0, ret = false, "file.Read is failed. [%s][%d]", pFilepath, readSize);
310
311         normalizedSize = Normalize(pBuf, size, pNormalizedBuf);
312         TryCatch(normalizedSize > 0, ret = false, "normalizedSize [%d]", readSize);
313
314         ctxt = xmlCreateMemoryParserCtxt(pNormalizedBuf, normalizedSize);
315         TryCatch(ctxt, ret = false, "invalid xml file, %s", pFilepath);
316
317         pSAXHandler = new (std::nothrow) xmlSAXHandler;
318         TryCatch(pSAXHandler, ret = false, "pSAXHandler is null");
319         memset(pSAXHandler, 0, sizeof(xmlSAXHandler));
320
321         ctxt->userData = (void *)this;
322
323         pSAXHandler->startElement = _PackageXmlHandler::StartElement;
324         pSAXHandler->endElement = _PackageXmlHandler::EndElement;
325         pSAXHandler->characters = _PackageXmlHandler::Characters;
326
327         ctxt->sax = pSAXHandler;
328
329         xmlParseDocument(ctxt);
330         xmlFreeParserCtxt(ctxt);
331
332         TryCatch(GetError() != true, ret = false, "xml parsing error is occurred.");
333
334 CATCH:
335         delete[] pBuf;
336         delete[] pNormalizedBuf;
337
338         return ret;
339 }
340
341 int
342 _PackageXmlHandler::Normalize(const char* pBuf, int size, char* pNormalizedBuf)
343 {
344         int idx = 0;
345         int normalizedIdx = 0;
346
347         while (pBuf[idx] && idx < size)
348         {
349                 if (pBuf[idx] == 0x0D)
350                 {
351                         if (pBuf[idx + 1] == 0x0A)
352                         {
353                                 idx++;
354                         }
355                         pNormalizedBuf[normalizedIdx] = 0x0A;
356                         normalizedIdx++;
357                 }
358                 else if((pBuf[idx] == 0X0A) && (pBuf[idx + 1] == 0x4d) && (pBuf[idx - 1] == 0x3E))
359                 {
360                         idx++;
361                         pNormalizedBuf[normalizedIdx] = pBuf[idx];
362                         normalizedIdx++;
363                 }
364                 else
365                 {
366                         pNormalizedBuf[normalizedIdx] = pBuf[idx];
367                         normalizedIdx++;
368                 }
369
370                 idx++;
371         }
372
373         return normalizedIdx;
374 }
375
376 bool
377 _PackageXmlHandler::SetElementName(const char* pElementName)
378 {
379         SysTryReturn(NID_APP, pElementName, false, E_SYSTEM, "pElementName is null");
380
381         if (__pElementName)
382         {
383                 delete[] __pElementName;
384                 __pElementName = null;
385         }
386
387         __pElementName = new (std::nothrow) char[strlen(pElementName)+1];
388         SysTryReturn(NID_APP, __pElementName, false, E_OUT_OF_MEMORY, "__pElementName is null");
389         strcpy(__pElementName, pElementName);
390
391         __elementStack.Push(*new (std::nothrow) String(pElementName));
392
393         return true;
394 }
395
396 char*
397 _PackageXmlHandler::GetElementName(void)
398 {
399         return __pElementName;
400 }
401
402 void
403 _PackageXmlHandler::DeleteElement(void)
404 {
405         delete[]__pElementName;
406         __pElementName = 0;
407
408         __elementStack.Pop();
409 }
410
411 bool
412 _PackageXmlHandler::SetCharacters(const char* pCharacter)
413 {
414         SysTryReturn(NID_APP, pCharacter, false, E_SYSTEM, "pCharacter is null");
415
416         if (__pCharacters == null)
417         {
418                 __pCharacters = new (std::nothrow) char[4096];
419                 SysTryReturn(NID_APP, __pCharacters, false, E_OUT_OF_MEMORY, "__pCharacters is null");
420
421                 memset(__pCharacters, 0, 4096);
422         }
423
424         strncat(__pCharacters, pCharacter, strlen(pCharacter));
425
426         return true;
427 }
428
429 char*
430 _PackageXmlHandler::GetCharacters(void)
431 {
432         return __pCharacters;
433 }
434
435 void
436 _PackageXmlHandler::DeleteCharacters(void)
437 {
438         delete[] __pCharacters;
439         __pCharacters = null;
440 }
441
442 void
443 _PackageXmlHandler::SetAttribute(_PackageXmlAttribute* pAttr)
444 {
445         __pAttr = pAttr;
446 }
447
448 _PackageXmlAttribute*
449 _PackageXmlHandler::GetAttribute(void)
450 {
451         return __pAttr;
452 }
453
454 void
455 _PackageXmlHandler::DeleteAttribute(void)
456 {
457         delete __pAttr;
458         __pAttr = null;
459 }
460
461 IEnumerator*
462 _PackageXmlHandler::GetElementEnumeratorN(void)
463 {
464         return __elementStack.GetEnumeratorN();
465 }
466
467 int
468 _PackageXmlHandler::GetElementCount(void)
469 {
470         return __elementStack.GetCount();
471 }
472
473 void
474 _PackageXmlHandler::SetError(void)
475 {
476         __error = true;
477 }
478
479 bool
480 _PackageXmlHandler::GetError(void)
481 {
482         return __error;
483 }
484
485
486 _PackageParser::_PackageParser(void)
487 :__pPackageInfoImpl(null)
488 ,__pAppInfo(null)
489 ,__pDefaultIconType(null)
490 ,__isDefaultName(false)
491 ,__isEflTpk(false)
492 {
493 }
494
495 _PackageParser::~_PackageParser(void)
496 {
497         delete[] __pDefaultIconType;
498         __pDefaultIconType = null;
499 }
500
501 bool
502 _PackageParser::Construct(PackageInfo* pPackageInfo)
503 {
504         SysTryReturn(NID_APP, pPackageInfo, false, E_INVALID_ARG, "pPackageInfo is null.");
505
506         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
507         SysTryReturn(NID_APP, pPackageInfoImpl, false, E_SYSTEM, "pPackageInfoImpl is null.");
508
509         __pPackageInfoImpl = pPackageInfoImpl;
510         return true;
511 }
512
513 bool
514 _PackageParser::Parse(const String& packagepath)
515 {
516         __packagePath = packagepath;
517
518         bool res = true;
519         FileUnzipper unzipper;
520
521         Directory::Remove("/tmp/__manifest/", true);
522         Directory::Create("/tmp/__manifest/", false);
523
524         result r = unzipper.Construct(packagepath);
525         SysTryReturn(NID_APP, !IsFailed(r), false, E_SYSTEM, "unzipper.Construct() failed.");
526
527         r = unzipper.UnzipTo(L"/tmp/__manifest", L"info/manifest.xml");
528         SysTryReturn(NID_APP, !IsFailed(r), false, E_SYSTEM, "unzipper.UnzipTo() failed.");
529
530         res = ParseNormalizedDocument("/tmp/__manifest/info/manifest.xml");
531
532         Directory::Remove("/tmp/__manifest/", true);
533
534         return res;
535 }
536
537 bool
538 _PackageParser::OnStartElement(const char* pName)
539 {
540         SysTryReturn(NID_APP, pName, true, E_SYSTEM, "pName is null.");
541
542         bool status = true;
543
544         if (strcasecmp(pName, "UiApp") == 0)
545         {
546                 status = OnUiAppStartElement();
547         }
548         else if (strcasecmp(pName, "ServiceApp") == 0)
549         {
550                 status = OnServiceAppStartElement();
551         }
552         else if (strcasecmp(pName, "Icons") == 0)
553         {
554                 status = OnIconsStartElement();
555         }
556
557         if (!status)
558         {
559                 return false;
560         }
561
562         return true;
563 }
564
565 bool
566 _PackageParser::OnEndElement(const char* pName)
567 {
568         SysTryReturn(NID_APP, pName, true, E_SYSTEM, "pName is null.");
569
570         bool status = true;
571
572         if (strcasecmp(pName, "UiApp") == 0)
573         {
574                 status = OnUiAppEndElement();
575         }
576         else if (strcasecmp(pName, "ServiceApp") == 0)
577         {
578                 status = OnServiceAppEndElement();
579         }
580         else if (strcasecmp(pName, "DisplayNames") == 0)
581         {
582                 status = OnDisplayNamesEndElement();
583         }
584
585         if (!status)
586         {
587                 return false;
588         }
589
590         return true;
591 }
592
593 bool
594 _PackageParser::OnCharacters(const char* pCharacters)
595 {
596         bool status = true;
597
598         char* pName = GetElementName();
599         SysTryReturn(NID_APP, pName, false, E_SYSTEM, "pName is null.");
600
601         if (strcasecmp(pName, "Id") == 0)
602         {
603                 status = OnIdValue(pCharacters);
604         }
605         else if (strcasecmp(pName, "Version") == 0)
606         {
607                 status = OnVersionValue(pCharacters);
608         }
609         else if (strcasecmp(pName, "Url") == 0)
610         {
611                 status = OnUrlValue(pCharacters);
612         }
613         else if (strcasecmp(pName, "Privilege") == 0)
614         {
615                 status = OnPrivilegeValue(pCharacters);
616         }
617         else if (strcasecmp(pName, "DisplayName") == 0)
618         {
619                 status = OnDisplayNameValue(pCharacters);
620         }
621         else if (strcasecmp(pName, "Author") == 0)
622         {
623                 status = OnAuthorValue(pCharacters);
624         }
625         else if (strcasecmp(pName, "Description") == 0)
626         {
627                 status = OnDescriptionValue(pCharacters);
628         }
629         else if (strcasecmp(pName, "Icon") == 0)
630         {
631                 status = OnIconValue(pCharacters);
632         }
633         else if (strcasecmp(pName, "Category") == 0)
634         {
635                 status = OnCategoryValue(pCharacters);
636         }
637
638         if (!status)
639         {
640                 return false;
641         }
642
643         return true;
644 }
645
646 bool
647 _PackageParser::OnUiAppStartElement(void)
648 {
649         __pAppInfo = new (std::nothrow) PackageAppInfo;
650         SysTryReturn(NID_APP, __pAppInfo, false, E_OUT_OF_MEMORY, "__pAppInfo is null.");
651
652         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
653         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
654
655         _PackageXmlAttribute* pAttr = GetAttribute();
656         SysTryReturn(NID_APP, pAttr, true, E_SYSTEM, "pAttr is null");
657
658         ParseAppAttribute(pAttr, true);
659
660         return true;
661 }
662
663 bool
664 _PackageParser::OnServiceAppStartElement(void)
665 {
666         __pAppInfo = new (std::nothrow) PackageAppInfo;
667         SysTryReturn(NID_APP, __pAppInfo, false, E_OUT_OF_MEMORY, "__pAppInfo is null.");
668
669         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
670         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
671
672         _PackageXmlAttribute* pAttr = GetAttribute();
673         SysTryReturn(NID_APP, pAttr, true, E_SYSTEM, "pAttr is null");
674
675         ParseAppAttribute(pAttr, false);
676
677         return true;
678 }
679
680 bool
681 _PackageParser::OnIconsStartElement(void)
682 {
683         int res = 0;
684         int width = 0;
685         String defaultIconType;
686
687         res = system_info_get_value_int(SYSTEM_INFO_KEY_SCREEN_WIDTH, &width);
688         if (res != SYSTEM_INFO_ERROR_NONE)
689         {
690                 SysLog(NID_APP, "system_info_get_value_int(SYSTEM_INFO_KEY_SCREEN_WIDTH) failed. res = [%d]", res);
691                 defaultIconType = L"Xhigh";
692         }
693         else
694         {
695                 if (width == 480)
696                 {
697                         defaultIconType = L"High";
698                 }
699                 else
700                 {
701                         defaultIconType = L"Xhigh";
702                 }
703         }
704
705         __pDefaultIconType = _StringConverter::CopyToCharArrayN(defaultIconType);
706         SysTryReturn(NID_APP, __pDefaultIconType, false, E_OUT_OF_MEMORY, "__pDefaultIconType is null.");
707
708         SysLog(NID_APP, "ScreenWidth = [%d], DefaultIconType = [%s]", width, __pDefaultIconType);
709
710         return true;
711 }
712
713 bool
714 _PackageParser::OnUiAppEndElement(void)
715 {
716         __pPackageInfoImpl->AddPackageAppInfo(*__pAppInfo);
717         __pAppInfo = null;
718
719         __isDefaultName = false;
720
721         return true;
722 }
723
724 bool
725 _PackageParser::OnServiceAppEndElement(void)
726 {
727         __pPackageInfoImpl->AddPackageAppInfo(*__pAppInfo);
728         __pAppInfo = null;
729
730         __isDefaultName = false;
731
732         return true;
733 }
734
735 bool
736 _PackageParser::OnDisplayNamesEndElement(void)
737 {
738         // fall-back
739
740         return true;
741 }
742
743 bool
744 _PackageParser::OnIdValue(const char* pCharacters)
745 {
746         SysLog(NID_APP, "Id = [%s]", pCharacters);
747         __pPackageInfoImpl->SetId(pCharacters);
748
749         RegularExpression regularExpression;
750         String pattern(L"([a-zA-Z0-9]{10})");
751         result r = regularExpression.Construct(pattern);
752         SysTryReturn(NID_APP, !IsFailed(r), false, E_SYSTEM, "regularExpression.Construct() failed.");
753
754         PackageId packageId = pCharacters;
755         if (regularExpression.Match(packageId, true) == true)
756         {
757                 SysLog(NID_APP, "[%ls] is tizen native application.", packageId.GetPointer());
758         }
759         else
760         {
761                 SysLog(NID_APP, "[%ls] is not tizen native application.", packageId.GetPointer());
762                 __isEflTpk = true;
763         }
764
765         return true;
766 }
767
768 bool
769 _PackageParser::OnVersionValue(const char* pCharacters)
770 {
771         SysLog(NID_APP, "Version = [%s]", pCharacters);
772         __pPackageInfoImpl->SetVersion(pCharacters);
773
774         return true;
775 }
776
777 bool
778 _PackageParser::OnAuthorValue(const char* pCharacters)
779 {
780         SysLog(NID_APP, "Author = [%s]", pCharacters);
781         __pPackageInfoImpl->SetAuthor(pCharacters);
782
783         return true;
784 }
785
786 bool
787 _PackageParser::OnUrlValue(const char* pCharacters)
788 {
789         SysLog(NID_APP, "Url = [%s]", pCharacters);
790         __pPackageInfoImpl->SetUrl(pCharacters);
791
792         return true;
793 }
794
795 bool
796 _PackageParser::OnPrivilegeValue(const char* pCharacters)
797 {
798         SysLog(NID_APP, "Privilege = [%s]", pCharacters);
799         __pPackageInfoImpl->AddPrivilege(*new (std::nothrow) String(pCharacters));
800
801         return true;
802 }
803
804 bool
805 _PackageParser::OnIconValue(const char* pCharacters)
806 {
807         SysTryReturn(NID_APP, __pDefaultIconType, false, E_SYSTEM, "__pDefaultIconType is null");
808
809         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
810         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
811
812         _PackageXmlAttribute* pAttr = GetAttribute();
813         SysTryReturn(NID_APP, pAttr, true, E_SYSTEM, "pAttr is null");
814
815         char* pSection = pAttr->Find("Section");
816         SysTryReturn(NID_APP, pSection, true, E_SYSTEM, "pSection is null");
817
818         String iconRelPath;
819         String tempIconPath;
820
821         if (strcasecmp(__pDefaultIconType, "Xhigh") == 0)
822         {
823                 iconRelPath.Format(1024, L"shared/res/screen-density-xhigh/%s", pCharacters);
824         }
825         else if (strcasecmp(__pDefaultIconType, "High") == 0)
826         {
827                 iconRelPath.Format(1024, L"shared/res/screen-density-high/%s", pCharacters);
828         }
829         else
830         {
831                 SysTryReturn(NID_APP, 0, false, E_SYSTEM, "Invalid __pDefaultIconType [%s]", __pDefaultIconType);
832         }
833
834         if (strcasecmp(pSection, "MainMenu") == 0)
835         {
836                 FileUnzipper unzipper;
837                 String tmp(L"/tmp/__icon/");
838                 Directory::Remove(tmp, true);
839                 Directory::Create(tmp, false);
840
841                 result r = unzipper.Construct(__packagePath);
842                 SysTryReturn(NID_APP, !IsFailed(r), false, E_SYSTEM, "unzipper.Construct() failed.");
843
844                 ZipEntry entry;
845                 r = unzipper.GetEntry(iconRelPath, entry);
846                 if (IsFailed(r))
847                 {
848                         AppLog("fallback, old path = [%ls]", iconRelPath.GetPointer());
849
850                         String densityXhigh("screen-density-xhigh/");
851                         String densityHigh("screen-density-high/");
852
853                         if (iconRelPath.Contains(densityXhigh) == true)
854                         {
855                                 iconRelPath.Replace(densityXhigh, densityHigh);
856                         }
857                         else if (iconRelPath.Contains(densityHigh) == true)
858                         {
859                                 iconRelPath.Replace(densityHigh, densityXhigh);
860                         }
861                         else
862                         {
863                                 AppLog("invalid icon [%ls]", iconRelPath.GetPointer());
864                                 return false;
865                         }
866
867                         AppLog("fallback, new path = [%ls]", iconRelPath.GetPointer());
868                 }
869
870                 r = unzipper.UnzipTo(tmp, iconRelPath);
871                 if (!IsFailed(r))
872                 {
873                         result r = E_SUCCESS;
874                         File file;
875                         tempIconPath = tmp + iconRelPath;
876
877                         r = file.Construct(tempIconPath, "r");
878                         if (r == E_SUCCESS)
879                         {
880                                 FileAttributes attr;
881                                 file.GetAttributes(tempIconPath, attr);
882                                 int size = (int)attr.GetFileSize();
883
884                                 char* pBuffer = new (std::nothrow) char[size];
885                                 SysTryReturn(NID_APP, pBuffer, false, E_OUT_OF_MEMORY, "pBuffer is null.");
886
887                                 memset(pBuffer, 0, size);
888                                 file.Read((void*)pBuffer, size);
889
890                                 pPackageAppInfoImpl->SetAppMenuIconBuffer(pBuffer, size);
891
892                                 Directory::Remove(tmp, true);
893                                 delete pBuffer;
894                         }
895                         else
896                         {
897                                 SysLog(NID_APP, "file.Construct(%ls) failed.", tempIconPath.GetPointer());
898                         }
899                 }
900                 else
901                 {
902                         SysLog(NID_APP, "UnzipTo(%ls) is failed.", iconRelPath.GetPointer());
903                 }
904         }
905
906         SysLog(NID_APP, "Section = [%s], Icon = [%ls], TempIcon = [%ls]", pSection, iconRelPath.GetPointer(), tempIconPath.GetPointer());
907
908         return true;
909 }
910
911 bool
912 _PackageParser::OnDisplayNameValue(const char* pCharacters)
913 {
914         _PackageXmlAttribute* pAttr = 0;
915         char* pAttrValue = 0;
916
917         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
918         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
919
920         pAttr = GetAttribute();
921         SysTryReturn(NID_APP, pAttr, true, E_SYSTEM, "pAttr is null");
922
923         pAttrValue = pAttr->Find("Locale");
924         SysTryReturn(NID_APP, pAttrValue, true, E_SYSTEM, "pAttrValue is null");
925
926         if (strcasecmp(pAttrValue, "eng-GB") == 0 || strcasecmp(pAttrValue, "eng-US") == 0)
927         {
928                 pPackageAppInfoImpl->SetAppDisplayName(pCharacters);
929
930                 if (__isDefaultName == true)
931                 {
932                         __pPackageInfoImpl->SetDisplayName(pCharacters);
933                 }
934         }
935
936         String* pValue = new (std::nothrow) String;
937         StringUtil::Utf8ToString(pCharacters, *pValue);
938
939         pPackageAppInfoImpl->AddName(*(new (std::nothrow) String(pAttrValue)), *pValue);
940
941         SysLog(NID_APP, "DisplayName Locale = [%s][%s]", pAttrValue, pCharacters);
942
943         return true;
944 }
945
946 bool
947 _PackageParser::OnDescriptionValue(const char* pCharacters)
948 {
949         _PackageXmlAttribute *pAttr = 0;
950         char *pAttrValue = 0;
951
952         pAttr = GetAttribute();
953         SysTryReturn(NID_APP, pAttr, true, E_SYSTEM, "pAttr is null");
954
955         pAttrValue = pAttr->Find("Locale");
956         SysTryReturn(NID_APP, pAttrValue, true, E_SYSTEM, "pAttrValue is null");
957
958         if (strcasecmp(pAttrValue, "eng-GB") == 0 || strcasecmp(pAttrValue, "eng-US") == 0)
959         {
960                 __pPackageInfoImpl->SetDescription(pCharacters);
961         }
962
963         SysLog(NID_APP, "Description Locale =[%s][%s]", pAttrValue, pCharacters);
964
965         return true;
966 }
967
968 bool
969 _PackageParser::OnCategoryValue(const char* pCharacters)
970 {
971         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
972         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
973
974         pPackageAppInfoImpl->AddCategory(new (std::nothrow) String(pCharacters));
975
976         SysLog(NID_APP, "Category = [%s]", pCharacters);
977
978         return true;
979 }
980
981 bool
982 _PackageParser::ParseAppAttribute(_PackageXmlAttribute* pAttr, bool isUiApp)
983 {
984         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(__pAppInfo);
985         SysTryReturn(NID_APP, pPackageAppInfoImpl, false, E_SYSTEM, "pPackageAppInfoImpl is null.");
986
987         char* pName = pAttr->Find("Name");
988         SysTryReturn(NID_APP, pName, true, E_SYSTEM, "pName is null");
989
990         SysLog(NID_APP, "Name = [%s]", pName);
991         pPackageAppInfoImpl->SetAppName(pName);
992
993         AppId appId = __pPackageInfoImpl->GetId() + L"." + pName;
994         if (__isEflTpk == false)
995         {
996                 pPackageAppInfoImpl->SetAppId(appId);
997         }
998         else
999         {
1000                 pPackageAppInfoImpl->SetAppId(__pPackageInfoImpl->GetId());
1001         }
1002
1003         char* pMain = pAttr->Find("Main");
1004         if (pMain)
1005         {
1006                 SysLog(NID_APP, "Main = [%s]", pMain);
1007                 if (strcasecmp(pMain, "True") == 0)
1008                 {
1009                         __isDefaultName = true;
1010                         if (__isEflTpk == false)
1011                         {
1012                                 __pPackageInfoImpl->SetMainAppId(appId);
1013                         }
1014                         else
1015                         {
1016                                 __pPackageInfoImpl->SetMainAppId(__pPackageInfoImpl->GetId());
1017                         }
1018                         pPackageAppInfoImpl->SetMainApp(true);
1019                 }
1020         }
1021
1022         if (isUiApp == true)
1023         {
1024                 char* pMenuIconVisible = pAttr->Find("MenuIconVisible");
1025                 if (pMenuIconVisible)
1026                 {
1027                         if (strcasecmp(pMenuIconVisible, "True") == 0)
1028                         {
1029                                 pPackageAppInfoImpl->SetMenuIconVisible(true);
1030                         }
1031                         else
1032                         {
1033                                 pPackageAppInfoImpl->SetMenuIconVisible(false);
1034                         }
1035
1036                         SysLog(NID_APP, "MenuIconVisible = [%s]", pMenuIconVisible);
1037                 }
1038         }
1039         else
1040         {
1041                 pPackageAppInfoImpl->SetMenuIconVisible(false);
1042         }
1043
1044         return true;
1045 }
1046
1047 } } } // Tizen::App::Package