File: importdialog.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (155 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (2)
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
/*
 * importdialog.cpp
 *
 * (c) 2003-2004,2008-2009 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * 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.
 */

/** @file importdialog.cpp
 * Source file for ImportDialog
 */

#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QInputDialog>
#include <QMessageBox>
#include <QSettings>
#include <QStringList>
#include "importdialog.h"
#include "csverror.h"
#include "database.h"
#include "importutils.h"
#include "qqutil/qqmenuhelper.h"

/**
 * Constructor.
 *
 * @param sourceType The type of file to open and import from
 * @param parent The widget to use as the parent of any dialogs launched
 */
ImportDialog::ImportDialog(DataSource sourceType, QWidget *parent)
  : QObject(), parentWidget(parent), source(sourceType)
{

}

/**
 * Launch the open file dialog (and a second dialog to select the text
 * encoding, if necessary).
 *
 * @return True if a file was was selected, false otherwise
 */
bool ImportDialog::exec()
{
    QString filter;
    if (source == CSV || source == OptionList) {
        filter = QString::null;
    }
    else if (source == MobileDB) {
        filter = tr("MobileDB files") + " (*.pdb)";
    }
    else if (source == XML) {
        filter = tr("XML files") + " (*.xml)";
    }
    else {
        filter = tr("Images") + " (*.jpg *.jpeg *.png)";
    }
    QSettings settings;
    QString lastDir = QQMenuHelper::getLastDir(&settings);
    QString file = QFileDialog::getOpenFileName(parentWidget,
                 tr("Choose a file"), lastDir, filter);
    if (file.isEmpty()) {
        return false;
    }
    else {
        QFileInfo info(file);
        settings.setValue("Files/LastDir", info.absolutePath());
        path = info.absoluteFilePath();
    }
    if (source == CSV || source == OptionList) {
        QStringList encodings;
        encodings.append("UTF-8");
        encodings.append("Latin-1");
        bool ok;
        encoding = QInputDialog::getItem(parentWidget, tr("Import"),
                                         tr("Text encoding") + ":",
                                         encodings, 0, false, &ok);
        if (!ok) {
            return false;
        }
    }
    return true;
}

/**
 * Import data from the selected file.  Should only be called if exec()
 * returned true.
 *
 * @param db The database to import into
 * @return True if data was imported, false otherwise
 */
bool ImportDialog::import(Database *db)
{
    QString error = "";
    QString data = "";
    if (source == CSV) {
        QStringList result = db->importFromCSV(path, encoding);
        int count = result.count();
        if (count > 0) {
            error = result[0];
        }
        if (count > 1) {
            data = result[1];
        }
    }
    else if (source == OptionList) {
        ImportUtils utils;
        error = utils.importTextLines(path, encoding, &options);
    }
    else if (source == MobileDB) {
        ImportUtils utils;
        error = utils.importMobileDB(path, db);
    }
    else if (source == XML) {
        ImportUtils utils;
        error = utils.importXML(path, db);
    }
    if (!error.isEmpty()) {
        if (data.isEmpty()) {
            QMessageBox::warning(0, qApp->applicationName(), error);
        }
        else {
            CSVErrorDialog dialog(error, data, 0);
            dialog.exec();
        }
        return false;
    }
    else {
        return true;
    }
}

/**
 * Get the list of enumeration options that were imported, if applicable.
 *
 * @return A list of enumeration option values
 */
QStringList ImportDialog::getOptions()
{
    return options;
}

/**
 * Get the absolute path of the file that was imported, if any.
 *
 * @return The imported file's absolute path
 */
QString ImportDialog::getPath()
{
    return path;
}