Blame src/actions.hpp

Packit 01d647
// ***************************************************************** -*- C++ -*-
Packit 01d647
/*
Packit 01d647
 * Copyright (C) 2004-2018 Exiv2 authors
Packit 01d647
 * This program is part of the Exiv2 distribution.
Packit 01d647
 *
Packit 01d647
 * This program is free software; you can redistribute it and/or
Packit 01d647
 * modify it under the terms of the GNU General Public License
Packit 01d647
 * as published by the Free Software Foundation; either version 2
Packit 01d647
 * of the License, or (at your option) any later version.
Packit 01d647
 *
Packit 01d647
 * This program is distributed in the hope that it will be useful,
Packit 01d647
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 01d647
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 01d647
 * GNU General Public License for more details.
Packit 01d647
 *
Packit 01d647
 * You should have received a copy of the GNU General Public License
Packit 01d647
 * along with this program; if not, write to the Free Software
Packit 01d647
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
Packit 01d647
 */
Packit 01d647
/*!
Packit 01d647
  @file    actions.hpp
Packit 01d647
  @brief   Implements base class Task, TaskFactory and the various supported
Packit 01d647
           actions (derived from Task).
Packit 01d647
  @author  Andreas Huggel (ahu)
Packit 01d647
           ahuggel@gmx.net
Packit 01d647
  @date    11-Dec-03, ahu: created
Packit 01d647
 */
Packit 01d647
#ifndef ACTIONS_HPP_
Packit 01d647
#define ACTIONS_HPP_
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// included header files
Packit 01d647
#include "exiv2app.hpp"
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// class declarations
Packit 01d647
Packit 01d647
namespace Exiv2 {
Packit 01d647
    class ExifData;
Packit 01d647
    class Image;
Packit 01d647
    class Metadatum;
Packit 01d647
    class PreviewImage;
Packit 01d647
}
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// namespace extensions
Packit 01d647
/*!
Packit 01d647
  @brief Contains all action classes (task subclasses).
Packit 01d647
 */
