15 * The source file of the operation thread of file manager |
15 * The source file of the operation thread of file manager |
16 * |
16 * |
17 */ |
17 */ |
18 |
18 |
19 #include "fmoperationthread.h" |
19 #include "fmoperationthread.h" |
20 #include "fmoperationbase.h" |
20 #include "fmbackuprestorehandler.h" |
21 #include "fmdrivedetailstype.h" |
|
22 #include "fmcommon.h" |
|
23 #include "fmoperationcopy.h" |
|
24 #include "fmoperationmove.h" |
|
25 #include "fmoperationremove.h" |
|
26 #include "fmoperationformat.h" |
|
27 |
21 |
28 #include <QDir> |
22 #include <QDir> |
29 #include <QStack> |
23 #include <QStack> |
30 |
24 |
31 FmOperationThread::FmOperationThread( QObject *parent ) : QThread( parent ), mOperationBase( 0 ) |
25 /* |
|
26 * Constructs the operation thread with \a parent. |
|
27 */ |
|
28 FmOperationThread::FmOperationThread( QObject *parent ) : QThread( parent ), |
|
29 mStop( false), mOperationBase( 0 ) |
32 { |
30 { |
33 |
31 setPriority( LowestPriority ); |
34 |
|
35 } |
32 } |
36 |
33 |
|
34 /* |
|
35 * Destructs the operation thread. |
|
36 */ |
37 FmOperationThread::~FmOperationThread() |
37 FmOperationThread::~FmOperationThread() |
38 { |
38 { |
39 } |
39 } |
40 |
40 |
41 int FmOperationThread::asyncCopy( FmOperationBase* operationBase ) |
41 /* |
42 { |
42 * Prepare some conditions before starts the operation. |
43 if( isRunning() ){ |
43 * Returns the error id. |
44 return FmErrAlreadyStarted; |
44 * \a operationBase the operation to be prepared. |
|
45 */ |
|
46 int FmOperationThread::prepareOperationAndStart( FmOperationBase* operationBase ) |
|
47 { |
|
48 if ( isRunning() ) { |
|
49 return FmErrAlreadyStarted; |
45 } |
50 } |
46 |
|
47 mOperationBase = operationBase; |
51 mOperationBase = operationBase; |
48 mOperationBase->setObjectName( "operationElement" ); |
52 int ret = mOperationBase->prepare(); |
49 QMetaObject::connectSlotsByName( this ); |
53 if ( ret == FmErrNone ) { |
50 connect( mOperationBase, SIGNAL( askForRename( QString, QString* ) ), |
54 mStop = false; |
51 this, SLOT( onAskForRename( QString, QString* )), Qt::BlockingQueuedConnection ); |
55 start(); |
52 connect( mOperationBase, SIGNAL( askForReplace( QString, QString, bool* ) ), |
56 } |
53 this, SLOT( onAskForReplace( QString, QString, bool* )), Qt::BlockingQueuedConnection ); |
57 return ret; |
54 connect( mOperationBase, SIGNAL( showNote( QString ) ), |
|
55 this, SLOT( onShowNote( QString )), Qt::BlockingQueuedConnection ); |
|
56 |
|
57 start(); |
|
58 return FmErrNone; |
|
59 } |
58 } |
60 |
59 |
61 int FmOperationThread::asyncMove( FmOperationBase* operationBase ) |
60 /* Stops the current thread. |
62 { |
61 * Caused by user interaction. |
63 if( isRunning() ){ |
62 */ |
64 return FmErrAlreadyStarted; |
|
65 } |
|
66 |
|
67 mOperationBase = operationBase; |
|
68 mOperationBase->setObjectName( "operationElement" ); |
|
69 QMetaObject::connectSlotsByName( this ); |
|
70 connect( mOperationBase, SIGNAL( askForRename( QString, QString* ) ), |
|
71 this, SLOT( onAskForRename( QString, QString* )), Qt::BlockingQueuedConnection ); |
|
72 connect( mOperationBase, SIGNAL( askForReplace( QString, QString, bool* ) ), |
|
73 this, SLOT( onAskForReplace( QString, QString, bool* )), Qt::BlockingQueuedConnection ); |
|
74 connect( mOperationBase, SIGNAL( showNote( QString ) ), |
|
75 this, SLOT( onShowNote( QString )), Qt::BlockingQueuedConnection ); |
|
76 |
|
77 |
|
78 start(); |
|
79 return FmErrNone; |
|
80 } |
|
81 |
|
82 int FmOperationThread::asyncRemove( FmOperationBase* operationBase ) |
|
83 { |
|
84 if( isRunning() ){ |
|
85 return FmErrAlreadyStarted; |
|
86 } |
|
87 |
|
88 mOperationBase = operationBase; |
|
89 mOperationBase->setObjectName( "operationElement" ); |
|
90 QMetaObject::connectSlotsByName( this ); |
|
91 |
|
92 start(); |
|
93 return FmErrNone; |
|
94 } |
|
95 |
|
96 int FmOperationThread::asyncFormat( FmOperationBase* operationBase ) |
|
97 { |
|
98 if( isRunning() ){ |
|
99 return FmErrAlreadyStarted; |
|
100 } |
|
101 |
|
102 mOperationBase = operationBase; |
|
103 mOperationBase->setObjectName( "operationElement" ); |
|
104 QMetaObject::connectSlotsByName( this ); |
|
105 |
|
106 start(); |
|
107 return FmErrNone; |
|
108 } |
|
109 int FmOperationThread::asyncViewDriveDetails( FmOperationBase* operationBase ) |
|
110 { |
|
111 if( isRunning() ){ |
|
112 return FmErrAlreadyStarted; |
|
113 } |
|
114 |
|
115 mOperationBase = operationBase; |
|
116 mOperationBase->setObjectName( "operationElement" ); |
|
117 QMetaObject::connectSlotsByName( this ); |
|
118 |
|
119 start(); |
|
120 return FmErrNone; |
|
121 } |
|
122 |
|
123 int FmOperationThread::asyncViewFolderDetails( FmOperationBase* operationBase ) |
|
124 { |
|
125 if( isRunning() ){ |
|
126 return FmErrAlreadyStarted; |
|
127 } |
|
128 |
|
129 mOperationBase = operationBase; |
|
130 mOperationBase->setObjectName( "operationElement" ); |
|
131 QMetaObject::connectSlotsByName( this ); |
|
132 |
|
133 start(); |
|
134 return FmErrNone; |
|
135 } |
|
136 |
|
137 |
|
138 void FmOperationThread::stop() |
63 void FmOperationThread::stop() |
139 { |
64 { |
140 mStop = true; |
65 mStop = true; |
141 } |
66 } |
142 |
67 |
143 void FmOperationThread::onAskForRename( const QString &srcFile, QString *destFile ) |
68 /* |
144 { |
69 * reimp |
145 emit askForRename( srcFile, destFile ); |
70 */ |
146 } |
71 void FmOperationThread::run() |
147 void FmOperationThread::onAskForReplace( const QString &srcFile, const QString &destFile, bool *isAccepted ) |
72 { |
148 { |
73 mOperationBase->start( &mStop ); |
149 emit askForReplace( srcFile, destFile, isAccepted ); |
|
150 } |
74 } |
151 |
75 |
152 void FmOperationThread::onShowNote( const char *noteString ) |
|
153 { |
|
154 emit showNote( noteString ); |
|
155 } |
|
156 |
|
157 void FmOperationThread::on_operationElement_notifyPreparing( bool cancelable ) |
|
158 { |
|
159 emit notifyPreparing( cancelable ); |
|
160 } |
|
161 void FmOperationThread::on_operationElement_notifyStart( bool cancelable, int maxSteps ) |
|
162 { |
|
163 emit notifyStart( cancelable, maxSteps ); |
|
164 } |
|
165 void FmOperationThread::on_operationElement_notifyProgress( int currentStep ) |
|
166 { |
|
167 emit notifyProgress( currentStep ); |
|
168 } |
|
169 |
|
170 void FmOperationThread::run() |
|
171 { |
|
172 mStop = false; |
|
173 this->setPriority( LowestPriority ); |
|
174 switch( mOperationBase->operationType() ) |
|
175 { |
|
176 case FmOperationService::EOperationTypeCopy: |
|
177 { |
|
178 mErrString.clear(); |
|
179 FmOperationCopy *operationCopy = static_cast<FmOperationCopy*>(mOperationBase); |
|
180 |
|
181 int ret = operationCopy->start( &mStop, &mErrString ); |
|
182 switch( ret ) |
|
183 { |
|
184 case FmErrCancel: |
|
185 emit notifyCanceled(); |
|
186 break; |
|
187 case FmErrNone: |
|
188 emit notifyFinish(); |
|
189 break; |
|
190 default: |
|
191 emit notifyError( ret, mErrString ); |
|
192 break; |
|
193 } |
|
194 // refresh drive space no care if cancel, error or finished. |
|
195 // as filemanger cannot notify drive space changed |
|
196 // do not refresh path as QFileSystemModel will do auto-refresh |
|
197 emit driveSpaceChanged(); |
|
198 break; |
|
199 } |
|
200 case FmOperationService::EOperationTypeMove: |
|
201 { |
|
202 mErrString.clear(); |
|
203 FmOperationMove *operationMove = static_cast<FmOperationMove*>(mOperationBase); |
|
204 |
|
205 int ret = operationMove->start( &mStop, &mErrString ); |
|
206 switch( ret ) |
|
207 { |
|
208 case FmErrCancel: |
|
209 emit notifyCanceled(); |
|
210 break; |
|
211 case FmErrNone: |
|
212 emit notifyFinish(); |
|
213 break; |
|
214 default: |
|
215 emit notifyError( ret, mErrString ); |
|
216 } |
|
217 // refresh drive space no care if cancel, error or finished. |
|
218 // as filemanger cannot notify drive space changed |
|
219 // do not refresh path as QFileSystemModel will do auto-refresh |
|
220 emit driveSpaceChanged(); |
|
221 break; |
|
222 } |
|
223 case FmOperationService::EOperationTypeRemove: |
|
224 { |
|
225 mErrString.clear(); |
|
226 FmOperationRemove *operationRemove = static_cast<FmOperationRemove*>(mOperationBase); |
|
227 |
|
228 int ret = operationRemove->start( &mStop, &mErrString ); |
|
229 switch( ret ) |
|
230 { |
|
231 case FmErrCancel: |
|
232 emit notifyCanceled(); |
|
233 break; |
|
234 case FmErrNone: |
|
235 emit notifyFinish(); |
|
236 break; |
|
237 default: |
|
238 emit notifyError( ret, mErrString ); |
|
239 } |
|
240 // refresh drive space no care if cancel, error or finished. |
|
241 // as filemanger cannot notify drive space changed |
|
242 // do not refresh path as QFileSystemModel will do auto-refresh |
|
243 emit driveSpaceChanged(); |
|
244 break; |
|
245 } |
|
246 case FmOperationService::EOperationTypeFormat: |
|
247 { |
|
248 // emit notifyWaiting( false ); |
|
249 FM_LOG(QString("start format")); |
|
250 FmOperationFormat *operationFormat = static_cast<FmOperationFormat*>( mOperationBase ); |
|
251 FM_LOG(QString("get param and start format")); |
|
252 |
|
253 if ( FmErrNone != operationFormat->start() ) { |
|
254 emit notifyError( FmErrTypeFormatFailed, operationFormat->driverName() ); |
|
255 return; |
|
256 } |
|
257 FM_LOG(QString("format done")); |
|
258 emit notifyFinish(); |
|
259 // refresh drive space no care if cancel, error or finished. |
|
260 // as filemanger cannot notify drive space changed |
|
261 // do not refresh path as QFileSystemModel will do auto-refresh |
|
262 emit driveSpaceChanged(); |
|
263 FM_LOG(QString("format done and emit finish")); |
|
264 break; |
|
265 } |
|
266 case FmOperationService::EOperationTypeDriveDetails: |
|
267 { |
|
268 emit notifyWaiting( true ); |
|
269 |
|
270 FmOperationDriveDetails *operationDriverDetails = static_cast<FmOperationDriveDetails*>( mOperationBase ); |
|
271 int ret = FmDriveDetailsContent::querySizeofContent( |
|
272 operationDriverDetails->driverName(), operationDriverDetails->detailsSizeList(), &mStop ); |
|
273 if( ret == FmErrNone ) { |
|
274 emit notifyFinish(); |
|
275 } else if( ret == FmErrCancel ) { |
|
276 emit notifyCanceled(); |
|
277 } |
|
278 |
|
279 break; |
|
280 } |
|
281 case FmOperationService::EOperationTypeFolderDetails: |
|
282 { |
|
283 emit notifyWaiting( true ); |
|
284 |
|
285 FmOperationFolderDetails *operationFolderDetails = static_cast<FmOperationFolderDetails*>( mOperationBase ); |
|
286 int ret = FmFolderDetails::getNumofSubfolders( operationFolderDetails->folderPath(), operationFolderDetails->numofSubFolders(), |
|
287 operationFolderDetails->numofFiles(), operationFolderDetails->sizeofFolder(), |
|
288 &mStop ); |
|
289 if( ret == FmErrNone ) { |
|
290 emit notifyFinish(); |
|
291 } else if( ret == FmErrCancel ) { |
|
292 emit notifyCanceled(); |
|
293 } |
|
294 |
|
295 break; |
|
296 |
|
297 } |
|
298 default: |
|
299 Q_ASSERT( false ); |
|
300 |
|
301 } |
|
302 } |
|
303 |
|