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
|
/**
* @licence app begin@
* Copyright (C) 2011-2012 BMW AG
*
* This file is part of COVESA Project Dlt Viewer.
*
* Contributions are licensed to the COVESA Alliance under one or more
* Contribution License Agreements.
*
* \copyright
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* \file file.cpp
* For further information see http://www.covesa.global/.
* @licence end@
*/
#include "file.h"
#include <iostream>
#include <fstream>
#include <QDebug>
using namespace std;
File::File():QTreeWidgetItem()
{
fileSerialNumber = 0;
packages = 0;
receivedPackages = 0;
sizeInBytes = 0;
buffer = 0;
dltFileIndex = NULL;
dltFile = NULL;
fileData = NULL;
}
File::File(QDltFile *qfile,QTreeWidgetItem *parent):QTreeWidgetItem(parent)
{
receivedPackages = 0;
fileSerialNumber = 0;
packages = 0;
receivedPackages = 0;
sizeInBytes = 0;
buffer = 0;
dltFileIndex = NULL;
dltFile = qfile;
fileData = NULL;
this->setText(COLUMN_STATUS, "Incomplete");
this->setTextColor(COLUMN_STATUS,Qt::white);
this->setBackgroundColor(COLUMN_STATUS,Qt::red);
this->setText(COLUMN_RECPACKAGES, "0");
}
File::~File()
{
}
QString File::getFilename(){
QStringList pathList = filenameWithPath.split("/");
return pathList.last();
}
QString File::getFileCreationDate(){
return fileCreationDate;
}
QString File::getFilenameOnTarget(){
return filenameWithPath;
}
QString File::getFileSerialNumber(){
QString str;
str.append(QString("%1").arg(fileSerialNumber));
return str;
}
unsigned int File:: getPackages(){
return packages;
}
unsigned int File::getReceivedPackages(){
return receivedPackages;
}
unsigned int File::getSizeInBytes(){
return sizeInBytes;
}
unsigned int File::getBufferSize(){
return buffer;
}
void File::setFilename(QString f){
filenameWithPath = f;
this->setText(COLUMN_FILENAME, filenameWithPath);
}
void File::setFileCreationDate(QString f){
fileCreationDate = f;
this->setText(COLUMN_FILEDATE, fileCreationDate);
}
void File::setFileSerialNumber(QString s)
{
fileSerialNumber = s.toUInt();
this->setText(COLUMN_FILEID, s);
}
void File::setPackages(QString p){
packages = p.toUInt();
dltFileIndex = new QList<int>[packages];
this->setText(COLUMN_PACKAGES, p);
}
void File::increaseReceivedPackages(){
receivedPackages++;
QString str;
str.append(QString("%1").arg(receivedPackages));
this->setText(COLUMN_RECPACKAGES, str);
}
void File::setSizeInBytes(QString s){
sizeInBytes = s.toUInt();
this->setText(COLUMN_SIZE, s);
}
void File::setBuffersize(QString b){
buffer = b.toUInt();
this->setText(COLUMN_BUFFERSIZE, b);
}
void File::setComplete(){
this->setText(COLUMN_STATUS, "Complete");
this->setBackgroundColor(COLUMN_STATUS,Qt::green);
this->setTextColor(COLUMN_STATUS,Qt::black);
}
void File::errorHappens(QString filename, QString errorCode1, QString errorCode2, QString time){
setFilename(filename);
fileSerialNumber = 1;
packages = 2;
receivedPackages = 3;
sizeInBytes = 4;
buffer = 5;
QString str = errorCode1+", "+errorCode2;
this->setText(COLUMN_FILEID,str);
this->setText(COLUMN_FILEDATE,time);
this->setText(COLUMN_STATUS, "ERROR");
this->setTextColor(COLUMN_STATUS,Qt::white);
this->setBackgroundColor(COLUMN_STATUS,Qt::red);
}
bool File::isComplete(){
return receivedPackages == packages;
}
void File::setQFileIndexForPackage(QString packageNumber, int index){
int i = packageNumber.toInt();
dltFileIndex->insert(i-1,index);
increaseReceivedPackages();
}
bool File::saveFile(QString newFile){
//QString newFile = directory.append("/").append(getFilename());
if(QFile::exists(newFile)){
if(!QFile::remove(newFile)){
return false;
}
}
QByteArray *completeFileData = getFileData();
QFile file(newFile);
if (!file.open(QIODevice::WriteOnly)){
freeFile();
return false;
}
QDataStream stream( &file );
int writtenBytes = stream.writeRawData(*completeFileData,completeFileData->size());
file.close();
freeFile();
if((unsigned int)writtenBytes != sizeInBytes){
return false;
}
return true;
}
void File::freeFile(){
delete fileData;
}
QByteArray* File::getFileData(){
QDltMsg msg;
QByteArray msgBuffer;
QDltArgument data;
fileData = new QByteArray();
for(unsigned int i=0; i<packages;i++){
int qfileIdx = dltFileIndex->value(i);
msgBuffer = dltFile->getMsg(qfileIdx);
msg.setMsg(msgBuffer);
msg.getArgument(PROTOCOL_FLDA_DATA,data);
fileData->append(data.getData());
}
return fileData;
}
|