summaryrefslogtreecommitdiffstats
path: root/kugar/part/kugar_part.cpp
blob: 98352e1393c9a28c739427d96473c2866fa4b6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2000 Phil Thompson <phil@river-bank.demon.co.uk>
//
// This file contains the implementation of the Kugar KPart.


#include <tdeapplication.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include <tdeaboutdata.h>
#include <tdeaction.h>
#include <kstdaction.h>
#include <tdemessagebox.h>
#include <tdeio/netaccess.h>
#include <tqfile.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tqwidget.h>
#include <tdefiledialog.h>
#include <kdebug.h>
#include <KoStore.h>

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "kugar_part.h"
#include "kugar_view.h"
#include "kugar_factory.h"

#include <KoFilterManager.h>

KugarPart::KugarPart( TQWidget *parentWidget, const char *widgetName, 
                      TQObject* parent, const char* name, bool singleViewMode ) 
    : KoDocument( parentWidget, widgetName, parent, name, singleViewMode ),
    m_templateOk( false )
{
    setInstance( KugarFactory::global(), false );
    m_reportEngine = new Kugar::MReportEngine();
    connect( m_reportEngine, TQ_SIGNAL( preferedTemplate( const TQString & ) ),
             TQ_SLOT( slotPreferredTemplate( const TQString & ) ) );
}

KugarPart::~KugarPart()
{
    m_reportEngine->removeRef();
    //  closeURL();
}

bool KugarPart::loadOasis( const TQDomDocument&, KoOasisStyles&, 
                           const TQDomDocument&, KoStore* )
{
    return false;
}

bool KugarPart::saveOasis( KoStore*, KoXmlWriter* )
{
    return false;
}

bool KugarPart::loadXML( TQIODevice *file, const TQDomDocument & /*doc*/ )
{
    m_docURL = url();
    bool ok = true;
    if ( file )
    {
        file->reset();
        m_reportData=TQString(file->readAll());

        if ( m_reportData.length() != 0 )
        {
            ok = m_reportEngine->setReportData( m_reportData );

            if ( m_templateOk )
            {
                m_reportEngine->renderReport();
                if ( ok )
                {
                    TQPtrList<KoView> vs = views();
                    if ( vs.count() )
                    {
                        for ( KoView * v = vs.first();v;v = vs.next() )
                        {
                            ok = static_cast<KugarView*>( v->tqt_cast( "KugarView" ) ) ->renderReport();
                            if ( !ok )
                                break;
                        }
                    }
                }
            }
            if ( !ok )
                KMessageBox::sorry( 0, i18n( "Invalid data file %1" ).arg( m_file ) );
        }
        else
        {
            ok = false;
            KMessageBox::sorry( 0, i18n( "The zero sized data file %1 can't be rendered" ).arg( m_file ) );
        }

    }
    else
    {
        ok = false;
        KMessageBox::sorry( 0, i18n( "Unable to open data file: %1" ).arg( m_file ) );
    }

    return ok;
}

bool KugarPart::initDoc( InitDocFlags /*flags*/, TQWidget* /*parentWidget*/ )
{
    TQString filename;
    bool ok = false;

    KFileDialog *dialog = 
        new KFileDialog( TQString(), TQString(), 0L, "file dialog", true );
    dialog->setMimeFilter( KoFilterManager::mimeFilter( KoDocument::readNativeFormatMimeType(),
                           KoFilterManager::Import ) );
    if ( dialog->exec() != TQDialog::Accepted )
    {
        delete dialog;
        return false;
    }

    KURL url( dialog->selectedURL() );
    delete dialog;

    if ( url.isEmpty() )
        return false;

    if ( url.isValid() )
    {
        ok = openURL( url );
    }
    return ok;

    // If nothing is loaded, do initialize here
    return TRUE;
}

