1
/*
2
 *   <Graphical interface>
3
 *   Copyright (C) <2011>  <José Antonio Sánchez Reynaga>, <Lisa "shainer" Vitolo>
4
 * 
5
 *   This library is free software; you can redistribute it and/or
6
 *   modify it under the terms of the GNU Lesser General Public
7
 *   License as published by the Free Software Foundation; either
8
 *   version 2.1 of the License, or (at your option) any later version.
9
 * 
10
 *   This library is distributed in the hope that it will be useful,
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *   Lesser General Public License for more details.
14
 * 
15
 *   You should have received a copy of the GNU Lesser General Public
16
 *   License along with this library; if not, write to the Free Software
17
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#include "window.h"
21
#include "ui_window.h"
22
23
#include <QDBusMessage>
24
#include <QDBusArgument>
25
26
Window::Window(DeviceList& dl, QWidget *parent) :
27
QWidget(parent), devList(dl),
28
ui(new Ui::Window)
29
{
30
  ui->setupUi(this);
31
  threadFormat = new ThreadFormat(this);
32
  
33
  /* Device slot */
34
  connect(&devList, SIGNAL(refreshDevices(USBDevice, bool)), this, SLOT(refreshListDevices(USBDevice, bool)));
35
  devList.initOperation();
36
  
37
  /* GUI slots */
38
  connect(ui->cb_devices, SIGNAL(activated(QString)), this, SLOT(newDeviceProperties(QString)));
39
  connect(ui->checkBox, SIGNAL(stateChanged(int)), this, SLOT(controlLabel(int)));
40
  
41
  /* "Thread" slots */
42
  connect(threadFormat, SIGNAL(started()), this, SLOT(disableGUI()));
43
  connect(threadFormat, SIGNAL(finished()), this, SLOT(enableGUI()));
44
  connect(threadFormat, SIGNAL(finished()), this, SLOT(checkFormat()));
45
  
46
  /* Button slots */
47
  connect(ui->but_format, SIGNAL(clicked(bool)), this, SLOT(preFormatOperations()));
48
  connect(ui->but_exit, SIGNAL(clicked(bool)), this, SLOT(close()));
49
    
50
  /* Available filesystems will appear here */
51
  ui->cb_filesystem->addItem("fat32");
52
  ui->cb_filesystem->addItem("ntfs");
53
  ui->cb_filesystem->addItem("ext4");
54
  ui->cb_filesystem->addItem("ext3");
55
  ui->cb_filesystem->addItem("ext2");
56
  
57
}
58
59
Window::~Window() {}
60
61
/*
62
 * Called when a new USB device dev is added (added=true) or removed
63
 * (added=false). Update lists accordingly.
64
 */
65
void Window::refreshListDevices(USBDevice dev, bool added)
66
{
67
  /* No changes while formatting is running */
68
  if(threadFormat->isRunning())
69
  {
70
    return;
71
  }
72
  
73
  if(added == true)
74
  {
75
    ui->cb_devices->insertItem(0, dev.getPath());
76
  }
77
  else
78
  {
79
    int i = ui->cb_devices->findText(dev.getPath());
80
    
81
    /*
82
     * **HACK ALERT**
83
     * Of course any attempt of removal is made with present devices, so i==-1 (device not present in list)
84
     * should never be true. However, see DeviceList implementation for an explanation.
85
     * In addition, when called from device notifier only the "caller" device is added to the list, so this
86
     * may happen anyway.
87
     */
88
    if (i != -1)
89
    {
90
      ui->cb_devices->removeItem(i);
91
    }
92
    
93
    /* No more devices! */
94
    if (ui->cb_devices->count() == 0)
95
    {
96
      ui->lb_product->setText(i18n("Product: "));
97
      ui->lb_size->setText(i18n("Capacity: "));
98
      ui->lb_filesystem->setText(i18n("Current filesystem: "));
99
    
100
      ui->lb_notice->setText(i18n("No devices found"));
101
    
102
      disableGUI();
103
      return;
104
    }
105
  }
106
107
  ui->cb_devices->setCurrentIndex(0);
108
  newDeviceProperties(ui->cb_devices->currentText());
109
}
110
111
/*
112
 * Refresh properties of a device when uses selects it on the combobox
113
 */
