Various tinkerings.
[libdcp.git] / src / asset.cc
index 5ecc2e9f72c5b4f6072c3b8a9991baae9671ddb8..43db41e30cbb669f3b4b98c34eebdea5c9974648 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
 
     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
 */
 
 /** @file  src/asset.cc
- *  @brief Parent class for assets of DCPs.
+ *  @brief Asset class.
  */
 
-#include <iostream>
-#include <fstream>
-#include <boost/filesystem.hpp>
-#include "AS_DCP.h"
-#include "KM_util.h"
 #include "asset.h"
 #include "util.h"
-#include "metadata.h"
+#include <libxml++/libxml++.h>
+#include <boost/lexical_cast.hpp>
 
-using namespace std;
-using namespace boost;
-using namespace libdcp;
+using std::string;
+using boost::lexical_cast;
+using boost::function;
+using namespace dcp;
 
-Asset::Asset (string directory, string file_name)
-       : _directory (directory)
-       , _file_name (file_name)
-       , _uuid (make_uuid ())
+/** Create an Asset with a randomly-generated ID */
+Asset::Asset ()
 {
-       if (_file_name.empty ()) {
-               _file_name = _uuid + ".xml";
-       }
+
 }
 
-void
-Asset::write_to_pkl (ostream& s) const
+/** Create an Asset from a given file.  The ID will
+ *  be extracted from the file.
+ *  @param file File name.
+ */
+Asset::Asset (boost::filesystem::path file)
+       : _file (file)
 {
-       s << "    <Asset>\n"
-         << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
-         << "      <AnnotationText>" << _file_name << "</AnnotationText>\n"
-         << "      <Hash>" << digest() << "</Hash>\n"
-         << "      <Size>" << filesystem::file_size(path()) << "</Size>\n"
-         << "      <Type>application/mxf</Type>\n"
-         << "    </Asset>\n";
+
+}
+
+/** Create an Asset with a specified ID.
+ *  @param id ID to use.
+ */
+Asset::Asset (string id)
+       : Object (id)
+{
+
 }
 
 void
-Asset::write_to_assetmap (ostream& s) const
+Asset::write_to_pkl (xmlpp::Node* node) const
 {
-       s << "    <Asset>\n"
-         << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
-         << "      <ChunkList>\n"
-         << "        <Chunk>\n"
-         << "          <Path>" << _file_name << "</Path>\n"
-         << "          <VolumeIndex>1</VolumeIndex>\n"
-         << "          <Offset>0</Offset>\n"
-         << "          <Length>" << filesystem::file_size(path()) << "</Length>\n"
-         << "        </Chunk>\n"
-         << "      </ChunkList>\n"
-         << "    </Asset>\n";
+       assert (!_file.empty ());
+       
+       xmlpp::Node* asset = node->add_child ("Asset");
+       asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
+       asset->add_child("AnnotationText")->add_child_text (_id);
+       asset->add_child("Hash")->add_child_text (hash ());
+       asset->add_child("Size")->add_child_text (lexical_cast<string> (boost::filesystem::file_size (_file)));
+       asset->add_child("Type")->add_child_text (pkl_type ());
 }
 
-filesystem::path
-Asset::path () const
+void
+Asset::write_to_assetmap (xmlpp::Node* node) const
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= _file_name;
-       return p;
+       assert (!_file.empty ());
+
+       xmlpp::Node* asset = node->add_child ("Asset");
+       asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
+       xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
+       xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
+       chunk->add_child("Path")->add_child_text (_file.string ());
+       chunk->add_child("VolumeIndex")->add_child_text ("1");
+       chunk->add_child("Offset")->add_child_text ("0");
+       chunk->add_child("Length")->add_child_text (lexical_cast<string> (boost::filesystem::file_size (_file)));
 }
 
 string
-Asset::digest () const
+Asset::hash () const
+{
+       assert (!_file.empty ());
+               
+       if (!_hash.empty ()) {
+               _hash = make_digest (_file, 0);
+       }
+
+       return _hash;
+}
+
+bool
+Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, function<void (NoteType, string)> note) const
 {
-       if (_digest.empty ()) {
-               _digest = make_digest (path().string(), 0);
+       if (_hash != other->_hash) {
+               note (ERROR, "Asset hashes differ");
+               return false;
        }
 
-       return _digest;
+       return true;
 }