KoView* KugarPart::createViewInstance( TQWidget* parent, const char* name )
{
    KugarView * v = new KugarView( this, parent, name );
    if ( m_templateOk )
        v->renderReport();
    return v;
}


void KugarPart::slotPreferredTemplate( const TQString &tpl )
{
    KURL url( tpl );
    TQString localtpl;
    bool isTemp = false;

    if ( !url.isValid() )
    {
/*        kdDebug() << "mailformed url" << endl;*/
        if ( tpl.find( '/' ) >= 0 )
        {
            if ( tpl.startsWith( "." ) )
            {
                KURL tmpURL( m_docURL );
                tmpURL.setFileName( "" );
                tmpURL.addPath( tpl );
                if ( TDEIO::NetAccess::download( tmpURL, localtpl ) )
                    isTemp = true;
                else
                    KMessageBox::sorry( 0, i18n( "Unable to download template file: %1" ).arg( url.prettyURL() ) );
            }
            else
                localtpl = tpl;
        }
        else
        {
            TQString former_localtpl = localtpl;
            localtpl = kapp -> dirs() -> findResource( "data", "kugar/templates/" + tpl );
            if ( localtpl.isEmpty() )
            {
                KURL tmpURL( m_docURL );
                tmpURL.setFileName( "" );
                tmpURL.addPath( tpl );
                if ( TDEIO::NetAccess::download( tmpURL, localtpl ) )
                    isTemp = true;
                else
                    KMessageBox::sorry( 0, i18n( "Unable to download template file: %1" ).arg( url.prettyURL() ) );
            }
        }
    }
    else
    {
        if ( TDEIO::NetAccess::download( url, localtpl ) )
            isTemp = true;
        else
            KMessageBox::sorry( 0, i18n( "Unable to download template file: %1" ).arg( url.prettyURL() ) );
    }

/*    kdDebug() << "localtpl: " << localtpl.latin1() << endl;*/
    if ( !localtpl.isEmpty() )
    {
        TQFile f( localtpl );

        if ( f.open( IO_ReadOnly ) )
        {
/*            kdDebug() << "localtpl opened" << endl;*/
            // Try to find out whether it is a mime multi part file
            char buf[ 5 ];
            if ( f.readBlock( buf, 4 ) == 4 )
            {
                bool isRawXML = ( strncasecmp( buf, "<?xm", 4 ) == 0 );
                f.close();

                if ( isRawXML )
                {
/*                    kdDebug() << "RawXML" << endl;*/
                    f.open( IO_ReadOnly );
                    if ( !m_reportEngine -> setReportTemplate( &f ) )
                        KMessageBox::sorry( 0, i18n( "Invalid template file: %1" ).arg( localtpl ) );
                    else
                    {
                        m_templateOk = true;
/*                        kdDebug() << "Setting m_templateOk" << endl;*/
                    }
                    f.close();
                }
                else
                {
                    KoStore *tmpStore = KoStore::createStore( localtpl, KoStore::Read );
                    if ( tmpStore->open( "maindoc.xml" ) )
                    {
                        if ( !m_reportEngine -> setReportTemplate( tmpStore->device() ) )
                            KMessageBox::sorry( 0, i18n( "%1 is not a valid Kugar Designer template file." ).arg( localtpl ) );
                        else
                            m_templateOk = true;
                        tmpStore->close();
                    }
                    else
                        KMessageBox::sorry( 0, i18n( "%1 is not a valid Kugar Designer template file." ).arg( localtpl ) );

                    delete tmpStore;
                }

            }
            else
            {
                f.close();
                KMessageBox::sorry( 0, i18n( "Couldn't read the beginning of the template file: %1" ).arg( localtpl ) );
            }

        }
        else
            KMessageBox::sorry( 0, i18n( "Unable to open template file: %1" ).arg( localtpl ) );

        if ( isTemp )
            TDEIO::NetAccess::removeTempFile( localtpl );
    }
}

#include "kugar_part.moc"