File: c2bExportDialog.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (117 lines) | stat: -rw-r--r-- 4,758 bytes parent folder | download
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
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bExportDialog.h"

#include "c2b.h"
#include "c2bFileDialog.h"
#include "c2bSettings.h"
#include "c2bUtils.h"

#include <QMessageBox>
#include <QPushButton>


c2bExportDialog::c2bExportDialog(const QString& fn, const QString& contents, const QStringList& keys, QWidget* parentw) :
    QDialog(parentw)
{
    _ed.bibtex_fn = fn;
    if (contents.isEmpty())
        _ed.bibtex_contents = c2bUtils::fileToString(fn);
    else
        _ed.bibtex_contents = contents;
    _ed.bibtex_keys = keys;
    ui.setupUi(this);
    setWindowTitle(tr("Export Entries - cb2Bib"));
    QPushButton* buttonExport = new QPushButton(tr("Export"));
    ui.buttonBox->addButton(buttonExport, QDialogButtonBox::ActionRole);
    buttonExport->setAutoDefault(true);
    buttonExport->setDefault(true);
    connect(buttonExport, SIGNAL(clicked()), this, SLOT(_export()));

    settings = c2bSettingsP;
    ui.ExportFile->setText(settings->value("c2bExportDialog/ExportFile", QString()).toString());
    ui.DocumentsDirectory->setText(settings->value("c2bExportDialog/DocumentsDirectory", QString()).toString());
    ui.CopyDocuments->setChecked(settings->value("c2bExportDialog/CopyDocuments").toBool());
    ui.RelativeDocumentDirectory->setChecked(settings->value("c2bExportDialog/RelativeDocumentDirectory").toBool());
    ui.DocumentsDirectoryBox->setEnabled(ui.CopyDocuments->isChecked());

    connect(ui.ExportFileDir, SIGNAL(clicked()), this, SLOT(getExportFile()));
    connect(ui.DocumentsDirectoryDir, SIGNAL(clicked()), this, SLOT(getDocumentsDirectory()));
    connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
}

c2bExportDialog::~c2bExportDialog()
{
    if (result() == QDialog::Accepted)
    {
        settings->setValue("c2bExportDialog/CopyDocuments", ui.CopyDocuments->isChecked());
        settings->setValue("c2bExportDialog/DocumentsDirectory", ui.DocumentsDirectory->text());
        settings->setValue("c2bExportDialog/ExportFile", ui.ExportFile->text());
        settings->setValue("c2bExportDialog/RelativeDocumentDirectory", ui.RelativeDocumentDirectory->isChecked());
    }
}


void c2bExportDialog::_export()
{
    _ed.copy_documents = ui.CopyDocuments->isChecked();
    _ed.documents_dir = ui.DocumentsDirectory->text();
    _ed.export_fn = ui.ExportFile->text();
    _ed.relative_dir = ui.RelativeDocumentDirectory->isChecked();

    // Check files
    if (QFileInfo(_ed.export_fn).exists())
    {
        const int button(QMessageBox::warning(this, tr("Warning - cb2Bib"),
                                              tr("File %1 already exists.\nDo you want to replace it?").arg(_ed.export_fn),
                                              QMessageBox::Yes | QMessageBox::No));
        if (button == QMessageBox::No)
            return;
    }
    if (_ed.copy_documents)
    {
        const QString ddname(c2bUtils::documentAbsoluteName(_ed.relative_dir, _ed.export_fn, _ed.documents_dir, QString()) + QDir::separator());
        QDir doc_dir(ddname);
        if (!doc_dir.exists())
            if (!doc_dir.mkpath(ddname))
            {
                QMessageBox::warning(this, tr("Warning - cb2Bib"),
                                     tr("Unable to create directory %1.").arg(ddname), QMessageBox::Ok);
                return;
            }
    }
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    c2bExport e(this);
    e.exportEntries(_ed);
    QApplication::restoreOverrideCursor();
    accept();
}

void c2bExportDialog::getExportFile()
{
    const QString fn(ui.ExportFile->text());
    const QString new_fn(c2bFileDialog::getFilename(this, tr("Select export filename"), fn, "BibTeX (*.bib)"));
    if (!new_fn.isEmpty())
        ui.ExportFile->setText(new_fn);
}

void c2bExportDialog::getDocumentsDirectory()
{
    const QString dir(c2bUtils::documentAbsoluteName(ui.RelativeDocumentDirectory->isChecked(), ui.ExportFile->text(),
                      ui.DocumentsDirectory->text(), QString()));
    QString new_dir(c2bFileDialog::getExistingDirectory(this, tr("Select documents directory"), dir));
    if (!new_dir.isEmpty())
    {
        new_dir = c2bUtils::documentDirName(ui.RelativeDocumentDirectory->isChecked(), ui.ExportFile->text(), new_dir);
        ui.DocumentsDirectory->setText(new_dir);
    }
}

void c2bExportDialog::help()
{
    c2bUtils::displayHelp("http://www.molspaces.com/d_cb2bib-c2beditor.php#export_entries");
}