summaryrefslogtreecommitdiffstats
path: root/filters/kword/abiword/ImportField.cc
blob: e9474364ff1cdaa90d138ed79e6ed54735443334 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* This file is part of the KDE project
   Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqstring.h>
#include <tqdom.h>

#include <tdelocale.h>

#include "ImportField.h"

TQString getFootnoteFramesetName(const TQString& id)
{
    return i18n("Frameset name","Footnote %1").arg(id);
}

static void InsertTimeVariable(TQDomDocument& mainDocument,
    TQDomElement& variableElement, TQString strKey)
{
    TQDomElement typeElement=mainDocument.createElement("TYPE");
    typeElement.setAttribute("key",strKey);
    typeElement.setAttribute("type",2); // Time
    typeElement.setAttribute("text","-"); // Dummy, we let KWord do the work!
    variableElement.appendChild(typeElement); //Append to <VARIABLE>
    TQDomElement timeElement=mainDocument.createElement("TIME");
    // We cannot calculate the time, so default to midnight
    timeElement.setAttribute("hour",0);
    timeElement.setAttribute("minute",0);
    timeElement.setAttribute("second",0);
    timeElement.setAttribute("fix",0); // AbiWord's <field> is never fixed
    variableElement.appendChild(timeElement); //Append to <VARIABLE>
}

static void InsertDateVariable(TQDomDocument& mainDocument,
    TQDomElement& variableElement, TQString strKey)
{
    TQDomElement typeElement=mainDocument.createElement("TYPE");
    typeElement.setAttribute("key",strKey);
    typeElement.setAttribute("type",0); // date
    typeElement.setAttribute("text","-"); // Just a dummy, KWord will do the work
    variableElement.appendChild(typeElement); //Append to <VARIABLE>
    TQDomElement dateElement=mainDocument.createElement("DATE");
    // As we have no idea about the current date, use the *nix epoch 1970-01-01
    dateElement.setAttribute("year",1970);
    dateElement.setAttribute("month",1);
    dateElement.setAttribute("day",1);
    dateElement.setAttribute("fix",0);  // AbiWord's <field> is never fixed
    variableElement.appendChild(dateElement); //Append to <VARIABLE>
}

static bool ProcessTimeField(TQDomDocument& mainDocument,
    TQDomElement& variableElement, TQString strType)
// strType: AbiWord's type
{
    if (strType=="time")
    {
        InsertTimeVariable(mainDocument, variableElement, "TIMELocale");
    }
    else if (strType=="time_miltime")
    {
        // AbiWord's military time is just the standard 24h time (with seconds)
        InsertTimeVariable(mainDocument, variableElement, "TIMEhh:mm:ss");
    }
    else if (strType=="time_ampm")
    {
        InsertTimeVariable(mainDocument, variableElement, "TIMEam");
    }
    else
    {
        // time_zone: not supported due to KWord
        // time_epoch: not supported due to KWord (%Z)
        return false;
    }
    return true;
}

static bool ProcessDateField(TQDomDocument& mainDocument,
    TQDomElement& variableElement, TQString strType)
// strType: AbiWord's type
// Help for the % formats:
//   man date
//  or
//   info date
{
    if (strType=="date")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0dddd mmmm dd, yyyy");
    }
    else if (strType=="date_mmddyy")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0mm/dd/yy");
    }
    else if (strType=="date_ddmmyy")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0dd/mm/yy");
    }
    else if (strType=="date_mdy")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0mmmm dd, yyyy");
    }
    else if (strType=="date_mthdy")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0mmm dd, yyyy");
    }
    else if (strType=="date_dfl")
    {   // Should be %c, but we cannot do time zones, so for now: Locale, no time! (TODO)
        InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
    }
    else if (strType=="date_ntdfl")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
    }
    else if (strType=="date_wkday")
    {
        InsertDateVariable(mainDocument, variableElement, "DATE0dddd");
    }
    else
    {
        // date_doy: not supported (%j)
        return false;
    }
    return true;
}

bool ProcessField(TQDomDocument& mainDocument,
    TQDomElement& variableElement, TQString strType, const TQXmlAttributes& attributes)
{
    // In AbiWord:
    //   field names are in the file: src/text/fmt/xp/fp_Fields.h
    //   field contents are in the file: src/text/fmt/xp/fp_Run.cpp

    bool done=false;
    if (strType.startsWith("time"))
    {
        done=ProcessTimeField(mainDocument, variableElement, strType);
    }
    else if (strType.startsWith("date"))
    {
        done=ProcessDateField(mainDocument, variableElement, strType);
    }
    else if ((strType=="page_number")||(strType=="page_count"))
    {
        TQDomElement typeElement=mainDocument.createElement("TYPE");
        typeElement.setAttribute("key","NUMBER");
        typeElement.setAttribute("type",4); // page number/count
        typeElement.setAttribute("text",1); // We cannot count the pages, so give a default value
        variableElement.appendChild(typeElement); //Append to <VARIABLE>
        TQDomElement pgnumElement=mainDocument.createElement("PGNUM");
        pgnumElement.setAttribute("subtype",(strType=="page_count")?1:0);
        pgnumElement.setAttribute("value",1);
        variableElement.appendChild(pgnumElement); //Append to <VARIABLE>
        done=true;
    }
    else if (strType=="file_name")
    {
        TQDomElement typeElement=mainDocument.createElement("TYPE");
        typeElement.setAttribute("key","STRING");
        typeElement.setAttribute("type",8);
        typeElement.setAttribute("text","?"); // TODO: do we need this information right now?
        variableElement.appendChild(typeElement); //Append to <VARIABLE>
        TQDomElement fieldElement=mainDocument.createElement("FIELD");
        fieldElement.setAttribute("subtype",0);
        fieldElement.setAttribute("value","?"); // Should be the same as the text attribute
        variableElement.appendChild(fieldElement); //Append to <VARIABLE>
        done=true;
    }
    else if (strType=="endnote_ref")
    {
        TQDomElement typeElement=mainDocument.createElement("TYPE");
        typeElement.setAttribute("key","STRING");
        typeElement.setAttribute("type",11);
        typeElement.setAttribute("text","?"); // ### TODO: do we need this information right now?
        variableElement.appendChild(typeElement); //Append to <VARIABLE>
        TQDomElement element=mainDocument.createElement("FOOTNOTE");
        element.setAttribute("numberingtype","auto"); // ### TODO: support other types
        element.setAttribute("notetype","footnote");
        TQString reference(attributes.value("endnote-id").stripWhiteSpace());
        element.setAttribute("frameset", getFootnoteFramesetName(reference)); // ### TODO: better name
        element.setAttribute("value","?"); // Should be the same as the text attribute
        variableElement.appendChild(element); //Append to <VARIABLE>
        done=true;
    }
    
    // Not supported:
    //  app_ver
    //  app_id
    //  app_options
    //  app_target
    //  app_compiledate
    //  app_compiletime
    //  list_label
    //  word_count
    //  char_count
    //  line_count
    //  para_count
    //  nbsp_count
    //  page_ref
    // ...

    return done;
}