Qt Cryptographic Architecture
qca_support.h
Go to the documentation of this file.
1 /*
2  * qca_support.h - Qt Cryptographic Architecture
3  * Copyright (C) 2003-2005 Justin Karneges <justin@affinix.com>
4  * Copyright (C) 2004,2005, 2007 Brad Hards <bradh@frogmouth.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22 
36 #ifndef QCA_SUPPORT_H
37 #define QCA_SUPPORT_H
38 
39 #include <QByteArray>
40 #include <QString>
41 #include <QObject>
42 #include <QVariant>
43 #include <QVariantList>
44 #include <QStringList>
45 #include <QList>
46 #include <QMetaObject>
47 #include <QThread>
48 #include "qca_export.h"
49 #include "qca_tools.h"
50 
51 namespace QCA {
52 
101 QCA_EXPORT QByteArray methodReturnType(const QMetaObject *obj, const QByteArray &method, const QList<QByteArray> argTypes);
102 
144 QCA_EXPORT bool invokeMethodWithVariants(QObject *obj, const QByteArray &method, const QVariantList &args, QVariant *ret, Qt::ConnectionType type = Qt::AutoConnection);
145 
272 class QCA_EXPORT SyncThread : public QThread
273 {
274  Q_OBJECT
275 public:
281  SyncThread(QObject *parent = nullptr);
282 
288  ~SyncThread() override;
289 
295  void start();
296 
302  void stop();
303 
322  QVariant call(QObject *obj, const QByteArray &method, const QVariantList &args = QVariantList(), bool *ok = nullptr);
323 
324 protected:
328  virtual void atStart() = 0;
329 
333  virtual void atEnd() = 0;
334 
338  void run() override;
339 
340 private:
341  Q_DISABLE_COPY(SyncThread)
342 
343  class Private;
344  friend class Private;
345  Private *d;
346 };
347 
353 class QCA_EXPORT Synchronizer : public QObject
354 {
355  Q_OBJECT
356 public:
363  ~Synchronizer() override;
364 
372  bool waitForCondition(int msecs = -1);
373 
377  void conditionMet();
378 
379 private:
380  Q_DISABLE_COPY(Synchronizer)
381 
382  class Private;
383  Private *d;
384 };
385 
401 class QCA_EXPORT DirWatch : public QObject
402 {
403  Q_OBJECT
404 public:
412  explicit DirWatch(const QString &dir = QString(), QObject *parent = nullptr);
413  ~DirWatch() override;
414 
418  QString dirName() const;
419 
425  void setDirName(const QString &dir);
426 
427 Q_SIGNALS:
434  void changed();
435 
436 private:
437  Q_DISABLE_COPY(DirWatch)
438 
439  class Private;
440  friend class Private;
441  Private *d;
442 };
443 
459 class QCA_EXPORT FileWatch : public QObject
460 {
461  Q_OBJECT
462 public:
470  explicit FileWatch(const QString &file = QString(), QObject *parent = nullptr);
471  ~FileWatch() override;
472 
476  QString fileName() const;
477 
483  void setFileName(const QString &file);
484 
485 Q_SIGNALS:
490  void changed();
491 
492 private:
493  Q_DISABLE_COPY(FileWatch)
494 
495  class Private;
496  friend class Private;
497  Private *d;
498 };
499 
500 class ConsolePrivate;
501 class ConsoleReferencePrivate;
502 class ConsoleReference;
503 
552 class QCA_EXPORT Console : public QObject
553 {
554  Q_OBJECT
555 public:
559  enum Type
560  {
561  Tty,
562  Stdio
563  };
568  {
570  ReadWrite
571  };
572 
577  {
579  Interactive
580  };
581 
599  Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent = nullptr);
600  ~Console() override;
601 
605  Type type() const;
606 
611 
616 
622  static bool isStdinRedirected();
623 
629  static bool isStdoutRedirected();
630 
637  static Console *ttyInstance();
638 
646 
652  void release();
653 
658  QByteArray bytesLeftToRead();
659 
664  QByteArray bytesLeftToWrite();
665 
666 private:
667  Q_DISABLE_COPY(Console)
668 
669  friend class ConsolePrivate;
670  ConsolePrivate *d;
671 
672  friend class ConsoleReference;
673 };
674 
684 class QCA_EXPORT ConsoleReference : public QObject
685 {
686  Q_OBJECT
687 public:
692  {
693  SecurityDisabled,
694  SecurityEnabled
695  };
696 
702  ConsoleReference(QObject *parent = nullptr);
703  ~ConsoleReference() override;
704 
718  bool start(Console *console, SecurityMode mode = SecurityDisabled);
719 
723  void stop();
724 
730  Console *console() const;
731 
739 
749  QByteArray read(int bytes = -1);
750 
759  void write(const QByteArray &a);
760 
769  SecureArray readSecure(int bytes = -1);
770 
778  void writeSecure(const SecureArray &a);
779 
786  void closeOutput();
787 
792  int bytesAvailable() const;
793 
798  int bytesToWrite() const;
799 
800 Q_SIGNALS:
805  void readyRead();
806 
814  void bytesWritten(int bytes);
815 
819  void inputClosed();
820 
824  void outputClosed();
825 
826 private:
827  Q_DISABLE_COPY(ConsoleReference)
828 
829  friend class ConsoleReferencePrivate;
830  ConsoleReferencePrivate *d;
831 
832  friend class Console;
833 };
834 
855 class QCA_EXPORT ConsolePrompt : public QObject
856 {
857  Q_OBJECT
858 public:
864  ConsolePrompt(QObject *parent = nullptr);
865  ~ConsolePrompt() override;
866 
876  void getHidden(const QString &promptStr);
877 
883  void getChar();
884 
892 
901 
909  QChar resultChar() const;
910 
911 Q_SIGNALS:
921  void finished();
922 
923 private:
924  Q_DISABLE_COPY(ConsolePrompt)
925 
926  class Private;
927  friend class Private;
928  Private *d;
929 };
930 
931 class AbstractLogDevice;
932 
954 class QCA_EXPORT Logger : public QObject
955 {
956  Q_OBJECT
957 public:
964  enum Severity
965  {
966  Quiet = 0,
967  Emergency = 1,
968  Alert = 2,
969  Critical = 3,
970  Error = 4,
971  Warning = 5,
972  Notice = 6,
973  Information = 7,
974  Debug = 8
975  };
976 
982  inline Severity level() const { return m_logLevel; }
983 
991  void setLevel(Severity level);
992 
998  void logTextMessage(const QString &message, Severity = Information);
999 
1009  void logBinaryMessage(const QByteArray &blob, Severity = Information);
1010 
1017 
1025  void unregisterLogDevice(const QString &loggerName);
1026 
1030  QStringList currentLogDevices() const;
1031 
1032 private:
1033  Q_DISABLE_COPY(Logger)
1034 
1035  friend class Global;
1036 
1040  Logger();
1041 
1042  ~Logger() override;
1043 
1044  QStringList m_loggerNames;
1045  QList<AbstractLogDevice*> m_loggers;
1046  Severity m_logLevel;
1047 };
1048 
1056 class QCA_EXPORT AbstractLogDevice : public QObject
1057 {
1058  Q_OBJECT
1059 public:
1063  QString name() const;
1064 
1075  virtual void logTextMessage(const QString &message, Logger::Severity severity);
1076 
1087  virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity);
1088 
1089 protected:
1096  explicit AbstractLogDevice(const QString &name, QObject *parent = nullptr);
1097 
1098  ~AbstractLogDevice() override = 0;
1099 
1100 private:
1101  Q_DISABLE_COPY(AbstractLogDevice)
1102 
1103  class Private;
1104  Private *d;
1105 
1106  QString m_name;
1107 };
1108 
1109 }
1110 
1111 #endif
QCA::Logger
Definition: qca_support.h:955
QObject
QCA::DirWatch::dirName
QString dirName() const
The name of the directory that is being monitored.
QCA::ConsoleReference::securityMode
SecurityMode securityMode() const
The security mode setting for the Console object managed by this object.
QCA::Console::ChannelMode
ChannelMode
The type of I/O to use with the console object.
Definition: qca_support.h:568
QCA::Console::TerminalMode
TerminalMode
The nature of the console operation.
Definition: qca_support.h:577
QCA::Synchronizer::conditionMet
void conditionMet()
Call to continue execution in the paused thread.
QCA::Console::Type
Type
The type of console object.
Definition: qca_support.h:560
QCA::Logger::Severity
Severity
The severity of the message.
Definition: qca_support.h:965
QCA::ConsoleReference::bytesWritten
void bytesWritten(int bytes)
Emitted when bytes are written to the Console.
QCA::DirWatch::changed
void changed()
The changed signal is emitted when the directory is changed (e.g.
QCA::SyncThread::atEnd
virtual void atEnd()=0
Reimplement this to perform your deinitialization.
QCA::SyncThread::call
QVariant call(QObject *obj, const QByteArray &method, const QVariantList &args=QVariantList(), bool *ok=nullptr)
Calls a slot of an object in the thread.
QCA::DirWatch::DirWatch
DirWatch(const QString &dir=QString(), QObject *parent=nullptr)
Standard constructor.
QCA::ConsoleReference
Definition: qca_support.h:685
QCA::Console::release
void release()
Release the Console.
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::ConsolePrompt::result
SecureArray result() const
Obtain the result of the user input.
QCA::Logger::level
Severity level() const
Get the current logging level.
Definition: qca_support.h:982
QCA::Logger::unregisterLogDevice
void unregisterLogDevice(const QString &loggerName)
Remove an AbstractLogDevice subclass from the existing list of loggers.
QCA::DirWatch::setDirName
void setDirName(const QString &dir)
Change the directory being monitored.
QCA::ConsoleReference::readyRead
void readyRead()
Emitted when there are bytes available to read from the Console being managed.
QList
qca_export.h
QCA::Logger::logBinaryMessage
void logBinaryMessage(const QByteArray &blob, Severity=Information)
Log a binary blob to all available log devices.
QCA::ConsolePrompt::waitForFinished
void waitForFinished()
Block waiting for user input.
QCA::Console::isStdinRedirected
static bool isStdinRedirected()
Test whether standard input is redirected.
QCA::Synchronizer::waitForCondition
bool waitForCondition(int msecs=-1)
Call to pause execution in this thread.
QCA::ConsolePrompt::getHidden
void getHidden(const QString &promptStr)
Allow the user to enter data without it being echo'd to the terminal.
QCA::ConsoleReference::write
void write(const QByteArray &a)
Write data to the Console.
QCA::SyncThread::start
void start()
Starts the thread, begins the event loop the thread, and then calls atStart() in the thread.
QCA::ConsolePrompt::getChar
void getChar()
Obtain one character from the user.
QCA::AbstractLogDevice::logTextMessage
virtual void logTextMessage(const QString &message, Logger::Severity severity)
Log a message.
QCA::ConsolePrompt::finished
void finished()
Emitted when the user input activity has been completed.
QCA::ConsolePrompt
Definition: qca_support.h:856
QCA::ConsoleReference::outputClosed
void outputClosed()
Emitted when the console output is closed.
QCA::Console::Read
@ Read
Read only (equivalent to stdin)
Definition: qca_support.h:569
QCA::Logger::currentLogDevices
QStringList currentLogDevices() const
Get a list of the names of all registered log devices.
QCA::Console::Tty
@ Tty
physical console
Definition: qca_support.h:561
QCA::ConsoleReference::read
QByteArray read(int bytes=-1)
Read data from the Console.
QCA::DirWatch
Definition: qca_support.h:402
QCA::logger
QCA_EXPORT Logger * logger()
Return a reference to the QCA Logger, which is used for diagnostics and error recording.
QCA::ConsoleReference::bytesAvailable
int bytesAvailable() const
The number of bytes available to read from the Console being managed.
QCA::Console::channelMode
ChannelMode channelMode() const
The ChannelMode of this Console object.
QCA::SecureArray
Definition: qca_tools.h:317
QCA::FileWatch::setFileName
void setFileName(const QString &file)
Change the file being monitored.
QCA::SyncThread
Definition: qca_support.h:273
QCA::SyncThread::run
void run() override
Starts the event loop and calls atStart and atStop as necessary.
QCA::AbstractLogDevice::logBinaryMessage
virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity)
Log a binary blob.
QCA::ConsoleReference::bytesToWrite
int bytesToWrite() const
The number of bytes remaining to be written to the Console being managed.
QCA::ConsoleReference::closeOutput
void closeOutput()
Close the write channel.
QCA::Console::stdioInstance
static Console * stdioInstance()
The current stdio-type console object.
QCA::ConsoleReference::inputClosed
void inputClosed()
Emitted when the console input is closed.
QCA::FileWatch::fileName
QString fileName() const
The name of the file that is being monitored.
QCA::ConsoleReference::start
bool start(Console *console, SecurityMode mode=SecurityDisabled)
Set the Console object to be managed, and start processing.
QCA::Synchronizer
Definition: qca_support.h:354
QCA::SyncThread::~SyncThread
~SyncThread() override
Calls stop() and then destructs.
QCA::ConsoleReference::console
Console * console() const
The Console object managed by this object.
QCA::AbstractLogDevice::AbstractLogDevice
AbstractLogDevice(const QString &name, QObject *parent=nullptr)
Create a new message logger.
QCA::Console::terminalMode
TerminalMode terminalMode() const
The TerminalMode of this Console object.
QCA::Console::isStdoutRedirected
static bool isStdoutRedirected()
Test whether standard output is redirected.
QCA::ConsoleReference::writeSecure
void writeSecure(const SecureArray &a)
Write secure data to the Console.
QCA::FileWatch::changed
void changed()
The changed signal is emitted when the file is changed (e.g.
QCA::AbstractLogDevice
Definition: qca_support.h:1057
QCA::SyncThread::stop
void stop()
Stops the event loop of the thread, calls atStop() in the thread, and instructs the thread to finish.
QCA::ConsoleReference::readSecure
SecureArray readSecure(int bytes=-1)
Read secure data from the Console.
QCA::Console::Default
@ Default
use default terminal settings
Definition: qca_support.h:578
QCA::FileWatch
Definition: qca_support.h:460
QCA::ConsolePrompt::ConsolePrompt
ConsolePrompt(QObject *parent=nullptr)
Standard constructor.
QCA::ConsoleReference::stop
void stop()
Stop processing, and release the Console.
QCA::Console::ttyInstance
static Console * ttyInstance()
The current terminal-type console object.
QCA::Logger::logTextMessage
void logTextMessage(const QString &message, Severity=Information)
Log a message to all available log devices.
QThread
QCA::Logger::registerLogDevice
void registerLogDevice(AbstractLogDevice *logger)
Add an AbstractLogDevice subclass to the existing list of loggers.
QCA::AbstractLogDevice::name
QString name() const
The name of this log device.
QCA::FileWatch::FileWatch
FileWatch(const QString &file=QString(), QObject *parent=nullptr)
Standard constructor.
QCA::SyncThread::SyncThread
SyncThread(QObject *parent=nullptr)
Standard constructor.
QCA::Console::bytesLeftToWrite
QByteArray bytesLeftToWrite()
Obtain remaining data from the Console, awaiting a write operation.
QCA::Console::Console
Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent=nullptr)
Standard constructor.
QCA::Logger::setLevel
void setLevel(Severity level)
Set the current logging level.
QCA::Console
Definition: qca_support.h:553
qca_tools.h
QCA::ConsoleReference::ConsoleReference
ConsoleReference(QObject *parent=nullptr)
Standard constructor.
QCA::ConsolePrompt::resultChar
QChar resultChar() const
Obtain the result of the user input.
QCA::ConsoleReference::SecurityMode
SecurityMode
The security setting to use for the Console being managed.
Definition: qca_support.h:692
QCA::Console::type
Type type() const
The Type of this Console object.
QCA::Console::bytesLeftToRead
QByteArray bytesLeftToRead()
Obtain remaining data from the Console, awaiting a read operation.
QCA::Synchronizer::Synchronizer
Synchronizer(QObject *parent)
Standard constructor.
QCA::SyncThread::atStart
virtual void atStart()=0
Reimplement this to perform your initialization.