ImportField.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqstring.h>
21#include <tqdom.h>
22
23#include <tdelocale.h>
24
25#include "ImportField.h"
26
27TQString getFootnoteFramesetName(const TQString& id)
28{
29 return i18n("Frameset name","Footnote %1").arg(id);
30}
31
32static void InsertTimeVariable(TQDomDocument& mainDocument,
33 TQDomElement& variableElement, TQString strKey)
34{
35 TQDomElement typeElement=mainDocument.createElement("TYPE");
36 typeElement.setAttribute("key",strKey);
37 typeElement.setAttribute("type",2); // Time
38 typeElement.setAttribute("text","-"); // Dummy, we let KWord do the work!
39 variableElement.appendChild(typeElement); //Append to <VARIABLE>
40 TQDomElement timeElement=mainDocument.createElement("TIME");
41 // We cannot calculate the time, so default to midnight
42 timeElement.setAttribute("hour",0);
43 timeElement.setAttribute("minute",0);
44 timeElement.setAttribute("second",0);
45 timeElement.setAttribute("fix",0); // AbiWord's <field> is never fixed
46 variableElement.appendChild(timeElement); //Append to <VARIABLE>
47}
48
49static void InsertDateVariable(TQDomDocument& mainDocument,
50 TQDomElement& variableElement, TQString strKey)
51{
52 TQDomElement typeElement=mainDocument.createElement("TYPE");
53 typeElement.setAttribute("key",strKey);
54 typeElement.setAttribute("type",0); // date
55 typeElement.setAttribute("text","-"); // Just a dummy, KWord will do the work
56 variableElement.appendChild(typeElement); //Append to <VARIABLE>
57 TQDomElement dateElement=mainDocument.createElement("DATE");
58 // As we have no idea about the current date, use the *nix epoch 1970-01-01
59 dateElement.setAttribute("year",1970);
60 dateElement.setAttribute("month",1);
61 dateElement.setAttribute("day",1);
62 dateElement.setAttribute("fix",0); // AbiWord's <field> is never fixed
63 variableElement.appendChild(dateElement); //Append to <VARIABLE>
64}
65
66static bool ProcessTimeField(TQDomDocument& mainDocument,
67 TQDomElement& variableElement, TQString strType)
68// strType: AbiWord's type
69{
70 if (strType=="time")
71 {
72 InsertTimeVariable(mainDocument, variableElement, "TIMELocale");
73 }
74 else if (strType=="time_miltime")
75 {
76 // AbiWord's military time is just the standard 24h time (with seconds)
77 InsertTimeVariable(mainDocument, variableElement, "TIMEhh:mm:ss");
78 }
79 else if (strType=="time_ampm")
80 {
81 InsertTimeVariable(mainDocument, variableElement, "TIMEam");
82 }
83 else
84 {
85 // time_zone: not supported due to KWord
86 // time_epoch: not supported due to KWord (%Z)
87 return false;
88 }
89 return true;
90}
91
92static bool ProcessDateField(TQDomDocument& mainDocument,
93 TQDomElement& variableElement, TQString strType)
94// strType: AbiWord's type
95// Help for the % formats:
96// man date
97// or
98// info date
99{
100 if (strType=="date")
101 {
102 InsertDateVariable(mainDocument, variableElement, "DATE0dddd mmmm dd, yyyy");
103 }
104 else if (strType=="date_mmddyy")
105 {
106 InsertDateVariable(mainDocument, variableElement, "DATE0mm/dd/yy");
107 }
108 else if (strType=="date_ddmmyy")
109 {
110 InsertDateVariable(mainDocument, variableElement, "DATE0dd/mm/yy");
111 }
112 else if (strType=="date_mdy")
113 {
114 InsertDateVariable(mainDocument, variableElement, "DATE0mmmm dd, yyyy");
115 }
116 else if (strType=="date_mthdy")
117 {
118 InsertDateVariable(mainDocument, variableElement, "DATE0mmm dd, yyyy");
119 }
120 else if (strType=="date_dfl")
121 { // Should be %c, but we cannot do time zones, so for now: Locale, no time! (TODO)
122 InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
123 }
124 else if (strType=="date_ntdfl")
125 {
126 InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
127 }
128 else if (strType=="date_wkday")
129 {
130 InsertDateVariable(mainDocument, variableElement, "DATE0dddd");
131 }
132 else
133 {
134 // date_doy: not supported (%j)
135 return false;
136 }
137 return true;
138}
139
140bool ProcessField(TQDomDocument& mainDocument,
141 TQDomElement& variableElement, TQString strType, const TQXmlAttributes& attributes)
142{
143 // In AbiWord:
144 // field names are in the file: src/text/fmt/xp/fp_Fields.h
145 // field contents are in the file: src/text/fmt/xp/fp_Run.cpp
146
147 bool done=false;
148 if (strType.startsWith("time"))
149 {
150 done=ProcessTimeField(mainDocument, variableElement, strType);
151 }
152 else if (strType.startsWith("date"))
153 {
154 done=ProcessDateField(mainDocument, variableElement, strType);
155 }
156 else if ((strType=="page_number")||(strType=="page_count"))
157 {
158 TQDomElement typeElement=mainDocument.createElement("TYPE");
159 typeElement.setAttribute("key","NUMBER");
160 typeElement.setAttribute("type",4); // page number/count
161 typeElement.setAttribute("text",1); // We cannot count the pages, so give a default value
162 variableElement.appendChild(typeElement); //Append to <VARIABLE>
163 TQDomElement pgnumElement=mainDocument.createElement("PGNUM");
164 pgnumElement.setAttribute("subtype",(strType=="page_count")?1:0);
165 pgnumElement.setAttribute("value",1);
166 variableElement.appendChild(pgnumElement); //Append to <VARIABLE>
167 done=true;
168 }
169 else if (strType=="file_name")
170 {
171 TQDomElement typeElement=mainDocument.createElement("TYPE");
172 typeElement.setAttribute("key","STRING");
173 typeElement.setAttribute("type",8);
174 typeElement.setAttribute("text","?"); // TODO: do we need this information right now?
175 variableElement.appendChild(typeElement); //Append to <VARIABLE>
176 TQDomElement fieldElement=mainDocument.createElement("FIELD");
177 fieldElement.setAttribute("subtype",0);
178 fieldElement.setAttribute("value","?"); // Should be the same as the text attribute
179 variableElement.appendChild(fieldElement); //Append to <VARIABLE>
180 done=true;
181 }
182 else if (strType=="endnote_ref")
183 {
184 TQDomElement typeElement=mainDocument.createElement("TYPE");
185 typeElement.setAttribute("key","STRING");
186 typeElement.setAttribute("type",11);
187 typeElement.setAttribute("text","?"); // ### TODO: do we need this information right now?
188 variableElement.appendChild(typeElement); //Append to <VARIABLE>
189 TQDomElement element=mainDocument.createElement("FOOTNOTE");
190 element.setAttribute("numberingtype","auto"); // ### TODO: support other types
191 element.setAttribute("notetype","footnote");
192 TQString reference(attributes.value("endnote-id").stripWhiteSpace());
193 element.setAttribute("frameset", getFootnoteFramesetName(reference)); // ### TODO: better name
194 element.setAttribute("value","?"); // Should be the same as the text attribute
195 variableElement.appendChild(element); //Append to <VARIABLE>
196 done=true;
197 }
198
199 // Not supported:
200 // app_ver
201 // app_id
202 // app_options
203 // app_target
204 // app_compiledate
205 // app_compiletime
206 // list_label
207 // word_count
208 // char_count
209 // line_count
210 // para_count
211 // nbsp_count
212 // page_ref
213 // ...
214
215 return done;
216}