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
An abstract log device.
Definition: qca_support.h:1057
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:856
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:685
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:692
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:553
static Console * ttyInstance()
The current terminal-type console object.
Type
The type of console object.
Definition: qca_support.h:560
@ Tty
physical console
Definition: qca_support.h:561
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:568
@ Read
Read only (equivalent to stdin)
Definition: qca_support.h:569
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:577
@ Default
use default terminal settings
Definition: qca_support.h:578
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:402
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.
void setDirName(const QString &dir)
Change the directory being monitored.
Support class to monitor a file for activity.
Definition: qca_support.h:460
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:955
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:982
void registerLogDevice(AbstractLogDevice *logger)
Add an AbstractLogDevice subclass to the existing list of loggers.
Severity
The severity of the message.
Definition: qca_support.h:965
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:273
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.
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:354
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.
Preprocessor magic to allow export of library symbols.
Header file for "tool" classes used in QCA.