CommitDlg: Update empty file list message
[TortoiseGit.git] / src / TortoiseShell / ItemIDList.cpp
blob575a643b64e86bf5bc2794d95fec0133cdcda1a0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006, 2009, 2011-2013, 2015 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "ShellExt.h"
21 #include "ItemIDList.h"
24 ItemIDList::ItemIDList(PCUITEMID_CHILD item, PCUIDLIST_RELATIVE parent)
25 : item_ (item)
26 , parent_ (parent)
27 , count_ (-1)
31 ItemIDList::ItemIDList(PCIDLIST_ABSOLUTE item)
32 : item_((PCUITEMID_CHILD)item)
33 , parent_(0)
34 , count_(-1)
38 ItemIDList::~ItemIDList()
42 int ItemIDList::size() const
44 if (count_ == -1)
46 count_ = 0;
47 if (item_)
49 LPCSHITEMID ptr = &item_->mkid;
50 while (ptr != 0 && ptr->cb != 0)
52 ++count_;
53 LPBYTE byte = (LPBYTE) ptr;
54 byte += ptr->cb;
55 ptr = (LPCSHITEMID) byte;
59 return count_;
62 LPCSHITEMID ItemIDList::get(int index) const
64 int count = 0;
66 if (item_ == NULL)
67 return NULL;
68 LPCSHITEMID ptr = &item_->mkid;
69 if (ptr == NULL)
70 return NULL;
71 while (ptr->cb != 0)
73 if (count == index)
74 break;
76 ++count;
77 LPBYTE byte = (LPBYTE) ptr;
78 byte += ptr->cb;
79 ptr = (LPCSHITEMID) byte;
81 return ptr;
85 tstring ItemIDList::toString(bool resolveLibraries /*= true*/)
87 CComPtr<IShellFolder> shellFolder;
88 CComPtr<IShellFolder> parentFolder;
89 tstring ret;
91 HRESULT hr = ::SHGetDesktopFolder(&shellFolder);
92 if (FAILED(hr))
93 return ret;
94 if (parent_)
96 hr = shellFolder->BindToObject(parent_, 0, IID_IShellFolder, (void**) &parentFolder);
97 if (FAILED(hr))
98 parentFolder = shellFolder;
100 else
102 parentFolder = shellFolder;
105 STRRET name;
106 TCHAR * szDisplayName = NULL;
107 if ((parentFolder != 0)&&(item_ != 0))
109 hr = parentFolder->GetDisplayNameOf(item_, SHGDN_NORMAL | SHGDN_FORPARSING, &name);
110 if (FAILED(hr))
111 return ret;
112 hr = StrRetToStr (&name, item_, &szDisplayName);
113 if (FAILED(hr))
114 return ret;
116 if (szDisplayName == NULL)
118 return ret;
120 ret = szDisplayName;
121 CoTaskMemFree(szDisplayName);
122 if ((resolveLibraries) &&
123 (_tcsncmp(ret.c_str(), _T("::{"), 3)==0))
125 CComPtr<IShellLibrary> plib;
126 hr = CoCreateInstance(CLSID_ShellLibrary,
127 NULL,
128 CLSCTX_INPROC_SERVER,
129 IID_PPV_ARGS(&plib));
130 if (SUCCEEDED(hr))
132 typedef HRESULT STDAPICALLTYPE SHCreateItemFromParsingNameFN(__in PCWSTR pszPath, __in_opt IBindCtx *pbc, __in REFIID riid, __deref_out void **ppv);
133 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
134 if (hShell)
136 SHCreateItemFromParsingNameFN *pfnSHCreateItemFromParsingName = (SHCreateItemFromParsingNameFN*)GetProcAddress(hShell, "SHCreateItemFromParsingName");
137 if (pfnSHCreateItemFromParsingName)
139 CComPtr<IShellItem> psiLibrary;
140 hr = pfnSHCreateItemFromParsingName(ret.c_str(), NULL, IID_PPV_ARGS(&psiLibrary));
141 if (SUCCEEDED(hr))
143 hr = plib->LoadLibraryFromItem(psiLibrary, STGM_READ|STGM_SHARE_DENY_NONE);
144 if (SUCCEEDED(hr))
146 CComPtr<IShellItem> psiSaveLocation;
147 hr = plib->GetDefaultSaveFolder(DSFT_DETECT, IID_PPV_ARGS(&psiSaveLocation));
148 if (SUCCEEDED(hr))
150 PWSTR pszName = NULL;
151 hr = psiSaveLocation->GetDisplayName(SIGDN_FILESYSPATH, &pszName);
152 if (SUCCEEDED(hr))
154 ret = pszName;
155 CoTaskMemFree(pszName);
164 return ret;
167 PCUITEMID_CHILD ItemIDList::operator& ()
169 return item_;