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 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
102 QCA_EXPORT int methodReturnType(const QMetaObject *obj, const QByteArray &method, const QList<QByteArray> &argTypes);
103 #else
104 QCA_EXPORT QByteArray methodReturnType(const QMetaObject *obj,
105  const QByteArray &method,
106  const QList<QByteArray> argTypes);
107 #endif
108 
150 QCA_EXPORT bool invokeMethodWithVariants(QObject *obj,
151  const QByteArray &method,
152  const QVariantList &args,
153  QVariant *ret,
154  Qt::ConnectionType type = Qt::AutoConnection);
155 
279 class QCA_EXPORT SyncThread : public QThread
280 {
281  Q_OBJECT
282 public:
288  SyncThread(QObject *parent = nullptr);
289 
295  ~SyncThread() override;
296 
302  void start();
303 
309  void stop();
310 
329  QVariant
330  call(QObject *obj, const QByteArray &method, const QVariantList &args = QVariantList(), bool *ok = nullptr);
331 
332 protected:
336  virtual void atStart() = 0;
337 
341  virtual void atEnd() = 0;
342 
346  void run() override;
347 
348 private:
349  Q_DISABLE_COPY(SyncThread)
350 
351  class Private;
352  friend class Private;
353  Private *d;
354 };
355 
361 class QCA_EXPORT Synchronizer : public QObject
362 {
363  Q_OBJECT
364 public:
371  ~Synchronizer() override;
372 
380  bool waitForCondition(int msecs = -1);
381 
385  void conditionMet();
386 
387 private:
388  Q_DISABLE_COPY(Synchronizer)
389 
390  class Private;
391  Private *d;
392 };
393 
409 class QCA_EXPORT DirWatch : public QObject
410 {
411  Q_OBJECT
412 public:
420  explicit DirWatch(const QString &dir = QString(), QObject *parent = nullptr);
421  ~DirWatch() override;
422 
426  QString dirName() const;
427 
433  void setDirName(const QString &dir);
434 
435 Q_SIGNALS:
442  void changed();
443 
444 private:
445  Q_DISABLE_COPY(DirWatch)
446 
447  class Private;
448  friend class Private;
449  Private *d;
450 };
451 
467 class QCA_EXPORT FileWatch : public QObject
468 {
469  Q_OBJECT
470 public:
478  explicit FileWatch(const QString &file = QString(), QObject *parent = nullptr);
479  ~FileWatch() override;
480 
484  QString fileName() const;
485 
491  void setFileName(const QString &file);
492 
493 Q_SIGNALS:
498  void changed();
499 
500 private:
501  Q_DISABLE_COPY(FileWatch)
502 
503  class Private;
504  friend class Private;
505  Private *d;
506 };
507 
508 class ConsolePrivate;
509 class ConsoleReferencePrivate;
510 class ConsoleReference;
511 
560 class QCA_EXPORT Console : public QObject
561 {
562  Q_OBJECT
563 public:
567  enum Type
568  {
569  Tty,
570  Stdio
571  };
576  {
578  ReadWrite
579  };
580 
585  {
587  Interactive
588  };
589 
607  Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent = nullptr);
608  ~Console() override;
609 
613  Type type() const;
614 
619 
624 
630  static bool isStdinRedirected();
631 
637  static bool isStdoutRedirected();
638 
645  static Console *ttyInstance();
646 
654 
660  void release();
661 
666  QByteArray bytesLeftToRead();
667 
672  QByteArray bytesLeftToWrite();
673 
674 private:
675  Q_DISABLE_COPY(Console)
676 
677  friend class ConsolePrivate;
678  ConsolePrivate *d;
679 
680  friend class ConsoleReference;
681 };
682 
692 class QCA_EXPORT ConsoleReference : public QObject
693 {
694  Q_OBJECT
695 public:
700  {
701  SecurityDisabled,
702  SecurityEnabled
703  };
704 
710  ConsoleReference(QObject *parent = nullptr);
711  ~ConsoleReference() override;
712 
726  bool start(Console *console, SecurityMode mode = SecurityDisabled);
727 
731  void stop();
732 
738  Console *console() const;
739 
747 
757  QByteArray read(int bytes = -1);
758 
767  void write(const QByteArray &a);
768 
777  SecureArray readSecure(int bytes = -1);
778 
786  void writeSecure(const SecureArray &a);
787 
794  void closeOutput();
795 
800  int bytesAvailable() const;
801 
806  int bytesToWrite() const;
807 
808 Q_SIGNALS:
813  void readyRead();
814 
822  void bytesWritten(int bytes);
823 
827  void inputClosed();
828 
832  void outputClosed();
833 
834 private:
835  Q_DISABLE_COPY(ConsoleReference)
836 
837  friend class ConsoleReferencePrivate;
838  ConsoleReferencePrivate *d;
839 
840  friend class Console;
841 };
842 
863 class QCA_EXPORT ConsolePrompt : public QObject
864 {
865  Q_OBJECT
866 public:
872  ConsolePrompt(QObject *parent = nullptr);
873  ~ConsolePrompt() override;
874 
884  void getHidden(const QString &promptStr);
885 
891  void getChar();
892 
900 
909 
917  QChar resultChar() const;
918 
919 Q_SIGNALS:
929  void finished();
930 
931 private:
932  Q_DISABLE_COPY(ConsolePrompt)
933 
934  class Private;
935  friend class Private;
936  Private *d;
937 };
938 
939 class AbstractLogDevice;
940 
962 class QCA_EXPORT Logger : public QObject
963 {
964  Q_OBJECT
965 public:
972  enum Severity
973  {
974  Quiet = 0,
975  Emergency = 1,
976  Alert = 2,
977  Critical = 3,
978  Error = 4,
979  Warning = 5,
980  Notice = 6,
981  Information = 7,
982  Debug = 8
983  };
984 
990  inline Severity level() const
991  {
992  return m_logLevel;
993  }
994 
1002  void setLevel(Severity level);
1003 
1009  void logTextMessage(const QString &message, Severity = Information);
1010 
1020  void logBinaryMessage(const QByteArray &blob, Severity = Information);
1021 
1028 
1036  void unregisterLogDevice(const QString &loggerName);
1037 
1041  QStringList currentLogDevices() const;
1042 
1043 private:
1044  Q_DISABLE_COPY(Logger)
1045 
1046  friend class Global;
1047 
1051  Logger();
1052 
1053  ~Logger() override;
1054 
1055  QStringList m_loggerNames;
1056  QList<AbstractLogDevice *> m_loggers;
1057  Severity m_logLevel;
1058 };
1059 
1067 class QCA_EXPORT AbstractLogDevice : public QObject
1068 {
1069  Q_OBJECT
1070 public:
1074  QString name() const;
1075 
1086  virtual void logTextMessage(const QString &message, Logger::Severity severity);
1087 
1098  virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity);
1099 
1100 protected:
1107  explicit AbstractLogDevice(const QString &name, QObject *parent = nullptr);
1108 
1109  ~AbstractLogDevice() override = 0;
1110 
1111 private:
1112  Q_DISABLE_COPY(AbstractLogDevice)
1113 
1114  class Private;
1115  Private *d;
1116 
1117  QString m_name;
1118 };
1119 
1120 }
1121 
1122 #endif
An abstract log device.
Definition: qca_support.h:1068
QString name() const
The name of this log device.
AbstractLogDevice(const QString &name, QObject *parent=nullptr)
Create a new message logger.
virtual void logBinaryMessage(const QByteArray &blob, Logger::Severity severity)
Log a binary blob.
virtual void logTextMessage(const QString &message, Logger::Severity severity)
Log a message.
Console prompt handler.
Definition: qca_support.h:864
void getChar()
Obtain one character from the user.
QChar resultChar() const
Obtain the result of the user input.
void waitForFinished()
Block waiting for user input.
void finished()
Emitted when the user input activity has been completed.
void getHidden(const QString &promptStr)
Allow the user to enter data without it being echo'd to the terminal.
SecureArray result() const
Obtain the result of the user input.
ConsolePrompt(QObject *parent=nullptr)
Standard constructor.
Manager for a Console.
Definition: qca_support.h:693
SecurityMode securityMode() const
The security mode setting for the Console object managed by this object.
void outputClosed()
Emitted when the console output is closed.
void bytesWritten(int bytes)
Emitted when bytes are written to the Console.
void closeOutput()
Close the write channel.
void stop()
Stop processing, and release the Console.
ConsoleReference(QObject *parent=nullptr)
Standard constructor.
int bytesToWrite() const
The number of bytes remaining to be written to the Console being managed.
void write(const QByteArray &a)
Write data to the Console.
SecurityMode
The security setting to use for the Console being managed.
Definition: qca_support.h:700
void writeSecure(const SecureArray &a)
Write secure data to the Console.
int bytesAvailable() const
The number of bytes available to read from the Console being managed.
QByteArray read(int bytes=-1)
Read data from the Console.
Console * console() const
The Console object managed by this object.
SecureArray readSecure(int bytes=-1)
Read secure data from the Console.
void readyRead()
Emitted when there are bytes available to read from the Console being managed.
void inputClosed()
Emitted when the console input is closed.
bool start(Console *console, SecurityMode mode=SecurityDisabled)
Set the Console object to be managed, and start processing.
QCA Console system
Definition: qca_support.h:561
static Console * ttyInstance()
The current terminal-type console object.
Type
The type of console object.
Definition: qca_support.h:568
@ Tty
physical console
Definition: qca_support.h:569
Console(Type type, ChannelMode cmode, TerminalMode tmode, QObject *parent=nullptr)
Standard constructor.
ChannelMode
The type of I/O to use with the console object.
Definition: qca_support.h:576
@ Read
Read only (equivalent to stdin)
Definition: qca_support.h:577
ChannelMode channelMode() const
The ChannelMode of this Console object.
QByteArray bytesLeftToRead()
Obtain remaining data from the Console, awaiting a read operation.
Type type() const
The Type of this Console object.
QByteArray bytesLeftToWrite()
Obtain remaining data from the Console, awaiting a write operation.
void release()
Release the Console.
TerminalMode
The nature of the console operation.
Definition: qca_support.h:585
@ Default
use default terminal settings
Definition: qca_support.h:586
static bool isStdinRedirected()
Test whether standard input is redirected.
static Console * stdioInstance()
The current stdio-type console object.
TerminalMode terminalMode() const
The TerminalMode of this Console object.
static bool isStdoutRedirected()
Test whether standard output is redirected.
Support class to monitor a directory for activity.
Definition: qca_support.h:410
QString dirName() const
The name of the directory that is being monitored.
DirWatch(const QString &dir=QString(), QObject *parent=nullptr)
Standard constructor.
void changed()
The changed signal is emitted when the directory is changed (e.g. modified by addition or deletion of...
void setDirName(const QString &dir)
Change the directory being monitored.
Support class to monitor a file for activity.
Definition: qca_support.h:468
FileWatch(const QString &file=QString(), QObject *parent=nullptr)
Standard constructor.
QString fileName() const
The name of the file that is being monitored.
void setFileName(const QString &file)
Change the file being monitored.
void changed()
The changed signal is emitted when the file is changed (e.g.
A simple logging system.
Definition: qca_support.h:963
QStringList currentLogDevices() const
Get a list of the names of all registered log devices.
Severity level() const
Get the current logging level.
Definition: qca_support.h:990
void registerLogDevice(AbstractLogDevice *logger)
Add an AbstractLogDevice subclass to the existing list of loggers.
Severity
The severity of the message.
Definition: qca_support.h:973
void logBinaryMessage(const QByteArray &blob, Severity=Information)
Log a binary blob to all available log devices.
void setLevel(Severity level)
Set the current logging level.
void unregisterLogDevice(const QString &loggerName)
Remove an AbstractLogDevice subclass from the existing list of loggers.
void logTextMessage(const QString &message, Severity=Information)
Log a message to all available log devices.
Secure array of bytes.
Definition: qca_tools.h:317
Convenience class to run a thread and interact with it synchronously.
Definition: qca_support.h:280
QCA_EXPORT bool invokeMethodWithVariants(QObject *obj, const QByteArray &method, const QVariantList &args, QVariant *ret, Qt::ConnectionType type=Qt::AutoConnection)
Convenience method to invoke a method by name, using a variant list of arguments.
virtual void atStart()=0
Reimplement this to perform your initialization.
QVariant call(QObject *obj, const QByteArray &method, const QVariantList &args=QVariantList(), bool *ok=nullptr)
Calls a slot of an object in the thread.
void run() override
Starts the event loop and calls atStart and atStop as necessary.
QCA_EXPORT int methodReturnType(const QMetaObject *obj, const QByteArray &method, const QList< QByteArray > &argTypes)
Convenience method to determine the return type of a method.
void stop()
Stops the event loop of the thread, calls atStop() in the thread, and instructs the thread to finish.
SyncThread(QObject *parent=nullptr)
Standard constructor.
virtual void atEnd()=0
Reimplement this to perform your deinitialization.
void start()
Starts the thread, begins the event loop the thread, and then calls atStart() in the thread.
~SyncThread() override
Calls stop() and then destructs.
Enable synchronization between two threads.
Definition: qca_support.h:362
bool waitForCondition(int msecs=-1)
Call to pause execution in this thread.
void conditionMet()
Call to continue execution in the paused thread.
Synchronizer(QObject *parent)
Standard constructor.
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA_EXPORT Logger * logger()
Return a reference to the QCA Logger, which is used for diagnostics and error recording.
Header file for "tool" classes used in QCA.