SilentEye 0.4.1

encodedialog.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 "encodedialog.h"
00017 #include "controller.h"
00018 
00019 #include <imagemoduleinterface.h>
00020 #include <audiomoduleinterface.h>
00021 #include <videomoduleinterface.h>
00022 
00023 namespace SilentEye {
00024 
00025     EncodeDialog::EncodeDialog(QWidget* parent)
00026         : OptionDialog(parent)
00027     {
00028         this->setObjectName("EncodeDialog");
00029         m_logger->setClassName("EncodeDialog");
00030 
00031         this->setWindowTitle("Encode message");
00032         this->setWindowIcon(QPixmap(QString::fromUtf8(":/icons/img/encode.png")));
00033 
00034         okButton->setText("Encode");
00035         iconLabel->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/encode.png")));
00036         msgLabel->setText("Write a message or select a file to hide.");
00037         updateDialogSize();
00038 
00039         connect(fileButton, SIGNAL(pressed()), this, SLOT(selectFile()));
00040     }
00041 
00042     void EncodeDialog::showEvent(QShowEvent* event)
00043     {
00044         if(!event->spontaneous())
00045         {
00046             m_logger->debug("set default output dir: " + Controller::instance()->config.get("output"));
00047             destinationLineEdit->setText(Controller::instance()->config.get("output"));
00048             msgTextEdit->setText("");
00049             cryptoPwd1LineEdit->setText("");
00050             cryptoPwd2LineEdit->setText("");
00051             checkCryptoPassword();
00052             formatChanged(formatComboBox->currentText());
00053             if(!m_md.isNull())
00054                 this->setWindowTitle("Encode message : " + m_md->filePath());
00055             updateDialogSize();
00056             removeFile();
00057         }
00058     }
00059 
00060     void EncodeDialog::dragEnterEvent(QDragEnterEvent *event)
00061     {
00062         if (event->mimeData()->hasUrls())
00063             event->acceptProposedAction();
00064         else
00065             m_logger->debug(event->mimeData()->formats().join(";"));
00066     }
00067 
00068     void EncodeDialog::dropEvent(QDropEvent *event)
00069     {
00070         if(event->mimeData()->hasUrls())
00071         {
00072             QList<QUrl> urls = event->mimeData()->urls();
00073             for(int i=0; i<urls.size(); i++)
00074             {
00075                 if(urls.at(i).toLocalFile().length()>0)
00076                     addFile(urls.at(i).toLocalFile());
00077             }
00078         }
00079         else
00080         {
00081             QMessageBox::warning(this, tr("Warning"),
00082                                  "The dropped object doesn't have any url.");
00083         }
00084 
00085     }
00086 
00087     QWidget* EncodeDialog::currentFormatWidget(FormatModuleInterface* module)
00088     {
00089         if (module == NULL)
00090             return NULL;
00091 
00092         return module->encodeWidget();
00093     }
00094 
00095     void EncodeDialog::ok()
00096     {
00097         if(!isDialogReady())
00098             return;
00099 
00100         /*
00101          * CHECK widgets are ready
00102          */
00103         if(msgTextEdit->toPlainText().length()==0 && m_filePath == "")
00104         {
00105             QMessageBox::critical(this, tr("SilentEye Warning"),
00106                                   "You must provide a message or select a file to hide.");
00107             return;
00108         } else if (m_filePath != "" && !QFile::exists(m_filePath)) {
00109             QMessageBox::critical(this, tr("SilentEye Warning"),
00110                                   "Selected file doesn't exist!");
00111             return;
00112         }
00113 
00114         QString output = destinationLineEdit->text();
00115         if(!QFile::exists(output))
00116         {
00117             QMessageBox::warning(this, tr("SilentEye Warning"),
00118                                  "Destination folder doesn't exists!");
00119             return;
00120         }
00121 
00122         if(!currentFormatModule()->isEncodeWidgetReady())
00123         {
00124             setCursor(Qt::ArrowCursor);
00125             QMessageBox::warning(this, tr("SilentEye Warning"),
00126                                  QString("Wrong options for the current encoding format.\n")+
00127                                  "Please check your options values before launching the encoding process.");
00128             return;
00129         }
00130 
00131         // NOW PROCESS BEGIN
00132 
00133         setCursor(Qt::WaitCursor);
00134 
00135         if (defaultCheckBox->checkState()!=Qt::Unchecked)
00136         {
00137             Controller::instance()->config.set("output", output);
00138             Controller::instance()->config.save();
00139         }
00140 
00141         CryptoModuleInterface* module;
00142         try
00143         {
00144             // don't compress yet if crypto enabled
00145             bool compress = compressCheckBox->checkState()!=Qt::Unchecked && cryptoCheckBox->checkState()==Qt::Unchecked;
00146             QPointer<EncodedData> data;
00147             if (m_filePath != "" && QFile::exists(m_filePath))
00148             {
00149                 m_logger->debug("File to hide: " + m_filePath);
00150                 QFile file(m_filePath);
00151                 data = new EncodedData(file, compress);
00152             }
00153             else
00154             {
00155                 m_logger->debug("Message to hide: " + msgTextEdit->toPlainText());
00156                 data = new EncodedData(msgTextEdit->toPlainText(), currentCharset(), compress);
00157             }
00158 
00159             if(cryptoCheckBox->checkState()!=Qt::Unchecked)
00160             {
00161                 module = currentCryptoModule();
00162                 if(module!=NULL)
00163                 {
00164                     data = module->encode(cryptoPwd2LineEdit->text(), data);
00165                     data->setCompressed(compressCheckBox->checkState()!=Qt::Unchecked, true);
00166                 }
00167                 else
00168                     m_logger->warning("Invalid encryption type");
00169             }
00170 
00171             if (m_md->type() == Media::IMAGE) {
00172                 Image* img = ((ImageModuleInterface*) currentFormatModule())->encodeImage(QPointer<Image>((Image*)m_md.data()), false);
00173                 m_md = QPointer<Media>(img);
00174             } else if (m_md->type() == Media::AUDIO) {
00175                 m_md = ((AudioModuleInterface*) currentFormatModule())->encodeAudio(QPointer<Audio>((Audio*)m_md.data()), false);
00176             } else if (m_md->type() == Media::VIDEO) {
00177                 m_md = ((VideoModuleInterface*) currentFormatModule())->encodeVideo(QPointer<Video>((Video*)m_md.data()), false);
00178             }
00179 
00180             if (!output.endsWith('/'))
00181             {
00182                 output += "/";
00183             }
00184             m_logger->debug(output+m_md->shortName() + " == " + m_md->filePath());
00185             if (m_md->filePath().compare(output+m_md->shortName(), Qt::CaseInsensitive) == 0)
00186             {
00187                 setCursor(Qt::ArrowCursor);
00188                 QMessageBox::warning(this, tr("SilentEye Warning"),
00189                                      "Cannot save new file into source directory, please select an other destination!");
00190                 return;
00191             }
00192             else if (QFile::exists(output+m_md->shortName()))
00193             {
00194                 setCursor(Qt::ArrowCursor);
00195                 QMessageBox msgBox;
00196                 msgBox.setIcon(QMessageBox::Question);
00197                 msgBox.setText("Destination file already exists !");
00198                 msgBox.setDetailedText(output+m_md->shortName());
00199                 msgBox.setInformativeText("Do you really want to override it ?");
00200                 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
00201                 msgBox.setDefaultButton(QMessageBox::Cancel);
00202                 if(msgBox.exec()==QMessageBox::Cancel)
00203                     return;
00204             }
00205 
00206             m_md->setEncodedData(data);
00207 
00208             QString errorMsg;
00209             if( m_md->saveToDir(output) )
00210             {
00211                 setCursor(Qt::ArrowCursor);
00212                 emit encodedImage(m_md->filePath());
00213                 this->accept();
00214             }
00215             else {
00216                 errorMsg += "- An error occured during the encoding process.\n";
00217                 errorMsg += "- Cannot save the output file : "+m_md->filePath()+".\n";
00218             }
00219 
00220             if(errorMsg.length() > 0)
00221                 QMessageBox::critical(this, tr("SilentEye Error"), errorMsg);
00222         }
00223         catch(ModuleException e)
00224         {
00225             if (module != NULL)
00226                 displayException(module->name(), e);
00227             else
00228                 displayException("Module unknown", e);
00229         }
00230         catch(SilentEyeException e)
00231         {
00232             displayException(module->name(), e);
00233         }
00234 
00235         setCursor(Qt::ArrowCursor);
00236     }
00237 
00238 }