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 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 10 | namespace { |
| 11 | |
| 12 | MtpFileEntry_FileType LibmtpFileTypeToProtoFileType( |
| 13 | LIBMTP_filetype_t file_type) { |
| 14 | switch (file_type) { |
| 15 | case LIBMTP_FILETYPE_FOLDER: |
| 16 | case LIBMTP_FILETYPE_JPEG: |
| 17 | case LIBMTP_FILETYPE_JFIF: |
| 18 | case LIBMTP_FILETYPE_TIFF: |
| 19 | case LIBMTP_FILETYPE_BMP: |
| 20 | case LIBMTP_FILETYPE_GIF: |
| 21 | case LIBMTP_FILETYPE_PICT: |
| 22 | case LIBMTP_FILETYPE_PNG: |
| 23 | case LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT: |
| 24 | case LIBMTP_FILETYPE_JP2: |
| 25 | case LIBMTP_FILETYPE_JPX: |
| 26 | case LIBMTP_FILETYPE_UNKNOWN: |
| 27 | return static_cast<MtpFileEntry_FileType>(file_type); |
| 28 | default: |
| 29 | return MtpFileEntry_FileType_FILE_TYPE_OTHER; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 35 | namespace mtpd { |
| 36 | |
| 37 | FileEntry::FileEntry(const LIBMTP_file_struct& file) |
| 38 | : item_id_(file.item_id), |
| 39 | parent_id_(file.parent_id), |
| 40 | file_size_(file.filesize), |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 41 | modification_time_(file.modificationdate), |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 42 | file_type_(file.filetype) { |
| 43 | if (file.filename) |
| 44 | file_name_ = file.filename; |
| 45 | } |
| 46 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 47 | FileEntry::FileEntry() |
| 48 | : item_id_(kInvalidFileId), |
| 49 | parent_id_(kInvalidFileId), |
| 50 | file_size_(0), |
| 51 | modification_time_(0), |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 52 | file_type_(LIBMTP_FILETYPE_UNKNOWN) {} |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 53 | |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 54 | FileEntry::~FileEntry() {} |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 55 | |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 56 | MtpFileEntry FileEntry::ToProtobuf() const { |
| 57 | MtpFileEntry protobuf; |
| 58 | protobuf.set_item_id(item_id_); |
| 59 | protobuf.set_parent_id(parent_id_); |
| 60 | protobuf.set_file_name(file_name_); |
| 61 | protobuf.set_file_size(file_size_); |
| 62 | protobuf.set_modification_time(modification_time_); |
| 63 | protobuf.set_file_type(LibmtpFileTypeToProtoFileType(file_type_)); |
| 64 | return protobuf; |
| 65 | } |
| 66 | |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 67 | std::vector<uint8_t> FileEntry::ToDBusFormat() const { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 68 | MtpFileEntry protobuf = ToProtobuf(); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 69 | int size = protobuf.ByteSize(); |
| 70 | std::vector<uint8_t> serialized_proto; |
| 71 | serialized_proto.resize(size); |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 72 | CHECK_GT(size, 0); |
| 73 | CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size)); |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 74 | return serialized_proto; |
| 75 | } |
| 76 | |
| 77 | // static |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 78 | std::vector<uint8_t> FileEntry::EmptyFileEntriesToDBusFormat() { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 79 | std::vector<FileEntry> dummy; |
| 80 | return FileEntriesToDBusFormat(dummy); |
| 81 | } |
| 82 | |
| 83 | // static |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 84 | std::vector<uint8_t> FileEntry::FileEntriesToDBusFormat( |
Ben Chan | 81757a0 | 2018-01-23 14:06:08 -0800 | [diff] [blame] | 85 | const std::vector<FileEntry>& entries) { |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 86 | MtpFileEntries protobuf; |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 87 | std::vector<uint8_t> serialized_proto; |
| 88 | |
| 89 | if (entries.empty()) |
| 90 | return serialized_proto; |
Lei Zhang | fa9e19a | 2012-08-31 14:36:59 -0700 | [diff] [blame] | 91 | |
| 92 | for (size_t i = 0; i < entries.size(); ++i) { |
| 93 | MtpFileEntry entry_protobuf = entries[i].ToProtobuf(); |
| 94 | MtpFileEntry* added_entry = protobuf.add_file_entries(); |
| 95 | *added_entry = entry_protobuf; |
| 96 | } |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 97 | int size = protobuf.ByteSize(); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 98 | serialized_proto.resize(size); |
Lei Zhang | a357970 | 2014-01-15 10:06:39 -0800 | [diff] [blame] | 99 | CHECK_GT(size, 0); |
| 100 | CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size)); |
Lei Zhang | 6cd0bfc | 2012-09-06 20:18:26 -0700 | [diff] [blame] | 101 | return serialized_proto; |
Lei Zhang | 91af91d | 2012-08-03 11:02:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace mtpd |