You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmymoney/kmymoney2/plugins/ofximport/dialogs/kofxdirectconnectdlg.cpp

224 lines
6.1 KiB

/***************************************************************************
kofxdirectconnectdlg.cpp
-------------------
begin : Sat Nov 13 2004
copyright : (C) 2002 by Ace Jones
email : acejones@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
// ----------------------------------------------------------------------------
// QT Includes
#include <qlabel.h>
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <kurl.h>
#include <kio/job.h>
#include <kio/jobclasses.h>
#include <kdebug.h>
#include <ktempfile.h>
#include <kprogress.h>
#include <kmessagebox.h>
#include <klocale.h>
// ----------------------------------------------------------------------------
// Project Includes
#include <kmymoney/mymoneyinstitution.h>
#include <kmymoney/mymoneyfile.h>
#include "mymoneyofxconnector.h"
#include "kofxdirectconnectdlg.h"
class KOfxDirectConnectDlg::Private
{
public:
QFile m_fpTrace;
};
KOfxDirectConnectDlg::KOfxDirectConnectDlg(const MyMoneyAccount& account, QWidget *parent, const char *name) :
KOfxDirectConnectDlgDecl(parent, name),
d(new Private),
m_tmpfile(NULL),
m_connector(account),
m_job(NULL)
{
}
KOfxDirectConnectDlg::~KOfxDirectConnectDlg()
{
if(d->m_fpTrace.isOpen()) {
d->m_fpTrace.close();
}
delete m_tmpfile;
delete d;
}
void KOfxDirectConnectDlg::init(void)
{
show();
QByteArray request = m_connector.statementRequest();
// For debugging, dump out the request
#if 0
QFile g( "request.ofx" );
g.open( IO_WriteOnly );
QTextStream(&g) << m_connector.url() << "\n" << QString(request);
g.close();
#endif
QDir homeDir(QDir::home());
if(homeDir.exists("ofxlog.txt")) {
d->m_fpTrace.setName(QString("%1/ofxlog.txt").arg(QDir::homeDirPath()));
d->m_fpTrace.open(IO_WriteOnly | IO_Append);
}
m_job = KIO::http_post(
m_connector.url(),
request,
true
);
if(d->m_fpTrace.isOpen()) {
QByteArray data = m_connector.url().utf8();
d->m_fpTrace.writeBlock("url: ", 5);
d->m_fpTrace.writeBlock(data, strlen(data));
d->m_fpTrace.writeBlock("\n", 1);
d->m_fpTrace.writeBlock("request:\n", 9);
d->m_fpTrace.writeBlock(request, request.size());
d->m_fpTrace.writeBlock("\n", 1);
d->m_fpTrace.writeBlock("response:\n", 10);
}
m_job->addMetaData("content-type", "Content-type: application/x-ofx" );
connect(m_job,SIGNAL(result(KIO::Job*)),this,SLOT(slotOfxFinished(KIO::Job*)));
connect(m_job,SIGNAL(data(KIO::Job*, const QByteArray&)),this,SLOT(slotOfxData(KIO::Job*,const QByteArray&)));
connect(m_job,SIGNAL(connected(KIO::Job*)),this,SLOT(slotOfxConnected(KIO::Job*)));
setStatus(QString("Contacting %1...").arg(m_connector.url()));
kProgress1->setTotalSteps(3);
kProgress1->setProgress(1);
}
void KOfxDirectConnectDlg::setStatus(const QString& _status)
{
textLabel1->setText(_status);
kdDebug(2) << "STATUS: " << _status << endl;
}
void KOfxDirectConnectDlg::setDetails(const QString& _details)
{
kdDebug(2) << "DETAILS: " << _details << endl;
}
void KOfxDirectConnectDlg::slotOfxConnected(KIO::Job*)
{
if ( m_tmpfile )
{
// throw new MYMONEYEXCEPTION(QString("Already connected, using %1.").arg(m_tmpfile->name()));
kdDebug(2) << "Already connected, using " << m_tmpfile->name() << endl;
delete m_tmpfile; //delete otherwise we mem leak
}
m_tmpfile = new KTempFile();
setStatus("Connection established, retrieving data...");
setDetails(QString("Downloading data to %1...").arg(m_tmpfile->name()));
kProgress1->advance(1);
}
void KOfxDirectConnectDlg::slotOfxData(KIO::Job*,const QByteArray& _ba)
{
if ( !m_tmpfile )
// throw new MYMONEYEXCEPTION("Not currently connected!!");
kdDebug(2) << "void ofxdcon::slotOfxData():: Not currently connected!" << endl;
*(m_tmpfile->textStream()) << QString(_ba);
if(d->m_fpTrace.isOpen()) {
d->m_fpTrace.writeBlock(_ba, _ba.size());
}
setDetails(QString("Got %1 bytes").arg(_ba.size()));
}
void KOfxDirectConnectDlg::slotOfxFinished(KIO::Job* /* e */)
{
kProgress1->advance(1);
setStatus("Completed.");
if(d->m_fpTrace.isOpen()) {
d->m_fpTrace.writeBlock("\nCompleted\n\n\n\n", 14);
}
int error = m_job->error();
if ( m_tmpfile )
{
m_tmpfile->close();
}
if ( error )
{
m_job->showErrorDialog();
}
else if ( m_job->isErrorPage() )
{
QString details;
QFile f( m_tmpfile->name() );
if ( f.open( IO_ReadOnly ) )
{
QTextStream stream( &f );
QString line;
while ( !stream.atEnd() ) {
details += stream.readLine(); // line of text excluding '\n'
}
f.close();
kdDebug(2) << "The HTTP request failed: " << details << endl;
}
KMessageBox::detailedSorry( this, i18n("The HTTP request failed."), details, i18n("Failed") );
}
else if ( m_tmpfile )
{
emit statementReady(m_tmpfile->name());
// TODO (Ace) unlink this file, when I'm sure this is all really working.
// in the meantime, I'll leave the file around to assist people in debugging.
// m_tmpfile->unlink();
}
delete m_tmpfile;
m_tmpfile = 0;
hide();
}
void KOfxDirectConnectDlg::reject(void)
{
m_job->kill();
if ( m_tmpfile )
{
m_tmpfile->close();
delete m_tmpfile;
m_tmpfile = NULL;
}
QDialog::reject();
}
#include "kofxdirectconnectdlg.moc"