kpresenter

KPrImportStyleDia.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 "KPrDocument.h"
22#include <tqvbox.h>
23#include <tqlayout.h>
24#include <tqlineedit.h>
25#include <tqpushbutton.h>
26#include <tqlistbox.h>
27#include <tdemessagebox.h>
28#include "KPrImportStyleDia.h"
29#include <KoStore.h>
30#include <tqfile.h>
31#include <tdefiledialog.h>
32#include <kdebug.h>
33#include <tqlabel.h>
34#include "KPrTextObject.h"
35
36KPrImportStyleDia::KPrImportStyleDia( KPrDocument *_doc, KoStyleCollection* currentCollection,
37 TQWidget *parent, const char *name )
38 :KoImportStyleDia( currentCollection, parent, name ),
39 m_doc(_doc)
40{
41}
42
43KPrImportStyleDia::~KPrImportStyleDia()
44{
45}
46
47void KPrImportStyleDia::loadFile()
48{
49 KFileDialog fd( TQString(), TQString(), 0, 0, true );
50 TQStringList lst = "application/x-kpresenter";
51#if 0 //For the future
52 lst << "application/vnd.oasis.opendocument.presentation";
53#endif
54 fd.setMimeFilter( lst );
55 fd.setCaption(i18n("Import Style"));
56 KURL url;
57 if ( fd.exec() != TQDialog::Accepted )
58 return;
59 url = fd.selectedURL();
60 if( url.isEmpty() )
61 {
62 KMessageBox::sorry( this,
63 i18n("File name is empty."),
64 i18n("Import Style"));
65 return;
66 }
67 TQMap<TQString, TQString>insertStyle;
68 KoStore* store=KoStore::createStore( this, url.path(), KoStore::Read );
69 if (store )
70 {
71 if (store->open("maindoc.xml") )
72 {
73 clear();
74 m_listStyleName->clear();
75
76 TQDomDocument doc;
77 doc.setContent( store->device() );
78 TQDomElement word = doc.documentElement();
79
80 TQDomElement stylesElem = word.namedItem( "STYLES" ).toElement();
81 if ( !stylesElem.isNull() )
82 {
83 //todo
84 //duplicate code try to remove it !
85 TQValueList<TQString> followingStyles;
86 TQDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
87 for (unsigned int item = 0; item < listStyles.count(); item++)
88 {
89 TQDomElement styleElem = listStyles.item( item ).toElement();
90
91 KoParagStyle *sty = new KoParagStyle( TQString() );
92 // Load the style from the <STYLE> element
93 sty->loadStyle( styleElem );
94
95 if ( currentCollection()->findStyle( sty->name() ) )
96 sty->setName(generateStyleName(sty->name() + "-%1"));
97 TQString name = sty->displayName();
98 // ### TODO: we should offer the option of updating the
99 // existing style instead of creating a foo-1 style. Any ideas for a GUI?
100 if ( currentCollection()->findStyleByDisplayName( name ) )
101 sty->setDisplayName(generateStyleDisplayName(sty->displayName() + "-%1"));
102 insertStyle.insert( name, sty->name() );
103
104 TQDomElement formatElem = styleElem.namedItem( "FORMAT" ).toElement();
105 if ( !formatElem.isNull() )
106 sty->format() = KPrTextObject::loadFormat( formatElem, 0L, m_doc->defaultFont(),
107 m_doc->globalLanguage(), m_doc->globalHyphenation() );
108 else
109 kdWarning(33001) << "No FORMAT tag in <STYLE>" << endl; // This leads to problems in applyStyle().
110
111
112 // Style created, now let's try to add it
113 sty = m_styleList.addStyle(sty);
114
115 if( m_styleList.count() >= 0 && uint( m_styleList.count() ) > followingStyles.count() )
116 {
117 TQString following = styleElem.namedItem("FOLLOWING").toElement().attribute("name");
118 followingStyles.append( following );
119 }
120 else
121 kdWarning(33001) << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
122 }
123
124 Q_ASSERT( m_styleList.count() >= 0 && followingStyles.count() == uint( m_styleList.count() ) );
125
126 unsigned int i=0;
127 for( TQValueList<TQString>::Iterator it = followingStyles.begin(); it != followingStyles.end(); ++it ) {
128 TQString newName =*it;
129 if ( insertStyle.contains( *it ) )
130 newName = (insertStyle)[ *it ];
131
132 KoParagStyle * style = m_styleList.findStyle(newName);
133 if ( style )
134 m_styleList.styleAt(i++)->setFollowingStyle( style );
135 }
136
137 }
138 initList();
139 }
140 else if ( store->hasFile( "content.xml" ) )
141 {
142 //oasis file format (for koffice-1.5)
143 }
144 else
145 {
146 KMessageBox::error( this,
147 i18n("File is not a KPresenter file!"),
148 i18n("Import Style"));
149 }
150 store->close();
151 }
152 delete store;
153}
154
155
156#include "KPrImportStyleDia.moc"