summaryrefslogtreecommitdiffstats
path: root/kmymoney2/plugins/ofximport/dialogs/kofxdirectconnectdlg.cpp
blob: 3db8fbc463fca5053925b07d1b1aaa42c1dffbab (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
/***************************************************************************
                         kofxdirectconnectdlg.cpp
                             -------------------
    begin                : Sat Nov 13 2004
    copyright            : (C) 2002 by Ace Jones
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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"