Merge changelog.
[dcpomatic.git] / src / lib / util.cc
index affbe3b002db82d96cbf2064a279e289f1cf01e5..e0495f18b495e5e16eb0e828e8318f765d455aa9 100644 (file)
@@ -45,6 +45,9 @@
 #include <magick/MagickCore.h>
 #include <magick/version.h>
 #include <libdcp/version.h>
+#include <libdcp/util.h>
+#include <libdcp/signer_chain.h>
+#include <libdcp/signer.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -61,6 +64,8 @@ extern "C" {
 #include "sound_processor.h"
 #include "config.h"
 #include "ratio.h"
+#include "job.h"
+#include "cross.h"
 #ifdef DCPOMATIC_WINDOWS
 #include "stack.hpp"
 #endif
@@ -273,6 +278,8 @@ dcpomatic_setup ()
 #endif 
        
        avfilter_register_all ();
+
+       libdcp::init ();
        
        Ratio::setup_ratios ();
        DCPContentType::setup_dcp_content_types ();
@@ -407,15 +414,24 @@ md5_digest (boost::filesystem::path file)
        return s.str ();
 }
 
+/** @param job Optional job for which to report progress */
 string
-md5_digest_directory (boost::filesystem::path directory)
+md5_digest_directory (boost::filesystem::path directory, shared_ptr<Job> job)
 {
        int const buffer_size = 64 * 1024;
        char buffer[buffer_size];
 
        MD5_CTX md5_context;
        MD5_Init (&md5_context);
-       
+
+       int files = 0;
+       if (job) {
+               for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
+                       ++files;
+               }
+       }
+
+       int j = 0;
        for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
                ifstream f (i->path().string().c_str(), std::ios::binary);
                if (!f.good ()) {
@@ -432,6 +448,11 @@ md5_digest_directory (boost::filesystem::path directory)
                        MD5_Update (&md5_context, buffer, t);
                        bytes -= t;
                }
+
+               if (job) {
+                       job->set_progress (float (j) / files);
+                       ++j;
+               }
        }
 
        unsigned char digest[MD5_DIGEST_LENGTH];
@@ -742,17 +763,17 @@ FrameRateConversion::FrameRateConversion (float source, int dcp)
        change_speed = !about_equal (source * factor(), dcp);
 
        if (!skip && !repeat && !change_speed) {
-               description = _("DCP and source have the same rate.\n");
+               description = _("Content and DCP have the same rate.\n");
        } else {
                if (skip) {
-                       description = _("DCP will use every other frame of the source.\n");
+                       description = _("DCP will use every other frame of the content.\n");
                } else if (repeat) {
-                       description = _("Each source frame will be doubled in the DCP.\n");
+                       description = _("Each content frame will be doubled in the DCP.\n");
                }
 
                if (change_speed) {
                        float const pc = dcp * 100 / (source * factor());
-                       description += String::compose (_("DCP will run at %1%% of the source speed.\n"), pc);
+                       description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc);
                }
        }
 }
@@ -775,3 +796,61 @@ LocaleGuard::~LocaleGuard ()
        setlocale (LC_NUMERIC, _old);
        free (_old);
 }
+
+bool
+valid_image_file (boost::filesystem::path f)
+{
+       string ext = f.extension().string();
+       transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
+       return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga");
+}
+
+string
+tidy_for_filename (string f)
+{
+       string t;
+       for (size_t i = 0; i < f.length(); ++i) {
+               if (isalnum (f[i]) || f[i] == '_' || f[i] == '-') {
+                       t += f[i];
+               } else {
+                       t += '_';
+               }
+       }
+
+       return t;
+}
+
+shared_ptr<const libdcp::Signer>
+make_signer ()
+{
+       boost::filesystem::path const sd = Config::instance()->signer_chain_directory ();
+       if (boost::filesystem::is_empty (sd)) {
+               libdcp::make_signer_chain (sd, openssl_path ());
+       }
+
+       libdcp::CertificateChain chain;
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "ca.self-signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "intermediate.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "leaf.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       boost::filesystem::path signer_key (sd);
+       signer_key /= "leaf.key";
+
+       return shared_ptr<const libdcp::Signer> (new libdcp::Signer (chain, signer_key));
+}
+