1
/* This file is part of the KDE project
2
Copyright (C) 2002 Montel Laurent <lmontel@mandrakesoft.com>
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.
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.
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., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
25
#include <qlineedit.h>
26
#include <qpushbutton.h>
28
#include <kmessagebox.h>
29
#include "kwimportstyledia.h"
33
#include <kfiledialog.h>
37
#include "kwtextparag.h"
39
KWImportStyleDia::KWImportStyleDia( KWDocument *_doc, KoStyleCollection* currentCollection, QWidget *parent, const char *name )
40
:KoImportStyleDia( currentCollection, parent, name ),
46
KWImportStyleDia::~KWImportStyleDia()
51
void KWImportStyleDia::loadFile()
53
KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
54
QStringList lst = "application/x-kword";
56
lst << "application/vnd.oasis.opendocument.text";
58
fd.setMimeFilter( lst );
59
fd.setCaption(i18n("Import Style"));
61
if ( fd.exec() != QDialog::Accepted )
63
url = fd.selectedURL();
66
KMessageBox::sorry( this,
67
i18n("File name is empty."),
68
i18n("Import Style"));
71
QMap<QString, QString>insertStyle;
73
KoStore* store=KoStore::createStore( this, url, KoStore::Read );
76
if (store->open("maindoc.xml") )
79
m_listStyleName->clear();
82
doc.setContent( store->device() );
83
QDomElement word = doc.documentElement();
84
QDomElement stylesElem = word.namedItem( "STYLES" ).toElement();
85
if ( !stylesElem.isNull() )
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)
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.
95
QValueList<QString> followingStyles;
96
QDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
97
for (unsigned int item = 0; item < listStyles.count(); item++)
99
QDomElement styleElem = listStyles.item( item ).toElement();
101
KWStyle *sty = new KWStyle( QString::null );
102
// Load the paraglayout from the <STYLE> element
103
sty->loadStyle( styleElem, m_doc->syntaxVersion() ); //#### wrong syntaxVersion!
105
QString name = sty->displayName();
106
if ( currentCollection()->findStyle( name ) )
107
sty->setInternalName(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()->findTranslatedStyle( name ) )
111
sty->setDisplayName(generateStyleDisplayName(sty->displayName() + "-%1"));
112
insertStyle.insert( name, sty->name() ); // old name -> new name
114
QDomElement 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() );
118
kdWarning(32001) << "No FORMAT tag in <STYLE>" << endl; // This leads to problems in applyStyle().
120
// Style created, now let's try to add it
121
m_styleList.append(sty);
123
if(m_styleList.count() > followingStyles.count() )
125
QString following = styleElem.namedItem("FOLLOWING").toElement().attribute("name");
126
followingStyles.append( following );
129
kdWarning () << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
132
Q_ASSERT( followingStyles.count() == m_styleList.count() );
135
for( QValueList<QString>::Iterator it = followingStyles.begin(); it != followingStyles.end(); ++it ) {
136
QString newName =*it;
137
if ( insertStyle.contains( *it ) )
138
newName = (insertStyle)[ *it ];
140
KWStyle * style = findStyle(newName);
142
m_styleList.at(i++)->setFollowingStyle( style );
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"));
154
else if ( store->hasFile( "content.xml" ) )
156
//oasis file format (for koffice-1.5)
160
KMessageBox::error( this,
161
i18n("This file is not a KWord file!"),
162
i18n("Import Style"));
169
KWImportFrameTableStyleDia::KWImportFrameTableStyleDia( KWDocument *_doc, const QStringList &_list, StyleType _type, QWidget *parent, const char *name )
170
: KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
172
setCaption( i18n("Import Style") );
176
QVBox *page = makeVBoxMainWidget();
177
new QLabel(i18n("Select style to import:"), page);
178
m_listStyleName = new QListBox( page );
179
m_listStyleName->setSelectionMode( QListBox::Multi );
185
KWImportFrameTableStyleDia::~KWImportFrameTableStyleDia()
187
m_frameStyleList.setAutoDelete(true);
188
m_tableStyleList.setAutoDelete(true);
189
m_frameStyleList.clear();
190
m_tableStyleList.clear();
193
QString KWImportFrameTableStyleDia::generateStyleName( const QString & templateName )
199
name = templateName.arg( num );
200
exists = (m_list.findIndex( name )!=-1);
207
void KWImportFrameTableStyleDia::loadFile()
209
KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
210
QStringList lst = "application/x-kword";
212
lst << "application/vnd.oasis.opendocument.text";
214
fd.setMimeFilter( lst );
215
fd.setCaption(i18n("Import Style"));
217
if ( fd.exec() != QDialog::Accepted )
219
url = fd.selectedURL();
222
KMessageBox::sorry( this,
223
i18n("File name is empty."),
224
i18n("Import Style"));
227
KoStore* store=KoStore::createStore( this, url, KoStore::Read );
230
if (store->open("maindoc.xml") )
233
doc.setContent( store->device() );
234
QDomElement word = doc.documentElement();
235
if ( m_typeStyle ==frameStyle )
237
QDomNodeList listStyles = word.elementsByTagName( "FRAMESTYLE" );
238
for (unsigned int item = 0; item < listStyles.count(); item++) {
239
QDomElement styleElem = listStyles.item( item ).toElement();
241
KWFrameStyle *sty = new KWFrameStyle( styleElem );
242
QString name =sty->name();
243
if ( m_list.findIndex( name )!=-1 )
244
sty->setName(generateStyleName( sty->displayName() + QString( "-%1")));
245
m_frameStyleList.append( sty);
250
QDomNodeList listStyles = word.elementsByTagName( "TABLESTYLE" );
251
for (unsigned int item = 0; item < listStyles.count(); item++) {
252
QDomElement styleElem = listStyles.item( item ).toElement();
253
KWTableStyle *sty = new KWTableStyle( styleElem,m_doc,2 );
254
QString name =sty->name();
255
if ( m_list.findIndex( name )!=-1 )
256
sty->setName(generateStyleName( sty->displayName() + QString( "-%1")));
257
m_tableStyleList.append( sty);
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"));
268
else if ( store->hasFile( "content.xml" ) )
270
//oasis file format (for koffice-1.5)
274
KMessageBox::error( this,
275
i18n("This file is not a KWord file!"),
276
i18n("Import Style"));
285
void KWImportFrameTableStyleDia::initList()
288
if ( m_typeStyle ==frameStyle )
290
for ( KWFrameStyle * p = m_frameStyleList.first(); p != 0L; p = m_frameStyleList.next() )
292
lst<<p->displayName();
297
for ( KWTableStyle * p = m_tableStyleList.first(); p != 0L; p = m_tableStyleList.next() )
299
lst<<p->displayName();
303
m_listStyleName->insertStringList(lst);
306
void KWImportFrameTableStyleDia::slotOk()
308
for (uint i = 0; i< m_listStyleName->count();i++)
310
if ( !m_listStyleName->isSelected( i ))
312
QString name = m_listStyleName->text(i );
313
if ( m_typeStyle ==frameStyle ) // frame styles
315
//remove this style from list
316
QPtrListIterator<KWFrameStyle> styleIt( m_frameStyleList );
317
for ( ; styleIt.current(); ++styleIt )
319
if ( styleIt.current()->displayName() == name )
321
m_frameStyleList.remove(styleIt.current());
326
else // then it will have to be table styles
328
//remove this style from list
329
QPtrListIterator<KWTableStyle> styleIt( m_tableStyleList );
330
for ( ; styleIt.current(); ++styleIt )
332
if ( styleIt.current()->displayName() == name )
334
m_tableStyleList.remove(styleIt.current());
341
KDialogBase::slotOk();
343
#include "kwimportstyledia.moc"