Merge pull request #473 from Montellese/onplaybackspeedchanged
[vuplus_xbmc] / xbmc / filesystem / HTTPDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2009 Team XBMC
3  *      http://www.xbmc.org
4  *
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.
9  *
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.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "HTTPDirectory.h"
23 #include "URL.h"
24 #include "FileCurl.h"
25 #include "FileItem.h"
26 #include "utils/RegExp.h"
27 #include "settings/AdvancedSettings.h"
28 #include "utils/StringUtils.h"
29 #include "utils/CharsetConverter.h"
30 #include "utils/log.h"
31 #include "utils/URIUtils.h"
32 #include "climits"
33
34 using namespace XFILE;
35
36 CHTTPDirectory::CHTTPDirectory(void){}
37 CHTTPDirectory::~CHTTPDirectory(void){}
38
39 bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
40 {
41   CFileCurl http;
42   CURL url(strPath);
43
44   CStdString strName, strLink;
45   CStdString strBasePath = url.GetFileName();
46
47   if(!http.Open(url))
48   {
49     CLog::Log(LOGERROR, "%s - Unable to get http directory", __FUNCTION__);
50     return false;
51   }
52
53   CRegExp reItem;
54   reItem.RegComp("<a href=\"(.*)\">(.*)</a>");
55
56   /* read response from server into string buffer */
57   char buffer[MAX_PATH + 1024];
58   while(http.ReadString(buffer, sizeof(buffer)-1))
59   {
60     CStdString strBuffer = buffer;
61     StringUtils::RemoveCRLF(strBuffer);
62
63     if (reItem.RegFind(strBuffer.c_str()) >= 0)
64     {
65       strLink = reItem.GetReplaceString("\\1");
66       strName = reItem.GetReplaceString("\\2");
67
68       if(strLink[0] == '/')
69         strLink = strLink.Mid(1);
70
71       CStdString strNameTemp = strName.Trim();
72       CStdString strLinkTemp = strLink;
73       URIUtils::RemoveSlashAtEnd(strLinkTemp);
74       URIUtils::RemoveSlashAtEnd(strNameTemp);
75       CURL::Decode(strLinkTemp);
76
77       if (strNameTemp == strLinkTemp)
78       {
79         g_charsetConverter.unknownToUTF8(strName);
80         URIUtils::RemoveSlashAtEnd(strName);
81
82         CFileItemPtr pItem(new CFileItem(strName));
83         pItem->SetPath(strBasePath + strLink);
84         pItem->SetProperty("IsHTTPDirectory", true);
85
86         if(URIUtils::HasSlashAtEnd(pItem->GetPath()))
87           pItem->m_bIsFolder = true;
88
89         url.SetFileName(pItem->GetPath());
90         pItem->SetPath(url.Get());
91
92         if (!pItem->m_bIsFolder && g_advancedSettings.m_bHTTPDirectoryStatFilesize)
93         {
94           CFileCurl file;
95           file.Open(url);
96           pItem->m_dwSize= file.GetLength();
97           file.Close();
98         }
99
100         if (!pItem->m_bIsFolder && pItem->m_dwSize == 0)
101         {
102           CRegExp reSize;
103           reSize.RegComp(">([0-9.]+)(K|M|G)</td>");
104           if (reSize.RegFind(strBuffer.c_str()) >= 0)
105           {
106             double Size = atof(reSize.GetReplaceString("\\1"));
107             CStdString strUnit = reSize.GetReplaceString("\\2");
108
109             if (strUnit == "M")
110               Size = Size * 1024;
111             else if (strUnit == "G")
112               Size = Size * 1000 * 1024;
113
114             pItem->m_dwSize = (int64_t)(Size * 1024);
115           }
116         }
117
118         items.Add(pItem);
119       }
120     }
121   }
122   http.Close();
123
124   return true;
125 }
126
127 bool CHTTPDirectory::Exists(const char* strPath)
128 {
129   CFileCurl http;
130   CURL url(strPath);
131   struct __stat64 buffer;
132
133   if( http.Stat(url, &buffer) != 0 )
134   {
135     return false;
136   }
137
138   if (buffer.st_mode == _S_IFDIR)
139           return true;
140
141   return false;
142 }