DVDCodecs: Amlogic: Handle conditions in which amcodec should be opened during Open()
[vuplus_xbmc] / xbmc / utils / FileUtils.cpp
1 /*
2  *      Copyright (C) 2010-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 #include "FileUtils.h"
21 #include "guilib/GUIWindowManager.h"
22 #include "dialogs/GUIDialogYesNo.h"
23 #include "guilib/GUIKeyboardFactory.h"
24 #include "utils/log.h"
25 #include "guilib/LocalizeStrings.h"
26 #include "JobManager.h"
27 #include "FileOperationJob.h"
28 #include "URIUtils.h"
29 #include "filesystem/MultiPathDirectory.h"
30 #include <vector>
31 #include "settings/MediaSourceSettings.h"
32 #include "Util.h"
33 #include "StringUtils.h"
34 #include "URL.h"
35
36 using namespace XFILE;
37 using namespace std;
38
39 bool CFileUtils::DeleteItem(const CStdString &strPath, bool force)
40 {
41   CFileItemPtr item(new CFileItem(strPath));
42   item->SetPath(strPath);
43   item->m_bIsFolder = URIUtils::HasSlashAtEnd(strPath);
44   item->Select(true);
45   return DeleteItem(item, force);
46 }
47
48 bool CFileUtils::DeleteItem(const CFileItemPtr &item, bool force)
49 {
50   if (!item || item->IsParentFolder())
51     return false;
52
53   CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
54   if (!force && pDialog)
55   {
56     pDialog->SetHeading(122);
57     pDialog->SetLine(0, 125);
58     pDialog->SetLine(1, URIUtils::GetFileName(item->GetPath()));
59     pDialog->SetLine(2, "");
60     pDialog->DoModal();
61     if (!pDialog->IsConfirmed()) return false;
62   }
63
64   // Create a temporary item list containing the file/folder for deletion
65   CFileItemPtr pItemTemp(new CFileItem(*item));
66   pItemTemp->Select(true);
67   CFileItemList items;
68   items.Add(pItemTemp);
69
70   // grab the real filemanager window, set up the progress bar,
71   // and process the delete action
72   CFileOperationJob op(CFileOperationJob::ActionDelete, items, "");
73
74   return op.DoWork();
75 }
76
77 bool CFileUtils::RenameFile(const CStdString &strFile)
78 {
79   CStdString strFileAndPath(strFile);
80   URIUtils::RemoveSlashAtEnd(strFileAndPath);
81   CStdString strFileName = URIUtils::GetFileName(strFileAndPath);
82   CStdString strPath = URIUtils::GetDirectory(strFileAndPath);
83   if (CGUIKeyboardFactory::ShowAndGetInput(strFileName, g_localizeStrings.Get(16013), false))
84   {
85     strPath = URIUtils::AddFileToFolder(strPath, strFileName);
86     CLog::Log(LOGINFO,"FileUtils: rename %s->%s\n", strFileAndPath.c_str(), strPath.c_str());
87     if (URIUtils::IsMultiPath(strFileAndPath))
88     { // special case for multipath renames - rename all the paths.
89       vector<CStdString> paths;
90       CMultiPathDirectory::GetPaths(strFileAndPath, paths);
91       bool success = false;
92       for (unsigned int i = 0; i < paths.size(); ++i)
93       {
94         CStdString filePath(paths[i]);
95         URIUtils::RemoveSlashAtEnd(filePath);
96         filePath = URIUtils::GetDirectory(filePath);
97         filePath = URIUtils::AddFileToFolder(filePath, strFileName);
98         if (CFile::Rename(paths[i], filePath))
99           success = true;
100       }
101       return success;
102     }
103     return CFile::Rename(strFileAndPath, strPath);
104   }
105   return false;
106 }
107
108 bool CFileUtils::RemoteAccessAllowed(const CStdString &strPath)
109 {
110   const unsigned int SourcesSize = 5;
111   CStdString SourceNames[] = { "programs", "files", "video", "music", "pictures" };
112
113   string realPath = URIUtils::GetRealPath(strPath);
114   // for rar:// and zip:// paths we need to extract the path to the archive
115   // instead of using the VFS path
116   while (URIUtils::IsInArchive(realPath))
117     realPath = CURL(realPath).GetHostName();
118
119   if (StringUtils::StartsWithNoCase(realPath, "virtualpath://upnproot/"))
120     return true;
121   else if (StringUtils::StartsWithNoCase(realPath, "musicdb://"))
122     return true;
123   else if (StringUtils::StartsWithNoCase(realPath, "videodb://"))
124     return true;
125   else if (StringUtils::StartsWithNoCase(realPath, "library://video"))
126     return true;
127   else if (StringUtils::StartsWithNoCase(realPath, "sources://video"))
128     return true;
129   else if (StringUtils::StartsWithNoCase(realPath, "special://musicplaylists"))
130     return true;
131   else if (StringUtils::StartsWithNoCase(realPath, "special://profile/playlists"))
132     return true;
133   else if (StringUtils::StartsWithNoCase(realPath, "special://videoplaylists"))
134     return true;
135   else if (StringUtils::StartsWithNoCase(realPath, "special://skin"))
136     return true;
137   else if (StringUtils::StartsWithNoCase(realPath, "special://profile/addon_data"))
138     return true;
139   else if (StringUtils::StartsWithNoCase(realPath, "addons://sources"))
140     return true;
141   else if (StringUtils::StartsWithNoCase(realPath, "upnp://"))
142     return true;
143   else if (StringUtils::StartsWithNoCase(realPath, "plugin://"))
144     return true;
145   bool isSource;
146   for (unsigned int index = 0; index < SourcesSize; index++)
147   {
148     VECSOURCES* sources = CMediaSourceSettings::Get().GetSources(SourceNames[index]);
149     int sourceIndex = CUtil::GetMatchingSource(realPath, *sources, isSource);
150     if (sourceIndex >= 0 && sourceIndex < (int)sources->size() && sources->at(sourceIndex).m_iHasLock != 2 && sources->at(sourceIndex).m_allowSharing)
151       return true;
152   }
153   return false;
154 }
155
156
157 unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuffer)
158 {
159   XFILE::auto_buffer buffer;
160   XFILE::CFile file;
161
162   const unsigned int total_read = file.LoadFile(filename, buffer);
163   outputBuffer = buffer.detach();
164
165   return total_read;
166 }