aboutsummaryrefslogtreecommitdiff
path: root/xbmc/platform/darwin/tvos/filesystem/TVOSDirectory.cpp
blob: 04639435057bcafc73eea107c34825067956f206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *      Copyright (C) 2018 Team MrMC
 *      https://github.com/MrMC
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MrMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "TVOSDirectory.h"

#include "FileItem.h"
#include "URL.h"
#include "filesystem/SpecialProtocol.h"
#include "utils/URIUtils.h"
#include "utils/XTimeUtils.h"
#include "utils/log.h"

#include "platform/darwin/tvos/TVOSNSUserDefaults.h"
#include "platform/darwin/tvos/filesystem/TVOSFile.h"
#include "platform/darwin/tvos/filesystem/TVOSFileUtils.h"
#include "platform/posix/filesystem/PosixDirectory.h"


using namespace XFILE;

bool CTVOSDirectory::WantsDirectory(const CURL& url)
{
  auto rootpath = CSpecialProtocol::TranslatePath(url);
  auto found = rootpath.find(CTVOSFileUtils::GetUserHomeDirectory());
  if (found == std::string::npos)
    return false;

  return true;
}

bool CTVOSDirectory::GetDirectory(const CURL& url, CFileItemList& items)
{
  // tvos keeps dirs and non-xml files in non-persistent Caches directory
  // xml files are vectored into NSUserDefaults which is persistent, so
  // we fetch dirs and non-xml files using CPosixDirectory, then fill
  // in any missing xml files from NSUserDefaults. The incoming url
  // will be a path, ending at some dir, so we only need to fetch any
  // xml files that might be at that dir level.
  bool rtn = CPosixDirectory::GetDirectory(url, items);
  if (!rtn)
    return false;

  // To see user home xml files in the file manager,
  // we have to populate a list on a directory request.
  auto rootpath = CSpecialProtocol::TranslatePath(url);
  // quick return check, we do not care if
  // not going to user home.
  auto found = rootpath.find(CTVOSFileUtils::GetUserHomeDirectory());
  if (found == std::string::npos)
    return rtn;

  // The directory request will point to the right path '.../home/userdata/..'
  // so we ask for files in ending directory, if we get xml file paths back
  // then we are re-vectoring them to persistent storage and need to
  // create CFileItems that will translate into CTVOSFile object later
  // when accessed.

  // GetDirectoryContents will return full paths
  std::vector<std::string> contents;
  CTVOSNSUserDefaults::GetDirectoryContents(rootpath, contents);
  for (const auto& path : contents)
  {
    CFileItemPtr pItem(new CFileItem(URIUtils::GetFileName(path)));
    // we only save files to persistent storage
    pItem->m_bIsFolder = false;
    // path must a full path, with no protocol
    // or they will not get intercepted in CFileFactory
    pItem->SetPath(path);
    if (!(m_flags & DIR_FLAG_NO_FILE_INFO))
    {
      struct __stat64 buffer;
      CTVOSFile tvOSFile;
      CURL url2(pItem->GetPath());
      if (tvOSFile.Stat(url2, &buffer) == 0)
      {
        // fake the datetime
        KODI::TIME::FileTime fileTime, localTime;
        KODI::TIME::TimeTToFileTime(buffer.st_mtime, &fileTime);
        KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime);
        pItem->m_dateTime = localTime;
        // all this to get the file size
        pItem->m_dwSize = buffer.st_size;
      }
    }
    items.Add(pItem);
  }

  return rtn || !contents.empty();
}

bool CTVOSDirectory::Create(const CURL& url)
{
  return CPosixDirectory::Create(url);
}

bool CTVOSDirectory::Exists(const CURL& url)
{
  return CPosixDirectory::Exists(url);
}

bool CTVOSDirectory::Remove(const CURL& url)
{
  return CPosixDirectory::Remove(url);
}