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 
00100 QCA_EXPORT QByteArray methodReturnType(const QMetaObject *obj, const QByteArray &method, const QList<QByteArray> argTypes);
00101 
00143 QCA_EXPORT bool invokeMethodWithVariants(QObject *obj, const QByteArray &method, const QVariantList &args, QVariant *ret, Qt::ConnectionType type = Qt::AutoConnection);
00144 
00271 class QCA_EXPORT SyncThread : public QThread
00272 {
00273         Q_OBJECT
00274 public:
00278         SyncThread(QObject *parent = 0);
00279 
00285         ~SyncThread();
00286 
00292         void start();
00293 
00299         void stop();
00300 
00319         QVariant call(QObject *obj, const QByteArray &method, const QVariantList &args = QVariantList(), bool *ok = 0);
00320 
00321 protected:
00325         virtual void atStart() = 0;
00326 
00330         virtual void atEnd() = 0;
00331 
00335         virtual void run();
00336 
00337 private:
00338         Q_DISABLE_COPY(SyncThread)
00339 
00340         class Private;
00341         friend class Private;
00342         Private *d;
00343 };
00344 
00345 class QCA_EXPORT Synchronizer : public QObject
00346 {
00347         Q_OBJECT
00348 public:
00349         Synchronizer(QObject *parent);
00350         ~Synchronizer();
00351 
00352         bool waitForCondition(int msecs = -1);
00353         void conditionMet();
00354 
00355 private:
00356         Q_DISABLE_COPY(Synchronizer)
00357 
00358         class Private;
00359         Private *d;
00360 };
00361 
00362 class QCA_EXPORT DirWatch : public QObject
00363 {
00364         Q_OBJECT
00365 public:
00366         explicit DirWatch(const QString &dir = QString(), QObject *parent = 0);
00367         ~DirWatch();
00368 
00369         QString dirName() const;
00370         void setDirName(const QString &dir);
00371 
00372 Q_SIGNALS:
00373         void changed();
00374 
00375 private:
00376         Q_DISABLE_COPY(DirWatch)
00377 
00378         class Private;
00379         friend class Private;
00380         Private *d;
00381 };
00382 
00393 class QCA_EXPORT FileWatch : public QObject
00394 {
00395         Q_OBJECT
00396 public:
00404         explicit FileWatch(const QString &file = QString(), QObject *parent = 0);
00405         ~FileWatch();
00406 
00410         QString fileName() const;
00411 
00417         void setFileName(const QString &file);
00418 
00419 Q_SIGNALS:
00424         void changed();
00425 
00426 private:
00427         Q_DISABLE_COPY(FileWatch)
00428 
00429         class Private;
00430         friend class Private;
00431         Private *d;
00432 };
00433 
00434 class ConsolePrivate;
00435 class ConsoleReferencePrivate;
00436 class ConsoleReference;
00437 
00438 // QCA Console system
00439 //
00440 // QCA provides an API for asynchronous, event-based access to
00441 //   the console and stdin/stdout, as these facilities are
00442 //   otherwise not portable.  The primary use of this system within
00443 //   QCA is for passphrase prompting in command-line applications,
00444 //   using the tty console type.
00445 //
00446 // How it works: Create a Console object for the type of console
00447 //   desired, and then use ConsoleReference to act on the console.
00448 //   Only one ConsoleReference may operate on a Console at a time.
00449 //
00450 // A Console object overtakes either the physical console (tty
00451 //   type) or stdin/stdout (stdio type).  Only one of each type
00452 //   may be created at a time.
00453 //
00454 // Whenever code is written that needs a tty or stdio object, the
00455 //   code should first call one of the static methods (ttyInstance
00456 //   or stdioInstance) to see if a console object for the desired
00457 //   type exists already.  If the object exists, use it.  If it does
00458 //   not exist, the rule is that the relevant code should create the
00459 //   object, use the object, and then destroy the object when the
00460 //   operation is completed.
00461 //
00462 // By following the above rule, you can write code that utilizes
00463 //   a console without the application having to create some master
00464 //   console object for you.  Of course, if the application has
00465 //   created a console then it will be used.
00466 //
00467 // Why make a master console object?  The primary reason is that it
00468 //   is not guaranteed that all I/O will survive creation and
00469 //   destruction of a console object.  If you are using the stdio
00470 //   type, then you probably want a long-lived console object.  It
00471 //   is possible to capture unprocessed I/O by calling
00472 //   bytesLeftToRead or bytesLeftToWrite.  However, it is not
00473 //   expected that general console-needing code will call these
00474 //   functions when utilizing a temporary console.  Thus, an
00475 //   application developer would need to create his own console
00476 //   object, invoke the console-needing code, and then do his own
00477 //   extraction of the unprocessed I/O if necessary.  Another reason
00478 //   to extract unprocessed I/O is if you need to switch from
00479 //   QCA::Console back to standard functions (e.g. fgets).
00480 //
00481 class QCA_EXPORT Console : public QObject
00482 {
00483         Q_OBJECT
00484 public:
00485         enum Type
00486         {
00487                 Tty,         // physical console
00488                 Stdio        // stdin/stdout
00489         };
00490 
00491         enum ChannelMode
00492         {
00493                 Read,        // stdin
00494                 ReadWrite    // stdin + stdout
00495         };
00496 
00497         enum TerminalMode
00498         {
00499                 Default,     // use default terminal settings
00500                 Interactive  // char-by-char input, no echo
00501         };
00502 
00503         Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent = 0);
00504         ~Console();
00505 
00506         Type type() const;
00507         ChannelMode channelMode() const;
00508         TerminalMode terminalMode() const;
00509 
00510         static bool isStdinRedirected();
00511         static bool isStdoutRedirected();
00512 
00513         static Console *ttyInstance();
00514         static Console *stdioInstance();
00515 
00516         // call release() to get access to unempty buffers
00517         void release();
00518         QByteArray bytesLeftToRead();
00519         QByteArray bytesLeftToWrite();
00520 
00521 private:
00522         Q_DISABLE_COPY(Console)
00523 
00524         friend class ConsolePrivate;
00525         ConsolePrivate *d;
00526 
00527         friend class ConsoleReference;
00528 };
00529 
00530 // note: only one ConsoleReference object can be active at a time
00531 class QCA_EXPORT ConsoleReference : public QObject
00532 {
00533         Q_OBJECT
00534 public:
00535         enum SecurityMode
00536         {
00537                 SecurityDisabled,
00538                 SecurityEnabled
00539         };
00540 
00541         ConsoleReference(QObject *parent = 0);
00542         ~ConsoleReference();
00543 
00544         bool start(Console *console, SecurityMode mode = SecurityDisabled);
00545         void stop();
00546 
00547         Console *console() const;
00548         SecurityMode securityMode() const;
00549 
00550         // normal i/o
00551         QByteArray read(int bytes = -1);
00552         void write(const QByteArray &a);
00553 
00554         // secure i/o
00555         SecureArray readSecure(int bytes = -1);
00556         void writeSecure(const SecureArray &a);
00557 
00558         // close write channel (only if writing enabled)
00559         void closeOutput();
00560 
00561         int bytesAvailable() const;
00562         int bytesToWrite() const;
00563 
00564 Q_SIGNALS:
00565         void readyRead();
00566         void bytesWritten(int bytes);
00567         void inputClosed();
00568         void outputClosed();
00569 
00570 private:
00571         Q_DISABLE_COPY(ConsoleReference)
00572 
00573         friend class ConsoleReferencePrivate;
00574         ConsoleReferencePrivate *d;
00575 
00576         friend class Console;
00577 };
00578 
00579 class QCA_EXPORT ConsolePrompt : public QObject
00580 {
00581         Q_OBJECT
00582 public:
00583         ConsolePrompt(QObject *parent = 0);
00584         ~ConsolePrompt();
00585 
00586         void getHidden(const QString &promptStr);
00587         void getChar();
00588         void waitForFinished();
00589 
00590         SecureArray result() const;
00591         QChar resultChar() const;
00592 
00593 signals:
00594         void finished();
00595 
00596 private:
00597         Q_DISABLE_COPY(ConsolePrompt)
00598 
00599         class Private;
00600         friend class Private;
00601         Private *d;
00602 };
00603 
00604 class AbstractLogDevice;
00605 
00627 class QCA_EXPORT Logger : public QObject
00628 {
00629         Q_OBJECT
00630 public:
00637         enum Severity
00638         {
00639                 Quiet = 0,       
00640                 Emergency = 1,   
00641                 Alert = 2,       
00642                 Critical = 3,    
00643                 Error = 4,       
00644                 Warning = 5,     
00645                 Notice = 6,      
00646                 Information = 7, 
00647                 Debug = 8        
00648         };
00649 
00655         inline Logger::Severity level() const { return m_logLevel; }
00656 
00664         void setLevel(Logger::Severity level);
00665 
00671         void logTextMessage(const QString &message, Severity = Information);
00672 
00682         void logBinaryMessage(const QByteArray &blob, Severity = Information);
00683 
00689         void registerLogDevice(AbstractLogDevice *logger);
00690 
00698         void unregisterLogDevice(const QString &loggerName);
00699 
00703         QStringList currentLogDevices() const;
00704 
00705 private:
00706         Q_DISABLE_COPY(Logger)
00707 
00708         friend class Global;
00709 
00713         Logger();
00714 
00715         ~Logger();
00716 
00717         QStringList m_loggerNames;
00718         QList<AbstractLogDevice*> m_loggers;
00719         Severity m_logLevel;
00720 };
00721 
00729 class QCA_EXPORT AbstractLogDevice : public QObject
00730 {
00731         Q_OBJECT
00732 public:
00736         QString name() const;
00737 
00745         virtual void logTextMessage(const QString &message, enum Logger::Severity severity);
00746 
00754         virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity);
00755 
00756 protected:
00763         explicit AbstractLogDevice(const QString &name, QObject *parent = 0);
00764 
00765         virtual ~AbstractLogDevice() = 0;
00766 
00767 private:
00768         Q_DISABLE_COPY(AbstractLogDevice)
00769 
00770         class Private;
00771         Private *d;
00772 
00773         QString m_name;
00774 };
00775 
00776 }
00777 
00778 #endif

Generated on Tue Aug 28 08:19:59 2007 for Qt Cryptographic Architecture by  doxygen 1.5.2