bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 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 <iostream> |
| 18 | #include <vector> |
| 19 | #include <sstream> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/statfs.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | #include <dirent.h> |
| 25 | #include <limits.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include <libgen.h> |
| 30 | |
| 31 | #include "btree.hpp" |
| 32 | #include "mtp.h" |
| 33 | #include "MtpDebug.h" |
| 34 | |
| 35 | |
| 36 | Node::Node() { |
| 37 | mtpid= -1; |
| 38 | path = ""; |
| 39 | left = NULL; |
| 40 | right = NULL; |
| 41 | parent = NULL; |
| 42 | mtpparentid = 0; |
| 43 | } |
| 44 | |
| 45 | void Node::setMtpid(int aMtpid) { mtpid = aMtpid; } |
| 46 | void Node::setPath(std::string aPath) { path = aPath; } |
| 47 | void Node::setLeft(Node* aLeft) { left = aLeft; } |
| 48 | void Node::setRight(Node* aRight) { right = aRight; } |
| 49 | void Node::setParent(Node* aParent) { parent = aParent; } |
| 50 | void Node::setMtpParentId(int id) { |
| 51 | mtpparentid = id; |
| 52 | MTPD("setting mtpparentid: %i on mtpid: %i\n", mtpparentid, mtpid); |
| 53 | } |
| 54 | int Node::Mtpid() { return mtpid; } |
| 55 | int Node::getMtpParentId() { return mtpparentid; } |
| 56 | std::string Node::getPath() { return path; } |
| 57 | Node* Node::Left() { return left; } |
| 58 | Node* Node::Right() { return right; } |
| 59 | Node* Node::Parent() { return parent; } |
| 60 | |
| 61 | uint64_t Node::getIntProperty(uint64_t property) { |
| 62 | for (unsigned index = 0; index < mtpProp.size(); ++index) { |
| 63 | if (mtpProp[index].property == property) |
| 64 | return mtpProp[index].valueInt; |
| 65 | } |
| 66 | MTPE("Node::getIntProperty failed to find property %x, returning -1\n", (unsigned)property); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | void Node::addProperty(uint64_t property, uint64_t valueInt, std::string valueStr, int dataType) { |
| 71 | MTPD("adding str property: %lld, valueInt: %lld, valueStr: %s, dataType: %d\n", property, valueInt, valueStr.c_str(), dataType); |
| 72 | struct mtpProperty prop; |
| 73 | prop.property = property; |
| 74 | prop.valueInt = valueInt; |
| 75 | prop.valueStr = valueStr; |
| 76 | prop.dataType = dataType; |
| 77 | mtpProp.push_back(prop); |
| 78 | } |
| 79 | |
| 80 | void Node::updateProperty(uint64_t property, uint64_t valueInt, std::string valueStr, int dataType) { |
| 81 | for (unsigned i = 0; i < mtpProp.size(); i++) { |
| 82 | if (mtpProp[i].property == property) { |
| 83 | mtpProp[i].valueInt = valueInt; |
| 84 | mtpProp[i].valueStr = valueStr; |
| 85 | mtpProp[i].dataType = dataType; |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | addProperty(property, valueInt, valueStr, dataType); |
| 90 | } |
| 91 | |
| 92 | std::vector<Node::mtpProperty>& Node::getMtpProps() { |
| 93 | return mtpProp; |
| 94 | } |
| 95 | |
| 96 | void Node::addProperties(int storageID, int parent_object) { |
| 97 | struct stat st; |
| 98 | int mFormat = 0; |
| 99 | uint64_t puid; |
| 100 | |
| 101 | std::string mtpidStr = static_cast<std::ostringstream*>( &(std::ostringstream() << mtpid) )->str(); |
| 102 | std::string storageIDStr = static_cast<std::ostringstream*>( &(std::ostringstream() << storageID) )->str(); |
| 103 | std::string puidStr = storageIDStr + mtpidStr; |
| 104 | if ( ! (std::istringstream(puidStr) >> puid) ) puid = 0; |
| 105 | stat(getPath().c_str(), &st); |
| 106 | std::string mtimeStr = static_cast<std::ostringstream*>( &(std::ostringstream() << st.st_mtime) )->str(); |
| 107 | std::string atimeStr = static_cast<std::ostringstream*>( &(std::ostringstream() << st.st_atime) )->str(); |
| 108 | if (S_ISDIR(st.st_mode)) { |
| 109 | mFormat = MTP_FORMAT_ASSOCIATION; // folder |
| 110 | } |
| 111 | else { |
| 112 | mFormat = MTP_FORMAT_UNDEFINED; // file |
| 113 | } |
| 114 | addProperty(MTP_PROPERTY_STORAGE_ID, storageID, "", MTP_TYPE_UINT32); |
| 115 | addProperty(MTP_PROPERTY_OBJECT_FORMAT, mFormat, "", MTP_TYPE_UINT16); |
| 116 | addProperty(MTP_PROPERTY_PROTECTION_STATUS, 0, "", MTP_TYPE_UINT16); |
| 117 | addProperty(MTP_PROPERTY_OBJECT_SIZE, st.st_size, "", MTP_TYPE_UINT64); |
| 118 | addProperty(MTP_PROPERTY_OBJECT_FILE_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 119 | MTPD("mtpid: %i, filename: '%s', parent object: %i\n", mtpid, basename(getPath().c_str()), parent_object); |
| 120 | addProperty(MTP_PROPERTY_DATE_MODIFIED, 0, mtimeStr, MTP_TYPE_STR); |
| 121 | addProperty(MTP_PROPERTY_PARENT_OBJECT, parent_object, "", MTP_TYPE_UINT32); |
| 122 | addProperty(MTP_PROPERTY_PERSISTENT_UID, puid, "", MTP_TYPE_UINT128); |
| 123 | addProperty(MTP_PROPERTY_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 124 | addProperty(MTP_PROPERTY_DISPLAY_NAME, 0, basename(getPath().c_str()), MTP_TYPE_STR); |
| 125 | addProperty(MTP_PROPERTY_DATE_ADDED, 0, atimeStr, MTP_TYPE_STR); |
| 126 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 127 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 128 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 129 | addProperty(MTP_PROPERTY_ALBUM_ARTIST, 0, "", MTP_TYPE_STR); |
| 130 | addProperty(MTP_PROPERTY_TRACK, 0, "", MTP_TYPE_UINT16); |
| 131 | addProperty(MTP_PROPERTY_ORIGINAL_RELEASE_DATE, 0, "00101T000000", MTP_TYPE_STR); |
| 132 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 133 | addProperty(MTP_PROPERTY_GENRE, 0, "", MTP_TYPE_STR); |
| 134 | addProperty(MTP_PROPERTY_COMPOSER, 0, "", MTP_TYPE_STR); |
| 135 | addProperty(MTP_PROPERTY_ARTIST, 0, "", MTP_TYPE_STR); |
| 136 | addProperty(MTP_PROPERTY_ALBUM_NAME, 0, "", MTP_TYPE_STR); |
| 137 | addProperty(MTP_PROPERTY_DURATION, 0, "", MTP_TYPE_UINT32); |
| 138 | addProperty(MTP_PROPERTY_DESCRIPTION, 0, "", MTP_TYPE_STR); |
| 139 | } |