blob: 00d328efd5d16e2e1f443d726b3cd5413f4deafb [file] [log] [blame]
Mike Lockwood5fd1ff02010-07-26 20:44:42 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "MtpClient.h"
18#include "MtpDevice.h"
19#include "MtpDeviceInfo.h"
20#include "MtpObjectInfo.h"
21#include "MtpStorage.h"
22#include "MtpUtils.h"
23
24#include "MtpFile.h"
25
26namespace android {
27
28MtpClient* MtpFile::sClient = NULL;
29
30MtpFile::MtpFile(MtpDevice* device)
31 : mDevice(device),
32 mStorage(0),
33 mHandle(0)
34{
35}
36
37MtpFile::MtpFile(MtpDevice* device, MtpStorageID storage)
38 : mDevice(device),
39 mStorage(storage),
40 mHandle(0)
41{
42}
43
44MtpFile::MtpFile(MtpDevice* device, MtpStorageID storage, MtpObjectHandle handle)
45 : mDevice(device),
46 mStorage(storage),
47 mHandle(handle)
48{
49}
50
51MtpFile::MtpFile(MtpFile* file)
52 : mDevice(file->mDevice),
53 mStorage(file->mStorage),
54 mHandle(file->mHandle)
55{
56}
57
58MtpFile::~MtpFile() {
59}
60
61void MtpFile::print() {
62 if (mHandle) {
63
64 } else if (mStorage) {
65 printf("%x\n", mStorage);
66 } else {
67 int id = mDevice->getID();
68 MtpDeviceInfo* info = mDevice->getDeviceInfo();
69 if (info)
70 printf("%d\t%s %s %s\n", id, info->mManufacturer, info->mModel, info->mSerial);
71 else
72 printf("%d\t(no device info available)\n", id);
73 delete info;
74 }
75}
76
77MtpObjectInfo* MtpFile::getObjectInfo() {
78 return mDevice->getObjectInfo(mHandle);
79}
80
81void MtpFile::list() {
82 if (mStorage) {
83 MtpObjectHandleList* handles = mDevice->getObjectHandles(mStorage, 0,
84 (mHandle ? mHandle : -1));
85 if (handles) {
86 for (int i = 0; i < handles->size(); i++) {
87 MtpObjectHandle handle = (*handles)[i];
88 MtpObjectInfo* info = mDevice->getObjectInfo(handle);
89 if (info) {
90 char modified[100];
91 struct tm tm;
92
93 gmtime_r(&info->mDateModified, &tm);
94 strftime(modified, sizeof(modified), "%a %b %e %H:%M:%S GMT %Y", &tm);
95 printf("%s Handle: %d Format: %04X Size: %d Modified: %s\n",
96 info->mName, handle, info->mFormat, info->mCompressedSize, modified);
97 delete info;
98 }
99 }
100 delete handles;
101 }
102 } else {
103 // list storage units for device
104 MtpStorageIDList* storageList = mDevice->getStorageIDs();
105 for (int i = 0; i < storageList->size(); i++) {
106 MtpStorageID storageID = (*storageList)[i];
107 printf("%x\n", storageID);
108 }
109 }
110}
111
112void MtpFile::init(MtpClient* client) {
113 sClient = client;
114}
115
116MtpFile* MtpFile::parsePath(MtpFile* base, char* path) {
117 MtpDevice* device = NULL;
118 MtpStorageID storage = 0;
119 MtpObjectHandle handle = 0;
120
121 if (path[0] != '/' && base) {
122 device = base->mDevice;
123 storage = base->mStorage;
124 handle = base->mHandle;
125 }
126
127 // parse an absolute path
128 if (path[0] == '/')
129 path++;
130 char* tok = strtok(path, "/");
131 while (tok) {
132 if (storage) {
133 // find child of current handle
134 MtpObjectHandleList* handles = device->getObjectHandles(storage, 0,
135 (handle ? handle : -1));
136 MtpObjectHandle childHandle = 0;
137
138 if (handles) {
139 for (int i = 0; i < handles->size() && !childHandle; i++) {
140 MtpObjectHandle handle = (*handles)[i];
141 MtpObjectInfo* info = device->getObjectInfo(handle);
142 if (info && !strcmp(tok, info->mName))
143 childHandle = handle;
144 delete info;
145 }
146 delete handles;
147 }
148 if (childHandle)
149 handle = childHandle;
150 else
151 return NULL;
152 } else if (device) {
153 unsigned int id;
154 // find storage for the device
155 if (sscanf(tok, "%x", &id) == 1) {
156 MtpStorageIDList* storageList = device->getStorageIDs();
157 bool found = false;
158 for (int i = 0; i < storageList->size(); i++) {
159 if ((*storageList)[i] == id) {
160 found = true;
161 break;
162 }
163 }
164 if (found)
165 storage = id;
166 else
167 return NULL;
168 }
169 } else {
170 // find device
171 unsigned int id;
172 if (sscanf(tok, "%d", &id) == 1)
173 device = sClient->getDevice(id);
174 if (!device)
175 return NULL;
176 }
177
178 tok = strtok(NULL, "/");
179 }
180
181 if (device)
182 return new MtpFile(device, storage, handle);
183 else
184 return NULL;
185}
186
187}