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
An abstract log device.
Definition: qca_support.h:1067
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:863
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:692
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:699
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:560
static Console * ttyInstance()
The current terminal-type console object.
Type
The type of console object.
Definition: qca_support.h:567
@ Tty
physical console
Definition: qca_support.h:568
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:575
@ Read
Read only (equivalent to stdin)
Definition: qca_support.h:576
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:584
@ Default
use default terminal settings
Definition: qca_support.h:585
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:409
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:467
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:962
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:989
void registerLogDevice(AbstractLogDevice *logger)
Add an AbstractLogDevice subclass to the existing list of loggers.
Severity
The severity of the message.
Definition: qca_support.h:972
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:279
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:361
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.