add DQT3_SUPPORT
[kdenetwork.git] / kdict / dict.h
blob07db9637491393a511bf6190a0c4d2df2cf1e2fd
1 /* -------------------------------------------------------------
3 dict.h (part of The KDE Dictionary Client)
5 Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
6 (C) by Matthias Hölzer 1998
8 This file is distributed under the Artistic License.
9 See LICENSE for details.
11 -------------------------------------------------------------
13 JobData used for data transfer between Client and Interface
14 DictAsyncClient all network related stuff happens here in an asynchrous thread
15 DictInterface interface for DictAsyncClient, job management
17 -------------------------------------------------------------*/
19 #ifndef _DICT_H_
20 #define _DICT_H_
22 #include <pthread.h>
23 #include <q3ptrlist.h>
24 #include <qsocketnotifier.h>
26 class QSocketNotifier;
27 struct in_addr;
30 //********* JobData ******************************************
33 class JobData
36 public:
38 enum QueryType { //type of transaction
39 TDefine=0,
40 TGetDefinitions,
41 TMatch,
42 TShowDatabases,
43 TShowDbInfo,
44 TShowStrategies,
45 TShowInfo,
46 TUpdate
49 enum ErrType { // error codes
50 ErrNoErr=0,
51 ErrCommunication, // display result!
52 ErrTimeout,
53 ErrBadHost,
54 ErrConnect, // display result!
55 ErrRefused,
56 ErrNotAvailable,
57 ErrSyntax,
58 ErrCommandNotImplemented,
59 ErrAccessDenied,
60 ErrAuthFailed,
61 ErrInvalidDbStrat,
62 ErrNoDatabases,
63 ErrNoStrategies,
64 ErrServerError, // display result!
65 ErrMsgTooLong
68 JobData(QueryType Ntype,bool NnewServer,QString const& Nserver,int Nport,
69 int NidleHold, int Ntimeout, int NpipeSize, QString const& Nencoding, bool NAuthEnabled,
70 QString const& Nuser, QString const& Nsecret, unsigned int NheadLayout);
72 QueryType type;
73 ErrType error;
75 bool canceled;
76 int numFetched;
77 QString result;
78 QStringList matches;
80 QString query;
81 QStringList defines;
83 bool newServer;
84 QString server;
85 int port, timeout, pipeSize, idleHold;
86 QString encoding;
87 bool authEnabled;
88 QString user, secret;
89 QStringList databases,strategies;
90 QString strategy;
91 unsigned int headLayout;
95 //********* DictAsyncClient ******************************************
98 class DictAsyncClient
101 public:
103 DictAsyncClient(int NfdPipeIn, int NfdPipeOut);
104 ~DictAsyncClient();
106 static void* startThread(void* pseudoThis);
108 void insertJob(JobData *newJob);
109 void removeJob();
111 private:
113 void waitForWork(); // main loop
114 void define();
115 bool getDefinitions();
116 bool match();
117 void showDatabases();
118 void showDbInfo();
119 void showStrategies();
120 void showInfo();
121 void update();
123 void openConnection(); // connect, handshake and authorization
124 void closeSocket();
125 void doQuit(); // send "quit" without timeout, without checks, close connection
126 bool waitForRead(); // used by getNextIntoBuffer()
127 bool waitForWrite(); // used by sendBuffer() & connect()
128 void clearPipe(); // remove start/stop signal
130 bool sendBuffer(); // send cmdBuffer to the server
131 bool getNextLine(); // set thisLine to next complete line of input
132 bool nextResponseOk(int code); // reads next line and checks the response code
133 bool getNextResponse(int &code); // reads next line and returns the response code
134 void handleErrors();
136 void resultAppend(const char* str);
137 void resultAppend(QString str);
139 JobData *job;
140 char *input;
141 QByteArray cmdBuffer;
142 const unsigned int inputSize;
143 char *thisLine, *nextLine, *inputEnd;
144 int fdPipeIn,fdPipeOut; //IPC-Pipes to/from async thread
145 int tcpSocket,timeout,idleHold;
146 QTextCodec *codec;
150 //********* DictInterface *************************************************
152 class DictInterface : public QObject
154 Q_OBJECT
156 public:
158 DictInterface();
159 ~DictInterface();
161 public slots:
163 void serverChanged(); // inform the client when server settings get changed
164 void stop(); // cancel all pending jobs
166 void define(const QString &query);
167 void getDefinitions(QStringList query);
168 void match(const QString &query);
169 void showDbInfo(const QString &db); // fetch detailed db info
170 void showDatabases(); // fetch misc. info...
171 void showStrategies();
172 void showInfo();
173 void updateServer(); // get info about databases & strategies the server knows
175 signals:
177 void infoReady(); // updateServer done
178 void resultReady(const QString &result, const QString &query); // define done
179 void matchReady(const QStringList &result); // match done
180 void started(const QString &message); // Client is active now, activate indicator
181 void stopped(const QString &message); // Client is now halted, deactivate indicator
183 private slots:
185 void clientDone();
187 private:
189 JobData* generateQuery(JobData::QueryType type, QString query);
190 void insertJob(JobData* job); // insert in job list, if nesscary cancel/remove previous jobs
191 void startClient(); // send start signal
192 void cleanPipes(); // empty the pipes, so that notifier stops firing
194 QSocketNotifier *notifier;
195 int fdPipeIn[2],fdPipeOut[2]; //IPC-Pipes to/from async thread
196 pthread_t threadID;
197 DictAsyncClient *client;
198 Q3PtrList<JobData> jobList;
199 bool newServer,clientDoneInProgress;
202 extern DictInterface *interface;
204 #endif