| 1 | #include "filebeditor.h" |
| 2 | #include "ui_filebeditor.h" |
| 3 | #include <QMessageBox> |
| 4 | #include <QFileDialog> |
| 5 | #include <QTemporaryFile> |
| 6 | #include <QProcess> |
| 7 | #include <QDebug> |
| 8 | |
| 9 | |
| 10 | FileBEditor::FileBEditor(QWidget *parent) : |
| 11 | QWidget(parent), |
| 12 | ui(new Ui::FileBEditor) |
| 13 | { |
| 14 | ui->setupUi(this); |
| 15 | connect(ui->saveAsButton, SIGNAL(released()), this, SLOT(saveAs())); |
| 16 | connect(ui->addButton, SIGNAL(released()), this, SLOT(add())); |
| 17 | connect(ui->removeButton, SIGNAL(released()), this, SLOT(remove())); |
| 18 | } |
| 19 | |
| 20 | FileBEditor::~FileBEditor() |
| 21 | { |
| 22 | delete ui; |
| 23 | } |
| 24 | |
| 25 | void FileBEditor::appendText(QString &text) |
| 26 | { |
| 27 | Q_ASSERT(text.contains("<CsFileB filename=") |
| 28 | && text.contains("</CsFileB>") ); |
| 29 | FileB file; |
| 30 | int index, len; |
| 31 | index = text.indexOf("<CsFileB filename=") + 18; |
| 32 | len = text.indexOf(">",index) - index; |
| 33 | QString fullName = text.mid(index, len).remove("\""); |
| 34 | text = text.right(text.size() - index - len - 1); // Now remove the tag before reusing the index variable |
| 35 | if (text.startsWith("\n")) { |
| 36 | text.remove(0,1); |
| 37 | } |
| 38 | index = fullName.lastIndexOf("/") + 1; |
| 39 | if (index == -1) { |
| 40 | index = 0; |
| 41 | } |
| 42 | file.fileName = fullName.mid(index); |
| 43 | file.path = fullName.left(index); |
| 44 | text.remove("</CsFileB>"); |
| 45 | text = text.trimmed(); // Remove initial and final line breaks |
| 46 | file.encData = text; |
| 47 | m_files.append(file); |
| 48 | ui->fileListWidget->addItem(file.fileName); |
| 49 | } |
| 50 | |
| 51 | QString FileBEditor::toPlainText(bool useQuotes) |
| 52 | { |
| 53 | QString text; |
| 54 | foreach(FileB file, m_files) { |
| 55 | if (useQuotes) { |
| 56 | text += "<CsFileB filename=\"" + file.path + file.fileName + "\">\n"; |
| 57 | } |
| 58 | else { |
| 59 | text += "<CsFileB filename=" + file.path + file.fileName + ">\n"; |
| 60 | } |
| 61 | text += file.encData + "\n"; |
| 62 | text += "</CsFileB>\n"; |
| 63 | } |
| 64 | return text; |
| 65 | } |
| 66 | |
| 67 | void FileBEditor::clear() |
| 68 | { |
| 69 | ui->fileListWidget->clear(); |
| 70 | m_files.clear(); |
| 71 | } |
| 72 | |
| 73 | void FileBEditor::saveAs() |
| 74 | { |
| 75 | QList<QListWidgetItem *> items = ui->fileListWidget->selectedItems(); |
| 76 | foreach(QListWidgetItem * item, items) { |
| 77 | QString fileName = |
| 78 | QFileDialog::getSaveFileName(this, |
| 79 | tr("Select destination file name for file %1", |
| 80 | "For saving binary encoded embedded file").arg(item->text()), |
| 81 | item->text() |
| 82 | ); |
| 83 | int row = ui->fileListWidget->row(item); |
| 84 | QFile file(fileName); |
| 85 | if (!file.open(QIODevice::WriteOnly)) { |
| 86 | QMessageBox::warning(this, tr("Saving embedded file"), |
| 87 | tr("Error saving embedded file. Aborting.")); |
| 88 | return; |
| 89 | } |
| 90 | QByteArray data = QByteArray::fromBase64(m_files[row].encData.toLocal8Bit()); |
| 91 | QDataStream s(&file); |
| 92 | s.writeRawData(data, data.size()); |
| 93 | file.close(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void FileBEditor::add() |
| 98 | { |
| 99 | QStringList files = QFileDialog::getOpenFileNames(this, tr("Select files to embed")); |
| 100 | foreach (QString file, files) { |
| 101 | QStringList args; |
| 102 | args << file; |
| 103 | QProcess *encProcess = new QProcess(); |
| 104 | #ifdef Q_OS_MACX |
| 105 | encProcess->start("/usr/local/bin/csb64enc", args); |
| 106 | #else |
| 107 | encProcess->start("csb64enc", args); |
| 108 | #endif |
| 109 | bool finished = encProcess->waitForFinished(10000); |
| 110 | if (!finished) { |
| 111 | qDebug() << "FileBEditor::add() Error: " << encProcess->errorString(); |
| 112 | QMessageBox::warning(this, tr("Encoding file"), |
| 113 | tr("Error encoding file. Aborting.")); |
| 114 | return; |
| 115 | } |
| 116 | QString encText = QString(encProcess->readAllStandardOutput()); |
| 117 | appendText(encText); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void FileBEditor::remove() |
| 122 | { |
| 123 | QList<QListWidgetItem *> items = ui->fileListWidget->selectedItems(); |
| 124 | foreach(QListWidgetItem * item, items) { |
| 125 | int row = ui->fileListWidget->row(item); |
| 126 | m_files.remove(row); |
| 127 | item = ui->fileListWidget->takeItem(row); |
| 128 | delete item; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void FileBEditor::clearUndoRedoStacks() |
| 133 | { |
| 134 | } |