home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kmimemagic.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  6.1 KB  |  219 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. /*
  21.  * KMimeMagic is inspired by the code of the
  22.  * Apache Web Server.
  23.  *
  24.  * Rewritten for KDE by Fritz Elfert
  25.  * fritz@kde.org
  26.  * Adaptations by Torben Weis <weis@kde.org>
  27.  * Fixes and documentation by David Faure <faure@kde.org>
  28.  */
  29.  
  30. #ifndef KMIMEMAGIC_H
  31. #define KMIMEMAGIC_H
  32.  
  33. #include <qstring.h>
  34. #include <kdelibs_export.h>
  35.  
  36. class KMimeMagic; // see below (read this one first)
  37.  
  38. /**
  39.  * @deprecated Use KMimeType::findByContent() instead
  40.  * May be removed in KDE 4.0.
  41.  * Returned by KMimeMagic @p find...Type methods.
  42.  *
  43.  * It contains the mimetype and the encoding of
  44.  * the file or buffer read.
  45.  */
  46. class KIO_EXPORT_DEPRECATED KMimeMagicResult
  47. {
  48. public:
  49.   KMimeMagicResult() { m_iAccuracy = 100; }
  50.   ~KMimeMagicResult() { }
  51.  
  52.   /**
  53.    * Retrieve the mimetype (e.g. "text/html") of the file or buffer parsed.
  54.    */
  55.   QString mimeType() const { return m_strMimeType; }
  56.   /**
  57.    * Retrieve the accuracy of the matching.
  58.    */
  59.   int accuracy() const { return m_iAccuracy; }
  60.   /**
  61.    * Returns whether the result is valid (i.e. mimetype not empty).
  62.    */
  63.   bool isValid() const { return !m_strMimeType.isEmpty(); }
  64.  
  65.   /////////////////
  66.   // Internal functions only
  67.   /////////////////
  68.   void setMimeType( const QString& _mime ) { m_strMimeType = _mime; }
  69.   void setAccuracy( int _accuracy ) { m_iAccuracy = _accuracy; }
  70.   void setInvalid() { m_strMimeType = QString::null; }
  71.  
  72. protected:
  73.   QString m_strMimeType;
  74.   int m_iAccuracy;
  75. };
  76.  
  77. /**
  78.  * @deprecated Use KMimeType::findByContent() instead
  79.  * May be removed in KDE 4.0.
  80.  * Determine auto-magically the type of file,
  81.  * not only by using its extension, but also by reading its contents.
  82.  *
  83.  *
  84.  * Unless specified otherwise, KMimeMagic uses
  85.  * $KDEDIR/share/mimelnk/magic for this purpose.
  86.  *
  87.  * To make KMimeMagic restore the 'atime' of a file after it opened it,
  88.  * add its directory in kmimemagicrc like:
  89.  * [Settings]
  90.  * atimeDirs=/tmp,/var/tmp,/home/dfaure/tmp
  91.  * This isn't done by default because it changes the 'ctime'.
  92.  * See kmimemagic.cpp for a full discussion on this issue.
  93.  *
  94.  * The basic usage of KMimeMagic is :
  95.  * @li Get a pointer to it, using KMimeMagic::self().
  96.  * @li Use it for any file or buffer you want, using one of the three
  97.  * @p find...Type() methods.
  98.  *
  99.  * The result is contained in the class KMimeMagicResult.
  100.  */
  101. class KIO_EXPORT_DEPRECATED KMimeMagic
  102. {
  103. public:
  104.   /**
  105.    * Create a parser and initialize it with the KDE-global data:
  106.    * the "magic" config file as well as the snippets from share/config/magic.
  107.    * @since 3.1
  108.    */
  109.   KMimeMagic();
  110.  
  111.   /**
  112.    * Create a parser and initialize it with the given config file.
  113.    */
  114.   KMimeMagic( const QString & configFile );
  115.  
  116.   /**
  117.    * Destroy the parser.
  118.    */
  119.   ~KMimeMagic();
  120.  
  121.   /**
  122.    * Merge an existing parse table with the data from the
  123.    * given file.
  124.    *
  125.    * @return @p true on success.
  126.    */
  127.   bool mergeConfig( const QString & configFile );
  128.  
  129.   /**
  130.    * Merge an existing parse table with the data from the
  131.    * given buffer.
  132.    *
  133.    * @return @p true on success.
  134.    */
  135.   bool mergeBufConfig(char *);
  136.  
  137.   /**
  138.    * Enable/Disable follow-links.
  139.    *
  140.    * (Default is disabled.)
  141.    */
  142.   void setFollowLinks( bool _enable );
  143.  
  144.   /**
  145.    * Try to find a MimeType for the given file.
  146.    *
  147.    * If no special
  148.    * MimeType is found, the default MimeType is returned.
  149.    * This function looks at the content of the file.
  150.    *
  151.    * @return A pointer to the result object. Do @em not delete the
  152.    *         result object. After another call to KMimeMagic
  153.    *         the returned result object changes its value
  154.    *         since it is reused by KMimeMagic.
  155.    */
  156.   KMimeMagicResult* findFileType( const QString & _filename );
  157.  
  158.   /**
  159.    * Same functionality as above, except data is not
  160.    * read from a file.
  161.    *
  162.    * Instead a buffer can be supplied which
  163.    * is examined.
  164.    *
  165.    * @return A pointer to the result object. Do @em not delete the
  166.    *         result object. After another call to KMimeMagic
  167.    *         the returned result object changes its value
  168.    *         since it is reused by KMimeMagic.
  169.    */
  170.   KMimeMagicResult* findBufferType( const QByteArray &p );
  171.  
  172.   /**
  173.    * Same functionality as findBufferType() but with
  174.    * additional capability of distinguishing between
  175.    * C-headers and C-Source.
  176.    *
  177.    * For this purpose this function looks
  178.    * at the extension of the filename. This means that 'filename'
  179.    * can be a filename on some FTP server, too.
  180.    *
  181.    * @return A pointer to the result object. Do @em not delete the
  182.    *         result object. After another call to KMimeMagic
  183.    *         the returned result object changes its value
  184.    *         since it is reused by KMimeMagic.
  185.    */
  186.   KMimeMagicResult * findBufferFileType( const QByteArray &, const QString & filename );
  187.  
  188.   /**
  189.    * Returns a pointer to the unique KMimeMagic instance in this process.
  190.    */
  191.   static KMimeMagic* self();
  192.  
  193. protected:
  194.   /**
  195.    * The result type.
  196.    */
  197.   KMimeMagicResult * magicResult;
  198.  
  199.   static void initStatic();
  200.   static KMimeMagic* s_pSelf;
  201.  
  202. private:
  203.   void init( const QString& configFile );
  204.  
  205.   bool bunused;
  206.   QString sunused;
  207.  
  208.   int parse_line(char *line, int *rule, int lineno);
  209.   int parse(char *, int);
  210.   int buff_apprentice(char*buff);
  211.   int apprentice(const QString &configFile);
  212.  
  213.   struct config_rec *conf; // this is also our "d pointer"
  214.   int iunused;
  215. };
  216.  
  217. #endif
  218.  
  219.