summaryrefslogtreecommitdiffstats
path: root/installerbuilder/common/utils.h
blob: a479e6f70b6d4a5200b60a71ad489e69229218b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**************************************************************************
**
** This file is part of Qt SDK**
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).*
**
** Contact:  Nokia Corporation qt-info@nokia.com**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception version
** 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you are unsure which license is appropriate for your use, please contact
** (qt-info@nokia.com).
**
**************************************************************************/
#ifndef QINSTALLER_UTILS_H
#define QINSTALLER_UTILS_H

#include "installer_global.h"

#include <QtCore/QBuffer>
#include <QtCore/QCryptographicHash>
#include <QtCore/QHash>
#include <QtCore/QUrl>
#include <QtCore/QTextStream>

#include <ostream>

QT_BEGIN_NAMESPACE
class QIODevice;
QT_END_NAMESPACE

namespace QInstaller
{
    QByteArray INSTALLER_EXPORT calculateHash( QIODevice* device, QCryptographicHash::Algorithm algo );

    QString INSTALLER_EXPORT replaceVariables( const QHash<QString,QString>& vars, const QString &str );
    QString INSTALLER_EXPORT replaceWindowsEnvironmentVariables( const QString &str );
    QStringList INSTALLER_EXPORT parseCommandLineArgs(int argc, char **argv);
#ifdef Q_OS_WIN
    QString createCommandline(const QString &program, const QStringList &arguments);
#endif

    void INSTALLER_EXPORT setVerbose( bool v );
    bool INSTALLER_EXPORT isVerbose();

    INSTALLER_EXPORT std::ostream& stdverbose();
    INSTALLER_EXPORT std::ostream& operator<<( std::ostream& os, const QUrl& url );
    INSTALLER_EXPORT std::ostream& operator<<( std::ostream& os, const QString& string );
    INSTALLER_EXPORT std::ostream& operator<<( std::ostream& os, const QByteArray& array );
    template< typename T >
    std::ostream& operator<<( std::ostream& os, const QList< T >& list )
    {
        os << "(";
        for( typename QList< T >::const_iterator it = list.begin(); it != list.end(); ++it )
            os << *it;
        os << ");";
        return os;
    }

    class VerboseWriter;
    INSTALLER_EXPORT VerboseWriter& verbose();

    class INSTALLER_EXPORT VerboseWriter : public QObject
    {
        Q_OBJECT
    public:
        VerboseWriter(QObject* parent = 0);
        ~VerboseWriter();

        static VerboseWriter* instance();

        inline VerboseWriter &operator<<(bool t) { stdverbose() << t; stream << (t ? "true" : "false"); return *this; }
        inline VerboseWriter &operator<<(int t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(qint64 t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(quint64 t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(double t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(std::string &t) { stdverbose() << t; stream << QString::fromStdString(t); return *this; }
        inline VerboseWriter &operator<<(const QByteArray &t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(const QString &t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(const QStringRef &t) { return verbose() << t.toString(); }
        inline VerboseWriter &operator<<(const QLatin1String &t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(const char *t) { stdverbose() << t; stream << t; return *this; }
        inline VerboseWriter &operator<<(const QUrl &t) { return verbose() << t.toString(); }
        template< typename T >
        VerboseWriter& operator<<(const QList< T >& list )
        {
            verbose() << "List ( ";
            for( typename QList< T >::const_iterator it = list.begin(); it != list.end(); ++it )
                verbose() << *it <<"; ";
            return verbose() << ");";
        }

        inline VerboseWriter &operator<<(std::ostream& (*f)(std::ostream &s)) { stdverbose() << *f; stream << "\n"; return *this; }
    public slots:
        void setOutputStream(const QString &fileName);

    private:
        QTextStream stream;
        QBuffer preFileBuffer;
        QString logFileName;
    };

}

#endif // QINSTALLER_UTILS_H