Packit 01d647
namespace Action {
Packit 01d647
Packit 01d647
    //! Enumerates all tasks
Packit 01d647
    enum TaskType { none, adjust, print, rename, erase, extract, insert,
Packit 01d647
                    modify, fixiso, fixcom };
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// class definitions
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief Abstract base class for all concrete actions.
Packit 01d647
Packit 01d647
      Task provides a simple interface that actions must implement and a few
Packit 01d647
      commonly used helpers.
Packit 01d647
     */
Packit 01d647
    class Task {
Packit 01d647
    public:
Packit 01d647
        //! Shortcut for an auto pointer.
Packit 01d647
        typedef std::auto_ptr<Task> AutoPtr;
Packit 01d647
        //! Virtual destructor.
Packit 01d647
        virtual ~Task();
Packit 01d647
        //! Virtual copy construction.
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
        /*!
Packit 01d647
          @brief Application interface to perform a task.
Packit 01d647
Packit 01d647
          @param path Path of the file to process.
Packit 01d647
          @return 0 if successful.
Packit 01d647
         */
Packit 01d647
        virtual int run(const std::string& path) =0;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        //! Internal virtual copy constructor.
Packit 01d647
        virtual Task* clone_() const =0;
Packit 01d647
Packit 01d647
    }; // class Task
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief Task factory.
Packit 01d647
Packit 01d647
      Creates an instance of the task of the requested type.  The factory is
Packit 01d647
      implemented as a singleton, which can be accessed only through the static
Packit 01d647
      member function instance().
Packit 01d647
    */
Packit 01d647
    class TaskFactory {
Packit 01d647
    public:
Packit 01d647
        /*!
Packit 01d647
          @brief Get access to the task factory.
Packit 01d647
Packit 01d647
          Clients access the task factory exclusively through
Packit 01d647
          this method.
Packit 01d647
        */
Packit 01d647
        static TaskFactory& instance();
Packit 01d647
        //! Destructor
Packit 01d647
        void cleanup();
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief  Create a task.
Packit 01d647
Packit 01d647
          @param  type Identifies the type of task to create.
Packit 01d647
          @return An auto pointer that owns a task of the requested type.  If
Packit 01d647
                  the task type is not supported, the pointer is 0.
Packit 01d647
          @remark The caller of the function should check the content of the
Packit 01d647
                  returned auto pointer and take appropriate action (e.g., throw
Packit 01d647
                  an exception) if it is 0.
Packit 01d647
         */
Packit 01d647
        Task::AutoPtr create(TaskType type);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Register a task prototype together with its type.
Packit 01d647
Packit 01d647
          The task factory creates new tasks of a given type by cloning its
Packit 01d647
          associated prototype. Additional tasks can be registered.  If called
Packit 01d647
          for a type which already exists in the list, the corresponding
Packit 01d647
          prototype is replaced.
Packit 01d647
Packit 01d647
          @param type Task type.
Packit 01d647
          @param task Pointer to the prototype. Ownership is transferred to the
Packit 01d647
                 task factory. That's what the auto pointer indicates.
Packit 01d647
        */
Packit 01d647
        void registerTask(TaskType type, Task::AutoPtr task);
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        //! Prevent construction other than through instance().
Packit 01d647
        TaskFactory();
Packit 01d647
        //! Prevent copy construction: not implemented.
Packit 01d647
        TaskFactory(const TaskFactory& rhs);
Packit 01d647
Packit 01d647
        //! Pointer to the one and only instance of this class.
Packit 01d647
        static TaskFactory* instance_;
Packit 01d647
        //! Type used to store Task prototype classes
Packit 01d647
        typedef std::map<TaskType, Task*> Registry;
Packit 01d647
        //! List of task types and corresponding prototypes.
Packit 01d647
        Registry registry_;
Packit 01d647
Packit 01d647
    }; // class TaskFactory
Packit 01d647
Packit 01d647
    //! %Print the Exif (or other metadata) of a file to stdout
Packit 01d647
    class Print : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Print();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Print> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
        //! Print the Jpeg comment
Packit 01d647
        int printComment();
Packit 01d647
        //! Print list of available preview images
Packit 01d647
        int printPreviewList();
Packit 01d647
        //! Print Exif summary information
Packit 01d647
        int printSummary();
Packit 01d647
        //! Print Exif, IPTC and XMP metadata in user defined format
Packit 01d647
        int printList();
Packit 01d647
        //! Return true if key should be printed, else false
Packit 01d647
        bool grepTag(const std::string& key);
Packit 01d647
        //! Return true if key should be printed, else false
Packit 01d647
        bool keyTag(const std::string& key);
Packit 01d647
        //! Print all metadata in a user defined format
Packit 01d647
        int printMetadata(const Exiv2::Image* image);
Packit 01d647
        //! Print a metadatum in a user defined format, return true if something was printed
Packit 01d647
        bool printMetadatum(const Exiv2::Metadatum& md, const Exiv2::Image* image);
Packit 01d647
        //! Print the label for a summary line
Packit 01d647
        void printLabel(const std::string& label) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Print one summary line with a label (if provided) and requested
Packit 01d647
                 data. A line break is printed only if a label is provided.
Packit 01d647
          @return 1 if a line was written, 0 if the key was not found.
Packit 01d647
         */
Packit 01d647
        int printTag(const Exiv2::ExifData& exifData,
Packit 01d647
                     const std::string& key,
Packit 01d647
                     const std::string& label ="") const;
Packit 01d647
        //! Type for an Exiv2 Easy access function
Packit 01d647
        typedef Exiv2::ExifData::const_iterator (*EasyAccessFct)(const Exiv2::ExifData& ed);
Packit 01d647
        /*!
Packit 01d647
          @brief Print one summary line with a label (if provided) and requested
Packit 01d647
                 data. A line break is printed only if a label is provided.
Packit 01d647
          @return 1 if a line was written, 0 if the information was not found.
Packit 01d647
         */
Packit 01d647
        int printTag(const Exiv2::ExifData& exifData,
Packit 01d647
                     EasyAccessFct easyAccessFct,
Packit 01d647
                     const std::string& label) const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Print* clone_() const;
Packit 01d647
Packit 01d647
        std::string path_;
Packit 01d647
        int align_;                // for the alignment of the summary output
Packit 01d647
    }; // class Print
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Rename a file to its metadate creation timestamp,
Packit 01d647
             in the specified format.
