FileBrowser Bookmarks: fix issue with invalid bookmarks.
authorBastien Montagne <montagne29@wanadoo.fr>
Wed, 11 Feb 2015 16:07:52 +0000 (17:07 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Wed, 11 Feb 2015 16:13:16 +0000 (17:13 +0100)
Reported by maxon through IRC, thanks.

Invalid (inexistant) bookmarks would not be selectable, hence not removable.

First, made invalid bookmarks grayed out in lists, so that user knows when there are some.

Then, added a new 'cleanup' operator that removes all invalid bookmarks.

This solution may not be completely satisfaying, but should do the work for now.
I do not want to add back those ugly 'X' delete buttons for each entry in list,
so better solution would be to make UIList able to select several items at once...

release/scripts/startup/bl_ui/space_filebrowser.py
source/blender/editors/space_file/file_intern.h
source/blender/editors/space_file/file_ops.c
source/blender/editors/space_file/space_file.c
source/blender/makesrna/intern/rna_space.c

index cda3dfe499d74a6e1e657afbc50f552e7d4a583c..ded307e868040fd15b2619077cb0a12ad67786f9 100644 (file)
@@ -98,7 +98,12 @@ class FILEBROWSER_UL_dir(bpy.types.UIList):
 
         if self.layout_type in {'DEFAULT', 'COMPACT'}:
             row = layout.row(align=True)
-            row.prop(direntry, "name", text="", emboss=False, icon=icon)
+            row.enabled = direntry.is_valid
+            # Non-editable entries would show grayed-out, which is bad in this specific case, so switch to mere label.
+            if direntry.is_property_readonly('name'):
+                row.label(text=direntry.name, icon=icon)
+            else:
+                row.prop(direntry, "name", text="", emboss=False, icon=icon)
 
         elif self.layout_type in {'GRID'}:
             layout.alignment = 'CENTER'
@@ -146,7 +151,9 @@ class FILEBROWSER_MT_bookmarks_specials(Menu):
 
     def draw(self, context):
         layout = self.layout
+        layout.operator("file.bookmark_cleanup", icon='X', text="Cleanup")
 
+        layout.separator()
         layout.operator("file.bookmark_move", icon='TRIA_UP_BAR', text="Move To Top").direction = 'TOP'
         layout.operator("file.bookmark_move", icon='TRIA_DOWN_BAR', text="Move To Bottom").direction = 'BOTTOM'
 
index 31d479b4617ab3086a5e5e4aa4219b689eeece29..7425f2e0385574e0fd46fa3d01789f9ea182b9aa 100644 (file)
@@ -66,6 +66,7 @@ void FILE_OT_select_border(struct wmOperatorType *ot);
 void FILE_OT_select_bookmark(struct wmOperatorType *ot);
 void FILE_OT_bookmark_add(struct wmOperatorType *ot);
 void FILE_OT_bookmark_delete(struct wmOperatorType *ot);
+void FILE_OT_bookmark_cleanup(struct wmOperatorType *ot);
 void FILE_OT_bookmark_move(struct wmOperatorType *ot);
 void FILE_OT_reset_recent(wmOperatorType *ot);
 void FILE_OT_hidedot(struct wmOperatorType *ot);
index a0e312e72efc810cb7ab6d8660a975967b420782..ddf9545289e03a735ac741192b077e5465d1b287 100644 (file)
@@ -573,6 +573,52 @@ void FILE_OT_bookmark_delete(wmOperatorType *ot)
        RNA_def_property_flag(prop, PROP_SKIP_SAVE);
 }
 
+static int bookmark_cleanup_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       ScrArea *sa = CTX_wm_area(C);
+       struct FSMenu *fsmenu = ED_fsmenu_get();
+       struct FSMenuEntry *fsme_next, *fsme = ED_fsmenu_get_category(fsmenu, FS_CATEGORY_BOOKMARKS);
+       int index;
+       bool changed = false;
+
+       for (index = 0; fsme; fsme = fsme_next) {
+               fsme_next = fsme->next;
+
+               if (!BLI_is_dir(fsme->path)) {
+                       fsmenu_remove_entry(fsmenu, FS_CATEGORY_BOOKMARKS, index);
+                       changed = true;
+               }
+               else {
+                       index++;
+               }
+       }
+
+       if (changed) {
+               char name[FILE_MAX];
+
+               BLI_make_file_string("/", name, BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL), BLENDER_BOOKMARK_FILE);
+               fsmenu_write_file(fsmenu, name);
+               ED_area_tag_refresh(sa);
+               ED_area_tag_redraw(sa);
+       }
+
+       return OPERATOR_FINISHED;
+}
+
+void FILE_OT_bookmark_cleanup(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Cleanup Bookmarks";
+       ot->description = "Delete all invalid bookmarks";
+       ot->idname = "FILE_OT_bookmark_cleanup";
+
+       /* api callbacks */
+       ot->exec = bookmark_cleanup_exec;
+       ot->poll = ED_operator_file_active;
+
+       /* properties */
+}
+
 enum {
        FILE_BOOKMARK_MOVE_TOP = -2,
        FILE_BOOKMARK_MOVE_UP = -1,
index 3c60233d0a933febd27220643e3ec88b8bc1e34d..fe861fa1dcad11056ecc5589fe664099d6adf297 100644 (file)
@@ -404,6 +404,7 @@ static void file_operatortypes(void)
        WM_operatortype_append(FILE_OT_bookmark_toggle);
        WM_operatortype_append(FILE_OT_bookmark_add);
        WM_operatortype_append(FILE_OT_bookmark_delete);
+       WM_operatortype_append(FILE_OT_bookmark_cleanup);
        WM_operatortype_append(FILE_OT_bookmark_move);
        WM_operatortype_append(FILE_OT_reset_recent);
        WM_operatortype_append(FILE_OT_hidedot);
index fc2b2d72ac9f8156f240f3546283e0beb040a793..70b9f18df8c4bcdebdae7c751f3912f62fda84a6 100644 (file)
@@ -1422,6 +1422,13 @@ static int rna_FileBrowser_FSMenuEntry_name_get_editable(PointerRNA *ptr)
        return fsm->save;
 }
 
+static int rna_FileBrowser_FSMenuEntry_is_valid_get(PointerRNA *ptr)
+{
+       char *path = ED_fsmenu_entry_get_path(ptr->data);
+
+       return path ? BLI_is_dir(path) : false;  /* For now, no path = invalid. */
+}
+
 static void rna_FileBrowser_FSMenu_next(CollectionPropertyIterator *iter)
 {
        ListBaseIterator *internal = &iter->internal.listbase;
@@ -3629,6 +3636,11 @@ static void rna_def_filemenu_entry(BlenderRNA *brna)
        RNA_def_property_boolean_sdna(prop, NULL, "save", 1);
        RNA_def_property_ui_text(prop, "Save", "Whether this path is saved in bookmarks, or generated from OS");
        RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+
+       prop = RNA_def_property(srna, "is_valid", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_funcs(prop, "rna_FileBrowser_FSMenuEntry_is_valid_get", NULL);
+       RNA_def_property_ui_text(prop, "Save", "Whether this path is saved in bookmarks, or generated from OS");
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 }
 
 static void rna_def_space_filebrowser(BlenderRNA *brna)