31#include <tqtextcodec.h>
33#include <tqtextstream.h>
35#include <tqfontinfo.h>
39#include <kgenericfactory.h>
42#include <KoPageLayout.h>
44#include <KoFilterChain.h>
45#include <KoFilterManager.h>
47#include <asciiimport.h>
48#include <asciiimport.moc>
49#include "ImportDialog.h"
51class ASCIIImportFactory : KGenericFactory<ASCIIImport, KoFilter>
54 ASCIIImportFactory(
void) : KGenericFactory<ASCIIImport, KoFilter> (
"kwordasciiimport")
57 virtual void setupTranslations(
void )
59 TDEGlobal::locale()->insertCatalogue(
"kofficefilters" );
63K_EXPORT_COMPONENT_FACTORY( libasciiimport, ASCIIImportFactory() )
65ASCIIImport::ASCIIImport(KoFilter *, const
char *, const TQStringList &) :
69void ASCIIImport::prepareDocument(TQDomDocument& mainDocument, TQDomElement& mainFramesetElement)
72 KoFormat paperFormat=PG_DIN_A4;
73 KoOrientation paperOrientation=PG_PORTRAIT;
75 mainDocument.appendChild(
76 mainDocument.createProcessingInstruction(
77 "xml",
"version=\"1.0\" encoding=\"UTF-8\""));
79 TQDomElement elementDoc;
80 elementDoc=mainDocument.createElement(
"DOC");
81 elementDoc.setAttribute(
"editor",
"KWord's Plain Text Import Filter");
82 elementDoc.setAttribute(
"mime",
"application/x-kword");
84 elementDoc.setAttribute(
"syntaxVersion",2);
85 mainDocument.appendChild(elementDoc);
88 element=mainDocument.createElement(
"ATTRIBUTES");
89 element.setAttribute(
"processing",0);
90 element.setAttribute(
"standardpage",1);
91 element.setAttribute(
"hasHeader",0);
92 element.setAttribute(
"hasFooter",0);
94 elementDoc.appendChild(element);
96 TQDomElement elementPaper=mainDocument.createElement(
"PAPER");
97 elementPaper.setAttribute(
"format",paperFormat);
98 elementPaper.setAttribute(
"width" ,KoPageFormat::width (paperFormat,paperOrientation) * 72.0 / 25.4);
99 elementPaper.setAttribute(
"height",KoPageFormat::height(paperFormat,paperOrientation) * 72.0 / 25.4);
100 elementPaper.setAttribute(
"orientation",PG_PORTRAIT);
101 elementPaper.setAttribute(
"columns",1);
102 elementPaper.setAttribute(
"columnspacing",2);
103 elementPaper.setAttribute(
"hType",0);
104 elementPaper.setAttribute(
"fType",0);
105 elementPaper.setAttribute(
"spHeadBody",9);
106 elementPaper.setAttribute(
"spFootBody",9);
107 elementPaper.setAttribute(
"zoom",100);
108 elementDoc.appendChild(elementPaper);
110 element=mainDocument.createElement(
"PAPERBORDERS");
111 element.setAttribute(
"left",28);
112 element.setAttribute(
"top",42);
113 element.setAttribute(
"right",28);
114 element.setAttribute(
"bottom",42);
115 elementPaper.appendChild(element);
117 TQDomElement framesetsPluralElementOut=mainDocument.createElement(
"FRAMESETS");
118 mainDocument.documentElement().appendChild(framesetsPluralElementOut);
120 mainFramesetElement=mainDocument.createElement(
"FRAMESET");
121 mainFramesetElement.setAttribute(
"frameType",1);
122 mainFramesetElement.setAttribute(
"frameInfo",0);
123 mainFramesetElement.setAttribute(
"autoCreateNewFrame",1);
124 mainFramesetElement.setAttribute(
"removable",0);
126 framesetsPluralElementOut.appendChild(mainFramesetElement);
128 TQDomElement frameElementOut=mainDocument.createElement(
"FRAME");
129 frameElementOut.setAttribute(
"left",28);
130 frameElementOut.setAttribute(
"top",42);
131 frameElementOut.setAttribute(
"bottom",566);
132 frameElementOut.setAttribute(
"right",798);
133 frameElementOut.setAttribute(
"runaround",1);
134 mainFramesetElement.appendChild(frameElementOut);
136 TQDomElement elementStylesPlural=mainDocument.createElement(
"STYLES");
137 elementDoc.appendChild(elementStylesPlural);
139 TQDomElement elementStyleStandard=mainDocument.createElement(
"STYLE");
140 elementStylesPlural.appendChild(elementStyleStandard);
142 element=mainDocument.createElement(
"NAME");
143 element.setAttribute(
"value",
"Standard");
144 elementStyleStandard.appendChild(element);
146 element=mainDocument.createElement(
"FOLLOWING");
147 element.setAttribute(
"name",
"Standard");
148 elementStyleStandard.appendChild(element);
150 TQDomElement elementFormat=mainDocument.createElement(
"FORMAT");
151 elementStyleStandard.appendChild(elementFormat);
154 TQFontInfo defaultFontInfo(KoGlobal::defaultFont());
156 element=mainDocument.createElement(
"FONT");
157 element.setAttribute(
"name",defaultFontInfo.family());
158 elementFormat.appendChild(element);
160 element=mainDocument.createElement(
"SIZE");
161 element.setAttribute(
"value",defaultFontInfo.pointSize());
162 elementFormat.appendChild(element);
164 element=mainDocument.createElement(
"ITALIC");
165 element.setAttribute(
"value",0);
166 elementFormat.appendChild(element);
168 element=mainDocument.createElement(
"WEIGHT");
169 element.setAttribute(
"value",50);
170 elementFormat.appendChild(element);
172 element=mainDocument.createElement(
"UNDERLINE");
173 element.setAttribute(
"value",0);
174 elementFormat.appendChild(element);
176 element=mainDocument.createElement(
"STRIKEOUT");
177 element.setAttribute(
"value",0);
178 elementFormat.appendChild(element);
180 element=mainDocument.createElement(
"VERTALIGN");
181 element.setAttribute(
"value",0);
182 elementFormat.appendChild(element);
184 element=mainDocument.createElement(
"COLOR");
185 element.setAttribute(
"red", -1);
186 element.setAttribute(
"green",1);
187 element.setAttribute(
"blue", -1);
188 elementFormat.appendChild(element);
190 element=mainDocument.createElement(
"TEXTBACKGROUNDCOLOR");
191 element.setAttribute(
"red", -1);
192 element.setAttribute(
"green",-1);
193 element.setAttribute(
"blue", -1);
194 elementFormat.appendChild(element);
198KoFilter::ConversionStatus ASCIIImport::convert(
const TQCString& from,
const TQCString& to )
200 if (to!=
"application/x-kword" || from!=
"text/plain")
201 return KoFilter::NotImplemented;
203 AsciiImportDialog* dialog = 0;
204 if (!m_chain->manager()->getBatchMode())
206 dialog =
new AsciiImportDialog();
209 kdError(30502) <<
"Dialog has not been created! Aborting!" << endl;
210 return KoFilter::StupidError;
214 kdDebug(30502) <<
"Dialog was aborted! Aborting filter!" << endl;
215 return KoFilter::UserCancelled;
220 int paragraphStrategy;
223 codec = dialog->getCodec();
224 paragraphStrategy=dialog->getParagraphStrategy();
229 codec = TQTextCodec::codecForName(
"UTF-8");
233 TQFile in(m_chain->inputFile());
234 if(!in.open(IO_ReadOnly)) {
235 kdError(30502) <<
"Unable to open input file!" << endl;
237 return KoFilter::FileNotFound;
242 TQDomDocument mainDocument;
243 TQDomElement mainFramesetElement;
245 prepareDocument(mainDocument,mainFramesetElement);
247 TQTextStream stream(&in);
251 kdError(30502) <<
"Could not create TQTextCodec! Aborting" << endl;
253 return KoFilter::StupidError;
256 kdDebug(30502) <<
"Charset used: " << codec->name() << endl;
258 stream.setCodec(codec);
260 if (1==paragraphStrategy)
261 sentenceConvert(stream, mainDocument, mainFramesetElement);
262 else if (999==paragraphStrategy)
263 oldWayConvert(stream, mainDocument, mainFramesetElement);
265 asIsConvert(stream, mainDocument, mainFramesetElement);
269 kdDebug(30502) << mainDocument.toString() << endl;
272 KoStoreDevice* out=m_chain->storageFile(
"root", KoStore::Write );
274 kdError(30502) <<
"Unable to open output file!" << endl;
276 return KoFilter::StorageCreationError;
278 TQCString cstr=mainDocument.toCString();
280 out->writeBlock(cstr,cstr.length());
285void ASCIIImport::oldWayConvert(TQTextStream& stream, TQDomDocument& mainDocument,
286 TQDomElement& mainFramesetElement)
288 kdDebug(30502) <<
"Entering: ASCIIImport::oldWayConvert" << endl;
289 TQStringList paragraph;
294 bool lastCharWasCr=
false;
296 while(!stream.atEnd())
300 for(
int line_no = numLines = 0; line_no < MAXLINES; line_no++, numLines++)
305 strLine=readLine(stream,lastCharWasCr);
306 if (strLine.isEmpty())
308 paragraph.append(TQString());
312 const int length = strLine.length();
313 if (strLine.at(length-1) ==
'-')
315 strLine.at(length-1)=TQChar(173);
319 paragraph.append(strLine);
324 if ( Table( &Line[0], &linecount, numLines, table_no, tbl, str))
333 if(ListItem( &Line[0], numLines, str))
334 linecount += (numLines + 1);
341 processParagraph(mainDocument,mainFramesetElement,paragraph);
342 linecount += ( numLines + 1);
349 if( table_no > 0) str += tbl;
353void ASCIIImport::asIsConvert(TQTextStream& stream, TQDomDocument& mainDocument,
354 TQDomElement& mainFramesetElement)
357 kdDebug(30502) <<
"Entering: ASCIIImport::asIsConvert" << endl;
358 bool lastCharWasCr=
false;
360 while(!stream.atEnd())
363 strLine=readLine(stream,lastCharWasCr);
364 writeOutParagraph(mainDocument,mainFramesetElement,
"Standard", strLine,
369void ASCIIImport::sentenceConvert(TQTextStream& stream, TQDomDocument& mainDocument,
370 TQDomElement& mainFramesetElement)
377 kdDebug(30502) <<
"Entering: ASCIIImport::sentenceConvert" << endl;
378 TQStringList paragraph;
379 bool lastCharWasCr=
false;
381 TQString stoppingPunctuation(
".!?");
382 TQString skippingQuotes(
"\"')");
383 while (!stream.atEnd())
391 strLine=readLine(stream,lastCharWasCr);
392 if (strLine.isEmpty())
397 paragraph.append(strLine);
399 uint lastPos=strLine.length()-1;
402 for (
int i=0;i<10;i++)
404 lastChar=strLine[lastPos];
405 if (lastChar.isNull())
407 else if (skippingQuotes.find(lastChar)==-1)
413 lastChar=strLine[lastPos];
414 if (lastChar.isNull())
416 else if (stoppingPunctuation.find(lastChar)!=-1)
420 writeOutParagraph(mainDocument,mainFramesetElement,
"Standard",
421 paragraph.join(
" ").simplifyWhiteSpace(), 0, 0);
429void ASCIIImport::processParagraph(TQDomDocument& mainDocument,
430 TQDomElement& mainFramesetElement,
const TQStringList& paragraph)
434 TQStringList::ConstIterator it=paragraph.begin();
435 TQStringList::ConstIterator previousLine=it;
436 int firstindent=Indent(*it);
439 for( it++; it!=paragraph.end(); it++)
441 text += *previousLine;
443 if( (*previousLine).length() <= shortline)
445 if((*it).length() > shortline)
448 const int secondindent = Indent(*previousLine);
449 writeOutParagraph(mainDocument,mainFramesetElement,
450 "Standard", text.simplifyWhiteSpace(), firstindent, secondindent);
452 firstindent = Indent(*it);
459 const int secondindent = Indent(*previousLine);
460 writeOutParagraph(mainDocument,mainFramesetElement,
461 "Standard", text.simplifyWhiteSpace(), firstindent, secondindent);
464void ASCIIImport::writeOutParagraph(TQDomDocument& mainDocument,
465 TQDomElement& mainFramesetElement,
const TQString& name,
466 const TQString& text,
const int firstindent,
const int secondindent)
468 TQDomElement paragraphElementOut=mainDocument.createElement(
"PARAGRAPH");
469 mainFramesetElement.appendChild(paragraphElementOut);
470 TQDomElement textElement=mainDocument.createElement(
"TEXT");
471 paragraphElementOut.appendChild(textElement);
474 TQDomElement layoutElement=mainDocument.createElement(
"LAYOUT");
475 paragraphElementOut.appendChild(layoutElement);
477 TQDomElement element;
478 element=mainDocument.createElement(
"NAME");
479 element.setAttribute(
"value",name);
480 layoutElement.appendChild(element);
483 element=mainDocument.createElement(
"INDENTS");
484 size = firstindent-secondindent;
486 element.setAttribute(
"first",TQString::number(size));
489 element.setAttribute(
"left",TQString::number(size));
490 element.setAttribute(
"right",0);
492 textElement.appendChild(mainDocument.createTextNode(text));
493 textElement.normalize();
498 if (firstindent > 0 || secondindent > 0) \
499 WriteOutIndents( firstindent, secondindent, str);
504 str +=
"<COUNTER type=\"";
506 str +=
"\" depth=\"0\" start=\"1\" numberingtype=\"0\" lefttext=\"\" righttext=\".\" />\n";
512 str +=
"</FORMAT>\n";
513 str +=
"</LAYOUT>\n";
514 str +=
"</PARAGRAPH>\n";
521int ASCIIImport::Indent(
const TQString& line)
const
527 for( uint i = 0; i < line.length(); i++ )
530 if( c == TQChar(
' '))
532 else if( c == TQChar(
'\t'))
533 count += spacespertab;
556 void ASCIIImport::WriteOutTableCell(
int table_no,
int row,
557 int col, Position *pos, TQString &str)
560 str +=
"<FRAMESET frameType=\"1\" frameInfo=\"0\" grpMgr=\"grpmgr_";
562 TQString buf = TQString::number(table_no);
566 buf = TQString::number(row);
570 buf = TQString::number(col);
573 str +=
"\" rows=\"1\" cols=\"1\" removable=\"0\" visible=\"1\" name=\"Cell ";
574 buf = TQString::number(row);
577 buf = TQString::number(col);
582 str +=
"<FRAME left=\"";
583 buf = TQString::number(pos->left);
587 buf = TQString::number(pos->top);
590 str +=
"\" right=\"";
591 buf = TQString::number(pos->right);
594 str +=
"\" bottom=\"";
595 buf = TQString::number(pos->bottom);
598 str +=
"\" runaround=\"0\" autoCreateNewFrame=\"0\" newFrameBehavior=\"1\" />\n";
607bool ASCIIImport::Table( TQString *Line,
int *linecount,
int no_lines,
608 int table_no, TQString &tbl, TQString &str )
625 enum LiType{paragraph, table} linetype[MAXLINES];
626 struct Tabs tabs[MAXLINES];
628 int width[MAXCOLUMNS];
629 TQString *currentline;
634 int index, index1, index2, index3;
648 for(i = 0; i < no_lines; i++)
654 if( (tabs[i].indent = Indent( *Line)) > 0)
658 Line->remove(TQRegExp(
"^[\\ \t]*"));
662 while((index2 = Line->find( TQRegExp(
"\t"),index)) > index
663 || (index3 = MultSpaces( *Line, index)) > index )
665 index1 = kMax(index2, index3);
667 index1 = Line->find( TQRegExp(
"[^\t]"), index1);
669 tabs[i].field[no_cols] = Line->mid(index, (index1 - index -1));
670 tabs[i].width[no_cols] = index1 - index + spacespertab - 1;
679 index1 = Line->length() - (index + 1);
680 tabs[i].field[no_cols] = Line->mid(index,index1);
681 tabs[i].width[no_cols] = index1;
684 if(tabs[i].indent > 0) *Line = text;
686 tabs[i].columns = no_cols;
691 if( tabcount == 0)
return false;
696 for(j = 0; j < MAXCOLUMNS; j++) width[j] = 0;
697 for( i = 0; i <= no_lines; i++)
699 if(tabs[i].columns == 0 )
702 linetype[i] = paragraph;
710 for(j = 0; j <= tabs[i].columns; j++)
711 width[j] = kMax(tabs[i].width[j], width[j] );
716 if( linetype[i] != linetype[(i - 1)] || i == no_lines)
718 if( linetype[(begin)] == paragraph )
721 currentline = firstline;
722 currentline += begin;
723 beginline = currentline;
724 firstindent = Indent(*currentline);
727 secondindent = Indent( *currentline );
728 else secondindent = 0;
730 for( j= begin; j < i; j++)
736 TQString text1 = text.simplifyWhiteSpace();
737 WriteOutParagraph(
"Standard",
"", text1 , firstindent, secondindent, str);
738 *linecount += (i - begin);
745 WriteOutParagraph(
"Standard",
"",
"" , 0, 0, str);
746 for( j = begin; j < i; j++ )
749 pos.top = (double)((*linecount) + 1) * ptsperline;
750 pos.bottom = pos.top + ptsperline;
752 pos.left = leftmargin + (double)tabs[i].indent * ptsperchar;
755 for( k = 0; k <= tabs[j].columns; k++)
758 text = tabs[j].field[k];
760 text1 = text.simplifyWhiteSpace();
763 pos.right = pos.left + (double)width[k] * ptsperchar;
765 WriteOutTableCell( table_no, row, k, &pos, tbl);
766 WriteOutParagraph(
"Standard",
"", text1 , 0, 0, tbl);
767 tbl +=
"</FRAMESET>\n";
768 pos.left += ptsperchar * (double)width[k];
789int ASCIIImport::MultSpaces(
const TQString& text,
const int index)
const
793 TQChar lastchar =
'c';
796 for (uint i = index; i < text.length(); i++)
800 if ( (c !=
' ') && found)
802 else if (c ==
' ' && lastchar ==
' ')
810 bool ASCIIImport::ListItem( TQString *Line,
int no_lines,
825 TQString *currentline;
826 enum Listtype{none, bullet, dash} listtype[MAXLINES];
837 for(i = 0; i < no_lines; i++)
842 if( IsListItem( *Line, TQChar(
'*')))
844 listtype[i] = bullet;
848 if( IsListItem( *Line, TQChar(
'-')))
859 if( listcount == 0)
return false;
864 for( i = 0; i < no_lines; i++)
869 if( listtype[i] != none || Line->isEmpty())
871 if( listtype[begin] == bullet)
875 text.remove( TQRegExp(
"^[ \t]*\\* ") );
877 else if( listtype[begin] == dash)
881 text.remove( TQRegExp(
"^[ \t]*\\- ") );
883 else if( listtype[begin] == none)
886 currentline = firstline;
887 currentline += begin;
888 firstindent = Indent(*currentline);
892 secondindent = Indent( *currentline );
894 else secondindent = 0;
897 TQString text1 = text.simplifyWhiteSpace();
898 WriteOutParagraph(
"Standard", type, text1 , firstindent, secondindent, str);
915 if( listtype[begin] == bullet)
919 text.replace( TQRegExp(
"^[ \t]*\\* "),
"");
921 else if( listtype[begin] == dash)
925 text.replace( TQRegExp(
"^[ \t]*\\- "),
"");
927 else if( listtype[begin] == none)
930 currentline = firstline;
931 currentline += begin;
932 firstindent = Indent(*currentline);
936 secondindent = Indent( *currentline );
938 else secondindent = 0;
941 TQString text1 = text.simplifyWhiteSpace();
942 WriteOutParagraph(
"Standard", type, text1 , firstindent, secondindent, str);
960bool ASCIIImport::IsListItem( TQString FirstLine, TQChar mark )
968 int k = FirstLine.find(mark);
970 if( k < 0)
return false;
973 for( i=0; IsWhiteSpace( c = FirstLine.at((uint)i) ); i++);
975 if(i != k )
return false;
977 c = FirstLine.at((uint)++i);
979 return IsWhiteSpace(c);
984bool ASCIIImport::IsWhiteSpace(
const TQChar& c)
const
986 if ( c == TQChar(
' ') || c == TQChar(
'\t') )
992TQString ASCIIImport::readLine(TQTextStream& textstream,
bool& lastCharWasCr)
999 while (!textstream.atEnd())
1009 lastCharWasCr=
false;
1023 else if (ch==
char(12))
1031 lastCharWasCr=
false;