kword

KWImportStyleDia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Montel Laurent <lmontel@mandrakesoft.com>
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 <tdelocale.h>
21#include "KWDocument.h"
22
23#include <tqvbox.h>
24#include <tqlayout.h>
25#include <tqlineedit.h>
26#include <tqpushbutton.h>
27#include <tqlistbox.h>
28#include <tdemessagebox.h>
29#include "KWImportStyleDia.h"
30#include <KoStore.h>
31#include <tqfile.h>
32
33#include <tdefiledialog.h>
34#include <kdebug.h>
35#include <tqlabel.h>
36
37#include "KWTextParag.h"
38
39KWImportStyleDia::KWImportStyleDia( KWDocument *_doc, KoStyleCollection* currentCollection, TQWidget *parent, const char *name )
40 :KoImportStyleDia( currentCollection, parent, name ),
41 m_doc(_doc)
42{
43
44}
45
46KWImportStyleDia::~KWImportStyleDia()
47{
48}
49
50
51void KWImportStyleDia::loadFile()
52{
53 KFileDialog fd( TQString(), TQString(), 0, 0, true );
54 TQStringList lst = "application/x-kword";
55#if 0
56 lst << "application/vnd.oasis.opendocument.text";
57#endif
58 fd.setMimeFilter( lst );
59 fd.setCaption(i18n("Import Style"));
60 KURL url;
61 if ( fd.exec() != TQDialog::Accepted )
62 return;
63 url = fd.selectedURL();
64 if( url.isEmpty() )
65 {
66 KMessageBox::sorry( this,
67 i18n("File name is empty."),
68 i18n("Import Style"));
69 return;
70 }
71 TQMap<TQString, TQString>insertStyle;
72
73 KoStore* store=KoStore::createStore( this, url, KoStore::Read );
74 if (store )
75 {
76 if (store->open("maindoc.xml") )
77 {
78 clear();
79 m_listStyleName->clear();
80
81 TQDomDocument doc;
82 doc.setContent( store->device() );
83 TQDomElement word = doc.documentElement();
84 TQDomElement stylesElem = word.namedItem( "STYLES" ).toElement();
85 if ( !stylesElem.isNull() )
86 {
87 //todo
88 //duplicate code try to remove it !
89 // TODO: use loadOasisStyleTemplates() for OASIS
90 // (and put as much code as possible in koimportstyledia of course)
91 //
92 // I guess we'll have to keep this old loading code forever though,
93 // so we can't really get rid of the subclasses.
94
95 TQValueList<TQString> followingStyles;
96 TQDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
97 for (unsigned int item = 0; item < listStyles.count(); item++)
98 {
99 TQDomElement styleElem = listStyles.item( item ).toElement();
100
101 KoParagStyle *sty = new KoParagStyle( TQString() );
102 // Load the paraglayout from the <STYLE> element
103 sty->loadStyle( styleElem, m_doc->syntaxVersion() ); //#### wrong syntaxVersion!
104
105 TQString name = sty->displayName();
106 if ( currentCollection()->findStyle( name ) )
107 sty->setName(generateStyleName(sty->name() + "-%1"));
108 // ### TODO: we should offer the option of updating the
109 // existing style instead of creating a foo-1 style. Any ideas for a GUI?
110 if ( currentCollection()->findStyleByDisplayName( name ) )
111 sty->setDisplayName(generateStyleDisplayName(sty->displayName() + "-%1"));
112 insertStyle.insert( name, sty->name() ); // old name -> new name
113
114 TQDomElement formatElem = styleElem.namedItem( "FORMAT" ).toElement();
115 if ( !formatElem.isNull() )
116 sty->format() = KWTextParag::loadFormat( formatElem, 0L, m_doc->defaultFont(), m_doc->globalLanguage(), m_doc->globalHyphenation() );
117 else
118 kdWarning(32001) << "No FORMAT tag in <STYLE>" << endl; // This leads to problems in applyStyle().
119
120 // Style created, now let's try to add it
121 sty = m_styleList.addStyle(sty);
122
123 if( m_styleList.count() >= 0 && uint( m_styleList.count() ) > followingStyles.count() )
124 {
125 TQString following = styleElem.namedItem("FOLLOWING").toElement().attribute("name");
126 followingStyles.append( following );
127 }
128 else
129 kdWarning () << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
130 }
131
132 Q_ASSERT( m_styleList.count() >= 0 && followingStyles.count() == uint( m_styleList.count() ) );
133
134 unsigned int i=0;
135 for( TQValueList<TQString>::Iterator it = followingStyles.begin(); it != followingStyles.end(); ++it ) {
136 TQString newName =*it;
137 if ( insertStyle.contains( *it ) )
138 newName = (insertStyle)[ *it ];
139
140 KoParagStyle * style = m_styleList.findStyle(newName);
141 if ( style )
142 m_styleList.styleAt(i++)->setFollowingStyle( style );
143 }
144
145 }
146 initList();
147 if(m_styleList.count() == 0) {
148 KMessageBox::error( this,
149 i18n("File does not contain any styles. It may be the wrong version."),
150 i18n("Import Style"));
151 }
152
153 }
154 else if ( store->hasFile( "content.xml" ) )
155 {
156 //oasis file format (for koffice-1.5)
157 }
158 else
159 {
160 KMessageBox::error( this,
161 i18n("This file is not a KWord file!"),
162 i18n("Import Style"));
163 }
164 store->close();
165 }
166 delete store;
167}
168
169KWImportFrameTableStyleDia::KWImportFrameTableStyleDia( KWDocument *_doc, const TQStringList &_list, StyleType _type, TQWidget *parent, const char *name )
170 : KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
171{
172 setCaption( i18n("Import Style") );
173 m_doc=_doc;
174 m_typeStyle = _type;
175 m_list =_list;
176 TQVBox *page = makeVBoxMainWidget();
177 new TQLabel(i18n("Select style to import:"), page);
178 m_listStyleName = new TQListBox( page );
179 m_listStyleName->setSelectionMode( TQListBox::Multi );
180 loadFile();
181 resize (300, 400);
182 setFocus();
183}
184
185KWImportFrameTableStyleDia::~KWImportFrameTableStyleDia()
186{
187 m_frameStyleList.setAutoDelete(true);
188 m_tableStyleList.setAutoDelete(true);
189 m_frameStyleList.clear();
190 m_tableStyleList.clear();
191}
192
193TQString KWImportFrameTableStyleDia::generateStyleName( const TQString & templateName )
194{
195 TQString name;
196 int num = 1;
197 bool exists;
198 do {
199 name = templateName.arg( num );
200 exists = (m_list.findIndex( name )!=-1);
201 ++num;
202 } while ( exists );
203 return name;
204}
205
206
207void KWImportFrameTableStyleDia::loadFile()
208{
209 KFileDialog fd( TQString(), TQString(), 0, 0, true );
210 TQStringList lst = "application/x-kword";
211#if 0
212 lst << "application/vnd.oasis.opendocument.text";
213#endif
214 fd.setMimeFilter( lst );
215 fd.setCaption(i18n("Import Style"));
216 KURL url;
217 if ( fd.exec() != TQDialog::Accepted )
218 return;
219 url = fd.selectedURL();
220 if( url.isEmpty() )
221 {
222 KMessageBox::sorry( this,
223 i18n("File name is empty."),
224 i18n("Import Style"));
225 return;
226 }
227 KoStore* store=KoStore::createStore( this, url, KoStore::Read );
228 if (store )
229 {
230 if (store->open("maindoc.xml") )
231 {
232 TQDomDocument doc;
233 doc.setContent( store->device() );
234 TQDomElement word = doc.documentElement();
235 if ( m_typeStyle ==frameStyle )
236 {
237 TQDomNodeList listStyles = word.elementsByTagName( "FRAMESTYLE" );
238 for (unsigned int item = 0; item < listStyles.count(); item++) {
239 TQDomElement styleElem = listStyles.item( item ).toElement();
240
241 KWFrameStyle *sty = new KWFrameStyle( styleElem );
242 TQString name =sty->name();
243 if ( m_list.findIndex( name )!=-1 )
244 sty->setDisplayName(generateStyleName( sty->displayName() + TQString( "-%1")));
245 m_frameStyleList.append( sty);
246 }
247 }
248 else
249 {
250 TQDomNodeList listStyles = word.elementsByTagName( "TABLESTYLE" );
251 for (unsigned int item = 0; item < listStyles.count(); item++) {
252 TQDomElement styleElem = listStyles.item( item ).toElement();
253 KWTableStyle *sty = new KWTableStyle( styleElem,m_doc,2 );
254 TQString name =sty->name();
255 if ( m_list.findIndex( name )!=-1 )
256 sty->setName(generateStyleName( sty->displayName() + TQString( "-%1")));
257 m_tableStyleList.append( sty);
258 }
259 }
260 initList();
261 if(m_tableStyleList.count() == 0 && m_frameStyleList.count()==0) {
262 KMessageBox::error( this,
263 i18n("File does not contain any styles. It may be the wrong version."),
264 i18n("Import Style"));
265 }
266
267 }
268 else if ( store->hasFile( "content.xml" ) )
269 {
270 //oasis file format (for koffice-1.5)
271 }
272 else
273 {
274 KMessageBox::error( this,
275 i18n("This file is not a KWord file!"),
276 i18n("Import Style"));
277 }
278
279 store->close();
280 }
281
282 delete store;
283}
284
285void KWImportFrameTableStyleDia::initList()
286{
287 TQStringList lst;
288 if ( m_typeStyle ==frameStyle )
289 {
290 for ( KWFrameStyle * p = m_frameStyleList.first(); p != 0L; p = m_frameStyleList.next() )
291 {
292 lst<<p->displayName();
293 }
294 }
295 else
296 {
297 for ( KWTableStyle * p = m_tableStyleList.first(); p != 0L; p = m_tableStyleList.next() )
298 {
299 lst<<p->displayName();
300 }
301 }
302
303 m_listStyleName->insertStringList(lst);
304}
305
306void KWImportFrameTableStyleDia::slotOk()
307{
308 for (uint i = 0; i< m_listStyleName->count();i++)
309 {
310 if ( !m_listStyleName->isSelected( i ))
311 {
312 TQString name = m_listStyleName->text(i );
313 if ( m_typeStyle ==frameStyle ) // frame styles
314 {
315 //remove this style from list
316 TQPtrListIterator<KWFrameStyle> styleIt( m_frameStyleList );
317 for ( ; styleIt.current(); ++styleIt )
318 {
319 if ( styleIt.current()->displayName() == name )
320 {
321 m_frameStyleList.remove(styleIt.current());
322 break;
323 }
324 }
325 }
326 else // then it will have to be table styles
327 {
328 //remove this style from list
329 TQPtrListIterator<KWTableStyle> styleIt( m_tableStyleList );
330 for ( ; styleIt.current(); ++styleIt )
331 {
332 if ( styleIt.current()->displayName() == name )
333 {
334 m_tableStyleList.remove(styleIt.current());
335 break;
336 }
337 }
338 }
339 }
340 }
341 KDialogBase::slotOk();
342}
343#include "KWImportStyleDia.moc"
static KoTextFormat loadFormat(TQDomElement &formatElem, KoTextFormat *refFormat, const TQFont &defaultFont, const TQString &defaultLanguage, bool hyphanation)
Public for loading a style (KWord-1.3 XML).