summaryrefslogtreecommitdiffstats
path: root/kmouth/phrasebook/phrasebook.h
blob: d156be207f025a728a40b52ccc813043bfb88699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
                          phrasebook.h  -  description
                             -------------------
    begin                : Don Sep 19 2002
    copyright            : (C) 2002 by Gunnar Schmi Dt
    email                : kmouth@schmi-dt.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PHRASEBOOK_H
#define PHRASEBOOK_H

#include <tqobject.h>
#include <tqdragobject.h>
#include <tqxml.h>

#include <kaction.h>
#include <kprinter.h>

class KURL;

/**
 * The class Phrase represents one phrase in a phrase book.
 * @author Gunnar Schmi Dt
 */
class Phrase {
   friend class PhraseBookParser;
public:
   Phrase();
   Phrase (const TQString &phrase);
   Phrase (const TQString &phrase, const TQString &shortcut);

   TQString getPhrase() const;
   TQString getShortcut() const;

   void setPhrase (const TQString &phrase);
   void setShortcut (const TQString &shortcut);

private:
   TQString phrase;
   TQString shortcut;
};

/**
 * The class PhraseBookEntry implements a phrase book entry. That can be either
 * a phrase or a start tag a sub phrase book.
 * @author Gunnar Schmi Dt
 */
class PhraseBookEntry {
public:
   PhraseBookEntry ();
   PhraseBookEntry (Phrase phrase, int level = 1, bool isPhrase = true);
   ~PhraseBookEntry () {};

   void setPhrase (Phrase phrase, int level = 1, bool isPhrase = true);

   bool isPhrase() const;
   Phrase getPhrase() const;
   int getLevel() const;

private:
   bool isPhraseValue;
   Phrase phrase;
   int level;
};

typedef TQValueList<PhraseBookEntry> PhraseBookEntryList;

/**
 * The class PhraseBook implements a phrase book. It mainly stores a
 * token list where each token is a phrase book entry (either a phrase
 * or a sub phrase book). The entries are placed into a tree structure
 * as follows:
 *
 * The level of each entry tells the level in the tree (level=0 is the top
 * level), each sub book in level i directly or indirectly contains all
 * following entries until an entry of level at most i or the end of the
 * token list.
 *
 * @author Gunnar Schmi Dt
 */
class PhraseBook : public PhraseBookEntryList {
public:
   PhraseBook() : PhraseBookEntryList() {};
   ~PhraseBook() {};

   /** opens a file containing a phrase book. Returns true if successful. */
   bool open (const KURL &url);

   /** decodes a phrase book. Returns true if successful. */
   bool decode (const TQString &xml);

   /** decodes a phrase book. Returns true if successful. */
   bool decode (TQXmlInputSource &source);

   /** Writes the phrases to a file. Returns true if successful. */
   bool save (const KURL &url);

   /** Writes the phrases to a file. Returns true if successful. */
   bool save (const KURL &url, bool asPhrasebook);

   /** Writes the phrases to a TQTextStream. */
   void save (TQTextStream &stream, bool asPhrasebook);

   /** Prints the phrases. */
   void print (KPrinter *pPrinter);

   /** Shows a file selector and writes the phrases to a file.
    *  @return 1, if the file got successfully written,
    *          0, if the user canceled the operation,
    *         -1, if there was an error when saving the file.
    */
   int save (TQWidget *parent, const TQString &title, KURL &url, bool phrasebookFirst = true);

   /** encodes the phrase book. Returns the encoded xml code. */
   TQString encode ();

   /** Stores all entries in a TQStringList. All hierarchy information and all
    * shortcuts are ignored during this operation.
    */
   TQStringList toStringList();

   /** Adds the entries of the book to both the given popup menu and the given
    * toolbar. The corresponding actions will be inserted into phrases.
    */
   void addToGUI (TQPopupMenu *popup, TDEToolBar *toolbar,
                  TDEActionCollection *phrases,
                  TQObject *receiver, const char *slot) const;

   /** Inserts book into a new sub phrase book.
    * @param name the name of the new sub phrase book.
    * @param book the phrase book to insert.
    */
   void insert (const TQString &name, const PhraseBook &book);
};

/**
 * The class PhraseBookDrag implements drag and drop support for phrase books.
 * @author Gunnar Schmi Dt
 */
class PhraseBookDrag: public TQDragObject {
   Q_OBJECT
  
public:
   PhraseBookDrag (PhraseBook *book, TQWidget *dragSource = 0, const char *name = 0);
   PhraseBookDrag (TQWidget *dragSource = 0, const char *name = 0);
   ~PhraseBookDrag ();

   virtual void setBook (PhraseBook *book);

   const char *format (int i) const;
   virtual TQByteArray encodedData (const char *) const;

   static bool canDecode (const TQMimeSource *e);
   static bool decode (const TQMimeSource *e, PhraseBook *book);

private:
   bool isEmpty;
   TQTextDrag xmlphrasebook;
   TQTextDrag xml;
   TQTextDrag plain;
};

class PhraseAction : public TDEAction {
   Q_OBJECT
  
public:
   PhraseAction (const TQString& phrase, const TQString& cut, const TQObject* receiver, const char* slot, TDEActionCollection* parent)
   : TDEAction (phrase, "phrase", TDEShortcut(cut), 0, 0, parent, phrase.latin1()) {
      this->phrase = phrase;
      connect (this, TQT_SIGNAL(slotActivated (const TQString &)), receiver, slot);
   };
   ~PhraseAction () {
   }

public slots:
   void slotActivated () {
      TDEAction::slotActivated();
      emit slotActivated (phrase);
   }

signals:
   void slotActivated (const TQString &phrase);

private:
   TQString phrase;
};

#endif