xref: /aosp_15_r20/external/lzma/CPP/7zip/UI/Agent/ArchiveFolderOpen.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Agent/ArchiveFolderOpen.cpp
2 
3 #include "StdAfx.h"
4 
5 // #ifdef NEW_FOLDER_INTERFACE
6 
7 #include "../../../Common/StringToInt.h"
8 #include "../../../Windows/DLL.h"
9 #include "../../../Windows/ResourceString.h"
10 
11 #include "Agent.h"
12 
13 extern HINSTANCE g_hInstance;
14 static const UINT kIconTypesResId = 100;
15 
LoadIcons(HMODULE m)16 void CCodecIcons::LoadIcons(HMODULE m)
17 {
18   IconPairs.Clear();
19   UString iconTypes;
20   NWindows::MyLoadString(m, kIconTypesResId, iconTypes);
21   UStringVector pairs;
22   SplitString(iconTypes, pairs);
23   FOR_VECTOR (i, pairs)
24   {
25     const UString &s = pairs[i];
26     int pos = s.Find(L':');
27     CIconPair iconPair;
28     iconPair.IconIndex = -1;
29     if (pos < 0)
30       pos = (int)s.Len();
31     else
32     {
33       const UString num = s.Ptr((unsigned)pos + 1);
34       if (!num.IsEmpty())
35       {
36         const wchar_t *end;
37         iconPair.IconIndex = (int)ConvertStringToUInt32(num, &end);
38         if (*end != 0)
39           continue;
40       }
41     }
42     iconPair.Ext = s.Left((unsigned)pos);
43     IconPairs.Add(iconPair);
44   }
45 }
46 
FindIconIndex(const UString & ext,int & iconIndex) const47 bool CCodecIcons::FindIconIndex(const UString &ext, int &iconIndex) const
48 {
49   iconIndex = -1;
50   FOR_VECTOR (i, IconPairs)
51   {
52     const CIconPair &pair = IconPairs[i];
53     if (ext.IsEqualTo_NoCase(pair.Ext))
54     {
55       iconIndex = pair.IconIndex;
56       return true;
57     }
58   }
59   return false;
60 }
61 
62 
LoadFormats()63 void CArchiveFolderManager::LoadFormats()
64 {
65   if (WasLoaded)
66     return;
67 
68   LoadGlobalCodecs();
69 
70   #ifdef Z7_EXTERNAL_CODECS
71   CodecIconsVector.Clear();
72   FOR_VECTOR (i, g_CodecsObj->Libs)
73   {
74     CCodecIcons &ci = CodecIconsVector.AddNew();
75     ci.LoadIcons(g_CodecsObj->Libs[i].Lib.Get_HMODULE());
76   }
77   #endif
78   InternalIcons.LoadIcons(g_hInstance);
79   WasLoaded = true;
80 }
81 
82 /*
83 int CArchiveFolderManager::FindFormat(const UString &type)
84 {
85   FOR_VECTOR (i, g_CodecsObj->Formats)
86     if (type.IsEqualTo_NoCase(g_CodecsObj->Formats[i].Name))
87       return (int)i;
88   return -1;
89 }
90 */
91 
Z7_COM7F_IMF(CArchiveFolderManager::OpenFolderFile (IInStream * inStream,const wchar_t * filePath,const wchar_t * arcFormat,IFolderFolder ** resultFolder,IProgress * progress))92 Z7_COM7F_IMF(CArchiveFolderManager::OpenFolderFile(IInStream *inStream,
93     const wchar_t *filePath, const wchar_t *arcFormat,
94     IFolderFolder **resultFolder, IProgress *progress))
95 {
96   CMyComPtr<IArchiveOpenCallback> openArchiveCallback;
97   if (progress)
98   {
99     CMyComPtr<IProgress> progressWrapper = progress;
100     progressWrapper.QueryInterface(IID_IArchiveOpenCallback, &openArchiveCallback);
101   }
102   CAgent *agent = new CAgent();
103   CMyComPtr<IInFolderArchive> archive = agent;
104 
105   const HRESULT res = archive->Open(inStream, filePath, arcFormat, NULL, openArchiveCallback);
106 
107   if (res != S_OK)
108   {
109     if (res != S_FALSE)
110       return res;
111     /* 20.01: we create folder even for Non-Open cases, if there is NonOpen_ErrorInfo information.
112          So we can get error information from that IFolderFolder later. */
113     if (!agent->_archiveLink.NonOpen_ErrorInfo.IsThereErrorOrWarning())
114       return res;
115   }
116 
117   RINOK(archive->BindToRootFolder(resultFolder))
118   return res;
119 }
120 
121 /*
122 HRESULT CAgent::FolderReOpen(
123     IArchiveOpenCallback *openArchiveCallback)
124 {
125   return ReOpenArchive(_archive, _archiveFilePath);
126 }
127 */
128 
129 
130 /*
131 Z7_COM7F_IMF(CArchiveFolderManager::GetExtensions(const wchar_t *type, BSTR *extensions))
132 {
133   *extensions = 0;
134   int formatIndex = FindFormat(type);
135   if (formatIndex <  0)
136     return E_INVALIDARG;
137   // Exts[0].Ext;
138   return StringToBstr(g_CodecsObj.Formats[formatIndex].GetAllExtensions(), extensions);
139 }
140 */
141 
AddIconExt(const CCodecIcons & lib,UString & dest)142 static void AddIconExt(const CCodecIcons &lib, UString &dest)
143 {
144   FOR_VECTOR (i, lib.IconPairs)
145   {
146     dest.Add_Space_if_NotEmpty();
147     dest += lib.IconPairs[i].Ext;
148   }
149 }
150 
151 
Z7_COM7F_IMF(CArchiveFolderManager::GetExtensions (BSTR * extensions))152 Z7_COM7F_IMF(CArchiveFolderManager::GetExtensions(BSTR *extensions))
153 {
154   *extensions = NULL;
155   LoadFormats();
156   UString res;
157 
158   #ifdef Z7_EXTERNAL_CODECS
159   /*
160   FOR_VECTOR (i, g_CodecsObj->Libs)
161     AddIconExt(g_CodecsObj->Libs[i].CodecIcons, res);
162   */
163   FOR_VECTOR (i, CodecIconsVector)
164     AddIconExt(CodecIconsVector[i], res);
165   #endif
166 
167   AddIconExt(
168       // g_CodecsObj->
169       InternalIcons, res);
170 
171   return StringToBstr(res, extensions);
172 }
173 
174 
Z7_COM7F_IMF(CArchiveFolderManager::GetIconPath (const wchar_t * ext,BSTR * iconPath,Int32 * iconIndex))175 Z7_COM7F_IMF(CArchiveFolderManager::GetIconPath(const wchar_t *ext, BSTR *iconPath, Int32 *iconIndex))
176 {
177   *iconPath = NULL;
178   *iconIndex = 0;
179   LoadFormats();
180 
181   #ifdef Z7_EXTERNAL_CODECS
182   // FOR_VECTOR (i, g_CodecsObj->Libs)
183   FOR_VECTOR (i, CodecIconsVector)
184   {
185     int ii;
186     if (CodecIconsVector[i].FindIconIndex(ext, ii))
187     {
188       const CCodecLib &lib = g_CodecsObj->Libs[i];
189       *iconIndex = ii;
190       return StringToBstr(fs2us(lib.Path), iconPath);
191     }
192   }
193   #endif
194 
195   int ii;
196   if (InternalIcons.FindIconIndex(ext, ii))
197   {
198     FString path;
199     if (NWindows::NDLL::MyGetModuleFileName(path))
200     {
201       *iconIndex = ii;
202       return StringToBstr(fs2us(path), iconPath);
203     }
204   }
205   return S_OK;
206 }
207 
208 /*
209 Z7_COM7F_IMF(CArchiveFolderManager::GetTypes(BSTR *types))
210 {
211   LoadFormats();
212   UString typesStrings;
213   FOR_VECTOR(i, g_CodecsObj.Formats)
214   {
215     const CArcInfoEx &ai = g_CodecsObj.Formats[i];
216     if (ai.AssociateExts.Size() == 0)
217       continue;
218     if (i != 0)
219       typesStrings.Add_Space();
220     typesStrings += ai.Name;
221   }
222   return StringToBstr(typesStrings, types);
223 }
224 Z7_COM7F_IMF(CArchiveFolderManager::CreateFolderFile(const wchar_t * type,
225     const wchar_t * filePath, IProgress progress))
226 {
227   return E_NOTIMPL;
228 }
229 */
230 
231 // #endif
232