Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / filesystem / HDDirectory.cpp
blob49a5a72f5ad23b0adf17edf6941cc1fc1d03d1e8
1 /*
2 * Copyright (C) 2005-2013 Team XBMC
3 * http://xbmc.org
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)
8 * 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 XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "HDDirectory.h"
22 #include "Util.h"
23 #include "iso9660.h"
24 #include "URL.h"
25 #include "FileItem.h"
26 #include "utils/AutoPtrHandle.h"
27 #include "utils/AliasShortcutUtils.h"
28 #include "utils/URIUtils.h"
30 #ifdef TARGET_WINDOWS
31 #include "utils/CharsetConverter.h"
32 #include "win32/WIN32Util.h"
33 #endif
35 #ifndef INVALID_FILE_ATTRIBUTES
36 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
37 #endif
39 #ifdef TARGET_WINDOWS
40 typedef WIN32_FIND_DATAW LOCAL_WIN32_FIND_DATA;
41 #define LocalFindFirstFile FindFirstFileW
42 #define LocalFindNextFile FindNextFileW
43 #else
44 typedef WIN32_FIND_DATA LOCAL_WIN32_FIND_DATA;
45 #define LocalFindFirstFile FindFirstFile
46 #define LocalFindNextFile FindNextFile
47 #endif
49 using namespace AUTOPTR;
50 using namespace XFILE;
52 CHDDirectory::CHDDirectory(void)
55 CHDDirectory::~CHDDirectory(void)
58 bool CHDDirectory::GetDirectory(const CStdString& strPath1, CFileItemList &items)
60 LOCAL_WIN32_FIND_DATA wfd;
61 memset(&wfd, 0, sizeof(wfd));
63 CStdString strPath=strPath1;
65 if (IsAliasShortcut(strPath))
66 TranslateAliasShortcut(strPath);
68 CStdString strRoot = strPath;
69 CURL url(strPath);
71 URIUtils::AddSlashAtEnd(strRoot);
72 if (URIUtils::IsDVD(strRoot) && m_isoReader.IsScanned())
74 // Reset iso reader and remount or
75 // we can't access the dvd-rom
76 m_isoReader.Reset();
79 #ifdef TARGET_WINDOWS
80 std::wstring strSearchMask(CWIN32Util::ConvertPathToWin32Form(strRoot));
81 strSearchMask += L"*.*";
82 #else
83 CStdString strSearchMask(strRoot);
84 #endif
86 FILETIME localTime;
87 CAutoPtrFind hFind ( LocalFindFirstFile(strSearchMask.c_str(), &wfd));
89 // on error, check if path exists at all, this will return true if empty folder
90 if (!hFind.isValid())
91 return Exists(strPath1);
93 if (hFind.isValid())
97 if (wfd.cFileName[0] != 0)
99 CStdString strLabel;
100 #ifdef TARGET_WINDOWS
101 g_charsetConverter.wToUTF8(wfd.cFileName,strLabel, true);
102 #else
103 strLabel = wfd.cFileName;
104 #endif
105 if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
107 if (strLabel != "." && strLabel != "..")
109 CFileItemPtr pItem(new CFileItem(strLabel));
110 CStdString itemPath(URIUtils::AddFileToFolder(strRoot, strLabel));
111 URIUtils::AddSlashAtEnd(itemPath);
112 pItem->SetPath(itemPath);
113 pItem->m_bIsFolder = true;
114 FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
115 pItem->m_dateTime=localTime;
117 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
118 pItem->SetProperty("file:hidden", true);
119 items.Add(pItem);
122 else
124 CFileItemPtr pItem(new CFileItem(strLabel));
125 pItem->SetPath(URIUtils::AddFileToFolder(strRoot, strLabel));
126 pItem->m_bIsFolder = false;
127 pItem->m_dwSize = CUtil::ToInt64(wfd.nFileSizeHigh, wfd.nFileSizeLow);
128 FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
129 pItem->m_dateTime=localTime;
131 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
132 pItem->SetProperty("file:hidden", true);
134 items.Add(pItem);
138 while (LocalFindNextFile((HANDLE)hFind, &wfd));
140 return true;
143 bool CHDDirectory::Create(const char* strPath)
145 if (!strPath || !*strPath)
146 return false;
147 CStdString strPath1 = strPath;
148 URIUtils::AddSlashAtEnd(strPath1);
150 #ifdef TARGET_WINDOWS
151 if (strPath1.size() == 3 && strPath1[1] == ':')
152 return Exists(strPath); // A drive - we can't "create" a drive
153 if(::CreateDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath1).c_str(), NULL))
154 #else
155 if(::CreateDirectory(strPath1.c_str(), NULL))
156 #endif
157 return true;
158 else if(GetLastError() == ERROR_ALREADY_EXISTS)
159 return true;
161 return false;
164 bool CHDDirectory::Remove(const char* strPath)
166 if (!strPath || !*strPath)
167 return false;
168 #ifdef TARGET_WINDOWS
169 return (::RemoveDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath).c_str()) || GetLastError() == ERROR_PATH_NOT_FOUND) ? true : false;
170 #else
171 return ::RemoveDirectory(strPath) ? true : false;
172 #endif
175 bool CHDDirectory::Exists(const char* strPath)
177 if (!strPath || !*strPath)
178 return false;
179 #ifdef TARGET_WINDOWS
180 DWORD attributes = GetFileAttributesW(CWIN32Util::ConvertPathToWin32Form(strPath).c_str());
181 #else
182 DWORD attributes = GetFileAttributes(strPath);
183 #endif
184 if(attributes == INVALID_FILE_ATTRIBUTES)
185 return false;
186 if (FILE_ATTRIBUTE_DIRECTORY & attributes) return true;
187 return false;