File indexing completed on 2024-05-12 05:09:45

0001 /***************************************************************************
0002     Copyright (C) 2005-2020 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_FETCH_Z3950CONNECTION_H
0026 #define TELLICO_FETCH_Z3950CONNECTION_H
0027 
0028 #include <QThread>
0029 #include <QEvent>
0030 #include <QPointer>
0031 
0032 namespace Tellico {
0033   namespace Fetch {
0034     class Fetcher;
0035 
0036 class Z3950ResultFound : public QEvent {
0037 public:
0038   Z3950ResultFound(const QString& s);
0039   ~Z3950ResultFound();
0040   const QString& result() const { return m_result; }
0041 
0042   static QEvent::Type uid() { return static_cast<QEvent::Type>(QEvent::User + 11111); }
0043 
0044 private:
0045   Q_DISABLE_COPY(Z3950ResultFound)
0046   QString m_result;
0047 };
0048 
0049 class Z3950ConnectionDone : public QEvent {
0050 public:
0051   Z3950ConnectionDone(bool more) : QEvent(uid()), m_type(-1), m_hasMore(more) {}
0052   Z3950ConnectionDone(bool more, const QString& s, int t) : QEvent(uid()), m_msg(s), m_type(t), m_hasMore(more) {}
0053 
0054   const QString& message() const { return m_msg; }
0055   int messageType() const { return m_type; }
0056   bool hasMoreResults() const { return m_hasMore; }
0057 
0058   static QEvent::Type uid() { return static_cast<QEvent::Type>(QEvent::User + 22222); }
0059 
0060 private:
0061   Q_DISABLE_COPY(Z3950ConnectionDone)
0062   QString m_msg;
0063   int m_type;
0064   bool m_hasMore;
0065 };
0066 
0067 class Z3950SyntaxChange : public QEvent {
0068 public:
0069   Z3950SyntaxChange(const QString& s) : QEvent(uid()), m_syntax(s) {}
0070   const QString& syntax() const { return m_syntax; }
0071 
0072   static QEvent::Type uid() { return static_cast<QEvent::Type>(QEvent::User + 33333); }
0073 
0074 private:
0075   Q_DISABLE_COPY(Z3950SyntaxChange)
0076   QString m_syntax;
0077 };
0078 
0079 /**
0080  * @author Robby Stephenson
0081  */
0082 class Z3950Connection : public QThread {
0083 public:
0084   Z3950Connection(Fetcher* fetcher,
0085                   const QString& host,
0086                   uint port,
0087                   const QString& dbname,
0088                   const QString& syntax,
0089                   const QString& esn);
0090   ~Z3950Connection();
0091 
0092   void reset();
0093   void setQuery(const QString& query);
0094   void setUserPassword(const QString& user, const QString& pword);
0095   void setCharacterSet(const QString& queryCharSet, const QString& responseCharSet);
0096   void run() Q_DECL_OVERRIDE;
0097 
0098   void abort() { m_aborted = true; }
0099 
0100 private:
0101   static QByteArray iconvRun(const QByteArray& text, const QString& fromCharSet, const QString& toCharSet);
0102   static QString toXML(const QByteArray& marc, const QString& fromCharSet);
0103 
0104   bool makeConnection();
0105   void done();
0106   void done(const QString& message, int type);
0107   QByteArray queryToByteArray(const QString& text);
0108   QString responseToString(const QByteArray& text);
0109   void checkPendingEvents();
0110 
0111   class Private;
0112   Private* d;
0113 
0114   bool m_connected;
0115   bool m_aborted;
0116 
0117   QPointer<Fetcher> m_fetcher;
0118   QString m_host;
0119   uint m_port;
0120   QString m_dbname;
0121   QString m_user;
0122   QString m_password;
0123   QString m_queryCharSet;
0124   QString m_responseCharSet;
0125   QString m_syntax;
0126   QString m_pqn;
0127   QString m_esn;
0128   size_t m_start;
0129   size_t m_limit;
0130   bool m_hasMore;
0131 
0132   friend class Z3950ResultFound;
0133   static int resultsLeft;
0134 };
0135 
0136   } // end namespace
0137 } // end namespace
0138 
0139 #endif