Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / UI / Agent / ArchiveFolderOpen.cpp
1 // Zip/ArchiveFolder.cpp\r
2 \r
3 #include "StdAfx.h"\r
4 \r
5 #include "Agent.h"\r
6 \r
7 #include "Common/StringConvert.h"\r
8 \r
9 extern HINSTANCE g_hInstance;\r
10 \r
11 static inline UINT GetCurrentFileCodePage()\r
12 {\r
13   #ifdef UNDER_CE\r
14   return CP_ACP;\r
15   #else\r
16   return AreFileApisANSI() ? CP_ACP : CP_OEMCP;\r
17   #endif\r
18 }\r
19 \r
20 void CArchiveFolderManager::LoadFormats()\r
21 {\r
22   if (!_codecs)\r
23   {\r
24     _compressCodecsInfo = _codecs = new CCodecs;\r
25     _codecs->Load();\r
26   }\r
27 }\r
28 \r
29 int CArchiveFolderManager::FindFormat(const UString &type)\r
30 {\r
31   for (int i = 0; i < _codecs->Formats.Size(); i++)\r
32     if (type.CompareNoCase(_codecs->Formats[i].Name) == 0)\r
33       return i;\r
34   return -1;\r
35 }\r
36 \r
37 STDMETHODIMP CArchiveFolderManager::OpenFolderFile(IInStream *inStream,\r
38     const wchar_t *filePath, const wchar_t *arcFormat,\r
39     IFolderFolder **resultFolder, IProgress *progress)\r
40 {\r
41   CMyComPtr<IArchiveOpenCallback> openArchiveCallback;\r
42   if (progress != 0)\r
43   {\r
44     CMyComPtr<IProgress> progressWrapper = progress;\r
45     progressWrapper.QueryInterface(IID_IArchiveOpenCallback, &openArchiveCallback);\r
46   }\r
47   CAgent *agent = new CAgent();\r
48   CMyComPtr<IInFolderArchive> archive = agent;\r
49   RINOK(agent->Open(inStream, filePath, arcFormat, NULL, openArchiveCallback));\r
50   return agent->BindToRootFolder(resultFolder);\r
51 }\r
52 \r
53 /*\r
54 HRESULT CAgent::FolderReOpen(\r
55     IArchiveOpenCallback *openArchiveCallback)\r
56 {\r
57   return ReOpenArchive(_archive, _archiveFilePath);\r
58 }\r
59 */\r
60 \r
61 \r
62 /*\r
63 STDMETHODIMP CArchiveFolderManager::GetExtensions(const wchar_t *type, BSTR *extensions)\r
64 {\r
65   *extensions = 0;\r
66   int formatIndex = FindFormat(type);\r
67   if (formatIndex <  0)\r
68     return E_INVALIDARG;\r
69   // Exts[0].Ext;\r
70   return StringToBstr(_codecs.Formats[formatIndex].GetAllExtensions(), extensions);\r
71 }\r
72 */\r
73 \r
74 static void AddIconExt(const CCodecIcons &lib, UString &dest)\r
75 {\r
76   for (int j = 0; j < lib.IconPairs.Size(); j++)\r
77   {\r
78     if (!dest.IsEmpty())\r
79       dest += L' ';\r
80     dest += lib.IconPairs[j].Ext;\r
81   }\r
82 }\r
83 \r
84 STDMETHODIMP CArchiveFolderManager::GetExtensions(BSTR *extensions)\r
85 {\r
86   LoadFormats();\r
87   *extensions = 0;\r
88   UString res;\r
89   for (int i = 0; i < _codecs->Libs.Size(); i++)\r
90     AddIconExt(_codecs->Libs[i], res);\r
91   AddIconExt(_codecs->InternalIcons, res);\r
92   return StringToBstr(res, extensions);\r
93 }\r
94 \r
95 STDMETHODIMP CArchiveFolderManager::GetIconPath(const wchar_t *ext, BSTR *iconPath, Int32 *iconIndex)\r
96 {\r
97   LoadFormats();\r
98   *iconPath = 0;\r
99   *iconIndex = 0;\r
100   for (int i = 0; i < _codecs->Libs.Size(); i++)\r
101   {\r
102     const CCodecLib &lib = _codecs->Libs[i];\r
103     int ii;\r
104     if (lib.FindIconIndex(ext, ii))\r
105     {\r
106       *iconIndex = ii;\r
107       return StringToBstr(GetUnicodeString(lib.Path, GetCurrentFileCodePage()), iconPath);\r
108     }\r
109   }\r
110   int ii;\r
111   if (_codecs->InternalIcons.FindIconIndex(ext, ii))\r
112   {\r
113     *iconIndex = ii;\r
114     UString path;\r
115     NWindows::NDLL::MyGetModuleFileName(g_hInstance, path);\r
116     return StringToBstr(path, iconPath);\r
117   }\r
118   return S_OK;\r
119 }\r
120 \r
121 /*\r
122 STDMETHODIMP CArchiveFolderManager::GetTypes(BSTR *types)\r
123 {\r
124   LoadFormats();\r
125   UString typesStrings;\r
126   for(int i = 0; i < _codecs.Formats.Size(); i++)\r
127   {\r
128     const CArcInfoEx &ai = _codecs.Formats[i];\r
129     if (ai.AssociateExts.Size() == 0)\r
130       continue;\r
131     if (i != 0)\r
132       typesStrings += L' ';\r
133     typesStrings += ai.Name;\r
134   }\r
135   return StringToBstr(typesStrings, types);\r
136 }\r
137 STDMETHODIMP CArchiveFolderManager::CreateFolderFile(const wchar_t * type,\r
138     const wchar_t * filePath, IProgress progress)\r
139 {\r
140   return E_NOTIMPL;\r
141 }\r
142 */\r