summaryrefslogtreecommitdiffstats
path: root/src/qphotorec.cpp
blob: 821f8d85f241ac230fe6a0a02af19ff74620ebdb (plain)
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
222
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>	/* unlink, ftruncate */
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <ctype.h>      /* tolower */
#ifdef HAVE_LOCALE_H
#include <locale.h>	/* setlocale */
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <errno.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <QApplication>
#include <QLayoutItem>
#include <QLabel>
#include <QLayout>
#include "types.h"
#include "common.h"
#include "hdcache.h"
#include "hdaccess.h"
#include "fnctdsk.h"
#include "filegen.h"
#include "photorec.h"
#include "qphotorec.h"
#include "intrf.h"

QPhotorec::QPhotorec(QWidget *my_parent) : QWidget(my_parent)
{
  const int verbose=1;
  const int testdisk_mode=TESTDISK_O_RDONLY|TESTDISK_O_READAHEAD_32K;
  list_disk_t *element_disk;

  list_disk=hd_parse(NULL, verbose, testdisk_mode);

  hd_update_all_geometry(list_disk, verbose);
  /* Activate the cache, even if photorec has its own */
  for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
    element_disk->disk=new_diskcache(element_disk->disk,testdisk_mode);
  if(list_disk==NULL)
    no_disk();
  else
    disk_sel();
}

void QPhotorec::partition_selection(disk_t *disk)
{
  list_part_t *list_part;
  list_part_t *element;
  QLabel *t_copy = new QLabel("PhotoRec 6.14-WIP, Data Recovery Utility, May 2012\nChristophe GRENIER <grenier@cgsecurity.org>\nhttp://www.cgsecurity.org");
  QLabel *t_disk = new QLabel(disk->description_short(disk));
  QPushButton *button_proceed = new QPushButton("&Proceed");
  QPushButton *button_quit= new QPushButton("&Quit");

  QWidget *B_widget = new QWidget(this);
  QHBoxLayout *B_layout = new QHBoxLayout(B_widget);
  B_layout->addWidget(button_proceed);
  B_layout->addWidget(button_quit);
  B_widget->setLayout(B_layout);

  PartListWidget= new QListWidget();
  list_part=disk->arch->read_part(disk, 0, 0);
  {
    int insert_error=0;
    partition_t *partition_wd;
    partition_wd=new_whole_disk(disk);
    list_part=insert_new_partition(list_part, partition_wd, 0, &insert_error);
    if(insert_error>0)
    {
      free(partition_wd);
    }
  }
  if(list_part==NULL)
    return;
  for(element=list_part; element!=NULL; element=element->next)
  {
    const char *msg;
    msg=aff_part_aux(AFF_PART_ORDER|AFF_PART_STATUS, disk, element->part);
    PartListWidget->addItem(msg);
  }

  clearWidgets();
  QLayout *mainLayout = this->layout();
//  this->setWindowTitle(tr("QPhotoRec: partition selection"));
  
  mainLayout->addWidget(t_copy);
  mainLayout->addWidget(t_disk);
  mainLayout->addWidget(PartListWidget);
  mainLayout->addWidget(B_widget);

  this->setLayout(mainLayout);
  
  connect( button_quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
//  connect( button_proceed, SIGNAL(clicked()), this, SLOT(partition_selected()));
//  connect( PartListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(partition_selected()));
}

void QPhotorec::disk_selected()
{
  if(HDDlistWidget->selectedItems().count()==1)
  {
    list_disk_t *element_disk;
    const QString& s = HDDlistWidget->selectedItems()[0]->text();
    for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
    {
      disk_t *disk=element_disk->disk;
      if(QString(disk->description_short(disk)).compare(s)==0)
      {
	return partition_selection(disk);
      }
    }
  }
}

void QPhotorec::no_disk()
{
  this->setWindowTitle(tr("QPhotoRec"));
  QLabel *t_copy = new QLabel("PhotoRec 6.14-WIP, Data Recovery Utility, May 2012\nChristophe GRENIER <grenier@cgsecurity.org>\nhttp://www.cgsecurity.org");
  QLabel *t_free_soft = new QLabel("PhotoRec is free software, and\ncomes with ABSOLUTELY NO WARRANTY.");
  QLabel *t_no_disk = new QLabel("No harddisk found\n");
#if defined(__CYGWIN__) || defined(__MINGW32__)
  t_no_disk->setText("No harddisk found\n"
      "You need to be administrator to use this program.\n"
      "Under Win9x, use the DOS version instead.\n"
      "Under Vista or later, select this program, right-click and choose \"Run as administrator\".\n");
#elif defined(DJGPP)
#else
#ifdef HAVE_GETEUID
  if(geteuid()!=0)
  {
    t_no_disk->setText("No harddisk found\n"
	"You need to be root to use PhotoRec.");
  }
#endif
#endif
  QPushButton *button_quit= new QPushButton("&Quit");

  QVBoxLayout *mainLayout = new QVBoxLayout();
  mainLayout->addWidget(t_copy);
  mainLayout->addWidget(t_free_soft);
  mainLayout->addWidget(t_no_disk);
  mainLayout->addWidget(button_quit);
  this->setLayout(mainLayout);
  connect( button_quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
}


void QPhotorec::disk_sel()
{
  list_disk_t *element_disk;
  this->setWindowTitle(tr("QPhotoRec"));
  QLabel *t_copy = new QLabel("PhotoRec 6.14-WIP, Data Recovery Utility, May 2012\nChristophe GRENIER <grenier@cgsecurity.org>\nhttp://www.cgsecurity.org");
  QLabel *t_free_soft = new QLabel("PhotoRec is free software, and\ncomes with ABSOLUTELY NO WARRANTY.");
  QLabel *t_select = new QLabel("Please select a media");

  HDDlistWidget = new QListWidget();
  for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
  {
    disk_t *disk=element_disk->disk;
    HDDlistWidget->addItem(disk->description_short(disk));
  }
  HDDlistWidget->setToolTip("Disk capacity must be correctly detected for a successful recovery.\n"
      "If a disk listed above has incorrect size, check HD jumper settings, BIOS\n"
      "detection, and install the latest OS patches and disk drivers."
  );

  QPushButton *button_proceed = new QPushButton("&Proceed");
  QPushButton *button_quit= new QPushButton("&Quit");

  QWidget *B_widget = new QWidget(this);
  QHBoxLayout *B_layout = new QHBoxLayout(B_widget);
  B_layout->addWidget(button_proceed);
  B_layout->addWidget(button_quit);
  B_widget->setLayout(B_layout);

  QVBoxLayout *mainLayout = new QVBoxLayout();
  mainLayout->addWidget(t_copy);
  mainLayout->addWidget(t_free_soft);
  mainLayout->addWidget(t_select);
  mainLayout->addWidget(HDDlistWidget);
  mainLayout->addWidget(B_widget);
  this->setLayout(mainLayout);

  connect( button_quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
  connect( button_proceed, SIGNAL(clicked()), this, SLOT(disk_selected()));
  connect( HDDlistWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(disk_selected()));
}

void QPhotorec::clearWidgets()
{
  while(1)
  {
    QLayoutItem *layoutwidget;
    layoutwidget = this->layout()->takeAt(0);
    if(layoutwidget==NULL)
      return ;
    layoutwidget->widget()->deleteLater();
  }
}