| 1 |
/* |
| 2 |
* fm-file-ops-job.c |
| 3 |
* |
| 4 |
* Copyright 2009 PCMan <[email protected]> |
| 5 |
* Copyright 2012-2016 Andriy Grytsenko (LStranger) <[email protected]> |
| 6 |
* |
| 7 |
* This file is a part of the Libfm library. |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION:fm-file-ops-job |
| 26 |
* @short_description: Job to do something with files. |
| 27 |
* @title: FmFileOpsJob |
| 28 |
* |
| 29 |
* @include: libfm/fm.h |
| 30 |
* |
| 31 |
* The #FmFileOpsJob can be used to do some file operation such as move, |
| 32 |
* copy, delete, change file attributes, etc. |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifdef HAVE_CONFIG_H |
| 36 |
#include <config.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <glib/gi18n-lib.h> |
| 40 |
|
| 41 |
#include "fm-file-ops-job.h" |
| 42 |
#include "fm-file-ops-job-xfer.h" |
| 43 |
#include "fm-file-ops-job-delete.h" |
| 44 |
#include "fm-file-ops-job-change-attr.h" |
| 45 |
#include "fm-marshal.h" |
| 46 |
#include "fm-file-info-job.h" |
| 47 |
#include "glib-compat.h" |
| 48 |
|
| 49 |
enum |
| 50 |
{ |
| 51 |
PREPARED, |
| 52 |
CUR_FILE, |
| 53 |
PERCENT, |
| 54 |
ASK_RENAME, |
| 55 |
N_SIGNALS |
| 56 |
}; |
| 57 |
|
| 58 |
static guint signals[N_SIGNALS]; |
| 59 |
|
| 60 |
static void fm_file_ops_job_finalize (GObject *object); |
| 61 |
|
| 62 |
static gboolean fm_file_ops_job_run(FmJob* fm_job); |
| 63 |
/* static void fm_file_ops_job_cancel(FmJob* job); */ |
| 64 |
|
| 65 |
/* funcs for io jobs */ |
| 66 |
static gboolean _fm_file_ops_job_link_run(FmFileOpsJob* job); |
| 67 |
|
| 68 |
|
| 69 |
G_DEFINE_TYPE(FmFileOpsJob, fm_file_ops_job, FM_TYPE_JOB); |
| 70 |
|
| 71 |
static void fm_file_ops_job_dispose(GObject *object) |
| 72 |
{ |
| 73 |
FmFileOpsJob *self; |
| 74 |
|
| 75 |
g_return_if_fail(object != NULL); |
| 76 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(object)); |
| 77 |
|
| 78 |
self = (FmFileOpsJob*)object; |
| 79 |
|
| 80 |
if(self->srcs) |
| 81 |
{ |
| 82 |
fm_path_list_unref(self->srcs); |
| 83 |
self->srcs = NULL; |
| 84 |
} |
| 85 |
if(self->dest) |
| 86 |
{ |
| 87 |
fm_path_unref(self->dest); |
| 88 |
self->dest = NULL; |
| 89 |
} |
| 90 |
if(self->display_name) |
| 91 |
{ |
| 92 |
g_free(self->display_name); |
| 93 |
self->display_name = NULL; |
| 94 |
} |
| 95 |
if(self->icon) |
| 96 |
{ |
| 97 |
g_object_unref(self->icon); |
| 98 |
self->icon = NULL; |
| 99 |
} |
| 100 |
if(self->target) |
| 101 |
{ |
| 102 |
g_free(self->target); |
| 103 |
self->target = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
G_OBJECT_CLASS(fm_file_ops_job_parent_class)->dispose(object); |
| 107 |
} |
| 108 |
|
| 109 |
static void fm_file_ops_job_class_init(FmFileOpsJobClass *klass) |
| 110 |
{ |
| 111 |
GObjectClass *g_object_class; |
| 112 |
FmJobClass* job_class; |
| 113 |
g_object_class = G_OBJECT_CLASS(klass); |
| 114 |
g_object_class->dispose = fm_file_ops_job_dispose; |
| 115 |
g_object_class->finalize = fm_file_ops_job_finalize; |
| 116 |
|
| 117 |
job_class = FM_JOB_CLASS(klass); |
| 118 |
job_class->run = fm_file_ops_job_run; |
| 119 |
|
| 120 |
/** |
| 121 |
* FmFileOpsJob::prepared: |
| 122 |
* @job: a job object which emitted the signal |
| 123 |
* |
| 124 |
* The #FmFileOpsJob::prepared signal is emitted when preparation |
| 125 |
* of the file operation is done and @job is ready to start |
| 126 |
* copying/deleting... |
| 127 |
* |
| 128 |
* Since: 0.1.10 |
| 129 |
*/ |
| 130 |
signals[PREPARED] = |
| 131 |
g_signal_new( "prepared", |
| 132 |
G_TYPE_FROM_CLASS ( klass ), |
| 133 |
G_SIGNAL_RUN_FIRST, |
| 134 |
G_STRUCT_OFFSET ( FmFileOpsJobClass, prepared ), |
| 135 |
NULL, NULL, |
| 136 |
g_cclosure_marshal_VOID__VOID, |
| 137 |
G_TYPE_NONE, 0 ); |
| 138 |
|
| 139 |
/** |
| 140 |
* FmFileOpsJob::cur-file: |
| 141 |
* @job: a job object which emitted the signal |
| 142 |
* @file: (const char *) file which is processing |
| 143 |
* |
| 144 |
* The #FmFileOpsJob::cur-file signal is emitted when @job is about |
| 145 |
* to start operation on the @file. |
| 146 |
* |
| 147 |
* Since: 0.1.0 |
| 148 |
*/ |
| 149 |
signals[CUR_FILE] = |
| 150 |
g_signal_new( "cur-file", |
| 151 |
G_TYPE_FROM_CLASS ( klass ), |
| 152 |
G_SIGNAL_RUN_FIRST, |
| 153 |
G_STRUCT_OFFSET ( FmFileOpsJobClass, cur_file ), |
| 154 |
NULL, NULL, |
| 155 |
g_cclosure_marshal_VOID__POINTER, |
| 156 |
G_TYPE_NONE, 1, G_TYPE_POINTER ); |
| 157 |
|
| 158 |
/** |
| 159 |
* FmFileOpsJob::percent: |
| 160 |
* @job: a job object which emitted the signal |
| 161 |
* @percent: current ratio of completed job size to full job size |
| 162 |
* |
| 163 |
* The #FmFileOpsJob::percent signal is emitted when one more file |
| 164 |
* operation is completed. |
| 165 |
* |
| 166 |
* Since: 0.1.0 |
| 167 |
*/ |
| 168 |
signals[PERCENT] = |
| 169 |
g_signal_new( "percent", |
| 170 |
G_TYPE_FROM_CLASS ( klass ), |
| 171 |
G_SIGNAL_RUN_FIRST, |
| 172 |
G_STRUCT_OFFSET ( FmFileOpsJobClass, percent ), |
| 173 |
NULL, NULL, |
| 174 |
g_cclosure_marshal_VOID__UINT, |
| 175 |
G_TYPE_NONE, 1, G_TYPE_UINT ); |
| 176 |
|
| 177 |
/** |
| 178 |
* FmFileOpsJob::ask-rename: |
| 179 |
* @job: a job object which emitted the signal |
| 180 |
* @src: (#FmFileInfo *) source file |
| 181 |
* @dest: (#FmFileInfo *) destination directory |
| 182 |
* @new_name: (char **) pointer to receive new name |
| 183 |
* |
| 184 |
* The #FmFileOpsJob::ask-rename signal is emitted when file operation |
| 185 |
* raises a conflict because file with the same name already exists |
| 186 |
* in the directory @dest. Signal handler should find a decision how |
| 187 |
* to resolve the situation. If there is more than one handler connected |
| 188 |
* to the signal then only one of them will receive it. |
| 189 |
* Implementations are expected to inspect supported options for the |
| 190 |
* decision calling fm_file_ops_job_get_options(). Behavior when |
| 191 |
* handler returns unsupported option is undefined. |
| 192 |
* |
| 193 |
* Return value: a #FmFileOpOption decision. |
| 194 |
* |
| 195 |
* Since: 0.1.0 |
| 196 |
*/ |
| 197 |
signals[ASK_RENAME] = |
| 198 |
g_signal_new( "ask-rename", |
| 199 |
G_TYPE_FROM_CLASS ( klass ), |
| 200 |
G_SIGNAL_RUN_LAST, |
| 201 |
G_STRUCT_OFFSET ( FmFileOpsJobClass, ask_rename ), |
| 202 |
g_signal_accumulator_first_wins, NULL, |
| 203 |
fm_marshal_INT__POINTER_POINTER_POINTER, |
| 204 |
G_TYPE_INT, 3, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER ); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
static void fm_file_ops_job_finalize(GObject *object) |
| 210 |
{ |
| 211 |
g_return_if_fail(object != NULL); |
| 212 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(object)); |
| 213 |
|
| 214 |
G_OBJECT_CLASS(fm_file_ops_job_parent_class)->finalize(object); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
static void fm_file_ops_job_init(FmFileOpsJob *self) |
| 219 |
{ |
| 220 |
fm_job_init_cancellable(FM_JOB(self)); |
| 221 |
|
| 222 |
/* for chown */ |
| 223 |
self->uid = -1; |
| 224 |
self->gid = -1; |
| 225 |
self->set_hidden = -1; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* fm_file_ops_job_new |
| 230 |
* @type: type of file operation the new job will handle |
| 231 |
* @files: list of source files to perform operation |
| 232 |
* |
| 233 |
* Creates new #FmFileOpsJob which can be used in #FmJob API. |
| 234 |
* |
| 235 |
* Returns: a new #FmFileOpsJob object. |
| 236 |
* |
| 237 |
* Since: 0.1.0 |
| 238 |
*/ |
| 239 |
FmFileOpsJob *fm_file_ops_job_new(FmFileOpType type, FmPathList* files) |
| 240 |
{ |
| 241 |
FmFileOpsJob* job = (FmFileOpsJob*)g_object_new(FM_FILE_OPS_JOB_TYPE, NULL); |
| 242 |
job->srcs = fm_path_list_ref(files); |
| 243 |
job->type = type; |
| 244 |
return job; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static gboolean fm_file_ops_job_run(FmJob* fm_job) |
| 249 |
{ |
| 250 |
FmFileOpsJob* job = FM_FILE_OPS_JOB(fm_job); |
| 251 |
GError *err; |
| 252 |
switch(job->type) |
| 253 |
{ |
| 254 |
case FM_FILE_OP_COPY: |
| 255 |
return _fm_file_ops_job_copy_run(job); |
| 256 |
case FM_FILE_OP_MOVE: |
| 257 |
return _fm_file_ops_job_move_run(job); |
| 258 |
case FM_FILE_OP_TRASH: |
| 259 |
return _fm_file_ops_job_trash_run(job); |
| 260 |
case FM_FILE_OP_UNTRASH: |
| 261 |
return _fm_file_ops_job_untrash_run(job); |
| 262 |
case FM_FILE_OP_DELETE: |
| 263 |
return _fm_file_ops_job_delete_run(job); |
| 264 |
case FM_FILE_OP_LINK: |
| 265 |
return _fm_file_ops_job_link_run(job); |
| 266 |
case FM_FILE_OP_CHANGE_ATTR: |
| 267 |
return _fm_file_ops_job_change_attr_run(job); |
| 268 |
case FM_FILE_OP_NONE: ; |
| 269 |
} |
| 270 |
err = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
| 271 |
_("Operation not supported")); |
| 272 |
fm_job_emit_error(FM_JOB(job), err, FM_JOB_ERROR_CRITICAL); |
| 273 |
g_error_free(err); |
| 274 |
return FALSE; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* fm_file_ops_job_set_dest |
| 280 |
* @job: a job to set |
| 281 |
* @dest: destination path |
| 282 |
* |
| 283 |
* Sets destination path for operations FM_FILE_OP_MOVE, FM_FILE_OP_COPY, |
| 284 |
* or FM_FILE_OP_LINK. |
| 285 |
* |
| 286 |
* This API may be used only before @job is started. |
| 287 |
* |
| 288 |
* Since: 0.1.0 |
| 289 |
*/ |
| 290 |
void fm_file_ops_job_set_dest(FmFileOpsJob* job, FmPath* dest) |
| 291 |
{ |
| 292 |
job->dest = fm_path_ref(dest); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* fm_file_ops_job_get_dest |
| 297 |
* @job: a job to inspect |
| 298 |
* |
| 299 |
* Retrieves the destination path for operation. If type of operation |
| 300 |
* in not FM_FILE_OP_MOVE, FM_FILE_OP_COPY, or FM_FILE_OP_LINK then |
| 301 |
* result of this call is undefined. The returned value is owned by |
| 302 |
* @job and should be not freed by caller. |
| 303 |
* |
| 304 |
* Returns: (transfer none): the #FmPath which was set by previous call |
| 305 |
* to fm_file_ops_job_set_dest(). |
| 306 |
* |
| 307 |
* Since: 0.1.0 |
| 308 |
*/ |
| 309 |
FmPath* fm_file_ops_job_get_dest(FmFileOpsJob* job) |
| 310 |
{ |
| 311 |
return job->dest; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* fm_file_ops_job_set_chmod |
| 316 |
* @job: a job to set |
| 317 |
* @new_mode: which bits of file mode should be set |
| 318 |
* @new_mode_mask: which bits of file mode should be reset |
| 319 |
* |
| 320 |
* Sets that files for file operation FM_FILE_OP_CHANGE_ATTR should have |
| 321 |
* file mode changed according to @new_mode_mask and @new_mode: bits |
| 322 |
* that are present only in @new_mode_mask will be set to 0, and bits |
| 323 |
* that are present in both @new_mode_mask and @new_mode will be set to 1. |
| 324 |
* |
| 325 |
* This API may be used only before @job is started. |
| 326 |
* |
| 327 |
* Since: 0.1.0 |
| 328 |
*/ |
| 329 |
void fm_file_ops_job_set_chmod(FmFileOpsJob* job, mode_t new_mode, mode_t new_mode_mask) |
| 330 |
{ |
| 331 |
job->new_mode = new_mode; |
| 332 |
job->new_mode_mask = new_mode_mask; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* fm_file_ops_job_set_chown |
| 337 |
* @job: a job to set |
| 338 |
* @uid: user id to set as file owner |
| 339 |
* @gid: group id to set as file group |
| 340 |
* |
| 341 |
* Sets that files for file operation FM_FILE_OP_CHANGE_ATTR should have |
| 342 |
* owner or group changed. If @uid >= 0 then @job will try to change |
| 343 |
* owner of files. If @gid >= 0 then @job will try to change group of |
| 344 |
* files. |
| 345 |
* |
| 346 |
* This API may be used only before @job is started. |
| 347 |
* |
| 348 |
* Since: 0.1.0 |
| 349 |
*/ |
| 350 |
void fm_file_ops_job_set_chown(FmFileOpsJob* job, gint uid, gint gid) |
| 351 |
{ |
| 352 |
job->uid = uid; |
| 353 |
job->gid = gid; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* fm_file_ops_job_set_recursive |
| 358 |
* @job: a job to set |
| 359 |
* @recursive: recursion attribute to set |
| 360 |
* |
| 361 |
* Sets 'recursive' attribute for file operation according to @recursive. |
| 362 |
* If @recursive is %TRUE then file operation @job will try to do all |
| 363 |
* operations recursively. |
| 364 |
* |
| 365 |
* This API may be used only before @job is started. |
| 366 |
* |
| 367 |
* Since: 0.1.0 |
| 368 |
*/ |
| 369 |
void fm_file_ops_job_set_recursive(FmFileOpsJob* job, gboolean recursive) |
| 370 |
{ |
| 371 |
job->recursive = recursive; |
| 372 |
} |
| 373 |
|
| 374 |
static gpointer emit_cur_file(FmJob* job, gpointer cur_file) |
| 375 |
{ |
| 376 |
g_signal_emit(job, signals[CUR_FILE], 0, (const char*)cur_file); |
| 377 |
return NULL; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* fm_file_ops_job_emit_cur_file |
| 382 |
* @job: the job to emit signal |
| 383 |
* @cur_file: the data to emit |
| 384 |
* |
| 385 |
* Emits the #FmFileOpsJob::cur-file signal in main thread. |
| 386 |
* |
| 387 |
* This API is private to #FmFileOpsJob and should not be used outside |
| 388 |
* of libfm implementation. |
| 389 |
* |
| 390 |
* Since: 0.1.0 |
| 391 |
*/ |
| 392 |
void fm_file_ops_job_emit_cur_file(FmFileOpsJob* job, const char* cur_file) |
| 393 |
{ |
| 394 |
fm_job_call_main_thread(FM_JOB(job), emit_cur_file, (gpointer)cur_file); |
| 395 |
} |
| 396 |
|
| 397 |
static gpointer emit_percent(FmJob* job, gpointer percent) |
| 398 |
{ |
| 399 |
g_signal_emit(job, signals[PERCENT], 0, GPOINTER_TO_UINT(percent)); |
| 400 |
return NULL; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* fm_file_ops_job_emit_percent |
| 405 |
* @job: the job to emit signal |
| 406 |
* |
| 407 |
* Emits the #FmFileOpsJob::percent signal in main thread. |
| 408 |
* |
| 409 |
* This API is private to #FmFileOpsJob and should not be used outside |
| 410 |
* of libfm implementation. |
| 411 |
* |
| 412 |
* Since: 0.1.0 |
| 413 |
*/ |
| 414 |
void fm_file_ops_job_emit_percent(FmFileOpsJob* job) |
| 415 |
{ |
| 416 |
guint percent; |
| 417 |
|
| 418 |
if (fm_job_is_cancelled(FM_JOB(job))) |
| 419 |
return; |
| 420 |
|
| 421 |
if(job->total > 0) |
| 422 |
{ |
| 423 |
gdouble dpercent = (gdouble)(job->finished + job->current_file_finished) / job->total; |
| 424 |
percent = (guint)(dpercent * 100); |
| 425 |
if(percent > 100) |
| 426 |
percent = 100; |
| 427 |
} |
| 428 |
else |
| 429 |
percent = 100; |
| 430 |
|
| 431 |
if( percent > job->percent ) |
| 432 |
{ |
| 433 |
fm_job_call_main_thread(FM_JOB(job), emit_percent, GUINT_TO_POINTER(percent)); |
| 434 |
job->percent = percent; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
static gpointer emit_prepared(FmJob* job, gpointer user_data) |
| 439 |
{ |
| 440 |
g_signal_emit(job, signals[PREPARED], 0); |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* fm_file_ops_job_emit_prepared |
| 446 |
* @job: the job to emit signal |
| 447 |
* |
| 448 |
* Emits the #FmFileOpsJob::prepared signal in main thread. |
| 449 |
* |
| 450 |
* This API is private to #FmFileOpsJob and should not be used outside |
| 451 |
* of libfm implementation. |
| 452 |
* |
| 453 |
* Since: 0.1.10 |
| 454 |
*/ |
| 455 |
void fm_file_ops_job_emit_prepared(FmFileOpsJob* job) |
| 456 |
{ |
| 457 |
fm_job_call_main_thread(FM_JOB(job), emit_prepared, NULL); |
| 458 |
} |
| 459 |
|
| 460 |
struct AskRename |
| 461 |
{ |
| 462 |
FmFileInfo* src_fi; |
| 463 |
FmFileInfo* dest_fi; |
| 464 |
char* new_name; |
| 465 |
FmFileOpOption ret; |
| 466 |
}; |
| 467 |
|
| 468 |
static gpointer emit_ask_rename(FmJob* job, gpointer input_data) |
| 469 |
{ |
| 470 |
#define data ((struct AskRename*)input_data) |
| 471 |
g_signal_emit(job, signals[ASK_RENAME], 0, data->src_fi, data->dest_fi, &data->new_name, &data->ret); |
| 472 |
#undef data |
| 473 |
return NULL; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* fm_file_ops_job_ask_rename |
| 478 |
* @job: a job which asked |
| 479 |
* @src: source file descriptor |
| 480 |
* @src_inf: source file information |
| 481 |
* @dest: destination descriptor |
| 482 |
* @new_dest: pointer to get new destination |
| 483 |
* |
| 484 |
* Asks the user in main thread how to resolve conflict if file being |
| 485 |
* copied or moved already exists in destination directory. Ask is done |
| 486 |
* by emitting the #FmFileOpsJob::ask-rename signal. |
| 487 |
* |
| 488 |
* This API is private to #FmFileOpsJob and should not be used outside |
| 489 |
* of libfm implementation. |
| 490 |
* |
| 491 |
* Returns: a decision how to resolve conflict. |
| 492 |
* |
| 493 |
* Since: 0.1.0 |
| 494 |
*/ |
| 495 |
FmFileOpOption fm_file_ops_job_ask_rename(FmFileOpsJob* job, GFile* src, GFileInfo* src_inf, GFile* dest, GFile** new_dest) |
| 496 |
{ |
| 497 |
struct AskRename data; |
| 498 |
FmFileInfoJob* fijob; |
| 499 |
FmFileInfo *src_fi = NULL, *dest_fi = NULL; |
| 500 |
|
| 501 |
if (fm_job_is_cancelled(FM_JOB(job))) |
| 502 |
return 0; |
| 503 |
|
| 504 |
fijob = fm_file_info_job_new(NULL, 0); |
| 505 |
if( !src_inf ) |
| 506 |
fm_file_info_job_add_gfile(fijob, src); |
| 507 |
else |
| 508 |
src_fi = fm_file_info_new_from_g_file_data(src, src_inf, NULL); |
| 509 |
fm_file_info_job_add_gfile(fijob, dest); |
| 510 |
|
| 511 |
fm_job_set_cancellable(FM_JOB(fijob), fm_job_get_cancellable(FM_JOB(job))); |
| 512 |
fm_job_run_sync(FM_JOB(fijob)); |
| 513 |
|
| 514 |
if( fm_job_is_cancelled(FM_JOB(fijob)) ) |
| 515 |
{ |
| 516 |
if(src_fi) |
| 517 |
fm_file_info_unref(src_fi); |
| 518 |
g_object_unref(fijob); |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
if(!src_inf) |
| 523 |
src_fi = fm_file_info_list_pop_head(fijob->file_infos); |
| 524 |
dest_fi = fm_file_info_list_pop_head(fijob->file_infos); |
| 525 |
g_object_unref(fijob); |
| 526 |
if(!dest_fi) /* invalid destination directory! */ |
| 527 |
{ |
| 528 |
GError *err = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_FAILED, |
| 529 |
_("Cannot access destination file")); |
| 530 |
fm_job_emit_error(FM_JOB(job), err, FM_JOB_ERROR_CRITICAL); |
| 531 |
g_error_free(err); |
| 532 |
fm_file_info_unref(src_fi); |
| 533 |
return FM_FILE_OP_CANCEL; |
| 534 |
} |
| 535 |
|
| 536 |
data.ret = 0; |
| 537 |
data.src_fi = src_fi; |
| 538 |
data.dest_fi = dest_fi; |
| 539 |
data.new_name = NULL; |
| 540 |
fm_job_call_main_thread(FM_JOB(job), emit_ask_rename, (gpointer)&data); |
| 541 |
|
| 542 |
if(data.ret == FM_FILE_OP_RENAME) |
| 543 |
{ |
| 544 |
if(data.new_name) |
| 545 |
{ |
| 546 |
GFile* parent = g_file_get_parent(dest); |
| 547 |
*new_dest = g_file_get_child(parent, data.new_name); |
| 548 |
g_object_unref(parent); |
| 549 |
g_free(data.new_name); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
fm_file_info_unref(src_fi); |
| 554 |
fm_file_info_unref(dest_fi); |
| 555 |
|
| 556 |
return data.ret; |
| 557 |
} |
| 558 |
|
| 559 |
/* the same as fm_file_ops_job_ask_rename() but handles the case if the |
| 560 |
destination does not exist, i.e. error such as G_IO_ERROR_FILENAME_TOO_LONG */ |
| 561 |
FmFileOpOption _fm_file_ops_job_ask_new_name(FmFileOpsJob* job, GFile* src, |
| 562 |
GFile* dest, GFile** new_dest, |
| 563 |
gboolean dest_exists) |
| 564 |
{ |
| 565 |
struct AskRename data; |
| 566 |
FmFileInfoJob* fijob; |
| 567 |
FmFileInfo *src_fi = NULL, *dest_fi = NULL; |
| 568 |
|
| 569 |
if (fm_job_is_cancelled(FM_JOB(job))) |
| 570 |
return 0; |
| 571 |
|
| 572 |
fijob = fm_file_info_job_new(NULL, 0); |
| 573 |
fm_file_info_job_add_gfile(fijob, src); |
| 574 |
if (dest_exists) |
| 575 |
fm_file_info_job_add_gfile(fijob, dest); |
| 576 |
|
| 577 |
fm_job_set_cancellable(FM_JOB(fijob), fm_job_get_cancellable(FM_JOB(job))); |
| 578 |
fm_job_run_sync(FM_JOB(fijob)); |
| 579 |
|
| 580 |
if( fm_job_is_cancelled(FM_JOB(fijob)) ) |
| 581 |
{ |
| 582 |
if(src_fi) |
| 583 |
fm_file_info_unref(src_fi); |
| 584 |
g_object_unref(fijob); |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
src_fi = fm_file_info_list_pop_head(fijob->file_infos); |
| 589 |
if (dest_exists) |
| 590 |
dest_fi = fm_file_info_list_pop_head(fijob->file_infos); |
| 591 |
else if (src_fi) |
| 592 |
{ |
| 593 |
FmPath *dpath = fm_path_new_for_gfile(dest); |
| 594 |
/* forge dest_fi with just display name */ |
| 595 |
dest_fi = fm_file_info_new(); |
| 596 |
fm_file_info_set_path(dest_fi, dpath); |
| 597 |
_fm_path_set_display_name(dpath, fm_file_info_get_disp_name(src_fi)); |
| 598 |
fm_path_unref(dpath); |
| 599 |
} |
| 600 |
g_object_unref(fijob); |
| 601 |
if(!dest_fi) /* invalid destination directory! */ |
| 602 |
{ |
| 603 |
GError *err = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_FAILED, |
| 604 |
_("Cannot access destination file")); |
| 605 |
fm_job_emit_error(FM_JOB(job), err, FM_JOB_ERROR_CRITICAL); |
| 606 |
g_error_free(err); |
| 607 |
if (src_fi) |
| 608 |
fm_file_info_unref(src_fi); |
| 609 |
return FM_FILE_OP_CANCEL; |
| 610 |
} |
| 611 |
|
| 612 |
data.ret = 0; |
| 613 |
data.src_fi = src_fi; |
| 614 |
data.dest_fi = dest_fi; |
| 615 |
data.new_name = NULL; |
| 616 |
fm_job_call_main_thread(FM_JOB(job), emit_ask_rename, (gpointer)&data); |
| 617 |
|
| 618 |
if(data.ret == FM_FILE_OP_RENAME) |
| 619 |
{ |
| 620 |
if(data.new_name) |
| 621 |
{ |
| 622 |
GFile* parent = g_file_get_parent(dest); |
| 623 |
*new_dest = g_file_get_child(parent, data.new_name); |
| 624 |
g_object_unref(parent); |
| 625 |
g_free(data.new_name); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
fm_file_info_unref(src_fi); |
| 630 |
fm_file_info_unref(dest_fi); |
| 631 |
|
| 632 |
return data.ret; |
| 633 |
} |
| 634 |
|
| 635 |
static gboolean _fm_file_ops_job_link_run(FmFileOpsJob* job) |
| 636 |
{ |
| 637 |
gboolean ret = TRUE; |
| 638 |
GFile *dest_dir; |
| 639 |
GList* l; |
| 640 |
FmJob* fmjob = FM_JOB(job); |
| 641 |
FmFolder *dest_folder; |
| 642 |
|
| 643 |
job->supported_options = FM_FILE_OP_RENAME | FM_FILE_OP_SKIP; |
| 644 |
dest_dir = fm_path_to_gfile(job->dest); |
| 645 |
|
| 646 |
/* cannot create links on non-native filesystems */ |
| 647 |
if(!g_file_is_native(dest_dir)) |
| 648 |
{ |
| 649 |
GError *err = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_FAILED, |
| 650 |
_("Cannot create a link on non-native filesystem")); |
| 651 |
fm_job_emit_error(FM_JOB(job), err, FM_JOB_ERROR_CRITICAL); |
| 652 |
g_error_free(err); |
| 653 |
g_object_unref(dest_dir); |
| 654 |
return FALSE; |
| 655 |
} |
| 656 |
|
| 657 |
job->total = fm_path_list_get_length(job->srcs); |
| 658 |
g_debug("total files to link: %lu", (gulong)job->total); |
| 659 |
|
| 660 |
fm_file_ops_job_emit_prepared(job); |
| 661 |
|
| 662 |
dest_folder = fm_folder_find_by_path(job->dest); |
| 663 |
for(l = fm_path_list_peek_head_link(job->srcs); |
| 664 |
!fm_job_is_cancelled(fmjob) && l; l=l->next) |
| 665 |
{ |
| 666 |
FmPath* path = FM_PATH(l->data); |
| 667 |
char* src = NULL; |
| 668 |
char *_basename = NULL; |
| 669 |
const char *basename = fm_path_get_basename(path); |
| 670 |
GFile* dest; |
| 671 |
GError* err = NULL; |
| 672 |
char* dname = NULL; |
| 673 |
|
| 674 |
/* if we drop URI query onto native filesystem, omit query part */ |
| 675 |
if (!fm_path_is_native(path) && g_file_is_native(dest_dir)) |
| 676 |
dname = strchr(basename, '?'); |
| 677 |
/* if basename consist only from query then use first part of it */ |
| 678 |
if (dname == basename) |
| 679 |
{ |
| 680 |
basename++; |
| 681 |
dname = strchr(basename, '&'); |
| 682 |
} |
| 683 |
if (dname) |
| 684 |
{ |
| 685 |
_basename = g_strndup(basename, dname - basename); |
| 686 |
dname = strrchr(_basename, G_DIR_SEPARATOR); |
| 687 |
g_debug("cutting '%s' to '%s'",basename,dname?&dname[1]:_basename); |
| 688 |
if (dname) |
| 689 |
basename = &dname[1]; |
| 690 |
else |
| 691 |
basename = _basename; |
| 692 |
} |
| 693 |
/* showing currently processed file. */ |
| 694 |
dest = g_file_get_child(dest_dir, basename); |
| 695 |
dname = g_file_get_parse_name(dest); |
| 696 |
fm_file_ops_job_emit_cur_file(job, dname); |
| 697 |
g_free(dname); |
| 698 |
g_free(_basename); |
| 699 |
|
| 700 |
_retry_link: |
| 701 |
if (fm_path_is_native(path)) |
| 702 |
{ |
| 703 |
if (src == NULL) |
| 704 |
src = fm_path_to_str(path); |
| 705 |
if(!g_file_make_symbolic_link(dest, src, fm_job_get_cancellable(fmjob), &err)) |
| 706 |
{ |
| 707 |
_link_error: |
| 708 |
if(err) |
| 709 |
{ |
| 710 |
if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_EXISTS) |
| 711 |
{ |
| 712 |
GFile *new_dest = NULL, *src_file; |
| 713 |
FmFileOpOption opt; |
| 714 |
|
| 715 |
src_file = fm_path_to_gfile(path); |
| 716 |
opt = _fm_file_ops_job_ask_new_name(job, src_file, dest, &new_dest, TRUE); |
| 717 |
g_object_unref(src_file); |
| 718 |
|
| 719 |
g_error_free(err); |
| 720 |
err = NULL; |
| 721 |
|
| 722 |
switch(opt) |
| 723 |
{ |
| 724 |
case FM_FILE_OP_RENAME: |
| 725 |
if (new_dest) /* we got new dest */ |
| 726 |
{ |
| 727 |
g_object_unref(dest); |
| 728 |
dest = new_dest; |
| 729 |
goto _retry_link; |
| 730 |
} |
| 731 |
break; |
| 732 |
case FM_FILE_OP_CANCEL: |
| 733 |
fm_job_cancel(fmjob); |
| 734 |
break; |
| 735 |
case FM_FILE_OP_OVERWRITE: |
| 736 |
/* we do not support overwrite */ |
| 737 |
case FM_FILE_OP_SKIP: |
| 738 |
break; |
| 739 |
case FM_FILE_OP_SKIP_ERROR: ; /* FIXME */ |
| 740 |
} |
| 741 |
if (new_dest) |
| 742 |
g_object_unref(new_dest); |
| 743 |
} |
| 744 |
} |
| 745 |
ret = FALSE; |
| 746 |
} |
| 747 |
} |
| 748 |
else /* create shortcut instead */ |
| 749 |
{ |
| 750 |
gsize out_len; |
| 751 |
/* unfortunately we cannot use g_file_replace_contents() here |
| 752 |
because we should handle the case if file already exists */ |
| 753 |
GFileOutputStream *out = g_file_create(dest, G_FILE_CREATE_NONE, |
| 754 |
fm_job_get_cancellable(fmjob), |
| 755 |
&err); |
| 756 |
GFile *src_file; |
| 757 |
GFileInfo *inf; |
| 758 |
char *name = NULL, *iname = NULL; |
| 759 |
if (out == NULL) |
| 760 |
goto _link_error; |
| 761 |
src_file = fm_path_to_gfile(path); |
| 762 |
inf = g_file_query_info(src_file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI"," |
| 763 |
G_FILE_ATTRIBUTE_STANDARD_ICON"," |
| 764 |
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, |
| 765 |
G_FILE_QUERY_INFO_NONE, |
| 766 |
fm_job_get_cancellable(fmjob), NULL); |
| 767 |
g_object_unref(src_file); |
| 768 |
if (inf) |
| 769 |
{ |
| 770 |
GIcon *icon = g_file_info_get_icon(inf); |
| 771 |
|
| 772 |
/* set target icon if available */ |
| 773 |
if (icon) |
| 774 |
{ |
| 775 |
iname = g_icon_to_string(icon); |
| 776 |
if (iname && strncmp(iname, ". GThemedIcon ", 14) == 0) |
| 777 |
{ |
| 778 |
char *tmp = strchr(&iname[14], ' '); |
| 779 |
/* it is a themed icon, guess "right" name from it */ |
| 780 |
if (tmp) |
| 781 |
*tmp = '\0'; |
| 782 |
tmp = g_strdup(&iname[14]); |
| 783 |
g_free(iname); |
| 784 |
iname = tmp; |
| 785 |
} |
| 786 |
} |
| 787 |
/* FIXME: guess the icon if not available */ |
| 788 |
src = g_strdup(g_file_info_get_attribute_string(inf, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI)); |
| 789 |
name = g_strdup(g_file_info_get_display_name(inf)); |
| 790 |
g_object_unref(inf); |
| 791 |
} |
| 792 |
if (src == NULL) |
| 793 |
src = fm_path_to_uri(path); |
| 794 |
if (name == NULL) |
| 795 |
name = fm_path_display_basename(path); |
| 796 |
dname = g_strdup_printf("[Desktop Entry]\n" |
| 797 |
"Type=Link\n" |
| 798 |
"Name=%s" |
| 799 |
"%s%s\n" |
| 800 |
"URL=%s\n", name, |
| 801 |
iname ? "\nIcon=" : "", iname ? iname : "", |
| 802 |
src); |
| 803 |
g_free(name); |
| 804 |
g_free(iname); |
| 805 |
if (!g_output_stream_write_all(G_OUTPUT_STREAM(out), dname, |
| 806 |
strlen(dname), &out_len, |
| 807 |
fm_job_get_cancellable(fmjob), &err) || |
| 808 |
!g_output_stream_close(G_OUTPUT_STREAM(out), |
| 809 |
fm_job_get_cancellable(fmjob), &err)) |
| 810 |
{ |
| 811 |
g_object_unref(out); |
| 812 |
g_free(dname); |
| 813 |
goto _link_error; |
| 814 |
} |
| 815 |
g_object_unref(out); |
| 816 |
g_free(dname); |
| 817 |
} |
| 818 |
|
| 819 |
job->finished++; |
| 820 |
|
| 821 |
/* update progress */ |
| 822 |
fm_file_ops_job_emit_percent(job); |
| 823 |
if (ret && dest_folder) |
| 824 |
{ |
| 825 |
FmPath *dest_path = fm_path_new_for_gfile(dest); |
| 826 |
if (!_fm_folder_event_file_added(dest_folder, dest_path)) |
| 827 |
fm_path_unref(dest_path); |
| 828 |
} |
| 829 |
|
| 830 |
g_free(src); |
| 831 |
g_object_unref(dest); |
| 832 |
} |
| 833 |
|
| 834 |
/* g_debug("finished: %llu, total: %llu", job->finished, job->total); */ |
| 835 |
|
| 836 |
g_object_unref(dest_dir); |
| 837 |
if (dest_folder) |
| 838 |
g_object_unref(dest_folder); |
| 839 |
return ret; |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* fm_file_ops_job_set_display_name |
| 844 |
* @job: a job to set |
| 845 |
* @name: new display name |
| 846 |
* |
| 847 |
* Sets that file for file operation FM_FILE_OP_CHANGE_ATTR should have |
| 848 |
* display name changed according to @name. The job will fail if it will |
| 849 |
* be started for more than one file or if file doesn't support display |
| 850 |
* name change. |
| 851 |
* |
| 852 |
* This API may be used only before @job is started. |
| 853 |
* |
| 854 |
* Since: 1.2.0 |
| 855 |
*/ |
| 856 |
void fm_file_ops_job_set_display_name(FmFileOpsJob *job, const char *name) |
| 857 |
{ |
| 858 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(job)); |
| 859 |
g_free(job->display_name); |
| 860 |
job->display_name = g_strdup(name); |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* fm_file_ops_job_set_icon |
| 865 |
* @job: a job to set |
| 866 |
* @icon: new icon |
| 867 |
* |
| 868 |
* Sets that files for file operation FM_FILE_OP_CHANGE_ATTR should have |
| 869 |
* associated icon changed according to @icon. Error will be generated |
| 870 |
* if some of the files doesn't support icon change. |
| 871 |
* |
| 872 |
* This API may be used only before @job is started. |
| 873 |
* |
| 874 |
* Since: 1.2.0 |
| 875 |
*/ |
| 876 |
void fm_file_ops_job_set_icon(FmFileOpsJob *job, GIcon *icon) |
| 877 |
{ |
| 878 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(job)); |
| 879 |
if (G_UNLIKELY(job->icon)) |
| 880 |
g_free(job->icon); |
| 881 |
job->icon = NULL; |
| 882 |
if (G_LIKELY(icon)) |
| 883 |
job->icon = g_object_ref(icon); |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
* fm_file_ops_job_set_hidden |
| 888 |
* @job: a job to set |
| 889 |
* @hidden: new hidden attribute |
| 890 |
* |
| 891 |
* Sets that files for file operation FM_FILE_OP_CHANGE_ATTR should have |
| 892 |
* 'hidden' attribute changed according to value @hidden. Error will be |
| 893 |
* generated if some of the files doesn't support such change. |
| 894 |
* |
| 895 |
* This API may be used only before @job is started. |
| 896 |
* |
| 897 |
* Since: 1.2.0 |
| 898 |
*/ |
| 899 |
void fm_file_ops_job_set_hidden(FmFileOpsJob *job, gboolean hidden) |
| 900 |
{ |
| 901 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(job)); |
| 902 |
job->set_hidden = hidden ? 1 : 0; |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* fm_file_ops_job_set_target |
| 907 |
* @job: a job to set |
| 908 |
* @url: new URL for shortcut |
| 909 |
* |
| 910 |
* Sets that shortcut file for file operation FM_FILE_OP_CHANGE_ATTR |
| 911 |
* should have its target URL changed according to @url. The job will |
| 912 |
* fail if it will be started for more than one file or if file doesn't |
| 913 |
* support target change. |
| 914 |
* |
| 915 |
* This API may be used only before @job is started. |
| 916 |
* |
| 917 |
* Since: 1.2.0 |
| 918 |
*/ |
| 919 |
void fm_file_ops_job_set_target(FmFileOpsJob *job, const char *url) |
| 920 |
{ |
| 921 |
g_return_if_fail(FM_IS_FILE_OPS_JOB(job)); |
| 922 |
g_free(job->target); |
| 923 |
job->target = g_strdup(url); |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
* fm_file_ops_job_get_options |
| 928 |
* @job: a job to set |
| 929 |
* |
| 930 |
* Retrieves bitmask set of options that are supported as return of the |
| 931 |
* #FmFileOpsJob::ask-rename signal handler. |
| 932 |
* |
| 933 |
* Returns: list of options. |
| 934 |
* |
| 935 |
* Since: 1.2.0 |
| 936 |
*/ |
| 937 |
FmFileOpOption fm_file_ops_job_get_options(FmFileOpsJob* job) |
| 938 |
{ |
| 939 |
g_return_val_if_fail(FM_IS_FILE_OPS_JOB(job), 0); |
| 940 |
return job->supported_options; |
| 941 |
} |