~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kword/kwimportstyledia.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place - Suite 330,
17
 
   Boston, MA 02111-1307, USA.
18
 
*/
19
 
 
20
 
#include <klocale.h>
21
 
#include "kwdoc.h"
22
 
 
23
 
#include <qvbox.h>
24
 
#include <qlayout.h>
25
 
#include <qlineedit.h>
26
 
#include <qpushbutton.h>
27
 
#include <qlistbox.h>
28
 
#include <kmessagebox.h>
29
 
#include "kwimportstyledia.h"
30
 
#include <koStore.h>
31
 
#include <qfile.h>
32
 
 
33
 
#include <kfiledialog.h>
34
 
#include <kdebug.h>
35
 
#include <qlabel.h>
36
 
 
37
 
#include "kwtextparag.h"
38
 
 
39
 
KWImportStyleDia::KWImportStyleDia( KWDocument *_doc, KoStyleCollection* currentCollection, QWidget *parent, const char *name )
40
 
    :KoImportStyleDia( currentCollection, parent, name ),
41
 
     m_doc(_doc)
42
 
{
43
 
 
44
 
}
45
 
 
46
 
KWImportStyleDia::~KWImportStyleDia()
47
 
{
48
 
}
49
 
 
50
 
 
51
 
void KWImportStyleDia::loadFile()
52
 
{
53
 
    KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
54
 
    QStringList 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() != QDialog::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
 
    QMap<QString, QString>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
 
            QDomDocument doc;
82
 
            doc.setContent( store->device() );
83
 
            QDomElement word = doc.documentElement();
84
 
            QDomElement 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
 
                QValueList<QString> followingStyles;
96
 
                QDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
97
 
                for (unsigned int item = 0; item < listStyles.count(); item++)
98
 
                {
99
 
                    QDomElement styleElem = listStyles.item( item ).toElement();
100
 
 
101
 
                    KWStyle *sty = new KWStyle( QString::null );
102
 
                    // Load the paraglayout from the <STYLE> element
103
 
                    sty->loadStyle( styleElem, m_doc->syntaxVersion() ); //#### wrong syntaxVersion!
104
 
 
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
113
 
 
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() );
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
 
                    m_styleList.append(sty);
122
 
 
123
 
                    if(m_styleList.count() > followingStyles.count() )
124
 
                    {
125
 
                        QString 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( followingStyles.count() == m_styleList.count() );
133
 
 
134
 
                unsigned int i=0;
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 ];
139
 
 
140
 
                    KWStyle * style = findStyle(newName);
141
 
                    if ( style )
142
 
                        m_styleList.at(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
 
 
169
 
KWImportFrameTableStyleDia::KWImportFrameTableStyleDia( KWDocument *_doc, const QStringList &_list, StyleType _type, QWidget *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
 
    QVBox *page = makeVBoxMainWidget();
177
 
    new QLabel(i18n("Select style to import:"), page);
178
 
    m_listStyleName = new QListBox( page );
179
 
    m_listStyleName->setSelectionMode( QListBox::Multi );
180
 
    loadFile();
181
 
    resize (300, 400);
182
 
    setFocus();
183
 
}
184
 
 
185
 
KWImportFrameTableStyleDia::~KWImportFrameTableStyleDia()
186
 
{
187
 
    m_frameStyleList.setAutoDelete(true);
188
 
    m_tableStyleList.setAutoDelete(true);
189
 
    m_frameStyleList.clear();
190
 
    m_tableStyleList.clear();
191
 
}
192
 
 
193
 
QString KWImportFrameTableStyleDia::generateStyleName( const QString & templateName )
194
 
{
195
 
    QString 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
 
 
207
 
void KWImportFrameTableStyleDia::loadFile()
208
 
{
209
 
    KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
210
 
    QStringList 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() != QDialog::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
 
            QDomDocument doc;
233
 
            doc.setContent( store->device() );
234
 
            QDomElement word = doc.documentElement();
235
 
            if ( m_typeStyle ==frameStyle )
236
 
            {
237
 
                QDomNodeList listStyles = word.elementsByTagName( "FRAMESTYLE" );
238
 
                for (unsigned int item = 0; item < listStyles.count(); item++) {
239
 
                    QDomElement styleElem = listStyles.item( item ).toElement();
240
 
 
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);
246
 
                }
247
 
            }
248
 
            else
249
 
            {
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);
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
 
 
285
 
void KWImportFrameTableStyleDia::initList()
286
 
{
287
 
    QStringList 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
 
 
306
 
void KWImportFrameTableStyleDia::slotOk()
307
 
{
308
 
    for (uint i = 0; i< m_listStyleName->count();i++)
309
 
    {
310
 
        if ( !m_listStyleName->isSelected( i ))
311
 
        {
312
 
            QString name = m_listStyleName->text(i );
313
 
            if ( m_typeStyle ==frameStyle ) // frame styles
314
 
            {
315
 
                //remove this style from list
316
 
                QPtrListIterator<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
 
                QPtrListIterator<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"