1 #include "e_fm_device.h"
2 #include "e_fm_shared_codec.h"
3 #include "e_fm_shared_device.h"
5 #define TEBIBYTE_SIZE 1099511627776LL
6 #define GIBIBYTE_SIZE 1073741824
7 #define MEBIBYTE_SIZE 1048576
8 #define KIBIBYTE_SIZE 1024
10 static void _e_fm2_volume_write(E_Volume *v) EINA_ARG_NONNULL(1);
11 static void _e_fm2_volume_erase(E_Volume *v) EINA_ARG_NONNULL(1);
12 static void _e_fm2_device_mount_free(E_Fm2_Mount *m); EINA_ARG_NONNULL(1);
13 static void _e_fm2_device_mount_ok(E_Fm2_Mount *m) EINA_ARG_NONNULL(1);
14 static void _e_fm2_device_mount_fail(E_Fm2_Mount *m) EINA_ARG_NONNULL(1);
15 static void _e_fm2_device_unmount_ok(E_Fm2_Mount *m) EINA_ARG_NONNULL(1);
16 static void _e_fm2_device_unmount_fail(E_Fm2_Mount *m) EINA_ARG_NONNULL(1);
18 static Eina_List *_e_stores = NULL;
19 static Eina_List *_e_vols = NULL;
22 _e_fm2_device_volume_setup(E_Volume *v)
24 char label[1024] = {0};
26 const char *icon = NULL;
27 unsigned long long sz;
29 /* Compute the size in a readable form */
32 if ((sz = (v->size / TEBIBYTE_SIZE)) > 0)
33 snprintf(size, sizeof(size) - 1, _("%llu TiB"), sz);
34 else if ((sz = (v->size / GIBIBYTE_SIZE)) > 0)
35 snprintf(size, sizeof(size) - 1, _("%llu GiB"), sz);
36 else if ((sz = (v->size / MEBIBYTE_SIZE)) > 0)
37 snprintf(size, sizeof(size) - 1, _("%llu MiB"), sz);
38 else if ((sz = (v->size / KIBIBYTE_SIZE)) > 0)
39 snprintf(size, sizeof(size) - 1, _("%llu KiB"), sz);
41 snprintf(size, sizeof(size) - 1, _("%llu B"), v->size);
44 /* Choose the label */
45 if ((v->label) && (v->label[0]))
47 else if ((v->partition_label) && (v->partition_label[0]))
48 snprintf(label, sizeof(label) - 1, "%s", v->partition_label);
49 else if (((v->storage->vendor) && (v->storage->vendor[0])) &&
50 ((v->storage->model) && (v->storage->model[0])))
53 snprintf(label, sizeof(label) - 1, "%s %s - %s", v->storage->vendor, v->storage->model, size);
55 snprintf(label, sizeof(label) - 1, "%s %s", v->storage->vendor, v->storage->model);
57 else if ((v->storage->model) && (v->storage->model[0]))
60 snprintf(label, sizeof(label) - 1, "%s - %s", v->storage->model, size);
62 snprintf(label, sizeof(label) - 1, "%s", v->storage->model);
64 else if ((v->storage->vendor) && (v->storage->vendor[0]))
67 snprintf(label, sizeof(label) - 1, "%s - %s", v->storage->vendor, size);
69 snprintf(label, sizeof(label) - 1, "%s", v->storage->vendor);
72 snprintf(label, sizeof(label), _("Unknown Volume"));
74 if (label[0]) eina_stringshare_replace(&v->label, label);
77 /* http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html */
78 if (v->storage->icon.volume)
79 icon = v->storage->icon.volume;
82 if ((!v->storage->drive_type) || (!strcmp(v->storage->drive_type, "disk")))
84 if (v->storage->removable == 0)
85 icon = "drive-harddisk";
87 icon = "drive-removable-media";
89 else if ((!strcmp(v->storage->drive_type, "cdrom")) || (!strncmp(v->storage->drive_type, "optical", 7)))
90 icon = "drive-optical";
91 else if (!strncmp(v->storage->drive_type, "floppy", 6))
92 icon = "media-floppy";
93 else if (!strcmp(v->storage->drive_type, "tape"))
95 else if (!strcmp(v->storage->drive_type, "compact_flash")
96 || !strcmp(v->storage->drive_type, "memory_stick")
97 || !strcmp(v->storage->drive_type, "smart_media")
98 || !strcmp(v->storage->drive_type, "sd_mmc")
99 || !strncmp(v->storage->drive_type, "flash", 5))
100 icon = "media-flash";
102 if (icon) eina_stringshare_replace(&v->icon, icon);
104 if ((!v->mount_point) ||
105 (strcmp(v->mount_point, "/") &&
106 strcmp(v->mount_point, "/home") &&
107 strcmp(v->mount_point, "/tmp")))
108 _e_fm2_volume_write(v);
112 e_fm2_device_storage_add(E_Storage *s)
117 if (e_fm2_device_storage_find(s->udi)) return;
119 s->validated = EINA_TRUE;
120 _e_stores = eina_list_append(_e_stores, s);
130 " media_available: %i\n"
131 " media_size: %lli\n"
132 " requires_eject: %i\n"
133 " hotpluggable: %i\n"
134 " media_check_enabled: %i\n"
136 " icon.volume: %s\n\n"
149 s->media_check_enabled,
153 if ((s->removable == 0) &&
154 (s->media_available == 0) &&
155 (s->media_size == 0) &&
156 (s->requires_eject == 0) &&
157 (s->hotpluggable == 0) &&
158 (s->media_check_enabled == 0))
160 // printf(" Ignore this storage\n");
164 s->trackable = EINA_TRUE;
167 EINA_LIST_FOREACH(_e_vols, l, v) /* catch volumes which were added before their storage */
169 if ((!v->storage) && (s->udi == v->parent))
172 _e_fm2_device_volume_setup(v);
178 e_fm2_device_storage_del(E_Storage *s)
180 // printf("STO- %s\n", s->udi);
181 _e_stores = eina_list_remove(_e_stores, s);
182 _e_fm_shared_device_storage_free(s);
186 e_fm2_device_storage_find(const char *udi)
191 if (!udi) return NULL;
192 EINA_LIST_FOREACH(_e_stores, l, s)
194 if (!strcmp(udi, s->udi)) return s;
201 e_fm2_device_volume_add(E_Volume *v)
205 if (e_fm2_device_volume_find(v->udi)) return;
207 v->validated = EINA_TRUE;
208 _e_vols = eina_list_append(_e_vols, v);
217 " partition_number: %d\n"
218 " partition_label: %s\n"
230 v->partition ? v->partition_label : "(not a partition)",
235 /* Check mount point */
236 if ((v->efm_mode == EFM_MODE_USING_HAL_MOUNT) &&
237 ((!v->mount_point) || (!v->mount_point[0])))
239 if (v->mount_point) eina_stringshare_del(v->mount_point);
240 v->mount_point = NULL;
241 v->mount_point = e_fm2_device_volume_mountpoint_get(v);
242 if ((!v->mount_point) || (!v->mount_point[0]))
247 if (v->mount_point) eina_stringshare_del(v->mount_point);
248 v->mount_point = NULL;
250 if ((v->uuid) && (v->uuid[0])) id = v->uuid;
251 if (ecore_file_is_dir("/media"))
252 snprintf(buf, sizeof(buf), "/media/%s", id);
253 else if (ecore_file_is_dir("/mnt"))
254 snprintf(buf, sizeof(buf), "/mnt/%s", id);
255 else if (ecore_file_is_dir("/tmp"))
256 snprintf(buf, sizeof(buf), "/tmp/%s", id);
259 v->mount_point = eina_stringshare_add(buf);
263 /* Search parent storage */
264 if ((s = e_fm2_device_storage_find(v->parent)))
267 s->volumes = eina_list_append(s->volumes, v);
270 if (v->storage) _e_fm2_device_volume_setup(v);
275 e_fm2_device_volume_del(E_Volume *v)
279 // printf("VOL- %s\n", v->udi);
280 _e_vols = eina_list_remove(_e_vols, v);
281 _e_fm2_volume_erase(v);
284 v->storage->volumes = eina_list_remove(v->storage->volumes, v);
287 EINA_LIST_FREE(v->mounts, m)
289 _e_fm2_device_unmount_ok(m);
290 _e_fm2_device_mount_free(m);
292 _e_fm_shared_device_volume_free(v);
296 _e_fm2_volume_write(E_Volume *v)
298 char buf[PATH_MAX], buf2[PATH_MAX];
302 if (!v->storage) return;
303 id = ecore_file_file_get(v->storage->udi);
304 // printf("vol write %s\n", id);
305 e_user_dir_snprintf(buf, sizeof(buf), "fileman/favorites/|%s_%d.desktop",
306 id, v->partition_number);
315 "X-Enlightenment-Type=Removable\n"
316 "X-Enlightenment-Removable-State=Full\n"
324 _("Removable Device"),
328 if (e_config->device_desktop)
330 e_user_homedir_snprintf(buf2, sizeof(buf2),
332 _("Desktop"), id, v->partition_number);
333 ecore_file_symlink(buf, buf2);
336 /* FIXME: manipulate icon directly */
337 _e_fm2_file_force_update(buf);
338 //_e_fm2_file_force_update(buf2);
348 _e_fm2_volume_erase(E_Volume *v)
350 char buf[PATH_MAX] = {0};
353 if (!v->storage) return;
354 id = ecore_file_file_get(v->storage->udi);
355 e_user_homedir_snprintf(buf, sizeof(buf), "%s/|%s_%d.desktop",
356 _("Desktop"), id, v->partition_number);
357 ecore_file_unlink(buf);
358 _e_fm2_file_force_update(buf);
360 if (e_config->device_desktop)
362 e_user_dir_snprintf(buf, sizeof(buf),
363 "fileman/favorites/|%s_%d.desktop",
364 id, v->partition_number);
365 ecore_file_unlink(buf);
366 _e_fm2_file_force_update(buf);
371 e_fm2_device_volume_find(const char *udi)
376 if (!udi) return NULL;
378 EINA_LIST_FOREACH(_e_vols, l, v)
380 if (!v->udi) continue;
381 if (!strcmp(udi, v->udi)) return v;
388 e_fm2_device_volume_mountpoint_get(E_Volume *v)
390 char buf[PATH_MAX] = {0};
395 // printf("GET MOUNTPOINT = %s\n", v->mount_point);
396 return eina_stringshare_add(v->mount_point);
398 else if (v->efm_mode != EFM_MODE_USING_HAL_MOUNT)
401 if (v->label && v->label[0] != '\0')
402 snprintf(buf, sizeof(buf) - 1, "/media/%s", v->label);
403 else if (v->uuid && v->uuid[0] != '\0')
404 snprintf(buf, sizeof(buf) - 1, "/media/%s", v->uuid);
405 else if ((v->storage) && (v->storage->serial) && v->storage->serial[0] != '\0')
406 snprintf(buf, sizeof(buf) - 1, "/media/%s", v->storage->serial);
409 static int mount_count = 1;
410 snprintf(buf, sizeof(buf) - 1, "/media/unknown-%i", mount_count++);
412 // printf("GET MOUNTPOINT = %s\n", buf);
413 return eina_stringshare_add(buf);
417 e_fm2_device_mount_add(E_Volume *v,
418 const char *mountpoint)
423 v->mounted = EINA_TRUE;
424 if (mountpoint && (mountpoint[0]))
425 eina_stringshare_replace(&v->mount_point, mountpoint);
427 EINA_LIST_FOREACH(v->mounts, l, m)
428 _e_fm2_device_mount_ok(m);
430 // printf("MOUNT %s %s\n", v->udi, v->mount_point);
434 e_fm2_device_mount_del(E_Volume *v)
438 v->mounted = EINA_FALSE;
441 eina_stringshare_del(v->mount_point);
442 v->mount_point = NULL;
445 EINA_LIST_FREE(v->mounts, m)
447 _e_fm2_device_unmount_ok(m);
448 _e_fm2_device_mount_free(m);
453 _e_fm2_device_mount_free(E_Fm2_Mount *m)
457 if (m->udi) eina_stringshare_del(m->udi);
458 if (m->mount_point) eina_stringshare_del(m->mount_point);
464 e_fm2_device_mount_find(const char *path)
469 EINA_LIST_FOREACH(_e_vols, l, v)
473 && !strncmp(path, v->mount_point, strlen(v->mount_point))
475 return eina_list_data_get(v->mounts);
481 e_fm2_device_mount(E_Volume *v,
485 Ecore_Cb unmount_fail,
492 m = calloc(1, sizeof(E_Fm2_Mount));
494 m->udi = eina_stringshare_add(v->udi);
495 m->mount_ok = mount_ok;
496 m->mount_fail = mount_fail;
497 m->unmount_ok = unmount_ok;
498 m->unmount_fail = unmount_fail;
501 m->mounted = v->mounted;
503 v->mounts = eina_list_prepend(v->mounts, m);
505 // printf("BEGIN MOUNT %p '%s'\n", m, v->mount_point);
509 v->auto_unmount = EINA_TRUE;
510 _e_fm2_client_mount(v->udi, v->mount_point);
514 v->auto_unmount = EINA_FALSE;
515 m->mount_point = eina_stringshare_add(v->mount_point);
522 e_fm2_device_mount_fail(E_Volume *v)
526 v->mounted = EINA_FALSE;
529 eina_stringshare_del(v->mount_point);
530 v->mount_point = NULL;
533 EINA_LIST_FREE(v->mounts, m)
535 _e_fm2_device_mount_fail(m);
536 _e_fm2_device_mount_free(m);
541 e_fm2_device_unmount(E_Fm2_Mount *m)
545 if (!(v = m->volume)) return;
546 v->mounts = eina_list_remove(v->mounts, m);
547 _e_fm2_device_mount_free(m);
549 if (v->auto_unmount && v->mounted && !eina_list_count(v->mounts))
550 _e_fm2_client_unmount(v->udi);
554 e_fm2_device_unmount_fail(E_Volume *v)
559 v->mounted = EINA_TRUE;
561 EINA_LIST_FOREACH(v->mounts, l, m)
562 _e_fm2_device_unmount_fail(m);
566 _e_fm2_device_mount_ok(E_Fm2_Mount *m)
568 if (m->mounted) return;
569 m->mounted = EINA_TRUE;
571 m->mount_point = eina_stringshare_add(m->volume->mount_point);
573 m->mount_ok(m->data);
574 // printf("MOUNT OK '%s'\n", m->mount_point);
578 _e_fm2_device_mount_fail(E_Fm2_Mount *m)
580 m->mounted = EINA_FALSE;
583 eina_stringshare_del(m->mount_point);
584 m->mount_point = NULL;
587 m->mount_fail(m->data);
591 _e_fm2_device_unmount_ok(E_Fm2_Mount *m)
593 if (!m->mounted) return;
594 m->mounted = EINA_FALSE;
597 eina_stringshare_del(m->mount_point);
598 m->mount_point = NULL;
601 m->unmount_ok(m->data);
605 _e_fm2_device_unmount_fail(E_Fm2_Mount *m)
607 if (m->mounted) return;
608 m->mounted = EINA_TRUE;
610 m->unmount_fail(m->data);
614 e_fm2_device_show_desktop_icons(void)
618 char buf[PATH_MAX] = {0};
619 char buf2[PATH_MAX] = {0};
622 EINA_LIST_FOREACH(_e_vols, l, v)
625 if (!v->storage) continue;
627 id = ecore_file_file_get(v->storage->udi);
629 e_user_dir_snprintf(buf, sizeof(buf),
630 "fileman/favorites/|%s_%d.desktop",
631 id, v->partition_number);
633 e_user_homedir_snprintf(buf2, sizeof(buf2),
635 _("Desktop"), id, v->partition_number);
637 if (ecore_file_exists(buf) && !ecore_file_exists(buf2))
639 ecore_file_symlink(buf, buf2);
640 _e_fm2_file_force_update(buf2);
646 e_fm2_device_hide_desktop_icons(void)
650 char buf[PATH_MAX] = {0};
653 EINA_LIST_FOREACH(_e_vols, l, v)
656 if (!v->storage) continue;
658 id = ecore_file_file_get(v->storage->udi);
660 e_user_homedir_snprintf(buf, sizeof(buf),
662 _("Desktop"), id, v->partition_number);
664 if (ecore_file_exists(buf))
666 ecore_file_unlink(buf);
667 _e_fm2_file_force_update(buf);
673 e_fm2_device_volume_list_get(void)