Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Eric Caruso | 721d82e | 2017-08-21 15:57:30 -0700 | [diff] [blame] | 5 | #include "mtpd/file_entry.h" |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 6 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
Lei Zhang | 12dbc6f | 2017-04-21 23:00:45 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 9 | |
Eric Caruso | 721d82e | 2017-08-21 15:57:30 -0700 | [diff] [blame] | 10 | #include "mtpd/string_helpers.h" |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 11 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | |
| 14 | MtpFileEntry_FileType LibmtpFileTypeToProtoFileType( |
| 15 | LIBMTP_filetype_t file_type) { |
| 16 | switch (file_type) { |
| 17 | case LIBMTP_FILETYPE_FOLDER: |
| 18 | case LIBMTP_FILETYPE_JPEG: |
| 19 | case LIBMTP_FILETYPE_JFIF: |
| 20 | case LIBMTP_FILETYPE_TIFF: |
| 21 | case LIBMTP_FILETYPE_BMP: |
| 22 | case LIBMTP_FILETYPE_GIF: |
| 23 | case LIBMTP_FILETYPE_PICT: |
| 24 | case LIBMTP_FILETYPE_PNG: |
| 25 | case LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT: |
| 26 | case LIBMTP_FILETYPE_JP2: |
| 27 | case LIBMTP_FILETYPE_JPX: |
| 28 | case LIBMTP_FILETYPE_UNKNOWN: |
| 29 | return static_cast<MtpFileEntry_FileType>(file_type); |
| 30 | default: |
| 31 | return MtpFileEntry_FileType_FILE_TYPE_OTHER; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 37 | namespace mtpd { |
| 38 | |
| 39 | FileEntry::FileEntry(const LIBMTP_file_struct& file) |
| 40 | : item_id_(file.item_id), |
| 41 | parent_id_(file.parent_id), |
| 42 | file_size_(file.filesize), |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 43 | modification_time_(file.modificationdate), |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 44 | file_type_(file.filetype) { |
| 45 | if (file.filename) |
| 46 | file_name_ = file.filename; |
| 47 | } |
| 48 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 49 | FileEntry::FileEntry() |
| 50 | : item_id_(kInvalidFileId), |
| 51 | parent_id_(kInvalidFileId), |
| 52 | file_size_(0), |
| 53 | modification_time_(0), |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 54 | file_type_(LIBMTP_FILETYPE_UNKNOWN) {} |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 55 | |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 56 | FileEntry::~FileEntry() {} |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 57 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 58 | MtpFileEntry FileEntry::ToProtobuf() const { |
| 59 | MtpFileEntry protobuf; |
| 60 | protobuf.set_item_id(item_id_); |
| 61 | protobuf.set_parent_id(parent_id_); |
| 62 | protobuf.set_file_name(file_name_); |
| 63 | protobuf.set_file_size(file_size_); |
| 64 | protobuf.set_modification_time(modification_time_); |
| 65 | protobuf.set_file_type(LibmtpFileTypeToProtoFileType(file_type_)); |
| 66 | return protobuf; |
| 67 | } |
| 68 | |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 69 | std::vector<uint8_t> FileEntry::ToDBusFormat() const { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 70 | MtpFileEntry protobuf = ToProtobuf(); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 71 | int size = protobuf.ByteSize(); |
| 72 | std::vector<uint8_t> serialized_proto; |
| 73 | serialized_proto.resize(size); |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 74 | CHECK_GT(size, 0); |
| 75 | CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size)); |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 76 | return serialized_proto; |
| 77 | } |
| 78 | |
| 79 | // static |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 80 | std::vector<uint8_t> FileEntry::EmptyFileEntriesToDBusFormat() { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 81 | std::vector<FileEntry> dummy; |
| 82 | return FileEntriesToDBusFormat(dummy); |
| 83 | } |
| 84 | |
| 85 | // static |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 86 | std::vector<uint8_t> FileEntry::FileEntriesToDBusFormat( |
Ben Chan | 81757a0 | 2018-01-23 14:06:08 -0800 | [diff] [blame^] | 87 | const std::vector<FileEntry>& entries) { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 88 | MtpFileEntries protobuf; |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 89 | std::vector<uint8_t> serialized_proto; |
| 90 | |
| 91 | if (entries.empty()) |
| 92 | return serialized_proto; |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 93 | |
| 94 | for (size_t i = 0; i < entries.size(); ++i) { |
| 95 | MtpFileEntry entry_protobuf = entries[i].ToProtobuf(); |
| 96 | MtpFileEntry* added_entry = protobuf.add_file_entries(); |
| 97 | *added_entry = entry_protobuf; |
| 98 | } |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 99 | int size = protobuf.ByteSize(); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 100 | serialized_proto.resize(size); |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 101 | CHECK_GT(size, 0); |
| 102 | CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size)); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 103 | return serialized_proto; |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace mtpd |