Packit 01d647
     */
Packit 01d647
    class Rename : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Rename();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Rename> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Rename* clone_() const;
Packit 01d647
    }; // class Rename
Packit 01d647
Packit 01d647
    //! %Adjust the Exif (or other metadata) timestamps
Packit 01d647
    class Adjust : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Adjust();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Adjust> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Adjust* clone_() const;
Packit 01d647
        int adjustDateTime(Exiv2::ExifData& exifData,
Packit 01d647
                           const std::string& key,
Packit 01d647
                           const std::string& path) const;
Packit 01d647
Packit 01d647
        long adjustment_;
Packit 01d647
        long yearAdjustment_;
Packit 01d647
        long monthAdjustment_;
Packit 01d647
        long dayAdjustment_;
Packit 01d647
Packit 01d647
    }; // class Adjust
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Erase the entire exif data or only the thumbnail section.
Packit 01d647
     */
Packit 01d647
    class Erase : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Erase();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Erase> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Delete the thumbnail image, incl IFD1 metadata from the file.
Packit 01d647
         */
Packit 01d647
        int eraseThumbnail(Exiv2::Image* image) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Erase the complete Exif data block from the file.
Packit 01d647
         */
Packit 01d647
        int eraseExifData(Exiv2::Image* image) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Erase all Iptc data from the file.
Packit 01d647
         */
Packit 01d647
        int eraseIptcData(Exiv2::Image* image) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Erase Jpeg comment from the file.
Packit 01d647
         */
Packit 01d647
        int eraseComment(Exiv2::Image* image) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Erase XMP packet from the file.
Packit 01d647
         */
Packit 01d647
        int eraseXmpData(Exiv2::Image* image) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Erase ICCProfile from the file.
Packit 01d647
         */
Packit 01d647
        int eraseIccProfile(Exiv2::Image* image) const;
Packit 01d647
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Erase* clone_() const;
Packit 01d647
        std::string path_;
Packit 01d647
Packit 01d647
    }; // class Erase
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Extract the entire exif data or only the thumbnail section.
Packit 01d647
     */
Packit 01d647
    class Extract : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Extract();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Extract> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Write the thumbnail image to a file. The filename is composed by
Packit 01d647
                 removing the suffix from the image filename and appending
Packit 01d647
                 "-thumb" and the appropriate suffix (".jpg" or ".tif"), depending
Packit 01d647
                 on the format of the Exif thumbnail image.
Packit 01d647
         */
Packit 01d647
        int writeThumbnail() const;
Packit 01d647
        /*!
Packit 01d647
          @brief Write preview images to files.
Packit 01d647
         */
Packit 01d647
        int writePreviews() const;
Packit 01d647
        /*!
Packit 01d647
          @brief Write one preview image to a file. The filename is composed by
Packit 01d647
                 removing the suffix from the image filename and appending
Packit 01d647
                 "-preview<num>" and the appropriate suffix (".jpg" or ".tif"),
Packit 01d647
                 depending on the format of the Exif thumbnail image.
Packit 01d647
         */
Packit 01d647
        void writePreviewFile(const Exiv2::PreviewImage& pvImg, int num) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Write embedded iccProfile files.
Packit 01d647
         */
Packit 01d647
        int writeIccProfile(const std::string& path) const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Extract* clone_() const;
Packit 01d647
        std::string path_;
Packit 01d647
Packit 01d647
    }; // class Extract
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Insert the Exif data from corresponding *.exv files.
Packit 01d647
     */
Packit 01d647
    class Insert : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Insert();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Insert> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Insert a Jpeg thumbnail image from a file into file \em path.
