// -*- C++ -*-
-/*
+/**
* \file GraphicsConverter.h
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * \author Angus Leeming <a.leeming@ic.ac.uk>
+ * \author Angus Leeming
*
- * class grfx::GConverter enables graphics files to be converted asynchronously
- * to a loadable format. It does this by building a shell script of all
- * the conversion commands needed for the transformation. This script is then
- * sent to the forked calls controller for non-blocking execution. When it
- * is finished a signal is emitted, thus informing us to proceed with the
- * loading of the image.
+ * Full author contact details are available in file CREDITS.
*
- * Ultimately, this class should be wrapped back into Dekel's converter class.
+ * The controller of a conversion process from file AA of format A to
+ * file BB of format B.
+ * Once finished, a signal is emitted to inform any listeners (connected
+ * through the connect() method).
*/
#ifndef GRAPHICSCONVERTER_H
#define GRAPHICSCONVERTER_H
-#include "LString.h"
-#include "Lsstream.h"
+#include <boost/signals2.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/utility.hpp>
+namespace lyx {
-#include <boost/signals/signal1.hpp>
-#include <boost/signals/trackable.hpp>
+namespace support { class FileName; }
-#include <list>
+namespace graphics {
-#include <sys/types.h> // needed for pid_t
-
-#ifdef __GNUG__
-#pragma interface
-#endif
-
-namespace grfx {
-
-class ConvProcess;
-
-class GConverter : boost::noncopyable {
+class Converter {
public:
-
- /// This is a singleton class. Get the instance.
- static GConverter & get();
-
/// Can the conversion be performed?
- bool isReachable(string const & from_format_name,
- string const & to_format_name) const;
+ static bool isReachable(std::string const & from_format_name,
+ std::string const & to_format_name);
- /** Convert the file and at the end return it by emitting this signal
- * If successful, the returned string will be the name of the
- * converted file (to_file_base + extension(to_format_name)).
- * If unsuccessful, the string will be empty.
+ /** One Converter per conversion ensures that the (hidden) signal
+ * is always connected to the expected slot.
*/
- typedef boost::signal1<void, string const &> SignalType;
- ///
- typedef boost::shared_ptr<SignalType> SignalTypePtr;
- ///
- void convert(string const & from_file, string const & to_file_base,
- string const & from_format, string const & to_format,
- SignalTypePtr on_finish);
+ Converter(support::FileName const & doc_fname,
+ support::FileName const & from_file, std::string const & to_file_base,
+ std::string const & from_format, std::string const & to_format);
-private:
- /** Make the c-tor private so we can control how many objects
- * are instantiated.
- */
- GConverter() {}
+ /// Needed for the pimpl
+ ~Converter();
- /** Build the conversion script, returning true if able to build it.
- * The script is output to the ostringstream 'script'.
- */
- bool build_script(string const & from_file, string const & to_file_base,
- string const & from_format, string const & to_format,
- ostringstream & script) const;
+ /// We are explicit about when we begin the conversion process.
+ void startConversion() const;
- /** Remove the ConvProcess from the list of all processes.
- * Called by ConvProcess::converted.
+ /** Connect and you'll be informed when the conversion process has
+ * finished.
+ * If the conversion is successful, then the listener is passed \c true.
*/
- friend class ConvProcess;
+ typedef boost::signals2::signal<void(bool)> sig_type;
+ typedef sig_type::slot_type slot_type;
///
- void erase(ConvProcess *);
+ boost::signals2::connection connect(slot_type const &) const;
- /// The list of all conversion processs
- typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
- ///
- std::list<ConvProcessPtr> all_processes_;
-};
-
-
-/// Each ConvProcess represents a single conversion process.
-struct ConvProcess : public boost::signals::trackable
-{
- ///
- typedef GConverter::SignalTypePtr SignalTypePtr;
-
- /** Each ConvProcess represents a single conversion process.
- * It is passed :
- * 1. The name of the script_file, which it deletes once the
- * conversion is comlpeted;
- * 2. The script command itself, which it passes on to the forked
- * call process;
- * 3. The name of the output file, which it returns to the calling
- * process on successfull completion, by emitting
- * 4. The signal on_finish.
+ /** If the conversion is successful, this returns the name of the
+ * resulting file.
+ * If conversion fails or has not been completed, however, it
+ * returns an empty string.
*/
- ConvProcess(string const & script_file, string const & script_command,
- string const & to_file, SignalTypePtr on_finish);
+ support::FileName const & convertedFile() const;
- /** This method is connected to a signal passed to the forked call
- * class, passing control back here when the conversion is completed.
- * Cleans-up the temporary files, emits the on_finish signal and
- * removes the ConvProcess from the list of all processes.
- */
- void converted(string const & cmd, pid_t pid, int retval);
-
- ///
- string script_file_;
- ///
- string to_file_;
- ///
- SignalTypePtr on_finish_;
+private:
+ /// noncopyable
+ Converter(Converter const &);
+ void operator=(Converter const &);
+
+ /// Use the Pimpl idiom to hide the internals.
+ class Impl;
+ /// The pointer never changes although *pimpl_'s contents may.
+ Impl * const pimpl_;
};
-} // namespace grfx
+} // namespace graphics
+} // namespace lyx
#endif // GRAPHICSCONVERTER_H