• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ie_imp.h

Go to the documentation of this file.
00001 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
00002 
00003 /* AbiWord
00004  * Copyright (C) 1998 AbiSource, Inc.
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019  * 02111-1307, USA.
00020  */
00021 
00022 #ifndef IE_IMP_H
00023 #define IE_IMP_H
00024 
00025 #include "ut_types.h"
00026 #include "ie_types.h"
00027 
00028 #include <map>
00029 
00030 #include "ut_vector.h"
00031 #include "ut_string_class.h"
00032 #include "pd_Document.h"
00033 
00034 #include "ut_go_file.h"
00035 #include <gsf/gsf-input.h>
00036 
00037 #if defined(__MINGW32__)
00038 #undef snprintf
00039 #define _GLIBCXX_USE_C99_DYNAMIC 1
00040 #endif
00041 
00042 #include <string>
00043 #include <vector>
00044 
00045 class PD_Document;
00046 class IE_Imp;
00047 
00051 class ABI_EXPORT IE_ImpSniffer
00052 {
00053     friend class IE_Imp;
00054 
00055 public:
00056 
00057     virtual ~IE_ImpSniffer();
00058 
00059     // these you get for free
00060     inline bool supportsFileType (IEFileType type) {return m_type == type;}
00061     inline IEFileType getFileType() const {return m_type;}
00062 
00063     // these you must override these next 4 methods!!!
00064 
00070     virtual UT_Confidence_t recognizeContents (const char * szBuf,
00071                                                UT_uint32 iNumbytes);
00072 
00078     virtual UT_Confidence_t recognizeContents (GsfInput * input);
00079 
00084     virtual const IE_SuffixConfidence * getSuffixConfidence () = 0;
00085 
00090     virtual const IE_MimeConfidence * getMimeConfidence () = 0;
00091 
00092     virtual bool getDlgLabels (const char ** szDesc,
00093                    const char ** szSuffixList,
00094                    IEFileType * ft) = 0;
00095     virtual UT_Error constructImporter (PD_Document * pDocument,
00096                         IE_Imp ** ppie) = 0;
00097 
00098     const UT_UTF8String & name () const { return m_name; }
00099 
00100     bool getCanPaste () const {
00101         return m_bCanPaste;
00102     }
00103 protected:
00104     IE_ImpSniffer(const char * name, bool canPaste = false);
00105 
00106 private:
00107     const UT_UTF8String m_name;
00108 
00109     // only IE_Imp ever calls this
00110     inline void setFileType (IEFileType type) {m_type = type;}
00111     IEFileType m_type;
00112 
00113     bool m_bCanPaste;
00114 };
00115 
00116 class ABI_EXPORT IE_Imp
00117 {
00118 public:
00119 
00120     // constructs an importer of the right type based upon
00121     // either the filename or sniffing the file.  caller is
00122     // responsible for destroying the importer when finished
00123     // with it.
00124 
00125     static IEFileType   fileTypeForContents(const char * szBuf,
00126                             UT_uint32 iNumbytes);
00127 
00128     static IEFileType   fileTypeForSuffix(const char * szSuffix);
00129     static IEFileType   fileTypeForDescription(const char * szSuffix);
00130     static IEFileType   fileTypeForMimetype(const char * szMimetype);
00131 
00132     static IEFileType fileTypeForSuffixes(const char * suffixList);
00133 
00134     static IE_ImpSniffer * snifferForFileType(IEFileType ieft);
00135     static const char * suffixesForFileType(IEFileType ieft);
00136     static const char * descriptionForFileType(IEFileType ieft);
00137 
00138     static UT_Error constructImporter(PD_Document * pDocument,
00139                                       const char * szFilename,
00140                                       IEFileType ieft,
00141                                       IE_Imp ** ppie,
00142                                       IEFileType * pieft = NULL);
00143 
00144     static UT_Error constructImporter(PD_Document * pDocument,
00145                                       GsfInput * input,
00146                                       IEFileType ieft,
00147                                       IE_Imp ** ppie,
00148                                       IEFileType * pieft = NULL);
00149 
00150     static UT_Error constructImporter(PD_Document * pDocument,
00151                                       IEFileType ieft,
00152                                       IE_Imp ** ppie,
00153                                       IEFileType * pieft = NULL);
00154 
00155     static bool     enumerateDlgLabels(UT_uint32 ndx,
00156                            const char ** pszDesc,
00157                            const char ** pszSuffixList,
00158                            IEFileType * ft);
00159 
00160     static UT_uint32    getImporterCount(void);
00161     static void registerImporter (IE_ImpSniffer * sniffer);
00162     static void unregisterImporter (IE_ImpSniffer * sniffer);
00163     static void unregisterAllImporters ();
00164     static std::vector<std::string> & getSupportedMimeTypes ();
00165     static std::vector<std::string> & getSupportedMimeClasses ();
00166     static std::vector<std::string> & getSupportedSuffixes ();
00167     static const char * getMimeTypeForSuffix (const char * suffix);
00168 
00169     virtual ~IE_Imp();
00170 
00171     // kept around for backwards compat
00172     UT_Error importFile(const char * szFilename);
00173     UT_Error importFile(GsfInput * input);
00174 
00175     static UT_Error loadFile(PD_Document * doc, const char * szFilename, IEFileType ieft = IEFT_Unknown, const char * props = NULL, IEFileType * savedAsType = NULL);
00176     static UT_Error loadFile(PD_Document * doc, GsfInput * input, IEFileType ieft = IEFT_Unknown, const char * props = NULL, IEFileType * savedAsType = NULL);
00177 
00178     // default impl
00179     virtual  bool       pasteFromBuffer(PD_DocumentRange * pDocRange,
00180                         const unsigned char * pData,
00181                         UT_uint32 lenData,
00182                         const char * szEncoding = 0);
00183 
00184     PD_Document *           getDoc() const;
00185 
00186     void setLoadStylesOnly(bool b) {m_bStylesOnly = b;}
00187     bool getLoadStylesOnly() const {return m_bStylesOnly;}
00188     virtual bool supportsLoadStylesOnly() {return false;}
00189 
00190     void setLoadDocProps(bool b) {m_bDocProps = b;}
00191     bool getLoadDocProps() const {return m_bDocProps;}
00192 
00193     UT_Confidence_t getFidelity () const {
00194         return m_fidelity;
00195     }
00196 
00197     void setProps (const char * props);
00198     bool isPasting(void) const { return m_isPaste;}
00199 
00200  protected:
00201     IE_Imp(PD_Document * pDocument, UT_Confidence_t fidelity = 0);
00202 
00203     PT_DocPosition getDocPos() const;
00204     void setClipboard (PT_DocPosition dpos);
00205     bool isClipboard () const;
00206 
00207     virtual bool appendStrux (PTStruxType pts, const gchar ** attributes);
00208     virtual bool appendStruxFmt(pf_Frag_Strux * pfs, const gchar ** attributes);
00209     virtual bool appendSpan (const UT_UCSChar * p, UT_uint32 length);
00210     virtual bool appendSpan ( const std::string& s );
00211     virtual bool appendObject (PTObjectType pto, const gchar ** attribs,
00212                        const gchar ** props = NULL);
00213     virtual bool appendFmt(const gchar ** attributes);
00214     virtual bool appendFmt(const UT_GenericVector<const gchar*>* pVecAttributes);
00215 
00216     virtual UT_Error _loadFile (GsfInput * input) = 0;
00217 
00218 public:
00219     const std::string & getProperty (const char * key) {
00220         return m_props_map[key];
00221     }
00222 
00223  private:
00224     PD_Document * m_pDocument;
00225     bool m_isPaste;
00226     PT_DocPosition m_dpos;
00227     bool m_bStylesOnly;
00228     bool m_bDocProps;
00229 
00230     typedef std::map<std::string, std::string> map_type;
00231     map_type m_props_map;
00232 
00233     UT_Confidence_t m_fidelity;
00234 };
00235 
00236 class ABI_EXPORT GsfInputMarker
00237 {
00238     GsfInput *m_input;
00239     gsf_off_t m_position;
00240     bool m_reset;
00241 
00242     GsfInputMarker();
00243     GsfInputMarker(const GsfInputMarker & rhs);
00244     GsfInputMarker& operator=(const GsfInputMarker &rhs);
00245 
00246 public:
00247     GsfInputMarker(GsfInput * input)
00248         : m_input(input), m_position(gsf_input_tell(input)), m_reset(false)
00249     {
00250         g_object_ref(G_OBJECT(m_input));
00251     }
00252 
00253     ~GsfInputMarker()
00254     {
00255         reset();
00256         g_object_unref(G_OBJECT(m_input));
00257     }
00258 
00259     void reset()
00260     {
00261         if(!m_reset)
00262             {
00263                 gsf_input_seek(m_input, m_position, G_SEEK_SET);
00264                 m_reset = true;
00265             }
00266     }
00267 };
00268 
00269 
00270 #endif /* IE_IMP_H */

Generated on Sun May 27 2012 for AbiWord by  doxygen 1.7.1