00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <klocale.h>
00021 #include "KWDocument.h"
00022
00023 #include <qvbox.h>
00024 #include <qlayout.h>
00025 #include <qlineedit.h>
00026 #include <qpushbutton.h>
00027 #include <qlistbox.h>
00028 #include <kmessagebox.h>
00029 #include "KWImportStyleDia.h"
00030 #include <KoStore.h>
00031 #include <qfile.h>
00032
00033 #include <kfiledialog.h>
00034 #include <kdebug.h>
00035 #include <qlabel.h>
00036
00037 #include "KWTextParag.h"
00038
00039 KWImportStyleDia::KWImportStyleDia( KWDocument *_doc, KoStyleCollection* currentCollection, QWidget *parent, const char *name )
00040 :KoImportStyleDia( currentCollection, parent, name ),
00041 m_doc(_doc)
00042 {
00043
00044 }
00045
00046 KWImportStyleDia::~KWImportStyleDia()
00047 {
00048 }
00049
00050
00051 void KWImportStyleDia::loadFile()
00052 {
00053 KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
00054 QStringList lst = "application/x-kword";
00055 #if 0
00056 lst << "application/vnd.oasis.opendocument.text";
00057 #endif
00058 fd.setMimeFilter( lst );
00059 fd.setCaption(i18n("Import Style"));
00060 KURL url;
00061 if ( fd.exec() != QDialog::Accepted )
00062 return;
00063 url = fd.selectedURL();
00064 if( url.isEmpty() )
00065 {
00066 KMessageBox::sorry( this,
00067 i18n("File name is empty."),
00068 i18n("Import Style"));
00069 return;
00070 }
00071 QMap<QString, QString>insertStyle;
00072
00073 KoStore* store=KoStore::createStore( this, url, KoStore::Read );
00074 if (store )
00075 {
00076 if (store->open("maindoc.xml") )
00077 {
00078 clear();
00079 m_listStyleName->clear();
00080
00081 QDomDocument doc;
00082 doc.setContent( store->device() );
00083 QDomElement word = doc.documentElement();
00084 QDomElement stylesElem = word.namedItem( "STYLES" ).toElement();
00085 if ( !stylesElem.isNull() )
00086 {
00087
00088
00089
00090
00091
00092
00093
00094
00095 QValueList<QString> followingStyles;
00096 QDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
00097 for (unsigned int item = 0; item < listStyles.count(); item++)
00098 {
00099 QDomElement styleElem = listStyles.item( item ).toElement();
00100
00101 KoParagStyle *sty = new KoParagStyle( QString::null );
00102
00103 sty->loadStyle( styleElem, m_doc->syntaxVersion() );
00104
00105 QString name = sty->displayName();
00106 if ( currentCollection()->findStyle( name ) )
00107 sty->setName(generateStyleName(sty->name() + "-%1"));
00108
00109
00110 if ( currentCollection()->findStyleByDisplayName( name ) )
00111 sty->setDisplayName(generateStyleDisplayName(sty->displayName() + "-%1"));
00112 insertStyle.insert( name, sty->name() );
00113
00114 QDomElement formatElem = styleElem.namedItem( "FORMAT" ).toElement();
00115 if ( !formatElem.isNull() )
00116 sty->format() = KWTextParag::loadFormat( formatElem, 0L, m_doc->defaultFont(), m_doc->globalLanguage(), m_doc->globalHyphenation() );
00117 else
00118 kdWarning(32001) << "No FORMAT tag in <STYLE>" << endl;
00119
00120
00121 sty = m_styleList.addStyle(sty);
00122
00123 if( m_styleList.count() >= 0 && uint( m_styleList.count() ) > followingStyles.count() )
00124 {
00125 QString following = styleElem.namedItem("FOLLOWING").toElement().attribute("name");
00126 followingStyles.append( following );
00127 }
00128 else
00129 kdWarning () << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
00130 }
00131
00132 Q_ASSERT( m_styleList.count() >= 0 && followingStyles.count() == uint( m_styleList.count() ) );
00133
00134 unsigned int i=0;
00135 for( QValueList<QString>::Iterator it = followingStyles.begin(); it != followingStyles.end(); ++it ) {
00136 QString newName =*it;
00137 if ( insertStyle.contains( *it ) )
00138 newName = (insertStyle)[ *it ];
00139
00140 KoParagStyle * style = m_styleList.findStyle(newName);
00141 if ( style )
00142 m_styleList.styleAt(i++)->setFollowingStyle( style );
00143 }
00144
00145 }
00146 initList();
00147 if(m_styleList.count() == 0) {
00148 KMessageBox::error( this,
00149 i18n("File does not contain any styles. It may be the wrong version."),
00150 i18n("Import Style"));
00151 }
00152
00153 }
00154 else if ( store->hasFile( "content.xml" ) )
00155 {
00156
00157 }
00158 else
00159 {
00160 KMessageBox::error( this,
00161 i18n("This file is not a KWord file!"),
00162 i18n("Import Style"));
00163 }
00164 store->close();
00165 }
00166 delete store;
00167 }
00168
00169 KWImportFrameTableStyleDia::KWImportFrameTableStyleDia( KWDocument *_doc, const QStringList &_list, StyleType _type, QWidget *parent, const char *name )
00170 : KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
00171 {
00172 setCaption( i18n("Import Style") );
00173 m_doc=_doc;
00174 m_typeStyle = _type;
00175 m_list =_list;
00176 QVBox *page = makeVBoxMainWidget();
00177 new QLabel(i18n("Select style to import:"), page);
00178 m_listStyleName = new QListBox( page );
00179 m_listStyleName->setSelectionMode( QListBox::Multi );
00180 loadFile();
00181 resize (300, 400);
00182 setFocus();
00183 }
00184
00185 KWImportFrameTableStyleDia::~KWImportFrameTableStyleDia()
00186 {
00187 m_frameStyleList.setAutoDelete(true);
00188 m_tableStyleList.setAutoDelete(true);
00189 m_frameStyleList.clear();
00190 m_tableStyleList.clear();
00191 }
00192
00193 QString KWImportFrameTableStyleDia::generateStyleName( const QString & templateName )
00194 {
00195 QString name;
00196 int num = 1;
00197 bool exists;
00198 do {
00199 name = templateName.arg( num );
00200 exists = (m_list.findIndex( name )!=-1);
00201 ++num;
00202 } while ( exists );
00203 return name;
00204 }
00205
00206
00207 void KWImportFrameTableStyleDia::loadFile()
00208 {
00209 KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
00210 QStringList lst = "application/x-kword";
00211 #if 0
00212 lst << "application/vnd.oasis.opendocument.text";
00213 #endif
00214 fd.setMimeFilter( lst );
00215 fd.setCaption(i18n("Import Style"));
00216 KURL url;
00217 if ( fd.exec() != QDialog::Accepted )
00218 return;
00219 url = fd.selectedURL();
00220 if( url.isEmpty() )
00221 {
00222 KMessageBox::sorry( this,
00223 i18n("File name is empty."),
00224 i18n("Import Style"));
00225 return;
00226 }
00227 KoStore* store=KoStore::createStore( this, url, KoStore::Read );
00228 if (store )
00229 {
00230 if (store->open("maindoc.xml") )
00231 {
00232 QDomDocument doc;
00233 doc.setContent( store->device() );
00234 QDomElement word = doc.documentElement();
00235 if ( m_typeStyle ==frameStyle )
00236 {
00237 QDomNodeList listStyles = word.elementsByTagName( "FRAMESTYLE" );
00238 for (unsigned int item = 0; item < listStyles.count(); item++) {
00239 QDomElement styleElem = listStyles.item( item ).toElement();
00240
00241 KWFrameStyle *sty = new KWFrameStyle( styleElem );
00242 QString name =sty->name();
00243 if ( m_list.findIndex( name )!=-1 )
00244 sty->setDisplayName(generateStyleName( sty->displayName() + QString( "-%1")));
00245 m_frameStyleList.append( sty);
00246 }
00247 }
00248 else
00249 {
00250 QDomNodeList listStyles = word.elementsByTagName( "TABLESTYLE" );
00251 for (unsigned int item = 0; item < listStyles.count(); item++) {
00252 QDomElement styleElem = listStyles.item( item ).toElement();
00253 KWTableStyle *sty = new KWTableStyle( styleElem,m_doc,2 );
00254 QString name =sty->name();
00255 if ( m_list.findIndex( name )!=-1 )
00256 sty->setName(generateStyleName( sty->displayName() + QString( "-%1")));
00257 m_tableStyleList.append( sty);
00258 }
00259 }
00260 initList();
00261 if(m_tableStyleList.count() == 0 && m_frameStyleList.count()==0) {
00262 KMessageBox::error( this,
00263 i18n("File does not contain any styles. It may be the wrong version."),
00264 i18n("Import Style"));
00265 }
00266
00267 }
00268 else if ( store->hasFile( "content.xml" ) )
00269 {
00270
00271 }
00272 else
00273 {
00274 KMessageBox::error( this,
00275 i18n("This file is not a KWord file!"),
00276 i18n("Import Style"));
00277 }
00278
00279 store->close();
00280 }
00281
00282 delete store;
00283 }
00284
00285 void KWImportFrameTableStyleDia::initList()
00286 {
00287 QStringList lst;
00288 if ( m_typeStyle ==frameStyle )
00289 {
00290 for ( KWFrameStyle * p = m_frameStyleList.first(); p != 0L; p = m_frameStyleList.next() )
00291 {
00292 lst<<p->displayName();
00293 }
00294 }
00295 else
00296 {
00297 for ( KWTableStyle * p = m_tableStyleList.first(); p != 0L; p = m_tableStyleList.next() )
00298 {
00299 lst<<p->displayName();
00300 }
00301 }
00302
00303 m_listStyleName->insertStringList(lst);
00304 }
00305
00306 void KWImportFrameTableStyleDia::slotOk()
00307 {
00308 for (uint i = 0; i< m_listStyleName->count();i++)
00309 {
00310 if ( !m_listStyleName->isSelected( i ))
00311 {
00312 QString name = m_listStyleName->text(i );
00313 if ( m_typeStyle ==frameStyle )
00314 {
00315
00316 QPtrListIterator<KWFrameStyle> styleIt( m_frameStyleList );
00317 for ( ; styleIt.current(); ++styleIt )
00318 {
00319 if ( styleIt.current()->displayName() == name )
00320 {
00321 m_frameStyleList.remove(styleIt.current());
00322 break;
00323 }
00324 }
00325 }
00326 else
00327 {
00328
00329 QPtrListIterator<KWTableStyle> styleIt( m_tableStyleList );
00330 for ( ; styleIt.current(); ++styleIt )
00331 {
00332 if ( styleIt.current()->displayName() == name )
00333 {
00334 m_tableStyleList.remove(styleIt.current());
00335 break;
00336 }
00337 }
00338 }
00339 }
00340 }
00341 KDialogBase::slotOk();
00342 }
00343 #include "KWImportStyleDia.moc"