2 * Copyright (C) 2010-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/>.
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"
29 #include "filesystem/MultiPathDirectory.h"
31 #include "settings/MediaSourceSettings.h"
33 #include "StringUtils.h"
36 using namespace XFILE;
39 bool CFileUtils::DeleteItem(const CStdString &strPath, bool force)
41 CFileItemPtr item(new CFileItem(strPath));
42 item->SetPath(strPath);
43 item->m_bIsFolder = URIUtils::HasSlashAtEnd(strPath);
45 return DeleteItem(item, force);
48 bool CFileUtils::DeleteItem(const CFileItemPtr &item, bool force)
50 if (!item || item->IsParentFolder())
53 CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
54 if (!force && pDialog)
56 pDialog->SetHeading(122);
57 pDialog->SetLine(0, 125);
58 pDialog->SetLine(1, CURL(item->GetPath()).GetWithoutUserDetails());
59 pDialog->SetLine(2, "");
61 if (!pDialog->IsConfirmed()) return false;
64 // Create a temporary item list containing the file/folder for deletion
65 CFileItemPtr pItemTemp(new CFileItem(*item));
66 pItemTemp->Select(true);
70 // grab the real filemanager window, set up the progress bar,
71 // and process the delete action
72 CFileOperationJob op(CFileOperationJob::ActionDelete, items, "");
77 bool CFileUtils::RenameFile(const CStdString &strFile)
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))
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);
92 for (unsigned int i = 0; i < paths.size(); ++i)
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))
103 return CFile::Rename(strFileAndPath, strPath);
108 bool CFileUtils::RemoteAccessAllowed(const CStdString &strPath)
110 const unsigned int SourcesSize = 5;
111 CStdString SourceNames[] = { "programs", "files", "video", "music", "pictures" };
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();
119 if (StringUtils::StartsWithNoCase(realPath, "virtualpath://upnproot/"))
121 else if (StringUtils::StartsWithNoCase(realPath, "musicdb://"))
123 else if (StringUtils::StartsWithNoCase(realPath, "videodb://"))
125 else if (StringUtils::StartsWithNoCase(realPath, "library://video"))
127 else if (StringUtils::StartsWithNoCase(realPath, "sources://video"))
129 else if (StringUtils::StartsWithNoCase(realPath, "special://musicplaylists"))
131 else if (StringUtils::StartsWithNoCase(realPath, "special://profile/playlists"))
133 else if (StringUtils::StartsWithNoCase(realPath, "special://videoplaylists"))
135 else if (StringUtils::StartsWithNoCase(realPath, "special://skin"))
137 else if (StringUtils::StartsWithNoCase(realPath, "special://profile/addon_data"))
139 else if (StringUtils::StartsWithNoCase(realPath, "addons://sources"))
141 else if (StringUtils::StartsWithNoCase(realPath, "upnp://"))
143 else if (StringUtils::StartsWithNoCase(realPath, "plugin://"))
146 for (unsigned int index = 0; index < SourcesSize; index++)
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)
157 unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuffer)
159 XFILE::auto_buffer buffer;
162 const unsigned int total_read = file.LoadFile(filename, buffer);
163 outputBuffer = buffer.detach();