2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
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 XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "HDDirectory.h"
26 #include "utils/AutoPtrHandle.h"
27 #include "utils/AliasShortcutUtils.h"
28 #include "utils/URIUtils.h"
31 #include "utils/CharsetConverter.h"
34 #ifndef INVALID_FILE_ATTRIBUTES
35 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
39 typedef WIN32_FIND_DATAW LOCAL_WIN32_FIND_DATA;
40 #define LocalFindFirstFile FindFirstFileW
41 #define LocalFindNextFile FindNextFileW
43 typedef WIN32_FIND_DATA LOCAL_WIN32_FIND_DATA;
44 #define LocalFindFirstFile FindFirstFile
45 #define LocalFindNextFile FindNextFile
48 using namespace AUTOPTR;
49 using namespace XFILE;
51 CHDDirectory::CHDDirectory(void)
54 CHDDirectory::~CHDDirectory(void)
57 bool CHDDirectory::GetDirectory(const CStdString& strPath1, CFileItemList &items)
59 LOCAL_WIN32_FIND_DATA wfd;
61 CStdString strPath=strPath1;
63 if (IsAliasShortcut(strPath))
64 TranslateAliasShortcut(strPath);
66 CStdString strRoot = strPath;
69 memset(&wfd, 0, sizeof(wfd));
70 URIUtils::AddSlashAtEnd(strRoot);
72 strRoot.Replace("/", "\\");
74 if (URIUtils::IsDVD(strRoot) && m_isoReader.IsScanned())
76 // Reset iso reader and remount or
77 // we can't access the dvd-rom
82 CStdStringW strSearchMask;
83 g_charsetConverter.utf8ToW(strRoot, strSearchMask, false);
84 strSearchMask += "*.*";
86 CStdString strSearchMask = strRoot;
90 CAutoPtrFind hFind ( LocalFindFirstFile(strSearchMask.c_str(), &wfd));
92 // on error, check if path exists at all, this will return true if empty folder
94 return Exists(strPath1);
100 if (wfd.cFileName[0] != 0)
104 g_charsetConverter.wToUTF8(wfd.cFileName,strLabel);
106 strLabel = wfd.cFileName;
108 if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
110 if (strLabel != "." && strLabel != "..")
112 CFileItemPtr pItem(new CFileItem(strLabel));
113 CStdString itemPath = strRoot + strLabel;
114 URIUtils::AddSlashAtEnd(itemPath);
115 pItem->SetPath(itemPath);
116 pItem->m_bIsFolder = true;
117 FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
118 pItem->m_dateTime=localTime;
120 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
121 pItem->SetProperty("file:hidden", true);
127 CFileItemPtr pItem(new CFileItem(strLabel));
128 pItem->SetPath(strRoot + strLabel);
129 pItem->m_bIsFolder = false;
130 pItem->m_dwSize = CUtil::ToInt64(wfd.nFileSizeHigh, wfd.nFileSizeLow);
131 FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
132 pItem->m_dateTime=localTime;
134 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
135 pItem->SetProperty("file:hidden", true);
141 while (LocalFindNextFile((HANDLE)hFind, &wfd));
146 bool CHDDirectory::Create(const char* strPath)
148 CStdString strPath1 = strPath;
149 URIUtils::AddSlashAtEnd(strPath1);
152 if (strPath1.size() == 3 && strPath1[1] == ':')
153 return Exists(strPath); // A drive - we can't "create" a drive
154 CStdStringW strWPath1;
155 g_charsetConverter.utf8ToW(strPath1, strWPath1, false);
156 if(::CreateDirectoryW(strWPath1, NULL))
158 if(::CreateDirectory(strPath1.c_str(), NULL))
161 else if(GetLastError() == ERROR_ALREADY_EXISTS)
167 bool CHDDirectory::Remove(const char* strPath)
170 CStdStringW strWPath;
171 g_charsetConverter.utf8ToW(strPath, strWPath, false);
172 return (::RemoveDirectoryW(strWPath) || GetLastError() == ERROR_PATH_NOT_FOUND) ? true : false;
174 return ::RemoveDirectory(strPath) ? true : false;
178 bool CHDDirectory::Exists(const char* strPath)
180 if (!strPath || !*strPath)
182 CStdString strReplaced=strPath;
184 CStdStringW strWReplaced;
185 strReplaced.Replace("/","\\");
186 URIUtils::AddSlashAtEnd(strReplaced);
187 g_charsetConverter.utf8ToW(strReplaced, strWReplaced, false);
188 DWORD attributes = GetFileAttributesW(strWReplaced);
190 DWORD attributes = GetFileAttributes(strReplaced.c_str());
192 if(attributes == INVALID_FILE_ATTRIBUTES)
194 if (FILE_ATTRIBUTE_DIRECTORY & attributes) return true;