qca_support.h

Go to the documentation of this file.
00001 /*
00002  * qca_support.h - Qt Cryptographic Architecture
00003  * Copyright (C) 2003-2005  Justin Karneges <justin@affinix.com>
00004  * Copyright (C) 2004,2005, 2007  Brad Hards <bradh@frogmouth.net>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00019  *
00020  */
00021 
00035 #ifndef QCA_SUPPORT_H
00036 #define QCA_SUPPORT_H
00037 
00038 #include <QByteArray>
00039 #include <QString>
00040 #include <QObject>
00041 #include <QVariant>
00042 #include <QVariantList>
00043 #include <QStringList>
00044 #include <QList>
00045 #include <QMetaObject>
00046 #include <QThread>
00047 #include "qca_export.h"
00048 #include "qca_tools.h"
00049 
00050 namespace QCA {
00051 
00098 QCA_EXPORT QByteArray methodReturnType(const QMetaObject *obj, const QByteArray &method, const QList<QByteArray> argTypes);
00099 
00139 QCA_EXPORT bool invokeMethodWithVariants(QObject *obj, const QByteArray &method, const QVariantList &args, QVariant *ret, Qt::ConnectionType type = Qt::AutoConnection);
00140 
00141 class QCA_EXPORT SyncThread : public QThread
00142 {
00143         Q_OBJECT
00144 public:
00145         SyncThread(QObject *parent = 0);
00146         ~SyncThread();
00147 
00148         void start();
00149         void stop();
00150         QVariant call(QObject *obj, const QByteArray &method, const QVariantList &args = QVariantList(), bool *ok = 0);
00151 
00152 protected:
00153         virtual void atStart() = 0;
00154         virtual void atEnd() = 0;
00155 
00156         // reimplemented
00157         virtual void run();
00158 
00159 private:
00160         Q_DISABLE_COPY(SyncThread)
00161 
00162         class Private;
00163         friend class Private;
00164         Private *d;
00165 };
00166 
00167 class QCA_EXPORT Synchronizer : public QObject
00168 {
00169         Q_OBJECT
00170 public:
00171         Synchronizer(QObject *parent);
00172         ~Synchronizer();
00173 
00174         bool waitForCondition(int msecs = -1);
00175         void conditionMet();
00176 
00177 private:
00178         Q_DISABLE_COPY(Synchronizer)
00179 
00180         class Private;
00181         Private *d;
00182 };
00183 
00184 class QCA_EXPORT DirWatch : public QObject
00185 {
00186         Q_OBJECT
00187 public:
00188         explicit DirWatch(const QString &dir = QString(), QObject *parent = 0);
00189         ~DirWatch();
00190 
00191         QString dirName() const;
00192         void setDirName(const QString &dir);
00193 
00194 Q_SIGNALS:
00195         void changed();
00196 
00197 private:
00198         Q_DISABLE_COPY(DirWatch)
00199 
00200         class Private;
00201         friend class Private;
00202         Private *d;
00203 };
00204 
00211 class QCA_EXPORT FileWatch : public QObject
00212 {
00213         Q_OBJECT
00214 public:
00222         explicit FileWatch(const QString &file = QString(), QObject *parent = 0);
00223         ~FileWatch();
00224 
00228         QString fileName() const;
00229 
00235         void setFileName(const QString &file);
00236 
00237 Q_SIGNALS:
00242         void changed();
00243 
00244 private:
00245         Q_DISABLE_COPY(FileWatch)
00246 
00247         class Private;
00248         friend class Private;
00249         Private *d;
00250 };
00251 
00252 class ConsolePrivate;
00253 class ConsoleReferencePrivate;
00254 class ConsoleReference;
00255 
00256 // QCA Console system
00257 //
00258 // QCA provides an API for asynchronous, event-based access to
00259 //   the console and stdin/stdout, as these facilities are
00260 //   otherwise not portable.  The primary use of this system within
00261 //   QCA is for passphrase prompting in command-line applications,
00262 //   using the tty console type.
00263 //
00264 // How it works: Create a Console object for the type of console
00265 //   desired, and then use ConsoleReference to act on the console.
00266 //   Only one ConsoleReference may operate on a Console at a time.
00267 //
00268 // A Console object overtakes either the physical console (tty
00269 //   type) or stdin/stdout (stdio type).  Only one of each type
00270 //   may be created at a time.
00271 //
00272 // Whenever code is written that needs a tty or stdio object, the
00273 //   code should first call one of the static methods (ttyInstance
00274 //   or stdioInstance) to see if a console object for the desired
00275 //   type exists already.  If the object exists, use it.  If it does
00276 //   not exist, the rule is that the relevant code should create the
00277 //   object, use the object, and then destroy the object when the
00278 //   operation is completed.
00279 //
00280 // By following the above rule, you can write code that utilizes
00281 //   a console without the application having to create some master
00282 //   console object for you.  Of course, if the application has
00283 //   created a console then it will be used.
00284 //
00285 // Why make a master console object?  The primary reason is that it
00286 //   is not guaranteed that all I/O will survive creation and
00287 //   destruction of a console object.  If you are using the stdio
00288 //   type, then you probably want a long-lived console object.  It
00289 //   is possible to capture unprocessed I/O by calling
00290 //   bytesLeftToRead or bytesLeftToWrite.  However, it is not
00291 //   expected that general console-needing code will call these
00292 //   functions when utilizing a temporary console.  Thus, an
00293 //   application developer would need to create his own console
00294 //   object, invoke the console-needing code, and then do his own
00295 //   extraction of the unprocessed I/O if necessary.  Another reason
00296 //   to extract unprocessed I/O is if you need to switch from
00297 //   QCA::Console back to standard functions (e.g. fgets).
00298 //
00299 class QCA_EXPORT Console : public QObject
00300 {
00301         Q_OBJECT
00302 public:
00303         enum Type
00304         {
00305                 Tty,         // physical console
00306                 Stdio        // stdin/stdout
00307         };
00308 
00309         enum ChannelMode
00310         {
00311                 Read,        // stdin
00312                 ReadWrite    // stdin + stdout
00313         };
00314 
00315         enum TerminalMode
00316         {
00317                 Default,     // use default terminal settings
00318                 Interactive  // char-by-char input, no echo
00319         };
00320 
00321         Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent = 0);
00322         ~Console();
00323 
00324         Type type() const;
00325         ChannelMode channelMode() const;
00326         TerminalMode terminalMode() const;
00327 
00328         static bool isStdinRedirected();
00329         static bool isStdoutRedirected();
00330 
00331         static Console *ttyInstance();
00332         static Console *stdioInstance();
00333 
00334         // call release() to get access to unempty buffers
00335         void release();
00336         QByteArray bytesLeftToRead();
00337         QByteArray bytesLeftToWrite();
00338 
00339 private:
00340         Q_DISABLE_COPY(Console)
00341 
00342         friend class ConsolePrivate;
00343         ConsolePrivate *d;
00344 
00345         friend class ConsoleReference;
00346 };
00347 
00348 // note: only one ConsoleReference object can be active at a time
00349 class QCA_EXPORT ConsoleReference : public QObject
00350 {
00351         Q_OBJECT
00352 public:
00353         enum SecurityMode
00354         {
00355                 SecurityDisabled,
00356                 SecurityEnabled
00357         };
00358 
00359         ConsoleReference(QObject *parent = 0);
00360         ~ConsoleReference();
00361 
00362         bool start(Console *console, SecurityMode mode = SecurityDisabled);
00363         void stop();
00364 
00365         Console *console() const;
00366         SecurityMode securityMode() const;
00367 
00368         // normal i/o
00369         QByteArray read(int bytes = -1);
00370         void write(const QByteArray &a);
00371 
00372         // secure i/o
00373         SecureArray readSecure(int bytes = -1);
00374         void writeSecure(const SecureArray &a);
00375 
00376         // close write channel (only if writing enabled)
00377         void closeOutput();
00378 
00379         int bytesAvailable() const;
00380         int bytesToWrite() const;
00381 
00382 Q_SIGNALS:
00383         void readyRead();
00384         void bytesWritten(int bytes);
00385         void inputClosed();
00386         void outputClosed();
00387 
00388 private:
00389         Q_DISABLE_COPY(ConsoleReference)
00390 
00391         friend class ConsoleReferencePrivate;
00392         ConsoleReferencePrivate *d;
00393 
00394         friend class Console;
00395 };
00396 
00397 class QCA_EXPORT ConsolePrompt : public QObject
00398 {
00399         Q_OBJECT
00400 public:
00401         ConsolePrompt(QObject *parent = 0);
00402         ~ConsolePrompt();
00403 
00404         void getHidden(const QString &promptStr);
00405         void getChar();
00406         void waitForFinished();
00407 
00408         SecureArray result() const;
00409         QChar resultChar() const;
00410 
00411 signals:
00412         void finished();
00413 
00414 private:
00415         Q_DISABLE_COPY(ConsolePrompt)
00416 
00417         class Private;
00418         friend class Private;
00419         Private *d;
00420 };
00421 
00422 class AbstractLogDevice;
00423 
00441 class QCA_EXPORT Logger : public QObject
00442 {
00443         Q_OBJECT
00444 public:
00451         enum Severity
00452         {
00453                 Quiet = 0,       
00454                 Emergency = 1,   
00455                 Alert = 2,       
00456                 Critical = 3,    
00457                 Error = 4,       
00458                 Warning = 5,     
00459                 Notice = 6,      
00460                 Information = 7, 
00461                 Debug = 8        
00462         };
00463 
00469         inline Logger::Severity level() const { return m_logLevel; }
00470 
00478         void setLevel(Logger::Severity level);
00479 
00485         void logTextMessage(const QString &message, Severity = Information);
00486 
00496         void logBinaryMessage(const QByteArray &blob, Severity = Information);
00497 
00503         void registerLogDevice(AbstractLogDevice *logger);
00504 
00512         void unregisterLogDevice(const QString &loggerName);
00513 
00517         QStringList currentLogDevices() const;
00518 
00519 private:
00520         Q_DISABLE_COPY(Logger)
00521 
00522         friend class Global;
00523 
00527         Logger();
00528 
00529         ~Logger();
00530 
00531         QStringList m_loggerNames;
00532         QList<AbstractLogDevice*> m_loggers;
00533         Severity m_logLevel;
00534 };
00535 
00539 class QCA_EXPORT AbstractLogDevice : public QObject
00540 {
00541         Q_OBJECT
00542 public:
00546         QString name() const;
00547 
00555         virtual void logTextMessage(const QString &message, enum Logger::Severity severity);
00556 
00564         virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity);
00565 
00566 protected:
00573         explicit AbstractLogDevice(const QString &name, QObject *parent = 0);
00574 
00575         virtual ~AbstractLogDevice() = 0;
00576 
00577 private:
00578         Q_DISABLE_COPY(AbstractLogDevice)
00579 
00580         class Private;
00581         Private *d;
00582 
00583         QString m_name;
00584 };
00585 
00586 }
00587 
00588 #endif

Generated on Fri Jul 6 13:22:42 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6