PictureCrypt  1.4.1
An image-steganography project
viewpc.cpp
Go to the documentation of this file.
1 #include "viewpc.h"
2 #include "ui_viewpc.h"
3 
4 ViewPC::ViewPC(QWidget *parent) :
5  QMainWindow(parent),
6  ui(new Ui::ViewPC)
7 {
8  ui->setupUi(this);
9 
10  progressDialogClosed = true;
11 
13 
14  isEncrypt = true;
15 }
20 {
21  delete ui;
22 }
23 
24 void ViewPC::on_encryptMode_clicked()
25 {
26  // Encrypt radio button clicked
27  setEncryptMode(true);
28 }
29 
30 void ViewPC::on_decryptMode_clicked()
31 {
32  // Decrypt radio button clicked
33  setEncryptMode(false);
34 }
39 {
40  // Opening QFileDialog depending on isEncrypt
41  if(isEncrypt)
42  inputFileName = QFileDialog::getOpenFileName(this, tr("Select file"), "/untitled.txt", tr("Text files (*.txt);;All Files (*)"));
43  else
44  inputFileName = QFileDialog::getOpenFileName(this, tr("Select file"), "/untitled.png", tr("PNG files (*.png);;All Files (*)"));
45  // Display the file name
46  ui->fileLabel->setText(inputFileName.isEmpty() ? tr("File not chosen") : inputFileName);
47 }
61 {
62  if(isEncrypt)
63  {
64  // Getting the data
65  QString text = ui->text->toPlainText();
66  QByteArray data;
67  if(text.isEmpty()) {
68  if(inputFileName.isEmpty()) {
69  alert("no_input_file", true);
70  return;
71  }
72  // Opening the file
73  QFile file(inputFileName);
74  if (!file.open(QIODevice::ReadOnly))
75  {
76  alert("open_file_fail", true);
77  return;
78  }
79  // Check the data size
80  auto size = file.size();
81  if(size > qPow(2, 24)) {
82  alert("muchdata", true);
83  file.close();
84  return;
85  }
86  data = file.readAll();
87  file.close();
88  }
89  else
90  data = text.toUtf8();
91  // Select image via EncryptDialog
92  EncryptDialog * dialog = new EncryptDialog(data);
93  dialog->exec();
94  if(!dialog->success)
95  return;
96 
97  // Get the data
98  QByteArray encr_data = dialog->compr_data;
99 
100  // Save the hash
101  QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Sha256);
102  encr_data = hash + encr_data;
103 
104  emit encrypt(data, &dialog->image, selectedMode, dialog->key, dialog->bitsUsed);
105  }
106  else
107  {
108  // Get the filename of the image
109  if(inputFileName.isEmpty()) {
110  alert("no_input_file", true);
111  return;
112  }
113  QByteArray key = requestKey().toUtf8();
114  if(key.isEmpty())
115  return;
116  QImage * res_image = new QImage(inputFileName);
117  emit decrypt(res_image, key, 0);
118  }
119 }
125 void ViewPC::alert(QString message, bool isWarning)
126 {
127  // Get message
128  if(errorsDict.contains(message))
129  message = errorsDict[message];
130  // Create message box
131  QMessageBox box;
132  if(isWarning)
133  box.setIcon(QMessageBox::Warning);
134  else
135  box.setIcon(QMessageBox::Information);
136  box.setText(message);
137  box.setWindowIcon(QIcon(":/icons/mail.png"));
138  box.setWindowTitle(tr("Message"));
139  box.exec();
140 }
146 void ViewPC::saveData(QByteArray Edata)
147 {
148  // Save data using QFileDialog
149  QString outputFileName = QFileDialog::getSaveFileName(this, tr("Save File"),
150  "/untitled.txt",
151  tr("Text(*.txt);;All files (*)"));
152  QFile writeFile(outputFileName);
153  if (!writeFile.open(QIODevice::WriteOnly))
154  {
155  alert("save_file_fail", true);
156  return;
157  }
158  writeFile.write(Edata);
159  writeFile.close();
160  alert("decryption_completed");
161 }
167 void ViewPC::saveImage(QImage * image)
168 {
169  // Save image using QFileDialog
170  QString outputFileName = QFileDialog::getSaveFileName(this, tr("Save Image"),
171  "/untitled.png",
172  tr("Images(*.png)"));
173  if(!image->save(outputFileName)) {
174  alert("save_file_fail", true);
175  return;
176  }
177  alert("encryption_completed");
178 }
185 void ViewPC::setProgress(int val)
186 {
187  if(val < 0) {
188  // Create dialog
189  dialog = new QProgressDialog(tr("Cryption in progress."), tr("Cancel"), 0, 100);
190  connect(dialog, SIGNAL(canceled()), this, SLOT(abortCircuit()));
191  progressDialogClosed = false;
192  dialog->setWindowTitle(tr("Processing"));
193  dialog->setWindowIcon(QIcon(":/icons/loading.png"));
194  dialog->show();
195  }
196  else if(val > 100 && !progressDialogClosed) {
197  // Close dialog
198  dialog->setValue(100);
199  QThread::msleep(25);
200  dialog->close();
201  dialog->reset();
202  progressDialogClosed = true;
203  }
204  // Update the progress
205  else if(!progressDialogClosed)
206  dialog->setValue(val);
207 }
212 {
213  // Set the flag
214  progressDialogClosed = true;
215  // Close the dialog
216  dialog->close();
217  dialog->reset();
218  emit abortModel();
219 }
224 void ViewPC::setEncryptMode(bool encr)
225 {
226  ui->text->setText("");
227  ui->text->setEnabled(encr);
228  isEncrypt = encr;
229  ui->startButton->setText(encr ? tr("Continue configuration") : tr("Start decryption"));
230  ui->enLabel1->setText(encr ? tr("Type in the text for encryption:") : tr("Text input isn't supported in decryption mode"));
231  ui->enLabel1->setEnabled(encr);
232  ui->enLabel2->setText(encr ? tr("Or use the file dialog to choose a file:") : tr("Choose a file for decryption:"));
233  ui->comboBox->setEnabled(encr);
234 }
239 void ViewPC::setVersion(QString version)
240 {
241  // Version setup
242  versionString = version;
243 }
249 {
250  bool ok;
251  QString text = QInputDialog::getText(this, tr("Dialog"),
252  tr("Enter the keyphrase:"), QLineEdit::Password,
253  "", &ok);
254  if(text.isEmpty() && ok) {
255  alert("no_key", true);
256  return QString();
257  }
258  return ok ? text : QString();
259 }
260 
261 QByteArray ViewPC::bytes(long long n)
262 {
263  return QByteArray::fromHex(QByteArray::number(n, 16));
264 }
269 {
270  AboutPC about;
271  about.setVersion(versionString);
272  about.exec();
273 }
274 
279 {
280  QUrl docLink("https://waleko.github.io/PictureCrypt/docs");
281  QDesktopServices::openUrl(docLink);
282 }
287 {
288  errorsDict["no_data"] = tr("No data given!");
289  errorsDict["muchdata"] = tr("Data size is too big (must be less than 15MB)!");
290  errorsDict["nullimage"] = tr("Invalid / null image!");
291  errorsDict["bigimage"] = tr("Image is too big!");
292  errorsDict["bitsWrong"] = tr("bitsUsed parameter is wrong!");
293  errorsDict["no_key"] = tr("No key given!");
294  errorsDict["big_key"] = tr("Given key is too big!");
295  errorsDict["undefined_mode"] = tr("Undefined mode is only available when decrypting!");
296  errorsDict["wrongmode"] = tr("Given mode isn't available!");
297  errorsDict["inject-v1.4"] = tr("ModelPC::inject() isn't available with v1.4 (advanced) mode");
298  errorsDict["all_modes_fail"] = tr("Given image isn't encrypted by this app (all modes have failed) or is damaged!");
299  errorsDict["nojphs"] = tr("JPHS is not installed!");
300  errorsDict["bitsBufferFail"] = tr("bitsBufferFail (holy crap, contact me or submit a bug)");
301  errorsDict["point_visited_twice"] = tr("One point visited twice (holy crap, contact me or submit a bug)");
302  errorsDict["bigdata"] = tr("Too much data for this image!");
303  errorsDict["veriffail"] = tr("Given image isn't encrypted with this mode or is damaged!");
304  errorsDict["noreaddata"] = tr("No data to read from image!");
305  errorsDict["new_version"] = tr("Version of the image is newer than yours (update!!!)");
306  errorsDict["old_version"] = tr("Version of the image is older than yours");
307  errorsDict["no_input_file"] = tr("No file given!");
308  errorsDict["open_file_fail"] = tr("Cannot open file!");
309  errorsDict["save_file_fail"] = tr("Cannot save file!");
310  errorsDict["decryption_completed"] = tr("Decryption completed!");
311  errorsDict["encryption_completed"] = tr("Encryption completed!");
312 }
313 
314 void ViewPC::on_actionJPHS_path_triggered()
315 {
316  QString dir = QFileDialog::getExistingDirectory(this, tr("Open JPHS folder"),
317  "/home",
318  QFileDialog::ShowDirsOnly
319  | QFileDialog::DontResolveSymlinks);
320  emit setJPHSDir(dir);
321 }
322 
323 void ViewPC::on_comboBox_currentIndexChanged(int index)
324 {
325  selectedMode = index + 1;
326 }
327 
328 void ViewPC::on_text_textChanged()
329 {
330  ui->fileButton->setEnabled(ui->text->toPlainText().isEmpty());
331 }
bool success
success Flag, if image was successfully selected and data was encrypted.
Definition: encryptdialog.h:46
bool progressDialogClosed
progressDialogClosed Flag, if dialog is closed.
Definition: viewpc.h:116
Definition: aboutpc.h:6
QImage image
image Inputted image
Definition: encryptdialog.h:79
void setJPHSDir(QString dir)
setJPHSPath Sets the default JPHS directory
void abortCircuit()
ViewPC::abortCircuit Slot to close ProgressDialog (ViewPC::dialog)
Definition: viewpc.cpp:211
The ViewPC class View layer of the app. Controls EncryptDialog and ProgressDialog.
Definition: viewpc.h:35
ViewPC(QWidget *parent=nullptr)
Definition: viewpc.cpp:4
QProgressDialog * dialog
dialog ProgressDialog used.
Definition: viewpc.h:111
~ViewPC()
ViewPC::~ViewPC Simple destructor for this layer.
Definition: viewpc.cpp:19
The EncryptDialog class Class to get the image and key to store secret info.
Definition: encryptdialog.h:21
void decrypt(QImage *_image, QString key, int mode)
decrypt Signal calling ModelPC::decrypt
void on_startButton_clicked()
ViewPC::on_startButton_clicked Slot to be called, when Start Button is pressed.
Definition: viewpc.cpp:60
void setEncryptMode(bool encr)
ViewPC::setEncryptMode Set the encrpt mode (ViewPC::isEncrypt)
Definition: viewpc.cpp:224
void on_actionAbout_triggered()
ViewPC::on_actionAbout_triggered Opens about page.
Definition: viewpc.cpp:268
void alert(QString message, bool isWarning=false)
ViewPC::alert Slot to create QMessageBox with message.
Definition: viewpc.cpp:125
void abortModel()
abortModel Signal calling to stop ModelPC::circuit
int bitsUsed
bitsUsed Bits used per byte of pixel.
Definition: encryptdialog.h:75
QMap< QString, QString > errorsDict
errorsDict QMap - Errors dictionary
Definition: viewpc.h:120
void setProgress(int val)
ViewPC::setProgress Slot to set the value of the ProgressDialog (ViewPC::dialog). ...
Definition: viewpc.cpp:185
void on_fileButton_clicked()
ViewPC::on_fileButton_clicked Slot to be called, when according button is pressed.
Definition: viewpc.cpp:38
void saveData(QByteArray Edata)
ViewPC::saveData Slot to be called to save data using QFileDialog.
Definition: viewpc.cpp:146
QString key
key Key to be used for encryption in EncrytDialog::zip
Definition: encryptdialog.h:62
void encrypt(QByteArray data, QImage *image, int mode, QString key, int bitsUsed)
encrypt Signal calling ModelPC::encrypt
void setVersion(QString version)
AboutPC::setVersion Function to set the version display.
Definition: aboutpc.cpp:19
QString requestKey()
ViewPC::requestKey Request keyphrase from user using InputDialog.
Definition: viewpc.cpp:248
void saveImage(QImage *image)
ViewPC::saveImage Slot to be called to save image using QFileDialog.
Definition: viewpc.cpp:167
QByteArray compr_data
compr_data Compressed data, aka Output data.
Definition: encryptdialog.h:50
void setVersion(QString version)
ViewPC::setVersion Set the version of the app from ControllerPC.
Definition: viewpc.cpp:239
The AboutPC class The About Page dialog.
Definition: aboutpc.h:12
void setupErrorsDict()
ViewPC::setupErrorsDict Setups errorsDict from strings.xml.
Definition: viewpc.cpp:286
void on_actionHelp_triggered()
ViewPC::on_actionHelp_triggered Opens online documentation.
Definition: viewpc.cpp:278