Packit 01d647
                 The filename of the thumbnail is expected to be the image
Packit 01d647
                 filename (\em path) minus its suffix plus "-thumb.jpg".
Packit 01d647
         */
Packit 01d647
        int insertThumbnail(const std::string& path) const;
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Insert an XMP packet from a xmpPath into file \em path.
Packit 01d647
         */
Packit 01d647
        int insertXmpPacket(const std::string& path,const std::string& xmpPath) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Insert xmp from a DataBuf into file \em path.
Packit 01d647
         */
Packit 01d647
        int insertXmpPacket(const std::string& path,const Exiv2::DataBuf& xmpBlob,bool usePacket=false) const;
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Insert an ICC profile from iccPath into file \em path.
Packit 01d647
         */
Packit 01d647
        int insertIccProfile(const std::string& path,const std::string& iccPath) const;
Packit 01d647
        /*!
Packit 01d647
          @brief Insert an ICC profile from binary DataBuf into file \em path.
Packit 01d647
         */
Packit 01d647
        int insertIccProfile(const std::string& path,Exiv2::DataBuf& iccProfileBlob) const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Insert* clone_() const;
Packit 01d647
Packit 01d647
    }; // class Insert
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Modify the Exif data according to the commands in the
Packit 01d647
             modification table.
Packit 01d647
     */
Packit 01d647
    class Modify : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~Modify();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<Modify> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
        Modify() {}
Packit 01d647
        //! Apply modification commands to the \em pImage, return 0 if successful.
Packit 01d647
        static int applyCommands(Exiv2::Image* pImage);
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual Modify* clone_() const;
Packit 01d647
        //! Copy constructor needed because of AutoPtr member
Packit 01d647
        Modify(const Modify& /*src*/) : Task() {}
Packit 01d647
Packit 01d647
        //! Add a metadatum to \em pImage according to \em modifyCmd
Packit 01d647
        static int addMetadatum(Exiv2::Image* pImage,
Packit 01d647
                                const ModifyCmd& modifyCmd);
Packit 01d647
        //! Set a metadatum in \em pImage according to \em modifyCmd
Packit 01d647
        static int setMetadatum(Exiv2::Image* pImage,
Packit 01d647
                                const ModifyCmd& modifyCmd);
Packit 01d647
        //! Delete a metadatum from \em pImage according to \em modifyCmd
Packit 01d647
        static void delMetadatum(Exiv2::Image* pImage,
Packit 01d647
                                 const ModifyCmd& modifyCmd);
Packit 01d647
        //! Register an XMP namespace according to \em modifyCmd
Packit 01d647
        static void regNamespace(const ModifyCmd& modifyCmd);
Packit 01d647
Packit 01d647
    }; // class Modify
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief %Copy ISO settings from any of the Nikon makernotes to the
Packit 01d647
             regular Exif tag, Exif.Photo.ISOSpeedRatings.
Packit 01d647
     */
Packit 01d647
    class FixIso : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~FixIso();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<FixIso> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual FixIso* clone_() const;
Packit 01d647
        std::string path_;
Packit 01d647
Packit 01d647
    }; // class FixIso
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief Fix the character encoding of Exif UNICODE user comments.
Packit 01d647
             Decodes the comment using the auto-detected or specified
Packit 01d647
             character encoding and writes it back in UCS-2.
Packit 01d647
     */
Packit 01d647
    class FixCom : public Task {
Packit 01d647
    public:
Packit 01d647
        virtual ~FixCom();
Packit 01d647
        virtual int run(const std::string& path);
Packit 01d647
        typedef std::auto_ptr<FixCom> AutoPtr;
Packit 01d647
        AutoPtr clone() const;
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        virtual FixCom* clone_() const;
Packit 01d647
        std::string path_;
Packit 01d647
Packit 01d647
    }; // class FixCom
Packit 01d647
Packit 01d647
}                                       // namespace Action
Packit 01d647
Packit 01d647
#endif                                  // #ifndef ACTIONS_HPP_