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