diff options
Diffstat (limited to 'installerbuilder/libinstaller/kdtools/KDToolsCore')
47 files changed, 4990 insertions, 0 deletions
diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDAutoPointer b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDAutoPointer new file mode 100644 index 000000000..5203ea2db --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDAutoPointer @@ -0,0 +1 @@ +#include "kdautopointer.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDByteSize b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDByteSize new file mode 100644 index 000000000..e0ee65f30 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDByteSize @@ -0,0 +1 @@ +#include "kdbytesize.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDGenericFactory b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDGenericFactory new file mode 100644 index 000000000..42f06640a --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDGenericFactory @@ -0,0 +1 @@ +#include "kdgenericfactory.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDJob b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDJob new file mode 100644 index 000000000..79fd1dde9 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDJob @@ -0,0 +1 @@ +#include "kdjob.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDLockfile b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDLockfile new file mode 100644 index 000000000..afd9f7ad1 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDLockfile @@ -0,0 +1 @@ +#include "kdlockfile.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDMetaMethodIterator b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDMetaMethodIterator new file mode 100644 index 000000000..cfb58d054 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDMetaMethodIterator @@ -0,0 +1 @@ +#include "kdmetamethoditerator.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDRunOnceChecker b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDRunOnceChecker new file mode 100644 index 000000000..d5ccad881 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDRunOnceChecker @@ -0,0 +1 @@ +#include "kdrunoncechecker.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSaveFile b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSaveFile new file mode 100644 index 000000000..f7df3f1b5 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSaveFile @@ -0,0 +1 @@ +#include "kdsavefile.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSelfRestarter b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSelfRestarter new file mode 100644 index 000000000..7de1c2779 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSelfRestarter @@ -0,0 +1 @@ +#include "kdselfrestarter.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSysInfo b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSysInfo new file mode 100644 index 000000000..7d5cec1e4 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDSysInfo @@ -0,0 +1 @@ +#include "kdsysinfo.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDToolsCore.pri b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDToolsCore.pri new file mode 100644 index 000000000..213c8343d --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDToolsCore.pri @@ -0,0 +1,42 @@ +DEPENDPATH += $$PWD +INCLUDEPATH += $$PWD + +CONFIG( shared, static|shared) { + DEFINES += BUILD_SHARED_KDTOOLSCORE +} + +HEADERS += $$PWD/pimpl_ptr.h \ + $$PWD/kdtoolsglobal.h \ + $$PWD/kdbytesize.h \ + $$PWD/kdjob.h \ + $$PWD/kdwatchdog.h \ + $$PWD/kdgenericfactory.h \ + $$PWD/kdautopointer.h \ + $$PWD/kdselfrestarter.h \ + $$PWD/kdsavefile.h \ + $$PWD/kdmetamethoditerator.h \ + $$PWD/kdrunoncechecker.h \ + $$PWD/kdlockfile.h \ + $$PWD/kdsysinfo.h \ + $$PWD/kdversion.h + +SOURCES += $$PWD/pimpl_ptr.cpp \ + $$PWD/kdtoolsglobal.cpp \ + $$PWD/kdbytesize.cpp \ + $$PWD/kdjob.cpp \ + $$PWD/kdwatchdog.cpp \ + $$PWD/kdgenericfactory.cpp \ + $$PWD/kdautopointer.cpp \ + $$PWD/kdselfrestarter.cpp \ + $$PWD/kdsavefile.cpp \ + $$PWD/kdmetamethoditerator.cpp \ + $$PWD/kdrunoncechecker.cpp \ + $$PWD/kdlockfile.cpp \ + $$PWD/kdsysinfo.cpp \ + $$PWD/kdversion.cpp + +unix:SOURCES += $$PWD/kdlockfile_unix.cpp +win32:SOURCES += $$PWD/kdlockfile_win.cpp +win32:SOURCES += $$PWD/kdsysinfo_win.cpp +macx:SOURCES += $$PWD/kdsysinfo_mac.cpp +unix:!macx:SOURCES += $$PWD/kdsysinfo_x11.cpp diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/KDWatchdog b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDWatchdog new file mode 100644 index 000000000..3032c2a37 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/KDWatchdog @@ -0,0 +1 @@ +#include "kdwatchdog.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.cpp new file mode 100644 index 000000000..3398ac8b8 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.cpp @@ -0,0 +1,577 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdautopointer.h" + +/*! + \class KDAutoPointer + \ingroup core smartptr + \brief Owning version of QPointer + \since_c 2.1 + + (The exception safety of this class has not been evaluated yet.) + + KDAutoPointer is a fusion of std::auto_ptr and QPointer, ie. it owns the + contained object, but if that object is otherwise deleted, it sets + itself to null, just like QPointer does. + + Like std::auto_ptr, ownership is never shared between instances of + KDAutoPointer. Copying and copy assignment \em transfer ownership, + ie. in the following code + + \code + KDAutoPointer<MyDialog> myDialog( new MyDialog ); + KDAutoPointer<QObject> monitor( myDialog ); // Probably WRONG! + \endcode + + \c monitor will end up owning the \c MyDialog instance, and \c + myDialog will be NULL, which is probably not what the programmer + intended. + + Like QPointer, KDAutoPointer can only hold objects of a class derived + from QObject. If you need this functionality for arbitrary types, + consider using std::tr1::shared_ptr together with + std::tr1::weak_ptr. + + \section overview Overview + + You construct a KDAutoPointer by passing a pointer to the object to be + owned in \link KDAutoPointer(T*) the constructor\endlink, or, after the + fact, by calling reset(). Neither of these operations happen + implicitly, since they entail a change of ownership that should be + visible in code. + + For the same reason, if you need to get a pointer to the contained + (owned) object, the conversion is not implicit, either. You have to + explicitly call get() (if you want to leave ownership to the + KDAutoPointer) or release() (if you want to transfer ownership out of + KDAutoPointer). + + Use \link swap(KDAutoPointer&,KDAutoPointer&) swap()\endlink to quickly + exchange the contents of two \link KDAutoPointer KDAutoPointers\endlink, and + operator unspecified_bool_type() to test whether KDAutoPointer (still) + contains an object: + + \code + KDAutoPointer w1( new QWidget ), w2(); + assert( w1 ); assert( !w2 ); + swap( w1, s2 ); + assert( !w1 ); assert( w2 ); + \endcode + + \section general-use General Use + + The general use case of KDAutoPointer is that of holding objects that + have unclear ownership semantics. E.g. classes that implement the + Command Pattern are sometimes implemented as deleting themselves + after the command finished. By holding instances of such classes in + KDAutoPointers, you do not have to care about whether or now the object + deletes itself. If it deletes itself, then KDAutoPointer will set itself + to NULL, if not, KDAutoPointer will delete the object when it goes out + of scope. + + \section child-creation Esp.: Child Creation + + You can also use KDAutoPointer for temporarily storing \link QObject + QObjects\endlink for exception safety. This is esp. useful when + following the Trolltech-recommended style of dialog creation (create + parent-less children up-front, add to layouts later): + + \code + QLabel * lb = new QLabel( tr("Name:" ); + QLineEdit * le = new QLineEdit; + + QHBoxLayout * hbox = new QHBoxLayout; + hbox->addWidget( lb ); + hbox->addWidget( le ); + // if somethings throws an exception here, + // all of the above will be leaked + setLayout( hbox ); + \endcode + + Rewriting this as (granted, cumbersome): + \code + KDAutoPointer<QLabel> lb( new QLabel( tr("Name:" ); + KDAutoPointer<QLineEdit> le( new QLineEdit ); + + KDAutoPointer<QHBoxLayout> hbox( new QHBoxLayout ); + hbox->addWidget( lb.get() ); + hbox->addWidget( le.get() ); + // if somethings throws an exception here, + // all of the above will be properly cleaned up + setLayout( hbox.release() ); + lb.release(); + le.release(); + \endcode + takes care of the leak. + + Since this is so cumbersome, you should instead always pass parents + to children as part of construction. + + \section model-dialogs Esp.: Modal Dialogs + + A more specific use case is (modal) dialogs. + + Traditionally, most programmers allocate these on the stack: + + \code + MyDialog dialog( parent ); + // set up 'dialog'... + if ( !dialog.exec() ) + return; + // extract information from 'dialog'... + return; + \endcode + + This is usually ok. Even though \c dialog has a parent, the parent + will usually live longer than the dialog. The dialog is modal, after + all, so the user is not supposed to be able to quit the application + while that dialog is on screen. + + There are some applications, however, that have other ways to + terminate than explicit user request. Whether the application exits + timer-driven, or by an IPC call, what will happen is that \c dialog + is deleted as a child of \c parent \em and \em then \em again by the + compiler as part of cleaning up the stack once \c exec() returns. + + This is where KDAutoPointer comes in. Instead of allocating modal + dialogs on the stack, allocate them into a KDAutoPointer: + + \code + const KDAutoPointer<MyDialog> dialog( new MyDialog( parent ) ); + // set up 'dialog'... + if ( !dialog->exec() || !dialog ) + return; + // extract information from 'dialog'... + return; + \endcode + + Note the use of \c const to prevent accidental copying of \c dialog, + and the additional check for \c dialog to catch the case where after + exec() returns, the dialog has been deleted. +*/ + +/*! + \typedef KDAutoPointer::element_type + + A typedef for \c T. For STL compatibility. +*/ + +/*! + \typedef KDAutoPointer::value_type + + A typedef for \c T. For STL compatibility. +*/ + +/*! + \typedef KDAutoPointer<T>::pointer + + A typedef for \c T*. For STL compatibility. +*/ + +/*! + \fn KDAutoPointer::KDAutoPointer() + + Default constructor. Constructs a NULL KDAutoPointer. + + \post get() == 0 +*/ + +/*! + \fn KDAutoPointer::KDAutoPointer( T * obj ) + + Constructor. Constructs a KDAutoPointer that contains (owns) \a obj. + + \post get() == obj +*/ + +/*! + \fn KDAutoPointer::KDAutoPointer( KDAutoPointer & other ) + + Move constructor. Constructs a KDAutoPointer that takes over ownership + of the object contained (owned) by \a other. + + \post this->get() == (the object previously owned by 'other') + \post other.get() == 0 +*/ + +/*! + \fn KDAutoPointer::KDAutoPointer( KDAutoPointer<U> & other ) + + \overload +*/ + +/*! + \fn KDAutoPointer & KDAutoPointer::operator=( KDAutoPointer & other ) + + Copy assignment operator. Equivalent to + \code + reset( other.release() ); + \endcode + + \post this->get() == (the object previously owned by 'other') + \post other.get() == 0 + \post The object previously owned by \c *this has been deleted. +*/ + +/*! + \fn KDAutoPointer & KDAutoPointer::operator=( KDAutoPointer<U> & other ) + + \overload +*/ + +/*! + \fn KDAutoPointer::~KDAutoPointer() + + Destructor. + + \post The object previously owned by \c *this has been deleted. +*/ + +/*! + \fn void KDAutoPointer::swap( KDAutoPointer & other ) + + Swaps the contents of \a *this and \a other. You probably want to + use the free function swap(KDAutoPointer&,KDAutoPointer&) instead. Also + qSwap() has been specialized to use this function internally. + + \post get() == (previous value of other.get()) + \post other.get() == (previous value of this->get()) +*/ + +/*! + \fn T * KDAutoPointer::get() const + + \returns a pointer to the contained (owned) object. +*/ + +/*! + \fn T * KDAutoPointer::release() + + Yields ownership of the contained object and returns a pointer to it. + + \post get() == 0 + \post The object previously owned by \c *this had \em not been deleted. +*/ + +/*! + \fn T * KDAutoPointer::data() const + + Equivalent to get(). Provided for consistency with Qt naming + conventions. +*/ + +/*! + \fn T * KDAutoPointer::take() + + Equivalent to release(). Provided for consistency with Qt naming + conventions. +*/ + +/*! + \fn void KDAutoPointer::reset( T * other ) + + Yields ownership of the contained object (if any) and and takes over + ownership of \a other. + + \post get() == other + \post The object previously owned by \c *this has been deleted. +*/ + +/*! + \fn T & KDAutoPointer::operator*() const + + Dereference operator. Returns \link get() *get()\endlink. +*/ + +/*! + \fn T * KDAutoPointer::operator->() const + + Member-by-pointer operator. Returns get(). +*/ + +/*! + \fn bool KDAutoPointer::operator==( const KDAutoPointer<S> & other ) const + + Equal-to operator. Returns get() == other.get(). + + \note Due to the fact that two \link KDAutoPointer KDAutoPointers\endlink + cannot contain the same object, the relational operators are not + very useful. +*/ + +/*! + \fn bool KDAutoPointer::operator!=( const KDAutoPointer<S> & other ) const + + Not-Equal-to operator. Returns get() != other.get(). + + \note Due to the fact that two \link KDAutoPointer KDAutoPointers\endlink + cannot contain the same object, the relational operators are not + very useful. +*/ + +/*! + \fn KDAutoPointer::operator unspecified_bool_type() const + + \returns \c true if \c *this contains an object, otherwise \c false. +*/ + +/*! + \fn void swap( KDAutoPointer<T> & lhs, KDAutoPointer<T> & rhs ) + \relates KDAutoPointer + + Equivalent to + \code + lhs.swap( rhs ); + \endcode + + Provided for generic code that uses the standard swap idiom: + \code + using std::swap; + swap( a, b ); + \endcode + to find through <a href="http://en.wikipedia.org/wiki/Argument_dependent_name_lookup">ADL</a>, + enabling such code to automatically use the more efficient + \link KDAutoPointer::swap() member swap\endlink instead of the default + implementation (which uses a temporary). +*/ + +/*! + \fn void qSwap( KDAutoPointer<T> & lhs, KDAutoPointer<S> & rhs ) + \relates KDAutoPointer + + Equivalent to + \code + swap( lhs, rhs ); + \endcode + except for code using %qSwap() instead of (std::)swap. +*/ + +/*! + \fn bool operator==( const KDAutoPointer<S> & lhs, const T * rhs ) + \relates KDAutoPointer + + Equal-to operator. Equivalent to + \code + lhs.get() == rhs + \endcode +*/ + +/*! + \fn bool operator==( const S * lhs, const KDAutoPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + +/*! + \fn bool operator==( const KDAutoPointer<S> & lhs, const QPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + +/*! + \fn bool operator==( const QPointer<S> & lhs, const KDAutoPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + + +/*! + \fn bool operator!=( const KDAutoPointer<S> & lhs, const T * rhs ) + \relates KDAutoPointer + + Not-equal-to operator. Equivalent to + \code + lhs.get() != rhs + \endcode +*/ + +/*! + \fn bool operator!=( const S * lhs, const KDAutoPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + +/*! + \fn bool operator!=( const KDAutoPointer<S> & lhs, const QPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + +/*! + \fn bool operator!=( const QPointer<S> & lhs, const KDAutoPointer<T> & rhs ) + \relates KDAutoPointer + \overload +*/ + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/Test> + +namespace { + + struct A : QObject {}; + struct B : QObject {}; + +} + +KDAB_UNITTEST_SIMPLE( KDAutoPointer, "kdcoretools" ) { + + { + QPointer<QObject> o; + { + const KDAutoPointer<QObject> qobject( new QObject ); + o = qobject.get(); + } + assertNull( o ); + } + + { + // check that conversion to bool works + KDAutoPointer<A> a( new A ); + const bool b1 = a; + assertTrue( b1 ); + const bool b2 = !a; + assertFalse( b2 ); + a.reset(); + const bool b3 = a; + assertFalse( b3 ); + const bool b4 = !a; + assertTrue( b4 ); + } + + { + KDAutoPointer<QObject> o; + assertNull( o.get() ); + assertFalse( o ); + QObject * obj; + o.reset( obj = new QObject ); + assertNotNull( o.get() ); + assertTrue( o ); + assertEqual( o.get(), obj ); + + KDAutoPointer<QObject> o2 = o; + assertNull( o.get() ); + assertNotNull( o2.get() ); + assertEqual( o2.get(), obj ); + + delete obj; + assertNull( o2.get() ); + } + + { + QPointer<QObject> obj = new QObject; + KDAutoPointer<QObject> o( obj ); + o = o; // self-assignment + assertNotNull( obj ); + o.reset( o.get() ); // self-assignment-ish + assertNotNull( obj ); + + o.reset( new QObject ); + assertNull( obj ); + assertNotNull( o.get() ); + } + + { + QPointer<A> ap; + { + KDAutoPointer<A> a( new A ); + ap = a.get(); + KDAutoPointer<QObject> o1( a ); + assertNull( a.get() ); + assertNotNull( o1.get() ); + KDAutoPointer<QObject> o2; + o2 = a; + // doesn't compile (but doesn't compile for std::auto_ptr, + // either, so it's ok, I think + //KDAutoPointer<QObject> o3 = a + } + assertNull( ap ); + } + + { + // test swapping + KDAutoPointer<A> a1( new A ), a2( new A ); + + A * A1 = a1.get(); + A * A2 = a2.get(); + + using std::swap; + swap( a1, a2 ); + + assertNotNull( a1.get() ); + assertNotNull( a2.get() ); + + assertEqual( a1.get(), A2 ); + assertEqual( a2.get(), A1 ); + + qSwap( a1, a2 ); + assertEqual( a1.get(), A1 ); + assertEqual( a2.get(), A2 ); + + } + + { + KDAutoPointer<A> apa( new A ); + QPointer<A> qpa( apa.get() ); + A * pa = apa.get(); + const A * cpa = pa; + + assertEqual( apa.get(), apa.get() ); + assertEqual( apa.get(), qpa ); + assertEqual( apa.get(), pa ); + assertEqual( apa.get(), cpa ); + assertEqual( qpa, apa.get() ); + assertEqual( pa, apa.get() ); + assertEqual( cpa, apa.get() ); + + KDAutoPointer<QObject> apq; cpa = pa = qpa = 0; + + assertNotEqual( apa.get(), qpa ); + assertNotEqual( apa.get(), pa ); + assertNotEqual( apa.get(), cpa ); + assertNotEqual( qpa, apa.get() ); + assertNotEqual( pa, apa.get() ); + assertNotEqual( cpa, apa.get() ); + + assertNotEqual( apq.get(), apa.get() ); + assertNotEqual( apa.get(), apq.get() ); + + assertNotNull( apa.get() ); + + apq.reset( new QObject ); + + assertNotEqual( apa.get(), qpa ); + assertNotEqual( apa.get(), pa ); + assertNotEqual( apa.get(), cpa ); + assertNotEqual( qpa, apa.get() ); + assertNotEqual( pa, apa.get() ); + assertNotEqual( cpa, apa.get() ); + + assertNotEqual( apq.get(), apa.get() ); + assertNotEqual( apa.get(), apq.get() ); + + assertNotNull( apa.get() ); + } +} + +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.h new file mode 100644 index 000000000..7a0558db0 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdautopointer.h @@ -0,0 +1,147 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLS_CORE_KDAUTOPOINTER_H__ +#define __KDTOOLS_CORE_KDAUTOPOINTER_H__ + +#include <KDToolsCore/kdtoolsglobal.h> + +#include <QtCore/QPointer> +#include <QtCore/QObject> + +class KDAutoPointerBase { +protected: + QPointer<QObject> o; + + KDAutoPointerBase() : o() {} + explicit KDAutoPointerBase( QObject * obj ) : o( obj ) {} + KDAutoPointerBase( KDAutoPointerBase & other ) : o( other.release() ) {} + ~KDAutoPointerBase() { delete o; } + + void swap( KDAutoPointerBase & other ) { + const QPointer<QObject> copy( o ); + o = other.o; + other.o = copy; + } + + QObject * release() { QObject * copy = o; o = 0 ; return copy; } + void reset( QObject * other ) { if ( o != other ) { delete o; o = other; } } + + KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR( o ); +}; + +template <typename T> +class MAKEINCLUDES_EXPORT KDAutoPointer : KDAutoPointerBase { +public: + friend inline void swap( KDAutoPointer & lhs, KDAutoPointer & rhs ) { lhs.swap( rhs ); } + + typedef T element_type; + typedef T value_type; + typedef T * pointer; + + KDAutoPointer() : KDAutoPointerBase() {} + explicit KDAutoPointer( T * obj ) : KDAutoPointerBase( obj ) {} + KDAutoPointer( KDAutoPointer & other ) : KDAutoPointerBase( other.release() ) {} + template <typename U> + KDAutoPointer( KDAutoPointer<U> & other ) : KDAutoPointerBase( other.release() ) { T * t = other.get(); ( void )t; } + + KDAutoPointer & operator=( KDAutoPointer & other ) { + this->reset( other.release() ); + return *this; + } + + template <typename U> + KDAutoPointer & operator=( KDAutoPointer<U> & other ) { + this->reset( other.release() ); + return *this; + } + + ~KDAutoPointer() {} + + void swap( KDAutoPointer & other ) { + KDAutoPointerBase::swap( other ); + } + + T * get() const { QObject * obj = o; return static_cast<T*>( obj ); } + T * release() { return static_cast<T*>( KDAutoPointerBase::release() ); } + + T * data() const { return get(); } + T * take() { return release(); } + + void reset( T * other=0 ) { KDAutoPointerBase::reset( other ); } + + T & operator*() const { return *get(); } + T * operator->() const { return get(); } + + template <typename S> + bool operator==( const KDAutoPointer<S> & other ) const { + return get() == other.get(); + } + template <typename S> + bool operator!=( const KDAutoPointer<S> & other ) const { + return get() != other.get(); + } + + KDAB_USING_SAFE_BOOL_OPERATOR( KDAutoPointerBase ) +}; + +template <typename T> +inline void swap( KDAutoPointer<T> & lhs, KDAutoPointer<T> & rhs ) { lhs.swap( rhs ); } +template <typename T> +inline void qSwap( KDAutoPointer<T> & lhs, KDAutoPointer<T> & rhs ) { lhs.swap( rhs ); } + +template <typename S, typename T> +inline bool operator==( const KDAutoPointer<S> & lhs, const T * rhs ) { + return lhs.get() == rhs; +} +template <typename S, typename T> +inline bool operator==( const S * lhs, const KDAutoPointer<T> & rhs ) { + return lhs == rhs.get(); +} +template <typename S, typename T> +inline bool operator==( const KDAutoPointer<S> & lhs, const QPointer<T> & rhs ) { + return lhs.get() == rhs; +} +template <typename S, typename T> +inline bool operator==( const QPointer<S> & lhs, const KDAutoPointer<T> & rhs ) { + return lhs == rhs.get(); +} + +template <typename S, typename T> +inline bool operator!=( const KDAutoPointer<S> & lhs, const T * rhs ) { + return !operator==( lhs, rhs ); +} +template <typename S, typename T> +inline bool operator!=( const S * lhs, const KDAutoPointer<T> & rhs ) { + return !operator==( lhs, rhs ); +} +template <typename S, typename T> +inline bool operator!=( const KDAutoPointer<S> & lhs, const QPointer<T> & rhs ) { + return !operator==( lhs, rhs ); +} +template <typename S, typename T> +inline bool operator!=( const QPointer<S> & lhs, const KDAutoPointer<T> & rhs ) { + return !operator==( lhs, rhs ); +} + + +#endif /* __KDTOOLS_CORE_KDAUTOPOINTER_H__ */ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.cpp new file mode 100644 index 000000000..0cf06f233 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ +#include "kdbytesize.h" + +#include <QStringList> +#include <QTextStream> + +KDByteSize::KDByteSize( quint64 size ) + : m_size( size ) +{ +} + +KDByteSize::~KDByteSize() +{ +} + +KDByteSize::operator quint64() const +{ + return m_size; +} + +quint64 KDByteSize::size() const +{ + return m_size; +} + +QString KDByteSize::toString() const +{ + static const QStringList units = QStringList() << QObject::tr( "B" ) + << QObject::tr( "kB" ) + << QObject::tr( "MB" ) + << QObject::tr( "GB" ) + << QObject::tr( "TB" ); + + double s = m_size; + quint64 factor = 1; + int unit = 0; + while( s >= 1024.0 && unit + 1 < units.count() ) + { + ++unit; + factor *= 1024; + s = 1.0 * m_size / factor; + } + + // only one digit after the decimal point is wanted + s = qRound( s * 10 ) / 10.0; + + return QString::fromLatin1( "%L1 %2" ).arg( s, 0, 'g', 4 ).arg( units[ unit ] ); +} + +bool operator==( const KDByteSize& lhs, const KDByteSize& rhs ) +{ + return lhs.size() == rhs.size(); +} + +bool operator<( const KDByteSize& lhs, const KDByteSize& rhs ) +{ + return lhs.size() < rhs.size(); +} + +KDByteSize operator*( const KDByteSize& lhs, int rhs ) +{ + return KDByteSize( lhs.size() * rhs ); +} + +#include <QDebug> + +QDebug operator<<( QDebug dbg, const KDByteSize& size ) +{ + return dbg << "KDByteSize(" << size.size() << ")"; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.h new file mode 100644 index 000000000..cc8f5a770 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdbytesize.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ +#ifndef KDBYTESIZE_H +#define KDBYTESIZE_H + +#include <KDToolsCore/kdtoolsglobal.h> + +class KDTOOLSCORE_EXPORT KDByteSize +{ +public: + explicit KDByteSize( quint64 size = 0 ); + virtual ~KDByteSize(); + + operator quint64() const; + quint64 size() const; + + QString toString() const; + +private: + quint64 m_size; +}; + +KDTOOLSCORE_EXPORT bool operator==( const KDByteSize& lhs, const KDByteSize& rhs ); +KDTOOLSCORE_EXPORT bool operator<( const KDByteSize& lhs, const KDByteSize& rhs ); +KDTOOLSCORE_EXPORT KDByteSize operator*( const KDByteSize& lhs, int rhs ); + +KDTOOLS_MAKE_RELATION_OPERATORS( KDByteSize, static inline ) + +class QDebug; +KDTOOLSCORE_EXPORT QDebug operator<<( QDebug dbg, const KDByteSize& size ); + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.cpp new file mode 100644 index 000000000..9352f83cd --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.cpp @@ -0,0 +1,250 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdgenericfactory.h" + +/*! + \class KDGenericFactory + \ingroup core + \brief Template based generic factory implementation + \since_c 2.1 + + (The exception safety of this class has not been evaluated yet.) + + KDGenericFactory is an implemention of of the factory pattern. It can be used to + "produce" instances of different classes having a common superclass + T_Product. The user of the + factory registers those producable classes in the factory by using an identifier + (T_Identifier, defaulting to QString). That identifer can then be used to + produce as many instances of the registered product as he wants. + + The advanced user can even choose the type of the map the factory is using to store its + FactoryFunctions by passing a T_Map template parameter. It defaults to QHash. KDGenericFactory + expects it to be a template class accepting T_Identifier and FactoryFunction as parameters. + Additionally it needs to provide: + + \li\link QHash::const_iterator a nested %const_iterator \endlink typedef for an iterator type that when dereferenced has type ((const) reference to) FactoryFunction (Qt convention), + \li\link QHash::insert %insert( T_Identifier, FactoryFunction ) \endlink, which must overwrite any existing entries with the same identifier. + \li\link QHash::find %find( T_Identifier ) \endlink, + \li\link QHash::end %end() \endlink, + \li\link QHash::size %size() \endlink, + \li\link QHash::remove %remove( T_Identifier ) \endlink, and + \li\link QHash::keys %keys ) \endlink, returning a QList<T_Identifier>. + + The only two class templates that currently match this concept are + QHash and QMap. QMultiHash and QMulitMap do not work, since they + violate the requirement on insert() above, and std::map and + std::unordered_map do not match because they don't have keys() and + because a dereferenced iterator has type + std::pair<const T_Identifier,FactoryFunction> + instead of just FactoryFunction. + + \section general-use General Use + + The following example shows how the general use case of KDGenericFactory looks like: + + \code + + class Fruit + { + }; + + class Apple : public Fruit + { + }; + + class Pear : public Fruit + { + }; + + int main() + { + // creates a common fruit "factory" + KDGenericFactory< Fruit > fruitPlantation; + // registers the product "Apple" + fruitPlantation.registerProduct< Apple >( "Apple" ); + // registers the product "Pear" + fruitPlantation.registerProduct< Pear >( "Pear" ); + + // lets create some stuff - here comes our tasty apple: + Fruit* myApple = fruitPlantation.create( "Apple" ); + + // and a pear, please: + Fruit* myPear = fruitPlantation.create( "Pear" ); + + // ohh - that doesn't work, returns a null pointer: + Fruit* myCherry = fruitPlantation.create( "Cherry" ); + } + + \endcode +*/ + +/*! + \fn KDGenericFactory::~KDGenericFactory + + Destructor. +*/ + +/*! + \typedef KDGenericFactory::FactoryFunction + + This typedef defines a factory function producing an object of type T_Product. +*/ + +/*! + \fn KDGenericFactory::registerProduct( const T_Identifier& name ) + + Registers a product of type T, identified by \a name in the factory. + Any type with the same name gets unregistered. + + If a product was registered via this method, it will be created using its + default constructor. +*/ + +/*! + \fn KDGenericFactory::unregisterProduct( const T_Identifier& name ) + + Unregisters the previously registered product identified by \a name from the factory. + If no such product is known, nothing is done. +*/ + +/*! + \fn KDGenericFactory::productCount() const + + Returns the number of different products known to the factory. +*/ + +/*! + \fn KDGenericFactory::availableProducts() const + + Returns the list of products known to the factory. +*/ + +/*! + \fn KDGenericFactory::create( const T_Identifier& name ) const + + Creates and returns a product of the type identified by \a name. + Ownership of the product is transferred to the caller. +*/ + +/*! + \fn KDGenericFactory::registerProductionFunction( const T_Identifier& name, FactoryFunction create ) + + Subclasses can use this method to register their own FactoryFunction \a create to create products of + type T, identified by \a name. When a product is registered via this method, it will be created + by calling create(). +*/ + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/test.h> + +#include <QStringList> +#include <QMap> + +class Fruit +{ +public: + virtual ~Fruit() {} +}; + +class Apple : public Fruit +{ +}; + +class Pear : public Fruit +{ +}; + +std::ostream& operator<<( std::ostream& stream, const QStringList& list ) +{ + stream << "QStringList("; + for( QStringList::const_iterator it = list.begin(); it != list.end(); ++it ) + { + stream << " " << it->toLocal8Bit().data(); + if( it + 1 != list.end() ) + stream << ","; + } + stream << " )"; + return stream; +} + +class KDGenericFactoryTest : public KDUnitTest::Test { +public: + KDGenericFactoryTest() : Test( "KDGenericFactory" ) {} + void run() { + doRun<QHash>(); + doRun<QMap>(); + } + + template <template <typename U, typename V> class T_Map> + void doRun(); +}; + +KDAB_EXPORT_UNITTEST( KDGenericFactoryTest, "kdcoretools" ) + +template <template <typename U, typename V> class T_Map> +void KDGenericFactoryTest::doRun() { + + { + KDGenericFactory< Fruit, QString, T_Map > fruitPlantation; + assertEqual( fruitPlantation.productCount(), 0U ); + assertEqual( fruitPlantation.availableProducts(), QStringList() ); + + fruitPlantation.template registerProduct< Apple >( QLatin1String( "Apple" ) ); + assertEqual( fruitPlantation.productCount(), 1U ); + assertEqual( fruitPlantation.availableProducts(), QStringList( QLatin1String( "Apple" ) ) ); + + fruitPlantation.template registerProduct< Pear >( QLatin1String( "Pear" ) ); + assertEqual( fruitPlantation.productCount(), 2U ); + + Fruit* fruit = 0; + fruit = fruitPlantation.create( QLatin1String( "Apple" ) ); + assertNotNull( fruit ); + assertNotNull( dynamic_cast< Apple* >( fruit ) ); + + fruit = fruitPlantation.create( QLatin1String( "Pear" ) ); + assertNotNull( fruit ); + assertNotNull( dynamic_cast< Pear* >( fruit ) ); + + fruit = fruitPlantation.create( QLatin1String( "Cherry" ) ); + assertNull( fruit ); + + fruitPlantation.unregisterProduct( QLatin1String( "Apple" ) ); + assertEqual( fruitPlantation.productCount(), 1U ); + assertEqual( fruitPlantation.availableProducts(), QStringList( QLatin1String( "Pear" ) ) ); + fruit = fruitPlantation.create( QLatin1String( "Apple" ) ); + assertNull( fruit ); + + fruit = fruitPlantation.create( QLatin1String( "Pear" ) ); + assertNotNull( fruit ); + assertNotNull( dynamic_cast< Pear* >( fruit ) ); + + + fruitPlantation.unregisterProduct( QLatin1String( "Pear" ) ); + assertEqual( fruitPlantation.productCount(), 0U ); + fruit = fruitPlantation.create( QLatin1String( "Pear" ) ); + assertNull( fruit ); + } + +} +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.h new file mode 100644 index 000000000..1a0d7f854 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdgenericfactory.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE__KDGENERICFACTORY_H__ +#define __KDTOOLSCORE__KDGENERICFACTORY_H__ + +#include <KDToolsCore/kdtoolsglobal.h> + +#include <QtCore/QHash> + +template< typename T_Product, typename T_Identifier = QString, template< typename U, typename V > class T_Map = QHash > +class MAKEINCLUDES_EXPORT KDGenericFactory +{ +public: + virtual ~KDGenericFactory() + { + } + + typedef T_Product* (*FactoryFunction)(); + + template< typename T > + void registerProduct( const T_Identifier& name ) + { +#ifdef Q_CC_MSVC + FactoryFunction function = &KDGenericFactory::create<T>; + registerProductionFunction( name, function ); +#else + registerProductionFunction( name, &create<T> ); +#endif + } + + void unregisterProduct( const T_Identifier& name ) + { + map.remove( name ); + } + + unsigned int productCount() const + { + return map.size(); + } + + QList< T_Identifier > availableProducts() const + { + return map.keys(); + } + + T_Product* create( const T_Identifier& name ) const + { + const typename T_Map< T_Identifier, FactoryFunction >::const_iterator it = map.find( name ); + if( it == map.end() ) + return 0; + return (*it)(); + } + +protected: + void registerProductionFunction( const T_Identifier& name, FactoryFunction create ) + { + map.insert( name, create ); + } + +private: + template< typename T > + static T_Product* create() + { + return new T; + } + + T_Map< T_Identifier, FactoryFunction > map; +}; + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.cpp new file mode 100644 index 000000000..d88380ffb --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdjob.h" + +#include <QEventLoop> + +class KDJob::Private { + KDJob* const q; +public: + explicit Private( KDJob* qq ) : q( qq ), error( KDJob::NoError ), errorString(), caps( KDJob::NoCapabilities ), autoDelete( true ), totalAmount( 100 ), processedAmount( 0 ) {} + + void delayedStart() { + q->doStart(); + emit q->started( q ); + } + + void waitForSignal( const char* sig ) { + QEventLoop loop; + q->connect( q, sig, &loop, SLOT(quit()) ); + loop.exec(); + } + + int error; + QString errorString; + KDJob::Capabilities caps; + bool autoDelete : 1; + quint64 totalAmount; + quint64 processedAmount; +}; + +KDJob::KDJob( QObject* parent ) : QObject( parent ), d( new Private( this ) ) { +} + +KDJob::~KDJob() { + //delete d; +} + +bool KDJob::autoDelete() const { + return d->autoDelete; +} + +void KDJob::setAutoDelete( bool autoDelete ) { + d->autoDelete = autoDelete; +} + +int KDJob::error() const { + return d->error; +} + +QString KDJob::errorString() const { + return d->errorString; +} + +void KDJob::emitFinished() { + emit finished( this ); + if ( d->autoDelete ) + deleteLater(); +} + +void KDJob::emitFinishedWithError( int error, const QString& errorString ) { + d->error = error; + d->errorString = errorString; + emitFinished(); +} + +void KDJob::setError( int error ) { + d->error = error; +} + +void KDJob::setErrorString( const QString& errorString ) { + d->errorString = errorString; +} + +void KDJob::waitForStarted() { + d->waitForSignal( SIGNAL(started(KDJob*)) ); +} + +void KDJob::waitForFinished() { + d->waitForSignal( SIGNAL(finished(KDJob*)) ); +} + +KDJob::Capabilities KDJob::capabilities() const { + return d->caps; +} + +bool KDJob::hasCapability( Capability c ) const { + return d->caps.testFlag( c ); +} + +void KDJob::setCapabilities( Capabilities c ) { + d->caps = c; +} + +void KDJob::start() { + QMetaObject::invokeMethod( this, "delayedStart", Qt::QueuedConnection ); +} + +void KDJob::doCancel() { +} + +void KDJob::cancel() { + doCancel(); + setError( Canceled ); +} + +quint64 KDJob::totalAmount() const { + return d->totalAmount; +} + +quint64 KDJob::processedAmount() const { + return d->processedAmount; +} + +void KDJob::setTotalAmount( quint64 amount ) { + if ( d->totalAmount == amount ) + return; + d->totalAmount = amount; + emit progress( this, d->processedAmount, d->totalAmount ); +} + +void KDJob::setProcessedAmount( quint64 amount ) { + if ( d->processedAmount == amount ) + return; + d->processedAmount = amount; + emit progress( this, d->processedAmount, d->totalAmount ); +} + +#include "moc_kdjob.cpp" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.h new file mode 100644 index 000000000..10cdd6899 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdjob.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLS_CORE_KDJOB_H__ +#define __KDTOOLS_CORE_KDJOB_H__ + +#include <QObject> + +#include <KDToolsCore/pimpl_ptr.h> + +class KDTOOLSCORE_EXPORT KDJob : public QObject { + Q_OBJECT + Q_PROPERTY( bool autoDelete READ autoDelete WRITE setAutoDelete ) + +public: + explicit KDJob( QObject* parent=0 ); + ~KDJob(); + + enum Error { + NoError=0, + Canceled=1, + UserDefinedError=128 + }; + + enum Capability { + NoCapabilities=0x0, + Cancelable=0x1 + }; + + Q_DECLARE_FLAGS(Capabilities, Capability) + + int error() const; + QString errorString() const; + + bool autoDelete() const; + void setAutoDelete( bool autoDelete ); + + Capabilities capabilities() const; + bool hasCapability( Capability c ) const; + + void waitForStarted(); + void waitForFinished(); + + quint64 totalAmount() const; + quint64 processedAmount() const; + +public Q_SLOTS: + void start(); + void cancel(); + +Q_SIGNALS: + void infoMessage( KDJob*, const QString& ); + +Q_SIGNALS: +#ifndef Q_MOC_RUN +private: // make signals private unless moc runs +#endif + void started( KDJob* ); + void finished( KDJob* ); + void progress( KDJob* job, quint64 processed, quint64 total ); + +protected: + virtual void doStart() = 0; + virtual void doCancel(); + + void setCapabilities( Capabilities c ); + void setTotalAmount( quint64 amount ); + void setProcessedAmount( quint64 amount ); + + void setError( int error ); + void setErrorString( const QString& errorString ); + + void emitFinished(); + void emitFinishedWithError( int error, const QString& errorString ); + +private: + class Private; + friend class ::KDJob::Private; + kdtools::pimpl_ptr<Private> d; + Q_PRIVATE_SLOT( d, void delayedStart() ) +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(KDJob::Capabilities) + +#endif // __KDTOOLS_CORE_KDJOB_H__ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.cpp new file mode 100644 index 000000000..234bc448e --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdlockfile.h" + +#include "kdlockfile_p.h" + +KDLockFile::Private::Private( const QString& filename_ ) + : filename( filename_ ) + , handle( 0 ) + , locked( false ) +{ +} + +KDLockFile::KDLockFile( const QString& name ) + : d( new Private( name ) ) +{ +} + +KDLockFile::~KDLockFile() +{ +} + +bool KDLockFile::lock() +{ + return d->lock(); +} + +QString KDLockFile::errorString() const +{ + return d->errorString; +} + +bool KDLockFile::unlock() +{ + return d->unlock(); +} + + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/Test> +#include <QDebug> +#include <QDir> + +KDAB_UNITTEST_SIMPLE( KDLockFile, "kdcoretools" ) { + { + KDLockFile f( QLatin1String("/jlksdfdsfjkldsf-doesnotexist/file") ); + const bool locked = f.lock(); + assertFalse( locked ); + qDebug() << f.errorString(); + assertTrue( !f.errorString().isEmpty() ); + if ( !locked ) + assertTrue( f.unlock() ); + } + { + KDLockFile f( QDir::currentPath() + QLatin1String("/kdlockfile-test") ); + const bool locked = f.lock(); + assertTrue( locked ); + if ( !locked ) + qDebug() << f.errorString(); + assertEqual( locked, f.errorString().isEmpty() ); + const bool unlocked = f.unlock(); + assertTrue( unlocked ); + if ( !unlocked ) + qDebug() << f.errorString(); + assertEqual( unlocked, f.errorString().isEmpty() ); + } +} + +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.h new file mode 100644 index 000000000..cecbc5cbd --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE_KDLOCKFILE_H__ +#define __KDTOOLSCORE_KDLOCKFILE_H__ + +#include <pimpl_ptr.h> + +class QString; + +class KDTOOLSCORE_EXPORT KDLockFile +{ +public: + explicit KDLockFile( const QString & name ); + ~KDLockFile(); + + QString errorString() const; + + bool lock(); + bool unlock(); + +private: + Q_DISABLE_COPY(KDLockFile) + class Private; + kdtools::pimpl_ptr< Private > d; +}; + +#endif // __KDTOOLSCORE_KDLOCKFILE_H__ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_p.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_p.h new file mode 100644 index 000000000..da4c4dc4c --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_p.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE_KDLOCKFILE_P_H__ +#define __KDTOOLSCORE_KDLOCKFILE_P_H__ + +#include "kdlockfile.h" +#include <QtCore/QString> +#ifdef Q_OS_WIN +#include <windows.h> +#endif + +class KDLockFile::Private +{ +public: + explicit Private( const QString& filename ); + ~Private(); + bool lock(); + bool unlock(); + + QString errorString; + +private: + QString filename; +#ifdef Q_OS_WIN + HANDLE handle; +#else + int handle; +#endif + bool locked; +}; + +#endif // LOCKFILE_P_H diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_unix.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_unix.cpp new file mode 100644 index 000000000..e3d187cbf --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_unix.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdlockfile_p.h" + +#include <QtCore/QCoreApplication> + +#include <cerrno> + +#include <sys/file.h> + +KDLockFile::Private::~Private() +{ + unlock(); +} + +bool KDLockFile::Private::lock() +{ + if ( locked ) + return true; + + errorString.clear(); + errno = 0; + handle = open( filename.toLatin1().constData(), O_CREAT | O_RDWR | O_NONBLOCK ); + if ( handle == -1 ) { + errorString = QObject::tr("Could not create lock file %1: %2").arg( filename, QLatin1String( strerror( errno ) ) ); + return false; + } + const QString pid = QString::number( qApp->applicationPid() ); + const QByteArray data = pid.toLatin1(); + errno = 0; + qint64 written = 0; + while ( written < data.size() ) { + const qint64 n = write( handle, data.constData() + written, data.size() - written ); + if ( n < 0 ) { + errorString = QObject::tr("Could not write PID to lock file %1: %2").arg( filename, QLatin1String( strerror( errno ) ) ); + return false; + } + written += n; + } + errno = 0; + locked = flock( handle, LOCK_NB | LOCK_EX ) != -1; + if ( !locked ) + errorString = QObject::tr("Could not lock lock file %1: %2").arg( filename, QLatin1String( strerror( errno ) ) ); + return locked; +} + +bool KDLockFile::Private::unlock() +{ + errorString.clear(); + if ( !locked ) + return true; + errno = 0; + locked = flock( handle, LOCK_UN | LOCK_NB ) == -1; + if ( locked ) + errorString = QObject::tr("Could not unlock lock file %1: %2").arg( filename, QLatin1String( strerror( errno ) ) ); + return !locked; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_win.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_win.cpp new file mode 100644 index 000000000..f7fed60ab --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdlockfile_win.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdlockfile.h" +#include "kdlockfile_p.h" + +#include <QtCore/QCoreApplication> +#include <QtCore/QFileInfo> + +KDLockFile::Private::~Private() +{ + unlock(); +} + +bool KDLockFile::Private::lock() +{ + const QFileInfo fi( filename ); + handle = CreateFile( filename.toStdWString().data(), + GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, + NULL, fi.exists() ? OPEN_EXISTING : CREATE_NEW, + FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL ); + + if ( !handle ) + return false; + QString pid = QString::number( qApp->applicationPid() ); + QByteArray data = pid.toLatin1(); + DWORD bytesWritten; + const bool wrotePid = WriteFile( handle, data.data(), data.size(), &bytesWritten, NULL ); + if ( !wrotePid ) + return false; + FlushFileBuffers( handle ); + + const bool locked = LockFile( handle, 0, 0, fi.size(), 0 ); + + this->locked = locked; + return locked; +} + +bool KDLockFile::Private::unlock() +{ + const QFileInfo fi( filename ); + if ( locked ) + { + const bool success = UnlockFile( handle, 0, 0, 0, fi.size() ); + this->locked = !success; + CloseHandle( handle ); + return success; + } + return true; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.cpp new file mode 100644 index 000000000..1a0b863c7 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.cpp @@ -0,0 +1,493 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdmetamethoditerator.h" + +/*! + \class KDMetaMethodIterator + \ingroup core + \brief Iterator over methods of QObjects + \since_c 2.3 + + KDMetaMethodIterator provides a way over all methods of a QObject which can be accessed via Qt's meta + object system. It is possible to filter on types of methods (Q_INVOKABLE methods, signals, slots, + constructors) and on access types (public, protected, private). Furthermore, KDMetaMethodIterator can + be configured to filter methods of the given object's base class or all methods provided by QObject. + + \section general-use General Use + + The following example shows a general use case of KDMetaMethodIterator: + + \code + + class QIODeviceWrapper : public QIODevice + { + Q_OBJECT + public: + QIODeviceWrapper( QIODevice* nestedDevice ) + : nested( nestedDevice ) + { + // make sure all signals of the nested QIODevice are forwarded, + // but make sure not to connect QObject's own destroyed() signal, + // therefore use KDMetaMethodIterator::IgnoreQObjectMethods + KDMetaMethodIterator it( QIODevice::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreQObjectMethods ); + while( it.hasNext() ) + { + it.next(); + connect( nested, it.connectableSignature(), this, it.connectableSignature() ); + } + } + + protected: + // virtual methods needed to access the contents... + + private: + QIODevice* const nested; + }; + + \endcode +*/ + +/*! + \enum KDMetaMethodIterator::AccessType + This enum describes the access type of methods to iterate: +*/ + +/*! + \var KDMetaMethodIterator::Private + Iterate over private methods. +*/ + +/*! + \var KDMetaMethodIterator::Protected + Iterate over protected methods. +*/ + +/*! + \var KDMetaMethodIterator::Public + Iterate over public methods. +*/ + +/*! + \var KDMetaMethodIterator::AllAccessTypes + Iterate over methods of all access types. +*/ + +/*! + \enum KDMetaMethodIterator::MethodType + This enum describes the type of methods to iterate: +*/ + +/*! + \var KDMetaMethodIterator::Method + Iterate over methods marked with Q_INVOKABLE +*/ + +/*! + \var KDMetaMethodIterator::Signal + Iterate over signals. +*/ + +/*! + \var KDMetaMethodIterator::Slot + Iterate over slots. +*/ + +/*! + \var KDMetaMethodIterator::Constructor + Iterate over constructors marked with Q_INVOKABLE. +*/ + +/*! + \var KDMetaMethodIterator::AllMethodTypes + Iterate over all supported method types. +*/ + +/*! + \enum KDMetaMethodIterator::IteratorFlag + This enum describes flags which can be passed to KDMetaMethodIterator: +*/ + +/*! + \var KDMetaMethodIterator::NoFlags + No flags set, only type filtering takes place. +*/ + +/*! + \var KDMetaMethodIterator::IgnoreQObjectMethods + KDMetaMethodIterator will not iterate over methods provided by QObject itself. +*/ + +/*! + \var KDMetaMethodIterator::IgnoreSuperClassMethods + KDMetaMethodIterator will only iterate over methods provided by the passed class instance itself, ignoring any base classes. +*/ + +/*! + \internal + */ +class KDMetaMethodIterator::Priv +{ +public: + Priv( const QMetaObject* object, MethodTypes types, AccessTypes access, IteratorFlags flags ) + : index( -1 ), + m( types ), + a( access ), + f( flags ), + metaObject( object ) + { + } + + Priv( const QMetaObject* object, MethodTypes types, IteratorFlags flags ) + : index( -1 ), + m( types ), + a( ( types & Signal ) ? ( Protected | Public ) : Public ), + f( flags ), + metaObject( object ) + { + } + + bool filterMatches( const QMetaMethod& method, int index ) const; + + int index; + const MethodTypes m; + const AccessTypes a; + const IteratorFlags f; + const QMetaObject* const metaObject; + + mutable QByteArray connectableSignature; +}; + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types. + By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set + to return protected and public. + + By default, \a flags is NoFlags, which lists all methods. + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types, IteratorFlags flags ) + : d( new Priv( &metaObject, types, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types, + matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types, AccessType access, IteratorFlags flags ) + : d( new Priv( &metaObject, types, access, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, AccessType access, IteratorFlags flags ) + : d( new Priv( &metaObject, AllMethodTypes, access, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types. + By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set + to return protected and public. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types, IteratorFlags flags ) + : d( new Priv( metaObject, types, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types, + matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types, AccessType access, IteratorFlags flags ) + : d( new Priv( metaObject, types, access, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, AccessType access, IteratorFlags flags ) + : d( new Priv( metaObject, AllMethodTypes, access, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods of the given \a types. + By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set + to return protected and public. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, MethodTypes types, IteratorFlags flags ) + : d( new Priv( object->metaObject(), types, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods of the given \a types, + matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, MethodTypes types, AccessType access, IteratorFlags flags ) + : d( new Priv( object->metaObject(), types, access, flags ) ) +{ +} + +/*! + Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods matching the \a access mode. + + By default, \a flags is NoFlags, which lists all methods. + + \overload + */ +KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, AccessType access, IteratorFlags flags ) + : d( new Priv( object->metaObject(), AllMethodTypes, access, flags ) ) +{ +} + +/*! + Destroys the KDMetaMethodIterator. + */ +KDMetaMethodIterator::~KDMetaMethodIterator() +{ +} + +/*! + Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the method list; otherwise returns false. + */ +bool KDMetaMethodIterator::hasNext() const +{ + for( int i = d->index + 1; i < d->metaObject->methodCount(); ++i ) + { + const QMetaMethod method = d->metaObject->method( i ); + if( d->filterMatches( method, i ) ) + return true; + } + return false; +} + +/*! + Returns the next method and advances the iterator by one position. + */ +QMetaMethod KDMetaMethodIterator::next() +{ + Q_ASSERT( hasNext() ); + d->connectableSignature.clear(); + for( ++d->index; d->index < d->metaObject->methodCount(); ++d->index ) + { + const QMetaMethod method = d->metaObject->method( d->index ); + if( d->filterMatches( method, d->index ) ) + return method; + } + return QMetaMethod(); +} + +/*! + Returns the current method's signature ready to be passed to QObject::connect, i.e. already passed through the SIGNAL or SLOT macro. + The return value stays valid up to the following next() call. This is only valid for signals and slots. + */ +const char* KDMetaMethodIterator::connectableSignature() const +{ + if( d->connectableSignature.isEmpty() ) + { + const QMetaMethod method = d->metaObject->method( d->index ); + d->connectableSignature = method.signature(); + d->connectableSignature.push_front( QString::number( ( static_cast< int >( method.methodType() ) - 3 ) * -1 ).toLocal8Bit() ); + } + return d->connectableSignature.constData(); +} + +/*! + Returns true if \a method matches the filters set on the iterator, currently pointing to \a index. + \internal + */ +bool KDMetaMethodIterator::Priv::filterMatches( const QMetaMethod& method, int index ) const +{ + // filter QObject's own methods + if( ( f & IgnoreQObjectMethods ) && index < QObject::staticMetaObject.methodCount() ) + return false; + + // filter any super classes methods + if( ( f & IgnoreSuperClassMethods ) && index < metaObject->methodOffset() ) + return false; + + // filter any methods with unwanted access types + if( ( ( 1 << method.access() ) & a ) == 0 ) + return false; + + // filter any methods with unwanted method types + return ( ( 1 << method.methodType() ) & m ) != 0; +} + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/Test> + +class TestClass : public QObject +{ + Q_OBJECT +public: + TestClass(){} + +public Q_SLOTS: + void publicSlot( int ) {} + +protected Q_SLOTS: + void protectedSlot( int ) {} + +private Q_SLOTS: + void privateSlot( int ) {} + +private: + Q_PRIVATE_SLOT( d, void veryPrivateSlot() ) + + class Private; + kdtools::pimpl_ptr< Private > d; +}; + +class TestClassDerived : public TestClass +{ + Q_OBJECT +public: + TestClassDerived(){} +}; + +class TestClass::Private +{ +public: + void veryPrivateSlot() {} +}; + +KDAB_UNITTEST_SIMPLE( KDMetaMethodIterator, "kdcoretools" ) { + + QObject o; + { + KDMetaMethodIterator it( &o, KDMetaMethodIterator::Signal ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed()" ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( QObject::staticMetaObject, KDMetaMethodIterator::Signal ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" ); + } + { + KDMetaMethodIterator it( o.metaObject(), KDMetaMethodIterator::Signal ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" ); + } + + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "destroyed()" ); + assertEqual( std::string( it.connectableSignature() ), "2destroyed()" ); + assertFalse( it.hasNext() ); + } + + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreQObjectMethods ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreSuperClassMethods ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "deleteLater()" ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "publicSlot(int)" ); + assertEqual( std::string( it.connectableSignature() ), "1publicSlot(int)" ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::IgnoreQObjectMethods ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "publicSlot(int)" ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Protected, KDMetaMethodIterator::IgnoreQObjectMethods ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "protectedSlot(int)" ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreQObjectMethods ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "privateSlot(int)" ); + assertEqual( std::string( it.next().signature() ), "veryPrivateSlot()" ); + assertFalse( it.hasNext() ); + } + + { + KDMetaMethodIterator it( TestClassDerived::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreQObjectMethods ); + assertTrue( it.hasNext() ); + assertEqual( std::string( it.next().signature() ), "privateSlot(int)" ); + assertEqual( std::string( it.next().signature() ), "veryPrivateSlot()" ); + assertFalse( it.hasNext() ); + } + { + KDMetaMethodIterator it( TestClassDerived::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreSuperClassMethods ); + assertFalse( it.hasNext() ); + } + +} + +#include "kdmetamethoditerator.moc" + +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.h new file mode 100644 index 000000000..94bb8f707 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE_KDMETAMETHODITERATOR_H__ +#define __KDTOOLSCORE_KDMETAMETHODITERATOR_H__ + +#include <KDToolsCore/pimpl_ptr.h> + +#include <QtCore/QMetaMethod> + +class KDTOOLSCORE_EXPORT KDMetaMethodIterator +{ +public: + enum AccessType + { + Private = 1 << QMetaMethod::Private, + Protected = 1 << QMetaMethod::Protected, + Public = 1 << QMetaMethod::Public, + AllAccessTypes = Private | Protected | Public + }; + Q_DECLARE_FLAGS( AccessTypes, AccessType ) + + enum MethodType + { + Method = 0x1, + Signal = 0x2, + Slot = 0x4, + Constructor = 0x8, + AllMethodTypes = Method | Signal | Slot | Constructor + }; + Q_DECLARE_FLAGS( MethodTypes, MethodType ) + + enum IteratorFlag + { + NoFlags = 0x00, + IgnoreQObjectMethods = 0x01, + IgnoreSuperClassMethods = 0x02 + }; + Q_DECLARE_FLAGS( IteratorFlags, IteratorFlag ) + + explicit KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types = AllMethodTypes, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types, AccessType access, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QMetaObject& metaObject, AccessType access, IteratorFlags flags = NoFlags ); + + explicit KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types = AllMethodTypes, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types, AccessType access, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QMetaObject* metaObject, AccessType access, IteratorFlags flags = NoFlags ); + + explicit KDMetaMethodIterator( const QObject* object, MethodTypes types = AllMethodTypes, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QObject* object, MethodTypes types, AccessType access, IteratorFlags flags = NoFlags ); + KDMetaMethodIterator( const QObject* object, AccessType access, IteratorFlags flags = NoFlags ); + + ~KDMetaMethodIterator(); + + bool hasNext() const; + QMetaMethod next(); + + const char* connectableSignature() const; + +private: + class Priv; + kdtools::pimpl_ptr< Priv > d; +}; + + +#endif // __KDTOOLSCORE_KDMETAMETHODITERATOR_H__ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.cpp new file mode 100644 index 000000000..457a6215a --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdrunoncechecker.h" +#include "kdlockfile.h" +#include "kdsysinfo.h" + +#include <QtCore/QList> +#include <QtCore/QCoreApplication> +#include <QtCore/QDir> +#include <QtCore/QFileInfo> + +#include <algorithm> + +class KDRunOnceChecker::Private +{ +public: + Private( const QString& filename ); + ~Private(); + KDLockFile m_lockfile; + bool m_hasLock; +}; + +KDRunOnceChecker::Private::Private( const QString& filename ) + : m_lockfile( filename ) + , m_hasLock( false ) +{ + +} + +KDRunOnceChecker::Private::~Private() +{ + +} + +KDRunOnceChecker::KDRunOnceChecker( const QString& filename ) + :d( new Private( filename ) ) +{ + +} + +KDRunOnceChecker::~KDRunOnceChecker() +{ + +} + +class ProcessnameEquals +{ +public: + ProcessnameEquals( const QString& name ): m_name( name ){} + bool operator()( const KDSysInfo::ProcessInfo& info ) + { +#ifndef Q_WS_WIN + if( info.name == m_name ) + return true; + const QFileInfo fi( info.name ); + if( fi.fileName() == m_name || fi.baseName() == m_name ) + return true; + return false; +#else + if( info.name.toLower() == m_name.toLower() ) + return true; + if( info.name.toLower() == QDir::toNativeSeparators(m_name.toLower()) ) + return true; + const QFileInfo fi( info.name ); + if( fi.fileName().toLower() == m_name.toLower() || fi.baseName().toLower() == m_name.toLower() ) + return true; + return info.name == m_name; +#endif + } + +private: + QString m_name; +}; + +bool KDRunOnceChecker::isRunning( Dependencies depends ) +{ + bool running = false; + switch ( depends ) + { + case( Lockfile ): + { + const bool locked = d->m_hasLock || d->m_lockfile.lock(); + if ( locked ) + d->m_hasLock = true; + running = running || ! locked; + } + break; + case( ProcessList ): + { + const QList< KDSysInfo::ProcessInfo > allProcesses = KDSysInfo::runningProcesses(); + const QString appName = qApp->applicationFilePath(); + //QList< KDSysInfo::ProcessInfo >::const_iterator it = std::find_if( allProcesses.constBegin(), allProcesses.constEnd(), ProcessnameEquals( appName ) ); + const int count = std::count_if( allProcesses.constBegin(), allProcesses.constEnd(), ProcessnameEquals( appName ) ); + running = running || /*it != allProcesses.constEnd()*/count > 1; + } + break; + case( BOTH ): + { + const QList< KDSysInfo::ProcessInfo > allProcesses = KDSysInfo::runningProcesses(); + const QString appName = qApp->applicationFilePath(); + //QList< KDSysInfo::ProcessInfo >::const_iterator it = std::find_if( allProcesses.constBegin(), allProcesses.constEnd(), ProcessnameEquals( appName ) ); + const int count = std::count_if( allProcesses.constBegin(), allProcesses.constEnd(), ProcessnameEquals( appName ) ); + const bool locked = d->m_hasLock || d->m_lockfile.lock(); + if ( locked ) + d->m_hasLock = true; + running = running || ( /*it != allProcesses.constEnd()*/count > 1 && !locked ); + + } + break; + } + + return running; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.h new file mode 100644 index 000000000..9e562d3cf --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdrunoncechecker.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLS_RUNONCECHECKER_H__ +#define __KDTOOLS_RUNONCECHECKER_H__ + +#include <KDToolsCore/pimpl_ptr> + +#include <QtCore/QString> + +class KDTOOLSCORE_EXPORT KDRunOnceChecker +{ + +public: + enum Dependencies{ ProcessList, Lockfile, BOTH }; + explicit KDRunOnceChecker( const QString& filename = QString() ); + ~KDRunOnceChecker(); + bool isRunning( Dependencies depends ); + +private: + Q_DISABLE_COPY(KDRunOnceChecker) + class Private; + kdtools::pimpl_ptr< Private > d; + +}; + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.cpp new file mode 100644 index 000000000..38a4b87bf --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.cpp @@ -0,0 +1,476 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdsavefile.h" + +#include <QtCore/QDebug> +#include <QtCore/QDir> +#include <QtCore/QPointer> +#include <QtCore/QTemporaryFile> + +#ifdef Q_OS_WIN +# include <io.h> +#endif +#include <memory> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> + +static int permissionsToMode( QFile::Permissions p, bool* ok ) { + Q_ASSERT( ok ); + int m = 0; +#ifdef Q_OS_WIN + //following qfsfileengine_win.cpp: QFSFileEngine::setPermissions() + if ( p & QFile::ReadOwner || p & QFile::ReadUser || p & QFile::ReadGroup || p & QFile::ReadOther ) + m |= _S_IREAD; + if ( p & QFile::WriteOwner || p & QFile::WriteUser || p & QFile::WriteGroup || p & QFile::WriteOther ) + m |= _S_IWRITE; + *ok = m != 0; +#else + if ( p & QFile::ReadUser ) + m |= S_IRUSR; + if ( p & QFile::WriteUser ) + m |= S_IWUSR; + if ( p & QFile::ExeUser) + m |= S_IXUSR; + if ( p & QFile::ReadGroup ) + m |= S_IRGRP; + if ( p & QFile::WriteGroup ) + m |= S_IWGRP; + if ( p & QFile::ExeGroup ) + m |= S_IXGRP; + if ( p & QFile::ReadOther ) + m |= S_IROTH; + if ( p & QFile::WriteOther ) + m |= S_IWOTH; + if ( p & QFile::ExeOther ) + m |= S_IXOTH; + *ok = true; +#endif + return m; +} + +static bool sync( int fd ) { +#ifdef Q_OS_WIN + return _commit( fd ) == 0; +#else + return fsync( fd ) == 0; +#endif +} + +static QString makeAbsolute( const QString& path ) { + if ( QDir::isAbsolutePath( path ) ) + return path; + return QDir::currentPath() + QLatin1String("/") + path; +} + +static int myOpen( const QString& path, int flags, int mode ) { +#ifdef Q_OS_WIN + int fd; + _wsopen_s( &fd, reinterpret_cast<const wchar_t*>( path.utf16() ), flags, _SH_DENYRW, mode ); + return fd; +#else + return open( QFile::encodeName( path ).constData(), flags, mode ); +#endif +} + +static void myClose( int fd ) { +#ifdef Q_OS_WIN + _close( fd ); +#else + close( fd ); +#endif +} + +static bool touchFile( const QString& path, QFile::Permissions p ) { + bool ok; + const int mode = permissionsToMode( p, &ok ); + if ( !ok ) + return false; + const int fd = myOpen( QDir::toNativeSeparators( path ), O_WRONLY|O_CREAT, mode ); + if ( fd < 0 ) + { + QFile file( path ); + if( !file.open( QIODevice::WriteOnly ) ) + return false; + if( !file.setPermissions( p ) ) { + QFile::remove( path ); + return false; + } + return true; + } + sync( fd ); + myClose( fd ); + return true; +} + +static QFile* createFile( const QString& path, QIODevice::OpenMode m, QFile::Permissions p, bool* openOk ) { + Q_ASSERT( openOk ); + if ( !touchFile( path, p ) ) + return 0; + std::auto_ptr<QFile> file( new QFile( path ) ); + *openOk = file->open( m | QIODevice::Append ); + if ( !*openOk ) + QFile::remove( path ); // try to remove empty file + return file.release(); +} + +/*! + Generates a temporary file name based on template \a path + \internal + */ +static QString generateTempFileName( const QString& path ) +{ + const QString tmp = path + QLatin1String( "tmp.dsfdf.%1" ); //TODO: use random suffix + int count = 1; + while ( QFile::exists( tmp.arg( count ) ) ) + ++count; + return tmp.arg( count ); +} + +/*! + \class KDSaveFile KDSaveFile + \ingroup core + \brief Secure and robust writing to a file + +*/ + +class KDSaveFile::Private { + KDSaveFile* const q; +public: + explicit Private( const QString& fname, KDSaveFile* qq ) : q( qq ), +#ifdef Q_OS_WIN + backupExtension( QLatin1String(".bak") ), +#else + backupExtension( QLatin1String("~") ), +#endif + permissions( QFile::ReadUser|QFile::WriteUser), filename( fname ), tmpFile() { + //TODO respect umask instead of hardcoded default permissions + } + + ~Private() { + deleteTempFile(); + } + + bool deleteTempFile() { + if ( !tmpFile ) + return true; + const QString name = tmpFile->fileName(); + delete tmpFile; + //force a real close by deleting the object, before deleting the actual file. Needed on Windows + QFile tmp( name ); + return tmp.remove(); + } + + bool recreateTemporaryFile( QIODevice::OpenMode mode ) { + deleteTempFile(); + bool ok; + tmpFile = createFile( generateTempFileName( filename ), mode, permissions, &ok ); + QObject::connect( tmpFile, SIGNAL(aboutToClose()), q, SIGNAL(aboutToClose()) ); + QObject::connect( tmpFile, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)) ); + QObject::connect( tmpFile, SIGNAL(readChannelFinished()), q, SIGNAL(readChannelFinished()) ); + QObject::connect( tmpFile, SIGNAL(readyRead()), q, SIGNAL(readyRead()) ); + return ok; + } + + QString generateBackupName() const { + const QString bf = filename + backupExtension; + if ( !QFile::exists( bf ) ) + return bf; + int count = 1; + while ( QFile::exists( bf + QString::number( count ) ) ) + ++count; + return bf + QString::number( count ); + } + + void propagateErrors() { + if ( !tmpFile ) + return; + q->setErrorString( tmpFile->errorString() ); + } + QString backupExtension; + QFile::Permissions permissions; + QString filename; + QPointer<QFile> tmpFile; +}; + +KDSaveFile::KDSaveFile( const QString& filename, QObject* parent ) : QIODevice( parent ), d( new Private( makeAbsolute( filename ), this ) ) { +} + +KDSaveFile::~KDSaveFile() { +} + +void KDSaveFile::close() { + d->deleteTempFile(); + QIODevice::close(); +} + +bool KDSaveFile::open( OpenMode mode ) { + setOpenMode( QIODevice::NotOpen ); + if ( mode & QIODevice::Append ) { + setErrorString( tr("Append mode not supported.") ); + return false; + } + + if ( ( mode & QIODevice::WriteOnly ) == 0 ) { + setErrorString( tr("Read-only access not supported.") ); + return false; + } + + // for some reason this seems to be problematic... let's remove it for now, will break in commit anyhow, if it's serious + //if ( QFile::exists( d->filename ) && !QFileInfo( d->filename ).isWritable() ) { + // setErrorString( tr("The target file %1 exists and is not writable").arg( d->filename ) ); + // return false; + //} + const bool opened = d->recreateTemporaryFile( mode ); + if ( opened ) { + setOpenMode( mode ); + } + + //if target file already exists, apply permissions of existing file to temp file + return opened; +} + +bool KDSaveFile::atEnd() { + return d->tmpFile ? d->tmpFile->atEnd() : QIODevice::atEnd(); +} + +qint64 KDSaveFile::bytesAvailable() const { + return d->tmpFile ? d->tmpFile->bytesAvailable() : QIODevice::bytesAvailable(); +} + +qint64 KDSaveFile::bytesToWrite() const { + return d->tmpFile ? d->tmpFile->bytesToWrite() : QIODevice::bytesToWrite(); +} + +bool KDSaveFile::canReadLine() const { + return d->tmpFile ? d->tmpFile->canReadLine() : QIODevice::canReadLine(); +} + +bool KDSaveFile::isSequential() const { + return d->tmpFile ? d->tmpFile->isSequential() : QIODevice::isSequential(); +} + +qint64 KDSaveFile::pos() const { + return d->tmpFile ? d->tmpFile->pos() : QIODevice::pos(); +} + +bool KDSaveFile::reset() { + return d->tmpFile ? d->tmpFile->reset() : QIODevice::reset(); +} + +bool KDSaveFile::seek( qint64 pos ) { + const bool ret = d->tmpFile ? d->tmpFile->seek( pos ) : QIODevice::seek( pos ); + if ( !ret ) + d->propagateErrors(); + return ret; +} + +qint64 KDSaveFile::size() const { + return d->tmpFile ? d->tmpFile->size() : QIODevice::size(); +} + +bool KDSaveFile::waitForBytesWritten( int msecs ) { + return d->tmpFile ? d->tmpFile->waitForBytesWritten( msecs ) : QIODevice::waitForBytesWritten( msecs ); +} + +bool KDSaveFile::waitForReadyRead( int msecs ) { + return d->tmpFile ? d->tmpFile->waitForReadyRead( msecs ) : QIODevice::waitForReadyRead( msecs ); +} + +bool KDSaveFile::commit( KDSaveFile::CommitMode mode ) { + if ( !d->tmpFile ) + return false; + const QString tmpfname = d->tmpFile->fileName(); + d->tmpFile->flush(); + delete d->tmpFile; + QFile orig( d->filename ); + QString backup; + if ( orig.exists() ) { + backup = d->generateBackupName(); + if ( !orig.rename( backup ) ) { + setErrorString( tr("Could not backup existing file %1: %2").arg( d->filename, orig.errorString() ) ); + QFile tmp( tmpfname ); + if ( !tmp.remove() ) // TODO how to report this error? + qWarning() << "Could not remove temp file" << tmpfname << tmp.errorString(); + if ( mode != OverwriteExistingFile ) + return false; + } + } + QFile target( tmpfname ); + if ( !target.rename( d->filename ) ) { + setErrorString( target.errorString() ); + return false; + } + if ( mode == OverwriteExistingFile ) { + QFile tmp( backup ); + const bool removed = !tmp.exists() || tmp.remove( backup ); + if ( !removed ) + qWarning() << "Could not remove the backup: " << tmp.errorString(); + } + + return true; +} + +QString KDSaveFile::fileName() const { + return d->filename; +} + +void KDSaveFile::setFileName( const QString& filename ) { + const QString fn = makeAbsolute( filename ); + if ( fn == d->filename ) + return; + close(); + delete d->tmpFile; + d->filename = fn; +} + +qint64 KDSaveFile::readData( char* data, qint64 maxSize ) { + if ( !d->tmpFile ) { + setErrorString( tr("TODO") ); + return -1; + } + const qint64 ret = d->tmpFile->read( data, maxSize ); + d->propagateErrors(); + return ret; +} + +qint64 KDSaveFile::readLineData( char* data, qint64 maxSize ) { + if ( !d->tmpFile ) { + setErrorString( tr("TODO") ); + return -1; + } + const qint64 ret = d->tmpFile->readLine( data, maxSize ); + d->propagateErrors(); + return ret; +} + +qint64 KDSaveFile::writeData( const char* data, qint64 maxSize ) { + if ( !d->tmpFile ) { + setErrorString( tr("TODO") ); + return -1; + } + const qint64 ret = d->tmpFile->write( data, maxSize ); + d->propagateErrors(); + return ret; +} + +bool KDSaveFile::flush() { + return d->tmpFile ? d->tmpFile->flush() : false; +} + +bool KDSaveFile::resize( qint64 sz ) { + return d->tmpFile ? d->tmpFile->resize( sz ) : false; +} + +int KDSaveFile::handle() const { + return d->tmpFile ? d->tmpFile->handle() : -1; +} + +QFile::Permissions KDSaveFile::permissions() const { + return d->tmpFile ? d->tmpFile->permissions() : d->permissions; +} + +bool KDSaveFile::setPermissions( QFile::Permissions p ) { + d->permissions = p; + if ( d->tmpFile ) + return d->tmpFile->setPermissions( p ); + return false; +} + +void KDSaveFile::setBackupExtension( const QString& ext ) { + d->backupExtension = ext; +} + +QString KDSaveFile::backupExtension() const { + return d->backupExtension; +} + +/** + * TODO + * + * + */ + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/Test> + +KDAB_UNITTEST_SIMPLE( KDSaveFile, "kdcoretools" ) { + //TODO test contents (needs blocking and checked write() ) + { + const QString testfile1 = QLatin1String("kdsavefile-test1"); + QByteArray testData("lalalala"); + KDSaveFile saveFile( testfile1 ); + assertTrue( saveFile.open( QIODevice::WriteOnly ) ); + saveFile.write( testData.constData(), testData.size() ); + assertTrue( saveFile.commit() ); + assertTrue( QFile::exists( testfile1 ) ); + assertTrue( QFile::remove( testfile1 ) ); + } + { + const QString testfile1 = QLatin1String("kdsavefile-test1"); + QByteArray testData("lalalala"); + KDSaveFile saveFile( testfile1 ); + assertTrue( saveFile.open( QIODevice::WriteOnly ) ); + saveFile.write( testData.constData(), testData.size() ); + saveFile.close(); + assertFalse( QFile::exists( testfile1 ) ); + } + { + const QString testfile1 = QLatin1String("kdsavefile-test1"); + QByteArray testData("lalalala"); + KDSaveFile saveFile( testfile1 ); + assertTrue( saveFile.open( QIODevice::WriteOnly ) ); + saveFile.write( testData.constData(), testData.size() ); + assertTrue( saveFile.commit() ); + assertTrue( QFile::exists( testfile1 ) ); + + KDSaveFile sf2( testfile1 ); + sf2.setBackupExtension( QLatin1String(".bak") ); + assertTrue( sf2.open( QIODevice::WriteOnly ) ); + sf2.write( testData.constData(), testData.size() ); + sf2.commit(); //commit in backup mode (default) + const QString backup = testfile1 + sf2.backupExtension(); + assertTrue( QFile::exists( backup ) ); + assertTrue( QFile::remove( backup ) ); + + KDSaveFile sf3( testfile1 ); + sf3.setBackupExtension( QLatin1String(".bak") ); + assertTrue( sf3.open( QIODevice::WriteOnly ) ); + sf3.write( testData.constData(), testData.size() ); + sf3.commit( KDSaveFile::OverwriteExistingFile ); + const QString backup2 = testfile1 + sf3.backupExtension(); + assertFalse( QFile::exists( backup2 ) ); + + assertTrue( QFile::remove( testfile1 ) ); + } + { + const QString testfile1 = QLatin1String("kdsavefile-test1"); + KDSaveFile sf( testfile1 ); + assertFalse( sf.open( QIODevice::ReadOnly ) ); + assertFalse( sf.open( QIODevice::WriteOnly|QIODevice::Append ) ); + assertTrue( sf.open( QIODevice::ReadWrite ) ); + } +} + +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.h new file mode 100644 index 000000000..94170608c --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsavefile.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE_KDSAVEFILE_H__ +#define __KDTOOLSCORE_KDSAVEFILE_H__ + +#include <KDToolsCore/kdtoolsglobal.h> +#include <KDToolsCore/pimpl_ptr.h> + +#include <QtCore/QIODevice> +#include <QtCore/QFile> +#include <QtCore/QString> + +class KDTOOLSCORE_EXPORT KDSaveFile : public QIODevice { + Q_OBJECT +public: + explicit KDSaveFile( const QString& filename=QString(), QObject* parent=0 ); + ~KDSaveFile(); + + enum CommitMode { + BackupExistingFile=0x1, + OverwriteExistingFile=0x2 + }; + + bool commit( CommitMode=BackupExistingFile ); + + QString fileName() const; + void setFileName( const QString& filename ); + + QFile::Permissions permissions() const; + bool setPermissions( QFile::Permissions ); + + QString backupExtension() const; + void setBackupExtension( const QString& extension ); + + bool flush(); + bool resize( qint64 sz ); + int handle() const; + + /* reimp */ bool atEnd(); + /* reimp */ qint64 bytesAvailable() const; + /* reimp */ qint64 bytesToWrite() const; + /* reimp */ bool canReadLine() const; + /* reimp */ void close(); + /* reimp */ bool isSequential() const; + /* reimp */ bool open( OpenMode mode=QIODevice::ReadWrite ); //only valid: WriteOnly, ReadWrite + /* reimp */ qint64 pos() const; + /* reimp */ bool reset(); + /* reimp */ bool seek( qint64 pos ); + /* reimp */ qint64 size() const; + /* reimp */ bool waitForBytesWritten( int msecs ); + /* reimp */ bool waitForReadyRead( int msecs ); + +private: + /* reimp */ qint64 readData( char* data, qint64 maxSize ); + /* reimp */ qint64 readLineData( char* data, qint64 maxSize ); + /* reimp */ qint64 writeData( const char* data, qint64 maxSize ); + +private: + class Private; + kdtools::pimpl_ptr<Private> d; +}; + +#endif // __KDTOOLSCORE_KDSAVEFILE_H__ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.cpp new file mode 100644 index 000000000..fcd66f238 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdselfrestarter.h" + +#include <QtCore/QCoreApplication> +#include <QtCore/QDir> +#include <QtCore/QProcess> + +class KDSelfRestarter::Private { +public: + Private( int argc, char** argv ) : restartOnQuit( false ) { + executable = QString::fromLocal8Bit( argv[0] ); + workingPath = QDir::currentPath(); + for ( int i = 1; i < argc; ++i ) + args << QString::fromLocal8Bit( argv[i] ); + } + + Private() + { + executable = qApp->applicationFilePath(); + workingPath = QDir::currentPath(); + args = qApp->arguments().mid( 1 ); + } + + ~Private() { + if ( restartOnQuit ) + QProcess::startDetached( executable, args, workingPath ); + } + + QString executable; + QStringList args; + bool restartOnQuit; + QString workingPath; + static KDSelfRestarter* instance; +}; + +KDSelfRestarter* KDSelfRestarter::Private::instance = 0; + +KDSelfRestarter::KDSelfRestarter( int argc, char** argv ) : d( new Private( argc, argv ) ) { + Q_ASSERT_X( !Private::instance, Q_FUNC_INFO, "Cannot create more than one KDSelfRestarter instance" ); + Private::instance = this; +} + +KDSelfRestarter::~KDSelfRestarter() +{ + Q_ASSERT_X( Private::instance == this, Q_FUNC_INFO, "Cannot create more than one KDSelfRestarter instance" ); + Private::instance = 0; +} + +void KDSelfRestarter::setRestartOnQuit( bool restart ) { + Q_ASSERT_X( Private::instance, Q_FUNC_INFO, "KDSelfRestarter instance must be created in main()" ); + if ( Private::instance ) + Private::instance->d->restartOnQuit = restart; +} + +bool KDSelfRestarter::restartOnQuit() { + return Private::instance ? Private::instance->d->restartOnQuit : false; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.h new file mode 100644 index 000000000..0582c778d --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdselfrestarter.h @@ -0,0 +1,42 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE_KDSELFRESTARTER_H__ +#define __KDTOOLSCORE_KDSELFRESTARTER_H__ + +#include <KDToolsCore/pimpl_ptr.h> + +class KDTOOLSCORE_EXPORT KDSelfRestarter { +public: + KDSelfRestarter( int argc, char** argv ); + ~KDSelfRestarter(); + + static bool restartOnQuit(); + static void setRestartOnQuit( bool restart ); + +private: + Q_DISABLE_COPY(KDSelfRestarter); + class Private; + kdtools::pimpl_ptr<Private> d; +}; + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.cpp new file mode 100644 index 000000000..e694d79fa --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.cpp @@ -0,0 +1,191 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdsysinfo.h" + +#include <algorithm> + +KDSysInfo::KDSysInfo() +{ +} + +KDSysInfo::~KDSysInfo() +{ +} + +#include <QDir> +#include <QDebug> + +QDebug operator<<( QDebug dbg, KDSysInfo::OperatingSystemType type ) +{ + switch( type ) + { + case KDSysInfo::Linux: + return dbg << "Linux"; + case KDSysInfo::MacOSX: + return dbg << "Mac OS X"; + case KDSysInfo::Windows: + return dbg << "Windows"; + default: + return dbg << "<unknown>"; + } +} + +QDebug operator<<( QDebug dbg, KDSysInfo::ArchitectureType type ) +{ + switch( type ) + { + case KDSysInfo::ARM: + return dbg << "ARM"; + case KDSysInfo::Intel: + return dbg << "Intel"; + case KDSysInfo::AMD64: + return dbg << "AMD64"; + case KDSysInfo::IA64: + return dbg << "IA64"; + case KDSysInfo::PowerPC: + return dbg << "Power PC"; + case KDSysInfo::Motorola68k: + return dbg << "Motorola 68k"; + default: + return dbg << "<unknown>"; + } +} + +QDebug operator<<( QDebug dbg, KDSysInfo::Volume volume ) +{ + return dbg << "KDSysInfo::Volume(" << volume.path() << ")"; +} + +QPair< KDByteSize, KDByteSize > volumeSpace( const QString& volume ); +QString volumeName( const QString& volume ); + +class KDSysInfo::Volume::Private : public QSharedData +{ +public: + QString p; + QString name; + KDByteSize size; + KDByteSize availableSpace; +}; + + +KDSysInfo::Volume::Volume() + : d( new Private ) +{ +} + +KDSysInfo::Volume::Volume( const Volume& other ) + : d( other.d ) +{ +} + +KDSysInfo::Volume::~Volume() +{ +} + +void KDSysInfo::Volume::swap( KDSysInfo::Volume& other ) +{ + std::swap( d, other.d ); +} + +KDSysInfo::Volume& KDSysInfo::Volume::operator=( const KDSysInfo::Volume& other ) +{ + KDSysInfo::Volume tmp( other ); + swap( tmp ); + return *this; +} + +void KDSysInfo::Volume::setPath( const QString& path ) +{ + d->p = path; +} + +bool KDSysInfo::Volume::operator == ( const Volume& other ) const +{ + return d->name == other.d->name && d->p == other.d->p; +} + +void KDSysInfo::Volume::setName( const QString& name ) +{ + d->name = name; +} + +QString KDSysInfo::Volume::name() const +{ + return d->name; +} + +QString KDSysInfo::Volume::path() const +{ + return d->p; +} + +KDByteSize KDSysInfo::Volume::size() const +{ + return d->size; +} + +void KDSysInfo::Volume::setSize( const KDByteSize& size ) +{ + d->size = size; +} + +KDByteSize KDSysInfo::Volume::availableSpace() const +{ + return d->availableSpace; +} + +void KDSysInfo::Volume::setAvailableSpace( const KDByteSize& available ) +{ + d->availableSpace = available; +} + +struct PathLongerThan +{ + bool operator()( const KDSysInfo::Volume& lhs, const KDSysInfo::Volume& rhs ) const + { + return lhs.path().length() > rhs.path().length(); + } +}; + +KDSysInfo::Volume KDSysInfo::Volume::fromPath( const QString& path ) +{ + QList< KDSysInfo::Volume > volumes = mountedVolumes(); + // sort by length to get the longest mount point (not just "/") first + std::sort( volumes.begin(), volumes.end(), PathLongerThan() ); + for( QList< KDSysInfo::Volume >::const_iterator it = volumes.constBegin(); it != volumes.constEnd(); ++it ) + { +#ifdef Q_WS_WIN + if( QDir::toNativeSeparators( path ).toLower().startsWith( it->path().toLower() ) ) +#else + if( path.startsWith( it->path() ) ) +#endif + return *it; + } + return KDSysInfo::Volume(); +} + +QDebug operator<<( QDebug dbg, KDSysInfo::ProcessInfo process ) +{ + return dbg << "KDSysInfo::ProcessInfo(" << process.id << ", " << process.name << ")"; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.h new file mode 100644 index 000000000..2324ccfc8 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo.h @@ -0,0 +1,116 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef KDSYSINFO_H +#define KDSYSINFO_H + +#include <KDToolsCore/kdtoolsglobal.h> + +#include <QtCore/QString> +#include <QtCore/QSysInfo> +#include <QtCore/QSharedDataPointer> + +#include "kdbytesize.h" + +class KDVersion; + +class KDTOOLSCORE_EXPORT KDSysInfo : public QSysInfo +{ +private: + KDSysInfo(); + +public: + ~KDSysInfo(); + + enum OperatingSystemType + { + UnknownOperatingSystem = -1, + Linux, + MacOSX, + Windows + }; + + enum ArchitectureType + { + UnknownArchitecture = -1, + ARM, + Intel, + AMD64, + IA64, + PowerPC, + Motorola68k + }; + + class KDTOOLSCORE_EXPORT Volume + { + friend class ::KDSysInfo; + public: + static Volume fromPath( const QString& path ); + + Volume(); + Volume( const Volume& other ); + ~Volume(); + + QString name() const; + QString path() const; + KDByteSize size() const; + KDByteSize availableSpace() const; + + void swap( Volume& other ); + Volume& operator=( const Volume& other ); + bool operator == ( const Volume& other ) const; + + private: + void setPath( const QString& path ); + void setName( const QString& name ); + void setSize( const KDByteSize& size ); + void setAvailableSpace( const KDByteSize& available ); + + private: + class Private; + QSharedDataPointer<Private> d; + }; + + struct ProcessInfo + { + quint32 id; + QString name; + }; + + static OperatingSystemType osType(); + static KDVersion osVersion(); + static QString osDescription(); + static ArchitectureType architecture(); + + static KDByteSize installedMemory(); + static QList< Volume > mountedVolumes(); + static QList< ProcessInfo > runningProcesses(); +}; + +class QDebug; + +QDebug operator<<( QDebug dbg, KDSysInfo::OperatingSystemType type ); +QDebug operator<<( QDebug dbg, KDSysInfo::ArchitectureType type ); +QDebug operator<<( QDebug dbg, KDSysInfo::Volume volume ); +QDebug operator<<( QDebug dbg, KDSysInfo::ProcessInfo process ); + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_mac.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_mac.cpp new file mode 100644 index 000000000..024d1dd4d --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_mac.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdsysinfo.h" + +#include "kdbytesize.h" +#include "kdversion.h" + +#include <Carbon/Carbon.h> + +static QString qt_mac_hfsunistr_to_qstring( const HFSUniStr255* hfs ) +{ + const QChar* const charPointer = reinterpret_cast< const QChar* >( hfs->unicode ); + return QString( charPointer, hfs->length ); +} + +KDSysInfo::OperatingSystemType KDSysInfo::osType() +{ + return MacOSX; +} + +KDVersion KDSysInfo::osVersion() +{ + SInt32 major = 0; + SInt32 minor = 0; + SInt32 bugfix = 0; + Gestalt( gestaltSystemVersionMajor, &major ); + Gestalt( gestaltSystemVersionMinor, &minor ); + Gestalt( gestaltSystemVersionBugFix, &bugfix ); + + QStringList result; + result << QString::number( major ); + result << QString::number( minor ); + result << QString::number( bugfix ); + + return KDVersion::fromString( result.join( QChar::fromLatin1( '.' ) ) ); +} + +QString KDSysInfo::osDescription() +{ + return QObject::tr( "Mac OS X Version %1" ).arg( osVersion().toString() ); +} + +KDByteSize KDSysInfo::installedMemory() +{ + SInt32 mb = 0; + Gestalt( gestaltPhysicalRAMSizeInMegabytes, &mb ); + return KDByteSize( static_cast< quint64 >( mb ) * 1024LL * 1024LL ); +} + +KDSysInfo::ArchitectureType KDSysInfo::architecture() +{ + SInt32 arch = 0; + Gestalt( gestaltSysArchitecture, &arch ); + switch( arch ) + { + case gestalt68k: + return Motorola68k; + case gestaltPowerPC: + return PowerPC; + case gestaltIntel: + return Intel; + default: + return UnknownArchitecture; + } +} + +QList< KDSysInfo::Volume > KDSysInfo::mountedVolumes() +{ + QList< KDSysInfo::Volume > result; + FSVolumeRefNum volume; + FSVolumeInfo info; + HFSUniStr255 volName; + FSRef ref; + int i = 0; + + while( FSGetVolumeInfo( kFSInvalidVolumeRefNum, ++i, &volume, kFSVolInfoFSInfo, &info, &volName, &ref ) == 0 ) + { + UInt8 path[ PATH_MAX + 1 ]; + if( FSRefMakePath( &ref, path, PATH_MAX ) == 0 ) + { + const QString name = qt_mac_hfsunistr_to_qstring( &volName ); + const QString mount = QString::fromLocal8Bit( reinterpret_cast< char* >( path ) ); + FSGetVolumeInfo( volume, 0, 0, kFSVolInfoSizes, &info, 0, 0 ); + Volume v; + v.setName( name ); + v.setPath( mount ); + v.setSize( KDByteSize( info.totalBytes ) ); + v.setAvailableSpace( KDByteSize( info.freeBytes ) ); + result.push_back( v ); + } + } + + return result; +} + +QList< KDSysInfo::ProcessInfo > KDSysInfo::runningProcesses() +{ + return QList< KDSysInfo::ProcessInfo >(); +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_win.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_win.cpp new file mode 100644 index 000000000..3456b89cb --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_win.cpp @@ -0,0 +1,323 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdsysinfo.h" + +#include "kdbytesize.h" +#include "kdversion.h" + +#include <windows.h> +#include <Tlhelp32.h> +#include <Psapi.h> + +#include <QDir> +#include <QPair> + +#include <QtCore/QLibrary> + +#define KDSYSINFO_PROCESS_QUERY_LIMITED_INFORMATION (0x1000) + +KDSysInfo::OperatingSystemType KDSysInfo::osType() +{ + return Windows; +} + +KDVersion KDSysInfo::osVersion() +{ + OSVERSIONINFO info; + memset( &info, 0, sizeof( info ) ); + info.dwOSVersionInfoSize = sizeof( info ); + GetVersionEx( &info ); + return KDVersion::fromString( QString::fromLatin1( "%1.%2" ).arg( info.dwMajorVersion ).arg( info.dwMinorVersion ) ); +} + +QString KDSysInfo::osDescription() +{ + switch( QSysInfo::windowsVersion() ) + { + case QSysInfo::WV_32s: + return QObject::tr( "Windows 3.1" ); + case QSysInfo::WV_95: + return QObject::tr( "Windows 95" ); + case QSysInfo::WV_98: + return QObject::tr( "Windows 98" ); + case QSysInfo::WV_Me: + return QObject::tr( "Windows ME" ); + case QSysInfo::WV_NT: + return QObject::tr( "Windows NT" ); + case QSysInfo::WV_2000: + return QObject::tr( "Windows 2000" ); + case QSysInfo::WV_XP: + return QObject::tr( "Windows XP" ); + case QSysInfo::WV_2003: + return QObject::tr( "Windows 2003" ); + case QSysInfo::WV_VISTA: + return QObject::tr( "Windows Vista" ); + case QSysInfo::WV_WINDOWS7: + return QObject::tr( "Windows 7" ); + case QSysInfo::WV_CE: + return QObject::tr( "Windows CE" ); + case QSysInfo::WV_CENET: + return QObject::tr( "Windows CE .NET" ); + case QSysInfo::WV_CE_5: + return QObject::tr( "Windows CE 5" ); + case QSysInfo::WV_CE_6: + return QObject::tr( "Windows CE 6" ); + default: + return QObject::tr( "Windows" ); + } +} + +KDByteSize KDSysInfo::installedMemory() +{ + MEMORYSTATUSEX status; + status.dwLength = sizeof( status ); + GlobalMemoryStatusEx( &status ); + return KDByteSize( status.ullTotalPhys ); +} + +KDSysInfo::ArchitectureType KDSysInfo::architecture() +{ +#if defined( _M_X64 ) + return AMD64; +#elif defined( _M_IX86 ) + return Intel; +#elif defined( _M_IA64 ) + return IA64; +#elif defined( _M_PPC ) + return PowerPC; +#elif defined( _M_ARM ) + return ARM; +#else + return UnknownArchitecture; +#endif +} + +QPair< KDByteSize, KDByteSize > volumeSpace( const QString& volume ) +{ + QPair< KDByteSize, KDByteSize > result; + ULARGE_INTEGER bytes; + ULARGE_INTEGER freebytes; + if( GetDiskFreeSpaceExA( qPrintable( volume ), 0, &bytes, &freebytes ) != 0 ) + { + result.first = KDByteSize( bytes.QuadPart ); + result.second = KDByteSize( freebytes.QuadPart ); + } + return result; +} + +QString volumeName( const QString& volume ) +{ + char name[ MAX_PATH + 1 ] = ""; + DWORD dummy; + char dummy2[ MAX_PATH + 1 ] = ""; + GetVolumeInformationA( qPrintable( volume ), name, MAX_PATH + 1, &dummy, &dummy, &dummy, dummy2, MAX_PATH + 1 ); + QString vName = QString::fromLatin1( name ); + if( vName.isEmpty() ) + { + const uint driveType = GetDriveTypeA( qPrintable( volume ) ); + switch( driveType ) + { + case DRIVE_REMOVABLE: + vName = QObject::tr( "Removable Disk" ); + break; + case DRIVE_CDROM: + vName = QObject::tr( "CD Drive" ); + break; + case DRIVE_FIXED: + vName = QObject::tr( "Local Disk" ); + break; + default: + return volume.left( 2 ); + } + } + return QString::fromLatin1( "%2 (%1)" ).arg( volume.left( 2 ), vName ); +} + +QList< KDSysInfo::Volume > KDSysInfo::mountedVolumes() +{ + QList< Volume > result; + const QFileInfoList drives = QDir::drives(); + for( QFileInfoList::const_iterator it = drives.constBegin(); it != drives.constEnd(); ++it ) + { + Volume volume; + const QString path = QDir::toNativeSeparators( it->path() ); + volume.setPath( path ); + volume.setName( volumeName( path ) ); + const QPair< KDByteSize, KDByteSize > sizes = volumeSpace( path ); + volume.setSize( sizes.first ); + volume.setAvailableSpace( sizes.second ); + result.push_back( volume ); + } + return result; +} + +struct EnumWindowsProcParam +{ + QList< KDSysInfo::ProcessInfo > processes; + QList< quint32 > seenIDs; +}; + +//BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam ) +//{ +// EnumWindowsProcParam* const list = reinterpret_cast< EnumWindowsProcParam* >( lParam ); + +// KDSysInfo::ProcessInfo info; + +// // process id +// DWORD procID = 0; +// GetWindowThreadProcessId( hWnd, &procID ); +// info.id = procID; + +// if( list->seenIDs.contains( info.id ) ) +// return TRUE; +// list->seenIDs.push_back( info.id ); + +// HINSTANCE inst = (HINSTANCE)GetWindowLongA( hWnd, GWL_HINSTANCE ); +// // process name +// HANDLE process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, procID ); + +// static char buffer[ 1024 ]; +// if( GetModuleFileNameExA( process, inst, buffer, sizeof( buffer ) ) ) +// info.name = QString::fromLocal8Bit( buffer ); +// CloseHandle( process ); + +// list->processes.push_back( info ); +// return TRUE; +//} + +//QList< KDSysInfo::ProcessInfo > KDSysInfo::runningProcesses() +//{ +// EnumWindowsProcParam param; +// HANDLE snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); +// if ( !snapshot ) +// return param.processes; +// PROCESSENTRY32 processStruct; +// processStruct.dwSize = sizeof( PROCESSENTRY32 ); +// bool foundProcess = Process32First( snapshot, &processStruct ); +// while ( foundProcess ) +// { +// //const QString executableName = QString::fromWCharArray( processStruct.szExeFile ); +// KDSysInfo::ProcessInfo info; +// HANDLE procHandle = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, false, processStruct.th32ProcessID ); +// char buffer[ 1024 ]; +// DWORD bufferSize = 1024; +// const bool succ = QueryFullProcessImageNameA( procHandle, 0, buffer, &bufferSize ); +// if ( succ ) +// { +// const QString executablepath = QString::fromLatin1( buffer ); +// const quint32 pid = processStruct.th32ProcessID; +// param.seenIDs.append( pid ); + +// info.id = pid; +// info.name = executablepath; +// param.processes.append( info ); +// } +// CloseHandle( procHandle ); + +// foundProcess = Process32Next( snapshot, &processStruct ); +// } +// if ( snapshot ) +// CloseHandle( snapshot ); +//// EnumDesktopWindows( 0, &EnumWindowsProc, reinterpret_cast< LPARAM >( ¶m ) ); +// return param.processes; +//} +typedef BOOL ( WINAPI *QueryFullProcessImageNamePtr )( HANDLE, DWORD, char*, PDWORD ); +typedef DWORD ( WINAPI *GetProcessImageFileNamePtr )( HANDLE, char*, DWORD ); +QList< KDSysInfo::ProcessInfo > KDSysInfo::runningProcesses() +{ + EnumWindowsProcParam param; + HANDLE snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); + if ( !snapshot ) + return param.processes; + PROCESSENTRY32 processStruct; + processStruct.dwSize = sizeof( PROCESSENTRY32 ); + bool foundProcess = Process32First( snapshot, &processStruct ); + const DWORD bufferSize = 1024; + char driveBuffer[ bufferSize ]; + QStringList deviceList; + if ( QSysInfo::windowsVersion() <= QSysInfo::WV_5_2 ) + { + DWORD size = GetLogicalDriveStringsA( bufferSize, driveBuffer ); + deviceList = QString::fromLatin1( driveBuffer, size ).split( QLatin1Char( (char)0 ), QString::SkipEmptyParts ); + } + + QLibrary kernel32( QLatin1String( "Kernel32.dll" ) ); + kernel32.load(); + void* pQueryFullProcessImageNameA = kernel32.resolve( "QueryFullProcessImageNameA" ); + + QLibrary psapi( QLatin1String ( "Psapi.dll" ) ); + psapi.load(); + void* pGetProcessImageFileNamePtr = psapi.resolve( "GetProcessImageFileNameA" ); + QueryFullProcessImageNamePtr callPtr = ( QueryFullProcessImageNamePtr ) pQueryFullProcessImageNameA; + GetProcessImageFileNamePtr callPtrXp = ( GetProcessImageFileNamePtr ) pGetProcessImageFileNamePtr; + while ( foundProcess ) + { + HANDLE procHandle = OpenProcess( QSysInfo::windowsVersion() > QSysInfo::WV_5_2 ? + KDSYSINFO_PROCESS_QUERY_LIMITED_INFORMATION : + PROCESS_QUERY_INFORMATION + , false, + processStruct.th32ProcessID ); + + char buffer[ 1024 ]; + DWORD bufferSize = 1024; + bool succ = false; + QString whichFailed; + QString executablePath; + KDSysInfo::ProcessInfo info; + if ( QSysInfo::windowsVersion() > QSysInfo::WV_5_2 ) + { + succ = callPtr( procHandle, 0, buffer, &bufferSize ); + executablePath = QString::fromLatin1( buffer ); + } + else + { + if (pGetProcessImageFileNamePtr) { + succ = callPtrXp( procHandle, buffer, bufferSize ); + executablePath = QString::fromLatin1( buffer ); + for ( int i = 0; i < deviceList.count(); ++i ) + { + executablePath.replace( QString::fromLatin1( "\\Device\\HarddiskVolume%1\\" ).arg( i + 1 ), deviceList.at( i ) ); + } + } + + } + if ( succ ) + { + const quint32 pid = processStruct.th32ProcessID; + param.seenIDs.append( pid ); + + info.id = pid; + info.name = executablePath; + param.processes.append( info ); + } + + CloseHandle( procHandle ); + foundProcess = Process32Next( snapshot, &processStruct ); + + } + if ( snapshot ) + CloseHandle( snapshot ); + + kernel32.unload(); + return param.processes; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_x11.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_x11.cpp new file mode 100644 index 000000000..a9d609d4d --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdsysinfo_x11.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdsysinfo.h" + +#include "kdbytesize.h" +#include "kdversion.h" + +#include <sys/utsname.h> +#include <sys/statvfs.h> + +#include <QtCore/QFile> +#include <QtCore/QTextStream> +#include <QtCore/QDir> +#include <QtCore/QFileInfo> + +KDSysInfo::OperatingSystemType KDSysInfo::osType() +{ +#if defined( Q_OS_LINUX ) + return Linux; +#else + UnknownOperatingSystem; +#endif +} + +KDVersion KDSysInfo::osVersion() +{ +#if defined( Q_OS_LINUX ) + struct utsname info; + uname( &info ); + QString v = QString::fromLatin1( info.release ); + if( v.contains( QString::fromLatin1( "-" ) ) ) + v = v.mid( 0, v.indexOf( QString::fromLatin1( "-" ) ) ); + + return KDVersion::fromString( v ); +#else + return KDVersion(); +#endif +} + +QString KDSysInfo::osDescription() +{ +#if defined( Q_OS_LINUX ) + return QObject::tr( "Linux %1" ).arg( osVersion().toString() ); +#else + return QString(); +#endif +} + +KDByteSize KDSysInfo::installedMemory() +{ +#ifdef Q_OS_LINUX + QFile f( QLatin1String( "/proc/meminfo" ) ); + f.open( QIODevice::ReadOnly ); + QTextStream stream( &f ); + while( true ) + { + const QString s = stream.readLine(); + if( !s.startsWith( QLatin1String( "MemTotal:" ) ) ) + continue; + else if( s.isEmpty() ) + return KDByteSize(); + + const QStringList parts = s.split( QLatin1Char( ' ' ), QString::SkipEmptyParts ); + return KDByteSize( parts.at(1).toInt() * 1024LL ); + } +#else + quint64 physmem; + size_t len = sizeof physmem; + static int mib[2] = { CTL_HW, HW_MEMSIZE }; + sysctl( mib, 2, &physmem, &len, 0, 0 ); + return KDByteSize( physmem ); +#endif + return KDByteSize(); +} + +KDSysInfo::ArchitectureType KDSysInfo::architecture() +{ + return UnknownArchitecture; +} + +QList< KDSysInfo::Volume > KDSysInfo::mountedVolumes() +{ + QList< Volume > result; + + QFile f( QLatin1String( "/etc/mtab" ) ); + if ( !f.open( QIODevice::ReadOnly ) ) { + qCritical( "%s: Could not open %s: %s", Q_FUNC_INFO, qPrintable(f.fileName()), qPrintable(f.errorString()) ); + return QList<KDSysInfo::Volume>(); //better error-handling? + } + + QTextStream stream( &f ); + while( true ) + { + const QString s = stream.readLine(); + if ( s.isNull() ) + return result; + + if( !s.startsWith( QLatin1Char( '/' ) ) ) + continue; + + const QStringList parts = s.split( QLatin1Char( ' ' ), QString::SkipEmptyParts ); + + Volume v; + v.setName( parts.at( 1 ) ); + v.setPath( parts.at( 1 ) ); + + struct statvfs data; + if( statvfs( qPrintable( v.name() ), &data ) == 0 ) + { + v.setSize( KDByteSize( static_cast< quint64 >( data.f_blocks ) * data.f_bsize ) ); + v.setAvailableSpace( KDByteSize( static_cast< quint64> ( data.f_bfree ) * data.f_bsize ) ); + } + + result.push_back( v ); + } + + return result; +} + +QList< KDSysInfo::ProcessInfo > KDSysInfo::runningProcesses() +{ + QList< KDSysInfo::ProcessInfo > processes; + QDir procDir( QLatin1String( "/proc" ) ); + const QFileInfoList procCont = procDir.entryInfoList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable ); + QRegExp validator( QLatin1String( "[0-9]+" ) ); + Q_FOREACH( const QFileInfo& info, procCont ) + { + if ( validator.exactMatch( info.fileName() ) ) + { + const QString linkPath = QDir( info.absoluteFilePath() ).absoluteFilePath( QLatin1String( "exe" ) ); + const QFileInfo linkInfo( linkPath ); + if ( linkInfo.exists() ) + { + KDSysInfo::ProcessInfo processInfo; + processInfo.name = linkInfo.symLinkTarget(); + processInfo.id = info.fileName().toInt(); + processes.append( processInfo ); + } + } + } + return processes; +} + diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.cpp new file mode 100644 index 000000000..f41d7974e --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdtoolsglobal.h" + +#include <QByteArray> + +#include <algorithm> + +namespace { + struct Version { + unsigned char v[3]; + }; + + static inline bool operator<( const Version & lhs, const Version & rhs ) { + return std::lexicographical_compare( lhs.v, lhs.v + 3, rhs.v, rhs.v + 3 ); + } + static inline bool operator==( const Version & lhs, const Version & rhs ) { + return std::equal( lhs.v, lhs.v + 3, rhs.v ); + } + KDTOOLS_MAKE_RELATION_OPERATORS( Version, static inline ) +} + +static Version kdParseQtVersion( const char * const version ) { + if ( !version || qstrlen( version ) < 5 || version[1] != '.' || version[3] != '.' || ( version[5] != 0 && version[5] != '.' && version[5] != '-' ) ) + return Version(); // parse error + const Version result = { { version[0] - '0', version[2] - '0', version[4] - '0' } }; + return result; +} + +bool _kdCheckQtVersion_impl( int major, int minor, int patchlevel ) { + static const Version actual = kdParseQtVersion( qVersion() ); // do this only once each run... + const Version requested = { { major, minor, patchlevel } }; + return actual >= requested; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.h new file mode 100644 index 000000000..3d687da39 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdtoolsglobal.h @@ -0,0 +1,135 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLS_KDTOOLSGLOBAL_H__ +#define __KDTOOLS_KDTOOLSGLOBAL_H__ + +#include <QtCore/QtGlobal> + +#define KDAB_DISABLE_COPY( x ) private: x( const x & ); x & operator=( const x & ) + +#ifdef KDTOOLS_SHARED +# ifdef BUILD_SHARED_KDTOOLSCORE +# define KDTOOLSCORE_EXPORT Q_DECL_EXPORT +# else +# define KDTOOLSCORE_EXPORT Q_DECL_IMPORT +# endif +# ifdef BUILD_SHARED_KDTOOLSGUI +# define KDTOOLSGUI_EXPORT Q_DECL_EXPORT +# else +# define KDTOOLSGUI_EXPORT Q_DECL_IMPORT +# endif +# ifdef BUILD_SHARED_KDTOOLSXML +# define KDTOOLSXML_EXPORT Q_DECL_EXPORT +# else +# define KDTOOLSXML_EXPORT Q_DECL_IMPORT +# endif +# ifdef BUILD_SHARED_KDUPDATER +# define KDTOOLS_UPDATER_EXPORT Q_DECL_EXPORT +# else +# define KDTOOLS_UPDATER_EXPORT Q_DECL_IMPORT +# endif +#else // KDTOOLS_SHARED +# define KDTOOLSCORE_EXPORT +# define KDTOOLSGUI_EXPORT +# define KDTOOLSXML_EXPORT +# define KDTOOLS_UPDATER_EXPORT +#endif // KDTOOLS_SHARED + +#define MAKEINCLUDES_EXPORT + +#define DOXYGEN_PROPERTY( x ) +#ifdef DOXYGEN_RUN +# define KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR( func ) operator unspecified_bool_type() const { return func; } +# define KDAB_USING_SAFE_BOOL_OPERATOR( Class ) operator unspecified_bool_type() const; +#else +# define KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR( func ) \ + private: struct __safe_bool_dummy__ { void nonnull() {} }; \ + typedef void ( __safe_bool_dummy__::*unspecified_bool_type )(); \ + public: \ + operator unspecified_bool_type() const { \ + return ( func ) ? &__safe_bool_dummy__::nonnull : 0 ; \ + } +#define KDAB_USING_SAFE_BOOL_OPERATOR( Class ) \ + using Class::operator Class::unspecified_bool_type; +#endif + +#define KDTOOLS_MAKE_RELATION_OPERATORS( Class, linkage ) \ + linkage bool operator>( const Class & lhs, const Class & rhs ) { \ + return operator<( rhs, lhs ); \ + } \ + linkage bool operator!=( const Class & lhs, const Class & rhs ) { \ + return !operator==( lhs, rhs ); \ + } \ + linkage bool operator<=( const Class & lhs, const Class & rhs ) { \ + return !operator>( lhs, rhs ); \ + } \ + linkage bool operator>=( const Class & lhs, const Class & rhs ) { \ + return !operator<( lhs, rhs ); \ + } + +template <typename T> +inline T & __kdtools__dereference_for_methodcall( T & o ) { + return o; +} + +template <typename T> +inline T & __kdtools__dereference_for_methodcall( T * o ) { + return *o; +} + +#define KDAB_SET_OBJECT_NAME( x ) __kdtools__dereference_for_methodcall( x ).setObjectName( QLatin1String( #x ) ) + +KDTOOLSCORE_EXPORT bool _kdCheckQtVersion_impl( int major, int minor=0, int patchlevel=0 ); +static inline bool kdCheckQtVersion( unsigned int major, unsigned int minor=0, unsigned int patchlevel=0 ) { + return (major<<16|minor<<8|patchlevel) <= static_cast<unsigned int>(QT_VERSION) + || _kdCheckQtVersion_impl( major, minor, patchlevel ); +} + +#define KDTOOLS_DECLARE_PRIVATE_BASE( Class ) \ +protected: \ + class Private; \ + Private * d_func() { return _d; } \ + const Private * d_func() const { return _d; } \ + Class( Private * _d_, bool b ) : _d( _d_ ) { init(b); } \ +private: \ + void init(bool); \ +private: \ + Private * _d + +#define KDTOOLS_DECLARE_PRIVATE_DERIVED( Class, Base ) \ +protected: \ + class Private; \ + Private * d_func() { \ + return reinterpret_cast<Private*>( Base::d_func() ); \ + } \ + const Private * d_func() const { \ + return reinterpret_cast<const Private*>( Base::d_func() ); \ + } \ + Class( Private * _d_, bool b ) \ + : Base( reinterpret_cast<Base::Private*>(_d_), b ) { init(b); } \ +private: \ + void init(bool) + + +#endif /* __KDTOOLS_KDTOOLSGLOBAL_H__ */ + diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.cpp new file mode 100644 index 000000000..53f91412f --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdversion.h" + +#include <QDebug> + +KDVersion::KDVersion() +{ +} + +KDVersion::~KDVersion() +{ +} + +bool KDVersion::isNull() const +{ + return parts.isEmpty(); +} + +QString KDVersion::toString() const +{ + return parts.join( QChar::fromLatin1( '.' ) ); +} + +KDVersion KDVersion::fromString( const QString& string ) +{ + KDVersion result; + result.parts = string.split( QChar::fromLatin1( '.' ) ); + return result; +} + +bool operator<( const KDVersion& lhs, const KDVersion& rhs ) +{ + for( int i = 0; i < lhs.parts.count(); ++i ) + { + if( i == rhs.parts.count() ) + return false; + + const QString& l = lhs.parts[ i ]; + const QString& r = lhs.parts[ i ]; + + bool okl = false; + bool okr = false; + const int li = l.toInt( &okl ); + const int ri = r.toInt( &okr ); + + if( okl && okr ) + { + if( li < ri ) + return true; + } + else if( QString::localeAwareCompare( l, r ) < 0 ) + { + return true; + } + } + return true; +} + +bool operator==( const KDVersion& lhs, const KDVersion& rhs ) +{ + return lhs.parts == rhs.parts; +} + +QDebug operator<<( QDebug debug, const KDVersion& version ) +{ + return debug << "KDVersion(" << version.toString().toLatin1().data() << ")"; + return debug; +} diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.h new file mode 100644 index 000000000..b71cb1933 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdversion.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef KDVERSION_H +#define KDVERSION_H + +#include <KDToolsCore/kdtoolsglobal.h> + +#include <QtCore/QStringList> + +class KDTOOLSCORE_EXPORT KDVersion +{ + friend bool operator<( const KDVersion& lhs, const KDVersion& rhs ); + friend bool operator==( const KDVersion& lhs, const KDVersion& rhs ); +public: + KDVersion(); + virtual ~KDVersion(); + + bool isNull() const; + + QString toString() const; + static KDVersion fromString( const QString& string ); + +private: + QStringList parts; +}; + +bool operator<( const KDVersion& lhs, const KDVersion& rhs ); +bool operator==( const KDVersion& lhs, const KDVersion& rhs ); + +QDebug operator<<( QDebug debug, const KDVersion& version ); + +#endif diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.cpp new file mode 100644 index 000000000..7c0abd486 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "kdwatchdog.h" + +#include <QTimer> + +/*! + \internal + */ +class KDWatchdog::Private +{ +public: + Private() + : active( true ) + { + } + + bool active; + QTimer timer; +}; + +/*! + Creates a new KDWatchdog with \a parent. + */ +KDWatchdog::KDWatchdog( QObject* parent ) + : QObject( parent ) +{ + d->timer.setSingleShot( true ); + connect( &d->timer, SIGNAL( timeout() ), this, SIGNAL( timeout() ) ); + setTimeoutInterval( 30000 ); + activate(); +} + +/*! + Destroys the KDWatchdog. + */ +KDWatchdog::~KDWatchdog() +{ +} + +bool KDWatchdog::isActive() const +{ + return d->active; +} + +int KDWatchdog::timeoutInterval() const +{ + return d->timer.interval(); +} + +void KDWatchdog::setTimeoutInterval( int interval ) +{ + d->timer.setInterval( interval ); + resetTimeoutTimer(); +} + +void KDWatchdog::setActive( bool active ) +{ + if( d->active == active ) + return; + d->active = active; + if( d->active ) + d->timer.start(); + else + d->timer.stop(); +} + +/*! + Activates the watchdog. + */ +void KDWatchdog::activate() +{ + setActive( true ); +} + +/*! + Deactivates the watchdog. + */ +void KDWatchdog::deactivate() +{ + setActive( false ); +} + +/*! + Resets the watchdog timer. This slot should be called whenever + the watched event occures. + If the watchdog has been stop()'ed, nothing happens. + */ +void KDWatchdog::resetTimeoutTimer() +{ + if( d->active ) + d->timer.start(); +} + +#include "moc_kdwatchdog.cpp" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.h new file mode 100644 index 000000000..4319b0e8b --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/kdwatchdog.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLS__CORE__KDWATCHDOG_H__ +#define __KDTOOLS__CORE__KDWATCHDOG_H__ + +#include <QtCore/QObject> + +#include <KDToolsCore/pimpl_ptr.h> + +class KDTOOLSCORE_EXPORT KDWatchdog : public QObject +{ + Q_OBJECT + Q_PROPERTY( bool active READ isActive WRITE setActive ) + Q_PROPERTY( int timeoutInterval READ timeoutInterval WRITE setTimeoutInterval ) + +public: + explicit KDWatchdog( QObject* parent = 0 ); + ~KDWatchdog(); + + bool isActive() const; + int timeoutInterval() const; + +public Q_SLOTS: + void setTimeoutInterval( int interval ); + + void setActive( bool active ); + void activate(); + void deactivate(); + + void resetTimeoutTimer(); + +Q_SIGNALS: + void timeout(); + +private: + class Private; + kdtools::pimpl_ptr< Private > d; +}; + +#endif /* __KDTOOLS__CORE__KDWATCHDOG_H__ */ diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr new file mode 100644 index 000000000..aa2f3984a --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr @@ -0,0 +1 @@ +#include "pimpl_ptr.h" diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.cpp b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.cpp new file mode 100644 index 000000000..1a7f017a1 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include "pimpl_ptr.h" + +/*! + \class pimpl_ptr: + \ingroup core smartptr + \brief Owning pointer for private implementations + \since_c 2.1 + + (The exception safety of this class has not been evaluated yet.) + + pimpl_ptr is a smart immutable pointer, which owns the contained object. Unlike other smart pointers, + it creates a standard constructed object when instanciated via the + \link pimpl_ptr() standard constructor\endlink. + Additionally, pimpl_ptr respects constness of the pointer object and returns \c const \c T* for + a const pimpl_ptr object. + + The content of a pimpl_ptr cannot be changed during it's lifetime. + + \section general-use General Use + + The general use case of pimpl_ptr is the "Pimpl Idiom", i.e. hiding the private implementation of a class + from the user's compiler which see \c MyClass as + + \code + class MyClass + { + public: + MyClass(); + ~MyClass(); + + // public class API + int value() const; + + private: + class Private; // defined later + kdtools::pimpl_ptr< Private > d; + }; + \endcode + + but not the private parts of it. These can only be seen (and accessed) by the code knowing \c MyClass::Private: + + \code + class MyClass::Private + { + public: + int value; + }; + + MyClass::MyClass() + { + // d was automatically filled with new Private + d->value = 42; + } + + MyClass::~MyClass() + { + // the content of d gets deleted automatically + } + + int MyClass::value() const + { + // access the private part: + // since MyClass::value() is const, the returned pointee is const, too + return d->value; + } + \endcode + +*/ + +/*! + \fn pimpl_ptr::pimpl_ptr() + + Default constructor. Constructs a pimpl_tr that contains (owns) a standard constructed + instance of \c T. + + \post \c *this owns a new object. +*/ + +/*! + \fn pimpl_ptr::pimpl_ptr( T * t ) + + Constructor. Constructs a pimpl_ptr that contains (owns) \a t. + + \post get() == obj +*/ + +/*! + \fn pimpl_ptr::~pimpl_ptr() + + Destructor. + + \post The object previously owned by \c *this has been deleted. +*/ + +/*! + \fn const T * pimpl_ptr::get() const + + \returns a const pointer to the contained (owned) object. + \overload +*/ + +/*! + \fn T * pimpl_ptr::get() + + \returns a pointer to the contained (owned) object. +*/ + +/*! + \fn const T & pimpl_ptr::operator*() const + + Dereference operator. Returns \link get() *get()\endlink. + \overload +*/ + +/*! + \fn T & pimpl_ptr::operator*() + + Dereference operator. Returns \link get() *get()\endlink. +*/ + +/*! + \fn const T * pimpl_ptr::operator->() const + + Member-by-pointer operator. Returns get(). + \overload +*/ + +/*! + \fn T * pimpl_ptr::operator->() + + Member-by-pointer operator. Returns get(). +*/ + +#ifdef KDTOOLSCORE_UNITTESTS + +#include <KDUnitTest/test.h> + +#include <QObject> +#include <QPointer> + +namespace +{ + struct ConstTester + { + bool isConst() + { + return false; + } + + bool isConst() const + { + return true; + } + }; +} + +KDAB_UNITTEST_SIMPLE( pimpl_ptr, "kdcoretools" ) { + + { + kdtools::pimpl_ptr< QObject > p; + assertNotNull( p.get() ); + assertNull( p->parent() ); + } + + + { + QPointer< QObject > o; + { + kdtools::pimpl_ptr< QObject > qobject( new QObject ); + o = qobject.get(); + assertEqual( o, qobject.operator->() ); + assertEqual( o, &(qobject.operator*()) ); + } + assertNull( o ); + } + + { + const kdtools::pimpl_ptr< QObject > qobject( new QObject ); + const QObject* o = qobject.get(); + assertEqual( o, qobject.operator->() ); + assertEqual( o, &(qobject.operator*()) ); + } + + { + kdtools::pimpl_ptr< QObject > o1; + assertTrue( o1 ); + kdtools::pimpl_ptr< QObject > o2( 0 ); + assertFalse( o2 ); + } + + { + const kdtools::pimpl_ptr< ConstTester > o1; + kdtools::pimpl_ptr< ConstTester > o2; + assertTrue( o1->isConst() ); + assertFalse( o2->isConst() ); + assertTrue( (*o1).isConst() ); + assertFalse( (*o2).isConst() ); + assertTrue( o1.get()->isConst() ); + assertFalse( o2.get()->isConst() ); + } +} + +#endif // KDTOOLSCORE_UNITTESTS diff --git a/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.h b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.h new file mode 100644 index 000000000..195b967d1 --- /dev/null +++ b/installerbuilder/libinstaller/kdtools/KDToolsCore/pimpl_ptr.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#ifndef __KDTOOLSCORE__PIMPL_PTR_H__ +#define __KDTOOLSCORE__PIMPL_PTR_H__ + +#include <KDToolsCore/kdtoolsglobal.h> + +#ifndef DOXYGEN_RUN +namespace kdtools { +#endif + + template <typename T> + class MAKEINCLUDES_EXPORT pimpl_ptr { + KDAB_DISABLE_COPY( pimpl_ptr ); + T * d; + public: + pimpl_ptr() : d( new T ) {} + explicit pimpl_ptr( T * t ) : d( t ) {} + ~pimpl_ptr() { delete d; d = 0; } + + T * get() { return d; } + const T * get() const { return d; } + + T * operator->() { return get(); } + const T * operator->() const { return get(); } + + T & operator*() { return *get(); } + const T & operator*() const { return *get(); } + + KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR( get() ) + }; + + // these are not implemented, so's we can catch their use at + // link-time. Leaving them undeclared would open up a comparison + // via operator unspecified-bool-type(). + template <typename T, typename S> + void operator==( const pimpl_ptr<T> &, const pimpl_ptr<S> & ); + template <typename T, typename S> + void operator!=( const pimpl_ptr<T> &, const pimpl_ptr<S> & ); + +#ifndef DOXYGEN_RUN +} // namespace kdtools +#endif + +#endif /* __KDTOOLSCORE__PIMPL_PTR_H__ */ + |