| 1 |
/* |
| 2 |
* fm-file-info-job.c |
| 3 |
* |
| 4 |
* Copyright 2009 PCMan <[email protected]> |
| 5 |
* Copyright 2013-2014 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-info-job |
| 26 |
* @short_description: Job to gather information about files. |
| 27 |
* @title: FmFileInfoJob |
| 28 |
* |
| 29 |
* @include: libfm/fm.h |
| 30 |
* |
| 31 |
* The #FmFileInfoJob can be used to get filled #FmFileInfo for some files. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "fm-file-info-job.h" |
| 35 |
|
| 36 |
#include <sys/types.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <string.h> |
| 40 |
#include <errno.h> |
| 41 |
|
| 42 |
#include "fm-file-info.h" |
| 43 |
|
| 44 |
static void fm_file_info_job_dispose (GObject *object); |
| 45 |
static gboolean fm_file_info_job_run(FmJob* fmjob); |
| 46 |
|
| 47 |
const char gfile_info_query_attribs[]="standard::*,unix::*,time::*,access::*,id::filesystem,metadata::emblems"; |
| 48 |
|
| 49 |
enum { |
| 50 |
GOT_INFO, |
| 51 |
N_SIGNALS |
| 52 |
}; |
| 53 |
|
| 54 |
static int signals[N_SIGNALS]; |
| 55 |
|
| 56 |
G_DEFINE_TYPE(FmFileInfoJob, fm_file_info_job, FM_TYPE_JOB); |
| 57 |
|
| 58 |
static void fm_file_info_job_class_init(FmFileInfoJobClass *klass) |
| 59 |
{ |
| 60 |
GObjectClass *g_object_class; |
| 61 |
FmJobClass* job_class; |
| 62 |
|
| 63 |
g_object_class = G_OBJECT_CLASS(klass); |
| 64 |
g_object_class->dispose = fm_file_info_job_dispose; |
| 65 |
/* use finalize from parent class */ |
| 66 |
|
| 67 |
job_class = FM_JOB_CLASS(klass); |
| 68 |
job_class->run = fm_file_info_job_run; |
| 69 |
|
| 70 |
/** |
| 71 |
* FmDirInfoJob::got-info |
| 72 |
* @job: a job that emitted the signal |
| 73 |
* @info: #FmFileInfo of file which info is completed |
| 74 |
* |
| 75 |
* The #FmDirInfoJob::got-info signal is emitted for every file info |
| 76 |
* during a job with FM_FILE_INFO_JOB_EMIT_FOR_EACH_FILE flag set. |
| 77 |
* This signal may be emitted only if info retrieving was successful. |
| 78 |
* |
| 79 |
* Since: 1.2.0 |
| 80 |
*/ |
| 81 |
signals[GOT_INFO] = |
| 82 |
g_signal_new("got-info", |
| 83 |
G_TYPE_FROM_CLASS(klass), |
| 84 |
G_SIGNAL_RUN_LAST, |
| 85 |
G_STRUCT_OFFSET(FmFileInfoJobClass, got_info), |
| 86 |
NULL, NULL, |
| 87 |
g_cclosure_marshal_VOID__POINTER, |
| 88 |
G_TYPE_NONE, 1, G_TYPE_POINTER); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static void fm_file_info_job_dispose(GObject *object) |
| 93 |
{ |
| 94 |
FmFileInfoJob *self; |
| 95 |
|
| 96 |
g_return_if_fail(object != NULL); |
| 97 |
g_return_if_fail(FM_IS_FILE_INFO_JOB(object)); |
| 98 |
|
| 99 |
self = (FmFileInfoJob*)object; |
| 100 |
if(self->file_infos) |
| 101 |
{ |
| 102 |
fm_file_info_list_unref(self->file_infos); |
| 103 |
self->file_infos = NULL; |
| 104 |
} |
| 105 |
if(self->current) |
| 106 |
{ |
| 107 |
fm_path_unref(self->current); |
| 108 |
self->current = NULL; |
| 109 |
} |
| 110 |
|
| 111 |
G_OBJECT_CLASS(fm_file_info_job_parent_class)->dispose(object); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static void fm_file_info_job_init(FmFileInfoJob *self) |
| 116 |
{ |
| 117 |
self->file_infos = fm_file_info_list_new(); |
| 118 |
fm_job_init_cancellable(FM_JOB(self)); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* fm_file_info_job_new |
| 123 |
* @files_to_query: (allow-none): list of paths to query informatiom |
| 124 |
* @flags: modificators of query mode |
| 125 |
* |
| 126 |
* Creates a new #FmFileInfoJob which can be used by #FmJob API. |
| 127 |
* |
| 128 |
* Returns: (transfer full): a new #FmFileInfoJob object. |
| 129 |
* |
| 130 |
* Since: 0.1.0 |
| 131 |
*/ |
| 132 |
FmFileInfoJob* fm_file_info_job_new(FmPathList* files_to_query, FmFileInfoJobFlags flags) |
| 133 |
{ |
| 134 |
GList* l; |
| 135 |
FmFileInfoJob* job = (FmFileInfoJob*)g_object_new(FM_TYPE_FILE_INFO_JOB, NULL); |
| 136 |
FmFileInfoList* file_infos; |
| 137 |
|
| 138 |
job->flags = flags; |
| 139 |
if(files_to_query) |
| 140 |
{ |
| 141 |
file_infos = job->file_infos; |
| 142 |
for(l = fm_path_list_peek_head_link(files_to_query);l;l=l->next) |
| 143 |
{ |
| 144 |
FmPath* path = FM_PATH(l->data); |
| 145 |
FmFileInfo* fi = fm_file_info_new(); |
| 146 |
fm_file_info_set_path(fi, path); |
| 147 |
fm_file_info_list_push_tail_noref(file_infos, fi); |
| 148 |
} |
| 149 |
} |
| 150 |
return job; |
| 151 |
} |
| 152 |
|
| 153 |
static void _check_native_display_names(FmPath *path) |
| 154 |
{ |
| 155 |
char *path_str, *disp_name; |
| 156 |
|
| 157 |
if (path == NULL || _fm_path_get_display_name(path) != NULL) |
| 158 |
return; /* all done */ |
| 159 |
path_str = fm_path_to_str(path); |
| 160 |
disp_name = g_filename_display_basename(path_str); |
| 161 |
g_free(path_str); |
| 162 |
_fm_path_set_display_name(path, disp_name); |
| 163 |
g_free(disp_name); |
| 164 |
_check_native_display_names(fm_path_get_parent(path)); /* recursion */ |
| 165 |
} |
| 166 |
|
| 167 |
static void _check_gfile_display_names(FmPath *path, GFile *child) |
| 168 |
{ |
| 169 |
GFile *gf; |
| 170 |
GFileInfo *inf; |
| 171 |
|
| 172 |
if (path == NULL || _fm_path_get_display_name(path) != NULL) |
| 173 |
return; /* all done */ |
| 174 |
gf = g_file_get_parent(child); |
| 175 |
if (gf == NULL) /* file systems such as search:// don't support this */ |
| 176 |
return; |
| 177 |
inf = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME"," |
| 178 |
G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME, |
| 179 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL); |
| 180 |
if (inf != NULL) |
| 181 |
{ |
| 182 |
const char *dname = g_file_info_get_edit_name(inf); |
| 183 |
if (!dname || strcmp(dname, "/") == 0) |
| 184 |
dname = g_file_info_get_display_name(inf); |
| 185 |
_fm_path_set_display_name(path, dname); |
| 186 |
g_object_unref(inf); |
| 187 |
} |
| 188 |
_check_gfile_display_names(fm_path_get_parent(path), gf); /* recursion */ |
| 189 |
g_object_unref(gf); |
| 190 |
} |
| 191 |
|
| 192 |
static gpointer _emit_current_file(FmJob* job, gpointer user_data) |
| 193 |
{ |
| 194 |
/* this callback is called from the main thread */ |
| 195 |
g_signal_emit(job, signals[GOT_INFO], 0, user_data); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
static gboolean fm_file_info_job_run(FmJob* fmjob) |
| 200 |
{ |
| 201 |
GList* l; |
| 202 |
FmFileInfoJob* job = (FmFileInfoJob*)fmjob; |
| 203 |
GError* err = NULL; |
| 204 |
|
| 205 |
if(job->file_infos == NULL) |
| 206 |
return FALSE; |
| 207 |
|
| 208 |
for(l = fm_file_info_list_peek_head_link(job->file_infos); !fm_job_is_cancelled(fmjob) && l;) |
| 209 |
{ |
| 210 |
FmFileInfo* fi = (FmFileInfo*)l->data; |
| 211 |
GList* next = l->next; |
| 212 |
FmPath* path = fm_file_info_get_path(fi); |
| 213 |
|
| 214 |
if(job->current) |
| 215 |
fm_path_unref(job->current); |
| 216 |
job->current = fm_path_ref(path); |
| 217 |
|
| 218 |
if(fm_path_is_native(path)) |
| 219 |
{ |
| 220 |
char* path_str = fm_path_to_str(path); |
| 221 |
if(!_fm_file_info_job_get_info_for_native_file(fmjob, fi, path_str, &err)) |
| 222 |
{ |
| 223 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 224 |
g_error_free(err); |
| 225 |
err = NULL; |
| 226 |
if(act == FM_JOB_RETRY) |
| 227 |
{ |
| 228 |
g_free(path_str); |
| 229 |
continue; /* retry */ |
| 230 |
} |
| 231 |
|
| 232 |
fm_file_info_list_delete_link(job->file_infos, l); /* also calls unref */ |
| 233 |
} |
| 234 |
else if(G_UNLIKELY(job->flags & FM_FILE_INFO_JOB_EMIT_FOR_EACH_FILE)) |
| 235 |
fm_job_call_main_thread(fmjob, _emit_current_file, fi); |
| 236 |
g_free(path_str); |
| 237 |
/* recursively set display names for path parents */ |
| 238 |
_check_native_display_names(fm_path_get_parent(path)); |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
GFile* gf; |
| 243 |
|
| 244 |
gf = fm_path_to_gfile(path); |
| 245 |
if(!_fm_file_info_job_get_info_for_gfile(fmjob, fi, gf, &err)) |
| 246 |
{ |
| 247 |
if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_NOT_MOUNTED) |
| 248 |
{ |
| 249 |
GFileInfo *inf; |
| 250 |
/* location by link isn't mounted; unfortunately we cannot |
| 251 |
launch a target if we don't know what kind of target we |
| 252 |
have; lets make a simplest directory-kind GFIleInfo */ |
| 253 |
/* FIXME: this may be dirty a bit */ |
| 254 |
g_error_free(err); |
| 255 |
err = NULL; |
| 256 |
inf = g_file_info_new(); |
| 257 |
g_file_info_set_file_type(inf, G_FILE_TYPE_DIRECTORY); |
| 258 |
g_file_info_set_name(inf, fm_path_get_basename(path)); |
| 259 |
g_file_info_set_display_name(inf, fm_path_get_basename(path)); |
| 260 |
fm_file_info_set_from_g_file_data(fi, gf, inf); |
| 261 |
g_object_unref(inf); |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 266 |
g_error_free(err); |
| 267 |
err = NULL; |
| 268 |
if(act == FM_JOB_RETRY) |
| 269 |
{ |
| 270 |
g_object_unref(gf); |
| 271 |
continue; /* retry */ |
| 272 |
} |
| 273 |
|
| 274 |
fm_file_info_list_delete_link(job->file_infos, l); /* also calls unref */ |
| 275 |
goto _next; |
| 276 |
} |
| 277 |
} |
| 278 |
else if(G_UNLIKELY(job->flags & FM_FILE_INFO_JOB_EMIT_FOR_EACH_FILE)) |
| 279 |
fm_job_call_main_thread(fmjob, _emit_current_file, fi); |
| 280 |
/* recursively set display names for path parents */ |
| 281 |
_check_gfile_display_names(fm_path_get_parent(path), gf); |
| 282 |
_next: |
| 283 |
g_object_unref(gf); |
| 284 |
} |
| 285 |
l = next; |
| 286 |
} |
| 287 |
return TRUE; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* fm_file_info_job_add |
| 292 |
* @job: a job to add file |
| 293 |
* @path: a path to add to query list |
| 294 |
* |
| 295 |
* Adds a @path to query list for the @job. |
| 296 |
* |
| 297 |
* This API may only be called before starting the @job. |
| 298 |
* |
| 299 |
* Since: 0.1.0 |
| 300 |
*/ |
| 301 |
void fm_file_info_job_add(FmFileInfoJob* job, FmPath* path) |
| 302 |
{ |
| 303 |
FmFileInfo* fi = fm_file_info_new(); |
| 304 |
fm_file_info_set_path(fi, path); |
| 305 |
fm_file_info_list_push_tail_noref(job->file_infos, fi); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* fm_file_info_job_add_gfile |
| 310 |
* @job: a job to add file |
| 311 |
* @gf: a file descriptor to add to query list |
| 312 |
* |
| 313 |
* Adds a path @gf to query list for the @job. |
| 314 |
* |
| 315 |
* This API may only be called before starting the @job. |
| 316 |
* |
| 317 |
* Since: 0.1.0 |
| 318 |
*/ |
| 319 |
void fm_file_info_job_add_gfile(FmFileInfoJob* job, GFile* gf) |
| 320 |
{ |
| 321 |
FmPath* path = fm_path_new_for_gfile(gf); |
| 322 |
FmFileInfo* fi = fm_file_info_new(); |
| 323 |
fm_file_info_set_path(fi, path); |
| 324 |
fm_path_unref(path); |
| 325 |
fm_file_info_list_push_tail_noref(job->file_infos, fi); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* fm_file_info_job_get_current |
| 330 |
* @job: the job to inspect |
| 331 |
* |
| 332 |
* Retrieves current the #FmPath which caused the error. |
| 333 |
* Returned data are owned by @job and shouldn't be freed by caller. |
| 334 |
* |
| 335 |
* This API may only be called in error handler. |
| 336 |
* |
| 337 |
* Returns: (transfer none): the current processing file path. |
| 338 |
* |
| 339 |
* Since: 0.1.10 |
| 340 |
*/ |
| 341 |
FmPath* fm_file_info_job_get_current(FmFileInfoJob* job) |
| 342 |
{ |
| 343 |
return job->current; |
| 344 |
} |