114
void Window::refreshDeviceProperties()
115
{  
116
  QString str_device = ui->cb_devices->currentText();
117
  newDeviceProperties(str_device);
118
}
119
120
void Window::newDeviceProperties(QString devname)
121
{
122
  USBDevice usb = devList.findDeviceFromPath(devname);
123
  QString size;
124
  size.setNum(usb.getCapacity());
125
  
126
  ui->lb_product->setText(i18n("Product: ") + usb.getVendor());
127
  ui->lb_size->setText(i18n("Capacity: ") + size + " MB");
128
  ui->lb_filesystem->setText(i18n("Current filesystem: ") +  usb.getFilesystem());
129
  
130
  enableGUI();
131
  ui->lb_notice->setText("");
132
}
133
134
Device Window::getCurrentDevice()
135
{
136
  QString path = ui->cb_devices->currentText();
137
  USBDevice ud = devList.findDeviceFromPath(path);
138
  return ud.getDevice();
139
}
140
141
void Window::preFormatOperations()
142
{
143
  /* Get confirmation from user */
144
  int rp = QMessageBox::question(this, i18n("Warning"), i18n("Do you really want to format the device?"), QMessageBox::Yes, QMessageBox::Cancel);
145
   
146
  if(rp == QMessageBox::Cancel)
147
  {
148
    return;
149
  }
150
   
151
  Device actual = getCurrentDevice();
152
153
  if(!actual.as<Block>()->device().contains(QRegExp("([0-9])$")))
154
  {
155
    qDebug() << "WARNING: It is needed to create a partition";
156
    QMessageBox::warning(this, i18n("Warning"), i18n("It is needed to create a partition"), QMessageBox::Ok);
157
    return;
158
  }
159
160
  applyFormat();
161
}
162
163
void Window::applyFormat()
164
{ 
165
   /* Saves arguments from the GUI */
166
   threadFormat->setFilesystem(ui->cb_filesystem->currentText());
167
   threadFormat->setDirDev(ui->cb_devices->currentText());
168
169
   ui->lb_notice->setText(i18n("Formatting USB device..."));
170
   threadFormat->start();
171
}
172
173
void Window::controlLabel(int state)
174
{
175
	if (state == 2)
176
	{
177
		ui->le_label_device->setEnabled(true);
178
	}
179
	else
180
	{
181
		ui->le_label_device->setDisabled(true);
182
	}
183
}	
184
185
void Window::disableGUI()
186
{
187
  ui->widget_2->setDisabled(true);
188
  ui->but_format->setDisabled(true);
189
  ui->widget_4->setDisabled(true);
190
  ui->widget_5->setDisabled(true);
191
}
192
193
void Window::enableGUI()
194
{
195
  ui->widget_2->setEnabled(true);
196
  ui->but_format->setEnabled(true);
197
  ui->widget_4->setEnabled(true);
198
  ui->widget_5->setEnabled(true);
199
}
200
201
/*
202
 * Automatically called when format ends (see the activated slots in the constructor)
203
 * checks if everything went okay and restores previous situation
204
 */
205
void Window::checkFormat()
206
{
207
	if (threadFormat->isOk())
208
	{
209
		ui->lb_notice->setText("Device successfully formatted");
210
	}
211
	else
212
	{
213
		ui->lb_notice->setText("An error occurred while formatting");
214
	}
215
	
216
	enableGUI();
217
}
218
219
Ui::Window* Window::getUi()
220
{
221
  return ui;
222
}
223
224
#include "window.moc"