filters

ImportField.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qstring.h>
00021 #include <qdom.h>
00022 
00023 #include <klocale.h>
00024 
00025 #include "ImportField.h"
00026 
00027 QString getFootnoteFramesetName(const QString& id)
00028 {
00029     return i18n("Frameset name","Footnote %1").arg(id);
00030 }
00031 
00032 static void InsertTimeVariable(QDomDocument& mainDocument,
00033     QDomElement& variableElement, QString strKey)
00034 {
00035     QDomElement typeElement=mainDocument.createElement("TYPE");
00036     typeElement.setAttribute("key",strKey);
00037     typeElement.setAttribute("type",2); // Time
00038     typeElement.setAttribute("text","-"); // Dummy, we let KWord do the work!
00039     variableElement.appendChild(typeElement); //Append to <VARIABLE>
00040     QDomElement timeElement=mainDocument.createElement("TIME");
00041     // We cannot calculate the time, so default to midnight
00042     timeElement.setAttribute("hour",0);
00043     timeElement.setAttribute("minute",0);
00044     timeElement.setAttribute("second",0);
00045     timeElement.setAttribute("fix",0); // AbiWord's <field> is never fixed
00046     variableElement.appendChild(timeElement); //Append to <VARIABLE>
00047 }
00048 
00049 static void InsertDateVariable(QDomDocument& mainDocument,
00050     QDomElement& variableElement, QString strKey)
00051 {
00052     QDomElement typeElement=mainDocument.createElement("TYPE");
00053     typeElement.setAttribute("key",strKey);
00054     typeElement.setAttribute("type",0); // date
00055     typeElement.setAttribute("text","-"); // Just a dummy, KWord will do the work
00056     variableElement.appendChild(typeElement); //Append to <VARIABLE>
00057     QDomElement dateElement=mainDocument.createElement("DATE");
00058     // As we have no idea about the current date, use the *nix epoch 1970-01-01
00059     dateElement.setAttribute("year",1970);
00060     dateElement.setAttribute("month",1);
00061     dateElement.setAttribute("day",1);
00062     dateElement.setAttribute("fix",0);  // AbiWord's <field> is never fixed
00063     variableElement.appendChild(dateElement); //Append to <VARIABLE>
00064 }
00065 
00066 static bool ProcessTimeField(QDomDocument& mainDocument,
00067     QDomElement& variableElement, QString strType)
00068 // strType: AbiWord's type
00069 {
00070     if (strType=="time")
00071     {
00072         InsertTimeVariable(mainDocument, variableElement, "TIMELocale");
00073     }
00074     else if (strType=="time_miltime")
00075     {
00076         // AbiWord's military time is just the standard 24h time (with seconds)
00077         InsertTimeVariable(mainDocument, variableElement, "TIMEhh:mm:ss");
00078     }
00079     else if (strType=="time_ampm")
00080     {
00081         InsertTimeVariable(mainDocument, variableElement, "TIMEam");
00082     }
00083     else
00084     {
00085         // time_zone: not supported due to KWord
00086         // time_epoch: not supported due to KWord (%Z)
00087         return false;
00088     }
00089     return true;
00090 }
00091 
00092 static bool ProcessDateField(QDomDocument& mainDocument,
00093     QDomElement& variableElement, QString strType)
00094 // strType: AbiWord's type
00095 // Help for the % formats:
00096 //   man date
00097 //  or
00098 //   info date
00099 {
00100     if (strType=="date")
00101     {
00102         InsertDateVariable(mainDocument, variableElement, "DATE0dddd mmmm dd, yyyy");
00103     }
00104     else if (strType=="date_mmddyy")
00105     {
00106         InsertDateVariable(mainDocument, variableElement, "DATE0mm/dd/yy");
00107     }
00108     else if (strType=="date_ddmmyy")
00109     {
00110         InsertDateVariable(mainDocument, variableElement, "DATE0dd/mm/yy");
00111     }
00112     else if (strType=="date_mdy")
00113     {
00114         InsertDateVariable(mainDocument, variableElement, "DATE0mmmm dd, yyyy");
00115     }
00116     else if (strType=="date_mthdy")
00117     {
00118         InsertDateVariable(mainDocument, variableElement, "DATE0mmm dd, yyyy");
00119     }
00120     else if (strType=="date_dfl")
00121     {   // Should be %c, but we cannot do time zones, so for now: Locale, no time! (TODO)
00122         InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
00123     }
00124     else if (strType=="date_ntdfl")
00125     {
00126         InsertDateVariable(mainDocument, variableElement, "DATE0Locale");
00127     }
00128     else if (strType=="date_wkday")
00129     {
00130         InsertDateVariable(mainDocument, variableElement, "DATE0dddd");
00131     }
00132     else
00133     {
00134         // date_doy: not supported (%j)
00135         return false;
00136     }
00137     return true;
00138 }
00139 
00140 bool ProcessField(QDomDocument& mainDocument,
00141     QDomElement& variableElement, QString strType, const QXmlAttributes& attributes)
00142 {
00143     // In AbiWord:
00144     //   field names are in the file: src/text/fmt/xp/fp_Fields.h
00145     //   field contents are in the file: src/text/fmt/xp/fp_Run.cpp
00146 
00147     bool done=false;
00148     if (strType.startsWith("time"))
00149     {
00150         done=ProcessTimeField(mainDocument, variableElement, strType);
00151     }
00152     else if (strType.startsWith("date"))
00153     {
00154         done=ProcessDateField(mainDocument, variableElement, strType);
00155     }
00156     else if ((strType=="page_number")||(strType=="page_count"))
00157     {
00158         QDomElement typeElement=mainDocument.createElement("TYPE");
00159         typeElement.setAttribute("key","NUMBER");
00160         typeElement.setAttribute("type",4); // page number/count
00161         typeElement.setAttribute("text",1); // We cannot count the pages, so give a default value
00162         variableElement.appendChild(typeElement); //Append to <VARIABLE>
00163         QDomElement pgnumElement=mainDocument.createElement("PGNUM");
00164         pgnumElement.setAttribute("subtype",(strType=="page_count")?1:0);
00165         pgnumElement.setAttribute("value",1);
00166         variableElement.appendChild(pgnumElement); //Append to <VARIABLE>
00167         done=true;
00168     }
00169     else if (strType=="file_name")
00170     {
00171         QDomElement typeElement=mainDocument.createElement("TYPE");
00172         typeElement.setAttribute("key","STRING");
00173         typeElement.setAttribute("type",8);
00174         typeElement.setAttribute("text","?"); // TODO: do we need this information right now?
00175         variableElement.appendChild(typeElement); //Append to <VARIABLE>
00176         QDomElement fieldElement=mainDocument.createElement("FIELD");
00177         fieldElement.setAttribute("subtype",0);
00178         fieldElement.setAttribute("value","?"); // Should be the same as the text attribute
00179         variableElement.appendChild(fieldElement); //Append to <VARIABLE>
00180         done=true;
00181     }
00182     else if (strType=="endnote_ref")
00183     {
00184         QDomElement typeElement=mainDocument.createElement("TYPE");
00185         typeElement.setAttribute("key","STRING");
00186         typeElement.setAttribute("type",11);
00187         typeElement.setAttribute("text","?"); // ### TODO: do we need this information right now?
00188         variableElement.appendChild(typeElement); //Append to <VARIABLE>
00189         QDomElement element=mainDocument.createElement("FOOTNOTE");
00190         element.setAttribute("numberingtype","auto"); // ### TODO: support other types
00191         element.setAttribute("notetype","footnote");
00192         QString reference(attributes.value("endnote-id").stripWhiteSpace());
00193         element.setAttribute("frameset", getFootnoteFramesetName(reference)); // ### TODO: better name
00194         element.setAttribute("value","?"); // Should be the same as the text attribute
00195         variableElement.appendChild(element); //Append to <VARIABLE>
00196         done=true;
00197     }
00198     
00199     // Not supported:
00200     //  app_ver
00201     //  app_id
00202     //  app_options
00203     //  app_target
00204     //  app_compiledate
00205     //  app_compiletime
00206     //  list_label
00207     //  word_count
00208     //  char_count
00209     //  line_count
00210     //  para_count
00211     //  nbsp_count
00212     //  page_ref
00213     // ...
00214 
00215     return done;
00216 }
KDE Home | KDE Accessibility Home | Description of Access Keys