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 "qca_export.h"
40 #include "qca_tools.h"
41 #include <QByteArray>
42 #include <QList>
43 #include <QMetaObject>
44 #include <QObject>
45 #include <QString>
46 #include <QStringList>
47 #include <QThread>
48 #include <QVariant>
49 #include <QVariantList>
50 
51 namespace QCA {
52 
101 QCA_EXPORT QByteArray methodReturnType(const QMetaObject * obj,
102  const QByteArray & method,
103  const QList<QByteArray> argTypes);
104 
146 QCA_EXPORT bool invokeMethodWithVariants(QObject * obj,
147  const QByteArray & method,
148  const QVariantList &args,
149  QVariant * ret,
150  Qt::ConnectionType type = Qt::AutoConnection);
151 
278 class QCA_EXPORT SyncThread : public QThread
279 {
280  Q_OBJECT
281 public:
287  SyncThread(QObject *parent = nullptr);
288 
294  ~SyncThread() override;
295 
301  void start();
302 
308  void stop();
309 
328  QVariant
329  call(QObject *obj, const QByteArray &method, const QVariantList &args = QVariantList(), bool *ok = nullptr);
330 
331 protected:
335  virtual void atStart() = 0;
336 
340  virtual void atEnd() = 0;
341 
345  void run() override;
346 
347 private:
348  Q_DISABLE_COPY(SyncThread)
349 
350  class Private;
351  friend class Private;
352  Private *d;
353 };
354 
360 class QCA_EXPORT Synchronizer : public QObject
361 {
362  Q_OBJECT
363 public:
370  ~Synchronizer() override;
371 
379  bool waitForCondition(int msecs = -1);
380 
384  void conditionMet();
385 
386 private:
387  Q_DISABLE_COPY(Synchronizer)
388 
389  class Private;
390  Private *d;
391 };
392 
408 class QCA_EXPORT DirWatch : public QObject
409 {
410  Q_OBJECT
411 public:
419  explicit DirWatch(const QString &dir = QString(), QObject *parent = nullptr);
420  ~DirWatch() override;
421 
425  QString dirName() const;
426 
432  void setDirName(const QString &dir);
433 
434 Q_SIGNALS:
441  void changed();
442 
443 private:
444  Q_DISABLE_COPY(DirWatch)
445 
446  class Private;
447  friend class Private;
448  Private *d;
449 };
450 
466 class QCA_EXPORT FileWatch : public QObject
467 {
468  Q_OBJECT
469 public:
477  explicit FileWatch(const QString &file = QString(), QObject *parent = nullptr);
478  ~FileWatch() override;
479 
483  QString fileName() const;
484 
490  void setFileName(const QString &file);
491 
492 Q_SIGNALS:
497  void changed();
498 
499 private:
500  Q_DISABLE_COPY(FileWatch)
501 
502  class Private;
503  friend class Private;
504  Private *d;
505 };
506 
507 class ConsolePrivate;
508 class ConsoleReferencePrivate;
509 class ConsoleReference;
510 
559 class QCA_EXPORT Console : public QObject
560 {
561  Q_OBJECT
562 public:
566  enum Type
567  {
568  Tty,
569  Stdio
570  };
575  {
577  ReadWrite
578  };
579 
584  {
586  Interactive
587  };
588 
606  Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent = nullptr);
607  ~Console() override;
608 
612  Type type() const;
613 
618 
623 
629  static bool isStdinRedirected();
630 
636  static bool isStdoutRedirected();
637 
644  static Console *ttyInstance();
645 
653 
659  void release();
660 
665  QByteArray bytesLeftToRead();
666 
671  QByteArray bytesLeftToWrite();
672 
673 private:
674  Q_DISABLE_COPY(Console)
675 
676  friend class ConsolePrivate;
677  ConsolePrivate *d;
678 
679  friend class ConsoleReference;
680 };
681 
691 class QCA_EXPORT ConsoleReference : public QObject
692 {
693  Q_OBJECT
694 public:
699  {
700  SecurityDisabled,
701  SecurityEnabled
702  };
703 
709  ConsoleReference(QObject *parent = nullptr);
710  ~ConsoleReference() override;
711 
725  bool start(Console *console, SecurityMode mode = SecurityDisabled);
726 
730  void stop();
731 
737  Console *console() const;
738 
746 
756  QByteArray read(int bytes = -1);
757 
766  void write(const QByteArray &a);
767 
776  SecureArray readSecure(int bytes = -1);
777 
785  void writeSecure(const SecureArray &a);
786 
793  void closeOutput();
794 
799  int bytesAvailable() const;
800 
805  int bytesToWrite() const;
806 
807 Q_SIGNALS:
812  void readyRead();
813 
821  void bytesWritten(int bytes);
822 
826  void inputClosed();
827 
831  void outputClosed();
832 
833 private:
834  Q_DISABLE_COPY(ConsoleReference)
835 
836  friend class ConsoleReferencePrivate;
837  ConsoleReferencePrivate *d;
838 
839  friend class Console;
840 };
841 
862 class QCA_EXPORT ConsolePrompt : public QObject
863 {
864  Q_OBJECT
865 public:
871  ConsolePrompt(QObject *parent = nullptr);
872  ~ConsolePrompt() override;
873 
883  void getHidden(const QString &promptStr);
884 
890  void getChar();
891 
899 
908 
916  QChar resultChar() const;
917 
918 Q_SIGNALS:
928  void finished();
929 
930 private:
931  Q_DISABLE_COPY(ConsolePrompt)
932 
933  class Private;
934  friend class Private;
935  Private *d;
936 };
937 
938 class AbstractLogDevice;
939 
961 class QCA_EXPORT Logger : public QObject
962 {
963  Q_OBJECT
964 public:
971  enum Severity
972  {
973  Quiet = 0,
974  Emergency = 1,
975  Alert = 2,
976  Critical = 3,
977  Error = 4,
978  Warning = 5,
979  Notice = 6,
980  Information = 7,
981  Debug = 8
982  };
983 
989  inline Severity level() const
990  {
991  return m_logLevel;
992  }
993 
1001  void setLevel(Severity level);
1002 
1008  void logTextMessage(const QString &message, Severity = Information);
1009 
1019  void logBinaryMessage(const QByteArray &blob, Severity = Information);
1020 
1027 
1035  void unregisterLogDevice(const QString &loggerName);
1036 
1040  QStringList currentLogDevices() const;
1041 
1042 private:
1043  Q_DISABLE_COPY(Logger)
1044 
1045  friend class Global;
1046 
1050  Logger();
1051 
1052  ~Logger() override;
1053 
1054  QStringList m_loggerNames;
1055  QList<AbstractLogDevice *> m_loggers;
1056  Severity m_logLevel;
1057 };
1058 
1066 class QCA_EXPORT AbstractLogDevice : public QObject
1067 {
1068  Q_OBJECT
1069 public:
1073  QString name() const;
1074 
1085  virtual void logTextMessage(const QString &message, Logger::Severity severity);
1086 
1097  virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity);
1098 
1099 protected:
1106  explicit AbstractLogDevice(const QString &name, QObject *parent = nullptr);
1107 
1108  ~AbstractLogDevice() override = 0;
1109 
1110 private:
1111  Q_DISABLE_COPY(AbstractLogDevice)
1112 
1113  class Private;
1114  Private *d;
1115 
1116  QString m_name;
1117 };
1118 
1119 }
1120 
1121 #endif
QCA::Logger
A simple logging system.
Definition: qca_support.h:962
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:575
QCA::Console::TerminalMode
TerminalMode
The nature of the console operation.
Definition: qca_support.h:584
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:567
QCA::Logger::Severity
Severity
The severity of the message.
Definition: qca_support.h:972
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
Manager for a Console.
Definition: qca_support.h:692
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:989
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
Preprocessor magic to allow export of library symbols.
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
Console prompt handler.
Definition: qca_support.h:863
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:576
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:568
QCA::ConsoleReference::read
QByteArray read(int bytes=-1)
Read data from the Console.
QCA::DirWatch
Support class to monitor a directory for activity.
Definition: qca_support.h:409
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
Secure array of bytes.
Definition: qca_tools.h:317
QCA::FileWatch::setFileName
void setFileName(const QString &file)
Change the file being monitored.
QCA::SyncThread
Convenience class to run a thread and interact with it synchronously.
Definition: qca_support.h:279
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
Enable synchronization between two threads.
Definition: qca_support.h:361
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
An abstract log device.
Definition: qca_support.h:1067
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:585
QCA::FileWatch
Support class to monitor a file for activity.
Definition: qca_support.h:467
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
QCA Console system
Definition: qca_support.h:560
qca_tools.h
Header file for "tool" classes used in QCA.
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:699
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.