SilentEye 0.4.1
|
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 "optiondialog.h" 00017 #include "controller.h" 00018 00019 #include <imagemoduleinterface.h> 00020 #include <audiomoduleinterface.h> 00021 #include <videomoduleinterface.h> 00022 00023 namespace SilentEye { 00024 00025 OptionDialog::OptionDialog(QWidget* parent) 00026 : QDialog(parent) 00027 { 00028 setupUi(this); 00029 m_logger = new Logger(this); 00030 00031 m_optionWidget = NULL; 00032 m_checkSize = true; 00033 m_originalHeight = minimumHeight(); 00034 loadEncryptionTypes(); 00035 00036 setupDialog(); 00037 connectSignals(); 00038 } 00039 00040 OptionDialog::~OptionDialog() 00041 { 00042 if(!m_md.isNull()) 00043 delete(m_md); 00044 delete m_logger; 00045 } 00046 00047 void OptionDialog::setMedia(QPointer<Media> md) 00048 { 00049 m_logger->debug("Adding media : " + md->shortName()); 00050 if (m_md.isNull()) 00051 delete m_md; 00052 00053 // get a media copy 00054 if (md->type() == Media::IMAGE) { 00055 m_md = new Image(*((Image*)md.data())); 00056 } else if (md->type() == Media::AUDIO) { 00057 m_md = new Audio(*((Audio*)md.data())); 00058 } else if (md->type() == Media::VIDEO) { 00059 m_md = new Video(*((Video*)md.data())); 00060 } else { 00061 m_md = new Media(*md); 00062 } 00063 00064 loadFormats(); 00065 } 00066 00067 void OptionDialog::selectFolder() 00068 { 00069 setCursor(Qt::WaitCursor); 00070 QFileDialog dialog(this, tr("Save to")); 00071 dialog.setViewMode(QFileDialog::List); 00072 dialog.setFileMode(QFileDialog::DirectoryOnly); 00073 dialog.setDirectory(Controller::instance()->config.get("output")); 00074 if(dialog.exec()) 00075 { 00076 QStringList fileNames = dialog.selectedFiles(); 00077 if(fileNames.size()>0) 00078 { 00079 if(QFile::exists(fileNames.at(0))) 00080 destinationLineEdit->setText(fileNames.at(0)); 00081 else 00082 QMessageBox::warning(this, tr("SilentEye Warning"), 00083 fileNames.at(0)+"\n"+ 00084 "Selected folder doesn't exists.\n"+ 00085 "Please select an other destination!"); 00086 } 00087 } 00088 setCursor(Qt::ArrowCursor); 00089 } 00090 00091 void OptionDialog::selectFile() 00092 { 00093 bool save = this->objectName() == "DecodeDialog"; 00094 setCursor(Qt::WaitCursor); 00095 QFileDialog dialog(this, (save)?tr("Select destination"):tr("Select file to hide")); 00096 dialog.setViewMode(QFileDialog::List); 00097 if (save) 00098 dialog.setFileMode(QFileDialog::DirectoryOnly); 00099 else 00100 dialog.setFileMode(QFileDialog::ExistingFiles); 00101 dialog.setDirectory(Controller::instance()->config.get("output")); 00102 if(dialog.exec()) 00103 { 00104 QStringList fileNames = dialog.selectedFiles(); 00105 if(fileNames.size()>0) 00106 { 00107 if (save) 00108 { 00109 QFile file(fileNames.at(0) + "/" + m_filePath); 00110 file.open(QFile::WriteOnly); 00111 file.write(m_md->encodedData()->toData()->data()); 00112 file.close(); 00113 QMessageBox::information(this, tr("SilentEye Information"), 00114 "Decoded file has been saved to:\n" + file.fileName() + "."); 00115 } 00116 else 00117 addFile(fileNames.at(0)); 00118 } 00119 } 00120 setCursor(Qt::ArrowCursor); 00121 } 00122 00123 void OptionDialog::addFile(const QString& filename) 00124 { 00125 if(QFile::exists(filename)) 00126 { 00127 QFile file(filename); 00128 if (file.open(QFile::ReadOnly)) 00129 { 00130 file.close(); 00131 m_filePath = filename; 00132 fileNameLabel->setText(QDir::fromNativeSeparators(m_filePath).section("/", -1, -1)); 00133 msgTextEdit->setDisabled(true); 00134 fileRemoveButton->setEnabled(true); 00135 msgTextEdit->setText(""); 00136 valueChanged(); 00137 } 00138 else 00139 QMessageBox::warning(this, tr("SilentEye Warning"), 00140 filename+"\n"+ 00141 "Selected file cannot be read!\n"); 00142 } 00143 else 00144 QMessageBox::warning(this, tr("SilentEye Warning"), 00145 filename+"\n"+ 00146 "Selected file doesn't exists.\n"); 00147 } 00148 00149 void OptionDialog::removeFile() 00150 { 00151 m_filePath = ""; 00152 fileNameLabel->setText("no file selected"); 00153 fileRemoveButton->setEnabled(false); 00154 msgTextEdit->setEnabled(true); 00155 valueChanged(); 00156 } 00157 00158 void OptionDialog::rejected() 00159 { 00160 } 00161 00162 bool OptionDialog::isDialogReady() 00163 { 00164 if(m_md==NULL) 00165 { 00166 QMessageBox::warning(this, tr("SilentEye Warning"), 00167 "No media has been selected, this is probably an internal error.\n please report it to developpers."); 00168 return false; 00169 } 00170 00171 if(cryptoCheckBox->checkState()!=Qt::Unchecked) 00172 { 00173 if(!checkCryptoPassword()) 00174 { 00175 QMessageBox::warning(this, tr("SilentEye Warning"), 00176 "Key for encryption process have to be valid ! (match and have more than 5 characters)"); 00177 return false; 00178 } 00179 } 00180 00181 if(m_checkSize && dataSizeRemaining() < 0) 00182 { 00183 QMessageBox::warning(this, tr("SilentEye Warning"), 00184 "Selected media doesn't have enough space to encode your message!"); 00185 return false; 00186 } 00187 return true; 00188 } 00189 00190 FormatModuleInterface* OptionDialog::currentFormatModule() 00191 { 00192 QString format = formatComboBox->currentText(); 00193 00194 FormatModuleInterface* module = NULL; 00195 if (m_md->type() == Media::IMAGE) { 00196 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::IMAGEFORMAT, format); 00197 } else if (m_md->type() == Media::AUDIO) { 00198 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::AUDIOFORMAT, format); 00199 } else if (m_md->type() == Media::VIDEO) { 00200 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::VIDEOFORMAT, format); 00201 } 00202 00203 if(module == NULL) { 00204 m_logger->warning("Cannot find module for '" + format + "' format" ); 00205 } else if(m_checkSize) { 00206 connect(module, SIGNAL(optionChanged()), this, SLOT(valueChanged()), Qt::UniqueConnection); 00207 } 00208 00209 return module; 00210 } 00211 00212 CryptoModuleInterface* OptionDialog::currentCryptoModule() 00213 { 00214 QString type = cryptoComboBox->currentText(); 00215 return (CryptoModuleInterface*)ModuleManager::get(ModuleManager::CRYPTO, type); 00216 } 00217 00218 Data::DataFormat OptionDialog::currentCharset() 00219 { 00220 Data::DataFormat charset = Data::F_UNDEF; 00221 switch(charsetComboBox->currentIndex()) 00222 { 00223 case 0: 00224 charset = Data::UTF8; 00225 break; 00226 case 1: 00227 charset = Data::LATIN1; 00228 break; 00229 case 2: 00230 charset = Data::ASCII; 00231 break; 00232 } 00233 00234 return charset; 00235 } 00236 00237 void OptionDialog::formatChanged(const QString& imageFormat) 00238 { 00239 FormatModuleInterface* module = NULL; 00240 if (m_md->type() == Media::IMAGE) 00241 { 00242 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::IMAGEFORMAT, imageFormat); 00243 } 00244 else if (m_md->type() == Media::AUDIO) 00245 { 00246 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::AUDIOFORMAT, imageFormat); 00247 } 00248 else if (m_md->type() == Media::VIDEO) 00249 { 00250 module = (FormatModuleInterface*) ModuleManager::get(ModuleManager::VIDEOFORMAT, imageFormat); 00251 } 00252 00253 if(module==NULL) { 00254 m_logger->warning("Cannot find module for '" + imageFormat + "' format" ); 00255 return; 00256 } 00257 00258 QWidget* w = currentFormatWidget(module); 00259 00260 if(w!=NULL) 00261 { 00262 if(m_optionWidget!=NULL) 00263 m_optionWidget->setVisible(false); 00264 m_optionWidget = w; 00265 optionsVerticalLayout->insertWidget(0, m_optionWidget); 00266 m_optionWidget->setVisible(true); 00267 } 00268 else 00269 m_logger->warning("The widget for encoding format is NULL"); 00270 00271 valueChanged(); 00272 } 00273 00274 bool OptionDialog::checkCryptoPassword() 00275 { 00276 QPalette palette; 00277 bool return_value = false; 00278 00279 if (cryptoPwd2LineEdit->text().length() >= 6 00280 && cryptoPwd1LineEdit->text() == cryptoPwd2LineEdit->text()) 00281 { 00282 palette.setColor(QPalette::Base, QColor(179, 211, 155)); // green 00283 return_value = true; 00284 } 00285 else if (cryptoPwd2LineEdit->text().length() > 0 && cryptoPwd2LineEdit->text().length() <= 6) 00286 palette.setColor(QPalette::Base, QColor(255, 204, 204)); // red 00287 00288 cryptoPwd1LineEdit->setPalette(palette); 00289 cryptoPwd2LineEdit->setPalette(palette); 00290 00291 return return_value; 00292 } 00293 00294 00295 void OptionDialog::cryptoStatusChanged(int status) 00296 { 00297 if(status==Qt::Unchecked) 00298 { 00299 cryptoWidget->setVisible(false); 00300 cryptoPwd1LineEdit->setText(""); 00301 cryptoPwd2LineEdit->setText(""); 00302 checkCryptoPassword(); 00303 } 00304 else 00305 { 00306 cryptoWidget->setVisible(true); 00307 } 00308 updateDialogSize(); 00309 } 00310 00311 void OptionDialog::cryptoPwd1TextEdited(QString) 00312 { 00313 checkCryptoPassword(); 00314 } 00315 00316 void OptionDialog::cryptoPwd2TextEdited(QString) 00317 { 00318 checkCryptoPassword(); 00319 } 00320 00321 void OptionDialog::valueChanged() 00322 { 00323 if(m_checkSize) 00324 { 00325 //m_logger->debug("ValueChanged!"); 00326 FormatModuleInterface* module = currentFormatModule(); 00327 QPointer<Media> media; 00328 if(module!=NULL) { 00329 if (m_md->type() == Media::IMAGE) { 00330 media = ((ImageModuleInterface*)module)->encodeImage(QPointer<Image>((Image*)m_md.data()), true); 00331 } else if (m_md->type() == Media::AUDIO) { 00332 media = ((AudioModuleInterface*)module)->encodeAudio(QPointer<Audio>((Audio*)m_md.data()), true); 00333 } else if (m_md->type() == Media::VIDEO) { 00334 media = ((VideoModuleInterface*)module)->encodeVideo(QPointer<Video>((Video*)m_md.data()), true); 00335 } 00336 dataSizeAvailableLabel->setText(QString::number(media->capacity() - 1)); // -1: "Data" format flag 00337 } 00338 00339 int remaining = dataSizeRemaining(module, media); // this function delete media object 00340 00341 QPalette palette; 00342 if (remaining < 0) 00343 palette.setColor(QPalette::WindowText, QColor(127, 0, 0)); 00344 else 00345 palette.setColor(QPalette::WindowText, QColor(0, 127, 14)); 00346 dataSizeRemainingLabel->setText(QString::number(remaining)); 00347 dataSizeRemainingLabel->setPalette(palette); 00348 } 00349 } 00350 00351 int OptionDialog::dataSizeRemaining(FormatModuleInterface* module, QPointer<Media> media) { 00352 long capacity = 0; 00353 if(module==NULL) 00354 module = currentFormatModule(); 00355 00356 if(module!=NULL) { 00357 00358 if (media.isNull()) 00359 { 00360 if (m_md->type() == Media::IMAGE) { 00361 media = ((ImageModuleInterface*)module)->encodeImage(QPointer<Image>((Image*)m_md.data()), true); 00362 } else if (m_md->type() == Media::AUDIO) { 00363 media = ((AudioModuleInterface*)module)->encodeAudio(QPointer<Audio>((Audio*)m_md.data()), true); 00364 } else if (m_md->type() == Media::VIDEO) { 00365 media = ((VideoModuleInterface*)module)->encodeVideo(QPointer<Video>((Video*)m_md.data()), true); 00366 } 00367 } 00368 if (!media.isNull()) 00369 { 00370 capacity = media->capacity(); 00371 delete media; 00372 } 00373 } 00374 00375 int size; 00376 if (m_filePath == "" || !QFile::exists(m_filePath)) 00377 { 00378 EncodedData data(msgTextEdit->toPlainText(), currentCharset(), 00379 compressCheckBox->checkState()!=Qt::Unchecked); 00380 size = data.size(); 00381 } 00382 else 00383 { 00384 QFile file(m_filePath); 00385 EncodedData data(file, 00386 compressCheckBox->checkState()!=Qt::Unchecked); 00387 size = data.size(); 00388 } 00389 00390 return capacity-size; 00391 } 00392 00393 void OptionDialog::loadFormats() 00394 { 00395 formatComboBox->clear(); 00396 00397 QMap<QString, ModuleInterface*> map; 00398 if (m_md->type() == Media::IMAGE) { 00399 map = ModuleManager::get(ModuleManager::IMAGEFORMAT); 00400 m_logger->debug(QString::number(map.size()) + " image module found"); 00401 mediaFormatIcon->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/imageformat.png"))); 00402 } else if (m_md->type() == Media::AUDIO) { 00403 map = ModuleManager::get(ModuleManager::AUDIOFORMAT); 00404 m_logger->debug(QString::number(map.size()) + " audio module found"); 00405 mediaFormatIcon->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/play.png"))); 00406 } else if (m_md->type() == Media::VIDEO) { 00407 map = ModuleManager::get(ModuleManager::VIDEOFORMAT); 00408 m_logger->debug(QString::number(map.size()) + " video module found"); 00409 } else { 00410 m_logger->warning("Media type unknown!"); 00411 } 00412 00413 foreach (ModuleInterface* module, map) 00414 { 00415 formatComboBox->addItem(module->typeSupported()); 00416 if (Controller::instance()->config.get("imageformat") == module->typeSupported() 00417 || Controller::instance()->config.get("audioformat") == module->typeSupported() 00418 || Controller::instance()->config.get("videoformat") == module->typeSupported()) 00419 { 00420 formatComboBox->setCurrentIndex(formatComboBox->count()-1); 00421 } 00422 } 00423 } 00424 00425 void OptionDialog::loadEncryptionTypes() 00426 { 00427 foreach(ModuleInterface* module, 00428 ModuleManager::get(ModuleManager::CRYPTO)) 00429 { 00430 cryptoComboBox->addItem(module->typeSupported()); 00431 if(Controller::instance()->config.get("encryption")==module->typeSupported()) 00432 { 00433 cryptoComboBox->setCurrentIndex(cryptoComboBox->count()-1); 00434 } 00435 } 00436 } 00437 00438 void OptionDialog::updateDialogSize() 00439 { 00440 int height = m_originalHeight; 00441 if (!cryptoWidget->isVisible()) { 00442 height -= cryptoWidget->minimumHeight(); 00443 height -= mainGridLayout->verticalSpacing(); 00444 } 00445 if (!dataWidget->isVisible()) { 00446 height -= dataWidget->minimumHeight(); 00447 height -= mainGridLayout->verticalSpacing(); 00448 } 00449 00450 setMinimumHeight(height); 00451 resize(minimumWidth(), height); 00452 } 00453 00454 void OptionDialog::setupDialog() 00455 { 00456 cryptoWidget->setVisible(false); 00457 00458 if(ModuleManager::count(ModuleManager::CRYPTO)<=0) 00459 cryptoCheckBox->setVisible(false); 00460 00461 if(ModuleManager::count(ModuleManager::IMAGEFORMAT)<=0) 00462 { 00463 imageFormatWidget->setEnabled(false); 00464 okButton->setEnabled(false); 00465 } 00466 00467 if(Controller::instance()->config.get("charset")=="Latin1") 00468 charsetComboBox->setCurrentIndex(1); 00469 else if(Controller::instance()->config.get("charset")=="ASCII") 00470 charsetComboBox->setCurrentIndex(2); 00471 else 00472 charsetComboBox->setCurrentIndex(0); 00473 00474 updateDialogSize(); 00475 } 00476 00477 void OptionDialog::connectSignals() 00478 { 00479 connect(okButton, SIGNAL(pressed()), this, SLOT(ok())); 00480 connect(this, SIGNAL(rejected()), this, SLOT(rejected())); 00481 connect(toolButton, SIGNAL(pressed()), this, SLOT(selectFolder())); 00482 connect(cryptoCheckBox, SIGNAL(stateChanged(int)), 00483 this, SLOT(cryptoStatusChanged(int))); 00484 connect(cryptoPwd1LineEdit, SIGNAL(textEdited(QString)), 00485 this, SLOT(cryptoPwd1TextEdited(QString))); 00486 connect(cryptoPwd2LineEdit, SIGNAL(textEdited(QString)), 00487 this, SLOT(cryptoPwd2TextEdited(QString))); 00488 connect(formatComboBox, SIGNAL(currentIndexChanged(const QString&)), 00489 this, SLOT(formatChanged(const QString&))); 00490 connect(msgTextEdit, SIGNAL(textChanged()), 00491 this, SLOT(valueChanged())); 00492 connect(compressCheckBox, SIGNAL(stateChanged(int)), 00493 this, SLOT(valueChanged())); 00494 connect(fileRemoveButton, SIGNAL(pressed()), this, SLOT(removeFile())); 00495 } 00496 00497 void OptionDialog::displayException(const QString& title, 00498 const SilentEyeException e){ 00499 QMessageBox msgBox; 00500 msgBox.setIcon(QMessageBox::Critical); 00501 msgBox.setWindowTitle(title); 00502 msgBox.setText(e.message()); 00503 msgBox.setDetailedText(e.details()); 00504 msgBox.exec(); 00505 } 00506 00507 }