Merge pull request #5095 from koying/fixdroidappcrash
[vuplus_xbmc] / xbmc / filesystem / RarDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "RarDirectory.h"
22 #include "RarManager.h"
23 #include "utils/log.h"
24 #include "utils/URIUtils.h"
25 #include "URL.h"
26 #include "FileItem.h"
27 #include "utils/StringUtils.h"
28
29 namespace XFILE
30 {
31   CRarDirectory::CRarDirectory()
32   {
33   }
34
35   CRarDirectory::~CRarDirectory()
36   {
37   }
38
39   bool CRarDirectory::GetDirectory(const CStdString& strPathOrig, CFileItemList& items)
40   {
41     CStdString strPath;
42
43     /* if this isn't a proper archive path, assume it's the path to a archive file */
44     if( !StringUtils::StartsWithNoCase(strPathOrig, "rar://") )
45       URIUtils::CreateArchivePath(strPath, "rar", strPathOrig, "");
46     else
47       strPath = strPathOrig;
48
49     CURL url(strPath);
50     CStdString strArchive = url.GetHostName();
51     CStdString strOptions = url.GetOptions();
52     CStdString strPathInArchive = url.GetFileName();
53     url.SetOptions("");
54
55     CStdString strSlashPath = url.Get();
56
57     // the RAR code depends on things having a "\" at the end of the path
58     URIUtils::AddSlashAtEnd(strSlashPath);
59
60     if (g_RarManager.GetFilesInRar(items,strArchive,true,strPathInArchive))
61     {
62       // fill in paths
63       for( int iEntry=0;iEntry<items.Size();++iEntry)
64       {
65         if (items[iEntry]->IsParentFolder())
66           continue;
67         items[iEntry]->SetPath(URIUtils::AddFileToFolder(strSlashPath,items[iEntry]->GetPath()+strOptions));
68         items[iEntry]->m_iDriveType = 0;
69         //CLog::Log(LOGDEBUG, "RarXFILE::GetDirectory() retrieved file: %s", items[iEntry]->m_strPath.c_str());
70       }
71       return( true);
72     }
73     else
74     {
75       CLog::Log(LOGWARNING,"%s: rar lib returned no files in archive %s, likely corrupt",__FUNCTION__,strArchive.c_str());
76       return( false );
77     }
78   }
79
80   bool CRarDirectory::Exists(const char* strPath)
81   {
82     CFileItemList items;
83     if (GetDirectory(strPath,items))
84       return true;
85
86     return false;
87   }
88
89   bool CRarDirectory::ContainsFiles(const CStdString& strPath)
90   {
91     CFileItemList items;
92     if (g_RarManager.GetFilesInRar(items,strPath))
93     {
94       if (items.Size() > 1)
95         return true;
96
97       return false;
98     }
99
100     return false;
101   }
102 }
103