1
/*
2
    <Formatting thread>
3
    Copyright (C) <2011>  <José Antonio Sánchez Reynaga>
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 "threadformat.h"
21
22
ThreadFormat::ThreadFormat(QObject* parent): QThread(parent)
23
{
24
25
}
26
27
ThreadFormat::~ThreadFormat()
28
{
29
    
30
}
31
32
33
void ThreadFormat::run()
34
{
35
    
36
    /* Time for unmounting the device */
37
    QThread::sleep(4);
38
        
39
        
40
    /* Formatting arguments */
41
    QStringList argumentos;
42
    
43
    if(filesystem.isEmpty()){
44
        qDebug() << "ERROR: Unknown filesystem :(";
45
        return;
46
    }
47
        
48
    if(dirDev.isEmpty()){
49
       qDebug() << "ERROR : Unknown volume to format";
50
       return;
51
    }
52
        
53
54
    if(filesystem == QString("ntfs")){
55
        
56
        
57
        
58
        qDebug() << "**** FORMATTING AS NTFS";
59
            
60
        if(label.isEmpty()) {
61
            argumentos << "-f" << "-v" << dirDev;
62
        } else {
63
            argumentos << "-f" << "-L"  << label  << "-v" << dirDev;
64
        }
65
            
66
        qDebug() << "EXEC COMMANDS : " << "mkntfs" << argumentos;
67
        
68
        if(QProcess::execute("mkntfs", argumentos) == 0){
69
            qDebug() << "USB FORMATTED SUCCESSFULLY";
70
            ok = true;
71
        }else{
72
            qDebug() << "UNABLE TO FORMAT";
73
            ok = false;
74
        }
75
        return;
76
    }
77
    else if(filesystem == QString("fat32")){
78
        
79
        qDebug() << "**** FORMATTING AS FAT 32";
80
        
81
        if(label.isEmpty()){
82
            argumentos << "-v" << dirDev;
83
        }else{
84
            argumentos << "-n"  << (label).toUpper().mid(0, 11)  << "-v" << dirDev;
85
        }
86
87
        qDebug() << "EXEC COMMANDS : " << "mkdosfs" << argumentos;
88
        
89
        if(QProcess::execute("mkdosfs", argumentos) == 0){
90
            qDebug() << "USB FORMATTED SUCCESSFULLY";
91
            ok = true;
92
        }else{
93
            qDebug() << "UNABLE TO FORMAT";
94
            ok = false;
95
        }
96
        return;
97
    }
98
    else if(filesystem == QString("ext4")){
99
         qDebug() << "**** FORMATTING AS EXT4";
100
        
101
        
102
            
103
            
104
              //Validacion de las etiquetas
105
        if(label.isEmpty()){
106
             //Argumentos cuando tiene etiqueta
107
            argumentos << "-t" << "ext4" << "-v" << dirDev;
108
        }else{
109
             //Argumentos cuando tiene etiqueta
110
            argumentos << "-t" << "ext4" << "-L"  << (label).mid(0, 16) << "-v" << dirDev;
111
        }
112
            
113
        qDebug() << "EXEC COMMANDS : " << "mke2fs" << argumentos;
114
        
115
        if(QProcess::execute("mke2fs", argumentos) == 0){
116
            qDebug() << "USB FORMATTED SUCCESSFULLY";
117
            ok = true;
118
        }else{
119
            qDebug() << "UNABLE TO FORMAT";
120
            ok = false;
121
        }
122
        return;
123
    }else if(filesystem == QString("ext2")){
124
         qDebug() << "**** FORMATTING AS EXT2";
125
        
126
        
127
            
128
        if(label.isEmpty()){
129
            argumentos << "-t" << "ext2" << "-v" << dirDev;
130
        }else{
131
            argumentos << "-t" << "ext2" << "-L"  << (label).mid(0, 16) << "-v" << dirDev;
132
        }
133
            
134
        qDebug() << "EXEC COMMANDS : " << "mke2fs" << argumentos;
135
        
136
        if(QProcess::execute("mke2fs", argumentos) == 0){
137
            qDebug() << "USB FORMATTED SUCCESSFULLY";
138
            ok = true;
139
        }else{
140
            qDebug() << "UNABLE TO FORMAT";
141
            ok = false;
142
        }
143
        return;
144
    }else if(filesystem == QString("ext3")){
145
         qDebug() << "**** FORMATTING AS EXT3";
146
        
147
        
148
            
149
            
150
        if(label.isEmpty()){
151
            argumentos << "-t" << "ext3" << "-v" << dirDev;
152
        }else{
153
            argumentos << "-t" << "ext3" << "-L"  << (label).mid(0, 16) << "-v" << dirDev;
154
        }
155
            
156
        qDebug() << "EXEC COMMANDS : " << "mke2fs" << argumentos;
157
        
158
        if(QProcess::execute("mke2fs", argumentos) == 0){
159
            qDebug() << "USB FORMATTED SUCCESSGULLY";
160
            ok = true;
161
        }else{
162
            qDebug() << "UNABLE TO FORMAT";
163
            ok = false;
164
        }
165
        return;
166
    }
167
    else{
168
            qDebug() << "ERROR: Unsupported filesystem = " << filesystem;
169
            ok = false;
170
    }
171
    
172
}
173
174
void ThreadFormat::setLabel(QString lb)
175
{
176
    label = lb;
177
}
178
179
180
bool ThreadFormat::isOk()
181
{
182
    return ok;
183
}
184
185
void ThreadFormat::setFilesystem(QString fs)
186
{
187
    filesystem = fs;
188
}
189
190
void ThreadFormat::setDirDev(QString dir)
191
{
192
    dirDev = dir;
193
}