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 "FTPDirectory.h"
24 #include "utils/URIUtils.h"
27 #include "utils/StringUtils.h"
28 #include "utils/CharsetConverter.h"
31 using namespace XFILE;
33 CFTPDirectory::CFTPDirectory(void){}
34 CFTPDirectory::~CFTPDirectory(void){}
36 bool CFTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
42 CStdString path = url.GetFileName();
43 if( !path.empty() && !StringUtils::EndsWith(path, "/") )
46 url.SetFileName(path);
49 if (!reader.Open(url))
52 bool serverNotUseUTF8 = url.GetProtocolOption("utf8").Equals("0");
54 char buffer[MAX_PATH + 1024];
55 while( reader.ReadString(buffer, sizeof(buffer)) )
57 CStdString strBuffer = buffer;
59 StringUtils::RemoveCRLF(strBuffer);
62 if (parse.FTPParse(strBuffer))
64 if( parse.getName().length() == 0 )
67 if( parse.getFlagtrycwd() == 0 && parse.getFlagtryretr() == 0 )
72 name.assign(parse.getName());
74 if( name.Equals("..") || name.Equals(".") )
77 // server returned filename could in utf8 or non-utf8 encoding
78 // we need utf8, so convert it to utf8 anyway
79 g_charsetConverter.unknownToUTF8(name);
81 // convert got empty result, ignore it
85 if (serverNotUseUTF8 || name != parse.getName())
86 // non-utf8 name path, tag it with protocol option.
87 // then we can talk to server with the same encoding in CurlFile according to this tag.
88 url.SetProtocolOption("utf8", "0");
90 url.RemoveProtocolOption("utf8");
92 CFileItemPtr pItem(new CFileItem(name));
94 pItem->m_bIsFolder = (bool)(parse.getFlagtrycwd() != 0);
95 CStdString filePath = path + name;
96 if (pItem->m_bIsFolder)
97 URIUtils::AddSlashAtEnd(filePath);
99 /* qualify the url with host and all */
100 url.SetFileName(filePath);
101 pItem->SetPath(url.Get());
103 pItem->m_dwSize = parse.getSize();
104 pItem->m_dateTime=parse.getTime();
113 bool CFTPDirectory::Exists(const char* strPath)
115 // make sure ftp dir ends with slash,
116 // curl need to known it's a dir to check ftp directory existence.
117 CStdString file = strPath;
118 URIUtils::AddSlashAtEnd(file);
122 return ftp.Exists(url);