summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/kimportdlg.cpp
blob: e8489d2f65d92b327c31077127ac995c8c305412 (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
/***************************************************************************
                          kimportdlg.cpp  -  description
                             -------------------
    begin                : Wed May 16 2001
    copyright            : (C) 2001 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@ctv.es>
                           Felix Rodriguez <frodriguez@mail.wesleyan.edu>
                           Thomas Baumgart <ipwizard@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.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Headers

#include <tqlineedit.h>
#include <tqtextstream.h>
#include <tqprogressbar.h>
#include <tqlabel.h>
#include <tqbuttongroup.h>
#include <tqpixmap.h>
#include <tqapplication.h>

// ----------------------------------------------------------------------------
// KDE Headers

#include <kglobalsettings.h>
#include <kpushbutton.h>
#include <kcombobox.h>
#include <kmessagebox.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kio/netaccess.h>
#include <kstandarddirs.h>

// ----------------------------------------------------------------------------
// Project Headers

#include "kimportdlg.h"
#include <kmymoney/mymoneyfile.h>
#include "mymoneyqifprofileeditor.h"
#include "../converter/mymoneyqifprofile.h"

KImportDlg::KImportDlg(TQWidget *tqparent, const char * name)
  : KImportDlgDecl(tqparent, name, TRUE)
{
  // Set all the last used options
  readConfig();

  loadProfiles(true);

  // load button icons
  m_qbuttonCancel->setGuiItem(KStdGuiItem::cancel());

  KIconLoader* il = KGlobal::iconLoader();
  KGuiItem okButtenItem( i18n( "&Import" ),
                      TQIconSet(il->loadIcon("fileimport", KIcon::Small, KIcon::SizeSmall)),
                      i18n("Start operation"),
                      i18n("Use this to start the import operation"));
  m_qbuttonOk->setGuiItem(okButtenItem);

  KGuiItem browseButtenItem( i18n( "&Browse..." ),
                      TQIconSet(il->loadIcon("fileopen", KIcon::Small, KIcon::SizeSmall)),
                      i18n("Select filename"),
                      i18n("Use this to select a filename to export to"));
  m_qbuttonBrowse->setGuiItem(browseButtenItem);

  KGuiItem newButtenItem( i18n( "&New..." ),
                      TQIconSet(il->loadIcon("filenew", KIcon::Small, KIcon::SizeSmall)),
                      i18n("Create a new profile"),
                      i18n("Use this to open the profile editor"));
  m_profileEditorButton->setGuiItem(newButtenItem);

  // connect the buttons to their functionality
  connect(m_qbuttonBrowse, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotBrowse() ) );
  connect(m_qbuttonOk, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotOkClicked()));
  connect(m_qbuttonCancel, TQT_SIGNAL(clicked()), this, TQT_SLOT(reject()));
  connect(m_profileEditorButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNewProfile()));

  // connect the change signals to the check slot and perform initial check
  connect(m_qlineeditFile, TQT_SIGNAL(textChanged(const TQString&)), this,
    TQT_SLOT(slotFileTextChanged(const TQString&)));

  // setup button enable status
  slotFileTextChanged(m_qlineeditFile->text());
}

KImportDlg::~KImportDlg()
{
}

void KImportDlg::slotBrowse()
{
  // determine what the browse prefix should be from the current profile

  MyMoneyQifProfile tmpprofile;
  tmpprofile.loadProfile("Profile-" + profile());

  KFileDialog dialog(KGlobalSettings::documentPath(),
                     i18n("%1|Import files\n%2|All files (*.*)").tqarg(tmpprofile.filterFileType()).tqarg("*"),
                     this, i18n("Import File..."), true);
  dialog.setMode(KFile::File | KFile::ExistingOnly);

  if(dialog.exec() == TQDialog::Accepted) {
#if KDE_IS_VERSION(3,4,0)
    m_qlineeditFile->setText(dialog.selectedURL().pathOrURL());
#else
    m_qlineeditFile->setText(dialog.selectedURL().prettyURL(0, KURL::StripFileProtocol));
#endif
  }
}

void KImportDlg::slotOkClicked()
{
  // Save the used options.
  writeConfig();
  // leave dialog directly
  accept();
}

void KImportDlg::readConfig(void)
{
  KConfig *kconfig = KGlobal::config();
  kconfig->setGroup("Last Use Settings");
  m_qlineeditFile->setText(kconfig->readEntry("KImportDlg_LastFile"));
}

void KImportDlg::writeConfig(void)
{
  KConfig *kconfig = KGlobal::config();
  kconfig->setGroup("Last Use Settings");
  kconfig->writeEntry("KImportDlg_LastFile", m_qlineeditFile->text());
  kconfig->writeEntry("KImportDlg_LastProfile", m_profileComboBox->currentText());
  kconfig->sync();
}

/** Make sure the text input is ok */
void KImportDlg::slotFileTextChanged(const TQString& text)
{
  if (!text.isEmpty() && KIO::NetAccess::exists(text, true, tqApp->mainWidget())) {
    // m_qcomboboxDateFormat->setEnabled(true);
    m_qbuttonOk->setEnabled(true);
    m_qlineeditFile->setText(text);
  } else {
    // m_qcomboboxDateFormat->setEnabled(false);
    m_qbuttonOk->setEnabled(false);
  }
}

void KImportDlg::slotNewProfile(void)
{
  MyMoneyQifProfileEditor* editor = new MyMoneyQifProfileEditor(true, this, "TQIF Profile Editor");

  if(editor->exec()) {
    m_profileComboBox->setCurrentText(editor->selectedProfile());
    loadProfiles();
  }

  delete editor;
}

void KImportDlg::slotSelectProfile(const TQString& profile)
{
  m_profileComboBox->setCurrentText(profile);
  loadProfiles();
}

void KImportDlg::loadProfiles(const bool selectLast)
{
  // Creating an editor object here makes sure that
  // we have at least the default profile available
  MyMoneyQifProfileEditor* edit = new MyMoneyQifProfileEditor(true, 0, 0);
  edit->slotOk();
  delete edit;

  TQString current = m_profileComboBox->currentText();

  m_profileComboBox->clear();

  TQStringList list;
  KConfig* config = KGlobal::config();
  config->setGroup("Profiles");

  list = config->readListEntry("profiles");
  list.sort();
  m_profileComboBox->insertStringList(list);

  if(selectLast == true) {
    config->setGroup("Last Use Settings");
    current = config->readEntry("KImportDlg_LastProfile");
  }

  m_profileComboBox->setCurrentItem(0);
  if(list.tqcontains(current) > 0) {
    m_profileComboBox->setCurrentText(current);
  }
}

void KImportDlg::addCategories(TQStringList& strList, const TQString& id, const TQString& leadIn) const
{
  MyMoneyFile *file = MyMoneyFile::instance();
  TQString name;

  MyMoneyAccount account = file->account(id);

  TQStringList accList = account.accountList();
  TQStringList::ConstIterator it_a;

  for(it_a = accList.begin(); it_a != accList.end(); ++it_a) {
    account = file->account(*it_a);
    strList << leadIn + account.name();
    addCategories(strList, *it_a, leadIn + account.name() + MyMoneyFile::AccountSeperator);
  }
}


#include "kimportdlg.moc"
// vim:cin:si:ai:et:ts=2:sw=2: