SilentEye 0.4.1

decodedialog.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye. If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "decodedialog.h"
00017 #include "controller.h"
00018 
00019 #include <imagemoduleinterface.h>
00020 #include <audiomoduleinterface.h>
00021 #include <videomoduleinterface.h>
00022 
00023 namespace SilentEye {
00024 
00025     DecodeDialog::DecodeDialog(QWidget* parent)
00026         : OptionDialog(parent)
00027     {
00028         this->setObjectName("DecodeDialog");
00029         m_logger->setClassName("DecodeDialog");
00030         m_checkSize = false;
00031 
00032         this->setWindowTitle("Decode message");
00033         this->setWindowIcon(QPixmap(QString::fromUtf8(":/icons/img/decode.png")));
00034 
00035         okButton->setText("Decode");
00036         iconLabel->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/decode.png")));
00037         msgLabel->setText("Decoded message");
00038         fileNameLabel->setText("");
00039         fileButton->setEnabled(false);
00040         fileRemoveButton->setVisible(false);
00041 
00042         outputWidget->setVisible(false);
00043         dataSizeWidget->setVisible(false);
00044         compressCheckBox->setText("Compressed data");
00045         cryptoCheckBox->setText("Encrypted data");
00046         msgTextEdit->setReadOnly(true);
00047         dataWidget->setVisible(false);
00048         orLabel->setVisible(false);
00049         updateDialogSize();
00050 
00051         connect(fileButton, SIGNAL(clicked(bool)), this, SLOT(selectFile()));
00052         //connect(fileButton, SIGNAL(released()), this, SLOT(releaseFileButton()));
00053     }
00054 
00055     DecodeDialog::~DecodeDialog()
00056     {
00057     }
00058 
00059     void DecodeDialog::showEvent(QShowEvent* event)
00060     {
00061         if(!event->spontaneous())
00062         {
00063             msgTextEdit->setText("");
00064             fileButton->setEnabled(false);
00065             cryptoPwd1LineEdit->setText("");
00066             cryptoPwd2LineEdit->setText("");
00067             checkCryptoPassword();
00068             formatChanged(formatComboBox->currentText());
00069             removeFile();
00070             if(!m_md.isNull())
00071                 this->setWindowTitle("Decode message: " + m_md->filePath());
00072             dataWidget->setVisible(false);
00073             updateDialogSize();
00074         }
00075     }
00076 
00077     QWidget* DecodeDialog::currentFormatWidget(FormatModuleInterface* module)
00078     {
00079         if (module == NULL)
00080             return NULL;
00081 
00082         return module->decodeWidget();
00083     }
00084 
00085     void DecodeDialog::ok()
00086     {
00087         if(!isDialogReady())
00088             return;
00089         msgTextEdit->setText("");
00090         removeFile();
00091 
00092         FormatModuleInterface* formatModule = currentFormatModule();
00093         if(formatModule == NULL || !formatModule->isDecodeWidgetReady())
00094         {
00095             QMessageBox::warning(this, tr("SilentEye Warning"),
00096                                  QString("Wrong options for the current encoding format.\n")+
00097                                  "Please check your options values before launching the decoding process.");
00098             return;
00099         }
00100 
00101         setCursor(Qt::WaitCursor);
00102 
00103         CryptoModuleInterface* module = NULL;
00104         try
00105         {
00106             // downcast to daughter class
00107             if (m_md->type() == Media::IMAGE) {
00108                 Image* img = ((ImageModuleInterface*) currentFormatModule())->decodeImage(QPointer<Image>((Image*)m_md.data()));
00109                 m_md = QPointer<Media>(img);
00110             } else if (m_md->type() == Media::AUDIO) {
00111                 m_md = ((AudioModuleInterface*) currentFormatModule())->decodeAudio(QPointer<Audio>((Audio*)m_md.data()));
00112             } else if (m_md->type() == Media::VIDEO) {
00113                 m_md = ((VideoModuleInterface*) currentFormatModule())->decodeVideo(QPointer<Video>((Video*)m_md.data()));
00114             }
00115 
00116             if(m_md->loadData())
00117             {
00118                 QPointer<EncodedData> data = m_md->encodedData();
00119                 data->setCompressed(compressCheckBox->checkState()!=Qt::Unchecked);
00120 
00121                 if(cryptoCheckBox->checkState()!=Qt::Unchecked)
00122                 {
00123                     module = currentCryptoModule();
00124                     if(module!=NULL)
00125                     {
00126                         QPointer<EncodedData> dataCrypto = module->decode(cryptoPwd2LineEdit->text(), data);
00127                         delete data;
00128                         data = dataCrypto;
00129                         m_md->setEncodedData(data);
00130                     }
00131                     else
00132                         m_logger->warning("Invalid encryption type");
00133                 }
00134 
00135                 switch(data->format())
00136                 {
00137                 case Data::FILE:
00138                     msgLabel->setText("Decoded file");
00139                     m_filePath = data->toData()->name();
00140                     fileNameLabel->setText(m_filePath);
00141                     msgTextEdit->setVisible(false);
00142                     fileWidget->setVisible(true);
00143                     fileButton->setEnabled(true);
00144                     dataWidget->setVisible(true);
00145                     updateDialogSize();
00146                     break;
00147                 case Data::ASCII:
00148                 case Data::UTF8:
00149                 case Data::LATIN1:
00150                     msgLabel->setText("Decoded message");
00151                     msgTextEdit->setText(data->toString());
00152                     msgTextEdit->setVisible(true);
00153                     fileWidget->setVisible(false);
00154                     dataWidget->setVisible(true);
00155                     updateDialogSize();
00156                     break;
00157                 default:
00158                     QMessageBox::warning(this, tr("SilentEye Warning"),
00159                                          "This media don't seem to have an hidden message.");
00160                 }
00161 
00162             } else {
00163                 QMessageBox::warning(this, tr("SilentEye Warning"),
00164                                      "This media don't seem to have an hidden message.");
00165             }
00166 
00167         }
00168         catch(ModuleException e)
00169         {
00170             if (module != NULL)
00171                 displayException(module->name(), e);
00172             else
00173                 displayException("Module unknown", e);
00174         }
00175         catch(SilentEyeException e)
00176         {
00177             displayException("SilentEye Warning", e);
00178         }
00179         setCursor(Qt::ArrowCursor);
00180     }
00181 
00182     void DecodeDialog::releaseFileButton()
00183     {
00184         if (!fileButton->isDown())
00185         {
00186             QDrag *drag = new QDrag(this);
00187             QMimeData *mimeData = new QMimeData;
00188 
00189             mimeData->setData("application/octet-stream", m_filePath.toUtf8());
00190             drag->setMimeData(mimeData);
00191 
00192             drag->exec(Qt::MoveAction);
00193         }
00194     }
00195 
00196 }