Skip to content

Commit

Permalink
filetransferplugin: Present save popup once
Browse files Browse the repository at this point in the history
The plugin's save command created a window for every successful or
erroneous attempt to save individual files selected.
This is tedious for more than a few files.

Instead, collect errors and present a single pop-up window.

In the future, an alternative might be to configure a selection between
this behaviour and a brief message in the status bar.

Fixes a bug where doFLDA was erroneously called on messages ending a
transfer ("FLFI..."), causing completed status for a file to be
incorrect and display inconsistently.

Adds FiletransferPlugin::doFLFI which emits a new signal so that
Form::finishfile_slot can record completion properly and auto-save if
needed. updatefile_slot now tracks received packages and completion is
ONLY recorded if a FLFI message is present.

Signed-off-by: Michael Foukarakis <[email protected]>
  • Loading branch information
mfukar committed Nov 22, 2023
1 parent b293a1f commit 55c7e87
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 114 deletions.
6 changes: 2 additions & 4 deletions plugin/filetransferplugin/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ File::File(QDltFile *qfile,QTreeWidgetItem *parent):QTreeWidgetItem(parent)
receivedPackages = 0;
fileSerialNumber = 0;
packages = 0;
receivedPackages = 0;
sizeInBytes = 0;
buffer = 0;
dltFileIndex = NULL;
Expand Down Expand Up @@ -76,7 +75,7 @@ QString File::getFileSerialNumber(){

return str;
}
unsigned int File:: getPackages(){
unsigned int File::getPackages(){
return packages;
}
unsigned int File::getReceivedPackages(){
Expand All @@ -90,7 +89,6 @@ unsigned int File::getBufferSize(){
}



void File::setFilename(QString f){
filenameWithPath = f;
this->setText(COLUMN_FILENAME, filenameWithPath);
Expand Down Expand Up @@ -161,7 +159,7 @@ bool File::isComplete(){

void File::setQFileIndexForPackage(QString packageNumber, int index){
int i = packageNumber.toInt();
dltFileIndex->insert(i-1,index);
dltFileIndex->insert(i-1, index);
increaseReceivedPackages();
}

Expand Down
17 changes: 10 additions & 7 deletions plugin/filetransferplugin/filetransferplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ void FiletransferPlugin::updateFiletransfer(int index, QDltMsg &msg)
msg.getArgument(PROTOCOL_FLFI_ENDFLAG,msgLastArgument);
if(msgLastArgument.toString().compare(config.getFlfiTag()) == 0)
{
doFLDA(index,&msg);
doFLFI(&msg);
}
return;
}
Expand Down Expand Up @@ -363,16 +363,19 @@ void FiletransferPlugin::doFLDA(int index,QDltMsg *msg)
msg->getArgument(PROTOCOL_FLDA_FILEID,argument);
msg->getArgument(PROTOCOL_FLDA_PACKAGENR,packageNumber);

emit(form->handleupdate_signal(argument.toString(),packageNumber.toString(), index ));
emit(form->handleupdate_signal(argument.toString(), packageNumber.toString(), index ));
return;
}

void FiletransferPlugin::doFLIF(QDltMsg *msg)
{
void FiletransferPlugin::doFLIF(QDltMsg *msg) {
Q_UNUSED(msg);
//not implemented yet. Would handle extended file information: file serialnumber, name ,size, creation date
}

//empty.
//not implemented yet. Would handle extended file information: file serialnumber, name ,size, creation date, number of packages
void FiletransferPlugin::doFLFI(QDltMsg *msg)
{
QDltArgument id;
msg->getArgument(PROTOCOL_FLFI_FILEID, id);
emit(form->handlefinish_signal(id.toString()));
}

void FiletransferPlugin::doFLER(QDltMsg *msg)
Expand Down
1 change: 1 addition & 0 deletions plugin/filetransferplugin/filetransferplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class FiletransferPlugin : public QObject, QDLTPluginInterface, QDltPluginViewer
void doFLST(QDltMsg *msg); // file transfer start
void doFLDA(int index, QDltMsg *msg); // file transfer update
void doFLIF(QDltMsg *msg);
void doFLFI(QDltMsg *msg);
void doFLER(QDltMsg *msg); // file transfer error handling

Configuration config;
Expand Down
200 changes: 97 additions & 103 deletions plugin/filetransferplugin/form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Form::Form(QWidget *parent) :

connect(this, SIGNAL( additem_signal(File *)), this, SLOT(additem_slot(File *) ) );
connect(this, SIGNAL( handleupdate_signal(QString, QString, int)), this, SLOT(updatefile_slot(QString,QString,int) ) );
connect(this, SIGNAL( handlefinish_signal(QString)), this, SLOT(finishfile_slot(QString) ) );
connect(this, SIGNAL( export_signal(QDir, QString *, bool * )), this, SLOT(export_slot(QDir, QString *, bool * ) ) );
connect(this, SIGNAL( handle_errorsignal(QString,QString,QString,QString)), this, SLOT(error_slot(QString,QString,QString,QString) ) );
}
Expand Down Expand Up @@ -175,99 +176,89 @@ void Form::on_saveButton_clicked()

void Form::savetofile()
{
QString path = QFileDialog::getExistingDirectory(this, tr("Save file to directory"), QDir::currentPath(), QFileDialog::DontResolveSymlinks);
if(path != nullptr)
QString path = QFileDialog::getExistingDirectory(this, tr("Save file to directory"),
QDir::currentPath(),
QFileDialog::DontResolveSymlinks);
if(path == nullptr)
{
QDir::setCurrent(path); // because we want to keep the last selected path to offer next time again
QTreeWidgetItemIterator it(ui->treeWidget,QTreeWidgetItemIterator::NoChildren );
QString text;
QString infoText;
QString detailedText;
QMessageBox msgBox;
QString absolutePath;
QString FiletoSave;
File *tmpfile;
unsigned int number_of_saved_files=0;
msgBox.setWindowTitle("Filetransfer Plugin");
bool errorOccured = true;

while (*it)
qDebug()<< "File save cancelled because of invalid path parameter passed to savetofile()";
return;
}
QDir::setCurrent(path); // because we want to keep the last selected path to offer next time again
unsigned int number_of_saved_files = 0;
unsigned int number_of_files_not_saved = 0;
unsigned int number_of_invalid_handles = 0;
QString pathnamesSavedText;
QString pathnamesNotSavedText;
QString invalidHandles;
QString incompleteFiles;
bool triedToSaveIncomplete = false;

for (QTreeWidgetItemIterator it(ui->treeWidget,QTreeWidgetItemIterator::NoChildren); *it ; ++it)
{
File * tmpfile = dynamic_cast<File*>(*it);
if (tmpfile == NULL)
{
tmpfile = dynamic_cast<File*>(*it);
if (tmpfile == NULL)
{
qDebug()<< "File handle is NULL";
msgBox.setIcon(QMessageBox::Critical);
text += ("Not vaild file selected");
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
else if ( !tmpfile->isComplete() && (tmpfile->checkState(COLUMN_CHECK) == Qt::Checked))
++number_of_invalid_handles;
invalidHandles += tmpfile->getFilename() + "\n";
}
else if ( !tmpfile->isComplete() && tmpfile->checkState(COLUMN_CHECK) == Qt::Checked)
{
triedToSaveIncomplete = true;
incompleteFiles += tmpfile->getFilename() + "\n";
}
else if (tmpfile->checkState(COLUMN_CHECK) == Qt::Checked)
{
QString absolutePath = path + "//" + tmpfile->getFilename();
if(!tmpfile->saveFile(absolutePath) )
{
// if it is not selected we do not care ...
FiletoSave = tmpfile->getFilename();
qDebug()<< "File is not complete" << FiletoSave;
msgBox.setIcon(QMessageBox::Critical);
text += ("File is not complete: ");
text += FiletoSave;
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
++number_of_files_not_saved;
pathnamesNotSavedText += tmpfile->getFilename() + "\n";
}
else if (tmpfile->checkState(COLUMN_CHECK) == Qt::Checked)
{
FiletoSave = tmpfile->getFilename();
absolutePath = path+"//"+FiletoSave;
if(!tmpfile->saveFile(absolutePath) )
{
qDebug()<< "File save incomplete: " << FiletoSave;
text = ("File save incomplete");
infoText ="The selected file "+FiletoSave+ " was not saved to "+path+".\n";
detailedText = tmpfile->getFilenameOnTarget() + "\n";
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(text);
msgBox.setInformativeText(infoText);
msgBox.setDetailedText(detailedText);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
else
{
qDebug()<< "Successfully saved " << FiletoSave;
number_of_saved_files++;
errorOccured = false; // we indicate that at least one file save was successful
}
}
else // file is not marked
else
{
FiletoSave = tmpfile->getFilename();
qDebug()<< "File " << FiletoSave << " not marked for save";
++number_of_saved_files;
pathnamesSavedText += tmpfile->getFilename() + "\n";
}
++it;
} // while

if(false == errorOccured ) // no error
}
else // file is not marked
{
msgBox.setIcon(QMessageBox::Information);
text = ("Save successful");
infoText = QString("%1 selected files were saved to %2\n").arg(number_of_saved_files).arg(path);
msgBox.setText(text);
msgBox.setInformativeText(infoText);
msgBox.setDetailedText("");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
qDebug()<< "File " << tmpfile->getFilename() << " not marked for save";
}
}// if path
else
{
qDebug()<< "Filesave canceled";
}
return; // void, but anyway ...

/* Done or not, present popup here only once: */
QString text;
QString infoText;
QString detailedText;
QMessageBox msgBox;
msgBox.setWindowTitle("Filetransfer Plugin");
msgBox.setIcon(QMessageBox::Information);
msgBox.setText("Save successful");
infoText += "Destination path: " + path + "\n";
if (number_of_files_not_saved || triedToSaveIncomplete || number_of_invalid_handles) {
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText("Errors were encountered");
if (number_of_files_not_saved) {
detailedText += "These files were not saved to " + path + ":\n" + incompleteFiles + "\n";
}
if (triedToSaveIncomplete) {
detailedText += "These files were incomplete and were not saved:\n" + pathnamesNotSavedText + "\n";
}
if (number_of_invalid_handles) {
detailedText += "These files had invalid handles and were not saved:\n" + invalidHandles + "\n";
}
}
if (number_of_saved_files) {
infoText += QString("%1 selected files were saved to %2\n").arg(number_of_saved_files).arg(path);
}

msgBox.setInformativeText(infoText);
msgBox.setDetailedText(detailedText);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}


Expand Down Expand Up @@ -408,34 +399,37 @@ void Form::updatefile_slot(QString filestring, QString packetnumber, int index)
else
{
File *file = (File*)result.at(0);
if(false == file->isComplete())
if (false == file->isComplete())
{
file->setQFileIndexForPackage(packetnumber,index);
file->setQFileIndexForPackage(packetnumber, index);
}
else
{
qDebug() << "Received file" << file->getFilename();
file->setComplete();
if ( true == autosave )
{
QString path = savepath +"//"+ file->getFilename();
if ( false == file->saveFile(path) )
{
qDebug() << "ERROR saving" << path << __LINE__ << __FILE__;
}
else
{
qDebug() << "Auto - saved" << path;
}
}
}
}

void Form::finishfile_slot (QString fileid) {
QList<QTreeWidgetItem *> result = getTreeWidget()->findItems(fileid, Qt::MatchExactly | Qt::MatchRecursive, COLUMN_FILEID);
if (result.isEmpty()) {
return;
}
File *file = (File *) result.at(0);
if (!file->isComplete()) {
return;
}

file->setComplete();
if (autosave) {
QString path = savepath + "//" + file->getFilename();
if ( false == file->saveFile(path) ) {
qDebug() << "Unable to save file with ID " << fileid << " at " << path << " " << __LINE__ << __FILE__;
} else {
qDebug() << fileid << " auto-saved at " << path;
}
}
}


void Form::additem_slot(File *f)
{

QList<QTreeWidgetItem *> result = getTreeWidget()->findItems(f->getFileSerialNumber(),Qt::MatchExactly | Qt::MatchRecursive,COLUMN_FILEID);
if(true == result.isEmpty())
{
Expand Down
2 changes: 2 additions & 0 deletions plugin/filetransferplugin/form.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Form : public QWidget
void additem_signal(File *f);
void export_signal(QDir dir, QString *errorText, bool *success);
void handleupdate_signal(QString filestring, QString packetnumber, int index);
void handlefinish_signal (QString fileid);
void err_signal(QDltMsg *msg);
void handle_errorsignal(QString filesname, QString errorCode1, QString errorCode2, QString time);

Expand All @@ -70,6 +71,7 @@ private slots:
void on_saveRightButtonClicked();
void additem_slot(File *f);
void updatefile_slot(QString filestring, QString packetnumber, int index);
void finishfile_slot (QString fileid);
void export_slot(QDir dir, QString *errorText, bool *success);
void error_slot(QString filesname, QString errorCode1, QString errorCode2, QString time);
};
Expand Down

0 comments on commit 55c7e87

Please sign in to comment.