File indexing completed on 2024-06-16 04:33:38

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef _K3B_VERSION_H_
0007 #define _K3B_VERSION_H_
0008 
0009 #include "k3b_export.h"
0010 
0011 #include <QSharedDataPointer>
0012 #include <QString>
0013 
0014 namespace K3b {
0015     /**
0016      * \brief Representation of a version.
0017      *
0018      * Version represents a version consisting of a major version (accessible via majorVersion()),
0019      * a minor version (accessible via minorVersion()), a patchLevel (accessible via patchLevel()),
0020      * and a suffix (accessible via suffix()).
0021      *
0022      * The major version is mandatory while all other fields are optional (in case of the minor version
0023      * and the patchlevel -1 means that the field is undefined).
0024      *
0025      * Version tries to treat version suffixes in an "intelligent" way to properly compare versions
0026      * (see compareSuffix() for more details).
0027      *
0028      * Version may also be used everywhere a QString is needed as it automatically converts to a
0029      * string representation using createVersionString().
0030      */
0031     class LIBK3B_EXPORT Version
0032     {
0033     public:
0034         /**
0035          * construct an empty version object
0036          * which is invalid
0037          * @ see isValid()
0038          */
0039         Version();
0040 
0041         /**
0042          * copy constructor
0043          */
0044         Version( const Version& );
0045 
0046         /**
0047          * this constructor tries to parse the given version string
0048          */
0049         Version( const QString& version );
0050 
0051         /**
0052          * sets the version and generates a version string from it
0053          */
0054         Version( int majorVersion, int minorVersion, int pachlevel = -1, const QString& suffix = QString() );
0055 
0056         /**
0057          * Destructor
0058          */
0059         ~Version();
0060 
0061         /**
0062          * Copy operator
0063          */
0064         Version& operator=( const Version& );
0065 
0066         Version& operator=( const QString& v );
0067 
0068         /**
0069          * tries to parse the version string
0070          * used by the constructor
0071          */
0072         void setVersion( const QString& );
0073 
0074         bool isValid() const;
0075 
0076         /**
0077          * sets the version and generates a version string from it
0078          * used by the constructor
0079          *
0080          * If minorVersion or pachlevel are -1 they will not be used when generating the version string.
0081          */
0082         void setVersion( int majorVersion, int minorVersion = -1, int patchlevel = -1, const QString& suffix = QString() );
0083 
0084         QString toString() const;
0085         QString versionString() const;
0086         int majorVersion() const;
0087         int minorVersion() const;
0088         int patchLevel() const;
0089         QString suffix() const;
0090 
0091         /**
0092          * just to make it possible to use as a QString
0093          */
0094         operator QString() const { return toString(); }
0095 
0096         /**
0097          * \return A new Version object which equals this one except that the suffix is empty.
0098          */
0099         Version simplify() const;
0100 
0101         /**
0102          * If minorVersion or pachlevel are -1 they will not be used when generating the version string.
0103          * If minorVersion is -1 patchlevel will be ignored.
0104          */
0105         static QString createVersionString( int majorVersion,
0106                                             int minorVersion = -1,
0107                                             int patchlevel = -1,
0108                                             const QString& suffix = QString() );
0109 
0110         /**
0111          * "Intelligent" comparison of two version suffixes.
0112          *
0113          * This method checks for the following types of suffixes and treats them in the
0114          * following order:
0115          *
0116          * [empty prefix] > rcX > preX > betaX > alphaX = aX (where X is a number)
0117          *
0118          * Every other suffixes are compared alphanumerical.
0119          * An empty prefix is always considered newer than an unknown non-empty suffix (e.g. not one of the above.)
0120          *
0121          * @return \li -1 if suffix1 is less than suffix2
0122          *         \li 0 if suffix1 equals suffix2 (be aware that this is not the same as comparing to strings as
0123          *             alphaX equals aX in this case.)
0124          *         \li 1 if suffix1 is greater than suffix2
0125          */
0126         static int compareSuffix( const QString& suffix1, const QString& suffix2 );
0127 
0128     private:
0129         class Private;
0130         QSharedDataPointer<Private> d;
0131     };
0132 
0133 
0134     LIBK3B_EXPORT bool operator<( const Version& v1, const Version& v2 );
0135     LIBK3B_EXPORT bool operator>( const Version& v1, const Version& v2 );
0136     LIBK3B_EXPORT bool operator==( const Version& v1, const Version& v2 );
0137     LIBK3B_EXPORT bool operator<=( const Version& v1, const Version& v2 );
0138     LIBK3B_EXPORT bool operator>=( const Version& v1, const Version& v2 );
0139 }
0140 
0141 #endif