Fix typos
[TortoiseGit.git] / src / TortoiseShell / ItemIDList.cpp
blobabd4431b6f0b5795a8ca5964152ae4ff7138fa0d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016, 2019, 2023 - TortoiseGit
4 // Copyright (C) 2003-2006, 2009, 2011-2013, 2015-2016 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "ShellExt.h"
22 #include "ItemIDList.h"
23 #include "StringUtils.h"
25 ItemIDList::ItemIDList(PCUITEMID_CHILD item, PCUIDLIST_RELATIVE parent)
26 : item_(item)
27 , parent_(parent)
31 ItemIDList::ItemIDList(PCIDLIST_ABSOLUTE item)
32 : item_(static_cast<PCUITEMID_CHILD>(item))
36 ItemIDList::~ItemIDList()
40 int ItemIDList::size() const
42 if (count_ == -1)
44 count_ = 0;
45 if (item_)
47 LPCSHITEMID ptr = &item_->mkid;
48 while (ptr && ptr->cb != 0)
50 ++count_;
51 LPCBYTE byte = reinterpret_cast<LPCBYTE>(ptr);
52 byte += ptr->cb;
53 ptr = reinterpret_cast<LPCSHITEMID>(byte);
57 return count_;
60 LPCSHITEMID ItemIDList::get(int index) const
62 int count = 0;
64 if (!item_)
65 return nullptr;
66 LPCSHITEMID ptr = &item_->mkid;
67 if (!ptr)
68 return nullptr;
69 while (ptr->cb != 0)
71 if (count == index)
72 break;
74 ++count;
75 LPCBYTE byte = reinterpret_cast<LPCBYTE>(ptr);
76 byte += ptr->cb;
77 ptr = reinterpret_cast<LPCSHITEMID>(byte);
79 return ptr;
82 std::wstring ItemIDList::toString(bool resolveLibraries /*= true*/)
84 CComPtr<IShellFolder> shellFolder;
85 CComPtr<IShellFolder> parentFolder;
86 std::wstring ret;
88 if (FAILED(::SHGetDesktopFolder(&shellFolder)))
89 return ret;
90 if (!parent_ || FAILED(shellFolder->BindToObject(parent_, 0, IID_IShellFolder, reinterpret_cast<void**>(&parentFolder))))
91 parentFolder = shellFolder;
93 if (parentFolder && item_ != 0)
95 STRRET name;
96 if (FAILED(parentFolder->GetDisplayNameOf(item_, SHGDN_NORMAL | SHGDN_FORPARSING, &name)))
97 return ret;
98 CComHeapPtr<wchar_t> szDisplayName;
99 if (FAILED(StrRetToStr(&name, item_, &szDisplayName)) || !szDisplayName)
100 return ret;
101 ret = szDisplayName;
103 else
104 return ret;
106 if (!((resolveLibraries) && (CStringUtils::StartsWith(ret.c_str(), L"::{"))))
107 return ret;
109 CComPtr<IShellLibrary> plib;
110 if (FAILED(plib.CoCreateInstance(CLSID_ShellLibrary, nullptr, CLSCTX_INPROC_SERVER)))
111 return ret;
113 CComPtr<IShellItem> psiLibrary;
114 if (FAILED(SHCreateItemFromParsingName(ret.c_str(), nullptr, IID_PPV_ARGS(&psiLibrary))))
115 return ret;
117 if (FAILED(plib->LoadLibraryFromItem(psiLibrary, STGM_READ | STGM_SHARE_DENY_NONE)))
118 return ret;
120 CComPtr<IShellItem> psiSaveLocation;
121 if (FAILED(plib->GetDefaultSaveFolder(DSFT_DETECT, IID_PPV_ARGS(&psiSaveLocation))))
122 return ret;
124 if (CComHeapPtr<WCHAR> pszName; SUCCEEDED(psiSaveLocation->GetDisplayName(SIGDN_FILESYSPATH, &pszName)))
125 return std::wstring(pszName);
127 return ret;
130 PCUITEMID_CHILD ItemIDList::operator& ()
132 return item_;