Blender V5.0
folder_history.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "BLI_listbase.h"
14#include "BLI_path_utils.hh"
15#include "BLI_string.h"
16
17#include "DNA_space_types.h"
18
19#include "ED_fileselect.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "file_intern.hh"
24
25/* -------------------------------------------------------------------- */
28
33
34void folderlist_popdir(ListBase *folderlist, char *dir)
35{
36 const char *prev_dir;
37 FolderList *folder;
38 folder = static_cast<FolderList *>(folderlist->last);
39
40 if (folder) {
41 /* remove the current directory */
42 MEM_freeN(folder->foldername);
43 BLI_freelinkN(folderlist, folder);
44
45 folder = static_cast<FolderList *>(folderlist->last);
46 if (folder) {
47 prev_dir = folder->foldername;
48 BLI_strncpy(dir, prev_dir, FILE_MAXDIR);
49 }
50 }
51 /* Delete the folder next or use set-directory directly before PREVIOUS OP. */
52}
53
54void folderlist_pushdir(ListBase *folderlist, const char *dir)
55{
56 if (!dir[0]) {
57 return;
58 }
59
60 FolderList *folder, *previous_folder;
61 previous_folder = static_cast<FolderList *>(folderlist->last);
62
63 /* check if already exists */
64 if (previous_folder && previous_folder->foldername) {
65 if (BLI_path_cmp(previous_folder->foldername, dir) == 0) {
66 return;
67 }
68 }
69
70 /* create next folder element */
71 folder = MEM_callocN<FolderList>(__func__);
72 folder->foldername = BLI_strdup(dir);
73
74 /* add it to the end of the list */
75 BLI_addtail(folderlist, folder);
76}
77
78const char *folderlist_peeklastdir(ListBase *folderlist)
79{
80 FolderList *folder;
81
82 if (!folderlist->last) {
83 return nullptr;
84 }
85
86 folder = static_cast<FolderList *>(folderlist->last);
87 return folder->foldername;
88}
89
91{
93 FolderList *folder;
94
95 /* if there is no folder_next there is nothing we can clear */
97 return false;
98 }
99
100 /* if previous_folder, next_folder or refresh_folder operators are executed
101 * it doesn't clear folder_next */
102 folder = static_cast<FolderList *>(sfile->folders_prev->last);
103 if ((!folder) || (BLI_path_cmp(folder->foldername, params->dir) == 0)) {
104 return false;
105 }
106
107 /* eventually clear flist->folders_next */
108 return true;
109}
110
111void folderlist_free(ListBase *folderlist)
112{
113 if (folderlist) {
114 LISTBASE_FOREACH_MUTABLE (FolderList *, folder, folderlist) {
115 MEM_freeN(folder->foldername);
116 MEM_delete(folder);
117 }
118 BLI_listbase_clear(folderlist);
119 }
120}
121
123{
124 ListBase folderlistn = {nullptr};
125
126 BLI_duplicatelist(&folderlistn, folderlist);
127
128 LISTBASE_FOREACH (FolderList *, folder, &folderlistn) {
129 folder->foldername = (char *)MEM_dupallocN(folder->foldername);
130 }
131 return folderlistn;
132}
133
135
136/* -------------------------------------------------------------------- */
139
141{
143 if (history->browse_mode == browse_mode) {
144 return history;
145 }
146 }
147
148 return nullptr;
149}
150
152{
154
155 if (!history) {
156 history = MEM_callocN<FileFolderHistory>(__func__);
157 history->browse_mode = sfile->browse_mode;
158 BLI_addtail(&sfile->folder_histories, history);
159 }
160
161 sfile->folders_next = &history->folders_next;
162 sfile->folders_prev = &history->folders_prev;
163}
164
166{
167 if (sfile->folders_prev == &history->folders_prev) {
168 sfile->folders_prev = nullptr;
169 }
170 if (sfile->folders_next == &history->folders_next) {
171 sfile->folders_next = nullptr;
172 }
173 folderlist_free(&history->folders_prev);
174 folderlist_free(&history->folders_next);
175 BLI_freelinkN(&sfile->folder_histories, history);
176}
177
179{
181 folder_history_entry_free(sfile, history);
182 }
183}
184
186{
187 ListBase histories = {nullptr};
188
189 LISTBASE_FOREACH (FileFolderHistory *, history, listbase) {
190 FileFolderHistory *history_new = static_cast<FileFolderHistory *>(MEM_dupallocN(history));
191 history_new->folders_prev = folderlist_duplicate(&history->folders_prev);
192 history_new->folders_next = folderlist_duplicate(&history->folders_next);
193 BLI_addtail(&histories, history_new);
194 }
195
196 return histories;
197}
198
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
#define FILE_MAXDIR
#define BLI_path_cmp
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
eFileBrowse_Mode
FileSelectParams * ED_fileselect_get_active_params(const SpaceFile *sfile)
Definition filesel.cc:379
Read Guarded memory(de)allocation.
bool folderlist_clear_next(SpaceFile *sfile)
const char * folderlist_peeklastdir(ListBase *folderlist)
void folder_history_list_ensure_for_active_browse_mode(SpaceFile *sfile)
void folder_history_list_free(SpaceFile *sfile)
void folderlist_pushdir(ListBase *folderlist, const char *dir)
ListBase folder_history_list_duplicate(ListBase *listbase)
void folderlist_free(ListBase *folderlist)
static FileFolderHistory * folder_history_find(const SpaceFile *sfile, eFileBrowse_Mode browse_mode)
static ListBase folderlist_duplicate(ListBase *folderlist)
void folderlist_popdir(ListBase *folderlist, char *dir)
static void folder_history_entry_free(SpaceFile *sfile, FileFolderHistory *history)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
FolderList * next
char * foldername
FolderList * prev
void * last
ListBase * folders_prev
ListBase * folders_next
ListBase folder_histories